|
@@ -5,8 +5,9 @@ import (
|
|
|
"github.com/spf13/cast"
|
|
|
"github.com/suyuan32/simple-admin-common/enum/errorcode"
|
|
|
"github.com/suyuan32/simple-admin-common/msg/errormsg"
|
|
|
+ "strings"
|
|
|
+ "wechat-api/ent/label"
|
|
|
"wechat-api/hook"
|
|
|
-
|
|
|
"wechat-api/internal/svc"
|
|
|
"wechat-api/internal/types"
|
|
|
|
|
@@ -49,10 +50,12 @@ func (l *GetFriendsAndGroupsLogic) GetFriendsAndGroups(req *types.IDReq) (resp *
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+ newFriendIds := make(map[uint64][]string)
|
|
|
+ newLagIds := make(map[string]struct{})
|
|
|
friendCnt := cast.ToInt(friendAndChatRoomList.CountFriend)
|
|
|
if friendCnt > 0 {
|
|
|
for _, friend := range friendAndChatRoomList.Friend {
|
|
|
- l.svcCtx.DB.Contact.Create().
|
|
|
+ id, err := l.svcCtx.DB.Contact.Create().
|
|
|
SetWxWxid(wxInfo.Wxid).
|
|
|
SetType(1).
|
|
|
SetWxid(friend.Wxid).
|
|
@@ -70,6 +73,65 @@ func (l *GetFriendsAndGroupsLogic) GetFriendsAndGroups(req *types.IDReq) (resp *
|
|
|
UpdateNewValues().
|
|
|
SetOrganizationID(organizationId).
|
|
|
ID(l.ctx)
|
|
|
+ if err == nil {
|
|
|
+ lags := splitStringToIntArray(friend.Lag)
|
|
|
+ newFriendIds[id] = lags
|
|
|
+ for _, lag := range lags {
|
|
|
+ if _, exists := newLagIds[lag]; !exists {
|
|
|
+ newLagIds[lag] = struct{}{}
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 平台和微信标签映射
|
|
|
+ if len(newLagIds) > 0 {
|
|
|
+ wxLagIdsSet := make(map[string]string)
|
|
|
+ wxLagIds, err := hookClient.GetContactLabelList()
|
|
|
+ if err == nil {
|
|
|
+ wxSysSet := make(map[string]uint64)
|
|
|
+ for _, wxLagId := range wxLagIds.Label {
|
|
|
+ wxLagIdsSet[wxLagId.Id] = wxLagId.Name
|
|
|
+ }
|
|
|
+ for lagId := range newLagIds {
|
|
|
+ name := wxLagIdsSet[lagId]
|
|
|
+ label, err := l.svcCtx.DB.Label.Query().
|
|
|
+ Where(
|
|
|
+ label.NameEQ(name), // Filter by ID
|
|
|
+ label.TypeEQ(1),
|
|
|
+ label.OrganizationID(organizationId), // Additional filter by organizationId
|
|
|
+ ).
|
|
|
+ Only(l.ctx)
|
|
|
+ if err != nil {
|
|
|
+ // 如果标签不存在则创建
|
|
|
+ newLabel, err := l.svcCtx.DB.Label.Create().
|
|
|
+ SetType(1).
|
|
|
+ SetName(name).
|
|
|
+ SetFrom(2).
|
|
|
+ SetMode(2).
|
|
|
+ SetConditions("{}").
|
|
|
+ SetOrganizationID(organizationId).
|
|
|
+ Save(l.ctx)
|
|
|
+ if err == nil {
|
|
|
+ wxSysSet[lagId] = newLabel.ID
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 如果标签存在
|
|
|
+ wxSysSet[lagId] = label.ID
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 为新联系人打标签
|
|
|
+ if len(newFriendIds) > 0 && len(newLagIds) > 0 {
|
|
|
+ for id, lags := range newFriendIds {
|
|
|
+ for _, lag := range lags {
|
|
|
+ l.svcCtx.DB.LabelRelationship.Create().
|
|
|
+ SetLabelID(wxSysSet[lag]).
|
|
|
+ SetContactID(id).
|
|
|
+ SetOrganizationID(organizationId).
|
|
|
+ Save(l.ctx)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
chatroomCnt := cast.ToInt(friendAndChatRoomList.CountChatroom)
|
|
@@ -114,3 +176,12 @@ func (l *GetFriendsAndGroupsLogic) GetFriendsAndGroups(req *types.IDReq) (resp *
|
|
|
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+func splitStringToIntArray(input string) []string {
|
|
|
+ strArray := strings.Split(input, ",")
|
|
|
+ intArray := make([]string, len(strArray))
|
|
|
+ for i, str := range strArray {
|
|
|
+ intArray[i] = str
|
|
|
+ }
|
|
|
+ return intArray
|
|
|
+}
|