plan.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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. var wg sync.WaitGroup
  250. c1 := make(chan int)
  251. for _, tId := range typeBillIds {
  252. productName := ""
  253. supplierName := ""
  254. f := excelize.NewFile()
  255. index := f.NewSheet("Sheet1")
  256. f.SetActiveSheet(index)
  257. f.SetDefaultFont("宋体")
  258. tidArr := strings.Split(tId, "_")
  259. var billExcel IExcel
  260. // 采购
  261. billId, _ := primitive.ObjectIDFromHex(tidArr[1])
  262. if tidArr[0] == "1" {
  263. purchase := model.PurchaseBill{}
  264. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  265. CollectName: repo.CollectionBillPurchase,
  266. Query: repo.Map{"_id": billId},
  267. }, &purchase)
  268. if found {
  269. billExcel = NewPurchaseBill(f)
  270. if purchase.Reviewed == 1 {
  271. if len(purchase.SignUsers) > 0 {
  272. signs := []*model.Signature{}
  273. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  274. CollectName: repo.CollectionSignature,
  275. Query: repo.Map{"_id": bson.M{"$in": purchase.SignUsers}},
  276. Sort: bson.M{"sort": 1},
  277. }, &signs)
  278. billExcel.SetSignatures(signs)
  279. }
  280. }
  281. productName = purchase.ProductName
  282. supplierName = purchase.Supplier
  283. billExcel.SetContent(&purchase)
  284. billExcel.SetTitle(fmt.Sprintf("%s原材料采购单", companyName))
  285. }
  286. }
  287. // 工艺
  288. if tidArr[0] == "2" {
  289. produce := model.ProduceBill{}
  290. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  291. CollectName: repo.CollectionBillProduce,
  292. Query: repo.Map{"_id": billId},
  293. }, &produce)
  294. if found {
  295. billExcel = NewProduceBill(f)
  296. if produce.Reviewed == 1 {
  297. if len(produce.SignUsers) > 0 {
  298. signs := []*model.Signature{}
  299. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  300. CollectName: repo.CollectionSignature,
  301. Query: repo.Map{"_id": bson.M{"$in": produce.SignUsers}},
  302. Sort: bson.M{"sort": 1},
  303. }, &signs)
  304. billExcel.SetSignatures(signs)
  305. }
  306. }
  307. productName = produce.ProductName
  308. supplierName = produce.Supplier
  309. billExcel.SetContent(&produce)
  310. billExcel.SetTitle(fmt.Sprintf("%s加工单", companyName))
  311. }
  312. }
  313. // 成品采购
  314. if tidArr[0] == "3" {
  315. product := model.ProductBill{}
  316. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  317. CollectName: repo.CollectionBillProduct,
  318. Query: repo.Map{"_id": billId},
  319. }, &product)
  320. if found {
  321. billExcel = NewProductBill(f)
  322. if product.Reviewed == 1 {
  323. if len(product.SignUsers) > 0 {
  324. signs := []*model.Signature{}
  325. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  326. CollectName: repo.CollectionSignature,
  327. Query: repo.Map{"_id": bson.M{"$in": product.SignUsers}},
  328. Sort: bson.M{"sort": 1},
  329. }, &signs)
  330. billExcel.SetSignatures(signs)
  331. }
  332. }
  333. productName = product.ProductName
  334. supplierName = product.Supplier
  335. billExcel.SetContent(&product)
  336. billExcel.SetTitle(companyName)
  337. }
  338. }
  339. if billExcel == nil {
  340. continue
  341. }
  342. billExcel.SetIsPdf("true")
  343. billExcel.Draws()
  344. buf, _ := f.WriteToBuffer()
  345. targePdfName := fmt.Sprintf("%s-%s.pdf", productName, supplierName)
  346. wg.Add(1)
  347. go toPdfAndSaveTask(buf, apictx.Svc.Conf.PdfApiAddr, saveTmpDir, targePdfName, c1, &wg)
  348. }
  349. go func() {
  350. // 等待所有goroutine
  351. wg.Wait()
  352. // 关闭channel
  353. close(c1)
  354. }()
  355. // channel关闭后结束后停止遍历接收channel中的值
  356. for n := range c1 {
  357. if n == -1 {
  358. return nil, errors.New("下载失败,请重试")
  359. }
  360. }
  361. c.Header("Content-Type", "application/octet-stream")
  362. c.Header("Content-Disposition", "attachment; filename="+planName+".zip")
  363. c.Header("Content-Transfer-Encoding", "binary")
  364. archive := zip.NewWriter(c.Writer)
  365. defer archive.Close()
  366. // 遍历路径信息
  367. filepath.Walk(saveTmpDir, func(path string, info os.FileInfo, _ error) error {
  368. // 如果是源路径,提前进行下一个遍历
  369. if path == saveTmpDir {
  370. return nil
  371. }
  372. // 获取:文件头信息
  373. header, _ := zip.FileInfoHeader(info)
  374. header.Name = strings.TrimPrefix(path, saveTmpDir+`/`)
  375. // 判断:文件是不是文件夹
  376. if info.IsDir() {
  377. header.Name += `/`
  378. } else {
  379. // 设置:zip的文件压缩算法
  380. header.Method = zip.Deflate
  381. }
  382. // 创建:压缩包头部信息
  383. writer, _ := archive.CreateHeader(header)
  384. if !info.IsDir() {
  385. file, _ := os.Open(path)
  386. defer file.Close()
  387. io.Copy(writer, file)
  388. }
  389. return nil
  390. })
  391. // 删除缓存目录
  392. os.RemoveAll(saveTmpDir)
  393. return nil, nil
  394. }
  395. type ToPdfResult struct {
  396. IsSucc bool
  397. Err error
  398. }
  399. func toPdfAndSaveTask(buf *bytes.Buffer, toPdfAddr, saveTmpDir, targetPdfName string, toPdfResult chan<- int, wg *sync.WaitGroup) {
  400. if buf.Len() < 1<<10 {
  401. fmt.Println("execl内容为空")
  402. log.Error("execl内容为空")
  403. toPdfResult <- -1
  404. wg.Done()
  405. return
  406. }
  407. res, err := excelToPdf(buf, toPdfAddr)
  408. if err != nil {
  409. fmt.Println(err)
  410. log.Error(err)
  411. // pdfRes := ToPdfResult{
  412. // IsSucc: false,
  413. // Err: err,
  414. // }
  415. // toPdfResult <- pdfRes
  416. toPdfResult <- -1
  417. wg.Done()
  418. return
  419. }
  420. byteData, err := io.ReadAll(res.Body)
  421. if err != nil {
  422. fmt.Println(err)
  423. // pdfRes := ToPdfResult{
  424. // IsSucc: false,
  425. // Err: err,
  426. // }
  427. // toPdfResult <- pdfRes
  428. toPdfResult <- -1
  429. wg.Done()
  430. return
  431. }
  432. if len(byteData) < 1 {
  433. fmt.Println("pdf内容为空")
  434. log.Error("pdf内容为空")
  435. toPdfResult <- -1
  436. wg.Done()
  437. return
  438. }
  439. defer res.Body.Close()
  440. err = savePdfToTmp(saveTmpDir, targetPdfName, byteData)
  441. if err != nil {
  442. // pdfRes := ToPdfResult{
  443. // IsSucc: false,
  444. // Err: err,
  445. // }
  446. // toPdfResult <- pdfRes
  447. toPdfResult <- -1
  448. wg.Done()
  449. return
  450. }
  451. // pdfRes := ToPdfResult{
  452. // IsSucc: true,
  453. // Err: err,
  454. // }
  455. // toPdfResult <- pdfRes
  456. toPdfResult <- 1
  457. wg.Done()
  458. }
  459. func DownLoadCompBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  460. _planId := c.Query("id")
  461. compId := c.Query("compId")
  462. planId, err := primitive.ObjectIDFromHex(_planId)
  463. if err != nil {
  464. return nil, errors.New("planId错误")
  465. }
  466. plan := model.ProductPlan{}
  467. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  468. CollectName: repo.CollectionProductPlan,
  469. Query: repo.Map{"_id": planId},
  470. }, &plan)
  471. if !found || err != nil {
  472. return nil, errors.New("数据未找到")
  473. }
  474. // 获取部件单据
  475. curComp := &model.PackComponent{}
  476. for _, comp := range plan.Pack.Components {
  477. if comp.Id == compId {
  478. curComp = comp
  479. }
  480. }
  481. if curComp.Id == "" {
  482. return nil, errors.New("该组件不存在")
  483. }
  484. // 获取bill
  485. if len(curComp.Stages) == 0 {
  486. return nil, errors.New("该组件数据不存在")
  487. }
  488. // 获取不同类型的单据id
  489. billIds := make([]string, 0)
  490. for _, stage := range curComp.Stages {
  491. billId, _ := primitive.ObjectIDFromHex(stage.BillId)
  492. if !billId.IsZero() {
  493. billIds = append(billIds, fmt.Sprintf("%d_%s", stage.BillType, stage.BillId))
  494. }
  495. }
  496. // 去重单据号
  497. typeBillIds := removeDuplicationSort(billIds)
  498. if len(typeBillIds) < 1 {
  499. return nil, errors.New("未找到单据信息")
  500. }
  501. f := excelize.NewFile()
  502. index := f.NewSheet("Sheet1")
  503. f.SetActiveSheet(index)
  504. f.SetDefaultFont("宋体")
  505. companyName := getCompanyName(apictx)
  506. row := 0
  507. for _, tId := range typeBillIds {
  508. tidArr := strings.Split(tId, "_")
  509. var billExcel IExcel
  510. // 采购
  511. billId, _ := primitive.ObjectIDFromHex(tidArr[1])
  512. if tidArr[0] == "1" {
  513. purchase := model.PurchaseBill{}
  514. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  515. CollectName: repo.CollectionBillPurchase,
  516. Query: repo.Map{"_id": billId},
  517. }, &purchase)
  518. if found {
  519. billExcel = NewPurchaseBill(f)
  520. if purchase.Reviewed == 1 {
  521. if len(purchase.SignUsers) > 0 {
  522. signs := []*model.Signature{}
  523. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  524. CollectName: repo.CollectionSignature,
  525. Query: repo.Map{"_id": bson.M{"$in": purchase.SignUsers}},
  526. Sort: bson.M{"sort": 1},
  527. }, &signs)
  528. billExcel.SetSignatures(signs)
  529. }
  530. }
  531. billExcel.SetContent(&purchase)
  532. billExcel.SetTitle(fmt.Sprintf("%s原材料采购单", companyName))
  533. }
  534. }
  535. // 工艺
  536. if tidArr[0] == "2" {
  537. produce := model.ProduceBill{}
  538. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  539. CollectName: repo.CollectionBillProduce,
  540. Query: repo.Map{"_id": billId},
  541. }, &produce)
  542. if found {
  543. billExcel = NewProduceBill(f)
  544. if produce.Reviewed == 1 {
  545. if len(produce.SignUsers) > 0 {
  546. signs := []*model.Signature{}
  547. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  548. CollectName: repo.CollectionSignature,
  549. Query: repo.Map{"_id": bson.M{"$in": produce.SignUsers}},
  550. Sort: bson.M{"sort": 1},
  551. }, &signs)
  552. billExcel.SetSignatures(signs)
  553. }
  554. }
  555. billExcel.SetContent(&produce)
  556. billExcel.SetTitle(fmt.Sprintf("%s加工单", companyName))
  557. }
  558. }
  559. // 成品采购
  560. if tidArr[0] == "3" {
  561. product := model.ProductBill{}
  562. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  563. CollectName: repo.CollectionBillProduct,
  564. Query: repo.Map{"_id": billId},
  565. }, &product)
  566. if found {
  567. billExcel = NewProductBill(f)
  568. if product.Reviewed == 1 {
  569. if len(product.SignUsers) > 0 {
  570. signs := []*model.Signature{}
  571. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  572. CollectName: repo.CollectionSignature,
  573. Query: repo.Map{"_id": bson.M{"$in": product.SignUsers}},
  574. Sort: bson.M{"sort": 1},
  575. }, &signs)
  576. billExcel.SetSignatures(signs)
  577. }
  578. }
  579. billExcel.SetContent(&product)
  580. billExcel.SetTitle(companyName)
  581. }
  582. }
  583. if billExcel == nil {
  584. continue
  585. }
  586. billExcel.SetRow(row)
  587. billExcel.Draws()
  588. row = billExcel.GetRow() + 5
  589. }
  590. c.Header("Content-Type", "application/octet-stream")
  591. c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
  592. c.Header("Content-Transfer-Encoding", "binary")
  593. err = f.Write(c.Writer)
  594. if err != nil {
  595. return nil, err
  596. }
  597. return nil, nil
  598. }
  599. // 创建生产计划
  600. func CreateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  601. var plan model.ProductPlan
  602. err := c.ShouldBindJSON(&plan)
  603. if err != nil {
  604. fmt.Println(err)
  605. return nil, errors.New("参数错误!")
  606. }
  607. if plan.Name == "" {
  608. return nil, errors.New("生产计划名为空")
  609. }
  610. if plan.Total == 0 {
  611. return nil, errors.New("生产计划数应不为0")
  612. }
  613. plan.Status = "process" // 进行中
  614. plan.CreateTime = time.Now()
  615. plan.UpdateTime = time.Now()
  616. result, err := repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, &plan)
  617. return result, err
  618. }
  619. // 获取生产计划信息
  620. func GetProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  621. planId := c.Param("id")
  622. id, err := primitive.ObjectIDFromHex(planId)
  623. if err != nil {
  624. return nil, errors.New("非法id")
  625. }
  626. var plan model.ProductPlan
  627. option := &repo.DocSearchOptions{
  628. CollectName: repo.CollectionProductPlan,
  629. Query: repo.Map{"_id": id},
  630. }
  631. found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), option, &plan)
  632. if !found || err != nil {
  633. log.Info(err)
  634. return nil, errors.New("数据未找到")
  635. }
  636. billStates := map[string]string{}
  637. if plan.Pack != nil && plan.Pack.Components != nil {
  638. for _, comp := range plan.Pack.Components {
  639. if comp.Stages != nil {
  640. for _, stage := range comp.Stages {
  641. if len(stage.BillId) > 0 {
  642. collectName := ""
  643. // 材料
  644. if stage.BillType == 1 {
  645. collectName = repo.CollectionBillPurchase
  646. }
  647. // 工艺
  648. if stage.BillType == 2 {
  649. collectName = repo.CollectionBillProduce
  650. }
  651. // 成品
  652. if stage.BillType == 3 {
  653. collectName = repo.CollectionBillProduct
  654. }
  655. ok, state := repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: collectName, Query: repo.Map{"_id": stage.BillId}, Project: []string{"status"}})
  656. if ok {
  657. billStates[stage.BillId] = state["status"].(string)
  658. }
  659. }
  660. }
  661. }
  662. }
  663. }
  664. return map[string]interface{}{
  665. "plan": plan,
  666. "billStates": billStates,
  667. }, nil
  668. }
  669. // 获取生产计划列表
  670. func GetProductPlans(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  671. page, size, query := UtilQueryPageSize(c)
  672. if _packId, ok := query["packId"]; ok {
  673. packId, _ := primitive.ObjectIDFromHex(_packId.(string))
  674. query["pack._id"] = packId
  675. delete(query, "packId")
  676. }
  677. if _name, ok := query["name"]; ok {
  678. delete(query, "name")
  679. query["name"] = bson.M{"$regex": _name.(string)}
  680. }
  681. option := &repo.PageSearchOptions{
  682. CollectName: repo.CollectionProductPlan,
  683. Query: query,
  684. Page: page,
  685. Size: size,
  686. Sort: bson.M{"createTime": -1},
  687. Project: []string{"_id", "thumbnail", "name", "updateTime", "createTime", "createUser", "total", "totalPrice", "status"},
  688. }
  689. return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
  690. }
  691. // 更新生产计划
  692. func UpdateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  693. var plan model.ProductPlan
  694. err := c.ShouldBindJSON(&plan)
  695. if err != nil {
  696. return nil, errors.New("参数错误")
  697. }
  698. if plan.Id.Hex() == "" {
  699. return nil, errors.New("id的为空")
  700. }
  701. plan.UpdateTime = time.Now()
  702. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, plan.Id.Hex(), &plan)
  703. }
  704. // 删除生产计划
  705. func DelProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  706. planId := c.Param("id")
  707. if planId == "" {
  708. return nil, errors.New("id为空")
  709. }
  710. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, planId)
  711. }