plan.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. "box-cost/log"
  6. "errors"
  7. "fmt"
  8. "strings"
  9. "time"
  10. "github.com/gin-gonic/gin"
  11. "github.com/xuri/excelize/v2"
  12. "go.mongodb.org/mongo-driver/bson"
  13. "go.mongodb.org/mongo-driver/bson/primitive"
  14. )
  15. // 生产计划管理
  16. func ProductPlan(r *GinRouter) {
  17. // 创建生产计划
  18. r.POST("/plan/create", CreateProductPlan)
  19. // 获取生产计划详情
  20. r.GET("/plan/detail/:id", GetProductPlan)
  21. // 获取生产计划列表
  22. r.GET("/plan/list", GetProductPlans)
  23. // 更新生产计划
  24. r.POST("/plan/update", UpdateProductPlan)
  25. // 删除生产计划
  26. r.POST("/plan/delete/:id", DelProductPlan)
  27. // 下载部件单据
  28. // r.GET("/bill/plan/download", DownLoadCompBills)
  29. r.GET("/bill/plan/download", DownLoadPlanBills)
  30. // 生产成本表
  31. r.GET("/plan/cost/download", DownLoadPlanCost)
  32. }
  33. type SupplierPlanCost struct {
  34. *model.ProductPlan
  35. SupplierId string
  36. }
  37. func DownLoadPlanCost(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  38. _planId := c.Query("id")
  39. supplierId := c.Query("supplierId")
  40. planId, _ := primitive.ObjectIDFromHex(_planId)
  41. if planId.IsZero() {
  42. return nil, errors.New("planId错误")
  43. }
  44. supplierPlanCost := &SupplierPlanCost{}
  45. plan := model.ProductPlan{}
  46. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  47. CollectName: repo.CollectionProductPlan,
  48. Query: repo.Map{"_id": planId},
  49. }, &plan)
  50. if !found || err != nil {
  51. return nil, errors.New("数据未找到")
  52. }
  53. supplierPlanCost.ProductPlan = &plan
  54. supplierPlanCost.SupplierId = supplierId
  55. f := excelize.NewFile()
  56. index := f.NewSheet("Sheet1")
  57. f.SetActiveSheet(index)
  58. f.SetDefaultFont("宋体")
  59. planCostExcel := NewPlanCostExcel(f)
  60. planCostExcel.Title = fmt.Sprintf("生产成本表(%s)%d盒", plan.Name, plan.Total)
  61. planCostExcel.Content = supplierPlanCost
  62. planCostExcel.Draws()
  63. c.Header("Content-Type", "application/octet-stream")
  64. c.Header("Content-Disposition", "attachment; filename="+"planCost.xlsx")
  65. c.Header("Content-Transfer-Encoding", "binary")
  66. err = f.Write(c.Writer)
  67. if err != nil {
  68. return nil, err
  69. }
  70. return nil, nil
  71. }
  72. func DownLoadPlanBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  73. _planId := c.Query("id")
  74. planId, err := primitive.ObjectIDFromHex(_planId)
  75. if err != nil {
  76. return nil, errors.New("planId错误")
  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. // 获取所有stages单据id
  87. billIds := make([]string, 0)
  88. curComp := &model.PackComponent{}
  89. for _, comp := range plan.Pack.Components {
  90. if comp.Id == "" || len(curComp.Stages) == 0 {
  91. continue
  92. }
  93. for _, stage := range comp.Stages {
  94. billId, _ := primitive.ObjectIDFromHex(stage.BillId)
  95. if !billId.IsZero() {
  96. billIds = append(billIds, fmt.Sprintf("%d_%s", stage.BillType, stage.BillId))
  97. }
  98. }
  99. }
  100. // 去重单据号
  101. typeBillIds := removeDuplicationSort(billIds)
  102. if len(typeBillIds) < 1 {
  103. return nil, errors.New("未找到单据信息")
  104. }
  105. f := excelize.NewFile()
  106. index := f.NewSheet("Sheet1")
  107. f.SetActiveSheet(index)
  108. f.SetDefaultFont("宋体")
  109. companyName := getCompanyName(apictx)
  110. row := 0
  111. for _, tId := range typeBillIds {
  112. tidArr := strings.Split(tId, "_")
  113. var billExcel IExcel
  114. // 采购
  115. billId, _ := primitive.ObjectIDFromHex(tidArr[1])
  116. if tidArr[0] == "1" {
  117. purchase := model.PurchaseBill{}
  118. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  119. CollectName: repo.CollectionBillPurchase,
  120. Query: repo.Map{"_id": billId},
  121. }, &purchase)
  122. if found {
  123. billExcel = NewPurchaseBill(f)
  124. if purchase.Reviewed == 1 {
  125. if len(purchase.SignUsers) > 0 {
  126. signs := []*model.Signature{}
  127. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  128. CollectName: repo.CollectionSignature,
  129. Query: repo.Map{"_id": bson.M{"$in": purchase.SignUsers}},
  130. Sort: bson.M{"sort": 1},
  131. }, &signs)
  132. billExcel.SetSignatures(signs)
  133. }
  134. }
  135. billExcel.SetContent(&purchase)
  136. billExcel.SetTitle(fmt.Sprintf("%s原材料采购单", companyName))
  137. }
  138. }
  139. // 工艺
  140. if tidArr[0] == "2" {
  141. produce := model.ProduceBill{}
  142. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  143. CollectName: repo.CollectionBillProduce,
  144. Query: repo.Map{"_id": billId},
  145. }, &produce)
  146. if found {
  147. billExcel = NewProduceBill(f)
  148. if produce.Reviewed == 1 {
  149. if len(produce.SignUsers) > 0 {
  150. signs := []*model.Signature{}
  151. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  152. CollectName: repo.CollectionSignature,
  153. Query: repo.Map{"_id": bson.M{"$in": produce.SignUsers}},
  154. Sort: bson.M{"sort": 1},
  155. }, &signs)
  156. billExcel.SetSignatures(signs)
  157. }
  158. }
  159. billExcel.SetContent(&produce)
  160. billExcel.SetTitle(fmt.Sprintf("%s加工单", companyName))
  161. }
  162. }
  163. // 成品采购
  164. if tidArr[0] == "3" {
  165. product := model.ProductBill{}
  166. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  167. CollectName: repo.CollectionBillProduct,
  168. Query: repo.Map{"_id": billId},
  169. }, &product)
  170. if found {
  171. billExcel = NewProductBill(f)
  172. if product.Reviewed == 1 {
  173. if len(product.SignUsers) > 0 {
  174. signs := []*model.Signature{}
  175. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  176. CollectName: repo.CollectionSignature,
  177. Query: repo.Map{"_id": bson.M{"$in": product.SignUsers}},
  178. Sort: bson.M{"sort": 1},
  179. }, &signs)
  180. billExcel.SetSignatures(signs)
  181. }
  182. }
  183. billExcel.SetContent(&product)
  184. billExcel.SetTitle(companyName)
  185. }
  186. }
  187. if billExcel == nil {
  188. continue
  189. }
  190. billExcel.SetRow(row)
  191. billExcel.Draws()
  192. row = billExcel.GetRow() + 5
  193. }
  194. c.Header("Content-Type", "application/octet-stream")
  195. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  196. c.Header("Content-Transfer-Encoding", "binary")
  197. err = f.Write(c.Writer)
  198. if err != nil {
  199. return nil, err
  200. }
  201. return nil, nil
  202. }
  203. func DownLoadCompBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  204. _planId := c.Query("id")
  205. compId := c.Query("compId")
  206. planId, err := primitive.ObjectIDFromHex(_planId)
  207. if err != nil {
  208. return nil, errors.New("planId错误")
  209. }
  210. plan := model.ProductPlan{}
  211. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  212. CollectName: repo.CollectionProductPlan,
  213. Query: repo.Map{"_id": planId},
  214. }, &plan)
  215. if !found || err != nil {
  216. return nil, errors.New("数据未找到")
  217. }
  218. // 获取部件单据
  219. curComp := &model.PackComponent{}
  220. for _, comp := range plan.Pack.Components {
  221. if comp.Id == compId {
  222. curComp = comp
  223. }
  224. }
  225. if curComp.Id == "" {
  226. return nil, errors.New("该组件不存在")
  227. }
  228. // 获取bill
  229. if len(curComp.Stages) == 0 {
  230. return nil, errors.New("该组件数据不存在")
  231. }
  232. // 获取不同类型的单据id
  233. billIds := make([]string, 0)
  234. for _, stage := range curComp.Stages {
  235. billId, _ := primitive.ObjectIDFromHex(stage.BillId)
  236. if !billId.IsZero() {
  237. billIds = append(billIds, fmt.Sprintf("%d_%s", stage.BillType, stage.BillId))
  238. }
  239. }
  240. // 去重单据号
  241. typeBillIds := removeDuplicationSort(billIds)
  242. if len(typeBillIds) < 1 {
  243. return nil, errors.New("未找到单据信息")
  244. }
  245. f := excelize.NewFile()
  246. index := f.NewSheet("Sheet1")
  247. f.SetActiveSheet(index)
  248. f.SetDefaultFont("宋体")
  249. companyName := getCompanyName(apictx)
  250. row := 0
  251. for _, tId := range typeBillIds {
  252. tidArr := strings.Split(tId, "_")
  253. var billExcel IExcel
  254. // 采购
  255. billId, _ := primitive.ObjectIDFromHex(tidArr[1])
  256. if tidArr[0] == "1" {
  257. purchase := model.PurchaseBill{}
  258. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  259. CollectName: repo.CollectionBillPurchase,
  260. Query: repo.Map{"_id": billId},
  261. }, &purchase)
  262. if found {
  263. billExcel = NewPurchaseBill(f)
  264. if purchase.Reviewed == 1 {
  265. if len(purchase.SignUsers) > 0 {
  266. signs := []*model.Signature{}
  267. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  268. CollectName: repo.CollectionSignature,
  269. Query: repo.Map{"_id": bson.M{"$in": purchase.SignUsers}},
  270. Sort: bson.M{"sort": 1},
  271. }, &signs)
  272. billExcel.SetSignatures(signs)
  273. }
  274. }
  275. billExcel.SetContent(&purchase)
  276. billExcel.SetTitle(fmt.Sprintf("%s原材料采购单", companyName))
  277. }
  278. }
  279. // 工艺
  280. if tidArr[0] == "2" {
  281. produce := model.ProduceBill{}
  282. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  283. CollectName: repo.CollectionBillProduce,
  284. Query: repo.Map{"_id": billId},
  285. }, &produce)
  286. if found {
  287. billExcel = NewProduceBill(f)
  288. if produce.Reviewed == 1 {
  289. if len(produce.SignUsers) > 0 {
  290. signs := []*model.Signature{}
  291. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  292. CollectName: repo.CollectionSignature,
  293. Query: repo.Map{"_id": bson.M{"$in": produce.SignUsers}},
  294. Sort: bson.M{"sort": 1},
  295. }, &signs)
  296. billExcel.SetSignatures(signs)
  297. }
  298. }
  299. billExcel.SetContent(&produce)
  300. billExcel.SetTitle(fmt.Sprintf("%s加工单", companyName))
  301. }
  302. }
  303. // 成品采购
  304. if tidArr[0] == "3" {
  305. product := model.ProductBill{}
  306. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  307. CollectName: repo.CollectionBillProduct,
  308. Query: repo.Map{"_id": billId},
  309. }, &product)
  310. if found {
  311. billExcel = NewProductBill(f)
  312. if product.Reviewed == 1 {
  313. if len(product.SignUsers) > 0 {
  314. signs := []*model.Signature{}
  315. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  316. CollectName: repo.CollectionSignature,
  317. Query: repo.Map{"_id": bson.M{"$in": product.SignUsers}},
  318. Sort: bson.M{"sort": 1},
  319. }, &signs)
  320. billExcel.SetSignatures(signs)
  321. }
  322. }
  323. billExcel.SetContent(&product)
  324. billExcel.SetTitle(companyName)
  325. }
  326. }
  327. if billExcel == nil {
  328. continue
  329. }
  330. billExcel.SetRow(row)
  331. billExcel.Draws()
  332. row = billExcel.GetRow() + 5
  333. }
  334. c.Header("Content-Type", "application/octet-stream")
  335. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  336. c.Header("Content-Transfer-Encoding", "binary")
  337. err = f.Write(c.Writer)
  338. if err != nil {
  339. return nil, err
  340. }
  341. return nil, nil
  342. }
  343. // 创建生产计划
  344. func CreateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  345. var plan model.ProductPlan
  346. err := c.ShouldBindJSON(&plan)
  347. if err != nil {
  348. fmt.Println(err)
  349. return nil, errors.New("参数错误!")
  350. }
  351. if plan.Name == "" {
  352. return nil, errors.New("生产计划名为空")
  353. }
  354. if plan.Total == 0 {
  355. return nil, errors.New("生产计划数应不为0")
  356. }
  357. plan.Status = "process" // 进行中
  358. plan.CreateTime = time.Now()
  359. plan.UpdateTime = time.Now()
  360. result, err := repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, &plan)
  361. return result, err
  362. }
  363. // 获取生产计划信息
  364. func GetProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  365. planId := c.Param("id")
  366. id, err := primitive.ObjectIDFromHex(planId)
  367. if err != nil {
  368. return nil, errors.New("非法id")
  369. }
  370. var plan model.ProductPlan
  371. option := &repo.DocSearchOptions{
  372. CollectName: repo.CollectionProductPlan,
  373. Query: repo.Map{"_id": id},
  374. }
  375. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &plan)
  376. if !found || err != nil {
  377. log.Info(err)
  378. return nil, errors.New("数据未找到")
  379. }
  380. billStates := map[string]string{}
  381. if plan.Pack != nil && plan.Pack.Components != nil {
  382. for _, comp := range plan.Pack.Components {
  383. if comp.Stages != nil {
  384. for _, stage := range comp.Stages {
  385. if len(stage.BillId) > 0 {
  386. collectName := ""
  387. // 材料
  388. if stage.BillType == 1 {
  389. collectName = repo.CollectionBillPurchase
  390. }
  391. // 工艺
  392. if stage.BillType == 2 {
  393. collectName = repo.CollectionBillProduce
  394. }
  395. // 成品
  396. if stage.BillType == 3 {
  397. collectName = repo.CollectionBillProduct
  398. }
  399. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: collectName, Query: repo.Map{"_id": stage.BillId}, Project: []string{"status"}})
  400. if ok {
  401. billStates[stage.BillId] = state["status"].(string)
  402. }
  403. }
  404. }
  405. }
  406. }
  407. }
  408. return map[string]interface{}{
  409. "plan": plan,
  410. "billStates": billStates,
  411. }, nil
  412. }
  413. // 获取生产计划列表
  414. func GetProductPlans(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  415. page, size, query := UtilQueryPageSize(c)
  416. if _packId, ok := query["packId"]; ok {
  417. packId, _ := primitive.ObjectIDFromHex(_packId.(string))
  418. query["pack._id"] = packId
  419. delete(query, "packId")
  420. }
  421. option := &repo.PageSearchOptions{
  422. CollectName: repo.CollectionProductPlan,
  423. Query: query,
  424. Page: page,
  425. Size: size,
  426. Sort: bson.M{"createTime": -1},
  427. Project: []string{"_id", "thumbnail", "name", "updateTime", "createTime", "createUser", "total", "totalPrice", "status"},
  428. }
  429. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  430. }
  431. // 更新生产计划
  432. func UpdateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  433. var plan model.ProductPlan
  434. err := c.ShouldBindJSON(&plan)
  435. if err != nil {
  436. return nil, errors.New("参数错误")
  437. }
  438. if plan.Id.Hex() == "" {
  439. return nil, errors.New("id的为空")
  440. }
  441. plan.UpdateTime = time.Now()
  442. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, plan.Id.Hex(), &plan)
  443. }
  444. // 删除生产计划
  445. func DelProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  446. planId := c.Param("id")
  447. if planId == "" {
  448. return nil, errors.New("id为空")
  449. }
  450. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, planId)
  451. }