bill-produce.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 BillProduce(r *GinRouter) {
  15. // 创建单据
  16. r.POST("/bill/produce/create", CreateProduceBill)
  17. // 获取单据详情
  18. r.GET("/bill/produce/detail/:id", GetProduceBill)
  19. // 获取单据列表
  20. r.GET("/bill/produce/list", GetProduceBills)
  21. // 更新单据
  22. r.POST("/bill/produce/update", UpdateProduceBill)
  23. // 删除单据
  24. r.POST("/bill/produce/delete/:id", DelProduceBill)
  25. }
  26. // 创建生产加工单据
  27. func CreateProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  28. bill := &model.ProduceBill{}
  29. err := c.ShouldBindJSON(bill)
  30. if err != nil {
  31. fmt.Println(err)
  32. return nil, errors.New("参数错误!")
  33. }
  34. ctx := apictx.CreateRepoCtx()
  35. if bill.PackId.Hex() == "" {
  36. return nil, errors.New("包装产品id为空")
  37. }
  38. if bill.PlanId.Hex() == "" {
  39. return nil, errors.New("生产计划id为空")
  40. }
  41. if bill.Type == "" {
  42. return nil, errors.New("类型为空")
  43. }
  44. bill.SerialNumber, err = generateSerial(apictx, bill.Type)
  45. if err != nil {
  46. return nil, err
  47. }
  48. bill.Status = "created"
  49. bill.CreateTime = time.Now()
  50. bill.UpdateTime = time.Now()
  51. result, err := repo.RepoAddDoc(ctx, repo.CollectionBillProduce, &bill)
  52. return result, err
  53. }
  54. // 获取单据信息
  55. func GetProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  56. billId := c.Param("id")
  57. id, err := primitive.ObjectIDFromHex(billId)
  58. if err != nil {
  59. return nil, errors.New("非法id")
  60. }
  61. var bill model.ProduceBill
  62. option := &repo.DocSearchOptions{
  63. CollectName: repo.CollectionBillProduce,
  64. Query: repo.Map{"_id": id},
  65. }
  66. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  67. if !found || err != nil {
  68. log.Info(err)
  69. return nil, errors.New("数据未找到")
  70. }
  71. return bill, nil
  72. }
  73. // 获取单据列表
  74. func GetProduceBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  75. page, size, query := UtilQueryPageSize(c)
  76. if query["packId"] != nil {
  77. query["packId"], _ = primitive.ObjectIDFromHex(query["packId"].(string))
  78. }
  79. if query["planId"] != nil {
  80. query["planId"], _ = primitive.ObjectIDFromHex(query["planId"].(string))
  81. }
  82. option := &repo.PageSearchOptions{
  83. CollectName: repo.CollectionBillProduce,
  84. Query: query,
  85. Page: page,
  86. Size: size,
  87. Sort: bson.M{"createTime": -1},
  88. }
  89. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  90. }
  91. // 更新单据
  92. func UpdateProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  93. var bill model.ProduceBill
  94. err := c.ShouldBindJSON(&bill)
  95. if err != nil {
  96. return nil, errors.New("参数错误")
  97. }
  98. if bill.Id.Hex() == "" {
  99. return nil, errors.New("id的为空")
  100. }
  101. billType, err := searchBillTypeById(apictx, repo.CollectionBillProduce, bill.Id)
  102. if err != nil {
  103. return nil, err
  104. }
  105. // 如果更改类型
  106. if billType != bill.Type {
  107. bill.SerialNumber, err = generateSerial(apictx, bill.Type)
  108. if err != nil {
  109. return nil, err
  110. }
  111. }
  112. bill.UpdateTime = time.Now()
  113. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, bill.Id.Hex(), &bill)
  114. }
  115. // 删除单据
  116. func DelProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  117. billId := c.Param("id")
  118. if billId == "" {
  119. return nil, errors.New("id为空")
  120. }
  121. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, billId)
  122. }