package label_relationship import ( "context" "github.com/suyuan32/simple-admin-common/msg/errormsg" "wechat-api/ent" "wechat-api/ent/contact" "wechat-api/ent/custom_types" "wechat-api/ent/labelrelationship" "wechat-api/ent/messagerecords" "wechat-api/ent/soptask" "wechat-api/internal/svc" "wechat-api/internal/types" "wechat-api/internal/utils/dberrorhandler" "github.com/zeromicro/go-zero/core/logx" ) type UpdateLabelRelationshipsLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewUpdateLabelRelationshipsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLabelRelationshipsLogic { return &UpdateLabelRelationshipsLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx} } func (l *UpdateLabelRelationshipsLogic) UpdateLabelRelationships(req *types.LabelRelationshipsInfo) (resp *types.BaseMsgResp, err error) { // 获取联系人信息 c, err := l.svcCtx.DB.Contact.Query().Where(contact.ID(*req.ContactId)).Only(l.ctx) // 获取联系人当前已关联的标签 currentLabelRelationships, err := l.svcCtx.DB.LabelRelationship.Query().Where(labelrelationship.ContactID(*req.ContactId)).All(l.ctx) if err != nil { return nil, dberrorhandler.DefaultEntError(l.Logger, err, req) } // 提取当前标签ID var currentLabelIds []uint64 for _, relationship := range currentLabelRelationships { currentLabelIds = append(currentLabelIds, relationship.LabelID) } // 对比新旧标签ID,找出需要新增和移除的标签 addLabelIds, removeLabelIds := compareLabelIds(req.LabelIds, currentLabelIds) // 如果 req.UpdateType 为空,或 req.UpdateType 的值为 “all” 时 if req.UpdateType == nil || *req.UpdateType == "all" { // 删除需要移除的标签关系 for _, id := range removeLabelIds { _, err := l.svcCtx.DB.LabelRelationship. Delete(). Where( labelrelationship.ContactID(*req.ContactId), labelrelationship.LabelID(id), ). Exec(l.ctx) if err != nil { return nil, dberrorhandler.DefaultEntError(l.Logger, err, req) } } } // 创建需要新增的标签关系 for _, id := range addLabelIds { _, _ = l.svcCtx.DB.LabelRelationship.Create(). SetLabelID(id). SetContactID(*req.ContactId). Save(l.ctx) //if err != nil { // return nil, dberrorhandler.DefaultEntError(l.Logger, err, req) //} } currentLabelIds = req.LabelIds // 获取所有 status 为 3 且 bot_wxid_list 包含 c.wx_wxid 的 sop_task sopTasks, err := l.svcCtx.DB.SopTask.Query().Where(soptask.Status(3)).All(l.ctx) if err != nil { return nil, dberrorhandler.DefaultEntError(l.Logger, err, req) } var filteredSopTasks []*ent.SopTask for _, task := range sopTasks { for _, botWxid := range task.BotWxidList { if botWxid == c.WxWxid { filteredSopTasks = append(filteredSopTasks, task) break } } } // 获取所有 filteredSopTasks 的 sop_stages var sopStages []*ent.SopStage for _, task := range filteredSopTasks { stages, err := task.QueryTaskStages().All(l.ctx) if err != nil { return nil, dberrorhandler.DefaultEntError(l.Logger, err, req) } sopStages = append(sopStages, stages...) } err = l.AddLabelRelationships(sopStages, *c, currentLabelIds) if err != nil { return nil, err } return &types.BaseMsgResp{ Code: 0, Msg: errormsg.Success, }, nil } func (l *UpdateLabelRelationshipsLogic) AddLabelRelationships(sopStages []*ent.SopStage, contact ent.Contact, currentLabelIds []uint64) (err error) { // 遍历 sop_stages,找出满足条件的 stage for _, stage := range sopStages { if stage.ConditionType == 1 && isLabelIdListMatchFilter(currentLabelIds, stage.ConditionOperator, stage.ConditionList) { // 判断是否有 contact_wxid、source_type、source_id、sub_source_id 相同的记录 _, err = l.svcCtx.DB.MessageRecords.Query(). Where( messagerecords.ContactWxid(contact.Wxid), messagerecords.SourceType(3), messagerecords.SourceID(stage.ID), messagerecords.SubSourceID(0), ). Only(l.ctx) if err == nil { continue } // 判断ActionMessage是否为空 sourceType := 3 if stage.ActionMessage != nil { for _, message := range stage.ActionMessage { _, _ = l.svcCtx.DB.MessageRecords.Create(). SetNotNilBotWxid(&contact.WxWxid). SetNotNilContactID(&contact.ID). SetNotNilContactType(&contact.Type). SetNotNilContactWxid(&contact.Wxid). SetNotNilContentType(&message.Type). SetNotNilContent(&message.Content). SetNotNilSourceType(&sourceType). SetNotNilSourceID(&stage.ID). Save(l.ctx) //if err != nil { // return dberrorhandler.DefaultEntError(l.Logger, err, nil) //} } } if stage.ActionLabel != nil { // 获取 addLabelIds 中不在 currentLabelIds 中的标签ID var newLabelIds []uint64 // 创建一个映射,用于快速查找 currentLabelIds 中的元素 currentLabelIdSet := make(map[uint64]struct{}) for _, id := range currentLabelIds { currentLabelIdSet[id] = struct{}{} } // 遍历 addLabelIds,找出不在 currentLabelIds 中的元素 for _, id := range stage.ActionLabel { if _, exists := currentLabelIdSet[id]; !exists { newLabelIds = append(newLabelIds, id) } } if len(newLabelIds) == 0 { return nil } // 创建需要新增的标签关系 for _, id := range newLabelIds { _, err = l.svcCtx.DB.LabelRelationship.Create(). SetLabelID(id). SetContactID(contact.ID). Save(l.ctx) //if err != nil { // return dberrorhandler.DefaultEntError(l.Logger, err, nil) //} } // 合并 currentLabelIds 和 newLabelIds currentLabelIds = append(currentLabelIds, newLabelIds...) // 递归调用 AddLabelRelationships err = l.AddLabelRelationships(sopStages, contact, currentLabelIds) if err != nil { return err } } } } return nil } // compareLabelIds compares the new label ids with the current ones and returns the ids to be added and removed func compareLabelIds(newLabelIds []uint64, currentLabelIds []uint64) (addLabelIds []uint64, removeLabelIds []uint64) { newLabelIdSet := make(map[uint64]struct{}, len(newLabelIds)) for _, id := range newLabelIds { newLabelIdSet[id] = struct{}{} } for _, id := range currentLabelIds { if _, ok := newLabelIdSet[id]; ok { delete(newLabelIdSet, id) } else { removeLabelIds = append(removeLabelIds, id) } } for id := range newLabelIdSet { addLabelIds = append(addLabelIds, id) } return } func isLabelIdListMatchFilter(labelIdList []uint64, conditionOperator int, conditionList []custom_types.Condition) bool { labelIdSet := make(map[uint64]struct{}) for _, id := range labelIdList { labelIdSet[id] = struct{}{} } for _, condition := range conditionList { match := false for _, id := range condition.LabelIdList { if _, ok := labelIdSet[id]; ok { match = true break } } if condition.Equal == 2 { match = !match } if (conditionOperator == 1 && !match) || (conditionOperator == 2 && match) { return match } } return conditionOperator == 1 }