bill-produce.go 7.3 KB

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