|
@@ -31,10 +31,264 @@ func Report(r *GinRouter) {
|
|
|
r.GETJWT("/report/list", ReportList)
|
|
|
r.GETJWT("/report/download", ReportListDownload)
|
|
|
//
|
|
|
- r.GETJWT("/report/bills/download", ReportBillsDownload)
|
|
|
+ r.GETJWT("/report/bills/download", ReportBillsDownload1)
|
|
|
}
|
|
|
|
|
|
// 下载单据
|
|
|
+func ReportBillsDownload1(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
+ _, _, query := UtilQueryPageSize(c)
|
|
|
+ filtter := handleReportQuery(query)
|
|
|
+
|
|
|
+ purchases := []*model.PurchaseBill{}
|
|
|
+ produces := []*model.ProduceBill{}
|
|
|
+ products := []*model.ProductBill{}
|
|
|
+
|
|
|
+ repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
|
|
|
+ CollectName: repo.CollectionBillPurchase,
|
|
|
+ Query: filtter,
|
|
|
+ }, &purchases)
|
|
|
+ repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
|
|
|
+ CollectName: repo.CollectionBillProduce,
|
|
|
+ Query: filtter,
|
|
|
+ }, &produces)
|
|
|
+ repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
|
|
|
+ CollectName: repo.CollectionBillProduct,
|
|
|
+ Query: filtter,
|
|
|
+ }, &products)
|
|
|
+
|
|
|
+ // 加入redis有序集合中进行排序
|
|
|
+ // 把对应type_id:对象存入map中避免再次查询表
|
|
|
+ typeBills := map[string]interface{}{}
|
|
|
+ redisCli := apictx.Svc.Redis
|
|
|
+ reportBillKey := "report-bill-list:" + apictx.User.Parent
|
|
|
+ isExist := redisCli.Exists(apictx.CreateRepoCtx().Ctx, reportBillKey).Val()
|
|
|
+ // 不存在这个key时
|
|
|
+ if isExist < 1 {
|
|
|
+ if len(purchases) > 0 {
|
|
|
+ for _, purchase := range purchases {
|
|
|
+ member := "purchase_" + purchase.Id.Hex()
|
|
|
+ score := purchase.CompleteTime.Unix()
|
|
|
+ redisCli.ZAdd(apictx.CreateRepoCtx().Ctx, reportBillKey, &redis.Z{Score: float64(score), Member: member})
|
|
|
+ typeBills[member] = purchase
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if len(produces) > 0 {
|
|
|
+ for _, produce := range produces {
|
|
|
+ member := "produce_" + produce.Id.Hex()
|
|
|
+ score := produce.CompleteTime.Unix()
|
|
|
+ redisCli.ZAdd(apictx.CreateRepoCtx().Ctx, reportBillKey, &redis.Z{Score: float64(score), Member: member})
|
|
|
+ typeBills[member] = produce
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if len(products) > 0 {
|
|
|
+ for _, product := range products {
|
|
|
+ member := "product_" + product.Id.Hex()
|
|
|
+ score := product.CompleteTime.Unix()
|
|
|
+ redisCli.ZAdd(apictx.CreateRepoCtx().Ctx, reportBillKey, &redis.Z{Score: float64(score), Member: member})
|
|
|
+ typeBills[member] = product
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 设置过期时间
|
|
|
+ redisCli.Expire(apictx.CreateRepoCtx().Ctx, reportBillKey, 1*time.Second)
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ total, err := redisCli.ZCard(apictx.CreateRepoCtx().Ctx, reportBillKey).Uint64()
|
|
|
+ fmt.Println("单据总数量: ", total)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(err)
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ reports, err := redisCli.ZRevRange(apictx.CreateRepoCtx().Ctx, reportBillKey, 0, -1).Result()
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(reports) < 1 {
|
|
|
+ return nil, errors.New("没有单据信息")
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取供应商名称
|
|
|
+ // supplier := &model.Supplier{}
|
|
|
+ // repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
+ // CollectName: repo.CollectionSupplier,
|
|
|
+ // Query: repo.Map{"_id": supplierId},
|
|
|
+ // Project: []string{"name"},
|
|
|
+ // }, supplier)
|
|
|
+
|
|
|
+ ct := time.Now().Format("20060102150405")
|
|
|
+ // 下载文件的名字
|
|
|
+ eName := fmt.Sprintf("统计单据_%s", ct)
|
|
|
+ companyName := getCompanyName(apictx)
|
|
|
+
|
|
|
+ f := excelize.NewFile()
|
|
|
+ f.SetDefaultFont("宋体")
|
|
|
+ flagIndex := -1
|
|
|
+ // 采购 加工 加工-印刷 加工-覆膜 成品采购
|
|
|
+ typeRows := []int{0, 0, 0, 0, 0}
|
|
|
+ for _, tId := range reports {
|
|
|
+ tidArr := strings.Split(tId, "_")
|
|
|
+ var billExcel IExcel
|
|
|
+ // 采购
|
|
|
+ if tidArr[0] == "purchase" {
|
|
|
+ purchase := typeBills[tidArr[1]].(*model.PurchaseBill)
|
|
|
+
|
|
|
+ sheetName := "采购单"
|
|
|
+ index := f.NewSheet(sheetName)
|
|
|
+ if flagIndex < 0 {
|
|
|
+ flagIndex = index
|
|
|
+ }
|
|
|
+
|
|
|
+ billExcel = NewPurchaseBill(f)
|
|
|
+ billExcel.SetSheetName(sheetName)
|
|
|
+
|
|
|
+ // 获取签名信息
|
|
|
+ if purchase.Reviewed == 1 {
|
|
|
+ if len(purchase.SignUsers) > 0 {
|
|
|
+ signs := []*model.Signature{}
|
|
|
+ repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
|
|
|
+ CollectName: repo.CollectionSignature,
|
|
|
+ Query: repo.Map{"_id": bson.M{"$in": purchase.SignUsers}},
|
|
|
+ Sort: bson.M{"sort": 1},
|
|
|
+ }, &signs)
|
|
|
+ billExcel.SetSignatures(signs)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取计划名
|
|
|
+ // plan := &model.ProductPlan{}
|
|
|
+ // repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
+ // CollectName: repo.CollectionProductPlan,
|
|
|
+ // Query: repo.Map{"_id": purchase.PlanId},
|
|
|
+ // Project: []string{"name"},
|
|
|
+ // }, plan)
|
|
|
+ // productName = purchase.ProductName
|
|
|
+ // supplierName = purchase.Supplier
|
|
|
+ // serialNumber = purchase.SerialNumber
|
|
|
+
|
|
|
+ billExcel.SetContent(purchase)
|
|
|
+ billExcel.SetTitle(fmt.Sprintf("%s原材料采购单", companyName))
|
|
|
+ billExcel.SetRow(typeRows[0])
|
|
|
+ billExcel.Draws()
|
|
|
+ typeRows[0] = billExcel.GetRow() + 5
|
|
|
+ }
|
|
|
+
|
|
|
+ // 加工单
|
|
|
+ if tidArr[0] == "produce" {
|
|
|
+ produce := typeBills[tidArr[1]].(*model.ProduceBill)
|
|
|
+
|
|
|
+ sheetName := "加工单"
|
|
|
+ if produce.IsPrint {
|
|
|
+ sheetName = "加工单-印刷"
|
|
|
+ } else if produce.IsLam {
|
|
|
+ sheetName = "加工单-覆膜"
|
|
|
+ }
|
|
|
+ index := f.NewSheet(sheetName)
|
|
|
+ if flagIndex < 0 {
|
|
|
+ flagIndex = index
|
|
|
+ }
|
|
|
+
|
|
|
+ billExcel = NewPurchaseBill(f)
|
|
|
+ billExcel.SetSheetName(sheetName)
|
|
|
+
|
|
|
+ // 获取签名信息
|
|
|
+ if produce.Reviewed == 1 {
|
|
|
+ if len(produce.SignUsers) > 0 {
|
|
|
+ signs := []*model.Signature{}
|
|
|
+ repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
|
|
|
+ CollectName: repo.CollectionSignature,
|
|
|
+ Query: repo.Map{"_id": bson.M{"$in": produce.SignUsers}},
|
|
|
+ Sort: bson.M{"sort": 1},
|
|
|
+ }, &signs)
|
|
|
+ billExcel.SetSignatures(signs)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取计划名
|
|
|
+ // plan := &model.ProductPlan{}
|
|
|
+ // repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
+ // CollectName: repo.CollectionProductPlan,
|
|
|
+ // Query: repo.Map{"_id": produce.PlanId},
|
|
|
+ // Project: []string{"name"},
|
|
|
+ // }, plan)
|
|
|
+
|
|
|
+ billExcel.SetContent(produce)
|
|
|
+ billExcel.SetTitle(fmt.Sprintf("%s加工单", companyName))
|
|
|
+ if produce.IsPrint {
|
|
|
+ billExcel.SetRow(typeRows[2])
|
|
|
+ billExcel.Draws()
|
|
|
+ typeRows[2] = billExcel.GetRow() + 5
|
|
|
+ } else if produce.IsLam {
|
|
|
+ billExcel.SetRow(typeRows[3])
|
|
|
+ billExcel.Draws()
|
|
|
+ typeRows[3] = billExcel.GetRow() + 5
|
|
|
+ } else {
|
|
|
+ billExcel.SetRow(typeRows[1])
|
|
|
+ billExcel.Draws()
|
|
|
+ typeRows[1] = billExcel.GetRow() + 5
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 成品
|
|
|
+ if tidArr[0] == "product" {
|
|
|
+ product := typeBills[tidArr[1]].(*model.ProductBill)
|
|
|
+ sheetName := "成品采购单"
|
|
|
+ index := f.NewSheet(sheetName)
|
|
|
+ if flagIndex < 0 {
|
|
|
+ flagIndex = index
|
|
|
+ }
|
|
|
+
|
|
|
+ billExcel = NewPurchaseBill(f)
|
|
|
+ billExcel.SetSheetName(sheetName)
|
|
|
+
|
|
|
+ // 获取签名信息
|
|
|
+ if product.Reviewed == 1 {
|
|
|
+ if len(product.SignUsers) > 0 {
|
|
|
+ signs := []*model.Signature{}
|
|
|
+ repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
|
|
|
+ CollectName: repo.CollectionSignature,
|
|
|
+ Query: repo.Map{"_id": bson.M{"$in": product.SignUsers}},
|
|
|
+ Sort: bson.M{"sort": 1},
|
|
|
+ }, &signs)
|
|
|
+ billExcel.SetSignatures(signs)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取计划名
|
|
|
+ // plan := &model.ProductPlan{}
|
|
|
+ // repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
|
+ // CollectName: repo.CollectionProductPlan,
|
|
|
+ // Query: repo.Map{"_id": product.PlanId},
|
|
|
+ // Project: []string{"name"},
|
|
|
+ // }, plan)
|
|
|
+ billExcel.SetContent(product)
|
|
|
+ billExcel.SetTitle(companyName)
|
|
|
+ billExcel.SetRow(typeRows[4])
|
|
|
+ billExcel.Draws()
|
|
|
+ typeRows[4] = billExcel.GetRow() + 5
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置活跃sheet
|
|
|
+ f.SetActiveSheet(flagIndex)
|
|
|
+ // 删除默认Sheet1
|
|
|
+ f.DeleteSheet("Sheet1")
|
|
|
+
|
|
|
+ c.Header("Content-Type", "application/octet-stream")
|
|
|
+ c.Header("Content-Disposition", "attachment; filename="+fmt.Sprintf("%s.xlsx", eName))
|
|
|
+ c.Header("Content-Transfer-Encoding", "binary")
|
|
|
+
|
|
|
+ f.Write(c.Writer)
|
|
|
+
|
|
|
+ return nil, nil
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+// 下载单据
|
|
|
+//
|
|
|
+// !更改为ReportBillsDownload1 确定不需要后删除
|
|
|
func ReportBillsDownload(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
_, _, query := UtilQueryPageSize(c)
|
|
|
// supplierId := primitive.NilObjectID
|