chart.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. // 单位
  145. if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  146. chartInfo.Unit = yDataList[0].Unit
  147. chartInfo.UnitEn = yDataList[0].UnitEn
  148. }
  149. resp.ChartInfo = chartInfo
  150. resp.EdbInfoList = edbList
  151. resp.XEdbIdValue = xEdbIdValue
  152. resp.YDataList = yDataList
  153. resp.DataResp = dataResp
  154. if utils.Re == nil {
  155. data, _ := json.Marshal(resp)
  156. utils.Rc.Put(key, data, 10*time.Minute)
  157. }
  158. br.Ret = 200
  159. br.Success = true
  160. br.Msg = "获取成功"
  161. br.Data = resp
  162. }
  163. // ChartInfoRefresh
  164. // @Title 图表刷新接口
  165. // @Description 图表刷新接口
  166. // @Param UniqueCode query string true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  167. // @Success Ret=200 刷新成功
  168. // @router /refresh [get]
  169. func (this *ChartController) ChartInfoRefresh() {
  170. br := new(models.BaseResponse).Init()
  171. chartId := 0
  172. defer func() {
  173. // 添加日志
  174. if chartId > 0 {
  175. shareChartRefreshLogInfo := &models.ShareChartRefreshLog{
  176. Ip: this.Ctx.Input.IP(),
  177. ChartId: chartId,
  178. CreateTime: time.Now(),
  179. }
  180. models.AddShareChartRefreshLog(shareChartRefreshLogInfo)
  181. }
  182. this.Data["json"] = br
  183. this.ServeJSON()
  184. }()
  185. uniqueCode := this.GetString("UniqueCode")
  186. if uniqueCode == "" {
  187. br.Msg = "参数错误"
  188. br.ErrMsg = "参数错误,uniqueCode is empty"
  189. return
  190. }
  191. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  192. if err != nil {
  193. if err.Error() == utils.ErrNoRow() {
  194. br.Msg = "该图已被删除,请刷新页面"
  195. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  196. return
  197. }
  198. br.Msg = "获取失败"
  199. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  200. return
  201. }
  202. chartId = chartInfo.ChartInfoId
  203. switch chartInfo.Source {
  204. case utils.CHART_SOURCE_CROSS_HEDGING:
  205. var config request.ChartConfigReq
  206. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &config)
  207. if err != nil {
  208. br.Msg = "解析跨品种分析配置失败"
  209. br.ErrMsg = "解析跨品种分析配置失败,Err:" + err.Error()
  210. return
  211. }
  212. // 获取关联的指标信息
  213. _, _, edbInfoIdList, tmpErr := cross_variety.GetXYEdbIdList(config.TagX, config.TagY, config.VarietyList)
  214. if tmpErr != nil {
  215. br.Msg = "刷新失败,获取指标信息失败"
  216. br.ErrMsg = "刷新失败,获取指标信息失败,Err:" + tmpErr.Error()
  217. return
  218. }
  219. err, _ = data.EdbInfoRefreshAllFromBase(edbInfoIdList, false)
  220. default:
  221. err = data.ChartInfoRefreshV2(chartInfo.ChartInfoId)
  222. }
  223. if err != nil {
  224. br.Msg = "刷新失败"
  225. br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  226. return
  227. }
  228. //清除数据缓存
  229. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  230. if utils.Re == nil {
  231. utils.Rc.Delete(key)
  232. }
  233. br.Ret = 200
  234. br.Success = true
  235. br.Msg = "刷新成功"
  236. }
  237. // 获取频度的英文版
  238. func GetFrequencyEn(frequency string) (frequencyEn string) {
  239. switch frequency {
  240. case "日度":
  241. frequencyEn = "day"
  242. return
  243. case "周度":
  244. frequencyEn = "week"
  245. return
  246. case "旬度":
  247. frequencyEn = "ten days"
  248. return
  249. case "月度":
  250. frequencyEn = "month"
  251. return
  252. case "季度":
  253. frequencyEn = "quarter"
  254. return
  255. case "年度":
  256. frequencyEn = "year"
  257. return
  258. }
  259. return
  260. }
  261. func GetLeadUnitEn(unit string) (unitEn string) {
  262. switch unit {
  263. case "天":
  264. unitEn = "day"
  265. return
  266. case "周":
  267. unitEn = "week"
  268. return
  269. case "月":
  270. unitEn = "month"
  271. return
  272. case "季":
  273. unitEn = "quarter"
  274. return
  275. case "年":
  276. unitEn = "year"
  277. return
  278. }
  279. return
  280. }
  281. // GetChartInfoDetailFromUniqueCode 根据编码获取图表详情
  282. func GetChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfo, key string) (resp *models.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  283. resp = new(models.ChartInfoDetailResp)
  284. // 获取主题样式
  285. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, chartInfo.Source, chartInfo.ChartType)
  286. if err != nil {
  287. msg = "获取失败"
  288. errMsg = "获取主题信息失败,Err:" + err.Error()
  289. return
  290. }
  291. chartInfo.ChartThemeStyle = chartTheme.Config
  292. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  293. chartInfoId := chartInfo.ChartInfoId
  294. dateType := chartInfo.DateType
  295. if dateType <= 0 {
  296. dateType = 3
  297. }
  298. startDate := chartInfo.StartDate
  299. endDate := chartInfo.EndDate
  300. startYear := chartInfo.StartYear
  301. calendar := chartInfo.Calendar
  302. chartType := chartInfo.ChartType
  303. if calendar == "" {
  304. calendar = "公历"
  305. }
  306. mappingList, err := models.GetChartEdbMappingList(chartInfoId)
  307. if err != nil {
  308. msg = "获取失败"
  309. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  310. return
  311. }
  312. yearMax := 0
  313. if dateType == utils.DateTypeNYears {
  314. for _, v := range mappingList {
  315. if v.LatestDate != "" {
  316. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  317. if tErr != nil {
  318. msg = "获取失败"
  319. errMsg = "获取图表日期信息失败,Err:" + tErr.Error()
  320. return
  321. }
  322. if lastDateT.Year() > yearMax {
  323. yearMax = lastDateT.Year()
  324. }
  325. }
  326. }
  327. }
  328. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  329. extraConfigStr := chartInfo.ExtraConfig
  330. // 柱方图的一些配置
  331. var barConfig data_manage.BarChartInfoReq
  332. if chartInfo != nil && chartInfo.ChartType == 7 {
  333. if chartInfo.BarConfig == `` {
  334. msg = "柱方图未配置"
  335. errMsg = "柱方图未配置"
  336. return
  337. }
  338. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  339. if err != nil {
  340. msg = "柱方图配置异常"
  341. errMsg = "柱方图配置异常"
  342. return
  343. }
  344. extraConfigStr = chartInfo.BarConfig
  345. }
  346. edbList, xEdbIdValue, yDataList, dataResp, err, tmpErrMsg := data.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig)
  347. if err != nil {
  348. msg = "获取失败"
  349. if tmpErrMsg != `` {
  350. msg = tmpErrMsg
  351. }
  352. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  353. return
  354. }
  355. for _, v := range edbList {
  356. // 指标别名
  357. if barConfig.EdbInfoIdList != nil && len(barConfig.EdbInfoIdList) > 0 {
  358. for _, reqEdb := range barConfig.EdbInfoIdList {
  359. if v.EdbInfoId == reqEdb.EdbInfoId {
  360. v.EdbAliasName = reqEdb.Name
  361. }
  362. }
  363. }
  364. }
  365. // 图表的指标来源
  366. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  367. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  368. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  369. // 单位
  370. if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  371. chartInfo.Unit = yDataList[0].Unit
  372. chartInfo.UnitEn = yDataList[0].UnitEn
  373. }
  374. resp.ChartInfo = chartInfo
  375. resp.EdbInfoList = edbList
  376. resp.XEdbIdValue = xEdbIdValue
  377. resp.YDataList = yDataList
  378. resp.DataResp = dataResp
  379. if utils.Re == nil {
  380. jsonData, _ := json.Marshal(resp)
  381. utils.Rc.Put(key, jsonData, 10*time.Minute)
  382. }
  383. isOk = true
  384. return
  385. }