publish_sop_task_logic.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package sop_task
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  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/predicate"
  12. "wechat-api/ent/soptask"
  13. "wechat-api/internal/utils/dberrorhandler"
  14. "wechat-api/internal/svc"
  15. "wechat-api/internal/types"
  16. "github.com/zeromicro/go-zero/core/logx"
  17. )
  18. type PublishSopTaskLogic struct {
  19. logx.Logger
  20. ctx context.Context
  21. svcCtx *svc.ServiceContext
  22. }
  23. func NewPublishSopTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PublishSopTaskLogic {
  24. return &PublishSopTaskLogic{
  25. Logger: logx.WithContext(ctx),
  26. ctx: ctx,
  27. svcCtx: svcCtx}
  28. }
  29. func (l *PublishSopTaskLogic) PublishSopTask(req *types.IDReq) (resp *types.BaseMsgResp, err error) {
  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. // 根据 id 查询 sop_task
  36. sopTask, err := tx.SopTask.Query().
  37. Where(
  38. soptask.ID(req.Id),
  39. soptask.Status(1),
  40. ).
  41. WithTaskStages().
  42. Only(l.ctx)
  43. if err != nil {
  44. _ = tx.Rollback()
  45. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  46. }
  47. // 判断 sop_task 是否存在
  48. if sopTask != nil {
  49. if sopTask.BotWxidList == nil {
  50. return nil, errors.New(errormsg.ValidationError)
  51. }
  52. // 查询任务的所有 sop_stages
  53. err = tx.SopTask.UpdateOneID(req.Id).
  54. SetStatus(3).
  55. Exec(l.ctx)
  56. if err != nil {
  57. _ = tx.Rollback()
  58. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  59. }
  60. sopStages, err := tx.SopStage.Query().All(l.ctx)
  61. if err != nil {
  62. _ = tx.Rollback()
  63. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  64. }
  65. // 遍历 stage
  66. for _, stage := range sopTask.Edges.TaskStages {
  67. if stage.ConditionType == 1 {
  68. // 构造查询条件
  69. var predicates []predicate.Contact
  70. for _, condition := range stage.ConditionList {
  71. subPredicate := contact.HasContactRelationshipsWith(labelrelationship.LabelIDIn(condition.LabelIdList...))
  72. if condition.Equal == 2 {
  73. subPredicate = contact.Not(subPredicate)
  74. }
  75. predicates = append(predicates, subPredicate)
  76. }
  77. // 查询满足条件的联系人
  78. var contacts []*ent.Contact
  79. var err error
  80. sourceType := 3
  81. if stage.ConditionOperator == 1 {
  82. contacts, err = tx.Contact.Query().Where(contact.And(predicates...)).All(l.ctx)
  83. } else {
  84. contacts, err = tx.Contact.Query().Where(contact.Or(predicates...)).All(l.ctx)
  85. }
  86. if err != nil {
  87. _ = tx.Rollback()
  88. return nil, err
  89. }
  90. // 遍历 contacts
  91. for _, c := range contacts {
  92. // 判断联系人所属微信是否包含在任务当中
  93. if sopTask.BotWxidList == nil || (sopTask.BotWxidList != nil && valueInArray(c.WxWxid, sopTask.BotWxidList)) {
  94. for _, message := range stage.ActionMessage {
  95. _, _ = tx.MessageRecords.Create().
  96. SetNotNilBotWxid(&c.WxWxid).
  97. SetNotNilContactID(&c.ID).
  98. SetNotNilContactType(&c.Type).
  99. SetNotNilContactWxid(&c.Wxid).
  100. SetNotNilContentType(&message.Type).
  101. SetNotNilContent(&message.Content).
  102. SetNotNilSourceType(&sourceType).
  103. SetNotNilSourceID(&stage.ID).
  104. Save(l.ctx)
  105. //if err != nil {
  106. // return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  107. //}
  108. }
  109. // 查询当前联系人的标签关系
  110. currentLabelRelationships, err := tx.LabelRelationship.Query().Where(labelrelationship.ContactID(c.ID)).All(l.ctx)
  111. if err != nil {
  112. _ = tx.Rollback()
  113. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  114. }
  115. // 提取当前标签ID
  116. var currentLabelIds []uint64
  117. for _, relationship := range currentLabelRelationships {
  118. currentLabelIds = append(currentLabelIds, relationship.LabelID)
  119. }
  120. if stage.ActionLabel != nil {
  121. // 递归调用 AddLabelRelationships
  122. err = l.AddLabelRelationships(sopStages, *c, currentLabelIds, stage.ActionLabel)
  123. if err != nil {
  124. _ = tx.Rollback()
  125. return nil, err
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }
  132. // 所有操作成功,提交事务
  133. err = tx.Commit()
  134. if err != nil {
  135. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  136. }
  137. return &types.BaseMsgResp{Msg: errormsg.Success}, nil
  138. } else {
  139. // 所有操作成功,提交事务
  140. err = tx.Commit()
  141. if err != nil {
  142. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  143. }
  144. // 返回错误信息:任务不存在
  145. return nil, errors.New(errormsg.TargetNotFound)
  146. }
  147. }
  148. func (l *PublishSopTaskLogic) AddLabelRelationships(sopStages []*ent.SopStage, contact ent.Contact, currentLabelIds []uint64, addLabelIds []uint64) (err error) {
  149. // 开始事务
  150. tx, err := l.svcCtx.DB.Tx(context.Background())
  151. if err != nil {
  152. return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  153. }
  154. // 获取 addLabelIds 中不在 currentLabelIds 中的标签ID
  155. var newLabelIds []uint64
  156. // 创建一个映射,用于快速查找 currentLabelIds 中的元素
  157. currentLabelIdSet := make(map[uint64]struct{})
  158. for _, id := range currentLabelIds {
  159. currentLabelIdSet[id] = struct{}{}
  160. }
  161. // 遍历 addLabelIds,找出不在 currentLabelIds 中的元素
  162. for _, id := range addLabelIds {
  163. if _, exists := currentLabelIdSet[id]; !exists {
  164. newLabelIds = append(newLabelIds, id)
  165. }
  166. }
  167. if len(newLabelIds) == 0 {
  168. return nil
  169. }
  170. // 创建需要新增的标签关系
  171. for _, id := range newLabelIds {
  172. _, err = tx.LabelRelationship.Create().
  173. SetLabelID(id).
  174. SetContactID(contact.ID).
  175. Save(l.ctx)
  176. if err != nil {
  177. _ = tx.Rollback()
  178. return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  179. }
  180. }
  181. // 合并 currentLabelIds 和 newLabelIds
  182. currentLabelIds = append(currentLabelIds, newLabelIds...)
  183. // 遍历 sop_stages,找出满足条件的 stage
  184. for _, stage := range sopStages {
  185. if stage.ConditionType == 1 && isLabelIdListMatchFilter(currentLabelIds, stage.ConditionOperator, stage.ConditionList) {
  186. // 判断是否有 contact_wxid、source_type、source_id、sub_source_id 相同的记录
  187. _, err := tx.MessageRecords.Query().
  188. Where(
  189. messagerecords.ContactWxid(contact.Wxid),
  190. messagerecords.SourceType(3),
  191. messagerecords.SourceID(stage.ID),
  192. messagerecords.SubSourceID(0),
  193. ).
  194. Only(l.ctx)
  195. if err != nil {
  196. continue
  197. }
  198. // 判断ActionMessage是否为空
  199. sourceType := 3
  200. if stage.ActionMessage != nil {
  201. for _, message := range stage.ActionMessage {
  202. _, _ = tx.MessageRecords.Create().
  203. SetNotNilBotWxid(&contact.WxWxid).
  204. SetNotNilContactID(&contact.ID).
  205. SetNotNilContactType(&contact.Type).
  206. SetNotNilContactWxid(&contact.Wxid).
  207. SetNotNilContentType(&message.Type).
  208. SetNotNilContent(&message.Content).
  209. SetNotNilSourceType(&sourceType).
  210. SetNotNilSourceID(&stage.ID).
  211. Save(l.ctx)
  212. //if err != nil {
  213. // return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  214. //}
  215. }
  216. }
  217. if stage.ActionLabel != nil {
  218. // 递归调用 AddLabelRelationships
  219. err = l.AddLabelRelationships(sopStages, contact, currentLabelIds, stage.ActionLabel)
  220. if err != nil {
  221. _ = tx.Rollback()
  222. return err
  223. }
  224. }
  225. }
  226. }
  227. // 所有操作成功,提交事务
  228. err = tx.Commit()
  229. if err != nil {
  230. return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  231. }
  232. return nil
  233. }
  234. func valueInArray(val string, array []string) bool {
  235. for _, item := range array {
  236. if item == val {
  237. return true
  238. }
  239. }
  240. return false
  241. }
  242. func isLabelIdListMatchFilter(labelIdList []uint64, conditionOperator int, conditionList []custom_types.Condition) bool {
  243. labelIdSet := make(map[uint64]struct{})
  244. for _, id := range labelIdList {
  245. labelIdSet[id] = struct{}{}
  246. }
  247. for _, condition := range conditionList {
  248. match := false
  249. for _, id := range condition.LabelIdList {
  250. if _, ok := labelIdSet[id]; ok {
  251. match = true
  252. break
  253. }
  254. }
  255. if condition.Equal == 2 {
  256. match = !match
  257. }
  258. if (conditionOperator == 1 && !match) || (conditionOperator == 2 && match) {
  259. return match
  260. }
  261. }
  262. return conditionOperator == 1
  263. }