sun-pc-linux 7 months ago
parent
commit
ee31263e0c
2 changed files with 67 additions and 3 deletions
  1. 50 3
      boxcost/api/report.go
  2. 17 0
      boxcost/api/utils.go

+ 50 - 3
boxcost/api/report.go

@@ -121,8 +121,15 @@ func ReportBillsDownload1(c *gin.Context, apictx *ApiSession) (interface{}, erro
 
 	ct := time.Now().Format("20060102150405")
 	// 下载文件的名字
-	eName := fmt.Sprintf("统计单据_%s", ct)
+	// eName := fmt.Sprintf("统计单据_%s", ct)
 	companyName := getCompanyName(apictx)
+	zipName := fmt.Sprintf("统计单据_%s", ct)
+	// 获取订单信息下载
+	saveTmpDir := fmt.Sprintf("tmp1/report/%s", zipName)
+
+	if isExistDir(saveTmpDir) {
+		os.RemoveAll(saveTmpDir)
+	}
 
 	f := excelize.NewFile()
 	f.SetDefaultFont("宋体")
@@ -274,12 +281,52 @@ func ReportBillsDownload1(c *gin.Context, apictx *ApiSession) (interface{}, erro
 	f.SetActiveSheet(flagIndex)
 	// 删除默认Sheet1
 	f.DeleteSheet("Sheet1")
+	buf, _ := f.WriteToBuffer()
+	targeName := fmt.Sprintf("%s.xlsx", zipName)
+
+	err = saveExcelToTmp1(saveTmpDir, targeName, buf.Bytes())
+	if err != nil {
+		return nil, err
+	}
 
 	c.Header("Content-Type", "application/octet-stream")
-	c.Header("Content-Disposition", "attachment; filename="+fmt.Sprintf("%s.xlsx", eName))
+	c.Header("Content-Disposition", "attachment; filename="+fmt.Sprintf("%s.zip", zipName))
 	c.Header("Content-Transfer-Encoding", "binary")
 
-	f.Write(c.Writer)
+	archive := zip.NewWriter(c.Writer)
+	defer archive.Close()
+	// 遍历路径信息
+	filepath.Walk(saveTmpDir, func(path string, info os.FileInfo, _ error) error {
+
+		// 如果是源路径,提前进行下一个遍历
+		if path == saveTmpDir {
+			return nil
+		}
+
+		// 获取:文件头信息
+		header, _ := zip.FileInfoHeader(info)
+		header.Name = strings.TrimPrefix(path, saveTmpDir+`/`)
+
+		// 判断:文件是不是文件夹
+		if info.IsDir() {
+			header.Name += `/`
+		} else {
+			// 设置:zip的文件压缩算法
+			header.Method = zip.Deflate
+		}
+
+		// 创建:压缩包头部信息
+		writer, _ := archive.CreateHeader(header)
+		if !info.IsDir() {
+			file, _ := os.Open(path)
+			defer file.Close()
+			io.Copy(writer, file)
+		}
+		return nil
+	})
+
+	// 删除缓存目录
+	os.RemoveAll(saveTmpDir)
 
 	return nil, nil
 

+ 17 - 0
boxcost/api/utils.go

@@ -107,6 +107,23 @@ func saveExcelToTmp(saveTmpDir, fileName string, data []byte, toExcelResult chan
 	wg.Done()
 }
 
+func saveExcelToTmp1(saveTmpDir, fileName string, data []byte) error {
+
+	err := os.MkdirAll(saveTmpDir, os.ModePerm)
+	if err != nil {
+		log.Error(err)
+		fmt.Println(err)
+		return err
+	}
+	fullFileName := fmt.Sprintf("%s/%s", saveTmpDir, fileName)
+	if err := os.WriteFile(fullFileName, data, 0666); err != nil {
+		log.Error(err)
+		fmt.Println(err)
+		return err
+	}
+	return nil
+}
+
 func isExistDir(dir string) bool {
 	_, err := os.Stat(dir)
 	if err != nil {