chart.go 16 KB

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