friend_push_notice.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package MessageHandlers
  2. import (
  3. "context"
  4. "encoding/json"
  5. "entgo.io/ent/dialect/sql"
  6. "github.com/zeromicro/go-zero/core/logx"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "wechat-api/ent"
  11. "wechat-api/ent/label"
  12. "wechat-api/ent/labellog"
  13. "wechat-api/ent/labelrelationship"
  14. "wechat-api/ent/wx"
  15. "wechat-api/internal/pkg/wechat_ws"
  16. "wechat-api/internal/svc"
  17. "wechat-api/workphone"
  18. )
  19. type FriendPushNoticeHandler struct {
  20. svcCtx *svc.ServiceContext
  21. }
  22. func NewFriendPushNoticeHandler(svcCtx *svc.ServiceContext) *FriendPushNoticeHandler {
  23. return &FriendPushNoticeHandler{
  24. svcCtx: svcCtx,
  25. }
  26. }
  27. func (f *FriendPushNoticeHandler) Handler(msg *wechat_ws.MsgJsonObject) error {
  28. if msg.MsgType == "FriendPushNotice" {
  29. message := workphone.FriendPushNoticeMessage{}
  30. err := json.Unmarshal([]byte(msg.Message), &message)
  31. if err != nil {
  32. return err
  33. }
  34. // 拿到租户 id
  35. wx_info, err := f.svcCtx.DB.Wx.Query().
  36. Where(
  37. wx.WxidEQ(message.WeChatId), // Additional filter by organizationId
  38. ).
  39. Only(context.TODO())
  40. //hookClient := hook.NewHook("", "", "")
  41. for _, friend := range message.Friends {
  42. friendType := 1
  43. //if friend.Type == 1 {
  44. // friendType = 2
  45. // _ = hookClient.RequestChatRoomInfo(friend.FriendId, message.WeChatId)
  46. //} else {
  47. // friendType = 1
  48. //}
  49. _, err = f.svcCtx.DB.Contact.Create().
  50. SetWxWxid(message.WeChatId).
  51. SetType(friendType).
  52. SetWxid(friend.FriendId).
  53. SetAccount(friend.FriendNo).
  54. SetNickname(friend.FriendNick).
  55. SetMarkname(friend.Memo).
  56. SetHeadimg(friend.Avatar).
  57. //SetSex(cast.ToInt(friend.Gender)).
  58. SetOrganizationID(wx_info.OrganizationID).
  59. OnConflict().
  60. UpdateNewValues().
  61. SetType(friendType).
  62. SetOrganizationID(wx_info.OrganizationID).
  63. ID(context.TODO())
  64. if err != nil {
  65. logx.Error("Contact.Create: ", wx_info.OrganizationID)
  66. return err
  67. }
  68. }
  69. }
  70. return nil
  71. }
  72. type FriendPushNoticeTypeHandler struct {
  73. svcCtx *svc.ServiceContext
  74. }
  75. func NewFriendPushNoticeTypeHandler(svcCtx *svc.ServiceContext) *FriendPushNoticeTypeHandler {
  76. return &FriendPushNoticeTypeHandler{
  77. svcCtx: svcCtx,
  78. }
  79. }
  80. // Handle 实现 MessageHandlerStrategy 接口
  81. func (f *FriendPushNoticeTypeHandler) Handle(ctx context.Context, msg *wechat_ws.MsgJsonObject, svcCtx *svc.ServiceContext) error {
  82. message := workphone.FriendPushNoticeMessage{}
  83. err := json.Unmarshal([]byte(msg.Message), &message)
  84. logx.Infof("msg.Message 的内容是:%s", msg.Message)
  85. if err != nil {
  86. logx.Errorf("Unmarshal.fail")
  87. return err
  88. }
  89. // 拿到租户 id
  90. wxInfo, err := svcCtx.DB.Wx.Query().
  91. Where(
  92. wx.WxidEQ(message.WeChatId), // Additional filter by organizationId
  93. ).Only(ctx)
  94. if err != nil {
  95. return err
  96. }
  97. var labelRelationshipCreates []*ent.LabelRelationshipCreate
  98. for _, friend := range message.Friends {
  99. var friendId uint64
  100. friendType := 1
  101. friendId, err = svcCtx.DB.Contact.Create().
  102. SetWxWxid(message.WeChatId).
  103. SetType(friendType).
  104. SetWxid(friend.FriendId).
  105. SetAccount(friend.FriendNo).
  106. SetNickname(friend.FriendNick).
  107. SetMarkname(friend.Memo).
  108. SetHeadimg(friend.Avatar).
  109. SetOrganizationID(wxInfo.OrganizationID).
  110. OnConflict().
  111. UpdateNewValues().
  112. SetType(friendType).
  113. SetOrganizationID(wxInfo.OrganizationID).
  114. ID(ctx)
  115. if err != nil {
  116. logx.Errorf("Contact.Create 失败, OrgID=%d, err=%v", wxInfo.OrganizationID, err)
  117. return err
  118. }
  119. //判断friend里的labelId="1,2,3,4,5"为空就不处理了,不为空的时候就查下label表里有没有这个labelId,没有就插入,有就跳过
  120. if friend.LabelIds == "" {
  121. logx.Infof("没有labelIds 失败, wx_wxId=%v", message.WeChatId)
  122. continue
  123. }
  124. //获取labelId,并且按照逗号去分割成数组
  125. labelIdsStr := friend.LabelIds
  126. var ids []int
  127. ids, err = ParseCSVToIntSlice(labelIdsStr)
  128. if err != nil {
  129. logx.Infof("labelstring切割失败, labelIds=%v", labelIdsStr)
  130. continue
  131. }
  132. //转换成labelIds的切片,去labelLog里 用labelId in 查下一下数据。
  133. //labelIds := strings.Split(labelIdsStr, ",")
  134. //ids, _ = ParseCSVToIntSlice(labelIdsStr)
  135. LabelLogs, err := svcCtx.DB.LabelLog.Query().
  136. Where(labellog.LabelIDIn(ids...)).
  137. Where(labellog.WxID(message.WeChatId)).
  138. All(ctx)
  139. if err != nil || len(LabelLogs) == 0 {
  140. logx.Error("labelLog.Query.fail: 跳过 || 或者查询失败", wxInfo.OrganizationID)
  141. continue
  142. }
  143. //映射本地的name + type + model + organization_id
  144. currentOrgID := wxInfo.OrganizationID
  145. for _, remoteLabel := range LabelLogs {
  146. labelInfo, err := svcCtx.DB.Label.Query().Where(
  147. label.NameEQ(remoteLabel.LabelName),
  148. //label.StatusEQ(remoteLabel.LabelName),
  149. label.OrganizationID(currentOrgID),
  150. ).Only(ctx)
  151. if err != nil || ent.IsNotFound(err) {
  152. logx.Error("label not found.fail: ", wxInfo.OrganizationID)
  153. continue
  154. }
  155. //svcCtx.DB.LabelRelationship.Create().
  156. // SetOrganizationID(wxInfo.OrganizationID).
  157. // SetContactID(friendId).
  158. // SetLabelID(labelInfo.ID).
  159. // SetAccount(friend.FriendNo).
  160. // SetNickname(friend.FriendNick).
  161. // SetMarkname(friend.Memo).
  162. // SetHeadimg(friend.Avatar).
  163. // SetOrganizationID(wxInfo.OrganizationID).
  164. // OnConflict().
  165. // UpdateNewValues().
  166. // SetType(friendType).
  167. // SetOrganizationID(wxInfo.OrganizationID).
  168. // ID(ctx)
  169. //生成批量的关系数据 待插入
  170. labelRelationshipCreates = append(labelRelationshipCreates,
  171. svcCtx.DB.LabelRelationship.Create().
  172. //SetID(int(label.LabelId)).
  173. SetOrganizationID(wxInfo.OrganizationID).
  174. SetContactID(friendId).
  175. SetStatus(1).
  176. SetLabelID(labelInfo.ID).
  177. SetCreatedAt(time.Now()).
  178. SetUpdatedAt(time.Now()),
  179. )
  180. //
  181. }
  182. }
  183. if len(labelRelationshipCreates) > 0 {
  184. errShip := svcCtx.DB.LabelRelationship.CreateBulk(labelRelationshipCreates...).
  185. OnConflict(
  186. sql.ConflictColumns(labelrelationship.FieldLabelID, labelrelationship.FieldContactID),
  187. ).DoNothing().Exec(ctx)
  188. if errShip != nil {
  189. logx.Error("label_relationship.create.fail: ", wxInfo.OrganizationID, labelRelationshipCreates)
  190. return err
  191. }
  192. }
  193. return nil
  194. }
  195. func ParseCSVToIntSlice(csv string) ([]int, error) {
  196. if csv == "" {
  197. return nil, nil
  198. }
  199. parts := strings.Split(csv, ",")
  200. var result []int
  201. for _, p := range parts {
  202. trimmed := strings.TrimSpace(p)
  203. if trimmed == "" {
  204. continue // 忽略空字符串
  205. }
  206. n, err := strconv.Atoi(trimmed)
  207. if err != nil {
  208. logx.Error("无法将 %s 转换为整数: %v", trimmed, err)
  209. continue // 忽略无效字符
  210. }
  211. result = append(result, n)
  212. }
  213. return result, nil
  214. }