chart_common.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_chart_lib/models"
  6. "hongze/hongze_chart_lib/models/data_manage"
  7. correlationServ "hongze/hongze_chart_lib/services/data/correlation"
  8. future_goodServ "hongze/hongze_chart_lib/services/data/future_good"
  9. "hongze/hongze_chart_lib/utils"
  10. "strings"
  11. "time"
  12. )
  13. // CommonChartInfoDetailFromUniqueCode
  14. // @Title 根据编码获取图表详情
  15. // @Description 根据编码获取图表详情接口
  16. // @Param UniqueCode query int true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  17. // @Param IsCache query bool true "是否走缓存,默认false"
  18. // @Success 200 {object} data_manage.ChartInfoDetailFromUniqueCodeResp
  19. // @router /common/detail [get]
  20. func (this *ChartController) CommonChartInfoDetailFromUniqueCode() {
  21. br := new(models.BaseResponse).Init()
  22. defer func() {
  23. this.Data["json"] = br
  24. this.ServeJSON()
  25. }()
  26. uniqueCode := this.GetString("UniqueCode")
  27. if uniqueCode == "" {
  28. br.Msg = "参数错误"
  29. br.ErrMsg = "参数错误,uniqueCode is empty"
  30. return
  31. }
  32. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  33. resp := new(models.ChartInfoDetailResp)
  34. //判断是否有缓存
  35. if utils.Re == nil {
  36. if utils.Re == nil && utils.Rc.IsExist(key) {
  37. if data, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  38. err := json.Unmarshal(data, &resp)
  39. if err == nil && resp != nil {
  40. br.Ret = 200
  41. br.Success = true
  42. br.Msg = "获取成功"
  43. br.Data = resp
  44. fmt.Println("source redis")
  45. return
  46. }
  47. }
  48. }
  49. }
  50. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  51. if err != nil {
  52. if err.Error() == utils.ErrNoRow() {
  53. br.Msg = "该图已被删除,请刷新页面"
  54. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  55. return
  56. }
  57. br.Msg = "获取失败"
  58. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  59. return
  60. }
  61. switch chartInfo.Source {
  62. case utils.CHART_SOURCE_DEFAULT:
  63. resp, isOk, msg, errMsg := GetChartInfoDetailFromUniqueCode(chartInfo, key)
  64. if !isOk {
  65. br.Msg = msg
  66. br.ErrMsg = errMsg
  67. return
  68. }
  69. br.Ret = 200
  70. br.Success = true
  71. br.Msg = "获取成功"
  72. br.Data = resp
  73. case utils.CHART_SOURCE_FUTURE_GOOD:
  74. resp, isOk, msg, errMsg := GetFutureGoodChartInfoDetailFromUniqueCode(chartInfo, key)
  75. if !isOk {
  76. br.Msg = msg
  77. br.ErrMsg = errMsg
  78. return
  79. }
  80. br.Ret = 200
  81. br.Success = true
  82. br.Msg = "获取成功"
  83. br.Data = resp
  84. case utils.CHART_SOURCE_CORRELATION:
  85. resp, isOk, msg, errMsg := GetCorrelationChartInfoDetailFromUniqueCode(chartInfo, key)
  86. if !isOk {
  87. br.Msg = msg
  88. br.ErrMsg = errMsg
  89. return
  90. }
  91. br.Ret = 200
  92. br.Success = true
  93. br.Msg = "获取成功"
  94. br.Data = resp
  95. default:
  96. br.Msg = "错误的图表"
  97. br.ErrMsg = "错误的图表"
  98. return
  99. }
  100. }
  101. // GetFutureGoodChartInfoDetailFromUniqueCode 根据编码获取图表详情
  102. func GetFutureGoodChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfo, key string) (resp *models.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  103. resp = new(models.ChartInfoDetailResp)
  104. chartInfoId := chartInfo.ChartInfoId
  105. startDate := chartInfo.StartDate
  106. endDate := chartInfo.EndDate
  107. // 兼容日期错误
  108. {
  109. if strings.Count(startDate, "-") == 1 {
  110. startDate = startDate + "-01"
  111. }
  112. if strings.Count(endDate, "-") == 1 {
  113. endDate = endDate + "-01"
  114. }
  115. }
  116. edbInfoMapping, err := models.GetEtaEdbChartEdbMapping(chartInfoId)
  117. if err != nil {
  118. msg = "获取失败"
  119. errMsg = "获取图表,现货指标信息失败,Err:" + err.Error()
  120. return
  121. }
  122. futureGoodEdbInfoMapping, err := models.GetFutureGoodEdbChartEdbMapping(chartInfoId)
  123. if err != nil {
  124. msg = "获取失败"
  125. errMsg = "获取图表的期货商品指标信息失败,Err:" + err.Error()
  126. return
  127. }
  128. // 商品价格曲线图的一些配置
  129. var barConfig data_manage.BarChartInfoReq
  130. barChartInfoDateList := make([]data_manage.BarChartInfoDateReq, 0)
  131. //barChartInfoSort := data_manage.BarChartInfoSortReq{}
  132. if chartInfo.BarConfig == `` {
  133. msg = "商品价格曲线图未配置"
  134. errMsg = "商品价格曲线图未配置"
  135. return
  136. }
  137. err = json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  138. if err != nil {
  139. msg = "商品价格曲线图配置异常"
  140. errMsg = "商品价格曲线图配置异常"
  141. return
  142. }
  143. barChartInfoDateList = barConfig.DateList
  144. //barChartInfoSort = barConfig.Sort
  145. // 获取图表中的指标数据
  146. barConfigEdbInfoIdList, edbList, xEdbIdValue, xDataList, yDataList, err := future_goodServ.GetChartEdbData(chartInfoId, startDate, endDate, edbInfoMapping, futureGoodEdbInfoMapping, barChartInfoDateList)
  147. if err != nil {
  148. msg = "获取失败"
  149. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  150. return
  151. }
  152. warnEdbList := make([]string, 0)
  153. if len(edbList) <= 0 {
  154. msg = "商品价格曲线图表指标异常"
  155. errMsg = "商品价格曲线图表异常"
  156. return
  157. }
  158. baseEdbInfo := edbList[0] //现货指标
  159. for _, v := range edbList {
  160. if v.IsNullData {
  161. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  162. }
  163. // 指标别名
  164. if barConfigEdbInfoIdList != nil && len(barConfigEdbInfoIdList) > 0 {
  165. for _, reqEdb := range barConfigEdbInfoIdList {
  166. if v.EdbInfoId == reqEdb.EdbInfoId {
  167. v.EdbAliasName = reqEdb.Name
  168. v.EdbAliasNameEn = reqEdb.NameEn
  169. }
  170. }
  171. }
  172. }
  173. chartInfo.UnitEn = baseEdbInfo.UnitEn
  174. resp.ChartInfo = chartInfo
  175. resp.EdbInfoList = edbList
  176. resp.XEdbIdValue = xEdbIdValue
  177. resp.YDataList = yDataList
  178. resp.XDataList = xDataList
  179. //resp.BarChartInfo = barConfig
  180. //resp.Status = true
  181. // 将数据加入缓存
  182. if utils.Re == nil {
  183. data, _ := json.Marshal(resp)
  184. utils.Rc.Put(key, data, 2*time.Hour)
  185. }
  186. isOk = true
  187. return
  188. }
  189. // FutureGoodChartInfoRefresh
  190. // @Title 商品价格图表刷新接口
  191. // @Description 商品价格图表刷新接口
  192. // @Param UniqueCode query string true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  193. // @Success Ret=200 刷新成功
  194. // @router /future_good/refresh [get]
  195. func (this *ChartController) FutureGoodChartInfoRefresh() {
  196. br := new(models.BaseResponse).Init()
  197. chartId := 0
  198. defer func() {
  199. // 添加日志
  200. if chartId > 0 {
  201. shareChartRefreshLogInfo := &models.ShareChartRefreshLog{
  202. Ip: this.Ctx.Input.IP(),
  203. ChartId: chartId,
  204. CreateTime: time.Now(),
  205. }
  206. models.AddShareChartRefreshLog(shareChartRefreshLogInfo)
  207. }
  208. this.Data["json"] = br
  209. this.ServeJSON()
  210. }()
  211. uniqueCode := this.GetString("UniqueCode")
  212. if uniqueCode == "" {
  213. br.Msg = "参数错误"
  214. br.ErrMsg = "参数错误,uniqueCode is empty"
  215. return
  216. }
  217. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  218. if err != nil {
  219. if err.Error() == utils.ErrNoRow() {
  220. br.Msg = "该图已被删除,请刷新页面"
  221. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  222. return
  223. }
  224. br.Msg = "获取失败"
  225. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  226. return
  227. }
  228. chartId = chartInfo.ChartInfoId
  229. err = future_goodServ.FutureGoodChartInfoRefresh(chartInfo.ChartInfoId)
  230. if err != nil {
  231. br.Msg = "刷新失败"
  232. br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  233. return
  234. }
  235. //err = data.ChartInfoRefresh(chartInfo.ChartInfoId)
  236. //if err != nil {
  237. // br.Msg = "刷新失败"
  238. // br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  239. // return
  240. //}
  241. //err = data.ChartInfoRefreshV2(chartInfo.ChartInfoId)
  242. //if err != nil {
  243. // br.Msg = "刷新失败"
  244. // br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  245. // return
  246. //}
  247. //清除数据缓存
  248. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  249. if utils.Re == nil {
  250. utils.Rc.Delete(key)
  251. }
  252. br.Ret = 200
  253. br.Success = true
  254. br.Msg = "刷新成功"
  255. }
  256. // GetCorrelationChartInfoDetailFromUniqueCode 根据编码获取相关性图表详情
  257. func GetCorrelationChartInfoDetailFromUniqueCode(chartInfo *models.ChartInfo, key string) (resp *models.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  258. resp = new(models.ChartInfoDetailResp)
  259. chartInfoId := chartInfo.ChartInfoId
  260. startDate := chartInfo.StartDate
  261. endDate := chartInfo.EndDate
  262. // 兼容日期错误
  263. {
  264. if strings.Count(startDate, "-") == 1 {
  265. startDate = startDate + "-01"
  266. }
  267. if strings.Count(endDate, "-") == 1 {
  268. endDate = endDate + "-01"
  269. }
  270. }
  271. // 相关性图表信息
  272. correlationChart := new(data_manage.ChartInfoCorrelation)
  273. if e := correlationChart.GetItemById(chartInfoId); e != nil {
  274. msg = "获取失败"
  275. errMsg = "获取图表相关性信息失败, Err:" + e.Error()
  276. return
  277. }
  278. if correlationChart.PeriodData == "" || correlationChart.CorrelationData == "" {
  279. msg = "获取失败"
  280. errMsg = "相关性图表数据有误"
  281. return
  282. }
  283. //chartInfo.CorrelationLeadUnit = correlationChart.LeadUnit
  284. xData := make([]int, 0)
  285. yData := make([]float64, 0)
  286. if e := json.Unmarshal([]byte(correlationChart.PeriodData), &xData); e != nil {
  287. msg = "获取失败"
  288. errMsg = "相关性图表X轴数据有误, Err:" + e.Error()
  289. return
  290. }
  291. if e := json.Unmarshal([]byte(correlationChart.CorrelationData), &yData); e != nil {
  292. msg = "获取失败"
  293. errMsg = "相关性图表X轴数据有误, Err:" + e.Error()
  294. return
  295. }
  296. // 获取指标信息
  297. edbInfoMappingA, e := models.GetChartEdbMappingByEdbInfoId(correlationChart.EdbInfoIdFirst)
  298. if e != nil {
  299. msg = "获取失败"
  300. errMsg = "获取相关性图表, A指标mapping信息失败, Err:" + e.Error()
  301. return
  302. }
  303. edbInfoMappingB, e := models.GetChartEdbMappingByEdbInfoId(correlationChart.EdbInfoIdSecond)
  304. if e != nil {
  305. msg = "获取失败"
  306. errMsg = "获取相关性图表, B指标mapping信息失败, Err:" + e.Error()
  307. return
  308. }
  309. xEdbIdValue := xData
  310. yDataList := make([]models.YData, 0)
  311. yDate := "0000-00-00"
  312. yDataList = append(yDataList, models.YData{
  313. Date: yDate,
  314. Value: yData,
  315. })
  316. // 完善指标信息
  317. edbList, e := correlationServ.GetChartEdbInfoFormat(chartInfo.ChartInfoId, edbInfoMappingA, edbInfoMappingB)
  318. if e != nil {
  319. msg = "获取失败"
  320. errMsg = "获取相关性图表, 完善指标信息失败, Err:" + e.Error()
  321. return
  322. }
  323. correlationInfo := new(models.CorrelationInfo)
  324. correlationInfo.LeadValue = correlationChart.LeadValue
  325. correlationInfo.LeadUnit = correlationChart.LeadUnit
  326. correlationInfo.StartDate = correlationChart.StartDate.Format(utils.FormatDate)
  327. correlationInfo.EndDate = correlationChart.EndDate.Format(utils.FormatDate)
  328. correlationInfo.LeadValue = correlationChart.LeadValue
  329. correlationInfo.EdbInfoIdFirst = correlationChart.EdbInfoIdFirst
  330. correlationInfo.EdbInfoIdSecond = correlationChart.EdbInfoIdSecond
  331. resp.ChartInfo = chartInfo
  332. resp.EdbInfoList = edbList
  333. resp.XEdbIdValue = xEdbIdValue
  334. resp.YDataList = yDataList
  335. resp.CorrelationChartInfo = correlationInfo
  336. // 将数据加入缓存
  337. if utils.Re == nil {
  338. d, _ := json.Marshal(resp)
  339. _ = utils.Rc.Put(key, d, 2*time.Hour)
  340. }
  341. isOk = true
  342. return
  343. }
  344. // CorrelationChartInfoRefresh
  345. // @Title 商品价格图表刷新接口
  346. // @Description 商品价格图表刷新接口
  347. // @Param UniqueCode query string true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  348. // @Success Ret=200 刷新成功
  349. // @router /correlation/refresh [get]
  350. func (this *ChartController) CorrelationChartInfoRefresh() {
  351. br := new(models.BaseResponse).Init()
  352. chartId := 0
  353. defer func() {
  354. // 添加日志
  355. if chartId > 0 {
  356. shareChartRefreshLogInfo := &models.ShareChartRefreshLog{
  357. Ip: this.Ctx.Input.IP(),
  358. ChartId: chartId,
  359. CreateTime: time.Now(),
  360. }
  361. models.AddShareChartRefreshLog(shareChartRefreshLogInfo)
  362. }
  363. this.Data["json"] = br
  364. this.ServeJSON()
  365. }()
  366. uniqueCode := this.GetString("UniqueCode")
  367. if uniqueCode == "" {
  368. br.Msg = "参数错误"
  369. br.ErrMsg = "参数错误,uniqueCode is empty"
  370. return
  371. }
  372. chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode)
  373. if err != nil {
  374. if err.Error() == utils.ErrNoRow() {
  375. br.Msg = "该图已被删除,请刷新页面"
  376. br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error()
  377. return
  378. }
  379. br.Msg = "获取失败"
  380. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  381. return
  382. }
  383. chartId = chartInfo.ChartInfoId
  384. // 刷新相关性图表
  385. if e := correlationServ.ChartInfoRefresh(chartInfo.ChartInfoId); e != nil {
  386. br.Msg = "刷新失败"
  387. br.ErrMsg = "刷新相关性图表失败, Err:" + e.Error()
  388. return
  389. }
  390. //清除数据缓存
  391. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  392. if utils.Re == nil {
  393. _ = utils.Rc.Delete(key)
  394. }
  395. br.Ret = 200
  396. br.Success = true
  397. br.Msg = "刷新成功"
  398. }