plan.go 8.1 KB

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