chart.go 11 KB

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