jiayue_index.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package index_data
  2. import (
  3. "bytes"
  4. "eta/eta_bridge/controller/resp"
  5. "eta/eta_bridge/global"
  6. "eta/eta_bridge/models/jiayue"
  7. indexDataReq "eta/eta_bridge/models/request/index_data"
  8. "eta/eta_bridge/models/response"
  9. indexDataService "eta/eta_bridge/services/index_data"
  10. "eta/eta_bridge/utils"
  11. "fmt"
  12. "github.com/gin-gonic/gin"
  13. "github.com/go-playground/validator/v10"
  14. "github.com/tealeg/xlsx"
  15. "net/http"
  16. "os"
  17. "path/filepath"
  18. "strings"
  19. "time"
  20. )
  21. // JiaYueIndexController 嘉悦指标
  22. type JiaYueIndexController struct{}
  23. // GetIndex
  24. // @Description 获取嘉悦指标信息
  25. // @Success 200 {string} string "获取成功"
  26. // @Router /jiayue/index [post]
  27. func (j *JiaYueIndexController) GetIndex(c *gin.Context) {
  28. var req indexDataReq.JiaYueIndexReq
  29. if e := c.Bind(&req); e != nil {
  30. err, ok := e.(validator.ValidationErrors)
  31. if !ok {
  32. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  33. return
  34. }
  35. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  36. return
  37. }
  38. if req.IndexCode == "" {
  39. resp.FailMsg("请输入指标code码", "请输入指标code码", c)
  40. return
  41. }
  42. if req.SourceExtend == "" {
  43. resp.FailMsg("请输入指标来源", "请输入指标来源", c)
  44. return
  45. }
  46. sourceArr := strings.Split(req.SourceExtend, ",")
  47. if len(sourceArr) == 0 {
  48. resp.FailMsg("指标来源有误", "指标来源有误", c)
  49. return
  50. }
  51. data, e := indexDataService.GetIndexFromJiaYue(req.IndexCode, sourceArr, req.IndexCodeRequired)
  52. if e != nil {
  53. resp.FailMsg("获取失败", "获取嘉悦指标信息失败, err: "+e.Error(), c)
  54. return
  55. }
  56. resp.OkData("获取成功", data, c)
  57. return
  58. }
  59. // GetIndexData
  60. // @Description 获取嘉悦指标数据
  61. // @Success 200 {string} string "获取成功"
  62. // @Router /jiayue/index_data [post]
  63. func (j *JiaYueIndexController) GetIndexData(c *gin.Context) {
  64. var req indexDataReq.JiaYueIndexAndDataReq
  65. if e := c.Bind(&req); e != nil {
  66. err, ok := e.(validator.ValidationErrors)
  67. if !ok {
  68. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  69. return
  70. }
  71. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  72. return
  73. }
  74. if req.IndexCode == "" {
  75. resp.FailMsg("请输入指标code码", "请输入指标code码", c)
  76. return
  77. }
  78. if req.SourceExtend == "" {
  79. resp.FailMsg("请输入指标来源", "请输入指标来源", c)
  80. return
  81. }
  82. sourceArr := strings.Split(req.SourceExtend, ",")
  83. if len(sourceArr) == 0 {
  84. resp.FailMsg("指标来源有误", "指标来源有误", c)
  85. return
  86. }
  87. data, e := indexDataService.GetIndexAndDataFromJiaYue(req.IndexCode, req.StartDate, req.EndDate, sourceArr, req.IndexCodeRequired)
  88. if e != nil {
  89. resp.FailMsg("获取失败", "获取嘉悦指标数据失败, err: "+e.Error(), c)
  90. return
  91. }
  92. resp.OkData("获取成功", data, c)
  93. return
  94. }
  95. // GetPageIndex
  96. // @Description 获取嘉悦指标列表-分页
  97. // @Success 200 {string} string "获取成功"
  98. // @Router /jiayue/page_index [post]
  99. func (j *JiaYueIndexController) GetPageIndex(c *gin.Context) {
  100. var req indexDataReq.JiaYuePageIndexReq
  101. if e := c.Bind(&req); e != nil {
  102. err, ok := e.(validator.ValidationErrors)
  103. if !ok {
  104. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  105. return
  106. }
  107. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  108. return
  109. }
  110. if req.SourceExtend == "" {
  111. resp.FailMsg("请输入指标来源", "请输入指标来源", c)
  112. return
  113. }
  114. sourceArr := strings.Split(req.SourceExtend, ",")
  115. if len(sourceArr) == 0 {
  116. resp.FailMsg("指标来源有误", "指标来源有误", c)
  117. return
  118. }
  119. total, list, err := indexDataService.GetPageIndexesFromJiaYue(req.PageIndex, req.PageSize, sourceArr, req.Keyword, req.Frequency, req.SortField, req.SortRule)
  120. if err != nil {
  121. resp.FailMsg("查询失败", err.Error(), c)
  122. return
  123. }
  124. var data response.JiaYuePageIndexResp
  125. data.Total = total
  126. data.List = list
  127. resp.OkData("操作成功", data, c)
  128. return
  129. }
  130. // GetIndexFrequency
  131. // @Description 获取指标频度列表
  132. // @Success 200 {string} string "获取成功"
  133. // @Router /jiayue/frequency_list [post]
  134. func (j *JiaYueIndexController) GetIndexFrequency(c *gin.Context) {
  135. list, err := jiayue.GetDictFrequency()
  136. if err != nil {
  137. resp.FailMsg("获取失败", err.Error(), c)
  138. return
  139. }
  140. // 过滤一下空的和斜杠
  141. data := make([]string, 0)
  142. for _, v := range list {
  143. if v != "" && v != "/" {
  144. data = append(data, v)
  145. }
  146. }
  147. resp.OkData("获取成功", data, c)
  148. return
  149. }
  150. // GetRecentNewIndex
  151. // @Description 获取最近新增的指标数据
  152. // @Success 200 {string} string "获取成功"
  153. // @Router /jiayue/new_index [post]
  154. func (j *JiaYueIndexController) GetRecentNewIndex(c *gin.Context) {
  155. data, e := indexDataService.GetNewIndexAndDataFromJiaYue()
  156. if e != nil {
  157. resp.FailMsg("获取失败", "获取嘉悦最近新增指标数据失败, err: "+e.Error(), c)
  158. return
  159. }
  160. resp.OkData("获取成功", data, c)
  161. }
  162. // GetMenuList
  163. // @Description 获取指标目录列表
  164. // @Success 200 {string} string "获取成功"
  165. // @Router /jiayue/menu_list [post]
  166. func (j *JiaYueIndexController) GetMenuList(c *gin.Context) {
  167. categories, e := jiayue.GetDictCategory(``, make([]interface{}, 0), "")
  168. if e != nil {
  169. resp.FailMsg("获取失败", "获取嘉业指标目录列表, err: "+e.Error(), c)
  170. return
  171. }
  172. data := make([]*response.IndexMenuData, 0)
  173. for _, v := range categories {
  174. data = append(data, indexDataService.FormatJiaYueDictCategory2Resp(v))
  175. }
  176. resp.OkData("获取成功", data, c)
  177. }
  178. // BuildImportExcel 生成初始化excel文件
  179. func (j *JiaYueIndexController) BuildImportExcel(c *gin.Context) {
  180. var req indexDataReq.JiaYueImportDataReq
  181. if e := c.Bind(&req); e != nil {
  182. err, ok := e.(validator.ValidationErrors)
  183. if !ok {
  184. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  185. return
  186. }
  187. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  188. return
  189. }
  190. sourceArr := make([]string, 0)
  191. if req.SourceExtend != "" {
  192. sourceArr = strings.Split(req.SourceExtend, ",")
  193. }
  194. // 获取初始化的指标
  195. dir, _ := os.Executable()
  196. dirPath := filepath.Dir(dir)
  197. downloadPath := dirPath + "/static/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  198. xlsxFile := xlsx.NewFile()
  199. sheet, e := xlsxFile.AddSheet("Sheet1")
  200. if e != nil {
  201. resp.FailMsg("新增Sheet失败", "新增Sheet失败, err: "+e.Error(), c)
  202. return
  203. }
  204. // 指标数据
  205. list, e := indexDataService.GetJiaYueImportData(sourceArr)
  206. if e != nil {
  207. resp.FailMsg("获取初始化指标数据失败", "获取导出指标数据失败, err: "+e.Error(), c)
  208. return
  209. }
  210. // 表头
  211. firstTitle := []string{"一级目录", "二级目录", "三级目录", "四级目录", "五级目录", "六级目录", "ID", "指标名称", "指标编码", "单位", "频度", "来源"}
  212. firstRow := sheet.AddRow()
  213. for _, v := range firstTitle {
  214. firstRow.AddCell().SetString(v)
  215. }
  216. // 数据行
  217. for _, v := range list {
  218. row := sheet.AddRow()
  219. row.AddCell().SetValue(v.FirstMenu)
  220. row.AddCell().SetValue(v.SecondMenu)
  221. row.AddCell().SetValue(v.ThirdMenu)
  222. row.AddCell().SetValue(v.FourthMenu)
  223. row.AddCell().SetValue(v.FifthMenu)
  224. row.AddCell().SetValue(v.SixthMenu)
  225. row.AddCell().SetValue(v.Id)
  226. row.AddCell().SetValue(v.IndexName)
  227. row.AddCell().SetValue(v.IndexCode)
  228. row.AddCell().SetValue(v.Unit)
  229. row.AddCell().SetValue(v.Frequency)
  230. row.AddCell().SetValue(v.SourceType)
  231. }
  232. if e = xlsxFile.Save(downloadPath); e != nil {
  233. resp.FailMsg("保存失败", "保存文件失败, err: "+e.Error(), c)
  234. return
  235. }
  236. defer func() {
  237. _ = os.Remove(downloadPath)
  238. }()
  239. var buffer bytes.Buffer
  240. _ = xlsxFile.Write(&buffer)
  241. content := bytes.NewReader(buffer.Bytes())
  242. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, downloadPath))
  243. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  244. http.ServeContent(c.Writer, c.Request, downloadPath, time.Now(), content)
  245. }