plan.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. package api
  2. import (
  3. "archive/zip"
  4. "box-cost/db/model"
  5. "box-cost/db/repo"
  6. "box-cost/log"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "os"
  11. "path/filepath"
  12. "strings"
  13. "time"
  14. "github.com/gin-gonic/gin"
  15. "github.com/xuri/excelize/v2"
  16. "go.mongodb.org/mongo-driver/bson"
  17. "go.mongodb.org/mongo-driver/bson/primitive"
  18. )
  19. // 生产计划管理
  20. func ProductPlan(r *GinRouter) {
  21. // 创建生产计划
  22. r.POST("/plan/create", CreateProductPlan)
  23. // 获取生产计划详情
  24. r.GET("/plan/detail/:id", GetProductPlan)
  25. // 获取生产计划列表
  26. r.GET("/plan/list", GetProductPlans)
  27. // 更新生产计划
  28. r.POST("/plan/update", UpdateProductPlan)
  29. // 删除生产计划
  30. r.POST("/plan/delete/:id", DelProductPlan)
  31. // 下载部件单据
  32. // r.GET("/bill/plan/download", DownLoadCompBills)
  33. r.GET("/bill/plan/download", DownLoadPlanBills)
  34. r.GET("/bill/plan/downloadPdf", DownLoadPlanBillsPdf)
  35. // 生产成本表
  36. r.GET("/plan/cost/download", DownLoadPlanCost)
  37. }
  38. type SupplierPlanCost struct {
  39. *model.ProductPlan
  40. SupplierId string
  41. }
  42. func DownLoadPlanCost(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  43. _planId := c.Query("id")
  44. supplierId := c.Query("supplierId")
  45. planId, _ := primitive.ObjectIDFromHex(_planId)
  46. if planId.IsZero() {
  47. return nil, errors.New("planId错误")
  48. }
  49. supplierPlanCost := &SupplierPlanCost{}
  50. plan := model.ProductPlan{}
  51. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  52. CollectName: repo.CollectionProductPlan,
  53. Query: repo.Map{"_id": planId},
  54. }, &plan)
  55. if !found || err != nil {
  56. return nil, errors.New("数据未找到")
  57. }
  58. supplierPlanCost.ProductPlan = &plan
  59. supplierPlanCost.SupplierId = supplierId
  60. f := excelize.NewFile()
  61. index := f.NewSheet("Sheet1")
  62. f.SetActiveSheet(index)
  63. f.SetDefaultFont("宋体")
  64. planCostExcel := NewPlanCostExcel(f)
  65. planCostExcel.Title = fmt.Sprintf("生产成本表(%s)%d盒", plan.Name, plan.Total)
  66. planCostExcel.Content = supplierPlanCost
  67. planCostExcel.Draws()
  68. c.Header("Content-Type", "application/octet-stream")
  69. c.Header("Content-Disposition", "attachment; filename="+"planCost.xlsx")
  70. c.Header("Content-Transfer-Encoding", "binary")
  71. err = f.Write(c.Writer)
  72. if err != nil {
  73. return nil, err
  74. }
  75. return nil, nil
  76. }
  77. func DownLoadPlanBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  78. _planId := c.Query("id")
  79. planId, err := primitive.ObjectIDFromHex(_planId)
  80. if err != nil {
  81. return nil, errors.New("planId错误")
  82. }
  83. plan := model.ProductPlan{}
  84. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  85. CollectName: repo.CollectionProductPlan,
  86. Query: repo.Map{"_id": planId},
  87. }, &plan)
  88. if !found || err != nil {
  89. return nil, errors.New("数据未找到")
  90. }
  91. // 获取所有stages单据id
  92. billIds := make([]string, 0)
  93. for _, comp := range plan.Pack.Components {
  94. if comp.Id == "" || len(comp.Stages) == 0 {
  95. continue
  96. }
  97. for _, stage := range comp.Stages {
  98. billId, _ := primitive.ObjectIDFromHex(stage.BillId)
  99. if !billId.IsZero() {
  100. billIds = append(billIds, fmt.Sprintf("%d_%s", stage.BillType, stage.BillId))
  101. }
  102. }
  103. }
  104. // 去重单据号
  105. typeBillIds := removeDuplicationSort(billIds)
  106. if len(typeBillIds) < 1 {
  107. return nil, errors.New("未找到单据信息")
  108. }
  109. f := excelize.NewFile()
  110. index := f.NewSheet("Sheet1")
  111. f.SetActiveSheet(index)
  112. f.SetDefaultFont("宋体")
  113. companyName := getCompanyName(apictx)
  114. row := 0
  115. for _, tId := range typeBillIds {
  116. tidArr := strings.Split(tId, "_")
  117. var billExcel IExcel
  118. // 采购
  119. billId, _ := primitive.ObjectIDFromHex(tidArr[1])
  120. if tidArr[0] == "1" {
  121. purchase := model.PurchaseBill{}
  122. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  123. CollectName: repo.CollectionBillPurchase,
  124. Query: repo.Map{"_id": billId},
  125. }, &purchase)
  126. if found {
  127. billExcel = NewPurchaseBill(f)
  128. if purchase.Reviewed == 1 {
  129. if len(purchase.SignUsers) > 0 {
  130. signs := []*model.Signature{}
  131. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  132. CollectName: repo.CollectionSignature,
  133. Query: repo.Map{"_id": bson.M{"$in": purchase.SignUsers}},
  134. Sort: bson.M{"sort": 1},
  135. }, &signs)
  136. billExcel.SetSignatures(signs)
  137. }
  138. }
  139. billExcel.SetContent(&purchase)
  140. billExcel.SetTitle(fmt.Sprintf("%s原材料采购单", companyName))
  141. }
  142. }
  143. // 工艺
  144. if tidArr[0] == "2" {
  145. produce := model.ProduceBill{}
  146. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  147. CollectName: repo.CollectionBillProduce,
  148. Query: repo.Map{"_id": billId},
  149. }, &produce)
  150. if found {
  151. billExcel = NewProduceBill(f)
  152. if produce.Reviewed == 1 {
  153. if len(produce.SignUsers) > 0 {
  154. signs := []*model.Signature{}
  155. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  156. CollectName: repo.CollectionSignature,
  157. Query: repo.Map{"_id": bson.M{"$in": produce.SignUsers}},
  158. Sort: bson.M{"sort": 1},
  159. }, &signs)
  160. billExcel.SetSignatures(signs)
  161. }
  162. }
  163. billExcel.SetContent(&produce)
  164. billExcel.SetTitle(fmt.Sprintf("%s加工单", companyName))
  165. }
  166. }
  167. // 成品采购
  168. if tidArr[0] == "3" {
  169. product := model.ProductBill{}
  170. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  171. CollectName: repo.CollectionBillProduct,
  172. Query: repo.Map{"_id": billId},
  173. }, &product)
  174. if found {
  175. billExcel = NewProductBill(f)
  176. if product.Reviewed == 1 {
  177. if len(product.SignUsers) > 0 {
  178. signs := []*model.Signature{}
  179. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  180. CollectName: repo.CollectionSignature,
  181. Query: repo.Map{"_id": bson.M{"$in": product.SignUsers}},
  182. Sort: bson.M{"sort": 1},
  183. }, &signs)
  184. billExcel.SetSignatures(signs)
  185. }
  186. }
  187. billExcel.SetContent(&product)
  188. billExcel.SetTitle(companyName)
  189. }
  190. }
  191. if billExcel == nil {
  192. continue
  193. }
  194. billExcel.SetRow(row)
  195. billExcel.Draws()
  196. row = billExcel.GetRow() + 5
  197. }
  198. c.Header("Content-Type", "application/octet-stream")
  199. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  200. c.Header("Content-Transfer-Encoding", "binary")
  201. err = f.Write(c.Writer)
  202. if err != nil {
  203. return nil, err
  204. }
  205. return nil, nil
  206. }
  207. func DownLoadPlanBillsPdf(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  208. _planId := c.Query("id")
  209. planId, err := primitive.ObjectIDFromHex(_planId)
  210. if err != nil {
  211. return nil, errors.New("planId错误")
  212. }
  213. plan := model.ProductPlan{}
  214. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  215. CollectName: repo.CollectionProductPlan,
  216. Query: repo.Map{"_id": planId},
  217. }, &plan)
  218. if !found || err != nil {
  219. return nil, errors.New("数据未找到")
  220. }
  221. // 获取所有stages单据id
  222. billIds := make([]string, 0)
  223. for _, comp := range plan.Pack.Components {
  224. if comp.Id == "" || len(comp.Stages) == 0 {
  225. continue
  226. }
  227. for _, stage := range comp.Stages {
  228. billId, _ := primitive.ObjectIDFromHex(stage.BillId)
  229. if !billId.IsZero() {
  230. billIds = append(billIds, fmt.Sprintf("%d_%s", stage.BillType, stage.BillId))
  231. }
  232. }
  233. }
  234. // 去重单据号
  235. typeBillIds := removeDuplicationSort(billIds)
  236. if len(typeBillIds) < 1 {
  237. return nil, errors.New("未找到单据信息")
  238. }
  239. companyName := getCompanyName(apictx)
  240. planName := plan.Name
  241. // 打包pdf的缓存目录
  242. saveTmpDir := fmt.Sprintf("tmp1/%s", planName)
  243. if isExistDir(saveTmpDir) {
  244. os.RemoveAll(saveTmpDir)
  245. }
  246. // 记录文件数量
  247. fileNum := 0
  248. for _, tId := range typeBillIds {
  249. productName := ""
  250. supplierName := ""
  251. f := excelize.NewFile()
  252. index := f.NewSheet("Sheet1")
  253. f.SetActiveSheet(index)
  254. f.SetDefaultFont("宋体")
  255. tidArr := strings.Split(tId, "_")
  256. var billExcel IExcel
  257. // 采购
  258. billId, _ := primitive.ObjectIDFromHex(tidArr[1])
  259. if tidArr[0] == "1" {
  260. purchase := model.PurchaseBill{}
  261. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  262. CollectName: repo.CollectionBillPurchase,
  263. Query: repo.Map{"_id": billId},
  264. }, &purchase)
  265. if found {
  266. billExcel = NewPurchaseBill(f)
  267. if purchase.Reviewed == 1 {
  268. if len(purchase.SignUsers) > 0 {
  269. signs := []*model.Signature{}
  270. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  271. CollectName: repo.CollectionSignature,
  272. Query: repo.Map{"_id": bson.M{"$in": purchase.SignUsers}},
  273. Sort: bson.M{"sort": 1},
  274. }, &signs)
  275. billExcel.SetSignatures(signs)
  276. }
  277. }
  278. productName = purchase.ProductName
  279. supplierName = purchase.Supplier
  280. billExcel.SetContent(&purchase)
  281. billExcel.SetTitle(fmt.Sprintf("%s原材料采购单", companyName))
  282. }
  283. }
  284. // 工艺
  285. if tidArr[0] == "2" {
  286. produce := model.ProduceBill{}
  287. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  288. CollectName: repo.CollectionBillProduce,
  289. Query: repo.Map{"_id": billId},
  290. }, &produce)
  291. if found {
  292. billExcel = NewProduceBill(f)
  293. if produce.Reviewed == 1 {
  294. if len(produce.SignUsers) > 0 {
  295. signs := []*model.Signature{}
  296. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  297. CollectName: repo.CollectionSignature,
  298. Query: repo.Map{"_id": bson.M{"$in": produce.SignUsers}},
  299. Sort: bson.M{"sort": 1},
  300. }, &signs)
  301. billExcel.SetSignatures(signs)
  302. }
  303. }
  304. productName = produce.ProductName
  305. supplierName = produce.Supplier
  306. billExcel.SetContent(&produce)
  307. billExcel.SetTitle(fmt.Sprintf("%s加工单", companyName))
  308. }
  309. }
  310. // 成品采购
  311. if tidArr[0] == "3" {
  312. product := model.ProductBill{}
  313. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  314. CollectName: repo.CollectionBillProduct,
  315. Query: repo.Map{"_id": billId},
  316. }, &product)
  317. if found {
  318. billExcel = NewProductBill(f)
  319. if product.Reviewed == 1 {
  320. if len(product.SignUsers) > 0 {
  321. signs := []*model.Signature{}
  322. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  323. CollectName: repo.CollectionSignature,
  324. Query: repo.Map{"_id": bson.M{"$in": product.SignUsers}},
  325. Sort: bson.M{"sort": 1},
  326. }, &signs)
  327. billExcel.SetSignatures(signs)
  328. }
  329. }
  330. productName = product.ProductName
  331. supplierName = product.Supplier
  332. billExcel.SetContent(&product)
  333. billExcel.SetTitle(companyName)
  334. }
  335. }
  336. if billExcel == nil {
  337. continue
  338. }
  339. billExcel.SetIsPdf("true")
  340. billExcel.Draws()
  341. buf, _ := f.WriteToBuffer()
  342. res, err := excelToPdf(buf, apictx.Svc.Conf.PdfApiAddr)
  343. if err != nil {
  344. fmt.Println(err)
  345. log.Error(err)
  346. return nil, errors.New("转化pdf失败")
  347. }
  348. body := res.Body
  349. byteData, err := io.ReadAll(body)
  350. if err != nil {
  351. fmt.Println(err)
  352. return nil, err
  353. }
  354. targePdfName := fmt.Sprintf("%s-%s.pdf", productName, supplierName)
  355. err = savePdfToTmp(saveTmpDir, targePdfName, byteData)
  356. if err != nil {
  357. return nil, err
  358. }
  359. fileNum++
  360. }
  361. fmt.Println("fileNum: ", fileNum)
  362. c.Header("Content-Type", "application/octet-stream")
  363. c.Header("Content-Disposition", "attachment; filename="+planName+".zip")
  364. c.Header("Content-Transfer-Encoding", "binary")
  365. archive := zip.NewWriter(c.Writer)
  366. defer archive.Close()
  367. // 遍历路径信息
  368. filepath.Walk(saveTmpDir, func(path string, info os.FileInfo, _ error) error {
  369. // 如果是源路径,提前进行下一个遍历
  370. if path == saveTmpDir {
  371. return nil
  372. }
  373. // 获取:文件头信息
  374. header, _ := zip.FileInfoHeader(info)
  375. header.Name = strings.TrimPrefix(path, saveTmpDir+`/`)
  376. // 判断:文件是不是文件夹
  377. if info.IsDir() {
  378. header.Name += `/`
  379. } else {
  380. // 设置:zip的文件压缩算法
  381. header.Method = zip.Deflate
  382. }
  383. // 创建:压缩包头部信息
  384. writer, _ := archive.CreateHeader(header)
  385. if !info.IsDir() {
  386. file, _ := os.Open(path)
  387. defer file.Close()
  388. io.Copy(writer, file)
  389. }
  390. return nil
  391. })
  392. // 删除缓存目录
  393. os.RemoveAll(saveTmpDir)
  394. return nil, nil
  395. }
  396. func DownLoadCompBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  397. _planId := c.Query("id")
  398. compId := c.Query("compId")
  399. planId, err := primitive.ObjectIDFromHex(_planId)
  400. if err != nil {
  401. return nil, errors.New("planId错误")
  402. }
  403. plan := model.ProductPlan{}
  404. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  405. CollectName: repo.CollectionProductPlan,
  406. Query: repo.Map{"_id": planId},
  407. }, &plan)
  408. if !found || err != nil {
  409. return nil, errors.New("数据未找到")
  410. }
  411. // 获取部件单据
  412. curComp := &model.PackComponent{}
  413. for _, comp := range plan.Pack.Components {
  414. if comp.Id == compId {
  415. curComp = comp
  416. }
  417. }
  418. if curComp.Id == "" {
  419. return nil, errors.New("该组件不存在")
  420. }
  421. // 获取bill
  422. if len(curComp.Stages) == 0 {
  423. return nil, errors.New("该组件数据不存在")
  424. }
  425. // 获取不同类型的单据id
  426. billIds := make([]string, 0)
  427. for _, stage := range curComp.Stages {
  428. billId, _ := primitive.ObjectIDFromHex(stage.BillId)
  429. if !billId.IsZero() {
  430. billIds = append(billIds, fmt.Sprintf("%d_%s", stage.BillType, stage.BillId))
  431. }
  432. }
  433. // 去重单据号
  434. typeBillIds := removeDuplicationSort(billIds)
  435. if len(typeBillIds) < 1 {
  436. return nil, errors.New("未找到单据信息")
  437. }
  438. f := excelize.NewFile()
  439. index := f.NewSheet("Sheet1")
  440. f.SetActiveSheet(index)
  441. f.SetDefaultFont("宋体")
  442. companyName := getCompanyName(apictx)
  443. row := 0
  444. for _, tId := range typeBillIds {
  445. tidArr := strings.Split(tId, "_")
  446. var billExcel IExcel
  447. // 采购
  448. billId, _ := primitive.ObjectIDFromHex(tidArr[1])
  449. if tidArr[0] == "1" {
  450. purchase := model.PurchaseBill{}
  451. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  452. CollectName: repo.CollectionBillPurchase,
  453. Query: repo.Map{"_id": billId},
  454. }, &purchase)
  455. if found {
  456. billExcel = NewPurchaseBill(f)
  457. if purchase.Reviewed == 1 {
  458. if len(purchase.SignUsers) > 0 {
  459. signs := []*model.Signature{}
  460. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  461. CollectName: repo.CollectionSignature,
  462. Query: repo.Map{"_id": bson.M{"$in": purchase.SignUsers}},
  463. Sort: bson.M{"sort": 1},
  464. }, &signs)
  465. billExcel.SetSignatures(signs)
  466. }
  467. }
  468. billExcel.SetContent(&purchase)
  469. billExcel.SetTitle(fmt.Sprintf("%s原材料采购单", companyName))
  470. }
  471. }
  472. // 工艺
  473. if tidArr[0] == "2" {
  474. produce := model.ProduceBill{}
  475. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  476. CollectName: repo.CollectionBillProduce,
  477. Query: repo.Map{"_id": billId},
  478. }, &produce)
  479. if found {
  480. billExcel = NewProduceBill(f)
  481. if produce.Reviewed == 1 {
  482. if len(produce.SignUsers) > 0 {
  483. signs := []*model.Signature{}
  484. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  485. CollectName: repo.CollectionSignature,
  486. Query: repo.Map{"_id": bson.M{"$in": produce.SignUsers}},
  487. Sort: bson.M{"sort": 1},
  488. }, &signs)
  489. billExcel.SetSignatures(signs)
  490. }
  491. }
  492. billExcel.SetContent(&produce)
  493. billExcel.SetTitle(fmt.Sprintf("%s加工单", companyName))
  494. }
  495. }
  496. // 成品采购
  497. if tidArr[0] == "3" {
  498. product := model.ProductBill{}
  499. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  500. CollectName: repo.CollectionBillProduct,
  501. Query: repo.Map{"_id": billId},
  502. }, &product)
  503. if found {
  504. billExcel = NewProductBill(f)
  505. if product.Reviewed == 1 {
  506. if len(product.SignUsers) > 0 {
  507. signs := []*model.Signature{}
  508. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  509. CollectName: repo.CollectionSignature,
  510. Query: repo.Map{"_id": bson.M{"$in": product.SignUsers}},
  511. Sort: bson.M{"sort": 1},
  512. }, &signs)
  513. billExcel.SetSignatures(signs)
  514. }
  515. }
  516. billExcel.SetContent(&product)
  517. billExcel.SetTitle(companyName)
  518. }
  519. }
  520. if billExcel == nil {
  521. continue
  522. }
  523. billExcel.SetRow(row)
  524. billExcel.Draws()
  525. row = billExcel.GetRow() + 5
  526. }
  527. c.Header("Content-Type", "application/octet-stream")
  528. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  529. c.Header("Content-Transfer-Encoding", "binary")
  530. err = f.Write(c.Writer)
  531. if err != nil {
  532. return nil, err
  533. }
  534. return nil, nil
  535. }
  536. // 创建生产计划
  537. func CreateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  538. var plan model.ProductPlan
  539. err := c.ShouldBindJSON(&plan)
  540. if err != nil {
  541. fmt.Println(err)
  542. return nil, errors.New("参数错误!")
  543. }
  544. if plan.Name == "" {
  545. return nil, errors.New("生产计划名为空")
  546. }
  547. if plan.Total == 0 {
  548. return nil, errors.New("生产计划数应不为0")
  549. }
  550. plan.Status = "process" // 进行中
  551. plan.CreateTime = time.Now()
  552. plan.UpdateTime = time.Now()
  553. result, err := repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, &plan)
  554. return result, err
  555. }
  556. // 获取生产计划信息
  557. func GetProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  558. planId := c.Param("id")
  559. id, err := primitive.ObjectIDFromHex(planId)
  560. if err != nil {
  561. return nil, errors.New("非法id")
  562. }
  563. var plan model.ProductPlan
  564. option := &repo.DocSearchOptions{
  565. CollectName: repo.CollectionProductPlan,
  566. Query: repo.Map{"_id": id},
  567. }
  568. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &plan)
  569. if !found || err != nil {
  570. log.Info(err)
  571. return nil, errors.New("数据未找到")
  572. }
  573. billStates := map[string]string{}
  574. if plan.Pack != nil && plan.Pack.Components != nil {
  575. for _, comp := range plan.Pack.Components {
  576. if comp.Stages != nil {
  577. for _, stage := range comp.Stages {
  578. if len(stage.BillId) > 0 {
  579. collectName := ""
  580. // 材料
  581. if stage.BillType == 1 {
  582. collectName = repo.CollectionBillPurchase
  583. }
  584. // 工艺
  585. if stage.BillType == 2 {
  586. collectName = repo.CollectionBillProduce
  587. }
  588. // 成品
  589. if stage.BillType == 3 {
  590. collectName = repo.CollectionBillProduct
  591. }
  592. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: collectName, Query: repo.Map{"_id": stage.BillId}, Project: []string{"status"}})
  593. if ok {
  594. billStates[stage.BillId] = state["status"].(string)
  595. }
  596. }
  597. }
  598. }
  599. }
  600. }
  601. return map[string]interface{}{
  602. "plan": plan,
  603. "billStates": billStates,
  604. }, nil
  605. }
  606. // 获取生产计划列表
  607. func GetProductPlans(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  608. page, size, query := UtilQueryPageSize(c)
  609. if _packId, ok := query["packId"]; ok {
  610. packId, _ := primitive.ObjectIDFromHex(_packId.(string))
  611. query["pack._id"] = packId
  612. delete(query, "packId")
  613. }
  614. option := &repo.PageSearchOptions{
  615. CollectName: repo.CollectionProductPlan,
  616. Query: query,
  617. Page: page,
  618. Size: size,
  619. Sort: bson.M{"createTime": -1},
  620. Project: []string{"_id", "thumbnail", "name", "updateTime", "createTime", "createUser", "total", "totalPrice", "status"},
  621. }
  622. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  623. }
  624. // 更新生产计划
  625. func UpdateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  626. var plan model.ProductPlan
  627. err := c.ShouldBindJSON(&plan)
  628. if err != nil {
  629. return nil, errors.New("参数错误")
  630. }
  631. if plan.Id.Hex() == "" {
  632. return nil, errors.New("id的为空")
  633. }
  634. plan.UpdateTime = time.Now()
  635. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, plan.Id.Hex(), &plan)
  636. }
  637. // 删除生产计划
  638. func DelProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  639. planId := c.Param("id")
  640. if planId == "" {
  641. return nil, errors.New("id为空")
  642. }
  643. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, planId)
  644. }