update_sop_node_logic.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package sop_node
  2. import (
  3. "context"
  4. "wechat-api/ent"
  5. "wechat-api/ent/custom_types"
  6. "wechat-api/ent/sopnode"
  7. "wechat-api/internal/svc"
  8. "wechat-api/internal/types"
  9. "wechat-api/internal/utils/dberrorhandler"
  10. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  11. "github.com/zeromicro/go-zero/core/logx"
  12. )
  13. type UpdateSopNodeLogic struct {
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. logx.Logger
  17. }
  18. func NewUpdateSopNodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateSopNodeLogic {
  19. return &UpdateSopNodeLogic{
  20. ctx: ctx,
  21. svcCtx: svcCtx,
  22. Logger: logx.WithContext(ctx),
  23. }
  24. }
  25. func (l *UpdateSopNodeLogic) UpdateSopNode(req *types.SopNodeInfo) (*types.BaseMsgResp, error) {
  26. organizationId := l.ctx.Value("organizationId").(uint64)
  27. // 根据 id 关联查询处 task 信息,并判断 status 是否为 1
  28. data, err := l.svcCtx.DB.SopNode.Query().
  29. Where(sopnode.ID(*req.Id)).
  30. WithSopStage(func(query *ent.SopStageQuery) {
  31. query.WithSopTask()
  32. }).
  33. Only(l.ctx)
  34. if err != nil {
  35. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  36. }
  37. task := data.Edges.SopStage.Edges.SopTask
  38. if task.Status != 1 || task.OrganizationID != organizationId {
  39. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  40. }
  41. var actionMessage []custom_types.Action
  42. if len(req.ActionMessage) > 0 {
  43. actionMessage = make([]custom_types.Action, len(req.ActionMessage))
  44. for i, condition := range req.ActionMessage {
  45. if condition.Type == 1 {
  46. actionMessage[i] = custom_types.Action{
  47. Type: condition.Type,
  48. Content: condition.Content,
  49. }
  50. } else {
  51. actionMessage[i] = custom_types.Action{
  52. Type: condition.Type,
  53. Content: condition.Content,
  54. Meta: &custom_types.Meta{
  55. Filename: condition.Meta.Filename,
  56. },
  57. }
  58. }
  59. }
  60. }
  61. var forward *custom_types.ActionForward
  62. if req.ActionForward != nil {
  63. forward = &custom_types.ActionForward{}
  64. forward.Wxid = req.ActionForward.Wxid
  65. var forwardAction []custom_types.Action
  66. if len(req.ActionForward.Action) > 0 {
  67. forwardAction = make([]custom_types.Action, len(req.ActionForward.Action))
  68. for i, condition := range req.ActionForward.Action {
  69. if condition.Type == 1 {
  70. forwardAction[i] = custom_types.Action{
  71. Type: condition.Type,
  72. Content: condition.Content,
  73. }
  74. } else {
  75. forwardAction[i] = custom_types.Action{
  76. Type: condition.Type,
  77. Content: condition.Content,
  78. Meta: &custom_types.Meta{
  79. Filename: condition.Meta.Filename,
  80. },
  81. }
  82. }
  83. }
  84. }
  85. forward.Action = forwardAction
  86. } else {
  87. forward = nil
  88. }
  89. var noReplyCondition uint64
  90. if req.NoReplyCondition == nil {
  91. noReplyCondition = 0
  92. } else {
  93. noReplyCondition = *req.NoReplyCondition
  94. }
  95. createStmt := l.svcCtx.DB.SopNode.UpdateOneID(*req.Id).
  96. SetStageID(*req.StageId).
  97. SetName(*req.Name).
  98. SetConditionType(*req.ConditionType).
  99. SetConditionList(req.ConditionList).
  100. SetNoReplyCondition(noReplyCondition).
  101. SetNoReplyUnit(*req.NoReplyUnit).
  102. SetNotNilActionMessage(actionMessage).
  103. SetNotNilActionLabelAdd(req.ActionLabelAdd).
  104. SetNotNilActionLabelDel(req.ActionLabelDel)
  105. if forward != nil {
  106. createStmt.SetActionForward(forward)
  107. } else {
  108. createStmt.ClearActionForward()
  109. }
  110. err = createStmt.Exec(l.ctx)
  111. if err != nil {
  112. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  113. }
  114. return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
  115. }