list.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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/array"
  9. "github.com/Jinnrry/pmail/utils/context"
  10. log "github.com/sirupsen/logrus"
  11. "strings"
  12. )
  13. import . "xorm.io/builder"
  14. func GetEmailList(ctx *context.Context, tagInfo dto.SearchTag, keyword string, pop3List bool, offset, limit int) (emailList []*response.EmailResponseData, total int64) {
  15. return getList(ctx, tagInfo, keyword, pop3List, offset, limit)
  16. }
  17. func getList(ctx *context.Context, tagInfo dto.SearchTag, keyword string, pop3List bool, offset, limit int) (emailList []*response.EmailResponseData, total int64) {
  18. querySQL, queryParams := genSQL(ctx, false, tagInfo, keyword, pop3List, offset, limit)
  19. err := db.Instance.SQL(querySQL, queryParams...).Find(&emailList)
  20. if err != nil {
  21. log.WithContext(ctx).Errorf("SQL ERROR: %s ,Error:%s", querySQL, err)
  22. }
  23. totalSQL, totalParams := genSQL(ctx, true, tagInfo, keyword, pop3List, offset, limit)
  24. _, err = db.Instance.SQL(totalSQL, totalParams...).Get(&total)
  25. if err != nil {
  26. log.WithContext(ctx).Errorf("SQL ERROR: %s ,Error:%s", querySQL, err)
  27. }
  28. return emailList, total
  29. }
  30. func genSQL(ctx *context.Context, count bool, tagInfo dto.SearchTag, keyword string, pop3List bool, offset, limit int) (string, []any) {
  31. sqlParams := []any{ctx.UserID}
  32. sql := "select "
  33. if count {
  34. sql += `count(1) from email e left join user_email ue on e.id=ue.email_id where ue.user_id = ? `
  35. } else if pop3List {
  36. sql += `e.id,e.size from email e left join user_email ue on e.id=ue.email_id where ue.user_id = ? `
  37. } else {
  38. sql += `e.*,ue.is_read from email e left join user_email ue on e.id=ue.email_id where ue.user_id = ? `
  39. }
  40. if tagInfo.Status != -1 {
  41. sql += " and ue.status =? "
  42. sqlParams = append(sqlParams, tagInfo.Status)
  43. } else if tagInfo.Status == -1 {
  44. sql += " and ue.status = 0"
  45. }
  46. if tagInfo.Type != -1 {
  47. sql += " and type =? "
  48. sqlParams = append(sqlParams, tagInfo.Type)
  49. }
  50. if tagInfo.GroupId != -1 {
  51. sql += " and ue.group_id=? "
  52. sqlParams = append(sqlParams, tagInfo.GroupId)
  53. } else {
  54. sql += " and ue.group_id=0 "
  55. }
  56. if keyword != "" {
  57. sql += " and (subject like ? or text like ? )"
  58. sqlParams = append(sqlParams, "%"+keyword+"%", "%"+keyword+"%")
  59. }
  60. if limit == 0 {
  61. limit = 10
  62. }
  63. sql += " order by e.id desc"
  64. if limit < 10000 {
  65. sql += fmt.Sprintf(" LIMIT %d OFFSET %d ", limit, offset)
  66. }
  67. return sql, sqlParams
  68. }
  69. type statRes struct {
  70. Total int64
  71. Size int64
  72. }
  73. // Stat 查询邮件总数和大小
  74. func Stat(ctx *context.Context) (int64, int64) {
  75. 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`
  76. var ret statRes
  77. _, err := db.Instance.SQL(sql, ctx.UserID).Get(&ret)
  78. if err != nil {
  79. log.WithContext(ctx).Errorf("SQL ERROR: %s ,Error:%s", sql, err)
  80. }
  81. return ret.Total, ret.Size
  82. }
  83. type ImapListReq struct {
  84. UidList []int
  85. Star int
  86. End int
  87. }
  88. func GetUEListByUID(ctx *context.Context, groupName string, star, end int, uidList []int) []*response.UserEmailUIDData {
  89. var ue []*response.UserEmailUIDData
  90. sql := "SELECT id,email_id, is_read, ROW_NUMBER() OVER (ORDER BY id) AS serial_number FROM `user_email` WHERE user_id = ? "
  91. params := []any{ctx.UserID}
  92. if len(uidList) > 0 {
  93. sql += fmt.Sprintf(" and id in (%s)", array.Join(uidList, ","))
  94. }
  95. if star > 0 {
  96. sql += " and id >=?"
  97. params = append(params, star)
  98. }
  99. if end > 0 {
  100. sql += " and id <=?"
  101. params = append(params, end)
  102. }
  103. switch groupName {
  104. case "INBOX":
  105. sql += " and status =?"
  106. params = append(params, 0)
  107. case "Sent Messages":
  108. sql += " and status =?"
  109. params = append(params, 1)
  110. case "Drafts":
  111. sql += " and status =?"
  112. params = append(params, 4)
  113. case "Deleted Messages":
  114. sql += " and status =?"
  115. params = append(params, 3)
  116. case "Junk":
  117. sql += " and status =?"
  118. params = append(params, 5)
  119. default:
  120. groupNames := strings.Split(groupName, "/")
  121. groupName = groupNames[len(groupNames)-1]
  122. var group models.Group
  123. db.Instance.Table("group").Where("user_id=? and name=?", ctx.UserID, groupName).Get(&group)
  124. if group.ID == 0 {
  125. return nil
  126. }
  127. sql += " and group_id = ?"
  128. params = append(params, group.ID)
  129. }
  130. db.Instance.SQL(sql, params...).Find(&ue)
  131. return ue
  132. }
  133. func getEmailListByUidList(ctx *context.Context, groupName string, req ImapListReq, uid bool) []*response.EmailResponseData {
  134. var ret []*response.EmailResponseData
  135. var ue []*response.UserEmailUIDData
  136. sql := fmt.Sprintf("SELECT id,email_id, is_read, ROW_NUMBER() OVER (ORDER BY id) AS serial_number FROM `user_email` WHERE (user_id = ? and id in (%s))", array.Join(req.UidList, ","))
  137. if req.Star > 0 && req.End != 0 {
  138. sql = fmt.Sprintf("SELECT id,email_id, is_read, ROW_NUMBER() OVER (ORDER BY id) AS serial_number FROM `user_email` WHERE (user_id = ? and id >=%d and id <= %d)", req.Star, req.End)
  139. }
  140. if req.Star > 0 && req.End == 0 {
  141. sql = fmt.Sprintf("SELECT id,email_id, is_read, ROW_NUMBER() OVER (ORDER BY id) AS serial_number FROM `user_email` WHERE (user_id = ? and id >=%d )", req.Star)
  142. }
  143. err := db.Instance.SQL(sql, ctx.UserID).Find(&ue)
  144. if err != nil {
  145. log.WithContext(ctx).Errorf("SQL ERROR: %s ,Error:%s", sql, err)
  146. }
  147. ueMap := map[int]*response.UserEmailUIDData{}
  148. var emailIds []int
  149. for _, email := range ue {
  150. ueMap[email.EmailID] = email
  151. emailIds = append(emailIds, email.EmailID)
  152. }
  153. _ = db.Instance.Table("email").Select("*").Where(Eq{"id": emailIds}).Find(&ret)
  154. for i, data := range ret {
  155. ret[i].IsRead = ueMap[data.Id].IsRead
  156. ret[i].SerialNumber = ueMap[data.Id].SerialNumber
  157. ret[i].UeId = ueMap[data.Id].ID
  158. }
  159. return ret
  160. }
  161. func GetEmailListByGroup(ctx *context.Context, groupName string, req ImapListReq, uid bool) []*response.EmailResponseData {
  162. if len(req.UidList) == 0 && req.Star == 0 && req.End == 0 {
  163. return nil
  164. }
  165. if uid {
  166. return getEmailListByUidList(ctx, groupName, req, uid)
  167. }
  168. var ret []*response.EmailResponseData
  169. var ue []*response.UserEmailUIDData
  170. sql := fmt.Sprintf("SELECT * from (SELECT id,email_id, is_read, ROW_NUMBER() OVER (ORDER BY id) AS serial_number FROM `user_email` WHERE (user_id = ? and status = ? and group_id=0 )) a WHERE serial_number in (%s)", array.Join(req.UidList, ","))
  171. if req.Star > 0 && req.End == 0 {
  172. sql = fmt.Sprintf("SELECT * from (SELECT id,email_id, is_read, ROW_NUMBER() OVER (ORDER BY id) AS serial_number FROM `user_email` WHERE (user_id = ? and status = ? and group_id=0 )) a WHERE serial_number >= %d", req.Star)
  173. }
  174. if req.Star > 0 && req.End > 0 {
  175. sql = fmt.Sprintf("SELECT * from (SELECT id,email_id, is_read, ROW_NUMBER() OVER (ORDER BY id) AS serial_number FROM `user_email` WHERE (user_id = ? and status = ? and group_id=0 )) a WHERE serial_number >= %d and serial_number <=%d", req.Star, req.End)
  176. }
  177. switch groupName {
  178. case "INBOX":
  179. db.Instance.SQL(sql, ctx.UserID, 0).Find(&ue)
  180. case "Sent Messages":
  181. db.Instance.SQL(sql, ctx.UserID, 1).Find(&ue)
  182. case "Drafts":
  183. db.Instance.SQL(sql, ctx.UserID, 4).Find(&ue)
  184. case "Deleted Messages":
  185. db.Instance.SQL(sql, ctx.UserID, 3).Find(&ue)
  186. case "Junk":
  187. db.Instance.SQL(sql, ctx.UserID, 5).Find(&ue)
  188. default:
  189. groupNames := strings.Split(groupName, "/")
  190. groupName = groupNames[len(groupNames)-1]
  191. var group models.Group
  192. db.Instance.Table("group").Where("user_id=? and name=?", ctx.UserID, groupName).Get(&group)
  193. if group.ID == 0 {
  194. return ret
  195. }
  196. db.Instance.
  197. SQL(fmt.Sprintf(
  198. "SELECT * from (SELECT id,email_id, is_read, ROW_NUMBER() OVER (ORDER BY id) AS serial_number FROM `user_email` WHERE (user_id = ? and group_id = ?)) a WHERE serial_number in (%s)",
  199. array.Join(req.UidList, ","))).
  200. Find(&ue, ctx.UserID, group.ID)
  201. }
  202. ueMap := map[int]*response.UserEmailUIDData{}
  203. var emailIds []int
  204. for _, email := range ue {
  205. ueMap[email.EmailID] = email
  206. emailIds = append(emailIds, email.EmailID)
  207. }
  208. _ = db.Instance.Table("email").Select("*").Where(Eq{"id": emailIds}).Find(&ret)
  209. for i, data := range ret {
  210. ret[i].IsRead = ueMap[data.Id].IsRead
  211. ret[i].SerialNumber = ueMap[data.Id].SerialNumber
  212. ret[i].UeId = ueMap[data.Id].ID
  213. }
  214. return ret
  215. }