|
@@ -25,8 +25,9 @@ import (
|
|
|
|
|
|
type CreateWhatcappBatchMsgLogic struct {
|
|
|
logx.Logger
|
|
|
- ctx context.Context
|
|
|
- svcCtx *svc.ServiceContext
|
|
|
+ ctx context.Context
|
|
|
+ svcCtx *svc.ServiceContext
|
|
|
+ MaxNumber int
|
|
|
}
|
|
|
|
|
|
func NewCreateWhatcappBatchMsgLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateWhatcappBatchMsgLogic {
|
|
@@ -38,7 +39,7 @@ func NewCreateWhatcappBatchMsgLogic(ctx context.Context, svcCtx *svc.ServiceCont
|
|
|
|
|
|
func (l *CreateWhatcappBatchMsgLogic) CreateWhatcappBatchMsg(req *types.BatchMsgInfo) (*types.BaseMsgResp, error) {
|
|
|
organizationId := l.ctx.Value("organizationId").(uint64)
|
|
|
-
|
|
|
+ l.MaxNumber = 1000 //批量处理的最大联系人数
|
|
|
var err error
|
|
|
|
|
|
var tagstring, tag string
|
|
@@ -145,27 +146,14 @@ func (l *CreateWhatcappBatchMsgLogic) CreateWhatcappBatchMsg(req *types.BatchMsg
|
|
|
return &types.BaseMsgResp{Msg: "未查询到收信人,请重新选择", Code: 3}, nil
|
|
|
}
|
|
|
|
|
|
- //TODO 发送批量消息
|
|
|
- sto := make([]string, 0)
|
|
|
- for _, v := range userList {
|
|
|
- sto = append(sto, v.Cc+v.Phone)
|
|
|
- }
|
|
|
- fullPhone := *req.Cc + *req.Phone
|
|
|
- result, err := aliyun.SendBatchChatappMessage(*req.TemplateCode, *req.Lang, fullPhone, sto)
|
|
|
- if err != nil {
|
|
|
- return nil, errorx.NewInvalidArgumentError(err.Error())
|
|
|
- }
|
|
|
- l.Logger.Infof("send_batch_chatapp_message result=%v\n", result)
|
|
|
-
|
|
|
+ // 开启事务
|
|
|
+ tx, err := l.svcCtx.DB.Tx(context.Background())
|
|
|
batchNo := uuidx.NewUUID().String()
|
|
|
-
|
|
|
var sendTime *time.Time
|
|
|
if !sendNow {
|
|
|
sendTime = &startTime
|
|
|
}
|
|
|
|
|
|
- tx, err := l.svcCtx.DB.Tx(context.Background())
|
|
|
-
|
|
|
_, err = tx.BatchMsg.Create().
|
|
|
SetNotNilBatchNo(&batchNo).
|
|
|
SetStatus(0).
|
|
@@ -192,35 +180,24 @@ func (l *CreateWhatcappBatchMsgLogic) CreateWhatcappBatchMsg(req *types.BatchMsg
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- msgs := make([]*ent.MsgCreate, 100)
|
|
|
- for idx, user := range userList {
|
|
|
- phone := user.Cc + user.Phone
|
|
|
- msgRow := tx.Msg.Create().
|
|
|
- SetNotNilCc(req.Cc).
|
|
|
- SetNotNilPhone(req.Phone).
|
|
|
- SetNotNilToid(&phone).
|
|
|
- SetMsgtype(int32(1)).
|
|
|
- SetNotNilMsg(req.Msg).
|
|
|
- SetStatus(1).
|
|
|
- SetNotNilBatchNo(&batchNo)
|
|
|
+ userSegment := make([]*ent.Contact, 0, l.MaxNumber)
|
|
|
+ for _, user := range userList {
|
|
|
+ userSegment = append(userSegment, user)
|
|
|
|
|
|
- msgs = append(msgs, msgRow)
|
|
|
- // 100条插入一次
|
|
|
- if idx == 100 {
|
|
|
- _, err = tx.Msg.CreateBulk(msgs...).Save(l.ctx)
|
|
|
+ if len(userSegment) == l.MaxNumber {
|
|
|
+ err = l.addBatchMsg(userSegment, tx, req, batchNo)
|
|
|
if err != nil {
|
|
|
_ = tx.Rollback()
|
|
|
- l.Logger.Errorf("msg CreateBulk err: %v", err)
|
|
|
+ return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
|
}
|
|
|
}
|
|
|
- msgs = make([]*ent.MsgCreate, 100)
|
|
|
+ userSegment = make([]*ent.Contact, 0, l.MaxNumber)
|
|
|
}
|
|
|
- // 不足100条插入一次
|
|
|
- if len(msgs) > 0 {
|
|
|
- _, err = tx.Msg.CreateBulk(msgs...).Save(l.ctx)
|
|
|
+ if len(userSegment) > 0 {
|
|
|
+ err = l.addBatchMsg(userSegment, tx, req, batchNo)
|
|
|
if err != nil {
|
|
|
_ = tx.Rollback()
|
|
|
- l.Logger.Errorf("msg CreateBulk err: %v", err)
|
|
|
+ return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -275,3 +252,38 @@ func (l *CreateWhatcappBatchMsgLogic) getLabelListByIds(labels, groupLabels []ui
|
|
|
|
|
|
return result
|
|
|
}
|
|
|
+
|
|
|
+func (l *CreateWhatcappBatchMsgLogic) addBatchMsg(userList []*ent.Contact, tx *ent.Tx, req *types.BatchMsgInfo, batchNo string) error {
|
|
|
+ // 发送批量消息
|
|
|
+ sto := make([]string, 0, 100)
|
|
|
+ for _, v := range userList {
|
|
|
+ sto = append(sto, v.Cc+v.Phone)
|
|
|
+ }
|
|
|
+ fromPhone := *req.Cc + *req.Phone
|
|
|
+ result, err := aliyun.SendBatchChatappMessage(*req.TemplateCode, *req.Lang, fromPhone, sto)
|
|
|
+ if err != nil {
|
|
|
+ return errorx.NewInvalidArgumentError(err.Error())
|
|
|
+ }
|
|
|
+ l.Logger.Infof("send_batch_chatapp_message result=%v\n", result)
|
|
|
+
|
|
|
+ msgs := make([]*ent.MsgCreate, 0, l.MaxNumber)
|
|
|
+ for _, user := range userList {
|
|
|
+ phone := user.Cc + user.Phone
|
|
|
+ msgRow := tx.Msg.Create().
|
|
|
+ SetNotNilCc(req.Cc).
|
|
|
+ SetNotNilPhone(req.Phone).
|
|
|
+ SetNotNilToid(&phone).
|
|
|
+ SetMsgtype(int32(1)).
|
|
|
+ SetNotNilMsg(req.Msg).
|
|
|
+ SetStatus(1).
|
|
|
+ SetNotNilBatchNo(&batchNo)
|
|
|
+
|
|
|
+ msgs = append(msgs, msgRow)
|
|
|
+ }
|
|
|
+ _, err = tx.Msg.CreateBulk(msgs...).Save(l.ctx)
|
|
|
+ if err != nil {
|
|
|
+ l.Logger.Errorf("msg CreateBulk err: %v", err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|