chart_common.go 29 KB

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