chart.go 14 KB

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