Browse Source

修复 bug

boweniac 9 months ago
parent
commit
0352a05130
1 changed files with 37 additions and 43 deletions
  1. 37 43
      internal/logic/label_relationship/update_label_relationships_logic.go

+ 37 - 43
internal/logic/label_relationship/update_label_relationships_logic.go

@@ -31,17 +31,17 @@ func NewUpdateLabelRelationshipsLogic(ctx context.Context, svcCtx *svc.ServiceCo
 
 func (l *UpdateLabelRelationshipsLogic) UpdateLabelRelationships(req *types.LabelRelationshipsInfo) (resp *types.BaseMsgResp, err error) {
 	// 开始事务
-	tx, err := l.svcCtx.DB.Tx(context.Background())
-	if err != nil {
-		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
-	}
+	//tx, err := l.svcCtx.DB.Tx(context.Background())
+	//if err != nil {
+	//	return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+	//}
 
 	// 获取联系人信息
-	c, err := tx.Contact.Query().Where(contact.ID(*req.ContactId)).Only(l.ctx)
+	c, err := l.svcCtx.DB.Contact.Query().Where(contact.ID(*req.ContactId)).Only(l.ctx)
 	// 获取联系人当前已关联的标签
-	currentLabelRelationships, err := tx.LabelRelationship.Query().Where(labelrelationship.ContactID(*req.ContactId)).All(l.ctx)
+	currentLabelRelationships, err := l.svcCtx.DB.LabelRelationship.Query().Where(labelrelationship.ContactID(*req.ContactId)).All(l.ctx)
 	if err != nil {
-		_ = tx.Rollback()
+		//_ = tx.Rollback()
 		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
 	}
 
@@ -58,7 +58,7 @@ func (l *UpdateLabelRelationshipsLogic) UpdateLabelRelationships(req *types.Labe
 	if req.UpdateType == nil || *req.UpdateType == "all" {
 		// 删除需要移除的标签关系
 		for _, id := range removeLabelIds {
-			_, err := tx.LabelRelationship.
+			_, err := l.svcCtx.DB.LabelRelationship.
 				Delete().
 				Where(
 					labelrelationship.ContactID(*req.ContactId),
@@ -66,7 +66,7 @@ func (l *UpdateLabelRelationshipsLogic) UpdateLabelRelationships(req *types.Labe
 				).
 				Exec(l.ctx)
 			if err != nil {
-				_ = tx.Rollback()
+				//_ = tx.Rollback()
 				return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
 			}
 		}
@@ -74,7 +74,7 @@ func (l *UpdateLabelRelationshipsLogic) UpdateLabelRelationships(req *types.Labe
 
 	// 创建需要新增的标签关系
 	for _, id := range addLabelIds {
-		_, _ = tx.LabelRelationship.Create().
+		_, _ = l.svcCtx.DB.LabelRelationship.Create().
 			SetLabelID(id).
 			SetContactID(*req.ContactId).
 			Save(l.ctx)
@@ -86,10 +86,10 @@ func (l *UpdateLabelRelationshipsLogic) UpdateLabelRelationships(req *types.Labe
 	currentLabelIds = req.LabelIds
 
 	// 获取所有 status 为 3 且 bot_wxid_list 包含 c.wx_wxid 的 sop_task
-	sopTasks, err := tx.SopTask.Query().Where(soptask.Status(3)).All(l.ctx)
+	sopTasks, err := l.svcCtx.DB.SopTask.Query().Where(soptask.Status(3)).All(l.ctx)
 	l.Logger.Info("UpdateLabelRelationships sopTasks:", sopTasks)
 	if err != nil {
-		_ = tx.Rollback()
+		//_ = tx.Rollback()
 		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
 	}
 
@@ -108,40 +108,37 @@ func (l *UpdateLabelRelationshipsLogic) UpdateLabelRelationships(req *types.Labe
 	for _, task := range filteredSopTasks {
 		stages, err := task.QueryTaskStages().All(l.ctx)
 		if err != nil {
-			_ = tx.Rollback()
+			//_ = tx.Rollback()
 			return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
 		}
 		sopStages = append(sopStages, stages...)
 	}
-
 	err = l.AddLabelRelationships(sopStages, *c, currentLabelIds)
+
 	if err != nil {
-		_ = tx.Rollback()
+		//_ = tx.Rollback()
 		return nil, err
 	}
-	// 所有操作成功,提交事务
-	err = tx.Commit()
-	if err != nil {
-		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
-	}
+	//// 所有操作成功,提交事务
+	//err = tx.Commit()
+	//if err != nil {
+	//	return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+	//}
 
-	return &types.BaseMsgResp{
-		Code: 0,
-		Msg:  errormsg.Success,
-	}, nil
+	return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
 }
 
 func (l *UpdateLabelRelationshipsLogic) AddLabelRelationships(sopStages []*ent.SopStage, contact ent.Contact, currentLabelIds []uint64) (err error) {
 	// 开始事务
-	tx, err := l.svcCtx.DB.Tx(context.Background())
-	if err != nil {
-		return dberrorhandler.DefaultEntError(l.Logger, err, nil)
-	}
+	//tx, err := l.svcCtx.DB.Tx(context.Background())
+	//if err != nil {
+	//	return dberrorhandler.DefaultEntError(l.Logger, err, nil)
+	//}
 	// 遍历 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 = tx.MessageRecords.Query().
+			_, err = l.svcCtx.DB.MessageRecords.Query().
 				Where(
 					messagerecords.ContactWxid(contact.Wxid),
 					messagerecords.SourceType(3),
@@ -156,7 +153,7 @@ func (l *UpdateLabelRelationshipsLogic) AddLabelRelationships(sopStages []*ent.S
 			sourceType := 3
 			if stage.ActionMessage != nil {
 				for _, message := range stage.ActionMessage {
-					_, _ = tx.MessageRecords.Create().
+					_, _ = l.svcCtx.DB.MessageRecords.Create().
 						SetNotNilBotWxid(&contact.WxWxid).
 						SetNotNilContactID(&contact.ID).
 						SetNotNilContactType(&contact.Type).
@@ -191,10 +188,9 @@ func (l *UpdateLabelRelationshipsLogic) AddLabelRelationships(sopStages []*ent.S
 				if len(newLabelIds) == 0 {
 					return nil
 				}
-
 				// 创建需要新增的标签关系
 				for _, id := range newLabelIds {
-					_, err = tx.LabelRelationship.Create().
+					_, _ = l.svcCtx.DB.LabelRelationship.Create().
 						SetLabelID(id).
 						SetContactID(contact.ID).
 						Save(l.ctx)
@@ -202,24 +198,22 @@ func (l *UpdateLabelRelationshipsLogic) AddLabelRelationships(sopStages []*ent.S
 					//	return dberrorhandler.DefaultEntError(l.Logger, err, nil)
 					//}
 				}
-
 				// 合并 currentLabelIds 和 newLabelIds
 				currentLabelIds = append(currentLabelIds, newLabelIds...)
 				// 递归调用 AddLabelRelationships
-				err = l.AddLabelRelationships(sopStages, contact, currentLabelIds)
-				if err != nil {
-					_ = tx.Rollback()
-					return err
-				}
+				//err = l.AddLabelRelationships(sopStages, contact, currentLabelIds)
+				//if err != nil {
+				//	//_ = tx.Rollback()
+				//	return err
+				//}
 			}
 		}
 	}
 	// 所有操作成功,提交事务
-	err = tx.Commit()
-	if err != nil {
-		return dberrorhandler.DefaultEntError(l.Logger, err, nil)
-	}
-
+	//err = tx.Commit()
+	//if err != nil {
+	//	return dberrorhandler.DefaultEntError(l.Logger, err, nil)
+	//}
 	return nil
 }