chart.go 14 KB

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