plan.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. func DownLoadPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  30. _packId := c.Query("id")
  31. compId := c.Query("compId")
  32. packId, err := primitive.ObjectIDFromHex(_packId)
  33. if err != nil {
  34. return nil, errors.New("packId错误")
  35. }
  36. plan := model.ProductPlan{}
  37. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  38. CollectName: repo.CollectionProductPlan,
  39. Query: repo.Map{"pack._id": packId},
  40. }, &plan)
  41. if !found || err != nil {
  42. return nil, errors.New("数据未找到")
  43. }
  44. // 获取部件单据
  45. curComp := &model.PackComponent{}
  46. for _, comp := range plan.Pack.Components {
  47. if comp.Id == compId {
  48. curComp = comp
  49. }
  50. }
  51. if curComp.Id == "" {
  52. return nil, errors.New("该组件不存在")
  53. }
  54. // 获取bill
  55. if len(curComp.Mats) == 0 {
  56. return nil, errors.New("该组件数据不存在")
  57. }
  58. f := excelize.NewFile()
  59. // Create a new sheet.
  60. index := f.NewSheet("Sheet1")
  61. f.SetActiveSheet(index)
  62. f.SetDefaultFont("宋体")
  63. //
  64. for _, mat := range curComp.Mats {
  65. // 采购单
  66. _purchaseId := mat.BillId
  67. purchaseId, err := primitive.ObjectIDFromHex(_purchaseId)
  68. if err == nil {
  69. purchase := model.PurchaseBill{}
  70. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  71. CollectName: repo.CollectionBillPurchase,
  72. Query: repo.Map{"_id": purchaseId},
  73. }, &purchase)
  74. if found {
  75. purchaseExcel := NewPurchaseBill(f)
  76. purchaseExcel.Content = &purchase
  77. //设置对应的数据
  78. purchaseExcel.Draws()
  79. purchaseExcel.Offset += 15
  80. }
  81. }
  82. if len(mat.Crafts) > 0 {
  83. for _, carft := range mat.Crafts {
  84. // 加工单
  85. _produceId := carft.BillId
  86. produceId, err := primitive.ObjectIDFromHex(_produceId)
  87. if err == nil {
  88. produce := model.ProduceBill{}
  89. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  90. CollectName: repo.CollectionBillProduce,
  91. Query: repo.Map{"_id": produceId},
  92. }, &produce)
  93. if found {
  94. produceExcel := NewProduceBill(f)
  95. produceExcel.Content = &produce
  96. //设置对应的数据
  97. produceExcel.Draws()
  98. produceExcel.Offset += 15
  99. }
  100. }
  101. }
  102. }
  103. }
  104. c.Header("Content-Type", "application/octet-stream")
  105. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  106. c.Header("Content-Transfer-Encoding", "binary")
  107. err = f.Write(c.Writer)
  108. if err != nil {
  109. return nil, err
  110. }
  111. return nil, nil
  112. }
  113. // 创建生产计划
  114. func CreateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  115. var plan model.ProductPlan
  116. err := c.ShouldBindJSON(&plan)
  117. if err != nil {
  118. fmt.Println(err)
  119. return nil, errors.New("参数错误!")
  120. }
  121. ctx := apictx.CreateRepoCtx()
  122. if plan.Name == "" {
  123. return nil, errors.New("生产计划名为空")
  124. }
  125. if plan.Total == 0 {
  126. return nil, errors.New("生产计划数应不为0")
  127. }
  128. plan.Status = "process" // 进行中
  129. plan.CreateTime = time.Now()
  130. plan.UpdateTime = time.Now()
  131. result, err := repo.RepoAddDoc(ctx, repo.CollectionProductPlan, &plan)
  132. return result, err
  133. }
  134. // 获取生产计划信息
  135. func GetProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  136. planId := c.Param("id")
  137. id, err := primitive.ObjectIDFromHex(planId)
  138. if err != nil {
  139. return nil, errors.New("非法id")
  140. }
  141. var plan model.ProductPlan
  142. option := &repo.DocSearchOptions{
  143. CollectName: repo.CollectionProductPlan,
  144. Query: repo.Map{"_id": id},
  145. }
  146. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &plan)
  147. if !found || err != nil {
  148. log.Info(err)
  149. return nil, errors.New("数据未找到")
  150. }
  151. billStates := map[string]string{}
  152. if plan.Pack != nil && plan.Pack.Components != nil {
  153. for _, comp := range plan.Pack.Components {
  154. if comp.Mats != nil {
  155. for _, mat := range comp.Mats {
  156. if len(mat.BillId) > 0 {
  157. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionBillPurchase, Query: repo.Map{"_id": mat.BillId}, Project: []string{"status"}})
  158. if ok {
  159. billStates[mat.BillId] = state["status"].(string)
  160. }
  161. }
  162. if mat.Crafts != nil {
  163. for _, craft := range mat.Crafts {
  164. if len(craft.BillId) > 0 {
  165. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionBillProduce, Query: repo.Map{"_id": craft.BillId}, Project: []string{"status"}})
  166. if ok {
  167. billStates[craft.BillId] = state["status"].(string)
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }
  176. return map[string]interface{}{
  177. "plan": plan,
  178. "billStates": billStates,
  179. }, nil
  180. }
  181. // 获取生产计划列表
  182. func GetProductPlans(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  183. page, size, query := UtilQueryPageSize(c)
  184. option := &repo.PageSearchOptions{
  185. CollectName: repo.CollectionProductPlan,
  186. Query: query,
  187. Page: page,
  188. Size: size,
  189. Sort: bson.M{"createTime": -1},
  190. Project: []string{"_id", "thumbnail", "name", "updateTime", "createUser", "total", "totalPrice", "status"},
  191. }
  192. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  193. }
  194. // 更新生产计划
  195. func UpdateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  196. var plan model.ProductPlan
  197. err := c.ShouldBindJSON(&plan)
  198. if err != nil {
  199. return nil, errors.New("参数错误")
  200. }
  201. if plan.Id.Hex() == "" {
  202. return nil, errors.New("id的为空")
  203. }
  204. plan.UpdateTime = time.Now()
  205. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, plan.Id.Hex(), &plan)
  206. }
  207. // 删除生产计划
  208. func DelProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  209. planId := c.Param("id")
  210. if planId == "" {
  211. return nil, errors.New("id为空")
  212. }
  213. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, planId)
  214. }