Browse Source

fix:修改批量发送wa任务

jimmyyem 1 month ago
parent
commit
b2a263cbd6

+ 2 - 0
desc/wechat/batch_msg.api

@@ -135,6 +135,8 @@ type (
 		To *string `json:"to,optional"`
 		SendTime *int64 `json:"sendTime,optional"`
 		BatchNo *string `json:"batchNo,optional"`
+		Cc *string `json:"cc,optional"`
+		Phone *string `json:"phone,optional"`
 	}
 
 	SendMsgReq {

+ 51 - 39
internal/logic/batch_msg/create_whatcapp_batch_msg_logic.go

@@ -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
+}

+ 2 - 0
internal/logic/batch_msg/get_whatcapp_batch_msg_history_logic.go

@@ -48,6 +48,8 @@ func (l *GetWhatcappBatchMsgHistoryLogic) GetWhatcappBatchMsgHistory(req *types.
 			From:     &value.Fromwxid,
 			To:       &value.Toid,
 			SendTime: pointy.GetPointer(value.CreatedAt.UnixMilli()),
+			Cc:       &value.Cc,
+			Phone:    &value.Phone,
 		})
 	}
 

+ 1 - 1
internal/logic/batch_msg/get_whatcapp_batch_msg_list_logic.go

@@ -37,7 +37,7 @@ func (l *GetWhatcappBatchMsgListLogic) GetWhatcappBatchMsgList(req *types.Whatsa
 		predicates = append(predicates, batchmsg.BatchNoContains(*req.BatchNo))
 	}
 	if req.Phone != nil {
-		predicates = append(predicates, batchmsg.FromwxidContains(*req.Phone))
+		predicates = append(predicates, batchmsg.PhoneContains(*req.Phone))
 	}
 	if req.Msg != nil {
 		predicates = append(predicates, batchmsg.MsgContains(*req.Msg))

+ 2 - 0
internal/types/types.go

@@ -2007,6 +2007,8 @@ type HistoryInfo struct {
 	To       *string `json:"to,optional"`
 	SendTime *int64  `json:"sendTime,optional"`
 	BatchNo  *string `json:"batchNo,optional"`
+	Cc       *string `json:"cc,optional"`
+	Phone    *string `json:"phone,optional"`
 }
 
 // swagger:model SendMsgReq