plan.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 ProductPlan(r *GinRouter) {
  16. // 创建生产计划
  17. r.POST("/plan/create", CreateProductPlan)
  18. // 获取生产计划详情
  19. r.GET("/plan/detail/:id", GetProductPlan)
  20. // 获取生产计划列表
  21. r.GET("/plan/list", GetProductPlans)
  22. // 更新生产计划
  23. r.POST("/plan/update", UpdateProductPlan)
  24. // 删除生产计划
  25. r.POST("/plan/delete/:id", DelProductPlan)
  26. // 下载部件打印单
  27. r.GET("/bill/plan/download", DownLoadPlan)
  28. }
  29. func DownLoadPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  30. _planId := c.Query("id")
  31. compId := c.Query("compId")
  32. planId, err := primitive.ObjectIDFromHex(_planId)
  33. if err != nil {
  34. return nil, errors.New("packId错误")
  35. }
  36. plan := model.ProductPlan{}
  37. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  38. CollectName: repo.CollectionProductPlan,
  39. Query: repo.Map{"_id": planId},
  40. }, &plan)
  41. if !found || err != nil {
  42. return nil, errors.New("数据未找到")
  43. }
  44. // 获取部件单据
  45. curComp := &model.PackComponent{}
  46. for _, comp := range plan.Pack.Components {
  47. if comp.Id == compId {
  48. curComp = comp
  49. }
  50. }
  51. if curComp.Id == "" {
  52. return nil, errors.New("该组件不存在")
  53. }
  54. // 获取bill
  55. if len(curComp.Mats) == 0 {
  56. return nil, errors.New("该组件数据不存在")
  57. }
  58. f := excelize.NewFile()
  59. // Create a new sheet.
  60. index := f.NewSheet("Sheet1")
  61. f.SetActiveSheet(index)
  62. f.SetDefaultFont("宋体")
  63. //
  64. offset := 0
  65. for _, mat := range curComp.Mats {
  66. // 采购单
  67. _purchaseId := mat.BillId
  68. purchaseId, err := primitive.ObjectIDFromHex(_purchaseId)
  69. if err == nil {
  70. purchase := model.PurchaseBill{}
  71. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  72. CollectName: repo.CollectionBillPurchase,
  73. Query: repo.Map{"_id": purchaseId},
  74. }, &purchase)
  75. if found {
  76. purchaseExcel := NewPurchaseBill(f)
  77. purchaseExcel.Content = &purchase
  78. //设置对应的数据
  79. purchaseExcel.Offset = offset
  80. purchaseExcel.Draws()
  81. offset += 15
  82. }
  83. }
  84. if len(mat.Crafts) > 0 {
  85. for _, carft := range mat.Crafts {
  86. // 加工单
  87. _produceId := carft.BillId
  88. produceId, err := primitive.ObjectIDFromHex(_produceId)
  89. if err == nil {
  90. produce := model.ProduceBill{}
  91. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  92. CollectName: repo.CollectionBillProduce,
  93. Query: repo.Map{"_id": produceId},
  94. }, &produce)
  95. if found {
  96. produceExcel := NewProduceBill(f)
  97. produceExcel.Content = &produce
  98. //设置对应的数据
  99. produceExcel.Offset = offset
  100. produceExcel.Draws()
  101. offset += 15
  102. }
  103. }
  104. }
  105. }
  106. }
  107. c.Header("Content-Type", "application/octet-stream")
  108. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  109. c.Header("Content-Transfer-Encoding", "binary")
  110. err = f.Write(c.Writer)
  111. if err != nil {
  112. return nil, err
  113. }
  114. return nil, nil
  115. }
  116. // 创建生产计划
  117. func CreateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  118. var plan model.ProductPlan
  119. err := c.ShouldBindJSON(&plan)
  120. if err != nil {
  121. fmt.Println(err)
  122. return nil, errors.New("参数错误!")
  123. }
  124. ctx := apictx.CreateRepoCtx()
  125. if plan.Name == "" {
  126. return nil, errors.New("生产计划名为空")
  127. }
  128. if plan.Total == 0 {
  129. return nil, errors.New("生产计划数应不为0")
  130. }
  131. plan.Status = "process" // 进行中
  132. plan.CreateTime = time.Now()
  133. plan.UpdateTime = time.Now()
  134. result, err := repo.RepoAddDoc(ctx, repo.CollectionProductPlan, &plan)
  135. return result, err
  136. }
  137. // 获取生产计划信息
  138. func GetProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  139. planId := c.Param("id")
  140. id, err := primitive.ObjectIDFromHex(planId)
  141. if err != nil {
  142. return nil, errors.New("非法id")
  143. }
  144. var plan model.ProductPlan
  145. option := &repo.DocSearchOptions{
  146. CollectName: repo.CollectionProductPlan,
  147. Query: repo.Map{"_id": id},
  148. }
  149. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &plan)
  150. if !found || err != nil {
  151. log.Info(err)
  152. return nil, errors.New("数据未找到")
  153. }
  154. billStates := map[string]string{}
  155. if plan.Pack != nil && plan.Pack.Components != nil {
  156. for _, comp := range plan.Pack.Components {
  157. if comp.Mats != nil {
  158. for _, mat := range comp.Mats {
  159. if len(mat.BillId) > 0 {
  160. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionBillPurchase, Query: repo.Map{"_id": mat.BillId}, Project: []string{"status"}})
  161. if ok {
  162. billStates[mat.BillId] = state["status"].(string)
  163. }
  164. }
  165. if mat.Crafts != nil {
  166. for _, craft := range mat.Crafts {
  167. if len(craft.BillId) > 0 {
  168. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionBillProduce, Query: repo.Map{"_id": craft.BillId}, Project: []string{"status"}})
  169. if ok {
  170. billStates[craft.BillId] = state["status"].(string)
  171. }
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. return map[string]interface{}{
  180. "plan": plan,
  181. "billStates": billStates,
  182. }, nil
  183. }
  184. // 获取生产计划列表
  185. func GetProductPlans(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  186. page, size, query := UtilQueryPageSize(c)
  187. option := &repo.PageSearchOptions{
  188. CollectName: repo.CollectionProductPlan,
  189. Query: query,
  190. Page: page,
  191. Size: size,
  192. Sort: bson.M{"createTime": -1},
  193. Project: []string{"_id", "thumbnail", "name", "updateTime", "createUser", "total", "totalPrice", "status"},
  194. }
  195. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  196. }
  197. // 更新生产计划
  198. func UpdateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  199. var plan model.ProductPlan
  200. err := c.ShouldBindJSON(&plan)
  201. if err != nil {
  202. return nil, errors.New("参数错误")
  203. }
  204. if plan.Id.Hex() == "" {
  205. return nil, errors.New("id的为空")
  206. }
  207. plan.UpdateTime = time.Now()
  208. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, plan.Id.Hex(), &plan)
  209. }
  210. // 删除生产计划
  211. func DelProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  212. planId := c.Param("id")
  213. if planId == "" {
  214. return nil, errors.New("id为空")
  215. }
  216. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, planId)
  217. }