Browse Source

fix:修改联系人设置标签和加减标签

jimmyyem 2 months ago
parent
commit
cc43b9974a

+ 4 - 0
internal/logic/label_relationship/set_whatsapp_contact_batch_label_logic.go

@@ -29,6 +29,7 @@ func NewSetWhatsappContactBatchLabelLogic(ctx context.Context, svcCtx *svc.Servi
 func (l *SetWhatsappContactBatchLabelLogic) SetWhatsappContactBatchLabel(req *types.BatchLabelRelationshipsInfo) (*types.BaseMsgResp, error) {
 func (l *SetWhatsappContactBatchLabelLogic) SetWhatsappContactBatchLabel(req *types.BatchLabelRelationshipsInfo) (*types.BaseMsgResp, error) {
 	organizationId := l.ctx.Value("organizationId").(uint64)
 	organizationId := l.ctx.Value("organizationId").(uint64)
 
 
+	l.Logger.Errorf("contactIds=%v labelIds=%v updateType=%v\n", req.ContactIds, req.LabelIds, req.UpdateType)
 	// 遍历所有联系人
 	// 遍历所有联系人
 	for _, contactId := range req.ContactIds {
 	for _, contactId := range req.ContactIds {
 		// 开始事务
 		// 开始事务
@@ -58,6 +59,9 @@ func (l *SetWhatsappContactBatchLabelLogic) SetWhatsappContactBatchLabel(req *ty
 
 
 		// 对比新旧标签ID,找出需要新增和移除的标签
 		// 对比新旧标签ID,找出需要新增和移除的标签
 		changeLabelIds, _ := compareLabelIdsForIncrement(req.LabelIds, currentLabelIds, req.UpdateType)
 		changeLabelIds, _ := compareLabelIdsForIncrement(req.LabelIds, currentLabelIds, req.UpdateType)
+		l.Logger.Errorf("contactId=%v req.LabelIds=%v \n", contactId, req.LabelIds)
+		l.Logger.Errorf("req.LabelIds=%v currentLabelIds=%v \n", req.LabelIds, currentLabelIds)
+		l.Logger.Errorf("changeLabelIds=%v \n", changeLabelIds)
 		// 如果 req.UpdateType 为空,或 req.UpdateType 的值为 “all” 时
 		// 如果 req.UpdateType 为空,或 req.UpdateType 的值为 “all” 时
 		if req.UpdateType == -1 {
 		if req.UpdateType == -1 {
 			// 删除需要移除的标签关系
 			// 删除需要移除的标签关系

+ 42 - 17
internal/logic/label_relationship/set_whatsapp_contact_label_logic.go

@@ -57,31 +57,30 @@ func (l *SetWhatsappContactLabelLogic) SetWhatsappContactLabel(req *types.LabelR
 	}
 	}
 
 
 	// 对比新旧标签ID,找出需要新增和移除的标签
 	// 对比新旧标签ID,找出需要新增和移除的标签
-	addLabelIds, removeLabelIds := compareLabelIds(req.LabelIds, currentLabelIds)
+	addLabelIds, removeLabelIds := diffLabelIds(req.LabelIds, currentLabelIds)
+	l.Logger.Infof("req.LabelIds=%v, currentLabelIds=%v\n", req.LabelIds, currentLabelIds)
 	l.Logger.Infof("addLabelIds=%v, removeLabelIds=%v\n", addLabelIds, removeLabelIds)
 	l.Logger.Infof("addLabelIds=%v, removeLabelIds=%v\n", addLabelIds, removeLabelIds)
 
 
 	// 如果 req.UpdateType 为空,或 req.UpdateType 的值为 “all” 时
 	// 如果 req.UpdateType 为空,或 req.UpdateType 的值为 “all” 时
-	if req.UpdateType == nil || *req.UpdateType == "all" {
-		// 删除需要移除的标签关系
-		l.Logger.Errorf("------------------------removeLabelIds--------------------------- %+v\n", removeLabelIds)
-		for _, id := range removeLabelIds {
-			_, err := tx.LabelRelationship.
-				Delete().
-					Where(
-						labelrelationship.ContactID(*req.ContactId),
-						labelrelationship.LabelID(id),
-					).
-				Exec(l.ctx)
-			if err != nil {
-				_ = tx.Rollback()
-				return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
-			}
+	// 删除需要移除的标签关系
+	l.Logger.Errorf("------------------------removeLabelIds--------------------------- %+v\n", removeLabelIds)
+	for _, id := range currentLabelIds {
+		_, err := tx.LabelRelationship.
+			Delete().
+			Where(
+				labelrelationship.ContactID(*req.ContactId),
+				labelrelationship.LabelID(id),
+			).
+			Exec(l.ctx)
+		if err != nil {
+			_ = tx.Rollback()
+			return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
 		}
 		}
 	}
 	}
 
 
 	// 创建需要新增的标签关系
 	// 创建需要新增的标签关系
 	l.Logger.Errorf("------------------------addLabelIds--------------------------- %+v\n", addLabelIds)
 	l.Logger.Errorf("------------------------addLabelIds--------------------------- %+v\n", addLabelIds)
-	for _, id := range addLabelIds {
+	for _, id := range req.LabelIds {
 		_, _ = tx.LabelRelationship.Create().
 		_, _ = tx.LabelRelationship.Create().
 			SetLabelID(id).
 			SetLabelID(id).
 			SetContactID(*req.ContactId).
 			SetContactID(*req.ContactId).
@@ -91,6 +90,32 @@ func (l *SetWhatsappContactLabelLogic) SetWhatsappContactLabel(req *types.LabelR
 		//	return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
 		//	return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
 		//}
 		//}
 	}
 	}
+	_ = tx.Commit()
 
 
 	return &resp, nil
 	return &resp, nil
 }
 }
+
+func diffLabelIds(newLabelIds []uint64, currentLabelIds []uint64) (addLabelIds []uint64, removeLabelIds []uint64) {
+
+	for _, v := range newLabelIds {
+		if !inArray(v, currentLabelIds) {
+			addLabelIds = append(addLabelIds, v)
+		}
+	}
+	for _, v := range currentLabelIds {
+		if !inArray(v, newLabelIds) {
+			removeLabelIds = append(removeLabelIds, v)
+		}
+	}
+
+	return
+}
+
+func inArray(ele uint64, array []uint64) bool {
+	for _, v := range array {
+		if ele == v {
+			return true
+		}
+	}
+	return false
+}