list.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package list
  2. import (
  3. "encoding/json"
  4. log "github.com/sirupsen/logrus"
  5. "pmail/db"
  6. "pmail/dto"
  7. "pmail/models"
  8. "pmail/utils/context"
  9. )
  10. func GetEmailList(ctx *context.Context, tag string, keyword string, offset, limit int) (emailList []*models.Email, total int) {
  11. querySQL, queryParams := genSQL(ctx, false, tag, keyword, offset, limit)
  12. counterSQL, counterParams := genSQL(ctx, true, tag, keyword, offset, limit)
  13. err := db.Instance.Select(&emailList, db.WithContext(ctx, querySQL), queryParams...)
  14. if err != nil {
  15. log.Errorf("SQL ERROR: %s ,Error:%s", querySQL, err)
  16. }
  17. err = db.Instance.Get(&total, db.WithContext(ctx, counterSQL), counterParams...)
  18. if err != nil {
  19. log.Errorf("SQL ERROR: %s ,Error:%s", querySQL, err)
  20. }
  21. return
  22. }
  23. func genSQL(ctx *context.Context, counter bool, tag, keyword string, offset, limit int) (string, []any) {
  24. sql := "select * from email where 1=1 "
  25. if counter {
  26. sql = "select count(1) from email where 1=1 "
  27. }
  28. sqlParams := []any{}
  29. var tagInfo dto.SearchTag
  30. _ = json.Unmarshal([]byte(tag), &tagInfo)
  31. if tagInfo.Type != -1 {
  32. sql += " and type =? "
  33. sqlParams = append(sqlParams, tagInfo.Type)
  34. }
  35. if tagInfo.Status != -1 {
  36. sql += " and status =? "
  37. sqlParams = append(sqlParams, tagInfo.Status)
  38. } else {
  39. sql += " and status != 3"
  40. }
  41. if tagInfo.GroupId != -1 {
  42. sql += " and group_id=? "
  43. sqlParams = append(sqlParams, tagInfo.GroupId)
  44. }
  45. if keyword != "" {
  46. sql += " and (subject like ? or text like ? )"
  47. sqlParams = append(sqlParams, "%"+keyword+"%", "%"+keyword+"%")
  48. }
  49. sql += " order by id desc limit ? offset ?"
  50. sqlParams = append(sqlParams, limit, offset)
  51. return sql, sqlParams
  52. }