Browse Source

优化sop死循环问题

boweniac 3 months ago
parent
commit
acbfc71812

+ 9 - 5
crontask/send_wx_on_timeout.go

@@ -28,7 +28,10 @@ func (l *CronTask) sendWxOnTimeout() {
 		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
+	}
 	// 查询所有 no_reply_condition 值非 0  的 sop_node 记录
 	nodes, err := l.svcCtx.DB.SopNode.Query().
 		Where(sopnode.NoReplyConditionNEQ(0)).
@@ -174,7 +177,7 @@ func (l *CronTask) sendWxOnTimeout() {
 					Wxid:   s.ContactWxid,
 				}
 				if node.ActionLabelAdd != nil || node.ActionLabelDel != nil {
-					_ = l.AddLabelRelationships(sopStages, *contact, currentLabelIds, node.ActionLabelAdd, node.ActionLabelDel, s.OrganizationID)
+					_ = l.AddLabelRelationships(stageMap, *contact, currentLabelIds, node.ActionLabelAdd, node.ActionLabelDel, s.OrganizationID)
 				}
 			}
 		}
@@ -193,7 +196,7 @@ func splitString(input string) []string {
 	return re.Split(input, -1)
 }
 
-func (l *CronTask) AddLabelRelationships(sopStages []*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) (err error) {
 	//// 开始事务
 	//tx, err := l.svcCtx.DB.Tx(context.Background())
 	//if err != nil {
@@ -268,8 +271,8 @@ func (l *CronTask) AddLabelRelationships(sopStages []*ent.SopStage, contact ent.
 		finalLabelIds = append(finalLabelIds, id)
 	}
 	// 遍历 sop_stages,找出满足条件的 stage
-	for _, stage := range sopStages {
-		if stage.ConditionType == 1 && isLabelIdListMatchFilter(finalLabelIds, stage.ConditionOperator, stage.ConditionList) {
+	for key, stage := range sopStages {
+		if stage != nil && 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(
@@ -340,6 +343,7 @@ func (l *CronTask) AddLabelRelationships(sopStages []*ent.SopStage, contact ent.
 
 			if stage.ActionLabelAdd != nil || stage.ActionLabelDel != nil {
 				// 递归调用 AddLabelRelationships
+				sopStages[key] = nil
 				err = l.AddLabelRelationships(sopStages, contact, finalLabelIds, stage.ActionLabelAdd, stage.ActionLabelDel, organizationId)
 				if err != nil {
 					//_ = tx.Rollback()

+ 1 - 1
internal/logic/message_records/get_message_records_list_logic.go

@@ -68,7 +68,7 @@ func (l *GetMessageRecordsListLogic) GetMessageRecordsList(req *types.MessageRec
 				Status:      &v.Status,
 				BotWxid:     &v.BotWxid,
 				ContactWxid: &v.ContactWxid,
-				ContentType: &v.ContentType,
+				ContactType: &v.ContactType,
 				Content:     &v.Content,
 				ErrorDetail: &v.ErrorDetail,
 				SendTime:    pointy.GetUnixMilliPointer(v.SendTime.UnixMilli()),

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

@@ -80,7 +80,10 @@ func (l *PublishSopTaskLogic) PublishSopTask(req *types.IDReq) (resp *types.Base
 			//_ = tx.Rollback()
 			return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
 		}
-
+		stageMap := make(map[uint64]*ent.SopStage)
+		for _, stage := range sopStages {
+			stageMap[stage.ID] = stage
+		}
 		// 遍历 stage
 		for _, stage := range sopTask.Edges.TaskStages {
 			if stage.ConditionType == 1 {
@@ -187,7 +190,7 @@ func (l *PublishSopTaskLogic) PublishSopTask(req *types.IDReq) (resp *types.Base
 
 						if stage.ActionLabelAdd != nil || stage.ActionLabelDel != nil {
 							// 递归调用 AddLabelRelationships
-							err = l.AddLabelRelationships(sopStages, *c, currentLabelIds, stage.ActionLabelAdd, stage.ActionLabelDel, organizationId)
+							err = l.AddLabelRelationships(stageMap, *c, currentLabelIds, stage.ActionLabelAdd, stage.ActionLabelDel, organizationId)
 							if err != nil {
 								//_ = tx.Rollback()
 								return nil, err
@@ -214,7 +217,7 @@ func (l *PublishSopTaskLogic) PublishSopTask(req *types.IDReq) (resp *types.Base
 	}
 }
 
-func (l *PublishSopTaskLogic) AddLabelRelationships(sopStages []*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) (err error) {
 	//// 开始事务
 	//tx, err := l.svcCtx.DB.Tx(context.Background())
 	//if err != nil {
@@ -293,8 +296,8 @@ func (l *PublishSopTaskLogic) AddLabelRelationships(sopStages []*ent.SopStage, c
 	}
 
 	// 遍历 sop_stages,找出满足条件的 stage
-	for _, stage := range sopStages {
-		if stage.ConditionType == 1 && isLabelIdListMatchFilter(finalLabelIds, stage.ConditionOperator, stage.ConditionList) {
+	for key, stage := range sopStages {
+		if stage != nil && 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(
@@ -365,6 +368,7 @@ func (l *PublishSopTaskLogic) AddLabelRelationships(sopStages []*ent.SopStage, c
 
 			if stage.ActionLabelAdd != nil || stage.ActionLabelDel != nil {
 				// 递归调用 AddLabelRelationships
+				sopStages[key] = nil
 				err = l.AddLabelRelationships(sopStages, contact, finalLabelIds, stage.ActionLabelAdd, stage.ActionLabelDel, organizationId)
 				if err != nil {
 					//_ = tx.Rollback()