bill-produce.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. st, ok1 := query["startTime"]
  136. delete(query, "startTime")
  137. et, ok2 := query["endTime"]
  138. delete(query, "endTime")
  139. if ok1 && ok2 {
  140. startTime := st.(string)
  141. endTime := et.(string)
  142. start, end := getTimeRange(startTime, endTime)
  143. query["createTime"] = bson.M{"$gte": start, "$lte": end}
  144. }
  145. option := &repo.PageSearchOptions{
  146. CollectName: repo.CollectionBillProduce,
  147. Query: query,
  148. Page: page,
  149. Size: size,
  150. Sort: bson.M{"createTime": -1},
  151. }
  152. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  153. }
  154. // 更新单据
  155. func UpdateProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  156. var bill model.ProduceBill
  157. err := c.ShouldBindJSON(&bill)
  158. if err != nil {
  159. return nil, errors.New("参数错误")
  160. }
  161. if bill.Id.Hex() == "" {
  162. return nil, errors.New("id的为空")
  163. }
  164. billType, err := searchBillTypeById(apictx, repo.CollectionBillProduce, bill.Id)
  165. if err != nil {
  166. return nil, err
  167. }
  168. // 如果更改类型
  169. if billType != bill.Type {
  170. bill.SerialNumber, err = generateSerial(apictx, bill.Type)
  171. if err != nil {
  172. return nil, err
  173. }
  174. }
  175. bill.UpdateTime = time.Now()
  176. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, bill.Id.Hex(), &bill)
  177. }
  178. // 删除单据
  179. func DelProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  180. billId := c.Param("id")
  181. if billId == "" {
  182. return nil, errors.New("id为空")
  183. }
  184. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, billId)
  185. }
  186. func DownProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  187. billId := c.Query("id")
  188. isPdf := c.Query("isPdf")
  189. if len(billId) < 1 {
  190. return nil, fmt.Errorf("id不能为空")
  191. }
  192. id, err := primitive.ObjectIDFromHex(billId)
  193. if err != nil {
  194. return nil, errors.New("非法id")
  195. }
  196. var bill model.ProduceBill
  197. option := &repo.DocSearchOptions{
  198. CollectName: repo.CollectionBillProduce,
  199. Query: repo.Map{"_id": id},
  200. }
  201. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  202. if !found || err != nil {
  203. log.Info(err)
  204. return nil, errors.New("数据未找到")
  205. }
  206. f := excelize.NewFile()
  207. // Create a new sheet.
  208. index := f.NewSheet("Sheet1")
  209. f.SetActiveSheet(index)
  210. f.SetDefaultFont("宋体")
  211. billExcel := NewProduceBill(f)
  212. // 获取已审核的签名数据
  213. if bill.Reviewed == 1 {
  214. if len(bill.SignUsers) > 0 {
  215. signs := []*model.Signature{}
  216. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  217. CollectName: repo.CollectionSignature,
  218. Query: repo.Map{"userId": bson.M{"$in": bill.SignUsers}},
  219. Sort: bson.M{"sort": 1}, // 升序
  220. }, &signs)
  221. billExcel.Signatures = signs
  222. }
  223. }
  224. billExcel.Content = &bill
  225. info := model.Setting{}
  226. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  227. CollectName: "infos",
  228. }, &info)
  229. billExcel.Title = fmt.Sprintf("%s加工单", info.CompanyName)
  230. //设置对应的数据
  231. billExcel.Draws()
  232. // 下载为pdf
  233. if isPdf == "true" {
  234. buf, _ := f.WriteToBuffer()
  235. res, err := excelToPdf(buf, apictx.Svc.Conf.PdfApiAddr)
  236. if err != nil {
  237. return nil, errors.New("转化pdf失败")
  238. }
  239. c.Header("Content-Type", "application/octet-stream")
  240. c.Header("Content-Disposition", "attachment; filename="+"bill.pdf")
  241. c.Header("Content-Transfer-Encoding", "binary")
  242. err = res.Write(c.Writer)
  243. if err != nil {
  244. return nil, err
  245. }
  246. return nil, nil
  247. }
  248. c.Header("Content-Type", "application/octet-stream")
  249. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  250. c.Header("Content-Transfer-Encoding", "binary")
  251. err = f.Write(c.Writer)
  252. if err != nil {
  253. return nil, err
  254. }
  255. return nil, nil
  256. }