plan.go 9.0 KB

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