DESKTOP-53URE31\USER 9 miesięcy temu
rodzic
commit
2a87f66abb

+ 3 - 0
desc/wechat/batch_msg.api

@@ -34,6 +34,9 @@ type (
 
         // 结束时间 
         StopTime  *int64 `json:"stopTime,optional"`
+
+        // 标签列表
+        Labels  []string `json:"labels,optional"`
     }
 
     // The response data of batch msg list | BatchMsg列表数据

+ 87 - 15
internal/logic/batch_msg/create_batch_msg_logic.go

@@ -2,13 +2,18 @@ package batch_msg
 
 import (
 	"context"
+	"strings"
 
+	"wechat-api/ent"
+	"wechat-api/ent/contact"
+	"wechat-api/ent/label"
+	"wechat-api/ent/labelrelationship"
 	"wechat-api/internal/svc"
 	"wechat-api/internal/types"
 	"wechat-api/internal/utils/dberrorhandler"
 
-    "github.com/suyuan32/simple-admin-common/msg/errormsg"
-    "github.com/suyuan32/simple-admin-common/utils/pointy"
+	"github.com/suyuan32/simple-admin-common/msg/errormsg"
+	"github.com/suyuan32/simple-admin-common/utils/uuidx"
 	"github.com/zeromicro/go-zero/core/logx"
 )
 
@@ -27,22 +32,89 @@ func NewCreateBatchMsgLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Cr
 }
 
 func (l *CreateBatchMsgLogic) CreateBatchMsg(req *types.BatchMsgInfo) (*types.BaseMsgResp, error) {
-    _, err := l.svcCtx.DB.BatchMsg.Create().
-			SetNotNilStatus(req.Status).
-			SetNotNilBatchNo(req.BatchNo).
+
+	all := false
+	for _, label := range req.Labels {
+		if strings.EqualFold(label, "all") || strings.EqualFold(label, "全部") {
+			all = true
+		}
+	}
+
+	tagstring := strings.Join(req.Labels, ",")
+	req.Tag = &tagstring
+
+	userlist := make([]*ent.Contact, 0)
+	var err error
+
+	if all {
+		// 获取 contact 表中  wx_wxid 等于 req.Fromwxid 的 type 为1或2的数据
+		userlist, err = l.svcCtx.DB.Contact.Query().Where(contact.WxWxid(*req.Fromwxid), contact.TypeIn(1, 2)).All(l.ctx)
+		if err != nil {
+			return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+		}
+	} else {
+		// 获取 label 表中 name 为 tags的记录
+		labids, err := l.svcCtx.DB.Label.Query().Where(label.NameIn(req.Labels...)).IDs(l.ctx)
+		if err != nil {
+			return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+		}
+		// 获取 label_relationship 表中,label_id 等于 labids 的 contact_id
+		labelrelationships, err := l.svcCtx.DB.LabelRelationship.Query().Where(labelrelationship.LabelIDIn(labids...)).All(l.ctx)
+		if err != nil {
+			return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+		}
+		contact_ids := make([]uint64, 0)
+		for _, labelrelationship := range labelrelationships {
+			contact_ids = append(contact_ids, labelrelationship.ContactID)
+		}
+		if len(contact_ids) > 0 {
+			// 获取 contact 表中 wx_wxid 等于 req.Fromwxid 并且 id 等于 contact_ids 并且 type 为1或2 的数据
+			userlist, err = l.svcCtx.DB.Contact.Query().Where(contact.WxWxid(*req.Fromwxid), contact.IDIn(contact_ids...), contact.TypeIn(1, 2)).All(l.ctx)
+			if err != nil {
+				return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+			}
+		}
+	}
+	total := int32(len(userlist))
+
+	if total == 0 {
+		return &types.BaseMsgResp{Msg: errormsg.TargetNotFound}, nil
+	}
+
+	uuid := uuidx.NewUUID()
+	batchNo := uuid.String()
+
+	_, err = l.svcCtx.DB.BatchMsg.Create().
+		SetNotNilBatchNo(&batchNo).
+		SetNotNilFromwxid(req.Fromwxid).
+		SetNotNilMsg(req.Msg).
+		SetNotNilTag(req.Tag).
+		SetTotal(total).
+		Save(l.ctx)
+
+	if err != nil {
+		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+	}
+
+	msgs := make([]*ent.MsgCreate, 0)
+
+	for _, user := range userlist {
+
+		msg := l.svcCtx.DB.Msg.Create().
 			SetNotNilFromwxid(req.Fromwxid).
+			SetNotNilToid(&user.Wxid).
+			SetMsgtype(1).
 			SetNotNilMsg(req.Msg).
-			SetNotNilTag(req.Tag).
-			SetNotNilTotal(req.Total).
-			SetNotNilSuccess(req.Success).
-			SetNotNilFail(req.Fail).
-			SetNotNilStartTime(pointy.GetTimeMilliPointer(req.StartTime)).
-			SetNotNilStopTime(pointy.GetTimeMilliPointer(req.StopTime)).
-			Save(l.ctx)
-
-    if err != nil {
+			SetNotNilBatchNo(&batchNo)
+
+		msgs = append(msgs, msg)
+	}
+
+	_, err = l.svcCtx.DB.Msg.CreateBulk(msgs...).Save(l.ctx)
+
+	if err != nil {
 		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
 	}
 
-    return &types.BaseMsgResp{Msg: errormsg.CreateSuccess}, nil
+	return &types.BaseMsgResp{Msg: errormsg.CreateSuccess}, nil
 }

+ 2 - 0
internal/types/types.go

@@ -1045,6 +1045,8 @@ type BatchMsgInfo struct {
 	StartTime *int64 `json:"startTime,optional"`
 	// 结束时间
 	StopTime *int64 `json:"stopTime,optional"`
+	// 标签列表
+	Labels []string `json:"labels,optional"`
 }
 
 // The response data of batch msg list | BatchMsg列表数据