plan.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. info := model.Setting{}
  64. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  65. CollectName: "infos",
  66. }, &info)
  67. offset := 0
  68. for _, mat := range curComp.Mats {
  69. // 采购单
  70. _purchaseId := mat.BillId
  71. purchaseId, err := primitive.ObjectIDFromHex(_purchaseId)
  72. if err == nil {
  73. purchase := model.PurchaseBill{}
  74. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  75. CollectName: repo.CollectionBillPurchase,
  76. Query: repo.Map{"_id": purchaseId},
  77. }, &purchase)
  78. if found {
  79. purchaseExcel := NewPurchaseBill(f)
  80. purchaseExcel.Content = &purchase
  81. purchaseExcel.Title = fmt.Sprintf("%s原材料采购单", info.CompanyName)
  82. //设置对应的数据
  83. purchaseExcel.Offset = offset
  84. purchaseExcel.Draws()
  85. offset += 15
  86. }
  87. }
  88. if len(mat.Crafts) > 0 {
  89. for _, carft := range mat.Crafts {
  90. // 加工单
  91. _produceId := carft.BillId
  92. produceId, err := primitive.ObjectIDFromHex(_produceId)
  93. if err == nil {
  94. produce := model.ProduceBill{}
  95. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  96. CollectName: repo.CollectionBillProduce,
  97. Query: repo.Map{"_id": produceId},
  98. }, &produce)
  99. if found {
  100. produceExcel := NewProduceBill(f)
  101. produceExcel.Content = &produce
  102. produceExcel.Title = fmt.Sprintf("%s加工单", info.CompanyName)
  103. //设置对应的数据
  104. produceExcel.Offset = offset
  105. produceExcel.Draws()
  106. offset += 15
  107. }
  108. }
  109. }
  110. }
  111. }
  112. c.Header("Content-Type", "application/octet-stream")
  113. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  114. c.Header("Content-Transfer-Encoding", "binary")
  115. err = f.Write(c.Writer)
  116. if err != nil {
  117. return nil, err
  118. }
  119. return nil, nil
  120. }
  121. // 创建生产计划
  122. func CreateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  123. var plan model.ProductPlan
  124. err := c.ShouldBindJSON(&plan)
  125. if err != nil {
  126. fmt.Println(err)
  127. return nil, errors.New("参数错误!")
  128. }
  129. ctx := apictx.CreateRepoCtx()
  130. if plan.Name == "" {
  131. return nil, errors.New("生产计划名为空")
  132. }
  133. if plan.Total == 0 {
  134. return nil, errors.New("生产计划数应不为0")
  135. }
  136. plan.Status = "process" // 进行中
  137. plan.CreateTime = time.Now()
  138. plan.UpdateTime = time.Now()
  139. result, err := repo.RepoAddDoc(ctx, repo.CollectionProductPlan, &plan)
  140. return result, err
  141. }
  142. // 获取生产计划信息
  143. func GetProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  144. planId := c.Param("id")
  145. id, err := primitive.ObjectIDFromHex(planId)
  146. if err != nil {
  147. return nil, errors.New("非法id")
  148. }
  149. var plan model.ProductPlan
  150. option := &repo.DocSearchOptions{
  151. CollectName: repo.CollectionProductPlan,
  152. Query: repo.Map{"_id": id},
  153. }
  154. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &plan)
  155. if !found || err != nil {
  156. log.Info(err)
  157. return nil, errors.New("数据未找到")
  158. }
  159. billStates := map[string]string{}
  160. if plan.Pack != nil && plan.Pack.Components != nil {
  161. for _, comp := range plan.Pack.Components {
  162. if comp.Mats != nil {
  163. for _, mat := range comp.Mats {
  164. if len(mat.BillId) > 0 {
  165. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionBillPurchase, Query: repo.Map{"_id": mat.BillId}, Project: []string{"status"}})
  166. if ok {
  167. billStates[mat.BillId] = state["status"].(string)
  168. }
  169. }
  170. if mat.Crafts != nil {
  171. for _, craft := range mat.Crafts {
  172. if len(craft.BillId) > 0 {
  173. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionBillProduce, Query: repo.Map{"_id": craft.BillId}, Project: []string{"status"}})
  174. if ok {
  175. billStates[craft.BillId] = state["status"].(string)
  176. }
  177. }
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }
  184. return map[string]interface{}{
  185. "plan": plan,
  186. "billStates": billStates,
  187. }, nil
  188. }
  189. // 获取生产计划列表
  190. func GetProductPlans(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  191. page, size, query := UtilQueryPageSize(c)
  192. option := &repo.PageSearchOptions{
  193. CollectName: repo.CollectionProductPlan,
  194. Query: query,
  195. Page: page,
  196. Size: size,
  197. Sort: bson.M{"createTime": -1},
  198. Project: []string{"_id", "thumbnail", "name", "updateTime", "createUser", "total", "totalPrice", "status"},
  199. }
  200. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  201. }
  202. // 更新生产计划
  203. func UpdateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  204. var plan model.ProductPlan
  205. err := c.ShouldBindJSON(&plan)
  206. if err != nil {
  207. return nil, errors.New("参数错误")
  208. }
  209. if plan.Id.Hex() == "" {
  210. return nil, errors.New("id的为空")
  211. }
  212. plan.UpdateTime = time.Now()
  213. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, plan.Id.Hex(), &plan)
  214. }
  215. // 删除生产计划
  216. func DelProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  217. planId := c.Param("id")
  218. if planId == "" {
  219. return nil, errors.New("id为空")
  220. }
  221. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, planId)
  222. }