|
@@ -2,6 +2,7 @@ package wechat
|
|
|
|
|
|
import (
|
|
import (
|
|
"context"
|
|
"context"
|
|
|
|
+ "github.com/zeromicro/go-zero/core/logx"
|
|
"wechat-api/ent"
|
|
"wechat-api/ent"
|
|
"wechat-api/ent/agent"
|
|
"wechat-api/ent/agent"
|
|
"wechat-api/ent/contact"
|
|
"wechat-api/ent/contact"
|
|
@@ -125,6 +126,15 @@ func (w *WxSafeChange) AgentSafe() error {
|
|
}
|
|
}
|
|
|
|
|
|
func (w *WxSafeChange) LabelSafe() 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().
|
|
contactLabelNames, err := w.Tx.Label.Query().
|
|
Where(
|
|
Where(
|
|
@@ -150,7 +160,6 @@ func (w *WxSafeChange) LabelSafe() error {
|
|
Where(
|
|
Where(
|
|
label.DeletedAtIsNil(),
|
|
label.DeletedAtIsNil(),
|
|
label.OrganizationIDEQ(w.OrganizationId),
|
|
label.OrganizationIDEQ(w.OrganizationId),
|
|
- label.NameNotIn(contactLabelNames...),
|
|
|
|
).
|
|
).
|
|
Select(label.FieldName).
|
|
Select(label.FieldName).
|
|
Strings(w.Ctx)
|
|
Strings(w.Ctx)
|
|
@@ -159,12 +168,27 @@ func (w *WxSafeChange) LabelSafe() error {
|
|
return err
|
|
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().
|
|
_, err = w.Tx.Label.Create().
|
|
SetName(labelName).
|
|
SetName(labelName).
|
|
SetOrganizationID(w.OrganizationId).
|
|
SetOrganizationID(w.OrganizationId).
|
|
|
|
+ SetConditions("{}").
|
|
Save(w.Ctx)
|
|
Save(w.Ctx)
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
@@ -173,6 +197,8 @@ func (w *WxSafeChange) LabelSafe() error {
|
|
}
|
|
}
|
|
|
|
|
|
// 建立新的标签 id 映射关系
|
|
// 建立新的标签 id 映射关系
|
|
|
|
+ logx.Info("contactLabelNames", contactLabelNames)
|
|
|
|
+ logx.Info("diffLabelNames", diffLabelNames)
|
|
orgLabels, err := w.Tx.Label.Query().
|
|
orgLabels, err := w.Tx.Label.Query().
|
|
Where(
|
|
Where(
|
|
label.DeletedAtIsNil(),
|
|
label.DeletedAtIsNil(),
|
|
@@ -186,6 +212,8 @@ func (w *WxSafeChange) LabelSafe() error {
|
|
}
|
|
}
|
|
labelSet := make(map[string]uint64)
|
|
labelSet := make(map[string]uint64)
|
|
for _, l := range orgLabels {
|
|
for _, l := range orgLabels {
|
|
|
|
+ logx.Info("l.Name", l.Name, l.ID)
|
|
|
|
+ logx.Info("l.ID", l.ID)
|
|
labelSet[l.Name] = l.ID
|
|
labelSet[l.Name] = l.ID
|
|
}
|
|
}
|
|
|
|
|
|
@@ -202,6 +230,9 @@ func (w *WxSafeChange) LabelSafe() error {
|
|
}
|
|
}
|
|
|
|
|
|
for _, relationship := range labelRelationships {
|
|
for _, relationship := range labelRelationships {
|
|
|
|
+ if relationship.Edges.Labels == nil {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
_, err = w.Tx.LabelRelationship.Create().
|
|
_, err = w.Tx.LabelRelationship.Create().
|
|
SetLabelID(labelSet[relationship.Edges.Labels.Name]).
|
|
SetLabelID(labelSet[relationship.Edges.Labels.Name]).
|
|
SetContactID(relationship.Edges.Contacts.ID).
|
|
SetContactID(relationship.Edges.Contacts.ID).
|
|
@@ -216,7 +247,7 @@ func (w *WxSafeChange) LabelSafe() error {
|
|
Where(
|
|
Where(
|
|
labelrelationship.DeletedAtIsNil(),
|
|
labelrelationship.DeletedAtIsNil(),
|
|
labelrelationship.HasLabelsWith(
|
|
labelrelationship.HasLabelsWith(
|
|
- label.OrganizationIDNEQ(w.OrganizationId)),
|
|
|
|
|
|
+ label.OrganizationIDNEQ(wxinfo.OrganizationID)),
|
|
labelrelationship.HasContactsWith(
|
|
labelrelationship.HasContactsWith(
|
|
contact.WxWxidEQ(w.WxId),
|
|
contact.WxWxidEQ(w.WxId),
|
|
),
|
|
),
|