list.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 int64) {
  11. querySQL, queryParams := genSQL(ctx, tag, keyword)
  12. total, err := db.Instance.Table("email").Where(querySQL, queryParams...).Desc("id").Limit(limit, offset).FindAndCount(&emailList)
  13. if err != nil {
  14. log.Errorf("SQL ERROR: %s ,Error:%s", querySQL, err)
  15. }
  16. return
  17. }
  18. func genSQL(ctx *context.Context, tag, keyword string) (string, []any) {
  19. sql := "1=1 "
  20. sqlParams := []any{}
  21. var tagInfo dto.SearchTag
  22. _ = json.Unmarshal([]byte(tag), &tagInfo)
  23. if tagInfo.Type != -1 {
  24. sql += " and type =? "
  25. sqlParams = append(sqlParams, tagInfo.Type)
  26. }
  27. if tagInfo.Status != -1 {
  28. sql += " and status =? "
  29. sqlParams = append(sqlParams, tagInfo.Status)
  30. } else {
  31. sql += " and status != 3"
  32. }
  33. if tagInfo.GroupId != -1 {
  34. sql += " and group_id=? "
  35. sqlParams = append(sqlParams, tagInfo.GroupId)
  36. }
  37. if keyword != "" {
  38. sql += " and (subject like ? or text like ? )"
  39. sqlParams = append(sqlParams, "%"+keyword+"%", "%"+keyword+"%")
  40. }
  41. return sql, sqlParams
  42. }