create_sop_node_logic.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package sop_node
  2. import (
  3. "context"
  4. "fmt"
  5. "wechat-api/ent/custom_types"
  6. "wechat-api/ent/sopstage"
  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 CreateSopNodeLogic struct {
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. logx.Logger
  17. }
  18. func NewCreateSopNodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateSopNodeLogic {
  19. return &CreateSopNodeLogic{
  20. ctx: ctx,
  21. svcCtx: svcCtx,
  22. Logger: logx.WithContext(ctx),
  23. }
  24. }
  25. func (l *CreateSopNodeLogic) CreateSopNode(req *types.SopNodeInfo) (*types.SopNodeCreateResp, error) {
  26. // 根据 id 关联查询处 task 信息,并判断 status 是否为 1
  27. data, err := l.svcCtx.DB.SopStage.Query().
  28. Where(sopstage.ID(*req.StageId)).
  29. WithSopTask().
  30. Only(l.ctx)
  31. if err != nil {
  32. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  33. }
  34. task := data.Edges.SopTask
  35. if task.Status != 1 {
  36. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  37. }
  38. fmt.Printf("req.ActionMessage: %+v\n", req.ActionMessage)
  39. var actionMessage []custom_types.Action
  40. if len(req.ActionMessage) > 0 {
  41. actionMessage = make([]custom_types.Action, len(req.ActionMessage))
  42. for i, condition := range req.ActionMessage {
  43. if condition.Type == 1 {
  44. actionMessage[i] = custom_types.Action{
  45. Type: condition.Type,
  46. Content: condition.Content,
  47. }
  48. } else {
  49. actionMessage[i] = custom_types.Action{
  50. Type: condition.Type,
  51. Content: condition.Content,
  52. Meta: &custom_types.Meta{
  53. Filename: condition.Meta.Filename,
  54. },
  55. }
  56. }
  57. }
  58. }
  59. newNode, err := l.svcCtx.DB.SopNode.Create().
  60. SetNotNilStatus(req.Status).
  61. SetNotNilStageID(req.StageId).
  62. SetNotNilParentID(req.ParentId).
  63. SetNotNilName(req.Name).
  64. SetNotNilConditionType(req.ConditionType).
  65. SetNotNilConditionList(req.ConditionList).
  66. SetNotNilNoReplyCondition(req.NoReplyCondition).
  67. SetNotNilActionMessage(actionMessage).
  68. SetNotNilActionLabel(req.ActionLabel).
  69. Save(l.ctx)
  70. if err != nil {
  71. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  72. }
  73. return &types.SopNodeCreateResp{BaseDataInfo: types.BaseDataInfo{Msg: errormsg.CreateSuccess}, Data: newNode.ID}, nil
  74. }