chart_common.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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/services/data/range_analysis"
  20. dwmini "eta/eta_chart_lib/services/dw_mini"
  21. "eta/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 err.Error() == utils.ErrNoRow() {
  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. if v.Source == 0 {
  249. name := strings.Split(v.EdbName, "(")
  250. if barConfig.FutureGoodEdbName != "" {
  251. name[0] = barConfig.FutureGoodEdbName
  252. }
  253. v.EdbName = name[0]
  254. if len(name) > 1 {
  255. v.EdbName = v.EdbName + "(" + name[1]
  256. }
  257. //英文
  258. // 编译正则表达式,匹配一个或多个数字
  259. if v.EdbNameEn != "" {
  260. name = strings.Split(v.EdbNameEn, "(")
  261. if barConfig.FutureGoodEdbNameEn != "" {
  262. name[0] = barConfig.FutureGoodEdbNameEn
  263. }
  264. v.EdbNameEn = name[0]
  265. if len(name) > 1 {
  266. v.EdbNameEn = v.EdbNameEn + "(" + name[1]
  267. }
  268. }
  269. }
  270. }
  271. chartInfo.UnitEn = baseEdbInfo.UnitEn
  272. // 图表的指标来源
  273. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  274. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  275. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  276. xDataListFinal := make([]models.XData, 0)
  277. for _, v := range xDataList {
  278. tmp := models.XData{
  279. Name: v.Name,
  280. NameEn: v.NameEn,
  281. IsHide: v.IsHide,
  282. }
  283. xDataListFinal = append(xDataListFinal, tmp)
  284. }
  285. resp.ChartInfo = chartInfo
  286. resp.EdbInfoList = edbList
  287. resp.XEdbIdValue = xEdbIdValue
  288. resp.YDataList = yDataList
  289. resp.XDataList = xDataListFinal
  290. //resp.BarChartInfo = barConfig
  291. //resp.Status = true
  292. // 将数据加入缓存
  293. if utils.Re == nil {
  294. data, _ := json.Marshal(resp)
  295. utils.Rc.Put(key, data, 2*time.Hour)
  296. }
  297. isOk = true
  298. return
  299. }
  300. // GetFutureGoodProfitChartInfoDetailFromUniqueCode 根据编码获取商品利润图表详情
  301. func GetFutureGoodProfitChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfo, key string) (resp *models.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  302. resp = new(models.ChartInfoDetailResp)
  303. chartInfoId := chartInfo.ChartInfoId
  304. // 获取主题样式
  305. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, 1)
  306. if err != nil {
  307. msg = "获取失败"
  308. errMsg = "获取主题信息失败,Err:" + err.Error()
  309. return
  310. }
  311. chartInfo.ChartThemeStyle = chartTheme.Config
  312. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  313. // 商品利润曲线图的一些配置
  314. var extraConf request.ChartInfoReq
  315. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConf)
  316. if err != nil {
  317. msg = "商品利润曲线图配置异常"
  318. errMsg = "商品利润曲线图配置异常,Err:" + err.Error()
  319. return
  320. }
  321. if len(extraConf.EdbInfoIdList) == 0 {
  322. extraConf.EdbInfoIdList = append(extraConf.EdbInfoIdList, extraConf.BaseEdbInfoId)
  323. }
  324. edbList := make([]*models.ChartEdbInfoMapping, 0)
  325. edbInfoMappingList, err := models.GetEtaEdbChartEdbMappingList(chartInfoId)
  326. if err != nil {
  327. msg = "获取失败"
  328. errMsg = "获取商品利润图表,基础指标信息失败,Err:" + err.Error()
  329. return
  330. }
  331. baseEdbInfo := new(models.ChartEdbInfoMapping)
  332. for _, v := range edbInfoMappingList {
  333. if v.EdbInfoId == extraConf.BaseEdbInfoId {
  334. baseEdbInfo = v
  335. }
  336. }
  337. edbList = edbInfoMappingList
  338. futureGoodEdbInfoMappingList, err := models.GetFutureGoodEdbChartEdbMappingList(chartInfoId)
  339. if err != nil {
  340. msg = "获取失败"
  341. errMsg = "获取商品利润图表,商品指标信息失败,Err:" + err.Error()
  342. return
  343. }
  344. edbList = append(edbList, futureGoodEdbInfoMappingList...)
  345. if len(edbList) <= 0 {
  346. msg = "商品利润曲线图表指标异常"
  347. errMsg = "商品利润曲线图表指标异常"
  348. return
  349. }
  350. xDataList := make([]models.XData, 0)
  351. yDataList := make([]models.YData, 0)
  352. // 查找商品利润图表的扩展信息
  353. chartInfoFutureGoodProfit := new(future_good.ChartInfoFutureGoodProfit)
  354. if err = chartInfoFutureGoodProfit.GetItemById(chartInfo.ChartInfoId); err != nil {
  355. msg = "获取失败"
  356. errMsg = "获取基础相关性图表信息失败, Err: " + err.Error()
  357. return
  358. }
  359. err = json.Unmarshal([]byte(chartInfoFutureGoodProfit.XValue), &xDataList)
  360. if err != nil {
  361. msg = "获取失败"
  362. errMsg = "转换X轴数据失败, Err: " + err.Error()
  363. return
  364. }
  365. err = json.Unmarshal([]byte(chartInfoFutureGoodProfit.YValue), &yDataList)
  366. if err != nil {
  367. msg = "获取失败"
  368. errMsg = "转换Y轴数据失败, Err: " + err.Error()
  369. return
  370. }
  371. warnEdbList := make([]string, 0)
  372. for _, v := range edbList {
  373. if v.IsNullData {
  374. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  375. }
  376. }
  377. chartInfo.UnitEn = baseEdbInfo.UnitEn
  378. resp.ChartInfo = chartInfo
  379. resp.EdbInfoList = edbList
  380. resp.DataResp = response.ProfitFutureGoodChartResp{
  381. YDataList: yDataList,
  382. XDataList: xDataList,
  383. ProfitName: chartInfoFutureGoodProfit.ProfitName,
  384. ProfitNameEn: chartInfoFutureGoodProfit.ProfitNameEn,
  385. Extra: extraConf,
  386. }
  387. // 将数据加入缓存
  388. if utils.Re == nil {
  389. data, _ := json.Marshal(resp)
  390. utils.Rc.Put(key, data, 2*time.Hour)
  391. }
  392. isOk = true
  393. return
  394. }
  395. // GetLineEquationChartInfoDetailFromUniqueCode 根据编码获取拟合方程图表详情
  396. func GetLineEquationChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfo, key string) (resp *models.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  397. resp = new(models.ChartInfoDetailResp)
  398. // 获取主题样式
  399. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, 1)
  400. if err != nil {
  401. msg = "获取失败"
  402. errMsg = "获取主题信息失败,Err:" + err.Error()
  403. return
  404. }
  405. chartInfo.ChartThemeStyle = chartTheme.Config
  406. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  407. //chartInfoId := chartInfo.ChartInfoId
  408. if chartInfo.ExtraConfig == `` {
  409. msg = "获取失败"
  410. errMsg = "获取配置信息失败"
  411. return
  412. }
  413. var lineChartInfoConfig line_equationReq.LineChartInfoReq
  414. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &lineChartInfoConfig)
  415. if err != nil {
  416. msg = "获取失败"
  417. errMsg = "获取图表配置信息失败, Err:" + err.Error()
  418. return
  419. }
  420. var getAData, getBData, getR2Data bool
  421. switch lineChartInfoConfig.Source {
  422. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_ONE:
  423. getAData = true
  424. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_TWO:
  425. getBData = true
  426. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_THREE:
  427. getR2Data = true
  428. }
  429. edbList, dataResp, err, errMsg := line_equation.GetChartEdbData(chartInfo.ChartInfoId, lineChartInfoConfig, getAData, getBData, getR2Data)
  430. if err != nil {
  431. if errMsg == `` {
  432. errMsg = "获取失败"
  433. }
  434. return
  435. }
  436. var resultResp interface{}
  437. switch lineChartInfoConfig.Source {
  438. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_ONE:
  439. resultResp = dataResp.AData
  440. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_TWO:
  441. resultResp = dataResp.BData
  442. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_THREE:
  443. resultResp = dataResp.R2Data
  444. }
  445. //chartInfo.UnitEn = baseEdbInfo.UnitEn
  446. // 图表的指标来源
  447. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  448. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  449. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  450. resp.ChartInfo = chartInfo
  451. resp.DataResp = resultResp
  452. resp.EdbInfoList = edbList
  453. // 将数据加入缓存
  454. if utils.Re == nil {
  455. data, _ := json.Marshal(resp)
  456. utils.Rc.Put(key, data, 2*time.Hour)
  457. }
  458. isOk = true
  459. return
  460. }
  461. // GetLineFeatureChartInfoDetailFromUniqueCode 根据编码获取统计特征图表详情
  462. func GetLineFeatureChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfo, key string) (resp *models.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  463. resp = new(models.ChartInfoDetailResp)
  464. // 获取主题样式
  465. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, 1)
  466. if err != nil {
  467. msg = "获取失败"
  468. errMsg = "获取主题信息失败,Err:" + err.Error()
  469. return
  470. }
  471. chartInfo.ChartThemeStyle = chartTheme.Config
  472. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  473. // 获取图表关联指标
  474. edbMappingList, err := models.GetChartEdbMappingList(chartInfo.ChartInfoId)
  475. if err != nil {
  476. msg = "获取失败"
  477. errMsg = "获取图表关联指标信息失败,Err:" + err.Error()
  478. return
  479. }
  480. if len(edbMappingList) != 1 {
  481. msg = "获取失败"
  482. errMsg = fmt.Sprint("获取图表关联指标信息异常,数量:", len(edbMappingList))
  483. return
  484. }
  485. edbMapping := edbMappingList[0]
  486. var edbList []*models.ChartEdbInfoMapping
  487. var resultResp interface{}
  488. switch chartInfo.Source {
  489. case utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION:
  490. calculateValue, tmpErr := strconv.Atoi(chartInfo.ExtraConfig)
  491. if tmpErr != nil {
  492. msg = "获取失败"
  493. errMsg = "格式化配置项失败,Err:" + tmpErr.Error()
  494. return
  495. }
  496. startDate, endDate := utils.GetDateByDateType(chartInfo.DateType, chartInfo.StartDate, chartInfo.EndDate)
  497. edbList, resultResp, err, msg = lineFeatureServ.GetStandardDeviationData(chartInfo.ChartInfoId, startDate, endDate, edbMapping, calculateValue)
  498. case utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE:
  499. var percentileConfig line_featureReq.Percentile
  500. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &percentileConfig)
  501. if err != nil {
  502. msg = "获取失败"
  503. errMsg = "格式化配置项失败,Err:" + err.Error()
  504. return
  505. }
  506. startDate, endDate := utils.GetDateByDateType(chartInfo.DateType, chartInfo.StartDate, chartInfo.EndDate)
  507. edbList, resultResp, err, msg = lineFeatureServ.GetPercentileData(chartInfo.ChartInfoId, startDate, endDate, edbMapping, percentileConfig.CalculateValue, percentileConfig.CalculateUnit, percentileConfig.PercentType)
  508. case utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY:
  509. var frequencyDistributionConfig line_featureReq.FrequencyDistribution
  510. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &frequencyDistributionConfig)
  511. if err != nil {
  512. msg = "获取失败"
  513. errMsg = "格式化配置项失败,Err:" + err.Error()
  514. return
  515. }
  516. // 获取图表中的指标数据
  517. edbList, resultResp, err, errMsg = lineFeatureServ.GetFrequencyDistributionData(0, edbMapping, frequencyDistributionConfig.DateType, frequencyDistributionConfig.FrequencyValue, frequencyDistributionConfig.StartDate, frequencyDistributionConfig.EndDate)
  518. default:
  519. msg = `错误的图表`
  520. errMsg = fmt.Sprint("错误的图表来源,source", chartInfo.Source)
  521. return
  522. }
  523. if err != nil {
  524. if msg == `` {
  525. msg = "获取失败"
  526. }
  527. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  528. return
  529. }
  530. //chartInfo.UnitEn = baseEdbInfo.UnitEn
  531. // 图表的指标来源
  532. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  533. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  534. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  535. resp.ChartInfo = chartInfo
  536. resp.DataResp = resultResp
  537. resp.EdbInfoList = edbList
  538. // 将数据加入缓存
  539. if utils.Re == nil {
  540. data, _ := json.Marshal(resp)
  541. utils.Rc.Put(key, data, 2*time.Hour)
  542. }
  543. isOk = true
  544. return
  545. }
  546. // GetCrossVarietyChartInfoDetailFromUniqueCode 根据编码获取图表详情
  547. func GetCrossVarietyChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfo, key string) (resp *models.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  548. resp = new(models.ChartInfoDetailResp)
  549. // 获取主题样式
  550. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, 5)
  551. if err != nil {
  552. msg = "获取失败"
  553. errMsg = "获取主题信息失败,Err:" + err.Error()
  554. return
  555. }
  556. chartInfo.ChartThemeStyle = chartTheme.Config
  557. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  558. if chartInfo.ExtraConfig == `` {
  559. msg = "图表配置信息异常"
  560. errMsg = "图表配置信息异常"
  561. return
  562. }
  563. var config cross_varietyReq.ChartConfigReq
  564. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &config)
  565. if err != nil {
  566. msg = "解析跨品种分析配置失败"
  567. errMsg = "解析跨品种分析配置失败,Err:" + err.Error()
  568. return
  569. }
  570. // 获取图表x轴y轴
  571. edbList, dataResp, err, msg, _ := cross_variety.GetChartData(0, config)
  572. if err != nil {
  573. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  574. return
  575. }
  576. //判断是否需要展示英文标识
  577. //chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList[0:1], chartInfo.Source, chartInfo.ChartType)
  578. //chartInfo.UnitEn = edbInfoMappingA.UnitEn
  579. // 另存为
  580. resp.ChartInfo = chartInfo
  581. resp.DataResp = dataResp
  582. resp.EdbInfoList = edbList
  583. // 图表的指标来源
  584. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  585. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  586. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  587. // 将数据加入缓存
  588. if utils.Re == nil {
  589. d, _ := json.Marshal(resp)
  590. _ = utils.Rc.Put(key, d, 2*time.Hour)
  591. }
  592. isOk = true
  593. return
  594. }
  595. func GetBalanceChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfo, key, lang string) (resp *models.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  596. resp = new(models.ChartInfoDetailResp)
  597. msg = `获取失败`
  598. var err error
  599. defer func() {
  600. if err != nil {
  601. if errMsg != "" {
  602. msg = errMsg
  603. }
  604. errMsg = err.Error()
  605. }
  606. }()
  607. // 相关联指标
  608. mappingListTmp, dataListMap, err, errMsg := excel.GetBalanceExcelChartSingle(chartInfo.ChartInfoId, 0, lang)
  609. if err != nil {
  610. errMsg = "获取失败"
  611. err = fmt.Errorf(" 获取图表,指标信息失败 Err:%s", err.Error())
  612. return
  613. }
  614. var chartInfoResp *models.ChartInfoDetailResp
  615. chartInfoResp, err, errMsg = data.GetBalanceExcelChartDetail(chartInfo, mappingListTmp, dataListMap)
  616. if err != nil {
  617. msg = "查询图表详情失败"
  618. errMsg = "查询图表详情失败,Err:" + err.Error()
  619. return
  620. }
  621. resp = &models.ChartInfoDetailResp{
  622. ChartInfo: chartInfoResp.ChartInfo,
  623. EdbInfoList: chartInfoResp.EdbInfoList,
  624. XEdbIdValue: chartInfoResp.XEdbIdValue,
  625. YDataList: chartInfoResp.YDataList,
  626. XDataList: chartInfoResp.XDataList,
  627. CorrelationChartInfo: chartInfoResp.CorrelationChartInfo,
  628. DataResp: chartInfoResp.DataResp,
  629. }
  630. if utils.Re == nil {
  631. jsonData, _ := json.Marshal(resp)
  632. utils.Rc.Put(key, jsonData, 10*time.Minute)
  633. }
  634. isOk = true
  635. return
  636. }
  637. // FutureGoodChartInfoRefresh
  638. // @Title 商品价格图表刷新接口
  639. // @Description 商品价格图表刷新接口
  640. // @Param UniqueCode query string true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  641. // @Success Ret=200 刷新成功
  642. // @router /future_good/refresh [get]
  643. func (this *ChartController) FutureGoodChartInfoRefresh() {
  644. br := new(models.BaseResponse).Init()
  645. chartId := 0
  646. defer func() {
  647. // 添加日志
  648. if chartId > 0 {
  649. shareChartRefreshLogInfo := &models.ShareChartRefreshLog{
  650. Ip: this.Ctx.Input.IP(),
  651. ChartId: chartId,
  652. CreateTime: time.Now(),
  653. }
  654. models.AddShareChartRefreshLog(shareChartRefreshLogInfo)
  655. }
  656. this.Data["json"] = br
  657. this.ServeJSON()
  658. }()
  659. uniqueCode := this.GetString("UniqueCode")
  660. if uniqueCode == "" {
  661. br.Msg = "参数错误"
  662. br.ErrMsg = "参数错误,uniqueCode is empty"
  663. return
  664. }
  665. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  666. if err != nil {
  667. if err.Error() == utils.ErrNoRow() {
  668. br.Msg = "该图已被删除,请刷新页面"
  669. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  670. return
  671. }
  672. br.Msg = "获取失败"
  673. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  674. return
  675. }
  676. chartId = chartInfo.ChartInfoId
  677. err = future_goodServ.FutureGoodChartInfoRefresh(chartInfo.ChartInfoId)
  678. if err != nil {
  679. br.Msg = "刷新失败"
  680. br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  681. return
  682. }
  683. //err = data.ChartInfoRefresh(chartInfo.ChartInfoId)
  684. //if err != nil {
  685. // br.Msg = "刷新失败"
  686. // br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  687. // return
  688. //}
  689. //err = data.ChartInfoRefreshV2(chartInfo.ChartInfoId)
  690. //if err != nil {
  691. // br.Msg = "刷新失败"
  692. // br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  693. // return
  694. //}
  695. //清除数据缓存
  696. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  697. if utils.Re == nil {
  698. utils.Rc.Delete(key)
  699. }
  700. br.Ret = 200
  701. br.Success = true
  702. br.Msg = "刷新成功"
  703. }
  704. // GetCorrelationChartInfoDetailFromUniqueCode 根据编码获取相关性图表详情
  705. func GetCorrelationChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfo, key string) (resp *models.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  706. resp = new(models.ChartInfoDetailResp)
  707. // 获取主题样式
  708. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, 1)
  709. if err != nil {
  710. msg = "获取失败"
  711. errMsg = "获取主题信息失败,Err:" + err.Error()
  712. return
  713. }
  714. chartInfo.ChartThemeStyle = chartTheme.Config
  715. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  716. chartInfoId := chartInfo.ChartInfoId
  717. startDate := chartInfo.StartDate
  718. endDate := chartInfo.EndDate
  719. // 兼容日期错误
  720. {
  721. if strings.Count(startDate, "-") == 1 {
  722. startDate = startDate + "-01"
  723. }
  724. if strings.Count(endDate, "-") == 1 {
  725. endDate = endDate + "-01"
  726. }
  727. }
  728. // 相关性图表信息
  729. correlationChart := new(data_manage.ChartInfoCorrelation)
  730. if e := correlationChart.GetItemById(chartInfoId); e != nil {
  731. msg = "获取失败"
  732. errMsg = "获取图表相关性信息失败, Err:" + e.Error()
  733. return
  734. }
  735. // 获取指标信息
  736. edbInfoMappingA, e := models.GetChartEdbMappingByEdbInfoId(correlationChart.EdbInfoIdFirst)
  737. if e != nil {
  738. msg = "获取失败"
  739. errMsg = "获取相关性图表, A指标mapping信息失败, Err:" + e.Error()
  740. return
  741. }
  742. edbInfoMappingB := new(models.ChartEdbInfoMapping)
  743. if correlationChart.AnalysisMode != 1 {
  744. edbInfoMappingB, e = models.GetChartEdbMappingByEdbInfoId(correlationChart.EdbInfoIdSecond)
  745. if e != nil {
  746. msg = "获取失败"
  747. errMsg = "获取相关性图表, B指标mapping信息失败, Err:" + e.Error()
  748. return
  749. }
  750. }
  751. var dataResp interface{} // 绘图数据返回(目前是滚动相关性的图)
  752. var xEdbIdValue []int
  753. var yDataList []models.YData
  754. if correlationChart.AnalysisMode != 1 {
  755. switch chartInfo.Source {
  756. case utils.CHART_SOURCE_CORRELATION: // 相关性图
  757. moveUnitDays, ok := utils.FrequencyDaysMap[correlationChart.CalculateUnit]
  758. if !ok {
  759. msg = "错误的分析周期"
  760. errMsg = "相关性图表数据有误"
  761. return
  762. }
  763. st := time.Now().AddDate(0, 0, -correlationChart.CalculateValue*moveUnitDays).Format(utils.FormatDate)
  764. ed := time.Now().Format(utils.FormatDate)
  765. xEdbIdValue, yDataList, e = correlationServ.GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, correlationChart.LeadValue, correlationChart.LeadUnit, st, ed, chartInfo.ExtraConfig)
  766. if e != nil {
  767. msg = "获取失败"
  768. errMsg = "获取相关性图表, 图表计算值失败, Err:" + e.Error()
  769. return
  770. }
  771. case utils.CHART_SOURCE_ROLLING_CORRELATION: // 滚动相关性图
  772. st, ed := utils.GetDateByDateType(correlationChart.DateType, correlationChart.StartDate.Format(utils.FormatDate), correlationChart.EndDate.Format(utils.FormatDate))
  773. dataResp, e = correlationServ.GetRollingCorrelationChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, correlationChart.LeadValue, correlationChart.LeadUnit, correlationChart.CalculateValue, correlationChart.CalculateUnit, st, ed, chartInfo.ChartName, chartInfo.ChartNameEn)
  774. }
  775. } else {
  776. xEdbIdValue, yDataList, e = correlationServ.GetFactorChartDataByChartId(chartInfoId, chartInfo.ExtraConfig)
  777. if e != nil {
  778. msg = "获取失败"
  779. errMsg = "获取相关性图表, 图表计算值失败, Err:" + e.Error()
  780. return
  781. }
  782. }
  783. // 完善指标信息
  784. edbList, e := correlationServ.GetChartEdbInfoFormat(chartInfo.ChartInfoId, edbInfoMappingA, edbInfoMappingB)
  785. if e != nil {
  786. msg = "获取失败"
  787. errMsg = "获取相关性图表, 完善指标信息失败, Err:" + e.Error()
  788. return
  789. }
  790. // 图表的指标来源
  791. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  792. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  793. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  794. correlationInfo := new(models.CorrelationInfo)
  795. correlationInfo.LeadValue = correlationChart.LeadValue
  796. correlationInfo.LeadUnit = correlationChart.LeadUnit
  797. correlationInfo.StartDate = correlationChart.StartDate.Format(utils.FormatDate)
  798. correlationInfo.EndDate = correlationChart.EndDate.Format(utils.FormatDate)
  799. correlationInfo.LeadValue = correlationChart.LeadValue
  800. correlationInfo.EdbInfoIdFirst = correlationChart.EdbInfoIdFirst
  801. correlationInfo.EdbInfoIdSecond = correlationChart.EdbInfoIdSecond
  802. correlationInfo.AnalysisMode = correlationChart.AnalysisMode
  803. resp.ChartInfo = chartInfo
  804. resp.EdbInfoList = edbList
  805. resp.XEdbIdValue = xEdbIdValue
  806. resp.YDataList = yDataList
  807. resp.CorrelationChartInfo = correlationInfo
  808. resp.DataResp = dataResp
  809. // 将数据加入缓存
  810. if utils.Re == nil {
  811. d, _ := json.Marshal(resp)
  812. _ = utils.Rc.Put(key, d, 2*time.Hour)
  813. }
  814. isOk = true
  815. return
  816. }
  817. // CorrelationChartInfoRefresh
  818. // @Title 商品价格图表刷新接口
  819. // @Description 商品价格图表刷新接口
  820. // @Param UniqueCode query string true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  821. // @Success Ret=200 刷新成功
  822. // @router /correlation/refresh [get]
  823. func (this *ChartController) CorrelationChartInfoRefresh() {
  824. br := new(models.BaseResponse).Init()
  825. chartId := 0
  826. defer func() {
  827. // 添加日志
  828. if chartId > 0 {
  829. shareChartRefreshLogInfo := &models.ShareChartRefreshLog{
  830. Ip: this.Ctx.Input.IP(),
  831. ChartId: chartId,
  832. CreateTime: time.Now(),
  833. }
  834. models.AddShareChartRefreshLog(shareChartRefreshLogInfo)
  835. }
  836. this.Data["json"] = br
  837. this.ServeJSON()
  838. }()
  839. uniqueCode := this.GetString("UniqueCode")
  840. if uniqueCode == "" {
  841. br.Msg = "参数错误"
  842. br.ErrMsg = "参数错误,uniqueCode is empty"
  843. return
  844. }
  845. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  846. if err != nil {
  847. if err.Error() == utils.ErrNoRow() {
  848. br.Msg = "该图已被删除,请刷新页面"
  849. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  850. return
  851. }
  852. br.Msg = "获取失败"
  853. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  854. return
  855. }
  856. chartId = chartInfo.ChartInfoId
  857. err = data.ChartInfoRefreshV2(chartInfo.ChartInfoId)
  858. if err != nil {
  859. br.Msg = "刷新失败"
  860. br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  861. return
  862. }
  863. //// 刷新相关性图表
  864. //if e := correlationServ.ChartInfoRefresh(chartInfo.ChartInfoId); e != nil {
  865. // br.Msg = "刷新失败"
  866. // br.ErrMsg = "刷新相关性图表失败, Err:" + e.Error()
  867. // return
  868. //}
  869. //清除数据缓存
  870. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  871. if utils.Re == nil {
  872. _ = utils.Rc.Delete(key)
  873. }
  874. br.Ret = 200
  875. br.Success = true
  876. br.Msg = "刷新成功"
  877. }
  878. // GetRangeAnalysisChartInfoDetailFromUniqueCode 根据编码获取区间计算图表详情
  879. func GetRangeAnalysisChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfo, key, lang string) (resp *models.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  880. resp = new(models.ChartInfoDetailResp)
  881. // 获取主题样式
  882. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, 1)
  883. if err != nil {
  884. msg = "获取失败"
  885. errMsg = "获取主题信息失败,Err:" + err.Error()
  886. return
  887. }
  888. chartInfo.ChartThemeStyle = chartTheme.Config
  889. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  890. chartInfoId := chartInfo.ChartInfoId
  891. // 获取指标信息
  892. //chartInfo.CorrelationLeadUnit = req.LeadUnit
  893. edbInfoMappingList, err := models.GetChartEdbMappingList(chartInfoId)
  894. if err != nil {
  895. msg = "获取失败"
  896. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  897. return
  898. }
  899. dateType := chartInfo.DateType
  900. // 开始/结束日期
  901. startYear := chartInfo.StartYear
  902. startDate := chartInfo.StartDate
  903. endDate := chartInfo.EndDate
  904. // 区间计算图表配置校验
  905. var extraConfig models.ChartRangeAnalysisExtraConf
  906. if chartInfo.ExtraConfig == `` {
  907. msg = "配置信息错误"
  908. return
  909. }
  910. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConfig)
  911. if err != nil {
  912. msg = "配置信息错误"
  913. errMsg = "图表配置信息错误,Err:" + err.Error()
  914. return
  915. }
  916. // 获取图表数据
  917. if len(edbInfoMappingList) == 0 {
  918. msg = "获取失败"
  919. errMsg = "图表没有指标,无法计算"
  920. return
  921. }
  922. // 获取图表x轴y轴
  923. edbList, xEdbIdValue, dataResp, e := range_analysis.GetChartDataByEdbInfoList(chartInfoId, dateType, startYear, startDate, endDate, edbInfoMappingList, &extraConfig)
  924. if e != nil {
  925. msg = "获取失败"
  926. errMsg = "获取区间计算图表, 图表计算值失败, Err:" + e.Error()
  927. return
  928. }
  929. // 图表的指标来源
  930. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  931. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  932. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  933. resp.ChartInfo = chartInfo
  934. resp.EdbInfoList = edbList
  935. resp.XEdbIdValue = xEdbIdValue
  936. resp.DataResp = dataResp
  937. if utils.Re == nil {
  938. jsonData, _ := json.Marshal(resp)
  939. utils.Rc.Put(key, jsonData, 10*time.Minute)
  940. }
  941. isOk = true
  942. return
  943. }