chart.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_chart_lib/models"
  5. "eta/eta_chart_lib/models/data_manage"
  6. "eta/eta_chart_lib/models/data_manage/cross_variety/request"
  7. "eta/eta_chart_lib/services/data"
  8. "eta/eta_chart_lib/services/data/cross_variety"
  9. "eta/eta_chart_lib/utils"
  10. "fmt"
  11. "strings"
  12. "time"
  13. )
  14. // 图表
  15. type ChartController struct {
  16. BaseAuthController
  17. }
  18. // ChartInfoDetail
  19. // @Title 获取图表详情
  20. // @Description 获取图表详情接口
  21. // @Param UniqueCode query string true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  22. // @Success 200 {object} data_manage.ChartInfoDetailResp
  23. // @router /detail [get]
  24. func (this *ChartController) ChartInfoDetail() {
  25. br := new(models.BaseResponse).Init()
  26. defer func() {
  27. this.Data["json"] = br
  28. this.ServeJSON()
  29. }()
  30. uniqueCode := this.GetString("UniqueCode")
  31. if uniqueCode == "" {
  32. br.Msg = "参数错误"
  33. br.ErrMsg = "参数错误,uniqueCode is empty"
  34. return
  35. }
  36. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  37. resp := new(models.ChartInfoDetailResp)
  38. //判断是否有缓存
  39. if utils.Re == nil {
  40. if utils.Re == nil && utils.Rc.IsExist(key) {
  41. if data, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  42. err := json.Unmarshal(data, &resp)
  43. if err == nil && resp != nil {
  44. br.Ret = 200
  45. br.Success = true
  46. br.Msg = "获取成功"
  47. br.Data = resp
  48. fmt.Println("source redis")
  49. return
  50. }
  51. }
  52. }
  53. }
  54. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  55. if err != nil {
  56. if err.Error() == utils.ErrNoRow() {
  57. br.Msg = "该图已被删除,请刷新页面"
  58. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  59. return
  60. }
  61. br.Msg = "获取失败"
  62. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  63. return
  64. }
  65. chartInfoId := chartInfo.ChartInfoId
  66. dateType := chartInfo.DateType
  67. if dateType <= 0 {
  68. dateType = 3
  69. }
  70. startDate := chartInfo.StartDate
  71. endDate := chartInfo.EndDate
  72. //seasonStartDate := chartInfo.SeasonStartDate
  73. //seasonEndDate := chartInfo.SeasonEndDate
  74. calendar := chartInfo.Calendar
  75. chartType := chartInfo.ChartType
  76. startYear := chartInfo.StartYear
  77. if calendar == "" {
  78. calendar = "公历"
  79. }
  80. mappingList, err := models.GetChartEdbMappingList(chartInfoId)
  81. if err != nil {
  82. br.Msg = "获取失败"
  83. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  84. return
  85. }
  86. yearMax := 0
  87. if dateType == utils.DateTypeNYears {
  88. for _, v := range mappingList {
  89. if v.LatestDate != "" {
  90. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  91. if tErr != nil {
  92. br.Msg = "获取失败"
  93. br.ErrMsg = "获取图表日期信息失败,Err:" + tErr.Error()
  94. return
  95. }
  96. if lastDateT.Year() > yearMax {
  97. yearMax = lastDateT.Year()
  98. }
  99. }
  100. }
  101. }
  102. // 开始/结束日期
  103. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  104. extraConfigStr := chartInfo.ExtraConfig
  105. // 柱方图的一些配置
  106. var barConfig data_manage.BarChartInfoReq
  107. if chartInfo != nil && chartInfo.ChartType == 7 {
  108. if chartInfo.BarConfig == `` {
  109. br.Msg = "柱方图未配置"
  110. br.ErrMsg = "柱方图未配置"
  111. return
  112. }
  113. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  114. if err != nil {
  115. br.Msg = "柱方图配置异常"
  116. br.ErrMsg = "柱方图配置异常"
  117. return
  118. }
  119. extraConfigStr = chartInfo.BarConfig
  120. }
  121. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig)
  122. if err != nil {
  123. br.Msg = "获取失败"
  124. if errMsg != `` {
  125. br.Msg = errMsg
  126. }
  127. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  128. return
  129. }
  130. for _, v := range edbList {
  131. // 指标别名
  132. if barConfig.EdbInfoIdList != nil && len(barConfig.EdbInfoIdList) > 0 {
  133. for _, reqEdb := range barConfig.EdbInfoIdList {
  134. if v.EdbInfoId == reqEdb.EdbInfoId {
  135. v.EdbAliasName = reqEdb.Name
  136. }
  137. }
  138. }
  139. }
  140. // 图表的指标来源
  141. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  142. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  143. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  144. resp.ChartInfo = chartInfo
  145. resp.EdbInfoList = edbList
  146. resp.XEdbIdValue = xEdbIdValue
  147. resp.YDataList = yDataList
  148. resp.DataResp = dataResp
  149. if utils.Re == nil {
  150. data, _ := json.Marshal(resp)
  151. utils.Rc.Put(key, data, 10*time.Minute)
  152. }
  153. br.Ret = 200
  154. br.Success = true
  155. br.Msg = "获取成功"
  156. br.Data = resp
  157. }
  158. // ChartInfoRefresh
  159. // @Title 图表刷新接口
  160. // @Description 图表刷新接口
  161. // @Param UniqueCode query string true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  162. // @Success Ret=200 刷新成功
  163. // @router /refresh [get]
  164. func (this *ChartController) ChartInfoRefresh() {
  165. br := new(models.BaseResponse).Init()
  166. chartId := 0
  167. defer func() {
  168. // 添加日志
  169. if chartId > 0 {
  170. shareChartRefreshLogInfo := &models.ShareChartRefreshLog{
  171. Ip: this.Ctx.Input.IP(),
  172. ChartId: chartId,
  173. CreateTime: time.Now(),
  174. }
  175. models.AddShareChartRefreshLog(shareChartRefreshLogInfo)
  176. }
  177. this.Data["json"] = br
  178. this.ServeJSON()
  179. }()
  180. uniqueCode := this.GetString("UniqueCode")
  181. if uniqueCode == "" {
  182. br.Msg = "参数错误"
  183. br.ErrMsg = "参数错误,uniqueCode is empty"
  184. return
  185. }
  186. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  187. if err != nil {
  188. if err.Error() == utils.ErrNoRow() {
  189. br.Msg = "该图已被删除,请刷新页面"
  190. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  191. return
  192. }
  193. br.Msg = "获取失败"
  194. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  195. return
  196. }
  197. chartId = chartInfo.ChartInfoId
  198. switch chartInfo.Source {
  199. case utils.CHART_SOURCE_CROSS_HEDGING:
  200. var config request.ChartConfigReq
  201. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &config)
  202. if err != nil {
  203. br.Msg = "解析跨品种分析配置失败"
  204. br.ErrMsg = "解析跨品种分析配置失败,Err:" + err.Error()
  205. return
  206. }
  207. // 获取关联的指标信息
  208. _, _, edbInfoIdList, tmpErr := cross_variety.GetXYEdbIdList(config.TagX, config.TagY, config.VarietyList)
  209. if tmpErr != nil {
  210. br.Msg = "刷新失败,获取指标信息失败"
  211. br.ErrMsg = "刷新失败,获取指标信息失败,Err:" + tmpErr.Error()
  212. return
  213. }
  214. err, _ = data.EdbInfoRefreshAllFromBase(edbInfoIdList, false)
  215. default:
  216. err = data.ChartInfoRefreshV2(chartInfo.ChartInfoId)
  217. }
  218. if err != nil {
  219. br.Msg = "刷新失败"
  220. br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  221. return
  222. }
  223. //清除数据缓存
  224. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  225. if utils.Re == nil {
  226. utils.Rc.Delete(key)
  227. }
  228. br.Ret = 200
  229. br.Success = true
  230. br.Msg = "刷新成功"
  231. }
  232. // 获取频度的英文版
  233. func GetFrequencyEn(frequency string) (frequencyEn string) {
  234. switch frequency {
  235. case "日度":
  236. frequencyEn = "day"
  237. return
  238. case "周度":
  239. frequencyEn = "week"
  240. return
  241. case "旬度":
  242. frequencyEn = "ten days"
  243. return
  244. case "月度":
  245. frequencyEn = "month"
  246. return
  247. case "季度":
  248. frequencyEn = "quarter"
  249. return
  250. case "年度":
  251. frequencyEn = "year"
  252. return
  253. }
  254. return
  255. }
  256. func GetLeadUnitEn(unit string) (unitEn string) {
  257. switch unit {
  258. case "天":
  259. unitEn = "day"
  260. return
  261. case "周":
  262. unitEn = "week"
  263. return
  264. case "月":
  265. unitEn = "month"
  266. return
  267. case "季":
  268. unitEn = "quarter"
  269. return
  270. case "年":
  271. unitEn = "year"
  272. return
  273. }
  274. return
  275. }
  276. // GetChartInfoDetailFromUniqueCode 根据编码获取图表详情
  277. func GetChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfo, key string) (resp *models.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  278. resp = new(models.ChartInfoDetailResp)
  279. // 获取主题样式
  280. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, chartInfo.Source, chartInfo.ChartType)
  281. if err != nil {
  282. msg = "获取失败"
  283. errMsg = "获取主题信息失败,Err:" + err.Error()
  284. return
  285. }
  286. chartInfo.ChartThemeStyle = chartTheme.Config
  287. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  288. chartInfoId := chartInfo.ChartInfoId
  289. dateType := chartInfo.DateType
  290. if dateType <= 0 {
  291. dateType = 3
  292. }
  293. startDate := chartInfo.StartDate
  294. endDate := chartInfo.EndDate
  295. startYear := chartInfo.StartYear
  296. calendar := chartInfo.Calendar
  297. chartType := chartInfo.ChartType
  298. if calendar == "" {
  299. calendar = "公历"
  300. }
  301. mappingList, err := models.GetChartEdbMappingList(chartInfoId)
  302. if err != nil {
  303. msg = "获取失败"
  304. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  305. return
  306. }
  307. yearMax := 0
  308. if dateType == utils.DateTypeNYears {
  309. for _, v := range mappingList {
  310. if v.LatestDate != "" {
  311. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  312. if tErr != nil {
  313. msg = "获取失败"
  314. errMsg = "获取图表日期信息失败,Err:" + tErr.Error()
  315. return
  316. }
  317. if lastDateT.Year() > yearMax {
  318. yearMax = lastDateT.Year()
  319. }
  320. }
  321. }
  322. }
  323. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  324. extraConfigStr := chartInfo.ExtraConfig
  325. // 柱方图的一些配置
  326. var barConfig data_manage.BarChartInfoReq
  327. if chartInfo != nil && chartInfo.ChartType == 7 {
  328. if chartInfo.BarConfig == `` {
  329. msg = "柱方图未配置"
  330. errMsg = "柱方图未配置"
  331. return
  332. }
  333. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  334. if err != nil {
  335. msg = "柱方图配置异常"
  336. errMsg = "柱方图配置异常"
  337. return
  338. }
  339. extraConfigStr = chartInfo.BarConfig
  340. }
  341. edbList, xEdbIdValue, yDataList, dataResp, err, tmpErrMsg := data.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig)
  342. if err != nil {
  343. msg = "获取失败"
  344. if tmpErrMsg != `` {
  345. msg = tmpErrMsg
  346. }
  347. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  348. return
  349. }
  350. for _, v := range edbList {
  351. // 指标别名
  352. if barConfig.EdbInfoIdList != nil && len(barConfig.EdbInfoIdList) > 0 {
  353. for _, reqEdb := range barConfig.EdbInfoIdList {
  354. if v.EdbInfoId == reqEdb.EdbInfoId {
  355. v.EdbAliasName = reqEdb.Name
  356. }
  357. }
  358. }
  359. }
  360. // 图表的指标来源
  361. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  362. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  363. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  364. resp.ChartInfo = chartInfo
  365. resp.EdbInfoList = edbList
  366. resp.XEdbIdValue = xEdbIdValue
  367. resp.YDataList = yDataList
  368. resp.DataResp = dataResp
  369. if utils.Re == nil {
  370. jsonData, _ := json.Marshal(resp)
  371. utils.Rc.Put(key, jsonData, 10*time.Minute)
  372. }
  373. isOk = true
  374. return
  375. }