|
@@ -7,10 +7,10 @@ import (
|
|
|
"strings"
|
|
|
"time"
|
|
|
"wechat-api/ent/custom_types"
|
|
|
+ "wechat-api/ent/label"
|
|
|
|
|
|
"wechat-api/ent"
|
|
|
"wechat-api/ent/contact"
|
|
|
- "wechat-api/ent/label"
|
|
|
"wechat-api/ent/labelrelationship"
|
|
|
"wechat-api/internal/svc"
|
|
|
"wechat-api/internal/types"
|
|
@@ -40,16 +40,24 @@ func (l *CreateBatchMsgLogic) CreateBatchMsg(req *types.BatchMsgInfo) (*types.Ba
|
|
|
|
|
|
var err error
|
|
|
|
|
|
- all := false
|
|
|
- for _, labelName := range req.Labels {
|
|
|
- if strings.EqualFold(labelName, "all") || strings.EqualFold(labelName, "全部") {
|
|
|
- all = true
|
|
|
- tagstring := "all"
|
|
|
- req.Tag = &tagstring
|
|
|
+ var tagstring, tag string
|
|
|
+ allContact, allGroup := false, false
|
|
|
+ tagIdArray := make([]uint64, 0)
|
|
|
+ for _, labelId := range req.Labels {
|
|
|
+ tagIdArray = append(tagIdArray, labelId)
|
|
|
+ if labelId == uint64(0) {
|
|
|
+ allContact = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for _, labelId := range req.GroupLabels {
|
|
|
+ tagIdArray = append(tagIdArray, labelId)
|
|
|
+ if labelId == uint64(0) {
|
|
|
+ allGroup = true
|
|
|
}
|
|
|
}
|
|
|
|
|
|
startTime := time.Now()
|
|
|
+ sendNow := true
|
|
|
|
|
|
if req.StartTimeStr != nil && *req.StartTimeStr != "" {
|
|
|
|
|
@@ -59,6 +67,7 @@ func (l *CreateBatchMsgLogic) CreateBatchMsg(req *types.BatchMsgInfo) (*types.Ba
|
|
|
l.Logger.Errorf("时间字符串转换错误: %v", err)
|
|
|
return nil, err
|
|
|
}
|
|
|
+ sendNow = false
|
|
|
}
|
|
|
|
|
|
|
|
@@ -93,47 +102,70 @@ func (l *CreateBatchMsgLogic) CreateBatchMsg(req *types.BatchMsgInfo) (*types.Ba
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- tagstring := strings.Join(req.Labels, ",")
|
|
|
- req.Tag = &tagstring
|
|
|
-
|
|
|
- userlist := make([]*ent.Contact, 0)
|
|
|
+ userList, groupList := make([]*ent.Contact, 0), make([]*ent.Contact, 0)
|
|
|
|
|
|
- if all {
|
|
|
+ tagMap := make(map[string][]uint64)
|
|
|
+ if allContact && allGroup {
|
|
|
|
|
|
- userlist, err = l.svcCtx.DB.Contact.Query().Where(contact.WxWxid(*req.Fromwxid), contact.TypeIn(1, 2)).All(l.ctx)
|
|
|
+ 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 {
|
|
|
-
|
|
|
- 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)
|
|
|
- }
|
|
|
-
|
|
|
|
|
|
-
|
|
|
- labelrelationships, err := l.svcCtx.DB.LabelRelationship.Query().Where(labelrelationship.LabelIDIn(labids...)).All(l.ctx)
|
|
|
+ tagids := make([]uint64, 0, 1)
|
|
|
+ tagids = append(tagids, uint64(0))
|
|
|
+ tagMap["contact_tag"] = tagids
|
|
|
+ tagMap["group_tag"] = tagids
|
|
|
+ tagByte, err := json.Marshal(tagMap)
|
|
|
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)
|
|
|
+ tagstring = string(tagByte)
|
|
|
+ tag = "全部"
|
|
|
+ } else {
|
|
|
+ if allContact {
|
|
|
+
|
|
|
+ userList, err = l.svcCtx.DB.Contact.Query().Where(contact.WxWxid(*req.Fromwxid), contact.TypeEQ(1)).All(l.ctx)
|
|
|
+ if err != nil {
|
|
|
+ return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
|
+ }
|
|
|
+ tag = "全部联系人"
|
|
|
+ } else {
|
|
|
+ userList, err = getContactList(l, req.Labels, *req.Fromwxid, 1)
|
|
|
+ if err != nil {
|
|
|
+ return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
|
|
|
- if len(contact_ids) > 0 {
|
|
|
-
|
|
|
- userlist, err = l.svcCtx.DB.Contact.Query().Where(contact.WxWxid(*req.Fromwxid), contact.IDIn(contact_ids...), contact.TypeIn(1, 2)).All(l.ctx)
|
|
|
+ if allGroup {
|
|
|
+
|
|
|
+ groupList, err = l.svcCtx.DB.Contact.Query().Where(contact.WxWxid(*req.Fromwxid), contact.TypeEQ(2)).All(l.ctx)
|
|
|
+ if err != nil {
|
|
|
+ return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
|
+ }
|
|
|
+ tag = "全部群"
|
|
|
+ } else {
|
|
|
+ groupList, err = getContactList(l, req.GroupLabels, *req.Fromwxid, 2)
|
|
|
if err != nil {
|
|
|
return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
+
|
|
|
+ tagMap["contact_tag"] = req.Labels
|
|
|
+ tagMap["group_tag"] = req.GroupLabels
|
|
|
+ tagByte, err := json.Marshal(tagMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
|
+ }
|
|
|
+ tagstring = string(tagByte)
|
|
|
+ tagArray := getLabelListByIds(l, req.Labels, req.GroupLabels)
|
|
|
+ if tag != "" {
|
|
|
+ tagArray = append(tagArray, tag)
|
|
|
+ }
|
|
|
+ tag = strings.Join(tagArray, ",")
|
|
|
}
|
|
|
|
|
|
- total := int32(len(userlist)) * int32(len(msgActionList))
|
|
|
+ total := int32(len(userList))*int32(len(msgActionList)) + int32(len(groupList))*int32(len(msgActionList))
|
|
|
|
|
|
if total == 0 {
|
|
|
return &types.BaseMsgResp{Msg: errormsg.TargetNotFound}, nil
|
|
@@ -142,14 +174,21 @@ func (l *CreateBatchMsgLogic) CreateBatchMsg(req *types.BatchMsgInfo) (*types.Ba
|
|
|
uuid := uuidx.NewUUID()
|
|
|
batchNo := uuid.String()
|
|
|
|
|
|
+ var sendTime *time.Time
|
|
|
+ if !sendNow {
|
|
|
+ sendTime = &startTime
|
|
|
+ }
|
|
|
+
|
|
|
_, err = l.svcCtx.DB.BatchMsg.Create().
|
|
|
SetNotNilBatchNo(&batchNo).
|
|
|
SetNotNilFromwxid(req.Fromwxid).
|
|
|
SetNotNilMsg(req.Msg).
|
|
|
- SetNotNilTag(req.Tag).
|
|
|
+ SetNotNilTag(&tag).
|
|
|
+ SetNotNilTagids(&tagstring).
|
|
|
SetTotal(total).
|
|
|
SetNotNilTaskName(req.TaskName).
|
|
|
SetNotNilStartTime(&startTime).
|
|
|
+ SetNillableSendTime(sendTime).
|
|
|
SetNotNilType(req.Type).
|
|
|
SetNotNilOrganizationID(&organizationId).
|
|
|
Save(l.ctx)
|
|
@@ -160,3 +199,45 @@ func (l *CreateBatchMsgLogic) CreateBatchMsg(req *types.BatchMsgInfo) (*types.Ba
|
|
|
|
|
|
return &types.BaseMsgResp{Msg: errormsg.CreateSuccess}, nil
|
|
|
}
|
|
|
+
|
|
|
+func getContactList(l *CreateBatchMsgLogic, labels []uint64, fromWxId string, stype int) ([]*ent.Contact, error) {
|
|
|
+
|
|
|
+ labelrelationships, err := l.svcCtx.DB.LabelRelationship.Query().Where(labelrelationship.LabelIDIn(labels...)).All(l.ctx)
|
|
|
+ if err != nil {
|
|
|
+ return nil, dberrorhandler.DefaultEntError(l.Logger, err, nil)
|
|
|
+ }
|
|
|
+ contact_ids := make([]uint64, 0, len(labelrelationships))
|
|
|
+ for _, labelrelationship := range labelrelationships {
|
|
|
+ contact_ids = append(contact_ids, labelrelationship.ContactID)
|
|
|
+ }
|
|
|
+ userList := make([]*ent.Contact, 0)
|
|
|
+
|
|
|
+ if len(contact_ids) > 0 {
|
|
|
+
|
|
|
+ userList, err = l.svcCtx.DB.Contact.Query().Where(contact.WxWxid(fromWxId), contact.IDIn(contact_ids...), contact.TypeEQ(stype)).All(l.ctx)
|
|
|
+ if err != nil {
|
|
|
+ return nil, dberrorhandler.DefaultEntError(l.Logger, err, nil)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return userList, nil
|
|
|
+}
|
|
|
+
|
|
|
+func getLabelListByIds(l *CreateBatchMsgLogic, labels, groupLabels []uint64) []string {
|
|
|
+ result := make([]string, 0)
|
|
|
+ labels = append(labels, groupLabels...)
|
|
|
+ if len(labels) > 0 {
|
|
|
+ contacts, err := l.svcCtx.DB.Label.Query().Where(
|
|
|
+ label.IDIn(labels...),
|
|
|
+ ).Select("name").All(l.ctx)
|
|
|
+ l.Logger.Infof("contacts=%v", contacts)
|
|
|
+ if err != nil {
|
|
|
+ return result
|
|
|
+ }
|
|
|
+ for _, val := range contacts {
|
|
|
+ result = append(result, val.Name)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return result
|
|
|
+}
|