create_batch_msg_logic.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package batch_msg
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "strings"
  7. "time"
  8. "wechat-api/ent/custom_types"
  9. "wechat-api/ent"
  10. "wechat-api/ent/contact"
  11. "wechat-api/ent/label"
  12. "wechat-api/ent/labelrelationship"
  13. "wechat-api/internal/svc"
  14. "wechat-api/internal/types"
  15. "wechat-api/internal/utils/dberrorhandler"
  16. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  17. "github.com/suyuan32/simple-admin-common/utils/uuidx"
  18. "github.com/zeromicro/go-zero/core/logx"
  19. )
  20. type CreateBatchMsgLogic struct {
  21. ctx context.Context
  22. svcCtx *svc.ServiceContext
  23. logx.Logger
  24. }
  25. func NewCreateBatchMsgLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateBatchMsgLogic {
  26. return &CreateBatchMsgLogic{
  27. ctx: ctx,
  28. svcCtx: svcCtx,
  29. Logger: logx.WithContext(ctx),
  30. }
  31. }
  32. func (l *CreateBatchMsgLogic) CreateBatchMsg(req *types.BatchMsgInfo) (*types.BaseMsgResp, error) {
  33. organizationId := l.ctx.Value("organizationId").(uint64)
  34. var err error
  35. all := false
  36. for _, labelName := range req.Labels {
  37. if strings.EqualFold(labelName, "all") || strings.EqualFold(labelName, "全部") {
  38. all = true
  39. tagstring := "all"
  40. req.Tag = &tagstring
  41. }
  42. }
  43. startTime := time.Now()
  44. // req.StartTimeStr 不为nil并且不为空
  45. if req.StartTimeStr != nil && *req.StartTimeStr != "" {
  46. // 将 req.StartTimeStr 转换为 req.StartTime
  47. startTime, err = time.Parse("2006-01-02 15:04:05", *req.StartTimeStr)
  48. if err != nil {
  49. // 处理错误,例如打印错误并返回
  50. l.Logger.Errorf("时间字符串转换错误: %v", err)
  51. return nil, err
  52. }
  53. }
  54. // 把 req.Msg 字符串的内容 json_decode 到 msgArray
  55. // 然后再把每一条信息都给所有指定的用户记录到 message_records 表
  56. var msgArray []custom_types.Action
  57. if req.Msg != nil && *req.Msg != "" {
  58. err = json.Unmarshal([]byte(*req.Msg), &msgArray)
  59. if err != nil {
  60. return nil, errors.New("解析JSON失败")
  61. }
  62. }
  63. var msgActionList []custom_types.Action
  64. if len(msgArray) > 0 {
  65. msgActionList = make([]custom_types.Action, len(msgArray))
  66. for i, msg := range msgArray {
  67. if msg.Type == 1 {
  68. msgActionList[i] = custom_types.Action{
  69. Type: msg.Type,
  70. Content: msg.Content,
  71. }
  72. } else {
  73. msgActionList[i] = custom_types.Action{
  74. Type: msg.Type,
  75. Content: msg.Content,
  76. Meta: &custom_types.Meta{
  77. Filename: msg.Meta.Filename,
  78. },
  79. }
  80. }
  81. }
  82. }
  83. tagstring := strings.Join(req.Labels, ",")
  84. req.Tag = &tagstring
  85. userlist := make([]*ent.Contact, 0)
  86. if all {
  87. // 获取 contact 表中 wx_wxid 等于 req.Fromwxid 的 type 为1或2的数据
  88. userlist, err = l.svcCtx.DB.Contact.Query().Where(contact.WxWxid(*req.Fromwxid), contact.TypeIn(1, 2)).All(l.ctx)
  89. if err != nil {
  90. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  91. }
  92. } else {
  93. // 获取 label 表中 name 为 tags的记录
  94. labids, err := l.svcCtx.DB.Label.Query().Where(label.NameIn(req.Labels...)).IDs(l.ctx)
  95. if err != nil {
  96. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  97. }
  98. //l.Logger.Infof("CreateBatchMsgLogic labids= %v", labids)
  99. // 获取 label_relationship 表中,label_id 等于 labids 的 contact_id
  100. labelrelationships, err := l.svcCtx.DB.LabelRelationship.Query().Where(labelrelationship.LabelIDIn(labids...)).All(l.ctx)
  101. if err != nil {
  102. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  103. }
  104. contact_ids := make([]uint64, 0)
  105. for _, labelrelationship := range labelrelationships {
  106. contact_ids = append(contact_ids, labelrelationship.ContactID)
  107. }
  108. //l.Logger.Infof("CreateBatchMsgLogic contact_ids= %v", contact_ids)
  109. if len(contact_ids) > 0 {
  110. // 获取 contact 表中 wx_wxid 等于 req.Fromwxid 并且 id 等于 contact_ids 并且 type 为1或2 的数据
  111. userlist, err = l.svcCtx.DB.Contact.Query().Where(contact.WxWxid(*req.Fromwxid), contact.IDIn(contact_ids...), contact.TypeIn(1, 2)).All(l.ctx)
  112. if err != nil {
  113. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  114. }
  115. //l.Logger.Infof("CreateBatchMsgLogic userlist= %v", userlist)
  116. }
  117. }
  118. // 这里是根据userlist 和 批量消息数 获得最终待发送消息总数
  119. total := int32(len(userlist)) * int32(len(msgActionList))
  120. if total == 0 {
  121. return &types.BaseMsgResp{Msg: errormsg.TargetNotFound}, nil
  122. }
  123. uuid := uuidx.NewUUID()
  124. batchNo := uuid.String()
  125. _, err = l.svcCtx.DB.BatchMsg.Create().
  126. SetNotNilBatchNo(&batchNo).
  127. SetNotNilFromwxid(req.Fromwxid).
  128. SetNotNilMsg(req.Msg).
  129. SetNotNilTag(req.Tag).
  130. SetTotal(total).
  131. SetNotNilTaskName(req.TaskName).
  132. SetNotNilStartTime(&startTime).
  133. SetNotNilType(req.Type).
  134. SetNotNilOrganizationID(&organizationId).
  135. Save(l.ctx)
  136. if err != nil {
  137. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  138. }
  139. return &types.BaseMsgResp{Msg: errormsg.CreateSuccess}, nil
  140. }