boweniac 10 hours ago
parent
commit
2fe9e52d18

+ 2 - 3
internal/logic/label/delete_label_logic.go

@@ -36,10 +36,9 @@ func (l *DeleteLabelLogic) DeleteLabel(req *types.IDsReq) (*types.BaseMsgResp, e
 		Where(
 			labelrelationship.HasLabelsWith(
 				label.IDIn(req.Ids...),
-			),                                                // Filter by ID
-			labelrelationship.OrganizationID(organizationId), // Additional filter by organizationId
+				label.OrganizationIDEQ(organizationId),
+			),
 			labelrelationship.HasContactsWith(
-				contact.OrganizationIDEQ(organizationId),
 				contact.DeletedAtIsNil(),
 			),
 		).

+ 4 - 1
internal/logic/label_tagging/delete_label_tagging_logic.go

@@ -27,7 +27,10 @@ func NewDeleteLabelTaggingLogic(ctx context.Context, svcCtx *svc.ServiceContext)
 
 func (l *DeleteLabelTaggingLogic) DeleteLabelTagging(req *types.IDsReq) (resp *types.BaseMsgResp, err error) {
 	organizationId := l.ctx.Value("organizationId").(uint64)
-	_, err = l.svcCtx.DB.LabelTagging.Delete().Where(labeltagging.IDIn(req.Ids...), labeltagging.OrganizationID(organizationId)).Exec(l.ctx)
+	_, err = l.svcCtx.DB.LabelTagging.Delete().Where(
+		labeltagging.IDIn(req.Ids...),
+		labeltagging.OrganizationID(organizationId),
+	).Exec(l.ctx)
 
 	if err != nil {
 		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)

+ 35 - 4
internal/service/wechat/wx_safe_change.go

@@ -2,6 +2,7 @@ package wechat
 
 import (
 	"context"
+	"github.com/zeromicro/go-zero/core/logx"
 	"wechat-api/ent"
 	"wechat-api/ent/agent"
 	"wechat-api/ent/contact"
@@ -125,6 +126,15 @@ func (w *WxSafeChange) AgentSafe() error {
 }
 
 func (w *WxSafeChange) LabelSafe() error {
+	wxinfo, err := w.Tx.Wx.Query().
+		Where(
+			wx.Wxid(w.WxId),
+		).
+		Only(w.Ctx)
+	if err != nil || wxinfo == nil {
+		return err
+	}
+
 	// 获取现有联系人的所有标签名称
 	contactLabelNames, err := w.Tx.Label.Query().
 		Where(
@@ -150,7 +160,6 @@ func (w *WxSafeChange) LabelSafe() error {
 		Where(
 			label.DeletedAtIsNil(),
 			label.OrganizationIDEQ(w.OrganizationId),
-			label.NameNotIn(contactLabelNames...),
 		).
 		Select(label.FieldName).
 		Strings(w.Ctx)
@@ -159,12 +168,27 @@ func (w *WxSafeChange) LabelSafe() error {
 		return err
 	}
 
-	if newLabelNames == nil && len(newLabelNames) > 0 {
+	// 构建 newLabelNames 的 set
+	newLabelSet := make(map[string]struct{})
+	for _, name := range newLabelNames {
+		newLabelSet[name] = struct{}{}
+	}
+
+	// 找出 contactLabelNames 中 newLabelNames 没有的
+	var diffLabelNames []string
+	for _, name := range contactLabelNames {
+		if _, exists := newLabelSet[name]; !exists {
+			diffLabelNames = append(diffLabelNames, name)
+		}
+	}
+
+	if diffLabelNames != nil && len(diffLabelNames) > 0 {
 		// 创建新标签
-		for _, labelName := range newLabelNames {
+		for _, labelName := range diffLabelNames {
 			_, err = w.Tx.Label.Create().
 				SetName(labelName).
 				SetOrganizationID(w.OrganizationId).
+				SetConditions("{}").
 				Save(w.Ctx)
 			if err != nil {
 				return err
@@ -173,6 +197,8 @@ func (w *WxSafeChange) LabelSafe() error {
 	}
 
 	// 建立新的标签 id 映射关系
+	logx.Info("contactLabelNames", contactLabelNames)
+	logx.Info("diffLabelNames", diffLabelNames)
 	orgLabels, err := w.Tx.Label.Query().
 		Where(
 			label.DeletedAtIsNil(),
@@ -186,6 +212,8 @@ func (w *WxSafeChange) LabelSafe() error {
 	}
 	labelSet := make(map[string]uint64)
 	for _, l := range orgLabels {
+		logx.Info("l.Name", l.Name, l.ID)
+		logx.Info("l.ID", l.ID)
 		labelSet[l.Name] = l.ID
 	}
 
@@ -202,6 +230,9 @@ func (w *WxSafeChange) LabelSafe() error {
 	}
 
 	for _, relationship := range labelRelationships {
+		if relationship.Edges.Labels == nil {
+			continue
+		}
 		_, err = w.Tx.LabelRelationship.Create().
 			SetLabelID(labelSet[relationship.Edges.Labels.Name]).
 			SetContactID(relationship.Edges.Contacts.ID).
@@ -216,7 +247,7 @@ func (w *WxSafeChange) LabelSafe() error {
 		Where(
 			labelrelationship.DeletedAtIsNil(),
 			labelrelationship.HasLabelsWith(
-				label.OrganizationIDNEQ(w.OrganizationId)),
+				label.OrganizationIDNEQ(wxinfo.OrganizationID)),
 			labelrelationship.HasContactsWith(
 				contact.WxWxidEQ(w.WxId),
 			),