group.go 2.9 KB

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