plan.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. // index := f.NewSheet("Sheet1")
  100. // f.SetActiveSheet(index)
  101. // f.SetDefaultFont("宋体")
  102. // companyName := getCompanyName(apictx)
  103. // offset := 0
  104. // for _, stage := range curComp.Stages {
  105. // _billId := stage.BillId
  106. // billId, _ := primitive.ObjectIDFromHex(_billId)
  107. // if !billId.IsZero(){
  108. // // 采购单
  109. // if stage.Type == 1
  110. // }
  111. // if err == nil {
  112. // purchase := model.PurchaseBill{}
  113. // found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  114. // CollectName: repo.CollectionBillPurchase,
  115. // Query: repo.Map{"_id": purchaseId},
  116. // }, &purchase)
  117. // fmt.Println(purchase)
  118. // if found {
  119. // var billExcel IPurchBill
  120. // if purchase.Process != nil {
  121. // billExcel = NewProcessBill(f)
  122. // }
  123. // if len(purchase.Paper) > 0 {
  124. // billExcel = NewPurchaseBill(f)
  125. // }
  126. // // purchaseExcel := NewPurchaseBill(f)
  127. // billExcel.SetContent(&purchase)
  128. // billExcel.SetTitle(fmt.Sprintf("%s原材料采购单", companyName))
  129. // billExcel.SetOffset(offset)
  130. // // billExcel.Title = fmt.Sprintf("%s原材料采购单", companyName)
  131. // //设置对应的数据
  132. // // purchaseExcel.Offset = offset
  133. // billExcel.Draws()
  134. // offset += 15
  135. // }
  136. // }
  137. // // if len(mat.Crafts) > 0 {
  138. // // for _, carft := range mat.Crafts {
  139. // // // 加工单
  140. // // _produceId := carft.BillId
  141. // // produceId, err := primitive.ObjectIDFromHex(_produceId)
  142. // // if err == nil {
  143. // // produce := model.ProduceBill{}
  144. // // found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  145. // // CollectName: repo.CollectionBillProduce,
  146. // // Query: repo.Map{"_id": produceId},
  147. // // }, &produce)
  148. // // if found {
  149. // // produceExcel := NewProduceBill(f)
  150. // // produceExcel.Content = &produce
  151. // // produceExcel.Title = fmt.Sprintf("%s加工单", companyName)
  152. // // //设置对应的数据
  153. // // produceExcel.Offset = offset
  154. // // produceExcel.Draws()
  155. // // offset += 15
  156. // // }
  157. // // }
  158. // // }
  159. // // }
  160. // }
  161. // c.Header("Content-Type", "application/octet-stream")
  162. // c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  163. // c.Header("Content-Transfer-Encoding", "binary")
  164. // err = f.Write(c.Writer)
  165. // if err != nil {
  166. // return nil, err
  167. // }
  168. // return nil, nil
  169. // }
  170. // 创建生产计划
  171. func CreateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  172. var plan model.ProductPlan
  173. err := c.ShouldBindJSON(&plan)
  174. if err != nil {
  175. fmt.Println(err)
  176. return nil, errors.New("参数错误!")
  177. }
  178. ctx := apictx.CreateRepoCtx()
  179. if plan.Name == "" {
  180. return nil, errors.New("生产计划名为空")
  181. }
  182. if plan.Total == 0 {
  183. return nil, errors.New("生产计划数应不为0")
  184. }
  185. plan.Status = "process" // 进行中
  186. plan.CreateTime = time.Now()
  187. plan.UpdateTime = time.Now()
  188. result, err := repo.RepoAddDoc(ctx, repo.CollectionProductPlan, &plan)
  189. return result, err
  190. }
  191. // 获取生产计划信息
  192. func GetProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  193. planId := c.Param("id")
  194. id, err := primitive.ObjectIDFromHex(planId)
  195. if err != nil {
  196. return nil, errors.New("非法id")
  197. }
  198. var plan model.ProductPlan
  199. option := &repo.DocSearchOptions{
  200. CollectName: repo.CollectionProductPlan,
  201. Query: repo.Map{"_id": id},
  202. }
  203. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &plan)
  204. if !found || err != nil {
  205. log.Info(err)
  206. return nil, errors.New("数据未找到")
  207. }
  208. billStates := map[string]string{}
  209. if plan.Pack != nil && plan.Pack.Components != nil {
  210. for _, comp := range plan.Pack.Components {
  211. if comp.Stages != nil {
  212. for _, stage := range comp.Stages {
  213. if len(stage.BillId) > 0 {
  214. collectName := ""
  215. // 材料
  216. if stage.Type == 1 {
  217. collectName = repo.CollectionBillPurchase
  218. }
  219. // 工艺
  220. if stage.Type == 2 {
  221. collectName = repo.CollectionBillProduce
  222. }
  223. // 成品
  224. if stage.Type == 3 {
  225. collectName = repo.CollectionBillProduct
  226. }
  227. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: collectName, Query: repo.Map{"_id": stage.BillId}, Project: []string{"status"}})
  228. if ok {
  229. billStates[stage.BillId] = state["status"].(string)
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }
  236. return map[string]interface{}{
  237. "plan": plan,
  238. "billStates": billStates,
  239. }, nil
  240. }
  241. // 获取生产计划列表
  242. func GetProductPlans(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  243. page, size, query := UtilQueryPageSize(c)
  244. if _packId, ok := query["packId"]; ok {
  245. packId, _ := primitive.ObjectIDFromHex(_packId.(string))
  246. query["pack._id"] = packId
  247. delete(query, "packId")
  248. }
  249. option := &repo.PageSearchOptions{
  250. CollectName: repo.CollectionProductPlan,
  251. Query: query,
  252. Page: page,
  253. Size: size,
  254. Sort: bson.M{"createTime": -1},
  255. Project: []string{"_id", "thumbnail", "name", "updateTime", "createTime", "createUser", "total", "totalPrice", "status"},
  256. }
  257. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  258. }
  259. // 更新生产计划
  260. func UpdateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  261. var plan model.ProductPlan
  262. err := c.ShouldBindJSON(&plan)
  263. if err != nil {
  264. return nil, errors.New("参数错误")
  265. }
  266. if plan.Id.Hex() == "" {
  267. return nil, errors.New("id的为空")
  268. }
  269. plan.UpdateTime = time.Now()
  270. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, plan.Id.Hex(), &plan)
  271. }
  272. // 删除生产计划
  273. func DelProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  274. planId := c.Param("id")
  275. if planId == "" {
  276. return nil, errors.New("id为空")
  277. }
  278. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, planId)
  279. }