create_batch_msg_logic.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package batch_msg
  2. import (
  3. "context"
  4. "strings"
  5. "time"
  6. "wechat-api/ent"
  7. "wechat-api/ent/contact"
  8. "wechat-api/ent/label"
  9. "wechat-api/ent/labelrelationship"
  10. "wechat-api/internal/svc"
  11. "wechat-api/internal/types"
  12. "wechat-api/internal/utils/dberrorhandler"
  13. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  14. "github.com/suyuan32/simple-admin-common/utils/uuidx"
  15. "github.com/zeromicro/go-zero/core/logx"
  16. )
  17. type CreateBatchMsgLogic struct {
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. logx.Logger
  21. }
  22. func NewCreateBatchMsgLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateBatchMsgLogic {
  23. return &CreateBatchMsgLogic{
  24. ctx: ctx,
  25. svcCtx: svcCtx,
  26. Logger: logx.WithContext(ctx),
  27. }
  28. }
  29. func (l *CreateBatchMsgLogic) CreateBatchMsg(req *types.BatchMsgInfo) (*types.BaseMsgResp, error) {
  30. all := false
  31. for _, label := range req.Labels {
  32. if strings.EqualFold(label, "all") || strings.EqualFold(label, "全部") {
  33. all = true
  34. tagstring := "all"
  35. req.Tag = &tagstring
  36. }
  37. }
  38. l.Logger.Infof("CreateBatchMsgLogic: %v", req)
  39. startTime := time.Now()
  40. // req.StartTimeStr 不为nil并且不为空
  41. if req.StartTimeStr != nil && *req.StartTimeStr != "" {
  42. var err error
  43. // 将 req.StartTimeStr 转换为 req.StartTime
  44. startTime, err = time.Parse("2006-01-02 15:04:05", *req.StartTimeStr)
  45. if err != nil {
  46. // 处理错误,例如打印错误并返回
  47. l.Logger.Errorf("时间字符串转换错误: %v", err)
  48. return nil, err
  49. }
  50. }
  51. tagstring := strings.Join(req.Labels, ",")
  52. req.Tag = &tagstring
  53. userlist := make([]*ent.Contact, 0)
  54. var err error
  55. if all {
  56. // 获取 contact 表中 wx_wxid 等于 req.Fromwxid 的 type 为1或2的数据
  57. userlist, err = l.svcCtx.DB.Contact.Query().Where(contact.WxWxid(*req.Fromwxid), contact.TypeIn(1, 2)).All(l.ctx)
  58. if err != nil {
  59. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  60. }
  61. } else {
  62. // 获取 label 表中 name 为 tags的记录
  63. labids, err := l.svcCtx.DB.Label.Query().Where(label.NameIn(req.Labels...)).IDs(l.ctx)
  64. if err != nil {
  65. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  66. }
  67. // 获取 label_relationship 表中,label_id 等于 labids 的 contact_id
  68. labelrelationships, err := l.svcCtx.DB.LabelRelationship.Query().Where(labelrelationship.LabelIDIn(labids...)).All(l.ctx)
  69. if err != nil {
  70. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  71. }
  72. contact_ids := make([]uint64, 0)
  73. for _, labelrelationship := range labelrelationships {
  74. contact_ids = append(contact_ids, labelrelationship.ContactID)
  75. }
  76. if len(contact_ids) > 0 {
  77. // 获取 contact 表中 wx_wxid 等于 req.Fromwxid 并且 id 等于 contact_ids 并且 type 为1或2 的数据
  78. userlist, err = l.svcCtx.DB.Contact.Query().Where(contact.WxWxid(*req.Fromwxid), contact.IDIn(contact_ids...), contact.TypeIn(1, 2)).All(l.ctx)
  79. if err != nil {
  80. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  81. }
  82. }
  83. }
  84. total := int32(len(userlist))
  85. if total == 0 {
  86. return &types.BaseMsgResp{Msg: errormsg.TargetNotFound}, nil
  87. }
  88. uuid := uuidx.NewUUID()
  89. batchNo := uuid.String()
  90. _, err = l.svcCtx.DB.BatchMsg.Create().
  91. SetNotNilBatchNo(&batchNo).
  92. SetNotNilFromwxid(req.Fromwxid).
  93. SetNotNilMsg(req.Msg).
  94. SetNotNilTag(req.Tag).
  95. SetTotal(total).
  96. SetNotNilStartTime(&startTime).
  97. Save(l.ctx)
  98. if err != nil {
  99. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  100. }
  101. return &types.BaseMsgResp{Msg: errormsg.CreateSuccess}, nil
  102. }