bill-produce.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 BillProduce(r *GinRouter) {
  16. // 创建单据
  17. r.POST("/bill/produce/create", CreateProduceBill)
  18. // 获取单据详情
  19. r.GET("/bill/produce/detail/:id", GetProduceBill)
  20. // 获取单据列表
  21. r.GET("/bill/produce/list", GetProduceBills)
  22. // 更新单据
  23. r.POST("/bill/produce/update", UpdateProduceBill)
  24. // 删除单据
  25. r.POST("/bill/produce/delete/:id", DelProduceBill)
  26. //下载单据
  27. r.GET("/bill/produce/download", DownProduceBill)
  28. // 审核单据
  29. r.POSTJWT("/bill/produce/review/:id", ProduceReview)
  30. }
  31. // 审核单据
  32. func ProduceReview(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  33. _id := c.Param("id")
  34. userId, err := primitive.ObjectIDFromHex(apictx.User.Parent)
  35. if err != nil {
  36. return nil, errors.New("用户异常")
  37. }
  38. user, err := getUserById(apictx, userId)
  39. if err != nil {
  40. return nil, errors.New("查找用户失败")
  41. }
  42. if !isManager(user.Roles) {
  43. return nil, errors.New("该用户没有权限")
  44. }
  45. // 更改状态为已审核 并签字
  46. signs := make([]primitive.ObjectID, 0)
  47. signs = append(signs, userId)
  48. produce := model.ProduceBill{
  49. Reviewed: 1,
  50. UpdateTime: time.Now(),
  51. SignUsers: signs,
  52. }
  53. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, _id, &produce)
  54. }
  55. // 创建生产加工单据
  56. func CreateProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  57. bill := &model.ProduceBill{}
  58. err := c.ShouldBindJSON(bill)
  59. if err != nil {
  60. fmt.Println(err)
  61. return nil, errors.New("参数错误!")
  62. }
  63. ctx := apictx.CreateRepoCtx()
  64. if bill.PackId.Hex() == "" {
  65. return nil, errors.New("包装产品id为空")
  66. }
  67. if bill.PlanId.Hex() == "" {
  68. return nil, errors.New("生产计划id为空")
  69. }
  70. if bill.Type == "" {
  71. return nil, errors.New("类型为空")
  72. }
  73. bill.SerialNumber, err = generateSerial(apictx, bill.Type)
  74. if err != nil {
  75. return nil, err
  76. }
  77. bill.Status = "created"
  78. if bill.Reviewed == 0 {
  79. bill.Reviewed = -1
  80. }
  81. bill.CreateTime = time.Now()
  82. bill.UpdateTime = time.Now()
  83. result, err := repo.RepoAddDoc(ctx, repo.CollectionBillProduce, &bill)
  84. return result, err
  85. }
  86. // 获取单据信息
  87. func GetProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  88. billId := c.Param("id")
  89. id, err := primitive.ObjectIDFromHex(billId)
  90. if err != nil {
  91. return nil, errors.New("非法id")
  92. }
  93. var bill model.ProduceBill
  94. option := &repo.DocSearchOptions{
  95. CollectName: repo.CollectionBillProduce,
  96. Query: repo.Map{"_id": id},
  97. }
  98. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  99. if !found || err != nil {
  100. log.Info(err)
  101. return nil, errors.New("数据未找到")
  102. }
  103. return bill, nil
  104. }
  105. // 获取单据列表
  106. func GetProduceBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  107. page, size, query := UtilQueryPageSize(c)
  108. if query["packId"] != nil {
  109. query["packId"], _ = primitive.ObjectIDFromHex(query["packId"].(string))
  110. }
  111. if query["planId"] != nil {
  112. query["planId"], _ = primitive.ObjectIDFromHex(query["planId"].(string))
  113. }
  114. option := &repo.PageSearchOptions{
  115. CollectName: repo.CollectionBillProduce,
  116. Query: query,
  117. Page: page,
  118. Size: size,
  119. Sort: bson.M{"createTime": -1},
  120. }
  121. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  122. }
  123. // 更新单据
  124. func UpdateProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  125. var bill model.ProduceBill
  126. err := c.ShouldBindJSON(&bill)
  127. if err != nil {
  128. return nil, errors.New("参数错误")
  129. }
  130. if bill.Id.Hex() == "" {
  131. return nil, errors.New("id的为空")
  132. }
  133. billType, err := searchBillTypeById(apictx, repo.CollectionBillProduce, bill.Id)
  134. if err != nil {
  135. return nil, err
  136. }
  137. // 如果更改类型
  138. if billType != bill.Type {
  139. bill.SerialNumber, err = generateSerial(apictx, bill.Type)
  140. if err != nil {
  141. return nil, err
  142. }
  143. }
  144. bill.UpdateTime = time.Now()
  145. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, bill.Id.Hex(), &bill)
  146. }
  147. // 删除单据
  148. func DelProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  149. billId := c.Param("id")
  150. if billId == "" {
  151. return nil, errors.New("id为空")
  152. }
  153. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, billId)
  154. }
  155. func DownProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  156. billId := c.Query("id")
  157. isPdf := c.Query("isPdf")
  158. if len(billId) < 1 {
  159. return nil, fmt.Errorf("id不能为空")
  160. }
  161. id, err := primitive.ObjectIDFromHex(billId)
  162. if err != nil {
  163. return nil, errors.New("非法id")
  164. }
  165. var bill model.ProduceBill
  166. option := &repo.DocSearchOptions{
  167. CollectName: repo.CollectionBillProduce,
  168. Query: repo.Map{"_id": id},
  169. }
  170. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  171. if !found || err != nil {
  172. log.Info(err)
  173. return nil, errors.New("数据未找到")
  174. }
  175. f := excelize.NewFile()
  176. // Create a new sheet.
  177. index := f.NewSheet("Sheet1")
  178. f.SetActiveSheet(index)
  179. f.SetDefaultFont("宋体")
  180. billExcel := NewProduceBill(f)
  181. // 获取已审核的签名数据
  182. if bill.Reviewed == 1 {
  183. if len(bill.SignUsers) > 0 {
  184. signs := []*model.Signature{}
  185. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  186. CollectName: repo.CollectionSignature,
  187. Query: repo.Map{"userId": bson.M{"$in": bill.SignUsers}},
  188. Sort: bson.M{"sort": 1}, // 升序
  189. }, &signs)
  190. billExcel.Signatures = signs
  191. }
  192. }
  193. billExcel.Content = &bill
  194. info := model.Setting{}
  195. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  196. CollectName: "infos",
  197. }, &info)
  198. billExcel.Title = fmt.Sprintf("%s加工单", info.CompanyName)
  199. //设置对应的数据
  200. billExcel.Draws()
  201. // 下载为pdf
  202. if isPdf == "true" {
  203. buf, _ := f.WriteToBuffer()
  204. res, err := excelToPdf(buf, apictx.Svc.Conf.PdfApiAddr)
  205. if err != nil {
  206. return nil, errors.New("转化pdf失败")
  207. }
  208. c.Header("Content-Type", "application/octet-stream")
  209. c.Header("Content-Disposition", "attachment; filename="+"bill.pdf")
  210. c.Header("Content-Transfer-Encoding", "binary")
  211. err = res.Write(c.Writer)
  212. if err != nil {
  213. return nil, err
  214. }
  215. return nil, nil
  216. }
  217. c.Header("Content-Type", "application/octet-stream")
  218. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  219. c.Header("Content-Transfer-Encoding", "binary")
  220. err = f.Write(c.Writer)
  221. if err != nil {
  222. return nil, err
  223. }
  224. return nil, nil
  225. }