plan.go 8.3 KB

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