bill.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 := &MatBillReq{}
  35. err := c.ShouldBindJSON(req)
  36. if err != nil || req.Bill == nil || req.CompIndex == nil || req.MatIndex == nil {
  37. fmt.Println(err)
  38. return nil, errors.New("参数错误!compIndex, matIndex 不能为空")
  39. }
  40. ctx := apictx.CreateRepoCtx()
  41. bill := req.Bill
  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.Status = "created"
  52. bill.CreateTime = time.Now()
  53. bill.UpdateTime = time.Now()
  54. result, err := repo.RepoAddDoc(ctx, repo.CollectionBillPurchase, &bill)
  55. if err != nil {
  56. return nil, err
  57. }
  58. updat := bson.M{}
  59. matkey := fmt.Sprintf("pack.components.%d.mats.%d.billId", *req.CompIndex, *req.MatIndex)
  60. updat[matkey] = result
  61. ret, err := repo.RepoUpdateSetDocProps(apictx.CreateRepoCtx(), repo.CollectionProductPlan, bill.PlanId.Hex(), bson.M{"$set": updat})
  62. if ret.ModifiedCount == 1 && err == nil {
  63. return result, nil
  64. }
  65. return nil, fmt.Errorf("创建失败")
  66. }
  67. // 获取单据信息
  68. func GetBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  69. billId := c.Param("id")
  70. id, err := primitive.ObjectIDFromHex(billId)
  71. if err != nil {
  72. return nil, errors.New("非法id")
  73. }
  74. var bill model.PurchaseBill
  75. option := &repo.DocSearchOptions{
  76. CollectName: repo.CollectionBillPurchase,
  77. Query: repo.Map{"_id": id},
  78. }
  79. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  80. if !found || err != nil {
  81. log.Info(err)
  82. return nil, errors.New("数据未找到")
  83. }
  84. return bill, nil
  85. }
  86. // 获取单据列表
  87. func GetBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  88. page, size, query := UtilQueryPageSize(c)
  89. if query["packId"] != nil {
  90. query["packId"], _ = primitive.ObjectIDFromHex(query["packId"].(string))
  91. }
  92. if query["planId"] != nil {
  93. query["planId"], _ = primitive.ObjectIDFromHex(query["planId"].(string))
  94. }
  95. option := &repo.PageSearchOptions{
  96. CollectName: repo.CollectionBillPurchase,
  97. Query: query,
  98. Page: page,
  99. Size: size,
  100. Sort: bson.M{"createTime": -1},
  101. }
  102. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  103. }
  104. // 更新单据
  105. func UpdateBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  106. var bill model.PurchaseBill
  107. err := c.ShouldBindJSON(&bill)
  108. if err != nil {
  109. return nil, errors.New("参数错误")
  110. }
  111. if bill.Id.Hex() == "" {
  112. return nil, errors.New("id的为空")
  113. }
  114. bill.UpdateTime = time.Now()
  115. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, bill.Id.Hex(), &bill)
  116. }
  117. // 删除单据
  118. func DelBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  119. billId := c.Param("id")
  120. if billId == "" {
  121. return nil, errors.New("id为空")
  122. }
  123. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, billId)
  124. }