plan.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. return plan, nil
  65. }
  66. // 获取生产计划列表
  67. func GetProductPlans(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  68. page, size, query := UtilQueryPageSize(c)
  69. option := &repo.PageSearchOptions{
  70. CollectName: repo.CollectionProductPlan,
  71. Query: query,
  72. Page: page,
  73. Size: size,
  74. Sort: bson.M{"createTime": -1},
  75. Project: []string{"_id", "thumbnail", "name", "updateTime", "createUser", "total", "totalPrice", "status"},
  76. }
  77. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  78. }
  79. // 更新生产计划
  80. func UpdateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  81. var plan model.ProductPlan
  82. err := c.ShouldBindJSON(&plan)
  83. if err != nil {
  84. return nil, errors.New("参数错误")
  85. }
  86. if plan.Id.Hex() == "" {
  87. return nil, errors.New("id的为空")
  88. }
  89. plan.UpdateTime = time.Now()
  90. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, plan.Id.Hex(), &plan)
  91. }
  92. // 删除生产计划
  93. func DelProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  94. planId := c.Param("id")
  95. if planId == "" {
  96. return nil, errors.New("id为空")
  97. }
  98. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, planId)
  99. }