update_label_relationships_logic.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package label_relationship
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "wechat-api/ent"
  6. "wechat-api/ent/contact"
  7. "wechat-api/ent/custom_types"
  8. "wechat-api/ent/labelrelationship"
  9. "wechat-api/ent/messagerecords"
  10. "wechat-api/ent/soptask"
  11. "wechat-api/internal/svc"
  12. "wechat-api/internal/types"
  13. "wechat-api/internal/utils/dberrorhandler"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. )
  16. type UpdateLabelRelationshipsLogic struct {
  17. logx.Logger
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. }
  21. func NewUpdateLabelRelationshipsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLabelRelationshipsLogic {
  22. return &UpdateLabelRelationshipsLogic{
  23. Logger: logx.WithContext(ctx),
  24. ctx: ctx,
  25. svcCtx: svcCtx}
  26. }
  27. func (l *UpdateLabelRelationshipsLogic) UpdateLabelRelationships(req *types.LabelRelationshipsInfo) (resp *types.BaseMsgResp, err error) {
  28. // 开始事务
  29. tx, err := l.svcCtx.DB.Tx(context.Background())
  30. if err != nil {
  31. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  32. }
  33. // 获取联系人信息
  34. c, err := tx.Contact.Query().Where(contact.ID(*req.ContactId)).Only(l.ctx)
  35. // 获取联系人当前已关联的标签
  36. currentLabelRelationships, err := tx.LabelRelationship.Query().Where(labelrelationship.ContactID(*req.ContactId)).All(l.ctx)
  37. if err != nil {
  38. _ = tx.Rollback()
  39. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  40. }
  41. // 提取当前标签ID
  42. var currentLabelIds []uint64
  43. for _, relationship := range currentLabelRelationships {
  44. currentLabelIds = append(currentLabelIds, relationship.LabelID)
  45. }
  46. // 对比新旧标签ID,找出需要新增和移除的标签
  47. addLabelIds, removeLabelIds := compareLabelIds(req.LabelIds, currentLabelIds)
  48. // 如果 req.UpdateType 为空,或 req.UpdateType 的值为 “all” 时
  49. if req.UpdateType == nil || *req.UpdateType == "all" {
  50. // 删除需要移除的标签关系
  51. for _, id := range removeLabelIds {
  52. _, err := tx.LabelRelationship.
  53. Delete().
  54. Where(
  55. labelrelationship.ContactID(*req.ContactId),
  56. labelrelationship.LabelID(id),
  57. ).
  58. Exec(l.ctx)
  59. if err != nil {
  60. _ = tx.Rollback()
  61. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  62. }
  63. }
  64. }
  65. // 创建需要新增的标签关系
  66. for _, id := range addLabelIds {
  67. _, _ = tx.LabelRelationship.Create().
  68. SetLabelID(id).
  69. SetContactID(*req.ContactId).
  70. Save(l.ctx)
  71. //if err != nil {
  72. // return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  73. //}
  74. }
  75. currentLabelIds = req.LabelIds
  76. // 获取所有 status 为 3 且 bot_wxid_list 包含 c.wx_wxid 的 sop_task
  77. sopTasks, err := tx.SopTask.Query().Where(soptask.Status(3)).All(l.ctx)
  78. l.Logger.Info("UpdateLabelRelationships sopTasks:", sopTasks)
  79. if err != nil {
  80. _ = tx.Rollback()
  81. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  82. }
  83. var filteredSopTasks []*ent.SopTask
  84. for _, task := range sopTasks {
  85. for _, botWxid := range task.BotWxidList {
  86. if botWxid == c.WxWxid {
  87. filteredSopTasks = append(filteredSopTasks, task)
  88. break
  89. }
  90. }
  91. }
  92. // 获取所有 filteredSopTasks 的 sop_stages
  93. var sopStages []*ent.SopStage
  94. for _, task := range filteredSopTasks {
  95. stages, err := task.QueryTaskStages().All(l.ctx)
  96. if err != nil {
  97. _ = tx.Rollback()
  98. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  99. }
  100. sopStages = append(sopStages, stages...)
  101. }
  102. err = l.AddLabelRelationships(sopStages, *c, currentLabelIds)
  103. if err != nil {
  104. _ = tx.Rollback()
  105. return nil, err
  106. }
  107. // 所有操作成功,提交事务
  108. err = tx.Commit()
  109. if err != nil {
  110. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  111. }
  112. return &types.BaseMsgResp{
  113. Code: 0,
  114. Msg: errormsg.Success,
  115. }, nil
  116. }
  117. func (l *UpdateLabelRelationshipsLogic) AddLabelRelationships(sopStages []*ent.SopStage, contact ent.Contact, currentLabelIds []uint64) (err error) {
  118. // 开始事务
  119. tx, err := l.svcCtx.DB.Tx(context.Background())
  120. if err != nil {
  121. return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  122. }
  123. // 遍历 sop_stages,找出满足条件的 stage
  124. for _, stage := range sopStages {
  125. if stage.ConditionType == 1 && isLabelIdListMatchFilter(currentLabelIds, stage.ConditionOperator, stage.ConditionList) {
  126. // 判断是否有 contact_wxid、source_type、source_id、sub_source_id 相同的记录
  127. _, err = tx.MessageRecords.Query().
  128. Where(
  129. messagerecords.ContactWxid(contact.Wxid),
  130. messagerecords.SourceType(3),
  131. messagerecords.SourceID(stage.ID),
  132. messagerecords.SubSourceID(0),
  133. ).
  134. Only(l.ctx)
  135. if err == nil {
  136. continue
  137. }
  138. // 判断ActionMessage是否为空
  139. sourceType := 3
  140. if stage.ActionMessage != nil {
  141. for _, message := range stage.ActionMessage {
  142. _, _ = tx.MessageRecords.Create().
  143. SetNotNilBotWxid(&contact.WxWxid).
  144. SetNotNilContactID(&contact.ID).
  145. SetNotNilContactType(&contact.Type).
  146. SetNotNilContactWxid(&contact.Wxid).
  147. SetNotNilContentType(&message.Type).
  148. SetNotNilContent(&message.Content).
  149. SetNotNilSourceType(&sourceType).
  150. SetNotNilSourceID(&stage.ID).
  151. Save(l.ctx)
  152. //if err != nil {
  153. // return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  154. //}
  155. }
  156. }
  157. if stage.ActionLabel != nil {
  158. // 获取 addLabelIds 中不在 currentLabelIds 中的标签ID
  159. var newLabelIds []uint64
  160. // 创建一个映射,用于快速查找 currentLabelIds 中的元素
  161. currentLabelIdSet := make(map[uint64]struct{})
  162. for _, id := range currentLabelIds {
  163. currentLabelIdSet[id] = struct{}{}
  164. }
  165. // 遍历 addLabelIds,找出不在 currentLabelIds 中的元素
  166. for _, id := range stage.ActionLabel {
  167. if _, exists := currentLabelIdSet[id]; !exists {
  168. newLabelIds = append(newLabelIds, id)
  169. }
  170. }
  171. if len(newLabelIds) == 0 {
  172. return nil
  173. }
  174. // 创建需要新增的标签关系
  175. for _, id := range newLabelIds {
  176. _, err = tx.LabelRelationship.Create().
  177. SetLabelID(id).
  178. SetContactID(contact.ID).
  179. Save(l.ctx)
  180. //if err != nil {
  181. // return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  182. //}
  183. }
  184. // 合并 currentLabelIds 和 newLabelIds
  185. currentLabelIds = append(currentLabelIds, newLabelIds...)
  186. // 递归调用 AddLabelRelationships
  187. err = l.AddLabelRelationships(sopStages, contact, currentLabelIds)
  188. if err != nil {
  189. _ = tx.Rollback()
  190. return err
  191. }
  192. }
  193. }
  194. }
  195. // 所有操作成功,提交事务
  196. err = tx.Commit()
  197. if err != nil {
  198. return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  199. }
  200. return nil
  201. }
  202. // compareLabelIds compares the new label ids with the current ones and returns the ids to be added and removed
  203. func compareLabelIds(newLabelIds []uint64, currentLabelIds []uint64) (addLabelIds []uint64, removeLabelIds []uint64) {
  204. newLabelIdSet := make(map[uint64]struct{}, len(newLabelIds))
  205. for _, id := range newLabelIds {
  206. newLabelIdSet[id] = struct{}{}
  207. }
  208. for _, id := range currentLabelIds {
  209. if _, ok := newLabelIdSet[id]; ok {
  210. delete(newLabelIdSet, id)
  211. } else {
  212. removeLabelIds = append(removeLabelIds, id)
  213. }
  214. }
  215. for id := range newLabelIdSet {
  216. addLabelIds = append(addLabelIds, id)
  217. }
  218. return
  219. }
  220. func isLabelIdListMatchFilter(labelIdList []uint64, conditionOperator int, conditionList []custom_types.Condition) bool {
  221. labelIdSet := make(map[uint64]struct{})
  222. for _, id := range labelIdList {
  223. labelIdSet[id] = struct{}{}
  224. }
  225. for _, condition := range conditionList {
  226. match := false
  227. for _, id := range condition.LabelIdList {
  228. if _, ok := labelIdSet[id]; ok {
  229. match = true
  230. break
  231. }
  232. }
  233. if condition.Equal == 2 {
  234. match = !match
  235. }
  236. if (conditionOperator == 1 && !match) || (conditionOperator == 2 && match) {
  237. return match
  238. }
  239. }
  240. return conditionOperator == 1
  241. }