chart_common.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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. cross_varietyReq "eta_gn/eta_chart_lib/models/data_manage/cross_variety/request"
  7. "eta_gn/eta_chart_lib/models/data_manage/future_good"
  8. "eta_gn/eta_chart_lib/models/data_manage/future_good/request"
  9. "eta_gn/eta_chart_lib/models/data_manage/future_good/response"
  10. line_equationReq "eta_gn/eta_chart_lib/models/data_manage/line_equation/request"
  11. line_featureReq "eta_gn/eta_chart_lib/models/data_manage/line_feature/request"
  12. "eta_gn/eta_chart_lib/services/data"
  13. correlationServ "eta_gn/eta_chart_lib/services/data/correlation"
  14. "eta_gn/eta_chart_lib/services/data/cross_variety"
  15. "eta_gn/eta_chart_lib/services/data/excel"
  16. future_goodServ "eta_gn/eta_chart_lib/services/data/future_good"
  17. "eta_gn/eta_chart_lib/services/data/line_equation"
  18. lineFeatureServ "eta_gn/eta_chart_lib/services/data/line_feature"
  19. "eta_gn/eta_chart_lib/services/data/range_analysis"
  20. dwmini "eta_gn/eta_chart_lib/services/dw_mini"
  21. "eta_gn/eta_chart_lib/utils"
  22. "fmt"
  23. "strconv"
  24. "strings"
  25. "time"
  26. )
  27. // CommonChartInfoDetailFromUniqueCode
  28. // @Title 根据编码获取图表详情
  29. // @Description 根据编码获取图表详情接口
  30. // @Param UniqueCode query int true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  31. // @Param IsCache query bool true "是否走缓存,默认false"
  32. // @Param Token query string true "东吴小程序token"
  33. // @Param Source query int true "查询来源 1:东吴"
  34. // @Success 200 {object} data_manage.ChartInfoDetailFromUniqueCodeResp
  35. // @router /common/detail [get]
  36. func (this *ChartController) CommonChartInfoDetailFromUniqueCode() {
  37. br := new(models.BaseResponse).Init()
  38. defer func() {
  39. this.Data["json"] = br
  40. this.ServeJSON()
  41. }()
  42. uniqueCode := this.GetString("UniqueCode")
  43. token := this.GetString("Token")
  44. source, _ := this.GetInt("Source")
  45. if uniqueCode == "" {
  46. br.Msg = "参数错误"
  47. br.ErrMsg = "参数错误,uniqueCode is empty"
  48. return
  49. }
  50. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  51. resp := new(models.ChartInfoDetailResp)
  52. // 图表水印
  53. conf, e := models.GetBusinessConf()
  54. if e != nil {
  55. br.Msg = "获取失败"
  56. br.ErrMsg = "获取配置信息失败, Err: " + e.Error()
  57. return
  58. }
  59. var isCollect bool
  60. if source == utils.CHART_SOURCE_DW && token != "" {
  61. tmpIsCollect, err := dwmini.GetMyChartIsCollect(token, uniqueCode)
  62. if err != nil {
  63. br.Msg = "获取失败"
  64. br.ErrMsg = "获取收藏状态失败,Err:" + err.Error()
  65. return
  66. }
  67. isCollect = tmpIsCollect
  68. }
  69. //判断是否有缓存
  70. if utils.Re == nil {
  71. if utils.Re == nil && utils.Rc.IsExist(key) {
  72. if data, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  73. err := json.Unmarshal(data, &resp)
  74. if err == nil && resp != nil {
  75. if conf[models.BusinessConfWatermarkChart] == "true" && conf[models.BusinessConfCompanyWatermark] != "" {
  76. resp.WaterMark = conf[models.BusinessConfCompanyWatermark]
  77. }
  78. if isCollect {
  79. resp.IsCollect = isCollect
  80. }
  81. br.Ret = 200
  82. br.Success = true
  83. br.Msg = "获取成功"
  84. br.Data = resp
  85. fmt.Println("source redis")
  86. return
  87. }
  88. }
  89. }
  90. }
  91. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  92. if err != nil {
  93. if utils.IsErrNoRow(err) {
  94. br.Msg = "该图已被删除,请刷新页面"
  95. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  96. return
  97. }
  98. br.Msg = "获取失败"
  99. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  100. return
  101. }
  102. //var resp interface{}
  103. var isOk bool
  104. var msg, errMsg string
  105. switch chartInfo.Source {
  106. case utils.CHART_SOURCE_DEFAULT:
  107. resp, isOk, msg, errMsg = GetChartInfoDetailFromUniqueCode(chartInfo, key)
  108. case utils.CHART_SOURCE_FUTURE_GOOD:
  109. resp, isOk, msg, errMsg = GetFutureGoodChartInfoDetailFromUniqueCode(chartInfo, key)
  110. case utils.CHART_SOURCE_FUTURE_GOOD_PROFIT:
  111. resp, isOk, msg, errMsg = GetFutureGoodProfitChartInfoDetailFromUniqueCode(chartInfo, key)
  112. case utils.CHART_SOURCE_CORRELATION, utils.CHART_SOURCE_ROLLING_CORRELATION:
  113. resp, isOk, msg, errMsg = GetCorrelationChartInfoDetailFromUniqueCode(chartInfo, key)
  114. case utils.CHART_SOURCE_LINE_EQUATION:
  115. resp, isOk, msg, errMsg = GetLineEquationChartInfoDetailFromUniqueCode(chartInfo, key)
  116. case utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE, utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY:
  117. resp, isOk, msg, errMsg = GetLineFeatureChartInfoDetailFromUniqueCode(chartInfo, key)
  118. case utils.CHART_SOURCE_CROSS_HEDGING:
  119. resp, isOk, msg, errMsg = GetCrossVarietyChartInfoDetailFromUniqueCode(chartInfo, key)
  120. case utils.CHART_SOURCE_BALANCE_EXCEL:
  121. resp, isOk, msg, errMsg = GetBalanceChartInfoDetailFromUniqueCode(chartInfo, key, this.Lang)
  122. if !isOk {
  123. br.Msg = msg
  124. br.ErrMsg = errMsg
  125. return
  126. }
  127. case utils.CHART_SOURCE_RANGE_ANALYSIS:
  128. resp, isOk, msg, errMsg = GetRangeAnalysisChartInfoDetailFromUniqueCode(chartInfo, key, this.Lang)
  129. if !isOk {
  130. br.Msg = msg
  131. br.ErrMsg = errMsg
  132. return
  133. }
  134. default:
  135. br.Msg = "错误的图表"
  136. br.ErrMsg = "错误的图表"
  137. return
  138. }
  139. if !isOk {
  140. br.Msg = msg
  141. br.ErrMsg = errMsg
  142. return
  143. }
  144. if isCollect {
  145. resp.IsCollect = isCollect
  146. }
  147. if conf[models.BusinessConfWatermarkChart] == "true" && conf[models.BusinessConfCompanyWatermark] != "" {
  148. resp.WaterMark = conf[models.BusinessConfCompanyWatermark]
  149. }
  150. br.Ret = 200
  151. br.Success = true
  152. br.Msg = "获取成功"
  153. br.Data = resp
  154. }
  155. // GetFutureGoodChartInfoDetailFromUniqueCode 根据编码获取图表详情
  156. func GetFutureGoodChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfo, key string) (resp *models.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  157. resp = new(models.ChartInfoDetailResp)
  158. // 获取主题样式
  159. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, 1)
  160. if err != nil {
  161. msg = "获取失败"
  162. errMsg = "获取主题信息失败,Err:" + err.Error()
  163. return
  164. }
  165. chartInfo.ChartThemeStyle = chartTheme.Config
  166. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  167. chartInfoId := chartInfo.ChartInfoId
  168. startDate := chartInfo.StartDate
  169. endDate := chartInfo.EndDate
  170. // 兼容日期错误
  171. {
  172. if strings.Count(startDate, "-") == 1 {
  173. startDate = startDate + "-01"
  174. }
  175. if strings.Count(endDate, "-") == 1 {
  176. endDate = endDate + "-01"
  177. }
  178. }
  179. edbInfoMappingList, err := models.GetEtaEdbChartEdbMappingList(chartInfoId)
  180. if err != nil {
  181. msg = "获取失败"
  182. errMsg = "获取图表,现货指标信息失败,Err:" + err.Error()
  183. return
  184. }
  185. futureGoodEdbInfoMapping, err := models.GetFutureGoodEdbChartEdbMapping(chartInfoId)
  186. if err != nil {
  187. msg = "获取失败"
  188. errMsg = "获取图表的期货商品指标信息失败,Err:" + err.Error()
  189. return
  190. }
  191. // 商品价格曲线图的一些配置
  192. var barConfig data_manage.FutureGoodBarChartInfoReq
  193. //barChartInfoSort := data_manage.BarChartInfoSortReq{}
  194. if chartInfo.BarConfig == `` {
  195. msg = "商品价格曲线图未配置"
  196. errMsg = "商品价格曲线图未配置"
  197. return
  198. }
  199. err = json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  200. if err != nil {
  201. msg = "商品价格曲线图配置异常"
  202. errMsg = "商品价格曲线图配置异常"
  203. return
  204. }
  205. baseEdbInfoId := barConfig.BaseEdbInfoId
  206. var baseEdbInfoMapping *models.ChartEdbInfoMapping
  207. // todo 兼容历史数据,
  208. if baseEdbInfoId == 0 {
  209. // 默认取第一个现货指标
  210. baseEdbInfoId = edbInfoMappingList[0].EdbInfoId
  211. baseEdbInfoMapping = edbInfoMappingList[0]
  212. barConfig.BaseEdbInfoId = baseEdbInfoId
  213. } else {
  214. baseEdbInfoMapping, err = models.GetChartEdbMappingByEdbInfoId(baseEdbInfoId)
  215. if err != nil {
  216. msg = "获取失败"
  217. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  218. return
  219. }
  220. }
  221. // 获取图表中的指标数据
  222. barConfigEdbInfoIdList, edbList, xEdbIdValue, xDataList, yDataList, err := future_goodServ.GetChartEdbData(chartInfoId, startDate, endDate, baseEdbInfoMapping, edbInfoMappingList, futureGoodEdbInfoMapping, barConfig, true)
  223. if err != nil {
  224. msg = "获取失败"
  225. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  226. return
  227. }
  228. warnEdbList := make([]string, 0)
  229. if len(edbList) <= 0 {
  230. msg = "商品价格曲线图表指标异常"
  231. errMsg = "商品价格曲线图表异常"
  232. return
  233. }
  234. baseEdbInfo := edbList[0] //现货指标
  235. for _, v := range edbList {
  236. if v.IsNullData {
  237. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  238. }
  239. // 指标别名
  240. if barConfigEdbInfoIdList != nil && len(barConfigEdbInfoIdList) > 0 {
  241. for _, reqEdb := range barConfigEdbInfoIdList {
  242. if v.EdbInfoId == reqEdb.EdbInfoId {
  243. v.EdbAliasName = reqEdb.Name
  244. v.EdbAliasNameEn = reqEdb.NameEn
  245. }
  246. }
  247. }
  248. }
  249. chartInfo.UnitEn = baseEdbInfo.UnitEn
  250. // 图表的指标来源
  251. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  252. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  253. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  254. xDataListFinal := make([]models.XData, 0)
  255. for _, v := range xDataList {
  256. tmp := models.XData{
  257. Name: v.Name,
  258. NameEn: v.NameEn,
  259. IsHide: v.IsHide,
  260. }
  261. xDataListFinal = append(xDataListFinal, tmp)
  262. }
  263. resp.ChartInfo = chartInfo
  264. resp.EdbInfoList = edbList
  265. resp.XEdbIdValue = xEdbIdValue
  266. resp.YDataList = yDataList
  267. resp.XDataList = xDataListFinal
  268. //resp.BarChartInfo = barConfig
  269. //resp.Status = true
  270. // 将数据加入缓存
  271. if utils.Re == nil {
  272. data, _ := json.Marshal(resp)
  273. utils.Rc.Put(key, data, 2*time.Hour)
  274. }
  275. isOk = true
  276. return
  277. }
  278. // GetFutureGoodProfitChartInfoDetailFromUniqueCode 根据编码获取商品利润图表详情
  279. func GetFutureGoodProfitChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfo, key string) (resp *models.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  280. resp = new(models.ChartInfoDetailResp)
  281. chartInfoId := chartInfo.ChartInfoId
  282. // 获取主题样式
  283. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, 1)
  284. if err != nil {
  285. msg = "获取失败"
  286. errMsg = "获取主题信息失败,Err:" + err.Error()
  287. return
  288. }
  289. chartInfo.ChartThemeStyle = chartTheme.Config
  290. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  291. // 商品利润曲线图的一些配置
  292. var extraConf request.ChartInfoReq
  293. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConf)
  294. if err != nil {
  295. msg = "商品利润曲线图配置异常"
  296. errMsg = "商品利润曲线图配置异常,Err:" + err.Error()
  297. return
  298. }
  299. if len(extraConf.EdbInfoIdList) == 0 {
  300. extraConf.EdbInfoIdList = append(extraConf.EdbInfoIdList, extraConf.BaseEdbInfoId)
  301. }
  302. edbList := make([]*models.ChartEdbInfoMapping, 0)
  303. edbInfoMappingList, err := models.GetEtaEdbChartEdbMappingList(chartInfoId)
  304. if err != nil {
  305. msg = "获取失败"
  306. errMsg = "获取商品利润图表,基础指标信息失败,Err:" + err.Error()
  307. return
  308. }
  309. baseEdbInfo := new(models.ChartEdbInfoMapping)
  310. for _, v := range edbInfoMappingList {
  311. if v.EdbInfoId == extraConf.BaseEdbInfoId {
  312. baseEdbInfo = v
  313. }
  314. }
  315. edbList = edbInfoMappingList
  316. futureGoodEdbInfoMappingList, err := models.GetFutureGoodEdbChartEdbMappingList(chartInfoId)
  317. if err != nil {
  318. msg = "获取失败"
  319. errMsg = "获取商品利润图表,商品指标信息失败,Err:" + err.Error()
  320. return
  321. }
  322. edbList = append(edbList, futureGoodEdbInfoMappingList...)
  323. if len(edbList) <= 0 {
  324. msg = "商品利润曲线图表指标异常"
  325. errMsg = "商品利润曲线图表指标异常"
  326. return
  327. }
  328. xDataList := make([]models.XData, 0)
  329. yDataList := make([]models.YData, 0)
  330. // 查找商品利润图表的扩展信息
  331. chartInfoFutureGoodProfit := new(future_good.ChartInfoFutureGoodProfit)
  332. if err = chartInfoFutureGoodProfit.GetItemById(chartInfo.ChartInfoId); err != nil {
  333. msg = "获取失败"
  334. errMsg = "获取基础相关性图表信息失败, Err: " + err.Error()
  335. return
  336. }
  337. err = json.Unmarshal([]byte(chartInfoFutureGoodProfit.XValue), &xDataList)
  338. if err != nil {
  339. msg = "获取失败"
  340. errMsg = "转换X轴数据失败, Err: " + err.Error()
  341. return
  342. }
  343. err = json.Unmarshal([]byte(chartInfoFutureGoodProfit.YValue), &yDataList)
  344. if err != nil {
  345. msg = "获取失败"
  346. errMsg = "转换Y轴数据失败, Err: " + err.Error()
  347. return
  348. }
  349. warnEdbList := make([]string, 0)
  350. for _, v := range edbList {
  351. if v.IsNullData {
  352. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  353. }
  354. }
  355. chartInfo.UnitEn = baseEdbInfo.UnitEn
  356. resp.ChartInfo = chartInfo
  357. resp.EdbInfoList = edbList
  358. resp.DataResp = response.ProfitFutureGoodChartResp{
  359. YDataList: yDataList,
  360. XDataList: xDataList,
  361. ProfitName: chartInfoFutureGoodProfit.ProfitName,
  362. ProfitNameEn: chartInfoFutureGoodProfit.ProfitNameEn,
  363. Extra: extraConf,
  364. }
  365. // 将数据加入缓存
  366. if utils.Re == nil {
  367. data, _ := json.Marshal(resp)
  368. utils.Rc.Put(key, data, 2*time.Hour)
  369. }
  370. isOk = true
  371. return
  372. }
  373. // GetLineEquationChartInfoDetailFromUniqueCode 根据编码获取拟合方程图表详情
  374. func GetLineEquationChartInfoDetailFromUniqueCode(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, utils.CHART_SOURCE_DEFAULT, 1)
  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. if chartInfo.ExtraConfig == `` {
  387. msg = "获取失败"
  388. errMsg = "获取配置信息失败"
  389. return
  390. }
  391. var lineChartInfoConfig line_equationReq.LineChartInfoReq
  392. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &lineChartInfoConfig)
  393. if err != nil {
  394. msg = "获取失败"
  395. errMsg = "获取图表配置信息失败, Err:" + err.Error()
  396. return
  397. }
  398. var getAData, getBData, getR2Data bool
  399. switch lineChartInfoConfig.Source {
  400. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_ONE:
  401. getAData = true
  402. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_TWO:
  403. getBData = true
  404. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_THREE:
  405. getR2Data = true
  406. }
  407. edbList, dataResp, err, errMsg := line_equation.GetChartEdbData(chartInfo.ChartInfoId, lineChartInfoConfig, getAData, getBData, getR2Data)
  408. if err != nil {
  409. if errMsg == `` {
  410. errMsg = "获取失败"
  411. }
  412. return
  413. }
  414. var resultResp interface{}
  415. switch lineChartInfoConfig.Source {
  416. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_ONE:
  417. resultResp = dataResp.AData
  418. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_TWO:
  419. resultResp = dataResp.BData
  420. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_THREE:
  421. resultResp = dataResp.R2Data
  422. }
  423. //chartInfo.UnitEn = baseEdbInfo.UnitEn
  424. // 图表的指标来源
  425. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  426. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  427. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  428. resp.ChartInfo = chartInfo
  429. resp.DataResp = resultResp
  430. resp.EdbInfoList = edbList
  431. // 将数据加入缓存
  432. if utils.Re == nil {
  433. data, _ := json.Marshal(resp)
  434. utils.Rc.Put(key, data, 2*time.Hour)
  435. }
  436. isOk = true
  437. return
  438. }
  439. // GetLineFeatureChartInfoDetailFromUniqueCode 根据编码获取统计特征图表详情
  440. func GetLineFeatureChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfo, key string) (resp *models.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  441. resp = new(models.ChartInfoDetailResp)
  442. // 获取主题样式
  443. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, 1)
  444. if err != nil {
  445. msg = "获取失败"
  446. errMsg = "获取主题信息失败,Err:" + err.Error()
  447. return
  448. }
  449. chartInfo.ChartThemeStyle = chartTheme.Config
  450. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  451. // 获取图表关联指标
  452. edbMappingList, err := models.GetChartEdbMappingList(chartInfo.ChartInfoId)
  453. if err != nil {
  454. msg = "获取失败"
  455. errMsg = "获取图表关联指标信息失败,Err:" + err.Error()
  456. return
  457. }
  458. if len(edbMappingList) != 1 {
  459. msg = "获取失败"
  460. errMsg = fmt.Sprint("获取图表关联指标信息异常,数量:", len(edbMappingList))
  461. return
  462. }
  463. edbMapping := edbMappingList[0]
  464. var edbList []*models.ChartEdbInfoMapping
  465. var resultResp interface{}
  466. switch chartInfo.Source {
  467. case utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION:
  468. calculateValue, tmpErr := strconv.Atoi(chartInfo.ExtraConfig)
  469. if tmpErr != nil {
  470. msg = "获取失败"
  471. errMsg = "格式化配置项失败,Err:" + tmpErr.Error()
  472. return
  473. }
  474. startDate, endDate := utils.GetDateByDateType(chartInfo.DateType, chartInfo.StartDate, chartInfo.EndDate)
  475. edbList, resultResp, err, msg = lineFeatureServ.GetStandardDeviationData(chartInfo.ChartInfoId, startDate, endDate, edbMapping, calculateValue)
  476. case utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE:
  477. var percentileConfig line_featureReq.Percentile
  478. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &percentileConfig)
  479. if err != nil {
  480. msg = "获取失败"
  481. errMsg = "格式化配置项失败,Err:" + err.Error()
  482. return
  483. }
  484. startDate, endDate := utils.GetDateByDateType(chartInfo.DateType, chartInfo.StartDate, chartInfo.EndDate)
  485. edbList, resultResp, err, msg = lineFeatureServ.GetPercentileData(chartInfo.ChartInfoId, startDate, endDate, edbMapping, percentileConfig.CalculateValue, percentileConfig.CalculateUnit, percentileConfig.PercentType)
  486. case utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY:
  487. var frequencyDistributionConfig line_featureReq.FrequencyDistribution
  488. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &frequencyDistributionConfig)
  489. if err != nil {
  490. msg = "获取失败"
  491. errMsg = "格式化配置项失败,Err:" + err.Error()
  492. return
  493. }
  494. // 获取图表中的指标数据
  495. edbList, resultResp, err, errMsg = lineFeatureServ.GetFrequencyDistributionData(0, edbMapping, frequencyDistributionConfig.DateType, frequencyDistributionConfig.FrequencyValue, frequencyDistributionConfig.StartDate, frequencyDistributionConfig.EndDate)
  496. default:
  497. msg = `错误的图表`
  498. errMsg = fmt.Sprint("错误的图表来源,source", chartInfo.Source)
  499. return
  500. }
  501. if err != nil {
  502. if msg == `` {
  503. msg = "获取失败"
  504. }
  505. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  506. return
  507. }
  508. //chartInfo.UnitEn = baseEdbInfo.UnitEn
  509. // 图表的指标来源
  510. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  511. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  512. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  513. resp.ChartInfo = chartInfo
  514. resp.DataResp = resultResp
  515. resp.EdbInfoList = edbList
  516. // 将数据加入缓存
  517. if utils.Re == nil {
  518. data, _ := json.Marshal(resp)
  519. utils.Rc.Put(key, data, 2*time.Hour)
  520. }
  521. isOk = true
  522. return
  523. }
  524. // GetCrossVarietyChartInfoDetailFromUniqueCode 根据编码获取图表详情
  525. func GetCrossVarietyChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfo, key string) (resp *models.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  526. resp = new(models.ChartInfoDetailResp)
  527. // 获取主题样式
  528. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, 5)
  529. if err != nil {
  530. msg = "获取失败"
  531. errMsg = "获取主题信息失败,Err:" + err.Error()
  532. return
  533. }
  534. chartInfo.ChartThemeStyle = chartTheme.Config
  535. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  536. if chartInfo.ExtraConfig == `` {
  537. msg = "图表配置信息异常"
  538. errMsg = "图表配置信息异常"
  539. return
  540. }
  541. var config cross_varietyReq.ChartConfigReq
  542. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &config)
  543. if err != nil {
  544. msg = "解析跨品种分析配置失败"
  545. errMsg = "解析跨品种分析配置失败,Err:" + err.Error()
  546. return
  547. }
  548. // 获取图表x轴y轴
  549. edbList, dataResp, err, msg, _ := cross_variety.GetChartData(0, config)
  550. if err != nil {
  551. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  552. return
  553. }
  554. //判断是否需要展示英文标识
  555. //chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList[0:1], chartInfo.Source, chartInfo.ChartType)
  556. //chartInfo.UnitEn = edbInfoMappingA.UnitEn
  557. // 另存为
  558. resp.ChartInfo = chartInfo
  559. resp.DataResp = dataResp
  560. resp.EdbInfoList = edbList
  561. // 图表的指标来源
  562. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  563. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  564. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  565. // 将数据加入缓存
  566. if utils.Re == nil {
  567. d, _ := json.Marshal(resp)
  568. _ = utils.Rc.Put(key, d, 2*time.Hour)
  569. }
  570. isOk = true
  571. return
  572. }
  573. func GetBalanceChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfo, key, lang string) (resp *models.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  574. resp = new(models.ChartInfoDetailResp)
  575. msg = `获取失败`
  576. var err error
  577. defer func() {
  578. if err != nil {
  579. if errMsg != "" {
  580. msg = errMsg
  581. }
  582. errMsg = err.Error()
  583. }
  584. }()
  585. // 相关联指标
  586. mappingListTmp, dataListMap, err, errMsg := excel.GetBalanceExcelChartSingle(chartInfo.ChartInfoId, 0, lang)
  587. if err != nil {
  588. errMsg = "获取失败"
  589. err = fmt.Errorf(" 获取图表,指标信息失败 Err:%s", err.Error())
  590. return
  591. }
  592. var chartInfoResp *models.ChartInfoDetailResp
  593. chartInfoResp, err, errMsg = data.GetBalanceExcelChartDetail(chartInfo, mappingListTmp, dataListMap)
  594. if err != nil {
  595. msg = "查询图表详情失败"
  596. errMsg = "查询图表详情失败,Err:" + err.Error()
  597. return
  598. }
  599. resp = &models.ChartInfoDetailResp{
  600. ChartInfo: chartInfoResp.ChartInfo,
  601. EdbInfoList: chartInfoResp.EdbInfoList,
  602. XEdbIdValue: chartInfoResp.XEdbIdValue,
  603. YDataList: chartInfoResp.YDataList,
  604. XDataList: chartInfoResp.XDataList,
  605. CorrelationChartInfo: chartInfoResp.CorrelationChartInfo,
  606. DataResp: chartInfoResp.DataResp,
  607. }
  608. if utils.Re == nil {
  609. jsonData, _ := json.Marshal(resp)
  610. utils.Rc.Put(key, jsonData, 10*time.Minute)
  611. }
  612. isOk = true
  613. return
  614. }
  615. // FutureGoodChartInfoRefresh
  616. // @Title 商品价格图表刷新接口
  617. // @Description 商品价格图表刷新接口
  618. // @Param UniqueCode query string true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  619. // @Success Ret=200 刷新成功
  620. // @router /future_good/refresh [get]
  621. func (this *ChartController) FutureGoodChartInfoRefresh() {
  622. br := new(models.BaseResponse).Init()
  623. chartId := 0
  624. defer func() {
  625. // 添加日志
  626. if chartId > 0 {
  627. shareChartRefreshLogInfo := &models.ShareChartRefreshLog{
  628. Ip: this.Ctx.Input.IP(),
  629. ChartId: chartId,
  630. CreateTime: time.Now(),
  631. }
  632. models.AddShareChartRefreshLog(shareChartRefreshLogInfo)
  633. }
  634. this.Data["json"] = br
  635. this.ServeJSON()
  636. }()
  637. uniqueCode := this.GetString("UniqueCode")
  638. if uniqueCode == "" {
  639. br.Msg = "参数错误"
  640. br.ErrMsg = "参数错误,uniqueCode is empty"
  641. return
  642. }
  643. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  644. if err != nil {
  645. if utils.IsErrNoRow(err) {
  646. br.Msg = "该图已被删除,请刷新页面"
  647. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  648. return
  649. }
  650. br.Msg = "获取失败"
  651. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  652. return
  653. }
  654. chartId = chartInfo.ChartInfoId
  655. err = future_goodServ.FutureGoodChartInfoRefresh(chartInfo.ChartInfoId)
  656. if err != nil {
  657. br.Msg = "刷新失败"
  658. br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  659. return
  660. }
  661. //err = data.ChartInfoRefresh(chartInfo.ChartInfoId)
  662. //if err != nil {
  663. // br.Msg = "刷新失败"
  664. // br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  665. // return
  666. //}
  667. //err = data.ChartInfoRefreshV2(chartInfo.ChartInfoId)
  668. //if err != nil {
  669. // br.Msg = "刷新失败"
  670. // br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  671. // return
  672. //}
  673. //清除数据缓存
  674. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  675. if utils.Re == nil {
  676. utils.Rc.Delete(key)
  677. }
  678. br.Ret = 200
  679. br.Success = true
  680. br.Msg = "刷新成功"
  681. }
  682. // GetCorrelationChartInfoDetailFromUniqueCode 根据编码获取相关性图表详情
  683. func GetCorrelationChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfo, key string) (resp *models.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  684. resp = new(models.ChartInfoDetailResp)
  685. // 获取主题样式
  686. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, 1)
  687. if err != nil {
  688. msg = "获取失败"
  689. errMsg = "获取主题信息失败,Err:" + err.Error()
  690. return
  691. }
  692. chartInfo.ChartThemeStyle = chartTheme.Config
  693. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  694. chartInfoId := chartInfo.ChartInfoId
  695. startDate := chartInfo.StartDate
  696. endDate := chartInfo.EndDate
  697. // 兼容日期错误
  698. {
  699. if strings.Count(startDate, "-") == 1 {
  700. startDate = startDate + "-01"
  701. }
  702. if strings.Count(endDate, "-") == 1 {
  703. endDate = endDate + "-01"
  704. }
  705. }
  706. // 相关性图表信息
  707. correlationChart := new(data_manage.ChartInfoCorrelation)
  708. if e := correlationChart.GetItemById(chartInfoId); e != nil {
  709. msg = "获取失败"
  710. errMsg = "获取图表相关性信息失败, Err:" + e.Error()
  711. return
  712. }
  713. // 获取指标信息
  714. edbInfoMappingA, e := models.GetChartEdbMappingByEdbInfoId(correlationChart.EdbInfoIdFirst)
  715. if e != nil {
  716. msg = "获取失败"
  717. errMsg = "获取相关性图表, A指标mapping信息失败, Err:" + e.Error()
  718. return
  719. }
  720. edbInfoMappingB := new(models.ChartEdbInfoMapping)
  721. if correlationChart.AnalysisMode != 1 {
  722. edbInfoMappingB, e = models.GetChartEdbMappingByEdbInfoId(correlationChart.EdbInfoIdSecond)
  723. if e != nil {
  724. msg = "获取失败"
  725. errMsg = "获取相关性图表, B指标mapping信息失败, Err:" + e.Error()
  726. return
  727. }
  728. }
  729. var dataResp interface{} // 绘图数据返回(目前是滚动相关性的图)
  730. var xEdbIdValue []int
  731. var yDataList []models.YData
  732. if correlationChart.AnalysisMode != 1 {
  733. switch chartInfo.Source {
  734. case utils.CHART_SOURCE_CORRELATION: // 相关性图
  735. moveUnitDays, ok := utils.FrequencyDaysMap[correlationChart.CalculateUnit]
  736. if !ok {
  737. msg = "错误的分析周期"
  738. errMsg = "相关性图表数据有误"
  739. return
  740. }
  741. st := time.Now().AddDate(0, 0, -correlationChart.CalculateValue*moveUnitDays).Format(utils.FormatDate)
  742. ed := time.Now().Format(utils.FormatDate)
  743. xEdbIdValue, yDataList, e = correlationServ.GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, correlationChart.LeadValue, correlationChart.LeadUnit, st, ed, chartInfo.ExtraConfig)
  744. if e != nil {
  745. msg = "获取失败"
  746. errMsg = "获取相关性图表, 图表计算值失败, Err:" + e.Error()
  747. return
  748. }
  749. case utils.CHART_SOURCE_ROLLING_CORRELATION: // 滚动相关性图
  750. st, ed := utils.GetDateByDateType(correlationChart.DateType, correlationChart.StartDate.Format(utils.FormatDate), correlationChart.EndDate.Format(utils.FormatDate))
  751. dataResp, e = correlationServ.GetRollingCorrelationChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, correlationChart.LeadValue, correlationChart.LeadUnit, correlationChart.CalculateValue, correlationChart.CalculateUnit, st, ed, chartInfo.ChartName, chartInfo.ChartNameEn)
  752. }
  753. } else {
  754. xEdbIdValue, yDataList, e = correlationServ.GetFactorChartDataByChartId(chartInfoId, chartInfo.ExtraConfig)
  755. if e != nil {
  756. msg = "获取失败"
  757. errMsg = "获取相关性图表, 图表计算值失败, Err:" + e.Error()
  758. return
  759. }
  760. }
  761. // 完善指标信息
  762. edbList, e := correlationServ.GetChartEdbInfoFormat(chartInfo.ChartInfoId, edbInfoMappingA, edbInfoMappingB)
  763. if e != nil {
  764. msg = "获取失败"
  765. errMsg = "获取相关性图表, 完善指标信息失败, Err:" + e.Error()
  766. return
  767. }
  768. // 图表的指标来源
  769. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  770. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  771. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  772. correlationInfo := new(models.CorrelationInfo)
  773. correlationInfo.LeadValue = correlationChart.LeadValue
  774. correlationInfo.LeadUnit = correlationChart.LeadUnit
  775. correlationInfo.StartDate = correlationChart.StartDate.Format(utils.FormatDate)
  776. correlationInfo.EndDate = correlationChart.EndDate.Format(utils.FormatDate)
  777. correlationInfo.LeadValue = correlationChart.LeadValue
  778. correlationInfo.EdbInfoIdFirst = correlationChart.EdbInfoIdFirst
  779. correlationInfo.EdbInfoIdSecond = correlationChart.EdbInfoIdSecond
  780. correlationInfo.AnalysisMode = correlationChart.AnalysisMode
  781. resp.ChartInfo = chartInfo
  782. resp.EdbInfoList = edbList
  783. resp.XEdbIdValue = xEdbIdValue
  784. resp.YDataList = yDataList
  785. resp.CorrelationChartInfo = correlationInfo
  786. resp.DataResp = dataResp
  787. // 将数据加入缓存
  788. if utils.Re == nil {
  789. d, _ := json.Marshal(resp)
  790. _ = utils.Rc.Put(key, d, 2*time.Hour)
  791. }
  792. isOk = true
  793. return
  794. }
  795. // CorrelationChartInfoRefresh
  796. // @Title 商品价格图表刷新接口
  797. // @Description 商品价格图表刷新接口
  798. // @Param UniqueCode query string true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  799. // @Success Ret=200 刷新成功
  800. // @router /correlation/refresh [get]
  801. func (this *ChartController) CorrelationChartInfoRefresh() {
  802. br := new(models.BaseResponse).Init()
  803. chartId := 0
  804. defer func() {
  805. // 添加日志
  806. if chartId > 0 {
  807. shareChartRefreshLogInfo := &models.ShareChartRefreshLog{
  808. Ip: this.Ctx.Input.IP(),
  809. ChartId: chartId,
  810. CreateTime: time.Now(),
  811. }
  812. models.AddShareChartRefreshLog(shareChartRefreshLogInfo)
  813. }
  814. this.Data["json"] = br
  815. this.ServeJSON()
  816. }()
  817. uniqueCode := this.GetString("UniqueCode")
  818. if uniqueCode == "" {
  819. br.Msg = "参数错误"
  820. br.ErrMsg = "参数错误,uniqueCode is empty"
  821. return
  822. }
  823. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  824. if err != nil {
  825. if utils.IsErrNoRow(err) {
  826. br.Msg = "该图已被删除,请刷新页面"
  827. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  828. return
  829. }
  830. br.Msg = "获取失败"
  831. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  832. return
  833. }
  834. chartId = chartInfo.ChartInfoId
  835. err = data.ChartInfoRefreshV2(chartInfo.ChartInfoId)
  836. if err != nil {
  837. br.Msg = "刷新失败"
  838. br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  839. return
  840. }
  841. //// 刷新相关性图表
  842. //if e := correlationServ.ChartInfoRefresh(chartInfo.ChartInfoId); e != nil {
  843. // br.Msg = "刷新失败"
  844. // br.ErrMsg = "刷新相关性图表失败, Err:" + e.Error()
  845. // return
  846. //}
  847. //清除数据缓存
  848. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  849. if utils.Re == nil {
  850. _ = utils.Rc.Delete(key)
  851. }
  852. br.Ret = 200
  853. br.Success = true
  854. br.Msg = "刷新成功"
  855. }
  856. // GetRangeAnalysisChartInfoDetailFromUniqueCode 根据编码获取区间计算图表详情
  857. func GetRangeAnalysisChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfo, key, lang string) (resp *models.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  858. resp = new(models.ChartInfoDetailResp)
  859. // 获取主题样式
  860. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, 1)
  861. if err != nil {
  862. msg = "获取失败"
  863. errMsg = "获取主题信息失败,Err:" + err.Error()
  864. return
  865. }
  866. chartInfo.ChartThemeStyle = chartTheme.Config
  867. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  868. chartInfoId := chartInfo.ChartInfoId
  869. // 获取指标信息
  870. //chartInfo.CorrelationLeadUnit = req.LeadUnit
  871. edbInfoMappingList, err := models.GetChartEdbMappingList(chartInfoId)
  872. if err != nil {
  873. msg = "获取失败"
  874. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  875. return
  876. }
  877. dateType := chartInfo.DateType
  878. // 开始/结束日期
  879. startYear := chartInfo.StartYear
  880. startDate := chartInfo.StartDate
  881. endDate := chartInfo.EndDate
  882. // 区间计算图表配置校验
  883. var extraConfig models.ChartRangeAnalysisExtraConf
  884. if chartInfo.ExtraConfig == `` {
  885. msg = "配置信息错误"
  886. return
  887. }
  888. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConfig)
  889. if err != nil {
  890. msg = "配置信息错误"
  891. errMsg = "图表配置信息错误,Err:" + err.Error()
  892. return
  893. }
  894. // 获取图表数据
  895. if len(edbInfoMappingList) == 0 {
  896. msg = "获取失败"
  897. errMsg = "图表没有指标,无法计算"
  898. return
  899. }
  900. // 获取图表x轴y轴
  901. edbList, xEdbIdValue, dataResp, e := range_analysis.GetChartDataByEdbInfoList(chartInfoId, dateType, startYear, startDate, endDate, edbInfoMappingList, &extraConfig)
  902. if e != nil {
  903. msg = "获取失败"
  904. errMsg = "获取区间计算图表, 图表计算值失败, Err:" + e.Error()
  905. return
  906. }
  907. // 图表的指标来源
  908. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  909. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  910. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  911. resp.ChartInfo = chartInfo
  912. resp.EdbInfoList = edbList
  913. resp.XEdbIdValue = xEdbIdValue
  914. resp.DataResp = dataResp
  915. if utils.Re == nil {
  916. jsonData, _ := json.Marshal(resp)
  917. utils.Rc.Put(key, jsonData, 10*time.Minute)
  918. }
  919. isOk = true
  920. return
  921. }