group.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package group
  2. import (
  3. "fmt"
  4. log "github.com/sirupsen/logrus"
  5. "pmail/db"
  6. "pmail/dto"
  7. "pmail/models"
  8. "pmail/utils/array"
  9. "pmail/utils/context"
  10. "pmail/utils/errors"
  11. )
  12. type GroupItem struct {
  13. Id int `json:"id"`
  14. Label string `json:"label"`
  15. Tag string `json:"tag"`
  16. Children []*GroupItem `json:"children"`
  17. }
  18. func DelGroup(ctx *context.Context, groupId int) (bool, error) {
  19. allGroupIds := getAllChildId(ctx, groupId)
  20. allGroupIds = append(allGroupIds, groupId)
  21. // 开启一个事务
  22. trans, err := db.Instance.Begin()
  23. if err != nil {
  24. return false, errors.Wrap(err)
  25. }
  26. res, err := trans.Exec(db.WithContext(ctx, fmt.Sprintf("delete from `group` where id in (%s) and user_id =?", array.Join(allGroupIds, ","))), ctx.UserID)
  27. if err != nil {
  28. trans.Rollback()
  29. return false, errors.Wrap(err)
  30. }
  31. num, err := res.RowsAffected()
  32. if err != nil {
  33. trans.Rollback()
  34. return false, errors.Wrap(err)
  35. }
  36. _, err = trans.Exec(db.WithContext(ctx, fmt.Sprintf("update email set group_id=0 where group_id in (%s)", array.Join(allGroupIds, ","))))
  37. if err != nil {
  38. trans.Rollback()
  39. return false, errors.Wrap(err)
  40. }
  41. trans.Commit()
  42. return num > 0, nil
  43. }
  44. type id struct {
  45. Id int `db:"id"`
  46. }
  47. func getAllChildId(ctx *context.Context, rootId int) []int {
  48. var ids []id
  49. var ret []int
  50. db.Instance.Select(&ids, db.WithContext(ctx, "select id from `group` where parent_id=? and user_id=?"), rootId, ctx.UserID)
  51. for _, item := range ids {
  52. ret = array.Merge(ret, getAllChildId(ctx, item.Id))
  53. ret = append(ret, item.Id)
  54. }
  55. return ret
  56. }
  57. // GetGroupInfoList 获取全部的分组
  58. func GetGroupInfoList(ctx *context.Context) []*GroupItem {
  59. return buildChildren(ctx, 0)
  60. }
  61. // MoveMailToGroup 将某封邮件移动到某个分组中
  62. func MoveMailToGroup(ctx *context.Context, mailId []int, groupId int) bool {
  63. res, err := db.Instance.Exec(db.WithContext(ctx, fmt.Sprintf("update email set group_id=? where id in (%s)", array.Join(mailId, ","))), groupId)
  64. if err != nil {
  65. log.WithContext(ctx).Errorf("SQL Error:%+v", err)
  66. return false
  67. }
  68. rowNum, err := res.RowsAffected()
  69. if err != nil {
  70. log.WithContext(ctx).Errorf("SQL Error:%+v", err)
  71. return false
  72. }
  73. return rowNum > 0
  74. }
  75. func buildChildren(ctx *context.Context, parentId int) []*GroupItem {
  76. var ret []*GroupItem
  77. var rootGroup []*models.Group
  78. err := db.Instance.Select(&rootGroup, db.WithContext(ctx, "select * from `group` where parent_id=? and user_id=?"), parentId, ctx.UserID)
  79. if err != nil {
  80. log.WithContext(ctx).Errorf("SQL Error:%v", err)
  81. }
  82. for _, group := range rootGroup {
  83. ret = append(ret, &GroupItem{
  84. Id: group.ID,
  85. Label: group.Name,
  86. Tag: dto.SearchTag{GroupId: group.ID, Status: -1, Type: -1}.ToString(),
  87. Children: buildChildren(ctx, group.ID),
  88. })
  89. }
  90. return ret
  91. }
  92. func GetGroupList(ctx *context.Context) []*models.Group {
  93. var ret []*models.Group
  94. db.Instance.Select(&ret, db.WithContext(ctx, "select * from `group` where user_id=?"), ctx.UserID)
  95. return ret
  96. }