|
@@ -57,31 +57,30 @@ func (l *SetWhatsappContactLabelLogic) SetWhatsappContactLabel(req *types.LabelR
|
|
|
}
|
|
|
|
|
|
// 对比新旧标签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)
|
|
|
|
|
|
// 如果 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)
|
|
|
- for _, id := range addLabelIds {
|
|
|
+ for _, id := range req.LabelIds {
|
|
|
_, _ = tx.LabelRelationship.Create().
|
|
|
SetLabelID(id).
|
|
|
SetContactID(*req.ContactId).
|
|
@@ -91,6 +90,32 @@ func (l *SetWhatsappContactLabelLogic) SetWhatsappContactLabel(req *types.LabelR
|
|
|
// return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
|
|
|
//}
|
|
|
}
|
|
|
+ _ = tx.Commit()
|
|
|
|
|
|
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
|
|
|
+}
|