plan.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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. // wg.Wait()
  381. // cnum := 0
  382. // for {
  383. // cnum++
  384. // result := <-c1
  385. // if !result.IsSucc {
  386. // return nil, result.Err
  387. // }
  388. // fmt.Println("cnum:", cnum)
  389. // fmt.Println("fileNum:", cnum)
  390. // if cnum == fileNum {
  391. // break
  392. // }
  393. // }
  394. // select {
  395. // case v := <-c1:
  396. // fmt.Println(v)
  397. // if !v.IsSucc {
  398. // return nil, err
  399. // }
  400. // }
  401. fmt.Println("fileNum: ", fileNum)
  402. c.Header("Content-Type", "application/octet-stream")
  403. c.Header("Content-Disposition", "attachment; filename="+planName+".zip")
  404. c.Header("Content-Transfer-Encoding", "binary")
  405. archive := zip.NewWriter(c.Writer)
  406. defer archive.Close()
  407. // 遍历路径信息
  408. filepath.Walk(saveTmpDir, func(path string, info os.FileInfo, _ error) error {
  409. // 如果是源路径,提前进行下一个遍历
  410. if path == saveTmpDir {
  411. return nil
  412. }
  413. // 获取:文件头信息
  414. header, _ := zip.FileInfoHeader(info)
  415. header.Name = strings.TrimPrefix(path, saveTmpDir+`/`)
  416. // 判断:文件是不是文件夹
  417. if info.IsDir() {
  418. header.Name += `/`
  419. } else {
  420. // 设置:zip的文件压缩算法
  421. header.Method = zip.Deflate
  422. }
  423. // 创建:压缩包头部信息
  424. writer, _ := archive.CreateHeader(header)
  425. if !info.IsDir() {
  426. file, _ := os.Open(path)
  427. defer file.Close()
  428. io.Copy(writer, file)
  429. }
  430. return nil
  431. })
  432. // 删除缓存目录
  433. os.RemoveAll(saveTmpDir)
  434. return nil, nil
  435. }
  436. type ToPdfResult struct {
  437. IsSucc bool
  438. Err error
  439. }
  440. func toPdfAndSaveTask(buf *bytes.Buffer, toPdfAddr, saveTmpDir, targetPdfName string, toPdfResult chan<- int, wg *sync.WaitGroup) {
  441. if buf.Len() < 1<<10 {
  442. fmt.Println("execl内容为空")
  443. log.Error("execl内容为空")
  444. toPdfResult <- -1
  445. wg.Done()
  446. return
  447. }
  448. res, err := excelToPdf(buf, toPdfAddr)
  449. if err != nil {
  450. fmt.Println(err)
  451. log.Error(err)
  452. // pdfRes := ToPdfResult{
  453. // IsSucc: false,
  454. // Err: err,
  455. // }
  456. // toPdfResult <- pdfRes
  457. toPdfResult <- -1
  458. wg.Done()
  459. return
  460. }
  461. byteData, err := io.ReadAll(res.Body)
  462. if err != nil {
  463. fmt.Println(err)
  464. // pdfRes := ToPdfResult{
  465. // IsSucc: false,
  466. // Err: err,
  467. // }
  468. // toPdfResult <- pdfRes
  469. toPdfResult <- -1
  470. wg.Done()
  471. return
  472. }
  473. if len(byteData) < 0 {
  474. fmt.Println("pdf内容为空")
  475. log.Error("pdf内容为空")
  476. toPdfResult <- -1
  477. wg.Done()
  478. return
  479. }
  480. defer res.Body.Close()
  481. err = savePdfToTmp(saveTmpDir, targetPdfName, byteData)
  482. if err != nil {
  483. // pdfRes := ToPdfResult{
  484. // IsSucc: false,
  485. // Err: err,
  486. // }
  487. // toPdfResult <- pdfRes
  488. toPdfResult <- -1
  489. wg.Done()
  490. return
  491. }
  492. // pdfRes := ToPdfResult{
  493. // IsSucc: true,
  494. // Err: err,
  495. // }
  496. // toPdfResult <- pdfRes
  497. toPdfResult <- 1
  498. wg.Done()
  499. }
  500. func DownLoadCompBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  501. _planId := c.Query("id")
  502. compId := c.Query("compId")
  503. planId, err := primitive.ObjectIDFromHex(_planId)
  504. if err != nil {
  505. return nil, errors.New("planId错误")
  506. }
  507. plan := model.ProductPlan{}
  508. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  509. CollectName: repo.CollectionProductPlan,
  510. Query: repo.Map{"_id": planId},
  511. }, &plan)
  512. if !found || err != nil {
  513. return nil, errors.New("数据未找到")
  514. }
  515. // 获取部件单据
  516. curComp := &model.PackComponent{}
  517. for _, comp := range plan.Pack.Components {
  518. if comp.Id == compId {
  519. curComp = comp
  520. }
  521. }
  522. if curComp.Id == "" {
  523. return nil, errors.New("该组件不存在")
  524. }
  525. // 获取bill
  526. if len(curComp.Stages) == 0 {
  527. return nil, errors.New("该组件数据不存在")
  528. }
  529. // 获取不同类型的单据id
  530. billIds := make([]string, 0)
  531. for _, stage := range curComp.Stages {
  532. billId, _ := primitive.ObjectIDFromHex(stage.BillId)
  533. if !billId.IsZero() {
  534. billIds = append(billIds, fmt.Sprintf("%d_%s", stage.BillType, stage.BillId))
  535. }
  536. }
  537. // 去重单据号
  538. typeBillIds := removeDuplicationSort(billIds)
  539. if len(typeBillIds) < 1 {
  540. return nil, errors.New("未找到单据信息")
  541. }
  542. f := excelize.NewFile()
  543. index := f.NewSheet("Sheet1")
  544. f.SetActiveSheet(index)
  545. f.SetDefaultFont("宋体")
  546. companyName := getCompanyName(apictx)
  547. row := 0
  548. for _, tId := range typeBillIds {
  549. tidArr := strings.Split(tId, "_")
  550. var billExcel IExcel
  551. // 采购
  552. billId, _ := primitive.ObjectIDFromHex(tidArr[1])
  553. if tidArr[0] == "1" {
  554. purchase := model.PurchaseBill{}
  555. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  556. CollectName: repo.CollectionBillPurchase,
  557. Query: repo.Map{"_id": billId},
  558. }, &purchase)
  559. if found {
  560. billExcel = NewPurchaseBill(f)
  561. if purchase.Reviewed == 1 {
  562. if len(purchase.SignUsers) > 0 {
  563. signs := []*model.Signature{}
  564. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  565. CollectName: repo.CollectionSignature,
  566. Query: repo.Map{"_id": bson.M{"$in": purchase.SignUsers}},
  567. Sort: bson.M{"sort": 1},
  568. }, &signs)
  569. billExcel.SetSignatures(signs)
  570. }
  571. }
  572. billExcel.SetContent(&purchase)
  573. billExcel.SetTitle(fmt.Sprintf("%s原材料采购单", companyName))
  574. }
  575. }
  576. // 工艺
  577. if tidArr[0] == "2" {
  578. produce := model.ProduceBill{}
  579. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  580. CollectName: repo.CollectionBillProduce,
  581. Query: repo.Map{"_id": billId},
  582. }, &produce)
  583. if found {
  584. billExcel = NewProduceBill(f)
  585. if produce.Reviewed == 1 {
  586. if len(produce.SignUsers) > 0 {
  587. signs := []*model.Signature{}
  588. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  589. CollectName: repo.CollectionSignature,
  590. Query: repo.Map{"_id": bson.M{"$in": produce.SignUsers}},
  591. Sort: bson.M{"sort": 1},
  592. }, &signs)
  593. billExcel.SetSignatures(signs)
  594. }
  595. }
  596. billExcel.SetContent(&produce)
  597. billExcel.SetTitle(fmt.Sprintf("%s加工单", companyName))
  598. }
  599. }
  600. // 成品采购
  601. if tidArr[0] == "3" {
  602. product := model.ProductBill{}
  603. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  604. CollectName: repo.CollectionBillProduct,
  605. Query: repo.Map{"_id": billId},
  606. }, &product)
  607. if found {
  608. billExcel = NewProductBill(f)
  609. if product.Reviewed == 1 {
  610. if len(product.SignUsers) > 0 {
  611. signs := []*model.Signature{}
  612. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  613. CollectName: repo.CollectionSignature,
  614. Query: repo.Map{"_id": bson.M{"$in": product.SignUsers}},
  615. Sort: bson.M{"sort": 1},
  616. }, &signs)
  617. billExcel.SetSignatures(signs)
  618. }
  619. }
  620. billExcel.SetContent(&product)
  621. billExcel.SetTitle(companyName)
  622. }
  623. }
  624. if billExcel == nil {
  625. continue
  626. }
  627. billExcel.SetRow(row)
  628. billExcel.Draws()
  629. row = billExcel.GetRow() + 5
  630. }
  631. c.Header("Content-Type", "application/octet-stream")
  632. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  633. c.Header("Content-Transfer-Encoding", "binary")
  634. err = f.Write(c.Writer)
  635. if err != nil {
  636. return nil, err
  637. }
  638. return nil, nil
  639. }
  640. // 创建生产计划
  641. func CreateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  642. var plan model.ProductPlan
  643. err := c.ShouldBindJSON(&plan)
  644. if err != nil {
  645. fmt.Println(err)
  646. return nil, errors.New("参数错误!")
  647. }
  648. if plan.Name == "" {
  649. return nil, errors.New("生产计划名为空")
  650. }
  651. if plan.Total == 0 {
  652. return nil, errors.New("生产计划数应不为0")
  653. }
  654. plan.Status = "process" // 进行中
  655. plan.CreateTime = time.Now()
  656. plan.UpdateTime = time.Now()
  657. result, err := repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, &plan)
  658. return result, err
  659. }
  660. // 获取生产计划信息
  661. func GetProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  662. planId := c.Param("id")
  663. id, err := primitive.ObjectIDFromHex(planId)
  664. if err != nil {
  665. return nil, errors.New("非法id")
  666. }
  667. var plan model.ProductPlan
  668. option := &repo.DocSearchOptions{
  669. CollectName: repo.CollectionProductPlan,
  670. Query: repo.Map{"_id": id},
  671. }
  672. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &plan)
  673. if !found || err != nil {
  674. log.Info(err)
  675. return nil, errors.New("数据未找到")
  676. }
  677. billStates := map[string]string{}
  678. if plan.Pack != nil && plan.Pack.Components != nil {
  679. for _, comp := range plan.Pack.Components {
  680. if comp.Stages != nil {
  681. for _, stage := range comp.Stages {
  682. if len(stage.BillId) > 0 {
  683. collectName := ""
  684. // 材料
  685. if stage.BillType == 1 {
  686. collectName = repo.CollectionBillPurchase
  687. }
  688. // 工艺
  689. if stage.BillType == 2 {
  690. collectName = repo.CollectionBillProduce
  691. }
  692. // 成品
  693. if stage.BillType == 3 {
  694. collectName = repo.CollectionBillProduct
  695. }
  696. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: collectName, Query: repo.Map{"_id": stage.BillId}, Project: []string{"status"}})
  697. if ok {
  698. billStates[stage.BillId] = state["status"].(string)
  699. }
  700. }
  701. }
  702. }
  703. }
  704. }
  705. return map[string]interface{}{
  706. "plan": plan,
  707. "billStates": billStates,
  708. }, nil
  709. }
  710. // 获取生产计划列表
  711. func GetProductPlans(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  712. page, size, query := UtilQueryPageSize(c)
  713. if _packId, ok := query["packId"]; ok {
  714. packId, _ := primitive.ObjectIDFromHex(_packId.(string))
  715. query["pack._id"] = packId
  716. delete(query, "packId")
  717. }
  718. option := &repo.PageSearchOptions{
  719. CollectName: repo.CollectionProductPlan,
  720. Query: query,
  721. Page: page,
  722. Size: size,
  723. Sort: bson.M{"createTime": -1},
  724. Project: []string{"_id", "thumbnail", "name", "updateTime", "createTime", "createUser", "total", "totalPrice", "status"},
  725. }
  726. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  727. }
  728. // 更新生产计划
  729. func UpdateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  730. var plan model.ProductPlan
  731. err := c.ShouldBindJSON(&plan)
  732. if err != nil {
  733. return nil, errors.New("参数错误")
  734. }
  735. if plan.Id.Hex() == "" {
  736. return nil, errors.New("id的为空")
  737. }
  738. plan.UpdateTime = time.Now()
  739. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, plan.Id.Hex(), &plan)
  740. }
  741. // 删除生产计划
  742. func DelProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  743. planId := c.Param("id")
  744. if planId == "" {
  745. return nil, errors.New("id为空")
  746. }
  747. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, planId)
  748. }