plan.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. r.GET("/plan/cost/download", DownLoadPlanCost)
  30. }
  31. func DownLoadPlanCost(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  32. _planId := c.Query("id")
  33. planId, err := primitive.ObjectIDFromHex(_planId)
  34. if err != nil {
  35. return nil, errors.New("packId错误")
  36. }
  37. plan := model.ProductPlan{}
  38. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  39. CollectName: repo.CollectionProductPlan,
  40. Query: repo.Map{"_id": planId},
  41. }, &plan)
  42. if !found || err != nil {
  43. return nil, errors.New("数据未找到")
  44. }
  45. f := excelize.NewFile()
  46. // Create a new sheet.
  47. index := f.NewSheet("Sheet1")
  48. f.SetActiveSheet(index)
  49. f.SetDefaultFont("宋体")
  50. planCostExcel := NewPlanCostExcel(f)
  51. planCostExcel.Content = &plan
  52. planCostExcel.Title = fmt.Sprintf("生产成本表(%s)", "梦华月色8600盒")
  53. // //设置对应的数据
  54. // produceExcel.Offset = offset
  55. planCostExcel.Draws()
  56. // info := model.Setting{}
  57. // repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  58. // CollectName: "infos",
  59. // }, &info)
  60. // offset := 0
  61. // for _, mat := range curComp.Mats {
  62. // // 采购单
  63. // _purchaseId := mat.BillId
  64. // purchaseId, err := primitive.ObjectIDFromHex(_purchaseId)
  65. // if err == nil {
  66. // purchase := model.PurchaseBill{}
  67. // found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  68. // CollectName: repo.CollectionBillPurchase,
  69. // Query: repo.Map{"_id": purchaseId},
  70. // }, &purchase)
  71. // if found {
  72. // purchaseExcel := NewPurchaseBill(f)
  73. // purchaseExcel.Content = &purchase
  74. // purchaseExcel.Title = fmt.Sprintf("%s原材料采购单", info.CompanyName)
  75. // //设置对应的数据
  76. // purchaseExcel.Offset = offset
  77. // purchaseExcel.Draws()
  78. // offset += 15
  79. // }
  80. // }
  81. // if len(mat.Crafts) > 0 {
  82. // for _, carft := range mat.Crafts {
  83. // // 加工单
  84. // _produceId := carft.BillId
  85. // produceId, err := primitive.ObjectIDFromHex(_produceId)
  86. // if err == nil {
  87. // produce := model.ProduceBill{}
  88. // found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  89. // CollectName: repo.CollectionBillProduce,
  90. // Query: repo.Map{"_id": produceId},
  91. // }, &produce)
  92. // if found {
  93. // produceExcel := NewProduceBill(f)
  94. // produceExcel.Content = &produce
  95. // produceExcel.Title = fmt.Sprintf("%s加工单", info.CompanyName)
  96. // //设置对应的数据
  97. // produceExcel.Offset = offset
  98. // produceExcel.Draws()
  99. // offset += 15
  100. // }
  101. // }
  102. // }
  103. // }
  104. // }
  105. // 另存为
  106. // _ = os.MkdirAll("excel", os.ModePerm)
  107. // filename1 := time.Now().Format("20060102150405") + ".xlsx"
  108. filename1 := "生产成本表.xlsx"
  109. err = f.SaveAs(filename1)
  110. if err != nil {
  111. return nil, err
  112. }
  113. // c.Header("Content-Type", "application/octet-stream")
  114. // c.Header("Content-Disposition", "attachment; filename="+"planCost.xlsx")
  115. // c.Header("Content-Transfer-Encoding", "binary")
  116. // err = f.Write(c.Writer)
  117. // if err != nil {
  118. // return nil, err
  119. // }
  120. return plan, nil
  121. return nil, nil
  122. }
  123. func DownLoadPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  124. _planId := c.Query("id")
  125. compId := c.Query("compId")
  126. planId, err := primitive.ObjectIDFromHex(_planId)
  127. if err != nil {
  128. return nil, errors.New("packId错误")
  129. }
  130. plan := model.ProductPlan{}
  131. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  132. CollectName: repo.CollectionProductPlan,
  133. Query: repo.Map{"_id": planId},
  134. }, &plan)
  135. if !found || err != nil {
  136. return nil, errors.New("数据未找到")
  137. }
  138. // 获取部件单据
  139. curComp := &model.PackComponent{}
  140. for _, comp := range plan.Pack.Components {
  141. if comp.Id == compId {
  142. curComp = comp
  143. }
  144. }
  145. if curComp.Id == "" {
  146. return nil, errors.New("该组件不存在")
  147. }
  148. // 获取bill
  149. if len(curComp.Mats) == 0 {
  150. return nil, errors.New("该组件数据不存在")
  151. }
  152. f := excelize.NewFile()
  153. // Create a new sheet.
  154. index := f.NewSheet("Sheet1")
  155. f.SetActiveSheet(index)
  156. f.SetDefaultFont("宋体")
  157. info := model.Setting{}
  158. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  159. CollectName: "infos",
  160. }, &info)
  161. offset := 0
  162. for _, mat := range curComp.Mats {
  163. // 采购单
  164. _purchaseId := mat.BillId
  165. purchaseId, err := primitive.ObjectIDFromHex(_purchaseId)
  166. if err == nil {
  167. purchase := model.PurchaseBill{}
  168. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  169. CollectName: repo.CollectionBillPurchase,
  170. Query: repo.Map{"_id": purchaseId},
  171. }, &purchase)
  172. if found {
  173. purchaseExcel := NewPurchaseBill(f)
  174. purchaseExcel.Content = &purchase
  175. purchaseExcel.Title = fmt.Sprintf("%s原材料采购单", info.CompanyName)
  176. //设置对应的数据
  177. purchaseExcel.Offset = offset
  178. purchaseExcel.Draws()
  179. offset += 15
  180. }
  181. }
  182. if len(mat.Crafts) > 0 {
  183. for _, carft := range mat.Crafts {
  184. // 加工单
  185. _produceId := carft.BillId
  186. produceId, err := primitive.ObjectIDFromHex(_produceId)
  187. if err == nil {
  188. produce := model.ProduceBill{}
  189. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  190. CollectName: repo.CollectionBillProduce,
  191. Query: repo.Map{"_id": produceId},
  192. }, &produce)
  193. if found {
  194. produceExcel := NewProduceBill(f)
  195. produceExcel.Content = &produce
  196. produceExcel.Title = fmt.Sprintf("%s加工单", info.CompanyName)
  197. //设置对应的数据
  198. produceExcel.Offset = offset
  199. produceExcel.Draws()
  200. offset += 15
  201. }
  202. }
  203. }
  204. }
  205. }
  206. c.Header("Content-Type", "application/octet-stream")
  207. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  208. c.Header("Content-Transfer-Encoding", "binary")
  209. err = f.Write(c.Writer)
  210. if err != nil {
  211. return nil, err
  212. }
  213. return nil, nil
  214. }
  215. // 创建生产计划
  216. func CreateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  217. var plan model.ProductPlan
  218. err := c.ShouldBindJSON(&plan)
  219. if err != nil {
  220. fmt.Println(err)
  221. return nil, errors.New("参数错误!")
  222. }
  223. ctx := apictx.CreateRepoCtx()
  224. if plan.Name == "" {
  225. return nil, errors.New("生产计划名为空")
  226. }
  227. if plan.Total == 0 {
  228. return nil, errors.New("生产计划数应不为0")
  229. }
  230. plan.Status = "process" // 进行中
  231. plan.CreateTime = time.Now()
  232. plan.UpdateTime = time.Now()
  233. result, err := repo.RepoAddDoc(ctx, repo.CollectionProductPlan, &plan)
  234. return result, err
  235. }
  236. // 获取生产计划信息
  237. func GetProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  238. planId := c.Param("id")
  239. id, err := primitive.ObjectIDFromHex(planId)
  240. if err != nil {
  241. return nil, errors.New("非法id")
  242. }
  243. var plan model.ProductPlan
  244. option := &repo.DocSearchOptions{
  245. CollectName: repo.CollectionProductPlan,
  246. Query: repo.Map{"_id": id},
  247. }
  248. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &plan)
  249. if !found || err != nil {
  250. log.Info(err)
  251. return nil, errors.New("数据未找到")
  252. }
  253. billStates := map[string]string{}
  254. if plan.Pack != nil && plan.Pack.Components != nil {
  255. for _, comp := range plan.Pack.Components {
  256. if comp.Mats != nil {
  257. for _, mat := range comp.Mats {
  258. if len(mat.BillId) > 0 {
  259. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionBillPurchase, Query: repo.Map{"_id": mat.BillId}, Project: []string{"status"}})
  260. if ok {
  261. billStates[mat.BillId] = state["status"].(string)
  262. }
  263. }
  264. if mat.Crafts != nil {
  265. for _, craft := range mat.Crafts {
  266. if len(craft.BillId) > 0 {
  267. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionBillProduce, Query: repo.Map{"_id": craft.BillId}, Project: []string{"status"}})
  268. if ok {
  269. billStates[craft.BillId] = state["status"].(string)
  270. }
  271. }
  272. }
  273. }
  274. }
  275. }
  276. }
  277. }
  278. return map[string]interface{}{
  279. "plan": plan,
  280. "billStates": billStates,
  281. }, nil
  282. }
  283. // 获取生产计划列表
  284. func GetProductPlans(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  285. page, size, query := UtilQueryPageSize(c)
  286. option := &repo.PageSearchOptions{
  287. CollectName: repo.CollectionProductPlan,
  288. Query: query,
  289. Page: page,
  290. Size: size,
  291. Sort: bson.M{"createTime": -1},
  292. Project: []string{"_id", "thumbnail", "name", "updateTime", "createUser", "total", "totalPrice", "status"},
  293. }
  294. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  295. }
  296. // 更新生产计划
  297. func UpdateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  298. var plan model.ProductPlan
  299. err := c.ShouldBindJSON(&plan)
  300. if err != nil {
  301. return nil, errors.New("参数错误")
  302. }
  303. if plan.Id.Hex() == "" {
  304. return nil, errors.New("id的为空")
  305. }
  306. plan.UpdateTime = time.Now()
  307. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, plan.Id.Hex(), &plan)
  308. }
  309. // 删除生产计划
  310. func DelProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  311. planId := c.Param("id")
  312. if planId == "" {
  313. return nil, errors.New("id为空")
  314. }
  315. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, planId)
  316. }