|
@@ -1,6 +1,7 @@
|
|
|
package excel
|
|
|
|
|
|
import (
|
|
|
+ "archive/zip"
|
|
|
"encoding/json"
|
|
|
"errors"
|
|
|
"eta/eta_mobile/models"
|
|
@@ -11,8 +12,12 @@ import (
|
|
|
"eta/eta_mobile/services/data"
|
|
|
"eta/eta_mobile/services/data/data_manage_permission"
|
|
|
excelService "eta/eta_mobile/services/data/excel"
|
|
|
+ excel2 "eta/eta_mobile/services/excel"
|
|
|
"eta/eta_mobile/utils"
|
|
|
"fmt"
|
|
|
+ "github.com/tealeg/xlsx"
|
|
|
+ "io/ioutil"
|
|
|
+ "os"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
@@ -450,3 +455,120 @@ func (c *ExcelInfoController) GetBalanceChartList() {
|
|
|
br.Msg = "查询成功"
|
|
|
br.Data = ret
|
|
|
}
|
|
|
+
|
|
|
+func downloadBalanceTable(excelInfo *excel.ExcelInfo, lang string) (savePath, zipName string, uploadDir string, err error, errMsg string) {
|
|
|
+ dateDir := time.Now().Format("20060102")
|
|
|
+ randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
|
|
|
+ uploadDir = "static/xls/" + dateDir + "/" + randStr
|
|
|
+ err = os.MkdirAll(uploadDir, utils.DIR_MOD)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fileList := make([]string, 0)
|
|
|
+ if excelInfo.ParentId != 0 && excelInfo.BalanceType != 0 {
|
|
|
+ errMsg = "平衡表类型错误"
|
|
|
+ err = fmt.Errorf("平衡表类型错误 ")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //复制静态表
|
|
|
+ staticCondition := " AND parent_id = 0 AND balance_type = 1 AND rel_excel_info_id=? "
|
|
|
+ var staticPars []interface{}
|
|
|
+ staticPars = append(staticPars, excelInfo.ExcelInfoId)
|
|
|
+ excelList, err := excel.GetExcelInfoListByCondition(staticCondition, staticPars)
|
|
|
+ if err != nil {
|
|
|
+ errMsg = "获取表格失败"
|
|
|
+ err = fmt.Errorf("获取表格失败 %s", err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ excelList = append(excelList, excelInfo)
|
|
|
+ for _, staticExcelInfo := range excelList {
|
|
|
+ cCondition := " AND parent_id = ?"
|
|
|
+ var cPars []interface{}
|
|
|
+ cPars = append(cPars, staticExcelInfo.ExcelInfoId)
|
|
|
+ childList, e := excel.GetExcelInfoListByCondition(cCondition, cPars)
|
|
|
+ if e != nil {
|
|
|
+ errMsg = "获取子表失败"
|
|
|
+ err = fmt.Errorf("获取子表失败 %s", err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ xlsxFile := xlsx.NewFile()
|
|
|
+ fileName := staticExcelInfo.ExcelName + ".xlsx"
|
|
|
+ fpath := uploadDir + "/" + fileName
|
|
|
+ for _, childExcelInfo := range childList {
|
|
|
+ var result request.MixedTableReq
|
|
|
+ err = json.Unmarshal([]byte(childExcelInfo.Content), &result)
|
|
|
+ if err != nil {
|
|
|
+ errMsg = "获取失败"
|
|
|
+ err = fmt.Errorf("表格json转结构体失败,Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ newResult, er, msg := excelService.GetMixedTableCellData(result, lang)
|
|
|
+ if er != nil {
|
|
|
+ err = er
|
|
|
+ errMsg = msg
|
|
|
+ return
|
|
|
+ }
|
|
|
+ tableData, er := excel2.GetTableDataByMixedTableData(newResult, false)
|
|
|
+ if er != nil {
|
|
|
+ errMsg = "获取失败"
|
|
|
+ err = fmt.Errorf("转换成table失败,Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将单个sheet的数据写入到excel
|
|
|
+ err = tableData.WriteExcelSheetData(xlsxFile, childExcelInfo.ExcelName)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //处理excel文件
|
|
|
+ //return
|
|
|
+ err = xlsxFile.Save(fpath)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fileList = append(fileList, fileName)
|
|
|
+ }
|
|
|
+ // 创建zip
|
|
|
+ zipName = excelInfo.ExcelName + ".zip"
|
|
|
+
|
|
|
+ savePath = uploadDir + "/" + zipName
|
|
|
+ fmt.Println(savePath)
|
|
|
+ zipFile, err := os.Create(savePath)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ zipWriter := zip.NewWriter(zipFile)
|
|
|
+ // 生成zip过程中报错关闭
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ zipWriter.Close()
|
|
|
+ zipFile.Close()
|
|
|
+ }
|
|
|
+ //os.Remove(savePath)
|
|
|
+ }()
|
|
|
+ //写入zip
|
|
|
+ for i := 0; i < len(fileList); i++ {
|
|
|
+ ioWriter, e := zipWriter.Create(fileList[i])
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("创建zip失败,Err:" + e.Error())
|
|
|
+ if os.IsPermission(e) {
|
|
|
+ fmt.Println("权限不足: ", e)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fullPath := uploadDir + "/" + fileList[i]
|
|
|
+ content, e := ioutil.ReadFile(fullPath)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("读取文件失败,Err:" + e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ ioWriter.Write(content)
|
|
|
+ }
|
|
|
+ // 生成zip后关闭,否则下载文件会损坏
|
|
|
+ zipWriter.Close()
|
|
|
+ zipFile.Close()
|
|
|
+ //this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
|
|
|
+ return
|
|
|
+}
|