plan.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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. res, err := excelToPdf(buf, toPdfAddr)
  442. if err != nil {
  443. fmt.Println(err)
  444. log.Error(err)
  445. // pdfRes := ToPdfResult{
  446. // IsSucc: false,
  447. // Err: err,
  448. // }
  449. // toPdfResult <- pdfRes
  450. toPdfResult <- -1
  451. wg.Done()
  452. return
  453. }
  454. byteData, err := io.ReadAll(res.Body)
  455. if err != nil {
  456. fmt.Println(err)
  457. // pdfRes := ToPdfResult{
  458. // IsSucc: false,
  459. // Err: err,
  460. // }
  461. // toPdfResult <- pdfRes
  462. toPdfResult <- -1
  463. wg.Done()
  464. return
  465. }
  466. defer res.Body.Close()
  467. err = savePdfToTmp(saveTmpDir, targetPdfName, byteData)
  468. if err != nil {
  469. // pdfRes := ToPdfResult{
  470. // IsSucc: false,
  471. // Err: err,
  472. // }
  473. // toPdfResult <- pdfRes
  474. toPdfResult <- -1
  475. wg.Done()
  476. return
  477. }
  478. // pdfRes := ToPdfResult{
  479. // IsSucc: true,
  480. // Err: err,
  481. // }
  482. // toPdfResult <- pdfRes
  483. toPdfResult <- 1
  484. wg.Done()
  485. }
  486. func DownLoadCompBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  487. _planId := c.Query("id")
  488. compId := c.Query("compId")
  489. planId, err := primitive.ObjectIDFromHex(_planId)
  490. if err != nil {
  491. return nil, errors.New("planId错误")
  492. }
  493. plan := model.ProductPlan{}
  494. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  495. CollectName: repo.CollectionProductPlan,
  496. Query: repo.Map{"_id": planId},
  497. }, &plan)
  498. if !found || err != nil {
  499. return nil, errors.New("数据未找到")
  500. }
  501. // 获取部件单据
  502. curComp := &model.PackComponent{}
  503. for _, comp := range plan.Pack.Components {
  504. if comp.Id == compId {
  505. curComp = comp
  506. }
  507. }
  508. if curComp.Id == "" {
  509. return nil, errors.New("该组件不存在")
  510. }
  511. // 获取bill
  512. if len(curComp.Stages) == 0 {
  513. return nil, errors.New("该组件数据不存在")
  514. }
  515. // 获取不同类型的单据id
  516. billIds := make([]string, 0)
  517. for _, stage := range curComp.Stages {
  518. billId, _ := primitive.ObjectIDFromHex(stage.BillId)
  519. if !billId.IsZero() {
  520. billIds = append(billIds, fmt.Sprintf("%d_%s", stage.BillType, stage.BillId))
  521. }
  522. }
  523. // 去重单据号
  524. typeBillIds := removeDuplicationSort(billIds)
  525. if len(typeBillIds) < 1 {
  526. return nil, errors.New("未找到单据信息")
  527. }
  528. f := excelize.NewFile()
  529. index := f.NewSheet("Sheet1")
  530. f.SetActiveSheet(index)
  531. f.SetDefaultFont("宋体")
  532. companyName := getCompanyName(apictx)
  533. row := 0
  534. for _, tId := range typeBillIds {
  535. tidArr := strings.Split(tId, "_")
  536. var billExcel IExcel
  537. // 采购
  538. billId, _ := primitive.ObjectIDFromHex(tidArr[1])
  539. if tidArr[0] == "1" {
  540. purchase := model.PurchaseBill{}
  541. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  542. CollectName: repo.CollectionBillPurchase,
  543. Query: repo.Map{"_id": billId},
  544. }, &purchase)
  545. if found {
  546. billExcel = NewPurchaseBill(f)
  547. if purchase.Reviewed == 1 {
  548. if len(purchase.SignUsers) > 0 {
  549. signs := []*model.Signature{}
  550. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  551. CollectName: repo.CollectionSignature,
  552. Query: repo.Map{"_id": bson.M{"$in": purchase.SignUsers}},
  553. Sort: bson.M{"sort": 1},
  554. }, &signs)
  555. billExcel.SetSignatures(signs)
  556. }
  557. }
  558. billExcel.SetContent(&purchase)
  559. billExcel.SetTitle(fmt.Sprintf("%s原材料采购单", companyName))
  560. }
  561. }
  562. // 工艺
  563. if tidArr[0] == "2" {
  564. produce := model.ProduceBill{}
  565. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  566. CollectName: repo.CollectionBillProduce,
  567. Query: repo.Map{"_id": billId},
  568. }, &produce)
  569. if found {
  570. billExcel = NewProduceBill(f)
  571. if produce.Reviewed == 1 {
  572. if len(produce.SignUsers) > 0 {
  573. signs := []*model.Signature{}
  574. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  575. CollectName: repo.CollectionSignature,
  576. Query: repo.Map{"_id": bson.M{"$in": produce.SignUsers}},
  577. Sort: bson.M{"sort": 1},
  578. }, &signs)
  579. billExcel.SetSignatures(signs)
  580. }
  581. }
  582. billExcel.SetContent(&produce)
  583. billExcel.SetTitle(fmt.Sprintf("%s加工单", companyName))
  584. }
  585. }
  586. // 成品采购
  587. if tidArr[0] == "3" {
  588. product := model.ProductBill{}
  589. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  590. CollectName: repo.CollectionBillProduct,
  591. Query: repo.Map{"_id": billId},
  592. }, &product)
  593. if found {
  594. billExcel = NewProductBill(f)
  595. if product.Reviewed == 1 {
  596. if len(product.SignUsers) > 0 {
  597. signs := []*model.Signature{}
  598. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  599. CollectName: repo.CollectionSignature,
  600. Query: repo.Map{"_id": bson.M{"$in": product.SignUsers}},
  601. Sort: bson.M{"sort": 1},
  602. }, &signs)
  603. billExcel.SetSignatures(signs)
  604. }
  605. }
  606. billExcel.SetContent(&product)
  607. billExcel.SetTitle(companyName)
  608. }
  609. }
  610. if billExcel == nil {
  611. continue
  612. }
  613. billExcel.SetRow(row)
  614. billExcel.Draws()
  615. row = billExcel.GetRow() + 5
  616. }
  617. c.Header("Content-Type", "application/octet-stream")
  618. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  619. c.Header("Content-Transfer-Encoding", "binary")
  620. err = f.Write(c.Writer)
  621. if err != nil {
  622. return nil, err
  623. }
  624. return nil, nil
  625. }
  626. // 创建生产计划
  627. func CreateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  628. var plan model.ProductPlan
  629. err := c.ShouldBindJSON(&plan)
  630. if err != nil {
  631. fmt.Println(err)
  632. return nil, errors.New("参数错误!")
  633. }
  634. if plan.Name == "" {
  635. return nil, errors.New("生产计划名为空")
  636. }
  637. if plan.Total == 0 {
  638. return nil, errors.New("生产计划数应不为0")
  639. }
  640. plan.Status = "process" // 进行中
  641. plan.CreateTime = time.Now()
  642. plan.UpdateTime = time.Now()
  643. result, err := repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, &plan)
  644. return result, err
  645. }
  646. // 获取生产计划信息
  647. func GetProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  648. planId := c.Param("id")
  649. id, err := primitive.ObjectIDFromHex(planId)
  650. if err != nil {
  651. return nil, errors.New("非法id")
  652. }
  653. var plan model.ProductPlan
  654. option := &repo.DocSearchOptions{
  655. CollectName: repo.CollectionProductPlan,
  656. Query: repo.Map{"_id": id},
  657. }
  658. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &plan)
  659. if !found || err != nil {
  660. log.Info(err)
  661. return nil, errors.New("数据未找到")
  662. }
  663. billStates := map[string]string{}
  664. if plan.Pack != nil && plan.Pack.Components != nil {
  665. for _, comp := range plan.Pack.Components {
  666. if comp.Stages != nil {
  667. for _, stage := range comp.Stages {
  668. if len(stage.BillId) > 0 {
  669. collectName := ""
  670. // 材料
  671. if stage.BillType == 1 {
  672. collectName = repo.CollectionBillPurchase
  673. }
  674. // 工艺
  675. if stage.BillType == 2 {
  676. collectName = repo.CollectionBillProduce
  677. }
  678. // 成品
  679. if stage.BillType == 3 {
  680. collectName = repo.CollectionBillProduct
  681. }
  682. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: collectName, Query: repo.Map{"_id": stage.BillId}, Project: []string{"status"}})
  683. if ok {
  684. billStates[stage.BillId] = state["status"].(string)
  685. }
  686. }
  687. }
  688. }
  689. }
  690. }
  691. return map[string]interface{}{
  692. "plan": plan,
  693. "billStates": billStates,
  694. }, nil
  695. }
  696. // 获取生产计划列表
  697. func GetProductPlans(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  698. page, size, query := UtilQueryPageSize(c)
  699. if _packId, ok := query["packId"]; ok {
  700. packId, _ := primitive.ObjectIDFromHex(_packId.(string))
  701. query["pack._id"] = packId
  702. delete(query, "packId")
  703. }
  704. option := &repo.PageSearchOptions{
  705. CollectName: repo.CollectionProductPlan,
  706. Query: query,
  707. Page: page,
  708. Size: size,
  709. Sort: bson.M{"createTime": -1},
  710. Project: []string{"_id", "thumbnail", "name", "updateTime", "createTime", "createUser", "total", "totalPrice", "status"},
  711. }
  712. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  713. }
  714. // 更新生产计划
  715. func UpdateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  716. var plan model.ProductPlan
  717. err := c.ShouldBindJSON(&plan)
  718. if err != nil {
  719. return nil, errors.New("参数错误")
  720. }
  721. if plan.Id.Hex() == "" {
  722. return nil, errors.New("id的为空")
  723. }
  724. plan.UpdateTime = time.Now()
  725. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, plan.Id.Hex(), &plan)
  726. }
  727. // 删除生产计划
  728. func DelProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  729. planId := c.Param("id")
  730. if planId == "" {
  731. return nil, errors.New("id为空")
  732. }
  733. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, planId)
  734. }