chart.go 15 KB

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