report.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. package api
  2. import (
  3. "archive/zip"
  4. "box-cost/db/model"
  5. "box-cost/db/repo"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "os"
  10. "path/filepath"
  11. "regexp"
  12. "strings"
  13. "sync"
  14. "time"
  15. "github.com/gin-gonic/gin"
  16. "github.com/go-redis/redis/v8"
  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 Report(r *GinRouter) {
  23. // 加工列表
  24. r.GETJWT("/report/produce/list", ReportProduceList)
  25. // 采购列表
  26. r.GETJWT("/report/purchase/list", ReportPurchaseList)
  27. r.GETJWT("/report/product/list", ReportProductList)
  28. r.GETJWT("/report/list", ReportList)
  29. r.GETJWT("/report/download", ReportListDownload)
  30. //
  31. r.GETJWT("/report/bills/download", ReportBillsDownload)
  32. }
  33. // 下载单据
  34. func ReportBillsDownload(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  35. _, _, query := UtilQueryPageSize(c)
  36. // supplierId := primitive.NilObjectID
  37. // if _supplierId, ok := query["supplierId"]; ok {
  38. // supplierId, _ = primitive.ObjectIDFromHex(_supplierId.(string))
  39. // }
  40. _isPdf := "false"
  41. if isPdf, ok := query["isPdf"]; ok {
  42. delete(query, "isPdf")
  43. if isPdf.(string) == "true" {
  44. _isPdf = "true"
  45. }
  46. }
  47. // timeRange := []interface{}{}
  48. // if _timeRange, ok := query["timeRange"]; ok {
  49. // timeRange, _ = _timeRange.([]interface{})
  50. // }
  51. filtter := handleReportQuery(query)
  52. purchases := []*model.PurchaseBill{}
  53. produces := []*model.ProduceBill{}
  54. products := []*model.ProductBill{}
  55. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  56. CollectName: repo.CollectionBillPurchase,
  57. Query: filtter,
  58. }, &purchases)
  59. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  60. CollectName: repo.CollectionBillProduce,
  61. Query: filtter,
  62. }, &produces)
  63. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  64. CollectName: repo.CollectionBillProduct,
  65. Query: filtter,
  66. }, &products)
  67. // 加入redis有序集合中进行排序
  68. // 把对应type_id:对象存入map中避免再次查询表
  69. typeBills := map[string]interface{}{}
  70. redisCli := apictx.Svc.Redis
  71. reportBillKey := "report-bill-list:" + apictx.User.Parent
  72. isExist := redisCli.Exists(apictx.CreateRepoCtx().Ctx, reportBillKey).Val()
  73. // 不存在这个key时
  74. if isExist < 1 {
  75. if len(purchases) > 0 {
  76. for _, purchase := range purchases {
  77. member := "purchase_" + purchase.Id.Hex()
  78. score := purchase.CompleteTime.Unix()
  79. redisCli.ZAdd(apictx.CreateRepoCtx().Ctx, reportBillKey, &redis.Z{Score: float64(score), Member: member})
  80. typeBills[member] = purchase
  81. }
  82. }
  83. if len(produces) > 0 {
  84. for _, produce := range produces {
  85. member := "produce_" + produce.Id.Hex()
  86. score := produce.CompleteTime.Unix()
  87. redisCli.ZAdd(apictx.CreateRepoCtx().Ctx, reportBillKey, &redis.Z{Score: float64(score), Member: member})
  88. typeBills[member] = produce
  89. }
  90. }
  91. if len(products) > 0 {
  92. for _, product := range products {
  93. member := "product_" + product.Id.Hex()
  94. score := product.CompleteTime.Unix()
  95. redisCli.ZAdd(apictx.CreateRepoCtx().Ctx, reportBillKey, &redis.Z{Score: float64(score), Member: member})
  96. typeBills[member] = product
  97. }
  98. }
  99. // 设置过期时间
  100. redisCli.Expire(apictx.CreateRepoCtx().Ctx, reportBillKey, 1*time.Second)
  101. }
  102. total, err := redisCli.ZCard(apictx.CreateRepoCtx().Ctx, reportBillKey).Uint64()
  103. fmt.Println("单据总数量: ", total)
  104. if err != nil {
  105. fmt.Println(err)
  106. return nil, err
  107. }
  108. reports, err := redisCli.ZRevRange(apictx.CreateRepoCtx().Ctx, reportBillKey, 0, -1).Result()
  109. if err != nil {
  110. return nil, err
  111. }
  112. if len(reports) < 1 {
  113. return nil, errors.New("没有单据信息")
  114. }
  115. // 获取供应商名称
  116. // supplier := &model.Supplier{}
  117. // repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  118. // CollectName: repo.CollectionSupplier,
  119. // Query: repo.Map{"_id": supplierId},
  120. // Project: []string{"name"},
  121. // }, supplier)
  122. r := regexp.MustCompile(`/`)
  123. ct := time.Now().Format("20060102150405")
  124. zipName := fmt.Sprintf("统计单据_%s", ct)
  125. // 获取订单信息下载
  126. saveTmpDir := fmt.Sprintf("tmp1/report/%s", zipName)
  127. if isExistDir(saveTmpDir) {
  128. os.RemoveAll(saveTmpDir)
  129. }
  130. // 记录文件数量
  131. var wg sync.WaitGroup
  132. c1 := make(chan int, 10)
  133. for _, tId := range reports {
  134. productName := ""
  135. supplierName := ""
  136. serialNumber := ""
  137. f := excelize.NewFile()
  138. index := f.NewSheet("Sheet1")
  139. f.SetActiveSheet(index)
  140. f.SetDefaultFont("宋体")
  141. tidArr := strings.Split(tId, "_")
  142. var billExcel IExcel
  143. // 采购
  144. if tidArr[0] == "purchase" {
  145. purchase := typeBills[tId].(*model.PurchaseBill)
  146. billExcel = NewPurchaseBill(f)
  147. // 获取签名信息
  148. if purchase.Reviewed == 1 {
  149. if len(purchase.SignUsers) > 0 {
  150. signs := []*model.Signature{}
  151. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  152. CollectName: repo.CollectionSignature,
  153. Query: repo.Map{"_id": bson.M{"$in": purchase.SignUsers}},
  154. Sort: bson.M{"sort": 1},
  155. }, &signs)
  156. billExcel.SetSignatures(signs)
  157. }
  158. }
  159. // 获取计划名
  160. plan := &model.ProductPlan{}
  161. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  162. CollectName: repo.CollectionProductPlan,
  163. Query: repo.Map{"_id": purchase.PlanId},
  164. Project: []string{"name"},
  165. }, plan)
  166. productName = purchase.ProductName
  167. supplierName = purchase.Supplier
  168. serialNumber = purchase.SerialNumber
  169. billExcel.SetContent(purchase)
  170. billExcel.SetTitle(fmt.Sprintf("%s原材料采购单", plan.Name))
  171. }
  172. // 工艺
  173. if tidArr[0] == "produce" {
  174. produce := typeBills[tId].(*model.ProduceBill)
  175. billExcel = NewProduceBill(f)
  176. if produce.Reviewed == 1 {
  177. if len(produce.SignUsers) > 0 {
  178. signs := []*model.Signature{}
  179. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  180. CollectName: repo.CollectionSignature,
  181. Query: repo.Map{"_id": bson.M{"$in": produce.SignUsers}},
  182. Sort: bson.M{"sort": 1},
  183. }, &signs)
  184. billExcel.SetSignatures(signs)
  185. }
  186. }
  187. // 获取计划名
  188. plan := &model.ProductPlan{}
  189. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  190. CollectName: repo.CollectionProductPlan,
  191. Query: repo.Map{"_id": produce.PlanId},
  192. Project: []string{"name"},
  193. }, plan)
  194. productName = produce.ProductName
  195. supplierName = produce.Supplier
  196. serialNumber = produce.SerialNumber
  197. billExcel.SetContent(produce)
  198. billExcel.SetTitle(fmt.Sprintf("%s加工单", plan.Name))
  199. }
  200. // 成品采购
  201. if tidArr[0] == "product" {
  202. product := typeBills[tId].(*model.ProductBill)
  203. billExcel = NewProductBill(f)
  204. if product.Reviewed == 1 {
  205. if len(product.SignUsers) > 0 {
  206. signs := []*model.Signature{}
  207. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  208. CollectName: repo.CollectionSignature,
  209. Query: repo.Map{"_id": bson.M{"$in": product.SignUsers}},
  210. Sort: bson.M{"sort": 1},
  211. }, &signs)
  212. billExcel.SetSignatures(signs)
  213. }
  214. }
  215. // 获取计划名
  216. plan := &model.ProductPlan{}
  217. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  218. CollectName: repo.CollectionProductPlan,
  219. Query: repo.Map{"_id": product.PlanId},
  220. Project: []string{"name"},
  221. }, plan)
  222. productName = product.ProductName
  223. supplierName = product.Supplier
  224. serialNumber = product.SerialNumber
  225. billExcel.SetContent(product)
  226. billExcel.SetTitle(plan.Name)
  227. }
  228. billExcel.SetIsPdf(_isPdf)
  229. billExcel.Draws()
  230. buf, _ := f.WriteToBuffer()
  231. ft := "xlsx"
  232. if _isPdf == "true" {
  233. ft = "pdf"
  234. }
  235. _productName := r.ReplaceAllString(productName, `&`)
  236. _supplierName := r.ReplaceAllString(supplierName, `&`)
  237. targePdfName := fmt.Sprintf("%s-%s-%s.%s", _supplierName, _productName, serialNumber, ft)
  238. wg.Add(1)
  239. if _isPdf == "true" {
  240. go toPdfAndSaveTask(buf, apictx.Svc.Conf.PdfApiAddr, saveTmpDir, targePdfName, c1, &wg)
  241. } else {
  242. go saveExcelToTmp(saveTmpDir, targePdfName, buf.Bytes(), c1, &wg)
  243. }
  244. }
  245. go func() {
  246. // 等待所有goroutine
  247. wg.Wait()
  248. // 关闭channel
  249. close(c1)
  250. }()
  251. // channel关闭后结束后停止遍历接收channel中的值
  252. for n := range c1 {
  253. if n == -1 {
  254. return nil, errors.New("下载失败,请重试")
  255. }
  256. }
  257. c.Header("Content-Type", "application/octet-stream")
  258. c.Header("Content-Disposition", "attachment; filename="+zipName+".zip")
  259. c.Header("Content-Transfer-Encoding", "binary")
  260. archive := zip.NewWriter(c.Writer)
  261. defer archive.Close()
  262. // 遍历路径信息
  263. filepath.Walk(saveTmpDir, func(path string, info os.FileInfo, _ error) error {
  264. // 如果是源路径,提前进行下一个遍历
  265. if path == saveTmpDir {
  266. return nil
  267. }
  268. // 获取:文件头信息
  269. header, _ := zip.FileInfoHeader(info)
  270. header.Name = strings.TrimPrefix(path, saveTmpDir+`/`)
  271. // 判断:文件是不是文件夹
  272. if info.IsDir() {
  273. header.Name += `/`
  274. } else {
  275. // 设置:zip的文件压缩算法
  276. header.Method = zip.Deflate
  277. }
  278. // 创建:压缩包头部信息
  279. writer, _ := archive.CreateHeader(header)
  280. if !info.IsDir() {
  281. file, _ := os.Open(path)
  282. defer file.Close()
  283. io.Copy(writer, file)
  284. }
  285. return nil
  286. })
  287. // 删除缓存目录
  288. os.RemoveAll(saveTmpDir)
  289. return nil, nil
  290. }
  291. // 加工单
  292. func ReportListDownload(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  293. _, _, query := UtilQueryPageSize(c)
  294. // start, stop := CreatePageRange(page, size)
  295. supplierId := primitive.NilObjectID
  296. if _supplierId, ok := query["supplierId"]; ok {
  297. supplierId, _ = primitive.ObjectIDFromHex(_supplierId.(string))
  298. }
  299. timeRange := []interface{}{}
  300. if _timeRange, ok := query["timeRange"]; ok {
  301. timeRange, _ = _timeRange.([]interface{})
  302. }
  303. filtter := handleReportQuery(query)
  304. purchases := []*model.PurchaseBill{}
  305. produces := []*model.ProduceBill{}
  306. products := []*model.ProductBill{}
  307. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  308. CollectName: repo.CollectionBillPurchase,
  309. Query: filtter,
  310. Project: []string{"_id", "completeTime"},
  311. }, &purchases)
  312. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  313. CollectName: repo.CollectionBillProduce,
  314. Query: filtter,
  315. Project: []string{"_id", "completeTime"},
  316. }, &produces)
  317. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  318. CollectName: repo.CollectionBillProduct,
  319. Query: filtter,
  320. Project: []string{"_id", "completeTime"},
  321. }, &products)
  322. // 加入redis有序集合中
  323. redisCli := apictx.Svc.Redis
  324. reportBillKey := "report-bill-list:" + apictx.User.Parent
  325. isExist := redisCli.Exists(apictx.CreateRepoCtx().Ctx, reportBillKey).Val()
  326. // 不存在这个key时
  327. if isExist < 1 {
  328. if len(purchases) > 0 {
  329. for _, purchase := range purchases {
  330. member := "purchase_" + purchase.Id.Hex()
  331. score := purchase.CompleteTime.Unix()
  332. redisCli.ZAdd(apictx.CreateRepoCtx().Ctx, reportBillKey, &redis.Z{Score: float64(score), Member: member})
  333. }
  334. }
  335. if len(produces) > 0 {
  336. for _, produce := range produces {
  337. member := "produce_" + produce.Id.Hex()
  338. score := produce.CompleteTime.Unix()
  339. redisCli.ZAdd(apictx.CreateRepoCtx().Ctx, reportBillKey, &redis.Z{Score: float64(score), Member: member})
  340. }
  341. }
  342. if len(products) > 0 {
  343. for _, product := range products {
  344. member := "product_" + product.Id.Hex()
  345. score := product.CompleteTime.Unix()
  346. redisCli.ZAdd(apictx.CreateRepoCtx().Ctx, reportBillKey, &redis.Z{Score: float64(score), Member: member})
  347. }
  348. }
  349. // 设置过期时间
  350. redisCli.Expire(apictx.CreateRepoCtx().Ctx, reportBillKey, 1*time.Second)
  351. }
  352. total, err := redisCli.ZCard(apictx.CreateRepoCtx().Ctx, reportBillKey).Uint64()
  353. fmt.Println(total)
  354. if err != nil {
  355. fmt.Println(err)
  356. return nil, err
  357. }
  358. reports, err := redisCli.ZRevRange(apictx.CreateRepoCtx().Ctx, reportBillKey, 0, -1).Result()
  359. if err != nil {
  360. return nil, err
  361. }
  362. if len(reports) < 1 {
  363. return nil, errors.New("没有单据信息")
  364. }
  365. lists := []map[string]interface{}{}
  366. for _, report := range reports {
  367. billArray := strings.Split(report, "_")
  368. billType := billArray[0]
  369. _billId := billArray[1]
  370. billId, _ := primitive.ObjectIDFromHex(_billId)
  371. billData := map[string]interface{}{}
  372. found := false
  373. if billType == "purchase" {
  374. found, billData = repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  375. CollectName: repo.CollectionBillPurchase,
  376. Query: repo.Map{"_id": billId},
  377. })
  378. }
  379. if billType == "produce" {
  380. found, billData = repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  381. CollectName: repo.CollectionBillProduce,
  382. Query: repo.Map{"_id": billId},
  383. })
  384. }
  385. if billType == "product" {
  386. found, billData = repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  387. CollectName: repo.CollectionBillProduct,
  388. Query: repo.Map{"_id": billId},
  389. })
  390. }
  391. if found {
  392. billData["billType"] = billType
  393. lists = append(lists, billData)
  394. }
  395. }
  396. f := excelize.NewFile()
  397. defer f.Close()
  398. index := f.NewSheet("Sheet1")
  399. f.SetActiveSheet(index)
  400. f.SetDefaultFont("宋体")
  401. report := NewReportExcel(f)
  402. supplier := model.Supplier{}
  403. supplierName := "【所有供应商】"
  404. if !supplierId.IsZero() {
  405. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  406. CollectName: repo.CollectionSupplier,
  407. Query: repo.Map{"_id": supplierId},
  408. Project: []string{"name"},
  409. }, &supplier)
  410. supplierName = supplier.Name
  411. }
  412. report.SupplierName = supplierName
  413. if len(timeRange) == 2 {
  414. report.TimeRange = append(report.TimeRange, timeRange[0].(string), timeRange[1].(string))
  415. }
  416. report.Content = lists
  417. report.Draws()
  418. c.Header("Content-Type", "application/octet-stream")
  419. c.Header("Content-Disposition", "attachment; filename="+"report.xlsx")
  420. c.Header("Content-Transfer-Encoding", "binary")
  421. err = f.Write(c.Writer)
  422. if err != nil {
  423. return nil, err
  424. }
  425. return nil, nil
  426. }
  427. // 加工单
  428. func ReportList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  429. page, size, query := UtilQueryPageSize(c)
  430. start, stop := CreatePageRange(page, size)
  431. filtter := handleReportQuery(query)
  432. purchases := []*model.PurchaseBill{}
  433. produces := []*model.ProduceBill{}
  434. products := []*model.ProductBill{}
  435. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  436. CollectName: repo.CollectionBillPurchase,
  437. Query: filtter,
  438. Project: []string{"_id", "completeTime"},
  439. }, &purchases)
  440. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  441. CollectName: repo.CollectionBillProduce,
  442. Query: filtter,
  443. Project: []string{"_id", "completeTime"},
  444. }, &produces)
  445. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  446. CollectName: repo.CollectionBillProduct,
  447. Query: filtter,
  448. Project: []string{"_id", "completeTime"},
  449. }, &products)
  450. // 加入redis有序集合中
  451. redisCli := apictx.Svc.Redis
  452. reportBillKey := "report-bill-list:" + apictx.User.Parent
  453. isExist := redisCli.Exists(apictx.CreateRepoCtx().Ctx, reportBillKey).Val()
  454. // 不存在这个key时
  455. if isExist < 1 {
  456. if len(purchases) > 0 {
  457. for _, purchase := range purchases {
  458. member := "purchase_" + purchase.Id.Hex()
  459. score := purchase.CompleteTime.Unix()
  460. redisCli.ZAdd(apictx.CreateRepoCtx().Ctx, reportBillKey, &redis.Z{Score: float64(score), Member: member})
  461. }
  462. }
  463. if len(produces) > 0 {
  464. for _, produce := range produces {
  465. member := "produce_" + produce.Id.Hex()
  466. score := produce.CompleteTime.Unix()
  467. redisCli.ZAdd(apictx.CreateRepoCtx().Ctx, reportBillKey, &redis.Z{Score: float64(score), Member: member})
  468. }
  469. }
  470. if len(products) > 0 {
  471. for _, product := range products {
  472. member := "product_" + product.Id.Hex()
  473. score := product.CompleteTime.Unix()
  474. redisCli.ZAdd(apictx.CreateRepoCtx().Ctx, reportBillKey, &redis.Z{Score: float64(score), Member: member})
  475. }
  476. }
  477. // 设置过期时间
  478. redisCli.Expire(apictx.CreateRepoCtx().Ctx, reportBillKey, 1*time.Second)
  479. }
  480. total, err := redisCli.ZCard(apictx.CreateRepoCtx().Ctx, reportBillKey).Uint64()
  481. if err != nil {
  482. fmt.Println(err)
  483. return nil, err
  484. }
  485. reports, err := redisCli.ZRevRange(apictx.CreateRepoCtx().Ctx, reportBillKey, start, stop).Result()
  486. if err != nil {
  487. return nil, err
  488. }
  489. if len(reports) < 1 {
  490. return repo.PageResult{
  491. List: []map[string]interface{}{},
  492. Page: page,
  493. Size: size,
  494. Total: 0,
  495. }, nil
  496. }
  497. lists := []map[string]interface{}{}
  498. for _, report := range reports {
  499. billArray := strings.Split(report, "_")
  500. billType := billArray[0]
  501. _billId := billArray[1]
  502. billId, _ := primitive.ObjectIDFromHex(_billId)
  503. billData := map[string]interface{}{}
  504. found := false
  505. if billType == "purchase" {
  506. found, billData = repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  507. CollectName: repo.CollectionBillPurchase,
  508. Query: repo.Map{"_id": billId},
  509. })
  510. }
  511. if billType == "produce" {
  512. found, billData = repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  513. CollectName: repo.CollectionBillProduce,
  514. Query: repo.Map{"_id": billId},
  515. })
  516. }
  517. if billType == "product" {
  518. found, billData = repo.RepoSeachDocMap(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  519. CollectName: repo.CollectionBillProduct,
  520. Query: repo.Map{"_id": billId},
  521. })
  522. }
  523. if found {
  524. billData["billType"] = billType
  525. lists = append(lists, billData)
  526. }
  527. }
  528. return repo.PageResult{
  529. List: lists,
  530. Total: int64(total),
  531. Page: page,
  532. Size: size,
  533. }, nil
  534. }
  535. func CreatePageRange(page, size int64) (int64, int64) {
  536. if page < 1 {
  537. page = 1
  538. }
  539. if size < 1 {
  540. size = 10
  541. }
  542. start := (page - 1) * size
  543. stop := page*size - 1
  544. return start, stop
  545. }
  546. func ReportProduceList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  547. page, size, query := UtilQueryPageSize(c)
  548. // 条件处理
  549. // 获取采购单符合条件的信息
  550. return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  551. CollectName: repo.CollectionBillProduce,
  552. Query: handleReportQuery(query),
  553. Page: page,
  554. Size: size,
  555. })
  556. }
  557. // 采购单
  558. func ReportPurchaseList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  559. page, size, query := UtilQueryPageSize(c)
  560. return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  561. CollectName: repo.CollectionBillPurchase,
  562. Query: handleReportQuery(query),
  563. Page: page,
  564. Size: size,
  565. })
  566. }
  567. func ReportProductList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  568. page, size, query := UtilQueryPageSize(c)
  569. return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  570. CollectName: repo.CollectionBillProduct,
  571. Query: handleReportQuery(query),
  572. Page: page,
  573. Size: size,
  574. })
  575. }