plan.go 21 KB

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