|
@@ -2,6 +2,7 @@ package crontask
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "github.com/zeromicro/go-zero/core/logx"
|
|
|
"regexp"
|
|
|
"time"
|
|
|
"wechat-api/ent"
|
|
@@ -24,15 +25,24 @@ func (l *CronTask) sendWxOnTimeout() {
|
|
|
soptask.DeletedAtIsNil(),
|
|
|
).
|
|
|
All(l.ctx)
|
|
|
+
|
|
|
sopTasks := make([]uint64, 0)
|
|
|
for _, st := range sopTask {
|
|
|
sopTasks = append(sopTasks, st.ID)
|
|
|
}
|
|
|
- sopStages, _ := l.svcCtx.DB.SopStage.Query().Where(sopstage.TaskIDIn(sopTasks...), sopstage.DeletedAtIsNil()).All(l.ctx)
|
|
|
- stageMap := make(map[uint64]*ent.SopStage)
|
|
|
- for _, stage := range sopStages {
|
|
|
- stageMap[stage.ID] = stage
|
|
|
+
|
|
|
+ botStageMap := make(map[string]map[uint64]*ent.SopStage)
|
|
|
+ for _, t := range sopTask {
|
|
|
+ for _, bot := range t.BotWxidList {
|
|
|
+ sopStages, _ := l.svcCtx.DB.SopStage.Query().Where(sopstage.TaskIDEQ(t.ID), sopstage.DeletedAtIsNil()).All(l.ctx)
|
|
|
+ stageMap := make(map[uint64]*ent.SopStage)
|
|
|
+ for _, stage := range sopStages {
|
|
|
+ stageMap[stage.ID] = stage
|
|
|
+ }
|
|
|
+ botStageMap[bot] = stageMap
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
// 查询所有 no_reply_condition 值非 0 的 sop_node 记录
|
|
|
nodes, err := l.svcCtx.DB.SopNode.Query().
|
|
|
Where(sopnode.NoReplyConditionNEQ(0)).
|
|
@@ -72,7 +82,6 @@ func (l *CronTask) sendWxOnTimeout() {
|
|
|
Where(messagerecords.StatusEQ(3)).
|
|
|
Where(messagerecords.SourceTypeEQ(3)).
|
|
|
Where(messagerecords.SourceIDEQ(node.StageID)).
|
|
|
- Where(messagerecords.SubSourceIDEQ(0)).
|
|
|
Where(messagerecords.SendTimeGTE(lowerBound)).
|
|
|
Where(messagerecords.SendTimeLTE(upperBound)).
|
|
|
All(ctx)
|
|
@@ -81,7 +90,6 @@ func (l *CronTask) sendWxOnTimeout() {
|
|
|
Where(messagerecords.StatusEQ(3)).
|
|
|
Where(messagerecords.SourceTypeEQ(4)).
|
|
|
Where(messagerecords.SourceIDIn(node.ParentID)).
|
|
|
- Where(messagerecords.SubSourceIDEQ(0)).
|
|
|
Where(messagerecords.SendTimeGTE(lowerBound)).
|
|
|
Where(messagerecords.SendTimeLTE(upperBound)).
|
|
|
All(ctx)
|
|
@@ -93,6 +101,8 @@ func (l *CronTask) sendWxOnTimeout() {
|
|
|
Where(messagerecords.StatusEQ(3)).
|
|
|
Order(ent.Desc(messagerecords.FieldID)).
|
|
|
First(ctx)
|
|
|
+ logx.Info("latest.ID: ", latest.ID)
|
|
|
+ logx.Info("s.ID: ", s.ID)
|
|
|
if latest.ID == s.ID {
|
|
|
// 创建 MessageRecords 记录
|
|
|
if node.ActionMessage != nil {
|
|
@@ -178,12 +188,14 @@ func (l *CronTask) sendWxOnTimeout() {
|
|
|
}
|
|
|
contact := &ent.Contact{
|
|
|
ID: s.ContactID,
|
|
|
- Type: s.ContentType,
|
|
|
+ Type: s.ContactType,
|
|
|
WxWxid: s.BotWxid,
|
|
|
Wxid: s.ContactWxid,
|
|
|
}
|
|
|
+
|
|
|
if node.ActionLabelAdd != nil || node.ActionLabelDel != nil {
|
|
|
- _ = l.AddLabelRelationships(stageMap, *contact, currentLabelIds, node.ActionLabelAdd, node.ActionLabelDel, s.OrganizationID)
|
|
|
+ visitedStages := make(map[uint64]bool)
|
|
|
+ _ = l.AddLabelRelationships(botStageMap[s.BotWxid], *contact, currentLabelIds, node.ActionLabelAdd, node.ActionLabelDel, s.OrganizationID, visitedStages)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -201,8 +213,7 @@ func splitString(input string) []string {
|
|
|
// Split the input string based on the pattern
|
|
|
return re.Split(input, -1)
|
|
|
}
|
|
|
-
|
|
|
-func (l *CronTask) AddLabelRelationships(sopStages map[uint64]*ent.SopStage, contact ent.Contact, currentLabelIds []uint64, addLabelIds []uint64, delLabelIds []uint64, organizationId uint64) (err error) {
|
|
|
+func (l *CronTask) AddLabelRelationships(sopStages map[uint64]*ent.SopStage, contact ent.Contact, currentLabelIds []uint64, addLabelIds []uint64, delLabelIds []uint64, organizationId uint64, visitedStages map[uint64]bool) (err error) {
|
|
|
//// 开始事务
|
|
|
//tx, err := l.svcCtx.DB.Tx(context.Background())
|
|
|
//if err != nil {
|
|
@@ -278,7 +289,10 @@ func (l *CronTask) AddLabelRelationships(sopStages map[uint64]*ent.SopStage, con
|
|
|
}
|
|
|
// 遍历 sop_stages,找出满足条件的 stage
|
|
|
for key, stage := range sopStages {
|
|
|
- if stage != nil && stage.ConditionType == 1 && isLabelIdListMatchFilter(finalLabelIds, stage.ConditionOperator, stage.ConditionList) {
|
|
|
+ if stage == nil || visitedStages[key] {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if stage.ConditionType == 1 && isLabelIdListMatchFilter(finalLabelIds, stage.ConditionOperator, stage.ConditionList) {
|
|
|
// 判断是否有 contact_wxid、source_type、source_id、sub_source_id 相同的记录
|
|
|
_, err := l.svcCtx.DB.MessageRecords.Query().
|
|
|
Where(
|
|
@@ -349,8 +363,8 @@ func (l *CronTask) AddLabelRelationships(sopStages map[uint64]*ent.SopStage, con
|
|
|
|
|
|
if stage.ActionLabelAdd != nil || stage.ActionLabelDel != nil {
|
|
|
// 递归调用 AddLabelRelationships
|
|
|
- sopStages[key] = nil
|
|
|
- err = l.AddLabelRelationships(sopStages, contact, finalLabelIds, stage.ActionLabelAdd, stage.ActionLabelDel, organizationId)
|
|
|
+ visitedStages[key] = true
|
|
|
+ err = l.AddLabelRelationships(sopStages, contact, finalLabelIds, stage.ActionLabelAdd, stage.ActionLabelDel, organizationId, visitedStages)
|
|
|
if err != nil {
|
|
|
//_ = tx.Rollback()
|
|
|
return err
|