boweniac пре 2 дана
родитељ
комит
0da2a34f22
2 измењених фајлова са 24 додато и 13 уклоњено
  1. 15 8
      crontask/send_wx_on_timeout.go
  2. 9 5
      internal/logic/sop_task/publish_sop_task_logic.go

+ 15 - 8
crontask/send_wx_on_timeout.go

@@ -2,6 +2,7 @@ package crontask
 
 import (
 	"context"
+	"github.com/zeromicro/go-zero/core/logx"
 	"regexp"
 	"time"
 	"wechat-api/ent"
@@ -73,7 +74,7 @@ func (l *CronTask) sendWxOnTimeout() {
 				Where(messagerecords.SourceTypeEQ(3)).
 				Where(messagerecords.SourceIDEQ(node.StageID)).
 				Where(messagerecords.SubSourceIDEQ(0)).
-				Where(messagerecords.SendTimeGTE(lowerBound)).
+				//Where(messagerecords.SendTimeGTE(lowerBound)).
 				Where(messagerecords.SendTimeLTE(upperBound)).
 				All(ctx)
 		} else {
@@ -183,7 +184,8 @@ func (l *CronTask) sendWxOnTimeout() {
 					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(stageMap, *contact, currentLabelIds, node.ActionLabelAdd, node.ActionLabelDel, s.OrganizationID, visitedStages)
 				}
 			}
 		}
@@ -201,8 +203,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,9 +279,12 @@ 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().
+			x, err := l.svcCtx.DB.MessageRecords.Query().
 				Where(
 					messagerecords.ContactWxid(contact.Wxid),
 					messagerecords.SourceType(3),
@@ -288,7 +292,10 @@ func (l *CronTask) AddLabelRelationships(sopStages map[uint64]*ent.SopStage, con
 					messagerecords.SubSourceID(0),
 				).
 				Only(l.ctx)
+			logx.Info("x: ", x)
+			logx.Info("err: ", err)
 			if !ent.IsNotFound(err) {
+				logx.Info("continue")
 				continue
 			}
 			// 判断ActionMessage是否为空
@@ -349,8 +356,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

+ 9 - 5
internal/logic/sop_task/publish_sop_task_logic.go

@@ -201,7 +201,8 @@ func (l *PublishSopTaskLogic) PublishSopTask(req *types.SopTaskInfo) (resp *type
 
 						if stage.ActionLabelAdd != nil || stage.ActionLabelDel != nil {
 							// 递归调用 AddLabelRelationships
-							err = l.AddLabelRelationships(stageMap, *c, currentLabelIds, stage.ActionLabelAdd, stage.ActionLabelDel, organizationId)
+							visitedStages := make(map[uint64]bool)
+							err = l.AddLabelRelationships(stageMap, *c, currentLabelIds, stage.ActionLabelAdd, stage.ActionLabelDel, organizationId, visitedStages)
 							if err != nil {
 								//_ = tx.Rollback()
 								return nil, err
@@ -228,7 +229,7 @@ func (l *PublishSopTaskLogic) PublishSopTask(req *types.SopTaskInfo) (resp *type
 	}
 }
 
-func (l *PublishSopTaskLogic) AddLabelRelationships(sopStages map[uint64]*ent.SopStage, contact ent.Contact, currentLabelIds []uint64, addLabelIds []uint64, delLabelIds []uint64, organizationId uint64) (err error) {
+func (l *PublishSopTaskLogic) 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 {
@@ -307,7 +308,10 @@ func (l *PublishSopTaskLogic) AddLabelRelationships(sopStages map[uint64]*ent.So
 
 	// 遍历 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(
@@ -378,8 +382,8 @@ func (l *PublishSopTaskLogic) AddLabelRelationships(sopStages map[uint64]*ent.So
 
 			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