123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- 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/uuidx"
- "github.com/zeromicro/go-zero/core/logx"
- )
- type CreateBatchMsgLogic struct {
- ctx context.Context
- svcCtx *svc.ServiceContext
- logx.Logger
- }
- func NewCreateBatchMsgLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateBatchMsgLogic {
- return &CreateBatchMsgLogic{
- ctx: ctx,
- svcCtx: svcCtx,
- Logger: logx.WithContext(ctx),
- }
- }
- func (l *CreateBatchMsgLogic) CreateBatchMsg(req *types.BatchMsgInfo) (*types.BaseMsgResp, error) {
- 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).
- 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
- }
|