bill.go 3.5 KB

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