123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- 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 嘉悦指标
- type JiaYueIndexController struct{}
- // GetIndex
- // @Description 获取嘉悦指标信息
- // @Success 200 {string} string "获取成功"
- // @Router /jiayue/index [post]
- func (j *JiaYueIndexController) GetIndex(c *gin.Context) {
- var req indexDataReq.JiaYueIndexReq
- 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
- }
- if req.IndexCode == "" {
- resp.FailMsg("请输入指标code码", "请输入指标code码", c)
- return
- }
- if req.SourceExtend == "" {
- resp.FailMsg("请输入指标来源", "请输入指标来源", c)
- return
- }
- sourceArr := strings.Split(req.SourceExtend, ",")
- if len(sourceArr) == 0 {
- resp.FailMsg("指标来源有误", "指标来源有误", c)
- return
- }
- data, e := indexDataService.GetIndexFromJiaYue(req.IndexCode, sourceArr, req.IndexCodeRequired)
- if e != nil {
- resp.FailMsg("获取失败", "获取嘉悦指标信息失败, err: "+e.Error(), c)
- return
- }
- resp.OkData("获取成功", data, c)
- return
- }
- // GetIndexData
- // @Description 获取嘉悦指标数据
- // @Success 200 {string} string "获取成功"
- // @Router /jiayue/index_data [post]
- func (j *JiaYueIndexController) GetIndexData(c *gin.Context) {
- var req indexDataReq.JiaYueIndexAndDataReq
- 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
- }
- if req.IndexCode == "" {
- resp.FailMsg("请输入指标code码", "请输入指标code码", c)
- return
- }
- if req.SourceExtend == "" {
- resp.FailMsg("请输入指标来源", "请输入指标来源", c)
- return
- }
- sourceArr := strings.Split(req.SourceExtend, ",")
- if len(sourceArr) == 0 {
- resp.FailMsg("指标来源有误", "指标来源有误", c)
- return
- }
- data, e := indexDataService.GetIndexAndDataFromJiaYue(req.IndexCode, req.StartDate, req.EndDate, sourceArr, req.IndexCodeRequired)
- if e != nil {
- resp.FailMsg("获取失败", "获取嘉悦指标数据失败, err: "+e.Error(), c)
- return
- }
- resp.OkData("获取成功", data, c)
- return
- }
- // GetPageIndex
- // @Description 获取嘉悦指标列表-分页
- // @Success 200 {string} string "获取成功"
- // @Router /jiayue/page_index [post]
- func (j *JiaYueIndexController) GetPageIndex(c *gin.Context) {
- var req indexDataReq.JiaYuePageIndexReq
- 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
- }
- if req.SourceExtend == "" {
- resp.FailMsg("请输入指标来源", "请输入指标来源", c)
- return
- }
- sourceArr := strings.Split(req.SourceExtend, ",")
- if len(sourceArr) == 0 {
- resp.FailMsg("指标来源有误", "指标来源有误", c)
- return
- }
- total, list, err := indexDataService.GetPageIndexesFromJiaYue(req.PageIndex, req.PageSize, sourceArr, req.Keyword, req.Frequency, req.SortField, req.SortRule)
- if err != nil {
- resp.FailMsg("查询失败", err.Error(), c)
- return
- }
- var data response.JiaYuePageIndexResp
- data.Total = total
- data.List = list
- resp.OkData("操作成功", data, c)
- return
- }
- // GetIndexFrequency
- // @Description 获取指标频度列表
- // @Success 200 {string} string "获取成功"
- // @Router /jiayue/frequency_list [post]
- func (j *JiaYueIndexController) GetIndexFrequency(c *gin.Context) {
- list, err := jiayue.GetDictFrequency()
- if err != nil {
- resp.FailMsg("获取失败", err.Error(), c)
- return
- }
- // 过滤一下空的和斜杠
- data := make([]string, 0)
- for _, v := range list {
- if v != "" && v != "/" {
- data = append(data, v)
- }
- }
- resp.OkData("获取成功", data, c)
- return
- }
- // GetRecentNewIndex
- // @Description 获取最近新增的指标数据
- // @Success 200 {string} string "获取成功"
- // @Router /jiayue/new_index [post]
- func (j *JiaYueIndexController) GetRecentNewIndex(c *gin.Context) {
- data, e := indexDataService.GetNewIndexAndDataFromJiaYue()
- if e != nil {
- resp.FailMsg("获取失败", "获取嘉悦最近新增指标数据失败, err: "+e.Error(), c)
- return
- }
- resp.OkData("获取成功", data, c)
- }
- // GetMenuList
- // @Description 获取指标目录列表
- // @Success 200 {string} string "获取成功"
- // @Router /jiayue/menu_list [post]
- func (j *JiaYueIndexController) GetMenuList(c *gin.Context) {
- categories, e := jiayue.GetDictCategory(``, make([]interface{}, 0), "")
- if e != nil {
- resp.FailMsg("获取失败", "获取嘉业指标目录列表, err: "+e.Error(), c)
- return
- }
- data := make([]*response.IndexMenuData, 0)
- for _, v := range categories {
- data = append(data, indexDataService.FormatJiaYueDictCategory2Resp(v))
- }
- 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)
- }
|