chart.go 13 KB

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