plan.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. "go.mongodb.org/mongo-driver/bson"
  11. "go.mongodb.org/mongo-driver/bson/primitive"
  12. )
  13. // 生产计划管理
  14. func ProductPlan(r *GinRouter) {
  15. // 创建生产计划
  16. r.POST("/plan/create", CreateProductPlan)
  17. // 获取生产计划详情
  18. r.GET("/plan/detail/:id", GetProductPlan)
  19. // 获取生产计划列表
  20. r.GET("/plan/list", GetProductPlans)
  21. // 更新生产计划
  22. r.POST("/plan/update", UpdateProductPlan)
  23. // 删除生产计划
  24. r.POST("/plan/delete/:id", DelProductPlan)
  25. // 下载部件打印单
  26. // r.GET("/bill/plan/download", DownLoadPlan)
  27. // 生产成本表
  28. // r.GET("/plan/cost/download", DownLoadPlanCost)
  29. }
  30. type SupplierPlanCost struct {
  31. *model.ProductPlan
  32. SupplierId string
  33. }
  34. // func DownLoadPlanCost(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  35. // _planId := c.Query("id")
  36. // supplierId := c.Query("supplierId")
  37. // planId, err := primitive.ObjectIDFromHex(_planId)
  38. // if err != nil {
  39. // return nil, errors.New("planId错误")
  40. // }
  41. // supplierPlanCost := &SupplierPlanCost{}
  42. // plan := model.ProductPlan{}
  43. // found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  44. // CollectName: repo.CollectionProductPlan,
  45. // Query: repo.Map{"_id": planId},
  46. // }, &plan)
  47. // if !found || err != nil {
  48. // return nil, errors.New("数据未找到")
  49. // }
  50. // supplierPlanCost.ProductPlan = &plan
  51. // supplierPlanCost.SupplierId = supplierId
  52. // f := excelize.NewFile()
  53. // index := f.NewSheet("Sheet1")
  54. // f.SetActiveSheet(index)
  55. // f.SetDefaultFont("宋体")
  56. // planCostExcel := NewPlanCostExcel(f)
  57. // planCostExcel.Title = fmt.Sprintf("生产成本表(%s)%d盒", plan.Name, plan.Total)
  58. // planCostExcel.Content = supplierPlanCost
  59. // planCostExcel.Draws()
  60. // c.Header("Content-Type", "application/octet-stream")
  61. // c.Header("Content-Disposition", "attachment; filename="+"planCost.xlsx")
  62. // c.Header("Content-Transfer-Encoding", "binary")
  63. // err = f.Write(c.Writer)
  64. // if err != nil {
  65. // return nil, err
  66. // }
  67. // return nil, nil
  68. // }
  69. // func DownLoadPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  70. // _planId := c.Query("id")
  71. // compId := c.Query("compId")
  72. // planId, err := primitive.ObjectIDFromHex(_planId)
  73. // if err != nil {
  74. // return nil, errors.New("planId错误")
  75. // }
  76. // plan := model.ProductPlan{}
  77. // found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  78. // CollectName: repo.CollectionProductPlan,
  79. // Query: repo.Map{"_id": planId},
  80. // }, &plan)
  81. // if !found || err != nil {
  82. // return nil, errors.New("数据未找到")
  83. // }
  84. // // 获取部件单据
  85. // curComp := &model.PackComponent{}
  86. // for _, comp := range plan.Pack.Components {
  87. // if comp.Id == compId {
  88. // curComp = comp
  89. // }
  90. // }
  91. // if curComp.Id == "" {
  92. // return nil, errors.New("该组件不存在")
  93. // }
  94. // // 获取bill
  95. // if len(curComp.Stages) == 0 {
  96. // return nil, errors.New("该组件数据不存在")
  97. // }
  98. // f := excelize.NewFile()
  99. // // Create a new sheet.
  100. // index := f.NewSheet("Sheet1")
  101. // f.SetActiveSheet(index)
  102. // f.SetDefaultFont("宋体")
  103. // companyName := getCompanyName(apictx)
  104. // offset := 0
  105. // for _, mat := range curComp.Stages {
  106. // // 采购单
  107. // _purchaseId := mat.BillId
  108. // purchaseId, err := primitive.ObjectIDFromHex(_purchaseId)
  109. // if err == nil {
  110. // purchase := model.PurchaseBill{}
  111. // found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  112. // CollectName: repo.CollectionBillPurchase,
  113. // Query: repo.Map{"_id": purchaseId},
  114. // }, &purchase)
  115. // fmt.Println(purchase)
  116. // if found {
  117. // var billExcel IPurchBill
  118. // if purchase.Process != nil {
  119. // billExcel = NewProcessBill(f)
  120. // }
  121. // if len(purchase.Paper) > 0 {
  122. // billExcel = NewPurchaseBill(f)
  123. // }
  124. // // purchaseExcel := NewPurchaseBill(f)
  125. // billExcel.SetContent(&purchase)
  126. // billExcel.SetTitle(fmt.Sprintf("%s原材料采购单", companyName))
  127. // billExcel.SetOffset(offset)
  128. // // billExcel.Title = fmt.Sprintf("%s原材料采购单", companyName)
  129. // //设置对应的数据
  130. // // purchaseExcel.Offset = offset
  131. // billExcel.Draws()
  132. // offset += 15
  133. // }
  134. // }
  135. // // if len(mat.Crafts) > 0 {
  136. // // for _, carft := range mat.Crafts {
  137. // // // 加工单
  138. // // _produceId := carft.BillId
  139. // // produceId, err := primitive.ObjectIDFromHex(_produceId)
  140. // // if err == nil {
  141. // // produce := model.ProduceBill{}
  142. // // found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  143. // // CollectName: repo.CollectionBillProduce,
  144. // // Query: repo.Map{"_id": produceId},
  145. // // }, &produce)
  146. // // if found {
  147. // // produceExcel := NewProduceBill(f)
  148. // // produceExcel.Content = &produce
  149. // // produceExcel.Title = fmt.Sprintf("%s加工单", companyName)
  150. // // //设置对应的数据
  151. // // produceExcel.Offset = offset
  152. // // produceExcel.Draws()
  153. // // offset += 15
  154. // // }
  155. // // }
  156. // // }
  157. // // }
  158. // }
  159. // c.Header("Content-Type", "application/octet-stream")
  160. // c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  161. // c.Header("Content-Transfer-Encoding", "binary")
  162. // err = f.Write(c.Writer)
  163. // if err != nil {
  164. // return nil, err
  165. // }
  166. // return nil, nil
  167. // }
  168. // 创建生产计划
  169. func CreateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  170. var plan model.ProductPlan
  171. err := c.ShouldBindJSON(&plan)
  172. if err != nil {
  173. fmt.Println(err)
  174. return nil, errors.New("参数错误!")
  175. }
  176. ctx := apictx.CreateRepoCtx()
  177. if plan.Name == "" {
  178. return nil, errors.New("生产计划名为空")
  179. }
  180. if plan.Total == 0 {
  181. return nil, errors.New("生产计划数应不为0")
  182. }
  183. plan.Status = "process" // 进行中
  184. plan.CreateTime = time.Now()
  185. plan.UpdateTime = time.Now()
  186. result, err := repo.RepoAddDoc(ctx, repo.CollectionProductPlan, &plan)
  187. return result, err
  188. }
  189. // 获取生产计划信息
  190. func GetProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  191. planId := c.Param("id")
  192. id, err := primitive.ObjectIDFromHex(planId)
  193. if err != nil {
  194. return nil, errors.New("非法id")
  195. }
  196. var plan model.ProductPlan
  197. option := &repo.DocSearchOptions{
  198. CollectName: repo.CollectionProductPlan,
  199. Query: repo.Map{"_id": id},
  200. }
  201. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &plan)
  202. if !found || err != nil {
  203. log.Info(err)
  204. return nil, errors.New("数据未找到")
  205. }
  206. billStates := map[string]string{}
  207. if plan.Pack != nil && plan.Pack.Components != nil {
  208. for _, comp := range plan.Pack.Components {
  209. if comp.Stages != nil {
  210. for _, stage := range comp.Stages {
  211. if len(stage.BillId) > 0 {
  212. collectName := ""
  213. // 材料
  214. if stage.Type == 1 {
  215. collectName = repo.CollectionBillPurchase
  216. }
  217. // 工艺
  218. if stage.Type == 2 {
  219. collectName = repo.CollectionBillProduce
  220. }
  221. // 成品
  222. if stage.Type == 3 {
  223. collectName = repo.CollectionBillProduct
  224. }
  225. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: collectName, Query: repo.Map{"_id": stage.BillId}, Project: []string{"status"}})
  226. if ok {
  227. billStates[stage.BillId] = state["status"].(string)
  228. }
  229. }
  230. }
  231. }
  232. }
  233. }
  234. return map[string]interface{}{
  235. "plan": plan,
  236. "billStates": billStates,
  237. }, nil
  238. }
  239. // 获取生产计划列表
  240. func GetProductPlans(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  241. page, size, query := UtilQueryPageSize(c)
  242. if _packId, ok := query["packId"]; ok {
  243. packId, _ := primitive.ObjectIDFromHex(_packId.(string))
  244. query["pack._id"] = packId
  245. delete(query, "packId")
  246. }
  247. option := &repo.PageSearchOptions{
  248. CollectName: repo.CollectionProductPlan,
  249. Query: query,
  250. Page: page,
  251. Size: size,
  252. Sort: bson.M{"createTime": -1},
  253. Project: []string{"_id", "thumbnail", "name", "updateTime", "createTime", "createUser", "total", "totalPrice", "status"},
  254. }
  255. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  256. }
  257. // 更新生产计划
  258. func UpdateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  259. var plan model.ProductPlan
  260. err := c.ShouldBindJSON(&plan)
  261. if err != nil {
  262. return nil, errors.New("参数错误")
  263. }
  264. if plan.Id.Hex() == "" {
  265. return nil, errors.New("id的为空")
  266. }
  267. plan.UpdateTime = time.Now()
  268. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, plan.Id.Hex(), &plan)
  269. }
  270. // 删除生产计划
  271. func DelProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  272. planId := c.Param("id")
  273. if planId == "" {
  274. return nil, errors.New("id为空")
  275. }
  276. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, planId)
  277. }