浏览代码

test 初始化excel

hsun 1 年之前
父节点
当前提交
c4d1c6365d

+ 84 - 0
controller/index_data/jiayue_index.go

@@ -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)
+}

+ 2 - 8
go.mod

@@ -18,6 +18,7 @@ require (
 	github.com/rdlucklib/rdluck_tools v1.0.3
 	github.com/spf13/viper v1.16.0
 	github.com/swaggo/swag v1.16.1
+	github.com/tealeg/xlsx v1.0.5
 	golang.org/x/image v0.9.0
 	gorm.io/driver/mysql v1.5.1
 	gorm.io/gorm v1.25.2
@@ -27,7 +28,6 @@ require (
 	github.com/KyleBanks/depth v1.2.1 // indirect
 	github.com/PuerkitoBio/purell v1.1.1 // indirect
 	github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
-	github.com/beorn7/perks v1.0.1 // indirect
 	github.com/bytedance/sonic v1.9.1 // indirect
 	github.com/cespare/xxhash/v2 v2.2.0 // indirect
 	github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
@@ -43,8 +43,6 @@ require (
 	github.com/go-sql-driver/mysql v1.7.0 // indirect
 	github.com/goccy/go-json v0.10.2 // indirect
 	github.com/godror/knownpb v0.1.1 // indirect
-	github.com/golang/protobuf v1.5.3 // indirect
-	github.com/hashicorp/golang-lru v0.5.4 // indirect
 	github.com/hashicorp/hcl v1.0.0 // indirect
 	github.com/jinzhu/inflection v1.0.0 // indirect
 	github.com/jinzhu/now v1.1.5 // indirect
@@ -57,16 +55,11 @@ require (
 	github.com/magiconair/properties v1.8.7 // indirect
 	github.com/mailru/easyjson v0.7.6 // indirect
 	github.com/mattn/go-isatty v0.0.19 // indirect
-	github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
 	github.com/mitchellh/mapstructure v1.5.0 // indirect
 	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
 	github.com/modern-go/reflect2 v1.0.2 // indirect
 	github.com/pelletier/go-toml/v2 v2.0.8 // indirect
 	github.com/pkg/errors v0.9.1 // indirect
-	github.com/prometheus/client_golang v1.16.0 // indirect
-	github.com/prometheus/client_model v0.3.0 // indirect
-	github.com/prometheus/common v0.42.0 // indirect
-	github.com/prometheus/procfs v0.10.1 // indirect
 	github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18 // indirect
 	github.com/spf13/afero v1.9.5 // indirect
 	github.com/spf13/cast v1.5.1 // indirect
@@ -83,6 +76,7 @@ require (
 	golang.org/x/text v0.13.0 // indirect
 	golang.org/x/tools v0.13.0 // indirect
 	google.golang.org/protobuf v1.30.0 // indirect
+	gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
 	gopkg.in/ini.v1 v1.67.0 // indirect
 	gopkg.in/yaml.v2 v2.4.0 // indirect
 	gopkg.in/yaml.v3 v3.0.1 // indirect

+ 4 - 0
models/request/index_data/jiayue_index.go

@@ -23,3 +23,7 @@ type JiaYuePageIndexReq struct {
 	SortField    int    `json:"sort_field" form:"sort_field" description:"排序字段: 1-指标开始时间; 2-指标最新时间; 3-更新时间"`
 	SortRule     int    `json:"sort_rule" form:"sort_rule" description:"排序方式: 1-正序; 2-倒序"`
 }
+
+type JiaYueImportDataReq struct {
+	SourceExtend string `json:"source_extend" form:"source_extend" description:"来源"`
+}

+ 1 - 0
routers/index_data.go

@@ -16,4 +16,5 @@ func InitIndexData(r *gin.RouterGroup) {
 	group.POST("jiayue/frequency_list", control.GetIndexFrequency)
 	group.POST("jiayue/new_index", control.GetRecentNewIndex)
 	group.POST("jiayue/menu_list", control.GetMenuList)
+	group.POST("jiayue/build_import_excel", control.BuildImportExcel)
 }

+ 125 - 0
services/index_data/jiayue_platform.go

@@ -300,3 +300,128 @@ func FormatJiaYueDictCategory2Resp(item jiayue.DictCategory) (res *response.Inde
 	res.Path = item.Path
 	return
 }
+
+type JiaYueImportIndexResp struct {
+	FirstMenu  string `description:"一级目录"`
+	SecondMenu string `description:"二级目录"`
+	ThirdMenu  string `description:"三级目录"`
+	FourthMenu string `description:"四级目录"`
+	FifthMenu  string `description:"五级目录"`
+	SixthMenu  string `description:"六级目录"`
+	Id         int    `description:"指标ID"`
+	IndexName  string `description:"指标名称"`
+	IndexCode  string `description:"指标编码"`
+	Unit       string `description:"单位"`
+	Frequency  string `description:"频度"`
+	SourceType string `description:"来源"`
+}
+
+func GetJiaYueImportData(sourceArr []string) (items []JiaYueImportIndexResp, err error) {
+	defer func() {
+		if err != nil {
+			global.LOG.Info("GetJiaYueImportData Err: " + err.Error())
+			go alarm_msg.SendAlarmMsg("GetJiaYueImportData Err: "+err.Error(), 3)
+			return
+		}
+	}()
+
+	// 获取指标
+	indexCond := ``
+	indexPars := make([]interface{}, 0)
+	if len(sourceArr) == 0 {
+		sourceArr = []string{"webisite", "website", "website_gf", "platts", "reuter", "reuter_vessel_q", "路透", "bloomberg", "bloomberg_tmp", "wind", "wind_p", "wind_stop", "wind_tmp"}
+	}
+	indexCond += ` AND SOURCE_TYPE IN (:1)`
+	indexPars = append(indexPars, sourceArr)
+	indexes, e := jiayue.GetDictIndex(indexCond, indexPars, "SOURCE_TYPE ASC, ID ASC")
+	if e != nil {
+		err = fmt.Errorf("GetDictIndex err: %s", e.Error())
+		return
+	}
+	if len(indexes) <= 0 {
+		global.LOG.Info("无指标需要导入")
+		return
+	}
+	index := indexes[0]
+	if index.Id <= 0 {
+		err = fmt.Errorf("指标信息有误")
+		return
+	}
+	if index.TableName == "" {
+		err = fmt.Errorf("指标信息数据表名为空")
+		return
+	}
+
+	// 获取全部目录
+	categories, e := jiayue.GetDictCategory(``, make([]interface{}, 0), "")
+	if e != nil {
+		err = fmt.Errorf("获取嘉业指标目录列表失败, err: %s", e.Error())
+		return
+	}
+	categoryMap := make(map[int]jiayue.DictCategory)
+	for _, v := range categories {
+		categoryMap[v.Id] = v
+	}
+
+	items = make([]JiaYueImportIndexResp, 0)
+	for _, v := range indexes {
+		var t JiaYueImportIndexResp
+		t.Id = v.Id
+		t.IndexName = v.Name
+		t.IndexCode = v.Code
+		t.Unit = v.Unit
+		t.Frequency = v.Frequency
+		t.SourceType = v.SourceType
+
+		// 目录
+		menuCond := ` R.INDEX_ID = :1`
+		menuPars := make([]interface{}, 0)
+		menuPars = append(menuPars, t.Id)
+		menus, e := jiayue.GetIndexCategory(menuCond, menuPars, "")
+		if e != nil {
+			err = fmt.Errorf("GetDictCategory err: %s", e.Error())
+			return
+		}
+		if len(menus) == 0 {
+			continue
+		}
+		menu := menus[0]
+		path := strings.TrimLeft(menu.Path, "/") // "path": "/61293/67576/67577/67579/67583"
+		menuIds := make([]int, 0)
+		menuIdArr := strings.Split(path, "/")
+		for _, m := range menuIdArr {
+			mid, _ := strconv.Atoi(m)
+			menuIds = append(menuIds, mid)
+		}
+		menuIds = append(menuIds, menu.Id)
+
+		// 取出目录名称
+		for k, m := range menuIds {
+			mu := categoryMap[m]
+			if mu.Id <= 0 {
+				continue
+			}
+			if k == 0 {
+				t.FirstMenu = mu.Name
+			}
+			if k == 1 {
+				t.SecondMenu = mu.Name
+			}
+			if k == 2 {
+				t.ThirdMenu = mu.Name
+			}
+			if k == 3 {
+				t.FourthMenu = mu.Name
+			}
+			if k == 4 {
+				t.FifthMenu = mu.Name
+			}
+			if k == 5 {
+				t.SixthMenu = mu.Name
+			}
+		}
+
+		items = append(items, t)
+	}
+	return
+}