plan.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. // // 另存为
  62. // // _ = os.MkdirAll("excel", os.ModePerm)
  63. // // filename1 := time.Now().Format("20060102150405") + ".xlsx"
  64. // filename1 := "生产成本表.xlsx"
  65. // err = f.SaveAs(filename1)
  66. // if err != nil {
  67. // return nil, err
  68. // }
  69. c.Header("Content-Type", "application/octet-stream")
  70. c.Header("Content-Disposition", "attachment; filename="+"planCost.xlsx")
  71. c.Header("Content-Transfer-Encoding", "binary")
  72. err = f.Write(c.Writer)
  73. if err != nil {
  74. return nil, err
  75. }
  76. return nil, nil
  77. }
  78. func DownLoadPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  79. _planId := c.Query("id")
  80. compId := c.Query("compId")
  81. planId, err := primitive.ObjectIDFromHex(_planId)
  82. if err != nil {
  83. return nil, errors.New("planId错误")
  84. }
  85. plan := model.ProductPlan{}
  86. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  87. CollectName: repo.CollectionProductPlan,
  88. Query: repo.Map{"_id": planId},
  89. }, &plan)
  90. if !found || err != nil {
  91. return nil, errors.New("数据未找到")
  92. }
  93. // 获取部件单据
  94. curComp := &model.PackComponent{}
  95. for _, comp := range plan.Pack.Components {
  96. if comp.Id == compId {
  97. curComp = comp
  98. }
  99. }
  100. if curComp.Id == "" {
  101. return nil, errors.New("该组件不存在")
  102. }
  103. // 获取bill
  104. if len(curComp.Mats) == 0 {
  105. return nil, errors.New("该组件数据不存在")
  106. }
  107. f := excelize.NewFile()
  108. // Create a new sheet.
  109. index := f.NewSheet("Sheet1")
  110. f.SetActiveSheet(index)
  111. f.SetDefaultFont("宋体")
  112. info := model.Setting{}
  113. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  114. CollectName: "infos",
  115. }, &info)
  116. offset := 0
  117. for _, mat := range curComp.Mats {
  118. // 采购单
  119. _purchaseId := mat.BillId
  120. purchaseId, err := primitive.ObjectIDFromHex(_purchaseId)
  121. if err == nil {
  122. purchase := model.PurchaseBill{}
  123. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  124. CollectName: repo.CollectionBillPurchase,
  125. Query: repo.Map{"_id": purchaseId},
  126. }, &purchase)
  127. if found {
  128. purchaseExcel := NewPurchaseBill(f)
  129. purchaseExcel.Content = &purchase
  130. purchaseExcel.Title = fmt.Sprintf("%s原材料采购单", info.CompanyName)
  131. //设置对应的数据
  132. purchaseExcel.Offset = offset
  133. purchaseExcel.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加工单", info.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.Mats != nil {
  212. for _, mat := range comp.Mats {
  213. if len(mat.BillId) > 0 {
  214. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionBillPurchase, Query: repo.Map{"_id": mat.BillId}, Project: []string{"status"}})
  215. if ok {
  216. billStates[mat.BillId] = state["status"].(string)
  217. }
  218. }
  219. if mat.Crafts != nil {
  220. for _, craft := range mat.Crafts {
  221. if len(craft.BillId) > 0 {
  222. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionBillProduce, Query: repo.Map{"_id": craft.BillId}, Project: []string{"status"}})
  223. if ok {
  224. billStates[craft.BillId] = state["status"].(string)
  225. }
  226. }
  227. }
  228. }
  229. }
  230. }
  231. }
  232. }
  233. return map[string]interface{}{
  234. "plan": plan,
  235. "billStates": billStates,
  236. }, nil
  237. }
  238. // 获取生产计划列表
  239. func GetProductPlans(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  240. page, size, query := UtilQueryPageSize(c)
  241. option := &repo.PageSearchOptions{
  242. CollectName: repo.CollectionProductPlan,
  243. Query: query,
  244. Page: page,
  245. Size: size,
  246. Sort: bson.M{"createTime": -1},
  247. Project: []string{"_id", "thumbnail", "name", "updateTime", "createUser", "total", "totalPrice", "status"},
  248. }
  249. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  250. }
  251. // 更新生产计划
  252. func UpdateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  253. var plan model.ProductPlan
  254. err := c.ShouldBindJSON(&plan)
  255. if err != nil {
  256. return nil, errors.New("参数错误")
  257. }
  258. if plan.Id.Hex() == "" {
  259. return nil, errors.New("id的为空")
  260. }
  261. plan.UpdateTime = time.Now()
  262. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, plan.Id.Hex(), &plan)
  263. }
  264. // 删除生产计划
  265. func DelProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  266. planId := c.Param("id")
  267. if planId == "" {
  268. return nil, errors.New("id为空")
  269. }
  270. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, planId)
  271. }