bill-produce.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. // 制单人数据
  103. userId, _ := primitive.ObjectIDFromHex(apictx.User.Parent)
  104. if !userId.IsZero() {
  105. user, err := getUserById(apictx, userId)
  106. if err != nil {
  107. bill.UserName = user.Name
  108. bill.UserId = userId
  109. }
  110. }
  111. result, err := repo.RepoAddDoc(ctx, repo.CollectionBillProduce, &bill)
  112. return result, err
  113. }
  114. // 获取单据信息
  115. func GetProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  116. billId := c.Param("id")
  117. id, err := primitive.ObjectIDFromHex(billId)
  118. if err != nil {
  119. return nil, errors.New("非法id")
  120. }
  121. var bill model.ProduceBill
  122. option := &repo.DocSearchOptions{
  123. CollectName: repo.CollectionBillProduce,
  124. Query: repo.Map{"_id": id},
  125. }
  126. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  127. if !found || err != nil {
  128. log.Info(err)
  129. return nil, errors.New("数据未找到")
  130. }
  131. return bill, nil
  132. }
  133. // 获取单据列表
  134. func GetProduceBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  135. page, size, query := UtilQueryPageSize(c)
  136. if query["packId"] != nil {
  137. query["packId"], _ = primitive.ObjectIDFromHex(query["packId"].(string))
  138. }
  139. if query["planId"] != nil {
  140. query["planId"], _ = primitive.ObjectIDFromHex(query["planId"].(string))
  141. }
  142. // 时间范围查询
  143. // createTime 选中的当天时间
  144. st, ok1 := query["startTime"]
  145. delete(query, "startTime")
  146. et, ok2 := query["endTime"]
  147. delete(query, "endTime")
  148. if ok1 && ok2 {
  149. startTime := st.(string)
  150. endTime := et.(string)
  151. start, end := getTimeRange(startTime, endTime)
  152. query["createTime"] = bson.M{"$gte": start, "$lte": end}
  153. }
  154. option := &repo.PageSearchOptions{
  155. CollectName: repo.CollectionBillProduce,
  156. Query: query,
  157. Page: page,
  158. Size: size,
  159. Sort: bson.M{"createTime": -1},
  160. }
  161. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  162. }
  163. // 更新单据
  164. func UpdateProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  165. var bill model.ProduceBill
  166. err := c.ShouldBindJSON(&bill)
  167. if err != nil {
  168. return nil, errors.New("参数错误")
  169. }
  170. if bill.Id.Hex() == "" {
  171. return nil, errors.New("id的为空")
  172. }
  173. billType, err := searchBillTypeById(apictx, repo.CollectionBillProduce, bill.Id)
  174. if err != nil {
  175. return nil, err
  176. }
  177. // 如果更改类型
  178. if billType != bill.Type {
  179. bill.SerialNumber, err = generateSerial(apictx, bill.Type)
  180. if err != nil {
  181. return nil, err
  182. }
  183. }
  184. if bill.Status == "complete" {
  185. bill.CompleteTime = time.Now()
  186. }
  187. bill.UpdateTime = time.Now()
  188. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, bill.Id.Hex(), &bill)
  189. }
  190. // 删除单据
  191. func DelProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  192. billId := c.Param("id")
  193. if billId == "" {
  194. return nil, errors.New("id为空")
  195. }
  196. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, billId)
  197. }
  198. func DownProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  199. billId := c.Query("id")
  200. isPdf := c.Query("isPdf")
  201. if len(billId) < 1 {
  202. return nil, fmt.Errorf("id不能为空")
  203. }
  204. id, err := primitive.ObjectIDFromHex(billId)
  205. if err != nil {
  206. return nil, errors.New("非法id")
  207. }
  208. var bill model.ProduceBill
  209. option := &repo.DocSearchOptions{
  210. CollectName: repo.CollectionBillProduce,
  211. Query: repo.Map{"_id": id},
  212. }
  213. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &bill)
  214. if !found || err != nil {
  215. log.Info(err)
  216. return nil, errors.New("数据未找到")
  217. }
  218. f := excelize.NewFile()
  219. // Create a new sheet.
  220. index := f.NewSheet("Sheet1")
  221. f.SetActiveSheet(index)
  222. f.SetDefaultFont("宋体")
  223. billExcel := NewProduceBill(f)
  224. // 获取已审核的签名数据
  225. if bill.Reviewed == 1 {
  226. if len(bill.SignUsers) > 0 {
  227. signs := []*model.Signature{}
  228. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  229. CollectName: repo.CollectionSignature,
  230. Query: repo.Map{"_id": bson.M{"$in": bill.SignUsers}},
  231. Sort: bson.M{"sort": 1}, // 升序
  232. }, &signs)
  233. billExcel.Signatures = signs
  234. }
  235. }
  236. billExcel.Content = &bill
  237. companyName := getCompanyName(apictx)
  238. billExcel.Title = fmt.Sprintf("%s加工单", companyName)
  239. //设置对应的数据
  240. billExcel.Draws()
  241. // 下载为pdf
  242. if isPdf == "true" {
  243. buf, _ := f.WriteToBuffer()
  244. res, err := excelToPdf(buf, apictx.Svc.Conf.PdfApiAddr)
  245. if err != nil {
  246. return nil, errors.New("转化pdf失败")
  247. }
  248. c.Header("Content-Type", "application/octet-stream")
  249. c.Header("Content-Disposition", "attachment; filename="+"bill.pdf")
  250. c.Header("Content-Transfer-Encoding", "binary")
  251. err = res.Write(c.Writer)
  252. if err != nil {
  253. return nil, err
  254. }
  255. return nil, nil
  256. }
  257. c.Header("Content-Type", "application/octet-stream")
  258. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  259. c.Header("Content-Transfer-Encoding", "binary")
  260. err = f.Write(c.Writer)
  261. if err != nil {
  262. return nil, err
  263. }
  264. return nil, nil
  265. }