list.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package list
  2. import (
  3. "fmt"
  4. "github.com/Jinnrry/pmail/db"
  5. "github.com/Jinnrry/pmail/dto"
  6. "github.com/Jinnrry/pmail/dto/response"
  7. "github.com/Jinnrry/pmail/models"
  8. "github.com/Jinnrry/pmail/utils/context"
  9. log "github.com/sirupsen/logrus"
  10. "strings"
  11. )
  12. import . "xorm.io/builder"
  13. func GetEmailList(ctx *context.Context, tagInfo dto.SearchTag, keyword string, pop3List bool, offset, limit int) (emailList []*response.EmailResponseData, total int64) {
  14. return getList(ctx, tagInfo, keyword, pop3List, offset, limit)
  15. }
  16. func getList(ctx *context.Context, tagInfo dto.SearchTag, keyword string, pop3List bool, offset, limit int) (emailList []*response.EmailResponseData, total int64) {
  17. querySQL, queryParams := genSQL(ctx, false, tagInfo, keyword, pop3List, offset, limit)
  18. err := db.Instance.SQL(querySQL, queryParams...).Find(&emailList)
  19. if err != nil {
  20. log.WithContext(ctx).Errorf("SQL ERROR: %s ,Error:%s", querySQL, err)
  21. }
  22. totalSQL, totalParams := genSQL(ctx, true, tagInfo, keyword, pop3List, offset, limit)
  23. _, err = db.Instance.SQL(totalSQL, totalParams...).Get(&total)
  24. if err != nil {
  25. log.WithContext(ctx).Errorf("SQL ERROR: %s ,Error:%s", querySQL, err)
  26. }
  27. return emailList, total
  28. }
  29. func genSQL(ctx *context.Context, count bool, tagInfo dto.SearchTag, keyword string, pop3List bool, offset, limit int) (string, []any) {
  30. sqlParams := []any{ctx.UserID}
  31. sql := "select "
  32. if count {
  33. sql += `count(1) from email e left join user_email ue on e.id=ue.email_id where ue.user_id = ? `
  34. } else if pop3List {
  35. sql += `e.id,e.size from email e left join user_email ue on e.id=ue.email_id where ue.user_id = ? `
  36. } else {
  37. sql += `e.*,ue.is_read from email e left join user_email ue on e.id=ue.email_id where ue.user_id = ? `
  38. }
  39. if tagInfo.Status != -1 {
  40. sql += " and ue.status =? "
  41. sqlParams = append(sqlParams, tagInfo.Status)
  42. } else {
  43. sql += " and ue.status != 3"
  44. }
  45. if tagInfo.Type != -1 {
  46. sql += " and type =? "
  47. sqlParams = append(sqlParams, tagInfo.Type)
  48. }
  49. if tagInfo.GroupId != -1 {
  50. sql += " and ue.group_id=? "
  51. sqlParams = append(sqlParams, tagInfo.GroupId)
  52. }
  53. if keyword != "" {
  54. sql += " and (subject like ? or text like ? )"
  55. sqlParams = append(sqlParams, "%"+keyword+"%", "%"+keyword+"%")
  56. }
  57. if limit == 0 {
  58. limit = 10
  59. }
  60. sql += " order by e.id desc"
  61. if limit < 10000 {
  62. sql += fmt.Sprintf(" LIMIT %d OFFSET %d ", limit, offset)
  63. }
  64. return sql, sqlParams
  65. }
  66. type statRes struct {
  67. Total int64
  68. Size int64
  69. }
  70. // Stat 查询邮件总数和大小
  71. func Stat(ctx *context.Context) (int64, int64) {
  72. sql := `select count(1) as total,sum(size) as size from email e left join user_email ue on e.id=ue.email_id where ue.user_id = ? and e.type = 0 and ue.status != 3`
  73. var ret statRes
  74. _, err := db.Instance.SQL(sql, ctx.UserID).Get(&ret)
  75. if err != nil {
  76. log.WithContext(ctx).Errorf("SQL ERROR: %s ,Error:%s", sql, err)
  77. }
  78. return ret.Total, ret.Size
  79. }
  80. func GetEmailListByGroup(ctx *context.Context, groupName string, offset, limit int) []*response.EmailResponseData {
  81. if limit == 0 {
  82. limit = 1
  83. }
  84. var ret []*response.EmailResponseData
  85. var ue []*models.UserEmail
  86. switch groupName {
  87. case "INBOX":
  88. db.Instance.Table("user_email").Select("email_id,is_read").Where("user_id=? and status=0", ctx.UserID).Limit(limit, offset).Find(&ue)
  89. case "Sent Messages":
  90. db.Instance.Table("user_email").Select("email_id,is_read").Where("user_id=? and status=1", ctx.UserID).Limit(limit, offset).Find(&ue)
  91. case "Drafts":
  92. db.Instance.Table("user_email").Select("email_id,is_read").Where("user_id=? and status=4", ctx.UserID).Limit(limit, offset).Find(&ue)
  93. case "Deleted Messages":
  94. db.Instance.Table("user_email").Select("email_id,is_read").Where("user_id=? and status=3", ctx.UserID).Limit(limit, offset).Find(&ue)
  95. case "Junk":
  96. db.Instance.Table("user_email").Select("email_id,is_read").Where("user_id=? and status=5", ctx.UserID).Limit(limit, offset).Find(&ue)
  97. default:
  98. groupNames := strings.Split(groupName, "/")
  99. groupName = groupNames[len(groupNames)-1]
  100. var group models.Group
  101. db.Instance.Table("group").Where("user_id=? and name=?", ctx.UserID, groupName).Get(&group)
  102. if group.ID == 0 {
  103. return ret
  104. }
  105. db.Instance.Table("user_email").Select("email_id,is_read").Where("user_id=? and group_id = ?", ctx.UserID, group.ID).Limit(limit, offset).Find(&ue)
  106. }
  107. ueMap := map[int]*models.UserEmail{}
  108. var emailIds []int
  109. for _, email := range ue {
  110. ueMap[email.EmailID] = email
  111. emailIds = append(emailIds, email.EmailID)
  112. }
  113. _ = db.Instance.Table("email").Select("*").Where(Eq{"id": emailIds}).Find(&ret)
  114. for i, data := range ret {
  115. ret[i].IsRead = ueMap[data.Id].IsRead
  116. }
  117. return ret
  118. }