update_label_relationships_logic.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. package label_relationship
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "regexp"
  6. "wechat-api/ent"
  7. "wechat-api/ent/contact"
  8. "wechat-api/ent/custom_types"
  9. "wechat-api/ent/labelrelationship"
  10. "wechat-api/ent/messagerecords"
  11. "wechat-api/ent/soptask"
  12. "wechat-api/internal/svc"
  13. "wechat-api/internal/types"
  14. "wechat-api/internal/utils/dberrorhandler"
  15. "github.com/zeromicro/go-zero/core/logx"
  16. )
  17. type UpdateLabelRelationshipsLogic struct {
  18. logx.Logger
  19. ctx context.Context
  20. svcCtx *svc.ServiceContext
  21. }
  22. func NewUpdateLabelRelationshipsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLabelRelationshipsLogic {
  23. return &UpdateLabelRelationshipsLogic{
  24. Logger: logx.WithContext(ctx),
  25. ctx: ctx,
  26. svcCtx: svcCtx}
  27. }
  28. func (l *UpdateLabelRelationshipsLogic) UpdateLabelRelationships(req *types.LabelRelationshipsInfo) (resp *types.BaseMsgResp, err error) {
  29. organizationId := l.ctx.Value("organizationId").(uint64)
  30. // 开始事务
  31. tx, err := l.svcCtx.DB.Tx(context.Background())
  32. if err != nil {
  33. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  34. }
  35. // 获取联系人信息
  36. c, err := tx.Contact.Query().Where(contact.ID(*req.ContactId), contact.OrganizationIDEQ(organizationId)).Only(l.ctx)
  37. // 获取联系人当前已关联的标签
  38. currentLabelRelationships, err := tx.LabelRelationship.Query().Where(labelrelationship.ContactID(*req.ContactId)).All(l.ctx)
  39. if err != nil {
  40. _ = tx.Rollback()
  41. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  42. }
  43. // 提取当前标签ID
  44. var currentLabelIds []uint64
  45. for _, relationship := range currentLabelRelationships {
  46. currentLabelIds = append(currentLabelIds, relationship.LabelID)
  47. }
  48. // 对比新旧标签ID,找出需要新增和移除的标签
  49. addLabelIds, removeLabelIds := compareLabelIds(req.LabelIds, currentLabelIds)
  50. // 如果 req.UpdateType 为空,或 req.UpdateType 的值为 “all” 时
  51. if req.UpdateType == nil || *req.UpdateType == "all" {
  52. // 删除需要移除的标签关系
  53. for _, id := range removeLabelIds {
  54. _, err := tx.LabelRelationship.
  55. Delete().
  56. Where(
  57. labelrelationship.ContactID(*req.ContactId),
  58. labelrelationship.LabelID(id),
  59. ).
  60. Exec(l.ctx)
  61. if err != nil {
  62. _ = tx.Rollback()
  63. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  64. }
  65. }
  66. }
  67. // 创建需要新增的标签关系
  68. for _, id := range addLabelIds {
  69. _, _ = tx.LabelRelationship.Create().
  70. SetLabelID(id).
  71. SetContactID(*req.ContactId).
  72. SetOrganizationID(organizationId).
  73. Save(l.ctx)
  74. //if err != nil {
  75. // return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  76. //}
  77. }
  78. // 获取所有 status 为 3 且 bot_wxid_list 包含 c.wx_wxid 的 sop_task
  79. sopTasks, err := tx.SopTask.Query().Where(soptask.Status(3), soptask.OrganizationIDEQ(organizationId)).All(l.ctx)
  80. if err != nil {
  81. _ = tx.Rollback()
  82. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  83. }
  84. var filteredSopTasks []*ent.SopTask
  85. for _, task := range sopTasks {
  86. for _, botWxid := range task.BotWxidList {
  87. if botWxid == c.WxWxid {
  88. filteredSopTasks = append(filteredSopTasks, task)
  89. break
  90. }
  91. }
  92. }
  93. // 获取所有 filteredSopTasks 的 sop_stages
  94. stageMap := make(map[uint64]*ent.SopStage)
  95. for _, task := range filteredSopTasks {
  96. stages, err := task.QueryTaskStages().All(l.ctx)
  97. if err != nil {
  98. _ = tx.Rollback()
  99. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  100. }
  101. for _, stage := range stages {
  102. stageMap[stage.ID] = stage
  103. }
  104. //sopStages = append(sopStages, stages...)
  105. }
  106. // 所有操作成功,提交事务
  107. err = tx.Commit()
  108. if err != nil {
  109. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  110. }
  111. err = l.AddLabelRelationships(stageMap, *c, req.LabelIds, organizationId)
  112. if err != nil {
  113. return nil, err
  114. }
  115. return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
  116. }
  117. func (l *UpdateLabelRelationshipsLogic) AddLabelRelationships(sopStages map[uint64]*ent.SopStage, contact ent.Contact, currentLabelIds []uint64, organizationId uint64) (err error) {
  118. // 遍历 sop_stages,找出满足条件的 stage
  119. for key, stage := range sopStages {
  120. if stage != nil && stage.ConditionType == 1 && isLabelIdListMatchFilter(currentLabelIds, stage.ConditionOperator, stage.ConditionList) {
  121. // 开始事务
  122. tx, err := l.svcCtx.DB.Tx(context.Background())
  123. if err != nil {
  124. return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  125. }
  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 i, message := range stage.ActionMessage {
  142. meta := custom_types.Meta{}
  143. if message.Meta != nil {
  144. meta.Filename = message.Meta.Filename
  145. }
  146. _, err = tx.MessageRecords.Create().
  147. SetNotNilBotWxid(&contact.WxWxid).
  148. SetNotNilContactID(&contact.ID).
  149. SetNotNilContactType(&contact.Type).
  150. SetNotNilContactWxid(&contact.Wxid).
  151. SetNotNilContentType(&message.Type).
  152. SetNotNilContent(&message.Content).
  153. SetMeta(meta).
  154. SetNotNilSourceType(&sourceType).
  155. SetNotNilSourceID(&stage.ID).
  156. SetSubSourceID(uint64(i)).
  157. SetOrganizationID(organizationId).
  158. Save(l.ctx)
  159. if err != nil {
  160. _ = tx.Rollback()
  161. return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  162. }
  163. }
  164. }
  165. if stage.ActionForward != nil {
  166. if stage.ActionForward.Wxid != "" {
  167. forwardWxids := splitString(stage.ActionForward.Wxid)
  168. for _, forwardWxid := range forwardWxids {
  169. for i, message := range stage.ActionForward.Action {
  170. meta := custom_types.Meta{}
  171. if message.Meta != nil {
  172. meta.Filename = message.Meta.Filename
  173. }
  174. _, err = tx.MessageRecords.Create().
  175. SetBotWxid(contact.WxWxid).
  176. SetContactID(0).
  177. SetContactType(0).
  178. SetContactWxid(forwardWxid).
  179. SetContentType(message.Type).
  180. SetContent(message.Content).
  181. SetMeta(meta).
  182. SetSourceType(sourceType).
  183. SetSourceID(stage.ID).
  184. SetSubSourceID(contact.ID + uint64(i)).
  185. SetOrganizationID(organizationId).
  186. Save(l.ctx)
  187. if err != nil {
  188. _ = tx.Rollback()
  189. return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  190. }
  191. }
  192. }
  193. }
  194. }
  195. if stage.ActionLabelAdd != nil || stage.ActionLabelDel != nil {
  196. // 获取 addLabelIds 中不在 currentLabelIds 中的标签ID
  197. var newLabelIds []uint64
  198. var remLabelIds []uint64
  199. var finalLabelIds []uint64
  200. // 创建一个映射,用于快速查找 currentLabelIds 中的元素
  201. currentLabelIdSet := make(map[uint64]struct{})
  202. for _, id := range currentLabelIds {
  203. currentLabelIdSet[id] = struct{}{}
  204. }
  205. delLabelIdSet := make(map[uint64]struct{})
  206. for _, id := range stage.ActionLabelDel {
  207. delLabelIdSet[id] = struct{}{}
  208. }
  209. if stage.ActionLabelAdd != nil {
  210. // 遍历 addLabelIds,找出不在 currentLabelIds 中的元素
  211. for _, id := range stage.ActionLabelAdd {
  212. if _, ce := currentLabelIdSet[id]; !ce {
  213. if _, re := delLabelIdSet[id]; !re {
  214. newLabelIds = append(newLabelIds, id)
  215. }
  216. }
  217. }
  218. if len(newLabelIds) > 0 {
  219. // 创建需要新增的标签关系
  220. for _, id := range newLabelIds {
  221. _, err = tx.LabelRelationship.Create().
  222. SetLabelID(id).
  223. SetContactID(contact.ID).
  224. SetOrganizationID(organizationId).
  225. Save(l.ctx)
  226. if err != nil {
  227. _ = tx.Rollback()
  228. return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  229. }
  230. }
  231. }
  232. // 合并 currentLabelIds 和 newLabelIds
  233. currentLabelIds = append(currentLabelIds, newLabelIds...)
  234. }
  235. if stage.ActionLabelDel != nil {
  236. // 遍历 delLabelIds,找出在 currentLabelIds 中的元素
  237. for _, id := range stage.ActionLabelDel {
  238. if _, exists := currentLabelIdSet[id]; exists {
  239. remLabelIds = append(newLabelIds, id)
  240. delete(currentLabelIdSet, id)
  241. }
  242. }
  243. if len(remLabelIds) > 0 {
  244. _, err = tx.LabelRelationship.Delete().Where(labelrelationship.IDIn(remLabelIds...), labelrelationship.ContactIDEQ(contact.ID), labelrelationship.OrganizationIDEQ(organizationId)).Exec(l.ctx)
  245. if err != nil {
  246. //_ = tx.Rollback()
  247. return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  248. }
  249. }
  250. }
  251. // 所有操作成功,提交事务
  252. err = tx.Commit()
  253. if err != nil {
  254. return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  255. }
  256. if len(newLabelIds) == 0 && len(remLabelIds) == 0 {
  257. return nil
  258. }
  259. for id := range currentLabelIdSet {
  260. finalLabelIds = append(finalLabelIds, id)
  261. }
  262. // 递归调用 AddLabelRelationships
  263. sopStages[key] = nil
  264. err = l.AddLabelRelationships(sopStages, contact, finalLabelIds, organizationId)
  265. if err != nil {
  266. return err
  267. }
  268. return nil
  269. } else {
  270. // 所有操作成功,提交事务
  271. err = tx.Commit()
  272. if err != nil {
  273. return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  274. }
  275. }
  276. }
  277. }
  278. // 所有操作成功,提交事务
  279. //err = tx.Commit()
  280. //if err != nil {
  281. // return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  282. //}
  283. return nil
  284. }
  285. // compareLabelIds compares the new label ids with the current ones and returns the ids to be added and removed
  286. func compareLabelIds(newLabelIds []uint64, currentLabelIds []uint64) (addLabelIds []uint64, removeLabelIds []uint64) {
  287. newLabelIdSet := make(map[uint64]struct{}, len(newLabelIds))
  288. for _, id := range newLabelIds {
  289. newLabelIdSet[id] = struct{}{}
  290. }
  291. for _, id := range currentLabelIds {
  292. if _, ok := newLabelIdSet[id]; ok {
  293. delete(newLabelIdSet, id)
  294. } else {
  295. removeLabelIds = append(removeLabelIds, id)
  296. }
  297. }
  298. for id := range newLabelIdSet {
  299. addLabelIds = append(addLabelIds, id)
  300. }
  301. return
  302. }
  303. func isLabelIdListMatchFilter(labelIdList []uint64, conditionOperator int, conditionList []custom_types.Condition) bool {
  304. labelIdSet := make(map[uint64]struct{})
  305. for _, id := range labelIdList {
  306. labelIdSet[id] = struct{}{}
  307. }
  308. for _, condition := range conditionList {
  309. match := false
  310. for _, id := range condition.LabelIdList {
  311. if _, ok := labelIdSet[id]; ok {
  312. match = true
  313. break
  314. }
  315. }
  316. if condition.Equal == 2 {
  317. match = !match
  318. }
  319. if (conditionOperator == 1 && !match) || (conditionOperator == 2 && match) {
  320. return match
  321. }
  322. }
  323. return conditionOperator == 1
  324. }
  325. func splitString(input string) []string {
  326. // Define the regular expression pattern to match Chinese comma, English comma, and Chinese enumeration comma
  327. pattern := `[,,、]`
  328. re := regexp.MustCompile(pattern)
  329. // Split the input string based on the pattern
  330. return re.Split(input, -1)
  331. }