chart.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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/services/data/range_analysis"
  11. dwmini "eta/eta_chart_lib/services/dw_mini"
  12. "eta/eta_chart_lib/utils"
  13. "fmt"
  14. "strings"
  15. "time"
  16. )
  17. // 图表
  18. type ChartController struct {
  19. BaseAuthController
  20. }
  21. // ChartInfoDetail
  22. // @Title 获取图表详情
  23. // @Description 获取图表详情接口
  24. // @Param UniqueCode query string true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  25. // @Success 200 {object} data_manage.ChartInfoDetailResp
  26. // @router /detail [get]
  27. func (this *ChartController) ChartInfoDetail() {
  28. br := new(models.BaseResponse).Init()
  29. defer func() {
  30. this.Data["json"] = br
  31. this.ServeJSON()
  32. }()
  33. uniqueCode := this.GetString("UniqueCode")
  34. token := this.GetString("Token")
  35. source, _ := this.GetInt("Source")
  36. if uniqueCode == "" {
  37. br.Msg = "参数错误"
  38. br.ErrMsg = "参数错误,uniqueCode is empty"
  39. return
  40. }
  41. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  42. resp := new(models.ChartInfoDetailResp)
  43. // 图表水印
  44. conf, e := models.GetBusinessConf()
  45. if e != nil {
  46. br.Msg = "获取失败"
  47. br.ErrMsg = "获取配置信息失败, Err: " + e.Error()
  48. return
  49. }
  50. var isCollect bool
  51. if source == utils.CHART_SOURCE_DW && token != "" {
  52. tmpIsCollect, err := dwmini.GetMyChartIsCollect(token, uniqueCode)
  53. if err != nil {
  54. br.Msg = "获取失败"
  55. br.ErrMsg = "获取收藏状态失败,Err:" + err.Error()
  56. return
  57. }
  58. isCollect = tmpIsCollect
  59. }
  60. //判断是否有缓存
  61. if utils.Re == nil {
  62. if utils.Re == nil && utils.Rc.IsExist(key) {
  63. if data, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  64. err := json.Unmarshal(data, &resp)
  65. if err == nil && resp != nil {
  66. if conf[models.BusinessConfWatermarkChart] == "true" && conf[models.BusinessConfCompanyWatermark] != "" {
  67. resp.WaterMark = conf[models.BusinessConfCompanyWatermark]
  68. }
  69. if isCollect {
  70. resp.IsCollect = isCollect
  71. }
  72. br.Ret = 200
  73. br.Success = true
  74. br.Msg = "获取成功"
  75. br.Data = resp
  76. fmt.Println("source redis")
  77. return
  78. }
  79. }
  80. }
  81. }
  82. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  83. if err != nil {
  84. if err.Error() == utils.ErrNoRow() {
  85. br.Msg = "该图已被删除,请刷新页面"
  86. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  87. return
  88. }
  89. br.Msg = "获取失败"
  90. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  91. return
  92. }
  93. //var resp interface{}
  94. var isOk bool
  95. var msg, errMsg string
  96. switch chartInfo.Source {
  97. case utils.CHART_SOURCE_DEFAULT:
  98. resp, isOk, msg, errMsg = GetChartInfoDetailFromUniqueCode(chartInfo, key)
  99. case utils.CHART_SOURCE_FUTURE_GOOD:
  100. resp, isOk, msg, errMsg = GetFutureGoodChartInfoDetailFromUniqueCode(chartInfo, key)
  101. case utils.CHART_SOURCE_FUTURE_GOOD_PROFIT:
  102. resp, isOk, msg, errMsg = GetFutureGoodProfitChartInfoDetailFromUniqueCode(chartInfo, key)
  103. case utils.CHART_SOURCE_CORRELATION, utils.CHART_SOURCE_ROLLING_CORRELATION:
  104. resp, isOk, msg, errMsg = GetCorrelationChartInfoDetailFromUniqueCode(chartInfo, key)
  105. case utils.CHART_SOURCE_LINE_EQUATION:
  106. resp, isOk, msg, errMsg = GetLineEquationChartInfoDetailFromUniqueCode(chartInfo, key)
  107. case utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE, utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY:
  108. resp, isOk, msg, errMsg = GetLineFeatureChartInfoDetailFromUniqueCode(chartInfo, key)
  109. case utils.CHART_SOURCE_CROSS_HEDGING:
  110. resp, isOk, msg, errMsg = GetCrossVarietyChartInfoDetailFromUniqueCode(chartInfo, key)
  111. case utils.CHART_SOURCE_BALANCE_EXCEL:
  112. resp, isOk, msg, errMsg = GetBalanceChartInfoDetailFromUniqueCode(chartInfo, key, this.Lang)
  113. if !isOk {
  114. br.Msg = msg
  115. br.ErrMsg = errMsg
  116. return
  117. }
  118. case utils.CHART_SOURCE_RANGE_ANALYSIS:
  119. resp, isOk, msg, errMsg = GetRangeAnalysisChartInfoDetailFromUniqueCode(chartInfo, key, this.Lang)
  120. if !isOk {
  121. br.Msg = msg
  122. br.ErrMsg = errMsg
  123. return
  124. }
  125. default:
  126. br.Msg = "错误的图表"
  127. br.ErrMsg = "错误的图表"
  128. return
  129. }
  130. if !isOk {
  131. br.Msg = msg
  132. br.ErrMsg = errMsg
  133. return
  134. }
  135. if isCollect {
  136. resp.IsCollect = isCollect
  137. }
  138. if conf[models.BusinessConfWatermarkChart] == "true" && conf[models.BusinessConfCompanyWatermark] != "" {
  139. resp.WaterMark = conf[models.BusinessConfCompanyWatermark]
  140. }
  141. br.Ret = 200
  142. br.Success = true
  143. br.Msg = "获取成功"
  144. br.Data = resp
  145. }
  146. // ChartInfoRefresh
  147. // @Title 图表刷新接口
  148. // @Description 图表刷新接口
  149. // @Param UniqueCode query string true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  150. // @Success Ret=200 刷新成功
  151. // @router /refresh [get]
  152. func (this *ChartController) ChartInfoRefresh() {
  153. br := new(models.BaseResponse).Init()
  154. chartId := 0
  155. defer func() {
  156. // 添加日志
  157. if chartId > 0 {
  158. shareChartRefreshLogInfo := &models.ShareChartRefreshLog{
  159. Ip: this.Ctx.Input.IP(),
  160. ChartId: chartId,
  161. CreateTime: time.Now(),
  162. }
  163. models.AddShareChartRefreshLog(shareChartRefreshLogInfo)
  164. }
  165. this.Data["json"] = br
  166. this.ServeJSON()
  167. }()
  168. uniqueCode := this.GetString("UniqueCode")
  169. if uniqueCode == "" {
  170. br.Msg = "参数错误"
  171. br.ErrMsg = "参数错误,uniqueCode is empty"
  172. return
  173. }
  174. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  175. if err != nil {
  176. if err.Error() == utils.ErrNoRow() {
  177. br.Msg = "该图已被删除,请刷新页面"
  178. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  179. return
  180. }
  181. br.Msg = "获取失败"
  182. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  183. return
  184. }
  185. chartId = chartInfo.ChartInfoId
  186. switch chartInfo.Source {
  187. case utils.CHART_SOURCE_CROSS_HEDGING:
  188. var config request.ChartConfigReq
  189. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &config)
  190. if err != nil {
  191. br.Msg = "解析跨品种分析配置失败"
  192. br.ErrMsg = "解析跨品种分析配置失败,Err:" + err.Error()
  193. return
  194. }
  195. // 获取关联的指标信息
  196. _, _, edbInfoIdList, tmpErr := cross_variety.GetXYEdbIdList(config.TagX, config.TagY, config.VarietyList)
  197. if tmpErr != nil {
  198. br.Msg = "刷新失败,获取指标信息失败"
  199. br.ErrMsg = "刷新失败,获取指标信息失败,Err:" + tmpErr.Error()
  200. return
  201. }
  202. err, _ = data.EdbInfoRefreshAllFromBase(edbInfoIdList, false)
  203. case utils.CHART_SOURCE_BALANCE_EXCEL:
  204. excelDetail, err := excel.GetExcelInfoByChartInfoId(chartInfo.ChartInfoId)
  205. if err != nil {
  206. br.Msg = "图表数据异常"
  207. br.ErrMsg = "未找到对应的表格" + err.Error()
  208. br.IsSendEmail = false
  209. return
  210. }
  211. err = refreshBalanceTable(excelDetail, this.Lang)
  212. if err != nil {
  213. br.Msg = "刷新失败"
  214. br.ErrMsg = "刷新失败,Err:" + err.Error()
  215. return
  216. }
  217. case utils.CHART_SOURCE_RANGE_ANALYSIS:
  218. //// 刷新相关性图表
  219. if _, e := range_analysis.ChartInfoRefresh(chartInfo.ChartInfoId, ""); e != nil {
  220. br.Msg = "刷新失败"
  221. br.ErrMsg = "刷新相关性图表失败, Err:" + e.Error()
  222. return
  223. }
  224. default:
  225. err = data.ChartInfoRefreshV2(chartInfo.ChartInfoId)
  226. }
  227. if err != nil {
  228. br.Msg = "刷新失败"
  229. br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  230. return
  231. }
  232. //清除数据缓存
  233. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  234. if utils.Re == nil {
  235. utils.Rc.Delete(key)
  236. }
  237. br.Ret = 200
  238. br.Success = true
  239. br.Msg = "刷新成功"
  240. }
  241. // CollectCancel
  242. // @Title 东吴小程序图表取消收藏接口
  243. // @Description 东吴小程序图表取消收藏接口
  244. // @Param request body models.ChartDwCollectReq true "type json string"
  245. // @Success Ret=200 取消收藏成功
  246. // @router /dw/collectCancel [post]
  247. func (this *ChartController) CollectCancel() {
  248. br := new(models.BaseResponse).Init()
  249. defer func() {
  250. this.Data["json"] = br
  251. this.ServeJSON()
  252. }()
  253. var req models.ChartDwCollectReq
  254. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  255. br.Msg = "参数错误"
  256. br.ErrMsg = "参数错误,Err:" + err.Error()
  257. return
  258. }
  259. if req.UniqueCode == "" {
  260. br.Msg = "参数错误"
  261. br.ErrMsg = "参数错误,UniqueCode is empty"
  262. return
  263. }
  264. if req.Token == "" {
  265. br.Msg = "参数错误"
  266. br.ErrMsg = "参数错误,Token is empty"
  267. return
  268. }
  269. result, err := dwmini.MyChartCollectCancel(req.Token, req.UniqueCode)
  270. if err != nil {
  271. br.Msg = "取消收藏失败"
  272. br.ErrMsg = "取消收藏失败,Err:" + err.Error()
  273. return
  274. }
  275. if result.Ret != 200 {
  276. br.Msg = result.Msg
  277. br.ErrMsg = "取消收藏失败,Err:" + result.ErrMsg
  278. return
  279. }
  280. br.Ret = 200
  281. br.Success = true
  282. br.Msg = "取消收藏成功"
  283. }
  284. // Collect
  285. // @Title 东吴小程序图表收藏接口
  286. // @Description 东吴小程序图表收藏接口
  287. // @Param request body models.ChartDwCollectReq true "type json string"
  288. // @Success Ret=200 收藏成功
  289. // @router /dw/collect [post]
  290. func (this *ChartController) Collect() {
  291. br := new(models.BaseResponse).Init()
  292. defer func() {
  293. this.Data["json"] = br
  294. this.ServeJSON()
  295. }()
  296. var req models.ChartDwCollectReq
  297. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  298. br.Msg = "参数错误"
  299. br.ErrMsg = "参数错误,Err:" + err.Error()
  300. return
  301. }
  302. if req.UniqueCode == "" {
  303. br.Msg = "参数错误"
  304. br.ErrMsg = "参数错误,UniqueCode is empty"
  305. return
  306. }
  307. if req.Token == "" {
  308. br.Msg = "参数错误"
  309. br.ErrMsg = "参数错误,Token is empty"
  310. return
  311. }
  312. chartInfo, err := models.GetChartInfoByUniqueCode(req.UniqueCode)
  313. if err != nil {
  314. if err.Error() == utils.ErrNoRow() {
  315. br.Msg = "该图已被删除,请刷新页面"
  316. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  317. return
  318. }
  319. br.Msg = "获取失败"
  320. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  321. return
  322. }
  323. result, err := dwmini.MyChartCollect(req.Token, req.UniqueCode, chartInfo.ChartName, chartInfo.ChartImage, chartInfo.ChartInfoId)
  324. if err != nil {
  325. br.Msg = "收藏失败"
  326. br.ErrMsg = "收藏失败,Err:" + err.Error()
  327. return
  328. }
  329. if result.Ret != 200 {
  330. br.Msg = result.Msg
  331. br.ErrMsg = "收藏失败,Err:" + result.ErrMsg
  332. return
  333. }
  334. br.Ret = 200
  335. br.Success = true
  336. br.Msg = "收藏成功"
  337. }
  338. // 获取频度的英文版
  339. func GetFrequencyEn(frequency string) (frequencyEn string) {
  340. switch frequency {
  341. case "日度":
  342. frequencyEn = "day"
  343. return
  344. case "周度":
  345. frequencyEn = "week"
  346. return
  347. case "旬度":
  348. frequencyEn = "ten days"
  349. return
  350. case "月度":
  351. frequencyEn = "month"
  352. return
  353. case "季度":
  354. frequencyEn = "quarter"
  355. return
  356. case "年度":
  357. frequencyEn = "year"
  358. return
  359. }
  360. return
  361. }
  362. func GetLeadUnitEn(unit string) (unitEn string) {
  363. switch unit {
  364. case "天":
  365. unitEn = "day"
  366. return
  367. case "周":
  368. unitEn = "week"
  369. return
  370. case "月":
  371. unitEn = "month"
  372. return
  373. case "季":
  374. unitEn = "quarter"
  375. return
  376. case "年":
  377. unitEn = "year"
  378. return
  379. }
  380. return
  381. }
  382. // GetChartInfoDetailFromUniqueCode 根据编码获取图表详情
  383. func GetChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfo, key string) (resp *models.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  384. resp = new(models.ChartInfoDetailResp)
  385. // 获取主题样式
  386. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, chartInfo.Source, chartInfo.ChartType)
  387. if err != nil {
  388. msg = "获取失败"
  389. errMsg = "获取主题信息失败,Err:" + err.Error()
  390. return
  391. }
  392. chartInfo.ChartThemeStyle = chartTheme.Config
  393. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  394. chartInfoId := chartInfo.ChartInfoId
  395. dateType := chartInfo.DateType
  396. if dateType <= 0 {
  397. dateType = 3
  398. }
  399. startDate := chartInfo.StartDate
  400. endDate := chartInfo.EndDate
  401. startYear := chartInfo.StartYear
  402. calendar := chartInfo.Calendar
  403. chartType := chartInfo.ChartType
  404. if calendar == "" {
  405. calendar = "公历"
  406. }
  407. mappingList, err := models.GetChartEdbMappingList(chartInfoId)
  408. if err != nil {
  409. msg = "获取失败"
  410. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  411. return
  412. }
  413. var dateMax time.Time
  414. if dateType == utils.DateTypeNYears {
  415. for _, v := range mappingList {
  416. if v.LatestDate != "" {
  417. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  418. if tErr != nil {
  419. msg = "获取失败"
  420. errMsg = "获取图表日期信息失败,Err:" + tErr.Error()
  421. return
  422. }
  423. if lastDateT.After(dateMax) {
  424. dateMax = lastDateT
  425. }
  426. }
  427. }
  428. }
  429. if chartInfo.DateType == utils.DateTypeNYears && chartInfo.ChartType == utils.CHART_TYPE_SEASON {
  430. // 季节性图表,要特殊处理起始日期, 最近N年
  431. dateMax = time.Date(dateMax.Year()+1, 1, 1, 0, 0, 0, 0, time.Local)
  432. }
  433. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, dateMax)
  434. extraConfigStr := chartInfo.ExtraConfig
  435. // 柱方图的一些配置
  436. var barConfig data_manage.BarChartInfoReq
  437. if chartInfo != nil && chartInfo.ChartType == 7 {
  438. if chartInfo.BarConfig == `` {
  439. msg = "柱方图未配置"
  440. errMsg = "柱方图未配置"
  441. return
  442. }
  443. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  444. if err != nil {
  445. msg = "柱方图配置异常"
  446. errMsg = "柱方图配置异常"
  447. return
  448. }
  449. extraConfigStr = chartInfo.BarConfig
  450. }
  451. edbList, xEdbIdValue, yDataList, dataResp, err, tmpErrMsg := data.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig)
  452. if err != nil {
  453. msg = "获取失败"
  454. if tmpErrMsg != `` {
  455. msg = tmpErrMsg
  456. }
  457. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  458. return
  459. }
  460. for _, v := range edbList {
  461. // 指标别名
  462. if barConfig.EdbInfoIdList != nil && len(barConfig.EdbInfoIdList) > 0 {
  463. for _, reqEdb := range barConfig.EdbInfoIdList {
  464. if v.EdbInfoId == reqEdb.EdbInfoId {
  465. v.EdbAliasName = reqEdb.Name
  466. }
  467. }
  468. }
  469. }
  470. // 图表的指标来源
  471. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  472. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  473. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  474. resp.ChartInfo = chartInfo
  475. resp.EdbInfoList = edbList
  476. resp.XEdbIdValue = xEdbIdValue
  477. resp.YDataList = yDataList
  478. resp.DataResp = dataResp
  479. if utils.Re == nil {
  480. jsonData, _ := json.Marshal(resp)
  481. utils.Rc.Put(key, jsonData, 10*time.Minute)
  482. }
  483. isOk = true
  484. return
  485. }