plan.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. "box-cost/log"
  6. "errors"
  7. "fmt"
  8. "time"
  9. "github.com/gin-gonic/gin"
  10. "go.mongodb.org/mongo-driver/bson"
  11. "go.mongodb.org/mongo-driver/bson/primitive"
  12. )
  13. // 生产计划管理
  14. func ProductPlan(r *GinRouter) {
  15. // 创建生产计划
  16. r.POST("/plan/create", CreateProductPlan)
  17. // 获取生产计划详情
  18. r.GET("/plan/detail/:id", GetProductPlan)
  19. // 获取生产计划列表
  20. r.GET("/plan/list", GetProductPlans)
  21. // 更新生产计划
  22. r.POST("/plan/update", UpdateProductPlan)
  23. // 删除生产计划
  24. r.POST("/plan/delete/:id", DelProductPlan)
  25. }
  26. // 创建生产计划
  27. func CreateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  28. var plan model.ProductPlan
  29. err := c.ShouldBindJSON(&plan)
  30. if err != nil {
  31. fmt.Println(err)
  32. return nil, errors.New("参数错误!")
  33. }
  34. ctx := apictx.CreateRepoCtx()
  35. if plan.Name == "" {
  36. return nil, errors.New("生产计划名为空")
  37. }
  38. if plan.Total == 0 {
  39. return nil, errors.New("生产计划数应不为0")
  40. }
  41. plan.Status = "process" // 进行中
  42. plan.CreateTime = time.Now()
  43. plan.UpdateTime = time.Now()
  44. result, err := repo.RepoAddDoc(ctx, repo.CollectionProductPlan, &plan)
  45. return result, err
  46. }
  47. // 获取生产计划信息
  48. func GetProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  49. planId := c.Param("id")
  50. id, err := primitive.ObjectIDFromHex(planId)
  51. if err != nil {
  52. return nil, errors.New("非法id")
  53. }
  54. var plan model.ProductPlan
  55. option := &repo.DocSearchOptions{
  56. CollectName: repo.CollectionProductPlan,
  57. Query: repo.Map{"_id": id},
  58. }
  59. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &plan)
  60. if !found || err != nil {
  61. log.Info(err)
  62. return nil, errors.New("数据未找到")
  63. }
  64. billStates := map[string]string{}
  65. if plan.Pack != nil && plan.Pack.Components != nil {
  66. for _, comp := range plan.Pack.Components {
  67. if comp.Mats != nil {
  68. for _, mat := range comp.Mats {
  69. if len(mat.BillId) > 0 {
  70. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionBillPurchase, Query: repo.Map{"_id": mat.BillId}, Project: []string{"status"}})
  71. if ok {
  72. billStates[mat.BillId] = state["status"].(string)
  73. }
  74. }
  75. if mat.Crafts != nil {
  76. for _, craft := range mat.Crafts {
  77. if len(craft.BillId) > 0 {
  78. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionBillProduce, Query: repo.Map{"_id": craft.BillId}, Project: []string{"status"}})
  79. if ok {
  80. billStates[craft.BillId] = state["status"].(string)
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }
  89. return map[string]interface{}{
  90. "plan": plan,
  91. "billStates": billStates,
  92. }, nil
  93. }
  94. // 获取生产计划列表
  95. func GetProductPlans(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  96. page, size, query := UtilQueryPageSize(c)
  97. option := &repo.PageSearchOptions{
  98. CollectName: repo.CollectionProductPlan,
  99. Query: query,
  100. Page: page,
  101. Size: size,
  102. Sort: bson.M{"createTime": -1},
  103. Project: []string{"_id", "thumbnail", "name", "updateTime", "createUser", "total", "totalPrice", "status"},
  104. }
  105. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  106. }
  107. // 更新生产计划
  108. func UpdateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  109. var plan model.ProductPlan
  110. err := c.ShouldBindJSON(&plan)
  111. if err != nil {
  112. return nil, errors.New("参数错误")
  113. }
  114. if plan.Id.Hex() == "" {
  115. return nil, errors.New("id的为空")
  116. }
  117. plan.UpdateTime = time.Now()
  118. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, plan.Id.Hex(), &plan)
  119. }
  120. // 删除生产计划
  121. func DelProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  122. planId := c.Param("id")
  123. if planId == "" {
  124. return nil, errors.New("id为空")
  125. }
  126. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, planId)
  127. }