sop_task_copy_logic.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package sop_task
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "wechat-api/ent"
  6. "wechat-api/ent/sopnode"
  7. "wechat-api/ent/soptask"
  8. "wechat-api/internal/svc"
  9. "wechat-api/internal/types"
  10. "wechat-api/internal/utils/dberrorhandler"
  11. "github.com/zeromicro/go-zero/core/logx"
  12. )
  13. type SopTaskCopyLogic struct {
  14. logx.Logger
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. }
  18. func NewSopTaskCopyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SopTaskCopyLogic {
  19. return &SopTaskCopyLogic{
  20. Logger: logx.WithContext(ctx),
  21. ctx: ctx,
  22. svcCtx: svcCtx}
  23. }
  24. func (l *SopTaskCopyLogic) SopTaskCopy(req *types.CopyReq) (resp *types.BaseMsgResp, err error) {
  25. isAdmin := l.ctx.Value("isAdmin").(bool)
  26. organizationId := l.ctx.Value("organizationId").(uint64)
  27. var targetOrganizationId uint64
  28. if isAdmin {
  29. if req.OrganizationId == 0 {
  30. targetOrganizationId = organizationId
  31. } else {
  32. targetOrganizationId = req.OrganizationId
  33. }
  34. } else {
  35. targetOrganizationId = organizationId
  36. }
  37. data, err := l.svcCtx.DB.SopTask.Query().
  38. Where(soptask.ID(req.Id), soptask.OrganizationIDEQ(organizationId)).
  39. WithTaskStages().
  40. Only(l.ctx)
  41. if err != nil {
  42. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  43. }
  44. // 开始事务
  45. tx, err := l.svcCtx.DB.Tx(context.Background())
  46. if err != nil {
  47. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  48. }
  49. newTask, err := tx.SopTask.Create().
  50. SetName(*req.Name).
  51. SetType(data.Type).
  52. SetOrganizationID(targetOrganizationId).
  53. Save(l.ctx)
  54. if err != nil {
  55. _ = tx.Rollback()
  56. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  57. }
  58. for _, s := range data.Edges.TaskStages {
  59. newStage, err := tx.SopStage.Create().
  60. SetTaskID(newTask.ID).
  61. SetName(s.Name).
  62. SetConditionType(s.ConditionType).
  63. SetConditionOperator(s.ConditionOperator).
  64. SetConditionList(s.ConditionList).
  65. SetNotNilActionMessage(s.ActionMessage).
  66. SetNotNilActionLabel(s.ActionLabel).
  67. SetIndexSort(s.IndexSort).
  68. Save(l.ctx)
  69. if err != nil {
  70. _ = tx.Rollback()
  71. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  72. }
  73. err = CreateNodes(tx, s.ID, 0, newStage.ID, 0, l.ctx, l.Logger)
  74. if err != nil {
  75. _ = tx.Rollback()
  76. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  77. }
  78. }
  79. // 所有操作成功,提交事务
  80. err = tx.Commit()
  81. if err != nil {
  82. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  83. }
  84. return &types.BaseMsgResp{Msg: errormsg.CreateSuccess}, nil
  85. }
  86. func CreateNodes(tx *ent.Tx, stageId uint64, parentId uint64, newStageId uint64, newParentId uint64, ctx context.Context, logger logx.Logger) (err error) {
  87. data, err := tx.SopNode.Query().
  88. Where(sopnode.StageID(stageId), sopnode.ParentID(parentId)).All(ctx)
  89. if err != nil {
  90. return nil
  91. }
  92. for _, n := range data {
  93. newNode, err := tx.SopNode.Create().
  94. SetStageID(newStageId).
  95. SetParentID(newParentId).
  96. SetName(n.Name).
  97. SetConditionType(n.ConditionType).
  98. SetConditionList(n.ConditionList).
  99. SetNotNilNoReplyCondition(&n.NoReplyCondition).
  100. SetNotNilActionMessage(n.ActionMessage).
  101. SetNotNilActionLabel(n.ActionLabel).
  102. Save(ctx)
  103. if err != nil {
  104. return err
  105. }
  106. err = CreateNodes(tx, stageId, n.ID, newStageId, newNode.ID, ctx, logger)
  107. if err != nil {
  108. return err
  109. }
  110. }
  111. return nil
  112. }