list.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package list
  2. import (
  3. "fmt"
  4. log "github.com/sirupsen/logrus"
  5. "pmail/db"
  6. "pmail/dto"
  7. "pmail/dto/response"
  8. "pmail/utils/context"
  9. )
  10. func GetEmailList(ctx *context.Context, tagInfo dto.SearchTag, keyword string, pop3List bool, offset, limit int) (emailList []*response.EmailResponseData, total int64) {
  11. return getList(ctx, tagInfo, keyword, pop3List, offset, limit)
  12. }
  13. func getList(ctx *context.Context, tagInfo dto.SearchTag, keyword string, pop3List bool, offset, limit int) (emailList []*response.EmailResponseData, total int64) {
  14. querySQL, queryParams := genSQL(ctx, false, tagInfo, keyword, pop3List, offset, limit)
  15. err := db.Instance.SQL(querySQL, queryParams...).Find(&emailList)
  16. if err != nil {
  17. log.WithContext(ctx).Errorf("SQL ERROR: %s ,Error:%s", querySQL, err)
  18. }
  19. totalSQL, totalParams := genSQL(ctx, true, tagInfo, keyword, pop3List, offset, limit)
  20. _, err = db.Instance.SQL(totalSQL, totalParams...).Get(&total)
  21. if err != nil {
  22. log.WithContext(ctx).Errorf("SQL ERROR: %s ,Error:%s", querySQL, err)
  23. }
  24. return emailList, total
  25. }
  26. func genSQL(ctx *context.Context, count bool, tagInfo dto.SearchTag, keyword string, pop3List bool, offset, limit int) (string, []any) {
  27. sqlParams := []any{ctx.UserID}
  28. sql := "select "
  29. if count {
  30. sql += `count(1) from email e left join user_email ue on e.id=ue.email_id where ue.user_id = ? `
  31. } else if pop3List {
  32. sql += `e.id,e.size from email e left join user_email ue on e.id=ue.email_id where ue.user_id = ? `
  33. } else {
  34. sql += `e.*,ue.is_read from email e left join user_email ue on e.id=ue.email_id where ue.user_id = ? `
  35. }
  36. if tagInfo.Status != -1 {
  37. sql += " and ue.status =? "
  38. sqlParams = append(sqlParams, tagInfo.Status)
  39. } else {
  40. sql += " and ue.status != 3"
  41. }
  42. if tagInfo.Type != -1 {
  43. sql += " and type =? "
  44. sqlParams = append(sqlParams, tagInfo.Type)
  45. }
  46. if tagInfo.GroupId != -1 {
  47. sql += " and ue.group_id=? "
  48. sqlParams = append(sqlParams, tagInfo.GroupId)
  49. }
  50. if keyword != "" {
  51. sql += " and (subject like ? or text like ? )"
  52. sqlParams = append(sqlParams, "%"+keyword+"%", "%"+keyword+"%")
  53. }
  54. if limit == 0 {
  55. limit = 10
  56. }
  57. sql += " order by e.id desc"
  58. if limit < 10000 {
  59. sql += fmt.Sprintf(" limit %d,%d ", offset, limit)
  60. }
  61. return sql, sqlParams
  62. }
  63. type statRes struct {
  64. Total int64
  65. Size int64
  66. }
  67. // Stat 查询邮件总数和大小
  68. func Stat(ctx *context.Context) (int64, int64) {
  69. 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`
  70. var ret statRes
  71. _, err := db.Instance.SQL(sql, ctx.UserID).Get(&ret)
  72. if err != nil {
  73. log.WithContext(ctx).Errorf("SQL ERROR: %s ,Error:%s", sql, err)
  74. }
  75. return ret.Total, ret.Size
  76. }