bill.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.Status = "created"
  52. bill.CreateTime = time.Now()
  53. bill.UpdateTime = time.Now()
  54. return repo.RepoAddDoc(ctx, repo.CollectionBillPurchase, &bill)
  55. }
  56. // 获取单据信息
  57. func GetBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  58. billId := c.Param("id")
  59. id, err := primitive.ObjectIDFromHex(billId)
  60. if err != nil {
  61. return nil, errors.New("非法id")
  62. }
  63. var bill model.PurchaseBill
  64. option := &repo.DocSearchOptions{
  65. CollectName: repo.CollectionBillPurchase,
  66. Query: repo.Map{"_id": id},
  67. }
  68. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  69. if !found || err != nil {
  70. log.Info(err)
  71. return nil, errors.New("数据未找到")
  72. }
  73. return bill, nil
  74. }
  75. // 获取单据列表
  76. func GetBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  77. page, size, query := UtilQueryPageSize(c)
  78. if query["packId"] != nil {
  79. query["packId"], _ = primitive.ObjectIDFromHex(query["packId"].(string))
  80. }
  81. if query["planId"] != nil {
  82. query["planId"], _ = primitive.ObjectIDFromHex(query["planId"].(string))
  83. }
  84. option := &repo.PageSearchOptions{
  85. CollectName: repo.CollectionBillPurchase,
  86. Query: query,
  87. Page: page,
  88. Size: size,
  89. Sort: bson.M{"createTime": -1},
  90. }
  91. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  92. }
  93. // 更新单据
  94. func UpdateBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  95. var bill model.PurchaseBill
  96. err := c.ShouldBindJSON(&bill)
  97. if err != nil {
  98. return nil, errors.New("参数错误")
  99. }
  100. if bill.Id.Hex() == "" {
  101. return nil, errors.New("id的为空")
  102. }
  103. bill.UpdateTime = time.Now()
  104. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, bill.Id.Hex(), &bill)
  105. }
  106. // 删除单据
  107. func DelBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  108. billId := c.Param("id")
  109. if billId == "" {
  110. return nil, errors.New("id为空")
  111. }
  112. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, billId)
  113. }