|
@@ -1,15 +1,23 @@
|
|
|
package index_data
|
|
|
|
|
|
import (
|
|
|
+ "bytes"
|
|
|
"eta/eta_bridge/controller/resp"
|
|
|
"eta/eta_bridge/global"
|
|
|
"eta/eta_bridge/models/jiayue"
|
|
|
indexDataReq "eta/eta_bridge/models/request/index_data"
|
|
|
"eta/eta_bridge/models/response"
|
|
|
indexDataService "eta/eta_bridge/services/index_data"
|
|
|
+ "eta/eta_bridge/utils"
|
|
|
+ "fmt"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
+ "github.com/tealeg/xlsx"
|
|
|
+ "net/http"
|
|
|
+ "os"
|
|
|
+ "path/filepath"
|
|
|
"strings"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
// JiaYueIndexController 嘉悦指标
|
|
@@ -178,3 +186,79 @@ func (j *JiaYueIndexController) GetMenuList(c *gin.Context) {
|
|
|
}
|
|
|
resp.OkData("获取成功", data, c)
|
|
|
}
|
|
|
+
|
|
|
+// BuildImportExcel 生成初始化excel文件
|
|
|
+func (j *JiaYueIndexController) BuildImportExcel(c *gin.Context) {
|
|
|
+ var req indexDataReq.JiaYueImportDataReq
|
|
|
+ if e := c.Bind(&req); e != nil {
|
|
|
+ err, ok := e.(validator.ValidationErrors)
|
|
|
+ if !ok {
|
|
|
+ resp.FailData("参数解析失败", "Err:"+e.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ resp.FailData("参数解析失败", err.Translate(global.Trans), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ sourceArr := make([]string, 0)
|
|
|
+ if req.SourceExtend != "" {
|
|
|
+ sourceArr = strings.Split(req.SourceExtend, ",")
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取初始化的指标
|
|
|
+ dir, _ := os.Executable()
|
|
|
+ dirPath := filepath.Dir(dir)
|
|
|
+ downloadPath := dirPath + "/static/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
|
|
|
+ xlsxFile := xlsx.NewFile()
|
|
|
+ sheet, e := xlsxFile.AddSheet("Sheet1")
|
|
|
+ if e != nil {
|
|
|
+ resp.FailMsg("新增Sheet失败", "新增Sheet失败, err: "+e.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 指标数据
|
|
|
+ list, e := indexDataService.GetJiaYueImportData(sourceArr)
|
|
|
+ if e != nil {
|
|
|
+ resp.FailMsg("获取初始化指标数据失败", "获取导出指标数据失败, err: "+e.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 表头
|
|
|
+ firstTitle := []string{"一级目录", "二级目录", "三级目录", "四级目录", "五级目录", "六级目录", "ID", "指标名称", "指标编码", "单位", "频度", "来源"}
|
|
|
+ firstRow := sheet.AddRow()
|
|
|
+ for _, v := range firstTitle {
|
|
|
+ firstRow.AddCell().SetString(v)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 数据行
|
|
|
+ for _, v := range list {
|
|
|
+ row := sheet.AddRow()
|
|
|
+ row.AddCell().SetValue(v.FirstMenu)
|
|
|
+ row.AddCell().SetValue(v.SecondMenu)
|
|
|
+ row.AddCell().SetValue(v.ThirdMenu)
|
|
|
+ row.AddCell().SetValue(v.FourthMenu)
|
|
|
+ row.AddCell().SetValue(v.FifthMenu)
|
|
|
+ row.AddCell().SetValue(v.SixthMenu)
|
|
|
+ row.AddCell().SetValue(v.Id)
|
|
|
+ row.AddCell().SetValue(v.IndexName)
|
|
|
+ row.AddCell().SetValue(v.IndexCode)
|
|
|
+ row.AddCell().SetValue(v.Unit)
|
|
|
+ row.AddCell().SetValue(v.Frequency)
|
|
|
+ row.AddCell().SetValue(v.SourceType)
|
|
|
+ }
|
|
|
+
|
|
|
+ if e = xlsxFile.Save(downloadPath); e != nil {
|
|
|
+ resp.FailMsg("保存失败", "保存文件失败, err: "+e.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //defer func() {
|
|
|
+ // _ = os.Remove(downloadPath)
|
|
|
+ //}()
|
|
|
+
|
|
|
+ var buffer bytes.Buffer
|
|
|
+ _ = xlsxFile.Write(&buffer)
|
|
|
+ content := bytes.NewReader(buffer.Bytes())
|
|
|
+
|
|
|
+ c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, downloadPath))
|
|
|
+ c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
|
|
+ http.ServeContent(c.Writer, c.Request, downloadPath, time.Now(), content)
|
|
|
+}
|