chart.go 16 KB

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