publish_sop_task_logic.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 := l.svcCtx.DB.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 = l.svcCtx.DB.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 := l.svcCtx.DB.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 = l.svcCtx.DB.Contact.Query().Where(contact.And(predicates...)).All(l.ctx)
  83. } else {
  84. contacts, err = l.svcCtx.DB.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 i, message := range stage.ActionMessage {
  95. _, _ = l.svcCtx.DB.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. SetSubSourceID(uint64(i)).
  105. Save(l.ctx)
  106. //if err != nil {
  107. // return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  108. //}
  109. }
  110. // 查询当前联系人的标签关系
  111. currentLabelRelationships, err := l.svcCtx.DB.LabelRelationship.Query().Where(labelrelationship.ContactID(c.ID)).All(l.ctx)
  112. if err != nil {
  113. //_ = tx.Rollback()
  114. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  115. }
  116. // 提取当前标签ID
  117. var currentLabelIds []uint64
  118. for _, relationship := range currentLabelRelationships {
  119. currentLabelIds = append(currentLabelIds, relationship.LabelID)
  120. }
  121. if stage.ActionLabel != nil {
  122. // 递归调用 AddLabelRelationships
  123. err = l.AddLabelRelationships(sopStages, *c, currentLabelIds, stage.ActionLabel)
  124. if err != nil {
  125. //_ = tx.Rollback()
  126. return nil, err
  127. }
  128. }
  129. }
  130. }
  131. }
  132. }
  133. // 所有操作成功,提交事务
  134. //err = tx.Commit()
  135. //if err != nil {
  136. // return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  137. //}
  138. return &types.BaseMsgResp{Msg: errormsg.Success}, nil
  139. } else {
  140. // 所有操作成功,提交事务
  141. //err = tx.Commit()
  142. //if err != nil {
  143. // return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  144. //}
  145. //// 返回错误信息:任务不存在
  146. return nil, errors.New(errormsg.TargetNotFound)
  147. }
  148. }
  149. func (l *PublishSopTaskLogic) AddLabelRelationships(sopStages []*ent.SopStage, contact ent.Contact, currentLabelIds []uint64, addLabelIds []uint64) (err error) {
  150. //// 开始事务
  151. //tx, err := l.svcCtx.DB.Tx(context.Background())
  152. //if err != nil {
  153. // return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  154. //}
  155. // 获取 addLabelIds 中不在 currentLabelIds 中的标签ID
  156. var newLabelIds []uint64
  157. // 创建一个映射,用于快速查找 currentLabelIds 中的元素
  158. currentLabelIdSet := make(map[uint64]struct{})
  159. for _, id := range currentLabelIds {
  160. currentLabelIdSet[id] = struct{}{}
  161. }
  162. // 遍历 addLabelIds,找出不在 currentLabelIds 中的元素
  163. for _, id := range addLabelIds {
  164. if _, exists := currentLabelIdSet[id]; !exists {
  165. newLabelIds = append(newLabelIds, id)
  166. }
  167. }
  168. if len(newLabelIds) == 0 {
  169. return nil
  170. }
  171. // 创建需要新增的标签关系
  172. for _, id := range newLabelIds {
  173. _, err = l.svcCtx.DB.LabelRelationship.Create().
  174. SetLabelID(id).
  175. SetContactID(contact.ID).
  176. Save(l.ctx)
  177. if err != nil {
  178. //_ = tx.Rollback()
  179. return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  180. }
  181. }
  182. // 合并 currentLabelIds 和 newLabelIds
  183. currentLabelIds = append(currentLabelIds, newLabelIds...)
  184. // 遍历 sop_stages,找出满足条件的 stage
  185. for _, stage := range sopStages {
  186. if stage.ConditionType == 1 && isLabelIdListMatchFilter(currentLabelIds, stage.ConditionOperator, stage.ConditionList) {
  187. // 判断是否有 contact_wxid、source_type、source_id、sub_source_id 相同的记录
  188. _, err := l.svcCtx.DB.MessageRecords.Query().
  189. Where(
  190. messagerecords.ContactWxid(contact.Wxid),
  191. messagerecords.SourceType(3),
  192. messagerecords.SourceID(stage.ID),
  193. messagerecords.SubSourceID(0),
  194. ).
  195. Only(l.ctx)
  196. if err != nil {
  197. continue
  198. }
  199. // 判断ActionMessage是否为空
  200. sourceType := 3
  201. if stage.ActionMessage != nil {
  202. for i, message := range stage.ActionMessage {
  203. _, _ = l.svcCtx.DB.MessageRecords.Create().
  204. SetNotNilBotWxid(&contact.WxWxid).
  205. SetNotNilContactID(&contact.ID).
  206. SetNotNilContactType(&contact.Type).
  207. SetNotNilContactWxid(&contact.Wxid).
  208. SetNotNilContentType(&message.Type).
  209. SetNotNilContent(&message.Content).
  210. SetNotNilSourceType(&sourceType).
  211. SetNotNilSourceID(&stage.ID).
  212. SetSubSourceID(uint64(i)).
  213. Save(l.ctx)
  214. //if err != nil {
  215. // return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  216. //}
  217. }
  218. }
  219. if stage.ActionLabel != nil {
  220. // 递归调用 AddLabelRelationships
  221. err = l.AddLabelRelationships(sopStages, contact, currentLabelIds, stage.ActionLabel)
  222. if err != nil {
  223. //_ = tx.Rollback()
  224. return err
  225. }
  226. }
  227. }
  228. }
  229. // 所有操作成功,提交事务
  230. //err = tx.Commit()
  231. //if err != nil {
  232. // return dberrorhandler.DefaultEntError(l.Logger, err, nil)
  233. //}
  234. return nil
  235. }
  236. func valueInArray(val string, array []string) bool {
  237. for _, item := range array {
  238. if item == val {
  239. return true
  240. }
  241. }
  242. return false
  243. }
  244. func isLabelIdListMatchFilter(labelIdList []uint64, conditionOperator int, conditionList []custom_types.Condition) bool {
  245. labelIdSet := make(map[uint64]struct{})
  246. for _, id := range labelIdList {
  247. labelIdSet[id] = struct{}{}
  248. }
  249. for _, condition := range conditionList {
  250. match := false
  251. for _, id := range condition.LabelIdList {
  252. if _, ok := labelIdSet[id]; ok {
  253. match = true
  254. break
  255. }
  256. }
  257. if condition.Equal == 2 {
  258. match = !match
  259. }
  260. if (conditionOperator == 1 && !match) || (conditionOperator == 2 && match) {
  261. return match
  262. }
  263. }
  264. return conditionOperator == 1
  265. }