123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package MessageHandlers
- import (
- "context"
- "encoding/json"
- "entgo.io/ent/dialect/sql"
- "github.com/zeromicro/go-zero/core/logx"
- "strconv"
- "time"
- "wechat-api/ent"
- "wechat-api/ent/wx"
- "wechat-api/internal/pkg/wechat_ws"
- "wechat-api/internal/svc"
- "wechat-api/workphone"
- )
- type ContactLabelInfoNotice struct {
- svcCtx *svc.ServiceContext
- }
- func NewContactLabelInfoNotice(svcCtx *svc.ServiceContext) *ContactLabelInfoNotice {
- return &ContactLabelInfoNotice{
- svcCtx: svcCtx,
- }
- }
- // Handle 实现 MessageHandlerStrategy 接口
- func (f *ContactLabelInfoNotice) Handle(ctx context.Context, msg *wechat_ws.MsgJsonObject, svcCtx *svc.ServiceContext) error {
- message := workphone.ContactLabelInfoNoticeMessage{}
- logx.Infof("msg.Message 的内容是:%s", msg.Message)
- err := json.Unmarshal([]byte(msg.Message), &message)
- if err != nil {
- return err
- }
- // 拿到租户 id
- wxInfo, err := svcCtx.DB.Wx.Query().
- Where(
- wx.WxidEQ(message.WeChatId), // Additional filter by organizationId
- ).
- Only(ctx)
- if err != nil {
- return err
- }
- var bulkCreates []*ent.LabelLogCreate
- var labelCreates []*ent.LabelCreate
- for _, label := range message.Labels {
- labelIDSet := make(map[int]struct{})
- labelIDSet[int(label.LabelId)] = struct{}{}
- var labelIDs []int
- for id := range labelIDSet {
- labelIDs = append(labelIDs, id)
- }
- existingMap := make(map[int]struct{})
- // 只拼接还没插入过的 label_id + organization_id 组合
- if _, exists := existingMap[int(label.LabelId)]; exists {
- logx.Error("label_log---已经存在的: ", wxInfo.OrganizationID, label.LabelId)
- continue
- }
- tsInt, err := strconv.ParseInt(label.CreateTime, 10, 64)
- if err != nil {
- logx.Errorf("时间戳转换失败: %v", err)
- continue
- }
- bulkCreates = append(bulkCreates,
- svcCtx.DB.LabelLog.Create().
- SetLabelName(label.LabelName).
- SetLabelID(int(label.LabelId)).
- SetOrganizationID(wxInfo.OrganizationID).
- SetWxID(message.WeChatId).
- SetCreatedAt(time.Unix(tsInt/1000, 0)),
- //SetUpdatedAt(time.Now()),
- )
- //label 主表
- labelCreates = append(labelCreates,
- svcCtx.DB.Label.Create().
- //SetID(int(label.LabelId)).
- SetName(label.LabelName).
- SetType(1).
- SetStatus(1).
- SetOrganizationID(wxInfo.OrganizationID).
- SetFrom(2).
- SetMode(2).
- SetConditions(string(json.RawMessage(`{}`))).
- SetCreatedAt(time.Now()).
- SetUpdatedAt(time.Now()),
- )
- logx.Info("数据:", label.LabelName+"-----", label.LabelId, wxInfo.OrganizationID, message.WeChatId, time.Unix(tsInt/1000, 0))
- }
- //批量插入labelLog
- if len(bulkCreates) > 0 {
- err := svcCtx.DB.LabelLog.CreateBulk(bulkCreates...).
- OnConflict(
- sql.ConflictColumns("label_id", "organization_id"),
- ).
- DoNothing().
- Exec(ctx)
- if err != nil {
- logx.Error("labelLog 批量插入失败", bulkCreates)
- //return err
- }
- }
- //批量插入label
- if len(labelCreates) > 0 {
- err := svcCtx.DB.Label.CreateBulk(labelCreates...).
- OnConflict(
- sql.ConflictColumns("name", "organization_id"),
- ).
- DoNothing().
- Exec(ctx)
- if err != nil {
- logx.Error("label 批量插入失败", labelCreates)
- return err
- }
- }
- return nil
- }
|