bill.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. "github.com/xuri/excelize/v2"
  11. "go.mongodb.org/mongo-driver/bson"
  12. "go.mongodb.org/mongo-driver/bson/primitive"
  13. )
  14. // 单据管理
  15. func Bill(r *GinRouter) {
  16. // 创建单据
  17. r.POST("/bill/purchase/create", CreateBill)
  18. // 获取单据详情
  19. r.GET("/bill/purchase/detail/:id", GetBill)
  20. // 获取单据列表
  21. r.GET("/bill/purchase/list", GetBills)
  22. // 获取单据列表
  23. r.GET("/bill/purchase/download", DownLoadBills)
  24. // 更新单据
  25. r.POST("/bill/purchase/update", UpdateBill)
  26. // 删除单据
  27. r.POST("/bill/purchase/delete/:id", DelBill)
  28. }
  29. type MatBillReq struct {
  30. Bill *model.PurchaseBill
  31. CompIndex *int
  32. MatIndex *int
  33. //MatKey string //components.0.mats.0.billId
  34. }
  35. // 创建单据
  36. func CreateBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  37. req := &model.PurchaseBill{}
  38. err := c.ShouldBindJSON(req)
  39. if err != nil {
  40. fmt.Println(err)
  41. return nil, errors.New("参数错误")
  42. }
  43. ctx := apictx.CreateRepoCtx()
  44. bill := req
  45. if bill.PackId.Hex() == "" {
  46. return nil, errors.New("包装产品id为空")
  47. }
  48. if bill.PlanId.Hex() == "" {
  49. return nil, errors.New("生产计划id为空")
  50. }
  51. if bill.Type == "" {
  52. return nil, errors.New("类型为空")
  53. }
  54. bill.SerialNumber, err = generateSerial(apictx, bill.Type)
  55. if err != nil {
  56. return nil, err
  57. }
  58. bill.Status = "created"
  59. bill.CreateTime = time.Now()
  60. bill.UpdateTime = time.Now()
  61. return repo.RepoAddDoc(ctx, repo.CollectionBillPurchase, &bill)
  62. }
  63. // 获取单据信息
  64. func GetBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  65. billId := c.Param("id")
  66. id, err := primitive.ObjectIDFromHex(billId)
  67. if err != nil {
  68. return nil, errors.New("非法id")
  69. }
  70. var bill model.PurchaseBill
  71. option := &repo.DocSearchOptions{
  72. CollectName: repo.CollectionBillPurchase,
  73. Query: repo.Map{"_id": id},
  74. }
  75. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  76. if !found || err != nil {
  77. log.Info(err)
  78. return nil, errors.New("数据未找到")
  79. }
  80. return bill, nil
  81. }
  82. // 获取单据列表
  83. func GetBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  84. page, size, query := UtilQueryPageSize(c)
  85. if query["packId"] != nil {
  86. query["packId"], _ = primitive.ObjectIDFromHex(query["packId"].(string))
  87. }
  88. if query["planId"] != nil {
  89. query["planId"], _ = primitive.ObjectIDFromHex(query["planId"].(string))
  90. }
  91. option := &repo.PageSearchOptions{
  92. CollectName: repo.CollectionBillPurchase,
  93. Query: query,
  94. Page: page,
  95. Size: size,
  96. Sort: bson.M{"createTime": -1},
  97. }
  98. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  99. }
  100. func DownLoadBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  101. billId := c.Query("id")
  102. if len(billId) < 1 {
  103. return nil, fmt.Errorf("id不能为空")
  104. }
  105. id, err := primitive.ObjectIDFromHex(billId)
  106. if err != nil {
  107. return nil, errors.New("非法id")
  108. }
  109. var bill model.PurchaseBill
  110. option := &repo.DocSearchOptions{
  111. CollectName: repo.CollectionBillPurchase,
  112. Query: repo.Map{"_id": id},
  113. }
  114. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  115. if !found || err != nil {
  116. log.Info(err)
  117. return nil, errors.New("数据未找到")
  118. }
  119. f := excelize.NewFile()
  120. // Create a new sheet.
  121. index := f.NewSheet("Sheet1")
  122. f.SetActiveSheet(index)
  123. f.SetDefaultFont("宋体")
  124. billExcel := NewPurchaseBill(f)
  125. billExcel.Content = &bill
  126. //设置对应的数据
  127. billExcel.Draws()
  128. c.Header("Content-Type", "application/octet-stream")
  129. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  130. c.Header("Content-Transfer-Encoding", "binary")
  131. err = f.Write(c.Writer)
  132. if err != nil {
  133. return nil, err
  134. }
  135. return nil, nil
  136. }
  137. // 更新单据
  138. func UpdateBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  139. var bill model.PurchaseBill
  140. err := c.ShouldBindJSON(&bill)
  141. if err != nil {
  142. return nil, errors.New("参数错误")
  143. }
  144. if bill.Id.Hex() == "" {
  145. return nil, errors.New("id的为空")
  146. }
  147. billType, err := searchBillTypeById(apictx, repo.CollectionBillPurchase, bill.Id)
  148. if err != nil {
  149. return nil, err
  150. }
  151. // 如果更改类型
  152. if billType != bill.Type {
  153. bill.SerialNumber, err = generateSerial(apictx, bill.Type)
  154. if err != nil {
  155. return nil, err
  156. }
  157. }
  158. bill.UpdateTime = time.Now()
  159. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, bill.Id.Hex(), &bill)
  160. }
  161. // 删除单据
  162. func DelBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  163. billId := c.Param("id")
  164. if billId == "" {
  165. return nil, errors.New("id为空")
  166. }
  167. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, billId)
  168. }