send_wx_on_timeout.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. package crontask
  2. import (
  3. "context"
  4. "regexp"
  5. "time"
  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/sopnode"
  12. "wechat-api/ent/sopstage"
  13. "wechat-api/ent/soptask"
  14. "wechat-api/internal/utils/dberrorhandler"
  15. )
  16. func (l *CronTask) sendWxOnTimeout() {
  17. ctx := context.Background()
  18. startTime := time.Now()
  19. sopTask, _ := l.svcCtx.DB.SopTask.Query().
  20. Where(
  21. soptask.StatusEQ(3),
  22. soptask.DeletedAtIsNil(),
  23. ).
  24. All(l.ctx)
  25. sopTasks := make([]uint64, 0)
  26. for _, st := range sopTask {
  27. sopTasks = append(sopTasks, st.ID)
  28. }
  29. sopStages, _ := l.svcCtx.DB.SopStage.Query().Where(sopstage.TaskIDIn(sopTasks...), sopstage.DeletedAtIsNil()).All(l.ctx)
  30. stageMap := make(map[uint64]*ent.SopStage)
  31. for _, stage := range sopStages {
  32. stageMap[stage.ID] = stage
  33. }
  34. // 查询所有 no_reply_condition 值非 0 的 sop_node 记录
  35. nodes, err := l.svcCtx.DB.SopNode.Query().
  36. Where(sopnode.NoReplyConditionNEQ(0)).
  37. Where(sopnode.HasSopStageWith(
  38. sopstage.StatusEQ(1),
  39. sopstage.DeletedAtIsNil(),
  40. sopstage.HasSopTaskWith(
  41. soptask.StatusEQ(3),
  42. soptask.DeletedAtIsNil(),
  43. ),
  44. )).
  45. All(ctx)
  46. if err != nil {
  47. l.Logger.Errorf("get node list failed %v", err)
  48. return
  49. }
  50. // 遍历 nodes,将其各记录 parent_id 的值存入一个新的数组 parent_nodes 中
  51. //parentNodes := make([]uint64, 0)
  52. //stages := make([]uint64, 0)
  53. messages := make([]*ent.MessageRecords, 0)
  54. for _, node := range nodes {
  55. var coef uint64 = 1
  56. switch node.NoReplyUnit {
  57. case "W":
  58. coef = 60 * 24 * 7
  59. case "D":
  60. coef = 60 * 24
  61. case "h":
  62. coef = 60
  63. }
  64. // 查询 node 对应的 stage 记录
  65. lowerBound := startTime.Add(-time.Minute * time.Duration(node.NoReplyCondition*coef+2))
  66. upperBound := startTime.Add(-time.Minute * time.Duration(node.NoReplyCondition*coef))
  67. if node.ParentID == 0 {
  68. messages, _ = l.svcCtx.DB.MessageRecords.Query().
  69. Where(messagerecords.StatusEQ(3)).
  70. Where(messagerecords.SourceTypeEQ(3)).
  71. Where(messagerecords.SourceIDEQ(node.StageID)).
  72. Where(messagerecords.SubSourceIDEQ(0)).
  73. Where(messagerecords.SendTimeGTE(lowerBound)).
  74. Where(messagerecords.SendTimeLTE(upperBound)).
  75. All(ctx)
  76. } else {
  77. messages, _ = l.svcCtx.DB.MessageRecords.Query().
  78. Where(messagerecords.StatusEQ(3)).
  79. Where(messagerecords.SourceTypeEQ(4)).
  80. Where(messagerecords.SourceIDIn(node.ParentID)).
  81. Where(messagerecords.SubSourceIDEQ(0)).
  82. Where(messagerecords.SendTimeGTE(lowerBound)).
  83. Where(messagerecords.SendTimeLTE(upperBound)).
  84. All(ctx)
  85. }
  86. for _, s := range messages {
  87. // 判断 s.Id 是否是 s.ContactID 的最新记录
  88. latest, _ := l.svcCtx.DB.MessageRecords.Query().
  89. Where(messagerecords.ContactIDEQ(s.ContactID)).
  90. Where(messagerecords.StatusEQ(3)).
  91. Order(ent.Asc(messagerecords.FieldID)).
  92. First(ctx)
  93. if latest.ID == s.ID {
  94. // 创建 MessageRecords 记录
  95. if node.ActionMessage != nil {
  96. for i, c := range node.ActionMessage {
  97. meta := custom_types.Meta{}
  98. if c.Meta != nil {
  99. meta.Filename = c.Meta.Filename
  100. }
  101. _, _ = l.svcCtx.DB.MessageRecords.Create().
  102. SetStatus(1).
  103. SetBotWxid(s.BotWxid).
  104. SetContactID(s.ContactID).
  105. SetContactType(s.ContactType).
  106. SetContactWxid(s.ContactWxid).
  107. SetContentType(c.Type).
  108. SetContent(c.Content).
  109. SetMeta(meta).
  110. SetSourceType(4).
  111. SetSourceID(node.ID).
  112. SetSubSourceID(uint64(i)).
  113. SetOrganizationID(s.OrganizationID).
  114. Save(ctx)
  115. }
  116. } else {
  117. meta := custom_types.Meta{}
  118. _, _ = l.svcCtx.DB.MessageRecords.Create().
  119. SetStatus(1).
  120. SetBotWxid(s.BotWxid).
  121. SetContactID(s.ContactID).
  122. SetContactType(s.ContactType).
  123. SetContactWxid(s.ContactWxid).
  124. SetContentType(1).
  125. SetMeta(meta).
  126. SetSourceType(4).
  127. SetSourceID(node.ID).
  128. SetSubSourceID(0).
  129. SetOrganizationID(s.OrganizationID).
  130. Save(ctx)
  131. }
  132. if node.ActionForward != nil {
  133. if node.ActionForward.Wxid != "" {
  134. forwardWxids := splitString(node.ActionForward.Wxid)
  135. for _, forwardWxid := range forwardWxids {
  136. for i, message := range node.ActionForward.Action {
  137. meta := custom_types.Meta{}
  138. if message.Meta != nil {
  139. meta.Filename = message.Meta.Filename
  140. }
  141. contactInfo, _ := l.svcCtx.DB.Contact.Query().Where(
  142. contact.WxWxidEQ(s.BotWxid),
  143. contact.WxidEQ(s.ContactWxid),
  144. contact.CtypeIn(1, 3),
  145. ).First(l.ctx)
  146. content := varReplace(message.Content, contactInfo)
  147. _, _ = l.svcCtx.DB.MessageRecords.Create().
  148. SetBotWxid(s.BotWxid).
  149. SetContactID(0).
  150. SetContactType(0).
  151. SetContactWxid(forwardWxid).
  152. SetContentType(message.Type).
  153. SetContent(content).
  154. SetMeta(meta).
  155. SetSourceType(4).
  156. SetSourceID(node.ID).
  157. SetSubSourceID(s.ContactID + uint64(i)).
  158. SetOrganizationID(s.OrganizationID).
  159. Save(l.ctx)
  160. }
  161. }
  162. }
  163. }
  164. // 查询当前联系人的标签关系
  165. currentLabelRelationships, _ := l.svcCtx.DB.LabelRelationship.Query().Where(labelrelationship.ContactID(s.ContactID)).All(l.ctx)
  166. if currentLabelRelationships == nil {
  167. continue
  168. }
  169. // 提取当前标签ID
  170. var currentLabelIds []uint64
  171. for _, relationship := range currentLabelRelationships {
  172. currentLabelIds = append(currentLabelIds, relationship.LabelID)
  173. }
  174. contact := &ent.Contact{
  175. ID: s.ContactID,
  176. Type: s.ContentType,
  177. WxWxid: s.BotWxid,
  178. Wxid: s.ContactWxid,
  179. }
  180. if node.ActionLabelAdd != nil || node.ActionLabelDel != nil {
  181. visitedStages := make(map[uint64]bool)
  182. _ = l.AddLabelRelationships(stageMap, *contact, currentLabelIds, node.ActionLabelAdd, node.ActionLabelDel, s.OrganizationID, visitedStages)
  183. }
  184. }
  185. }
  186. }
  187. finishTime := time.Now()
  188. l.Logger.Infof("This process cost %v", finishTime.Sub(startTime).String())
  189. return
  190. }
  191. func splitString(input string) []string {
  192. // Define the regular expression pattern to match Chinese comma, English comma, and Chinese enumeration comma
  193. pattern := `[,,、]`
  194. re := regexp.MustCompile(pattern)
  195. // Split the input string based on the pattern
  196. return re.Split(input, -1)
  197. }
  198. func (l *CronTask) AddLabelRelationships(sopStages map[uint64]*ent.SopStage, contact ent.Contact, currentLabelIds []uint64, addLabelIds []uint64, delLabelIds []uint64, organizationId uint64, visitedStages map[uint64]bool) (err error) {
  199. //// 开始事务
  200. //tx, err := l.svcCtx.DB.Tx(context.Background())
  201. //if err != nil {
  202. // return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  203. //}
  204. // 获取 addLabelIds 中不在 currentLabelIds 中的标签ID
  205. var newLabelIds []uint64
  206. var remLabelIds []uint64
  207. var finalLabelIds []uint64
  208. // 创建一个映射,用于快速查找 currentLabelIds 中的元素
  209. currentLabelIdSet := make(map[uint64]struct{})
  210. for _, id := range currentLabelIds {
  211. currentLabelIdSet[id] = struct{}{}
  212. }
  213. delLabelIdSet := make(map[uint64]struct{})
  214. for _, id := range delLabelIds {
  215. delLabelIdSet[id] = struct{}{}
  216. }
  217. if addLabelIds != nil {
  218. // 遍历 addLabelIds,找出不在 currentLabelIds 中的元素
  219. for _, id := range addLabelIds {
  220. if _, ce := currentLabelIdSet[id]; !ce {
  221. if _, re := delLabelIdSet[id]; !re {
  222. newLabelIds = append(newLabelIds, id)
  223. }
  224. }
  225. }
  226. if len(newLabelIds) > 0 {
  227. // 创建需要新增的标签关系
  228. for _, id := range newLabelIds {
  229. currentLabelIdSet[id] = struct{}{}
  230. _, err = l.svcCtx.DB.LabelRelationship.Create().
  231. SetLabelID(id).
  232. SetContactID(contact.ID).
  233. SetOrganizationID(organizationId).
  234. Save(l.ctx)
  235. if err != nil {
  236. //_ = tx.Rollback()
  237. return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  238. }
  239. }
  240. // 合并 currentLabelIds 和 newLabelIds
  241. currentLabelIds = append(currentLabelIds, newLabelIds...)
  242. }
  243. }
  244. if delLabelIds != nil {
  245. // 遍历 delLabelIds,找出在 currentLabelIds 中的元素
  246. for _, id := range delLabelIds {
  247. if _, exists := currentLabelIdSet[id]; exists {
  248. remLabelIds = append(remLabelIds, id)
  249. delete(currentLabelIdSet, id)
  250. }
  251. }
  252. if len(remLabelIds) > 0 {
  253. _, err = l.svcCtx.DB.LabelRelationship.Delete().Where(labelrelationship.LabelIDIn(remLabelIds...), labelrelationship.ContactIDEQ(contact.ID), labelrelationship.OrganizationIDEQ(organizationId)).Exec(l.ctx)
  254. if err != nil {
  255. //_ = tx.Rollback()
  256. return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  257. }
  258. }
  259. }
  260. if len(newLabelIds) == 0 && len(remLabelIds) == 0 {
  261. return nil
  262. }
  263. for id := range currentLabelIdSet {
  264. finalLabelIds = append(finalLabelIds, id)
  265. }
  266. // 遍历 sop_stages,找出满足条件的 stage
  267. for key, stage := range sopStages {
  268. if stage == nil || visitedStages[key] {
  269. continue
  270. }
  271. if stage.ConditionType == 1 && isLabelIdListMatchFilter(finalLabelIds, stage.ConditionOperator, stage.ConditionList) {
  272. // 判断是否有 contact_wxid、source_type、source_id、sub_source_id 相同的记录
  273. _, err := l.svcCtx.DB.MessageRecords.Query().
  274. Where(
  275. messagerecords.ContactWxid(contact.Wxid),
  276. messagerecords.SourceType(3),
  277. messagerecords.SourceID(stage.ID),
  278. messagerecords.SubSourceID(0),
  279. ).
  280. Only(l.ctx)
  281. if !ent.IsNotFound(err) {
  282. continue
  283. }
  284. // 判断ActionMessage是否为空
  285. sourceType := 3
  286. if stage.ActionMessage != nil {
  287. for i, message := range stage.ActionMessage {
  288. meta := custom_types.Meta{}
  289. if message.Meta != nil {
  290. meta.Filename = message.Meta.Filename
  291. }
  292. _, _ = l.svcCtx.DB.MessageRecords.Create().
  293. SetNotNilBotWxid(&contact.WxWxid).
  294. SetNotNilContactID(&contact.ID).
  295. SetNotNilContactType(&contact.Type).
  296. SetNotNilContactWxid(&contact.Wxid).
  297. SetNotNilContentType(&message.Type).
  298. SetNotNilContent(&message.Content).
  299. SetMeta(meta).
  300. SetNotNilSourceType(&sourceType).
  301. SetNotNilSourceID(&stage.ID).
  302. SetSubSourceID(uint64(i)).
  303. SetOrganizationID(organizationId).
  304. Save(l.ctx)
  305. //if err != nil {
  306. // return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  307. //}
  308. }
  309. }
  310. if stage.ActionForward != nil {
  311. if stage.ActionForward.Wxid != "" {
  312. forwardWxids := splitString(stage.ActionForward.Wxid)
  313. for _, forwardWxid := range forwardWxids {
  314. for i, message := range stage.ActionForward.Action {
  315. meta := custom_types.Meta{}
  316. if message.Meta != nil {
  317. meta.Filename = message.Meta.Filename
  318. }
  319. _, err = l.svcCtx.DB.MessageRecords.Create().
  320. SetBotWxid(contact.WxWxid).
  321. SetContactID(0).
  322. SetContactType(0).
  323. SetContactWxid(forwardWxid).
  324. SetContentType(message.Type).
  325. SetContent(message.Content).
  326. SetMeta(meta).
  327. SetSourceType(sourceType).
  328. SetSourceID(stage.ID).
  329. SetSubSourceID(contact.ID + uint64(i)).
  330. SetOrganizationID(organizationId).
  331. Save(l.ctx)
  332. }
  333. }
  334. }
  335. }
  336. if stage.ActionLabelAdd != nil || stage.ActionLabelDel != nil {
  337. // 递归调用 AddLabelRelationships
  338. visitedStages[key] = true
  339. err = l.AddLabelRelationships(sopStages, contact, finalLabelIds, stage.ActionLabelAdd, stage.ActionLabelDel, organizationId, visitedStages)
  340. if err != nil {
  341. //_ = tx.Rollback()
  342. return err
  343. }
  344. }
  345. }
  346. }
  347. // 所有操作成功,提交事务
  348. //err = tx.Commit()
  349. //if err != nil {
  350. // return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  351. //}
  352. return nil
  353. }
  354. func isLabelIdListMatchFilter(labelIdList []uint64, conditionOperator int, conditionList []custom_types.Condition) bool {
  355. labelIdSet := make(map[uint64]struct{})
  356. for _, id := range labelIdList {
  357. labelIdSet[id] = struct{}{}
  358. }
  359. for _, condition := range conditionList {
  360. match := false
  361. for _, id := range condition.LabelIdList {
  362. if _, ok := labelIdSet[id]; ok {
  363. match = true
  364. break
  365. }
  366. }
  367. if condition.Equal == 2 {
  368. match = !match
  369. }
  370. if conditionOperator == 1 && !match {
  371. return false
  372. } else if conditionOperator == 2 && match {
  373. return true
  374. }
  375. }
  376. return conditionOperator == 1
  377. }