|
@@ -7,12 +7,14 @@ import (
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
+ "sync"
|
|
|
"time"
|
|
|
"wechat-api/ent"
|
|
|
"wechat-api/ent/label"
|
|
|
"wechat-api/ent/labellog"
|
|
|
"wechat-api/ent/labelrelationship"
|
|
|
"wechat-api/ent/wx"
|
|
|
+ "wechat-api/internal/lock"
|
|
|
"wechat-api/internal/pkg/wechat_ws"
|
|
|
"wechat-api/internal/svc"
|
|
|
"wechat-api/workphone"
|
|
@@ -76,7 +78,8 @@ func (f *FriendPushNoticeHandler) Handler(msg *wechat_ws.MsgJsonObject) error {
|
|
|
}
|
|
|
|
|
|
type FriendPushNoticeTypeHandler struct {
|
|
|
- svcCtx *svc.ServiceContext
|
|
|
+ svcCtx *svc.ServiceContext
|
|
|
+ lockMap sync.Map // 微信号 -> *sync.Mutex
|
|
|
}
|
|
|
|
|
|
func NewFriendPushNoticeTypeHandler(svcCtx *svc.ServiceContext) *FriendPushNoticeTypeHandler {
|
|
@@ -106,6 +109,13 @@ func (f *FriendPushNoticeTypeHandler) Handle(ctx context.Context, msg *wechat_ws
|
|
|
for _, friend := range message.Friends {
|
|
|
var friendId uint64
|
|
|
friendType := 1
|
|
|
+ if friend.Type == 1 {
|
|
|
+ friendType = 4
|
|
|
+ } else {
|
|
|
+ friendType = 1
|
|
|
+ }
|
|
|
+
|
|
|
+ //修改拉黑后的状态被重置
|
|
|
friendId, err = svcCtx.DB.Contact.Create().
|
|
|
SetWxWxid(message.WeChatId).
|
|
|
SetType(friendType).
|
|
@@ -116,9 +126,14 @@ func (f *FriendPushNoticeTypeHandler) Handle(ctx context.Context, msg *wechat_ws
|
|
|
SetHeadimg(friend.Avatar).
|
|
|
SetOrganizationID(wxInfo.OrganizationID).
|
|
|
OnConflict().
|
|
|
- UpdateNewValues().
|
|
|
- SetType(friendType).
|
|
|
- SetOrganizationID(wxInfo.OrganizationID).
|
|
|
+ UpdateWxWxid().
|
|
|
+ UpdateWxid().
|
|
|
+ UpdateType().
|
|
|
+ UpdateAccount().
|
|
|
+ UpdateNickname().
|
|
|
+ UpdateMarkname().
|
|
|
+ UpdateHeadimg().
|
|
|
+ UpdateOrganizationID().
|
|
|
ID(ctx)
|
|
|
if err != nil {
|
|
|
logx.Errorf("Contact.Create 失败, OrgID=%d, err=%v", wxInfo.OrganizationID, err)
|
|
@@ -164,23 +179,6 @@ func (f *FriendPushNoticeTypeHandler) Handle(ctx context.Context, msg *wechat_ws
|
|
|
logx.Error("label not found.fail: ", wxInfo.OrganizationID)
|
|
|
continue
|
|
|
}
|
|
|
-
|
|
|
- //svcCtx.DB.LabelRelationship.Create().
|
|
|
- // SetOrganizationID(wxInfo.OrganizationID).
|
|
|
- // SetContactID(friendId).
|
|
|
- // SetLabelID(labelInfo.ID).
|
|
|
- // SetAccount(friend.FriendNo).
|
|
|
- // SetNickname(friend.FriendNick).
|
|
|
- // SetMarkname(friend.Memo).
|
|
|
- // SetHeadimg(friend.Avatar).
|
|
|
- // SetOrganizationID(wxInfo.OrganizationID).
|
|
|
- // OnConflict().
|
|
|
- // UpdateNewValues().
|
|
|
- // SetType(friendType).
|
|
|
- // SetOrganizationID(wxInfo.OrganizationID).
|
|
|
- // ID(ctx)
|
|
|
-
|
|
|
- //生成批量的关系数据 待插入
|
|
|
labelRelationshipCreates = append(labelRelationshipCreates,
|
|
|
svcCtx.DB.LabelRelationship.Create().
|
|
|
//SetID(int(label.LabelId)).
|
|
@@ -195,14 +193,15 @@ func (f *FriendPushNoticeTypeHandler) Handle(ctx context.Context, msg *wechat_ws
|
|
|
}
|
|
|
}
|
|
|
if len(labelRelationshipCreates) > 0 {
|
|
|
- errShip := svcCtx.DB.LabelRelationship.CreateBulk(labelRelationshipCreates...).
|
|
|
- OnConflict(
|
|
|
- sql.ConflictColumns(labelrelationship.FieldLabelID, labelrelationship.FieldContactID),
|
|
|
- ).DoNothing().Exec(ctx)
|
|
|
- if errShip != nil {
|
|
|
- logx.Error("label_relationship.create.fail: ", wxInfo.OrganizationID, labelRelationshipCreates)
|
|
|
- return err
|
|
|
- }
|
|
|
+ lock.GetWxIdLockManager().RunWithLock(message.WeChatId, func() {
|
|
|
+ errShip := svcCtx.DB.LabelRelationship.CreateBulk(labelRelationshipCreates...).
|
|
|
+ OnConflict(
|
|
|
+ sql.ConflictColumns(labelrelationship.FieldLabelID, labelrelationship.FieldContactID),
|
|
|
+ ).DoNothing().Exec(ctx)
|
|
|
+ if errShip != nil {
|
|
|
+ logx.Error("label_relationship.create.fail: ", wxInfo.OrganizationID, labelRelationshipCreates)
|
|
|
+ }
|
|
|
+ })
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
@@ -227,3 +226,8 @@ func ParseCSVToIntSlice(csv string) ([]int, error) {
|
|
|
}
|
|
|
return result, nil
|
|
|
}
|
|
|
+
|
|
|
+func (f *FriendPushNoticeTypeHandler) getWxLock(wxid string) *sync.Mutex {
|
|
|
+ actual, _ := f.lockMap.LoadOrStore(wxid, &sync.Mutex{})
|
|
|
+ return actual.(*sync.Mutex)
|
|
|
+}
|