chart.go 10 KB

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