plan.go 21 KB

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