get_sop_task_outline_logic.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package sop_task
  2. import (
  3. "context"
  4. "strconv"
  5. "wechat-api/ent"
  6. "wechat-api/ent/soptask"
  7. "wechat-api/internal/utils/dberrorhandler"
  8. "wechat-api/internal/svc"
  9. "wechat-api/internal/types"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type GetSopTaskOutlineLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewGetSopTaskOutlineLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSopTaskOutlineLogic {
  18. return &GetSopTaskOutlineLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx}
  22. }
  23. func (l *GetSopTaskOutlineLogic) GetSopTaskOutline(req *types.IDReq) (resp *types.SopTaskOutlineResp, err error) {
  24. organizationId := l.ctx.Value("organizationId").(uint64)
  25. data, err := l.svcCtx.DB.SopTask.Query().
  26. Where(soptask.ID(req.Id), soptask.OrganizationIDEQ(organizationId)).
  27. WithTaskStages(func(query *ent.SopStageQuery) {
  28. query.WithStageNodes()
  29. }).
  30. Only(l.ctx)
  31. if err != nil {
  32. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  33. }
  34. var sopTaskOutlineInfo []types.SopTaskOutlineInfo
  35. sopTaskOutlineInfo = []types.SopTaskOutlineInfo{}
  36. for _, stage := range data.Edges.TaskStages {
  37. nodes := stage.Edges.StageNodes
  38. nodesSet := make(map[uint64][]*ent.SopNode)
  39. for _, node := range nodes {
  40. nodesSet[node.ParentID] = append(nodesSet[node.ParentID], node)
  41. }
  42. key := strconv.Itoa(int(stage.ID))
  43. children := getChildren(nodesSet, 0)
  44. outlineInfo := types.SopTaskOutlineInfo{
  45. Title: stage.Name,
  46. Key: key,
  47. Children: children,
  48. }
  49. sopTaskOutlineInfo = append(sopTaskOutlineInfo, outlineInfo)
  50. }
  51. resp = &types.SopTaskOutlineResp{}
  52. resp.Msg = "success"
  53. resp.Data = sopTaskOutlineInfo
  54. return resp, nil
  55. }
  56. func getChildren(nodesSet map[uint64][]*ent.SopNode, parentID uint64) (nodeChildren []types.SopTaskOutlineInfo) {
  57. if nodesSet[parentID] == nil || len(nodesSet[parentID]) == 0 {
  58. return nil
  59. }
  60. nodeChildren = []types.SopTaskOutlineInfo{}
  61. for _, node := range nodesSet[parentID] {
  62. key := strconv.Itoa(int(node.ID))
  63. nodeChildren = append(nodeChildren, types.SopTaskOutlineInfo{
  64. Title: node.Name,
  65. Key: key,
  66. Children: getChildren(nodesSet, node.ID),
  67. })
  68. }
  69. return nodeChildren
  70. }