Jianghan преди 1 година
родител
ревизия
2458a9bc9a
променени са 1 файла, в които са добавени 21 реда и са изтрити 19 реда
  1. 21 19
      service/exprot.go

+ 21 - 19
service/exprot.go

@@ -51,7 +51,8 @@ func (a *ExportReq) AllProjectExport() {
 	timeStr = fmt.Sprintf("%s%s", timeStr, gconv.String(randomInt))
 	pathArr := export("customer_data_ttzl_project", tableColumn, exportKey, timeStr)
 	if len(pathArr) > 0 {
-		pathstr := compressFiles(pathArr, ExportUrl, timeStr)
+		folderPath := fmt.Sprintf("%s/%s", ExportDirectory, timeStr)
+		pathstr := compressFiles(pathArr, folderPath, ExportUrl, timeStr)
 		state := sendMail("项目", a.Mail, pathstr)
 		BiService.Insert("export_record", map[string]interface{}{
 			"positionId": a.PositionId,
@@ -79,7 +80,8 @@ func (a *ExportReq) AllInfoExport() {
 	timeStr = fmt.Sprintf("%s%s", timeStr, gconv.String(randomInt))
 	pathArr := export("customer_data_ttzl", tableColumn, exportKey, timeStr)
 	if len(pathArr) > 0 {
-		pathstr := compressFiles(pathArr, ExportUrl, timeStr)
+		folderPath := fmt.Sprintf("%s/%s", ExportDirectory, timeStr)
+		pathstr := compressFiles(pathArr, folderPath, ExportUrl, timeStr)
 		state := sendMail("标讯", a.Mail, pathstr)
 		BiService.Insert("export_record", map[string]interface{}{
 			"positionId": a.PositionId,
@@ -104,7 +106,7 @@ func export(table string, tableColumn []string, key []interface{}, timeStr strin
 		if a%ExportCount == 0 && a > 0 {
 			a = 0
 			//入excel处理
-			writer, file, path = Warehousing(writer, file, fileCounter, key, timeStr)
+			writer, file, path = Warehousing(writer, file, fileCounter, key, ExportDirectory, timeStr)
 			fileCounter++
 			pathArr = append(pathArr, path)
 		}
@@ -113,7 +115,7 @@ func export(table string, tableColumn []string, key []interface{}, timeStr strin
 		return true
 	}, sqlStr)
 	if a > 0 && a != ExportCount {
-		writer, file, path = Warehousing(writer, file, fileCounter, key, timeStr)
+		writer, file, path = Warehousing(writer, file, fileCounter, key, ExportDirectory, timeStr)
 		pathArr = append(pathArr, path)
 	}
 	return pathArr
@@ -132,10 +134,10 @@ func dataHandle(writer *excelize.StreamWriter, l *[]map[string]interface{}, a in
 }
 
 // 入excel
-func Warehousing(writer *excelize.StreamWriter, file *excelize.File, fileCounter int, key []interface{}, timeStr string) (*excelize.StreamWriter, *excelize.File, string) {
+func Warehousing(writer *excelize.StreamWriter, file *excelize.File, fileCounter int, key []interface{}, path, timeStr string) (*excelize.StreamWriter, *excelize.File, string) {
 	log.Println(fmt.Sprintf("开始第%d数据", fileCounter))
 	writer.Flush()
-	folderPath := fmt.Sprintf("%s/%s", ComFileDir, timeStr)
+	folderPath := fmt.Sprintf("%s/%s", path, timeStr)
 	_, err := os.Stat(folderPath)
 	if os.IsNotExist(err) {
 		//创建文件夹
@@ -188,17 +190,16 @@ func sendMail(title, target_mail string, path string) bool {
 }
 
 // 文件压缩
-func compressFiles(filePattern []string, url, timeStr string) string {
-	folderPath := fmt.Sprintf("%s/%s", ComFileDir, timeStr)
-	_, err := os.Stat(folderPath)
+func compressFiles(filePattern []string, url, path, timeStr string) string {
+	_, err := os.Stat(path)
 	if os.IsNotExist(err) {
 		//创建文件夹
-		err := os.MkdirAll(folderPath, os.ModePerm)
+		err := os.MkdirAll(path, os.ModePerm)
 		if err != nil {
 			log.Println("创建文件夹失败:", err)
 		}
 	}
-	zipFilename := fmt.Sprintf("%s/%s.zip", folderPath, timeStr)
+	zipFilename := fmt.Sprintf("%s/%s.zip", path, timeStr)
 	// 创建 ZIP 文件
 	zipFile, err := os.Create(zipFilename)
 	if err != nil {
@@ -259,9 +260,10 @@ func (e *ExportByDbReq) ExportDataByDb() []byte {
 	if stype == "" {
 		stype = "public"
 	}
-	pathArr := exportA(e.Query, stype, key)
+	folderPath := fmt.Sprintf("%s/%s/%s", ComFileDir, stype, timeStr)
+	pathArr := exportA(e.Query, folderPath, timeStr, key)
 	if len(pathArr) > 0 {
-		pathstr := compressFiles(pathArr, ComFileUrl, stype)
+		pathstr := compressFiles(pathArr, folderPath, folderPath, timeStr)
 		state := sendMailA(e.Title, e.Mails, e.Content, pathstr)
 
 		BiService.Insert("export_record", map[string]interface{}{
@@ -291,27 +293,27 @@ func hName(sql string) []interface{} {
 }
 
 // 导出开始
-func exportA(sql, timeStr string, key []interface{}) []string {
+func exportA(sql, path, timeStr string, key []interface{}) []string {
 	pathArr := []string{}
 	fileCounter := 1
 	writer, file := InirWrite(key)
 	a := 0
-	path := ""
+	fpath := ""
 	BiService.SelectByBath(1, func(l *[]map[string]interface{}) bool {
 		if a%ExportCount == 0 && a > 0 {
 			a = 0
 			//入excel处理
-			writer, file, path = Warehousing(writer, file, fileCounter, key, timeStr)
+			writer, file, fpath = Warehousing(writer, file, fileCounter, key, path, timeStr)
 			fileCounter++
-			pathArr = append(pathArr, path)
+			pathArr = append(pathArr, fpath)
 		}
 		dataHandle(writer, l, a, common.ObjArrToStringArr(key))
 		a++
 		return true
 	}, sql)
 	if a > 0 && a != ExportCount {
-		writer, file, path = Warehousing(writer, file, fileCounter, key, timeStr)
-		pathArr = append(pathArr, path)
+		writer, file, fpath = Warehousing(writer, file, fileCounter, key, path, timeStr)
+		pathArr = append(pathArr, fpath)
 	}
 	return pathArr
 }