plan.go 8.1 KB

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