chart_common.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. package chart
  2. import (
  3. "context"
  4. "encoding/json"
  5. "github.com/gin-gonic/gin"
  6. "hongze/hongze_yb/controller/response"
  7. "hongze/hongze_yb/global"
  8. "hongze/hongze_yb/models/request"
  9. responseModel "hongze/hongze_yb/models/response"
  10. "hongze/hongze_yb/models/response/chart_info"
  11. chartEdbMappingModel "hongze/hongze_yb/models/tables/chart_edb_mapping"
  12. chartInfoModel "hongze/hongze_yb/models/tables/chart_info"
  13. "hongze/hongze_yb/models/tables/chart_info_correlation"
  14. "hongze/hongze_yb/models/tables/chart_info_log"
  15. "hongze/hongze_yb/models/tables/yb_my_chart"
  16. "hongze/hongze_yb/services/chart"
  17. "hongze/hongze_yb/services/chart/correlation"
  18. future_goodServ "hongze/hongze_yb/services/chart/future_good"
  19. "hongze/hongze_yb/services/user"
  20. "hongze/hongze_yb/utils"
  21. "io/ioutil"
  22. "strconv"
  23. "strings"
  24. "time"
  25. )
  26. // CommonChartInfoDetailFromUniqueCode 获取图表详情(通用)
  27. // @Tags 图库模块
  28. // @Summary 获取图表详情
  29. // @Description 获取图表详情
  30. // @Security ApiKeyAuth
  31. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  32. // @Accept json
  33. // @Product json
  34. // @Param DateType query string false "时间段:1-00年至今; 2-10年至今; 3-15年至今; 4-21年至今; 5-指定区间; 6-指定年月至今; 7-18年至今; 8-19年至今; 9-20年至今"
  35. // @Param ClassifyId query string false "图表分类ID"
  36. // @Success 200 {object} chart_info.ChartInfoDetailResp
  37. // @failure 400 {string} string "图表详情获取失败"
  38. // @Router /my_chart/common/detail [get]
  39. func CommonChartInfoDetailFromUniqueCode(c *gin.Context) {
  40. // 图表ID
  41. reqChartInfoId := c.DefaultQuery("ChartInfoId", "")
  42. if reqChartInfoId == "" {
  43. response.Fail("参数有误:图表ID", c)
  44. return
  45. }
  46. chartInfoId, _ := strconv.Atoi(reqChartInfoId)
  47. reqMyChartClassifyId := c.DefaultQuery("MyChartClassifyId", "")
  48. myChartClassifyId, _ := strconv.Atoi(reqMyChartClassifyId)
  49. chartInfo := new(chartInfoModel.ChartInfoView)
  50. chartInfo, err := chartInfoModel.GetChartInfoViewById(chartInfoId)
  51. if err != nil {
  52. if err == utils.ErrNoRow {
  53. response.FailMsg("图表不存在,请刷新页面", "图表不存在,请刷新页面", c)
  54. return
  55. }
  56. response.FailMsg(`获取失败`, "获取图表信息失败, Err:"+err.Error(), c)
  57. return
  58. }
  59. switch chartInfo.Source {
  60. case utils.CHART_SOURCE_DEFAULT:
  61. resp, isOk, msg, errMsg := getChartInfoDetail(chartInfo, myChartClassifyId, user.GetInfoByClaims(c))
  62. if !isOk {
  63. response.FailMsg(msg, errMsg, c)
  64. return
  65. }
  66. response.OkData("获取成功", resp, c)
  67. return
  68. case utils.CHART_SOURCE_FUTURE_GOOD:
  69. resp, isOk, msg, errMsg := getFutureGoodChartInfoDetail(chartInfo, myChartClassifyId, user.GetInfoByClaims(c))
  70. if !isOk {
  71. response.FailMsg(msg, errMsg, c)
  72. return
  73. }
  74. response.OkData("获取成功", resp, c)
  75. return
  76. case utils.CHART_SOURCE_CORRELATION:
  77. resp, isOk, msg, errMsg := getCorrelationChartInfoDetail(chartInfo, myChartClassifyId, user.GetInfoByClaims(c))
  78. if !isOk {
  79. response.FailMsg(msg, errMsg, c)
  80. return
  81. }
  82. response.OkData("获取成功", resp, c)
  83. return
  84. default:
  85. msg := "错误的图表"
  86. errMsg := "错误的图表"
  87. response.FailMsg(msg, errMsg, c)
  88. return
  89. }
  90. }
  91. // getFutureGoodChartInfoDetail 获取商品价格曲线图表详情
  92. func getFutureGoodChartInfoDetail(chartInfo *chartInfoModel.ChartInfoView, myChartClassifyId int, userInfo user.UserInfo) (resp *chart_info.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  93. resp = new(chart_info.ChartInfoDetailResp)
  94. // 获取图表信息
  95. var err error
  96. chartInfoId := chartInfo.ChartInfoId
  97. startDate := chartInfo.StartDate
  98. endDate := chartInfo.EndDate
  99. // 兼容日期错误
  100. {
  101. if strings.Count(startDate, "-") == 1 {
  102. startDate = startDate + "-01"
  103. }
  104. if strings.Count(endDate, "-") == 1 {
  105. endDate = endDate + "-01"
  106. }
  107. }
  108. edbInfoMapping, err := chartEdbMappingModel.GetEtaEdbChartEdbMapping(chartInfoId)
  109. if err != nil {
  110. msg = "获取失败"
  111. errMsg = "获取图表,现货指标信息失败,Err:" + err.Error()
  112. return
  113. }
  114. futureGoodEdbInfoMapping, err := chartEdbMappingModel.GetFutureGoodEdbChartEdbMapping(chartInfoId)
  115. if err != nil {
  116. msg = "获取失败"
  117. errMsg = "获取图表的期货商品指标信息失败,Err:" + err.Error()
  118. return
  119. }
  120. // 商品价格曲线图的一些配置
  121. var barConfig request.BarChartInfoReq
  122. barChartInfoDateList := make([]request.BarChartInfoDateReq, 0)
  123. barChartInfoSort := request.BarChartInfoSortReq{}
  124. if chartInfo.BarConfig == `` {
  125. msg = "商品价格曲线图未配置"
  126. errMsg = "商品价格曲线图未配置"
  127. return
  128. }
  129. err = json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  130. if err != nil {
  131. msg = "商品价格曲线图配置异常"
  132. errMsg = "商品价格曲线图配置异常"
  133. return
  134. }
  135. barChartInfoDateList = barConfig.DateList
  136. barChartInfoSort = barConfig.Sort
  137. // 获取图表中的指标数据
  138. barConfigEdbInfoIdList, edbList, xEdbIdValue, yDataList, sourceArr, err := future_goodServ.GetChartEdbData(chartInfoId, startDate, endDate, edbInfoMapping, futureGoodEdbInfoMapping, barChartInfoDateList, barChartInfoSort)
  139. if err != nil {
  140. msg = "获取失败"
  141. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  142. return
  143. }
  144. if len(edbList) <= 0 {
  145. msg = "商品价格曲线图表指标异常"
  146. errMsg = "商品价格曲线图表异常"
  147. return
  148. }
  149. sourceArr = append(sourceArr, "弘则研究")
  150. chartInfo.ChartSource = strings.Join(sourceArr, ",")
  151. for _, v := range edbList {
  152. // 指标别名
  153. if barConfigEdbInfoIdList != nil && len(barConfigEdbInfoIdList) > 0 {
  154. for _, reqEdb := range barConfigEdbInfoIdList {
  155. if v.EdbInfoId == reqEdb.EdbInfoId {
  156. v.EdbAliasName = reqEdb.Name
  157. v.EdbAliasNameEn = reqEdb.NameEn
  158. }
  159. }
  160. }
  161. }
  162. baseEdbInfo := edbList[0] //现货指标
  163. chartInfo.UnitEn = baseEdbInfo.UnitEn
  164. // 访问记录-仅普通用户记录
  165. ok, _, _ := user.GetAdminByUserInfo(userInfo)
  166. if !ok {
  167. go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
  168. }
  169. // 用户是否有收藏该图表
  170. {
  171. ob := new(yb_my_chart.YbMyChart)
  172. cond := `user_id = ? AND chart_info_id = ?`
  173. pars := make([]interface{}, 0)
  174. pars = append(pars, userInfo.UserID, chartInfo.ChartInfoId)
  175. exists, e := ob.FetchByCondition(cond, pars)
  176. if e != nil && e != utils.ErrNoRow {
  177. msg = `操作失败`
  178. errMsg = "获取用户图表失败, Err: " + e.Error()
  179. return
  180. }
  181. myChartInfo := new(responseModel.MyChartItem)
  182. if exists != nil && exists.MyChartID > 0 {
  183. myChartInfo.MyChartID = exists.MyChartID
  184. myChartInfo.MyChartClassifyID = exists.MyChartClassifyID
  185. myChartInfo.ChartInfoID = exists.ChartInfoID
  186. myChartInfo.ChartName = exists.ChartName
  187. myChartInfo.UniqueCode = exists.UniqueCode
  188. myChartInfo.ChartImage = exists.ChartImage
  189. myChartInfo.UserID = exists.UserID
  190. myChartInfo.ReportID = exists.ReportID
  191. myChartInfo.ReportChapterID = exists.ReportChapterID
  192. myChartInfo.CreateTime = utils.TimeTransferString(utils.FormatDateTime, exists.CreateTime)
  193. }
  194. resp.MyChartInfo = myChartInfo
  195. }
  196. resp.ChartInfo = chartInfo
  197. resp.EdbInfoList = edbList
  198. resp.XEdbIdValue = xEdbIdValue
  199. resp.YDataList = yDataList
  200. isOk = true
  201. return
  202. }
  203. // RefreshFutureGoodChartInfo 刷新商品价格曲线图表信息
  204. // @Tags 图库模块
  205. // @Summary 刷新图表信息
  206. // @Description 刷新图表信息
  207. // @Security ApiKeyAuth
  208. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  209. // @Accept json
  210. // @Product json
  211. // @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
  212. // @Success 200 {string} string "操作成功"
  213. // @failure 400 {string} string "操作失败"
  214. // @Router /my_chart/future_good/refreshChartInfo [post]
  215. func RefreshFutureGoodChartInfo(c *gin.Context) {
  216. // 参数校验
  217. var req chartInfoModel.RefreshChartInfoReq
  218. if c.ShouldBind(&req) != nil {
  219. response.Fail("参数异常", c)
  220. return
  221. }
  222. chartInfoId := req.ChartInfoId
  223. if chartInfoId == 0 {
  224. response.Fail("参数有误", c)
  225. return
  226. }
  227. //userInfo := user.GetInfoByClaims(c)
  228. //ok, _, err := user.GetAdminByUserInfo(userInfo)
  229. //if err != nil {
  230. // response.FailMsg("刷新失败", "RefreshChartInfo-获取系统用户信息失败"+err.Error(), c)
  231. // return
  232. //}
  233. //if !ok {
  234. // // 普通用户刷新频率限制-每个用户/图/天/2次
  235. // cacheKey := utils.HZ_CHART_LIB_DETAIL + "YB_REFRESH_LIMIT_" + strconv.Itoa(chartInfoId) + "_" + strconv.Itoa(int(userInfo.UserID))
  236. // fmt.Println("refreshCacheKey:", cacheKey)
  237. // countUserRefresh, _ := global.Redis.Get(context.TODO(), cacheKey).Int()
  238. // if countUserRefresh >= 2 {
  239. // response.Ok("目前已是最新数据", c)
  240. // return
  241. // }
  242. // countUserRefresh += 1
  243. // now := time.Now()
  244. // today := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.Local)
  245. // sub := today.Sub(now)
  246. // _ = global.Redis.SetEX(context.TODO(), cacheKey, countUserRefresh, sub)
  247. //}
  248. // 图表信息校验
  249. chartInfo, err := chartInfoModel.GetChartInfoById(chartInfoId)
  250. if err != nil {
  251. if err == utils.ErrNoRow {
  252. response.Fail("图表已被删除,无需刷新", c)
  253. return
  254. }
  255. response.FailMsg("刷新失败", "刷新失败, Err:"+err.Error(), c)
  256. return
  257. }
  258. // 刷新图表
  259. //if err = chart.ChartInfoRefreshV2(chartInfo.ChartInfoId); err != nil {
  260. // errContent := fmt.Sprint("ErrMsg: 刷新图表关联指标信息失败, " + err.Error())
  261. // if global.CONFIG.Serve.RunMode == "release" {
  262. // go alarm_msg.SendAlarmMsg("刷新图表报错"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+errContent, 3)
  263. // //go services.SendEmail("弘则研报小程序-release-刷新图表报错", errContent, utils.EmailSendToUsers)
  264. // } else {
  265. // global.LOG.Info(errContent)
  266. // }
  267. //}
  268. //清除图表缓存
  269. {
  270. key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
  271. _ = global.Redis.Del(context.TODO(), key)
  272. }
  273. response.OkData("刷新成功", "", c)
  274. }
  275. // FutureGoodChartInfoSave 编辑商品价格图表信息
  276. // @Tags 图库模块
  277. // @Summary 编辑图表信息
  278. // @Description 编辑图表信息
  279. // @Security ApiKeyAuth
  280. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  281. // @Accept json
  282. // @Product json
  283. // @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
  284. // @Success 200 {string} string "操作成功"
  285. // @failure 400 {string} string "操作失败"
  286. // @Router /my_chart/editChartInfo [post]
  287. func FutureGoodChartInfoSave(c *gin.Context) {
  288. // 参数校验
  289. var req chartInfoModel.SaveChartInfoReq
  290. if c.ShouldBind(&req) != nil {
  291. response.Fail("参数异常", c)
  292. return
  293. }
  294. if req.ChartInfoId <= 0 {
  295. response.Fail("参数异常", c)
  296. return
  297. }
  298. // 操作权限校验
  299. userInfo := user.GetInfoByClaims(c)
  300. ok, adminInfo, err := user.GetAdminByUserInfo(userInfo)
  301. if err != nil {
  302. response.Fail("操作人信息有误", c)
  303. return
  304. }
  305. if !ok {
  306. response.Fail("非内部人员无权进行操作", c)
  307. return
  308. }
  309. // 图表信息校验
  310. chartItem, err := chartInfoModel.GetChartInfoById(req.ChartInfoId)
  311. if err != nil {
  312. if err == utils.ErrNoRow {
  313. response.Fail("图表已被删除,请刷新页面!", c)
  314. return
  315. }
  316. response.FailMsg("操作失败", "获取图表信息失败, Err:"+err.Error(), c)
  317. return
  318. }
  319. if chartItem.Source != 2 {
  320. response.Fail("该图不是商品价格曲线图", c)
  321. return
  322. }
  323. chartItem.LeftMin = req.LeftMin
  324. chartItem.LeftMax = req.LeftMax
  325. err = chartItem.Update([]string{"LeftMin", "LeftMax"})
  326. if err != nil {
  327. response.FailMsg("保存失败", "保存失败,Err:"+err.Error(), c)
  328. return
  329. }
  330. // 清除图表缓存
  331. cacheKey := utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode
  332. _ = global.Redis.Del(context.TODO(), cacheKey)
  333. // 新增操作日志
  334. {
  335. chartLog := new(chart_info_log.ChartInfoLog)
  336. chartLog.ChartName = chartItem.ChartName
  337. chartLog.ChartInfoId = req.ChartInfoId
  338. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  339. chartLog.SysUserId = int(adminInfo.AdminID)
  340. chartLog.SysUserRealName = adminInfo.RealName
  341. chartLog.UniqueCode = chartItem.UniqueCode
  342. chartLog.CreateTime = time.Now()
  343. bodyBytes, _ := ioutil.ReadAll(c.Request.Body)
  344. chartLog.Content = string(bodyBytes)
  345. chartLog.Status = "修改配置项"
  346. chartLog.Method = c.Request.URL.String()
  347. go chartLog.Create()
  348. }
  349. response.OkData("操作成功", "", c)
  350. }
  351. // getCorrelationChartInfoDetail 获取相关性图表详情
  352. func getCorrelationChartInfoDetail(chartInfo *chartInfoModel.ChartInfoView, myChartClassifyId int, userInfo user.UserInfo) (resp *chart_info.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  353. resp = new(chart_info.ChartInfoDetailResp)
  354. // 获取图表信息
  355. //var err error
  356. chartInfoId := chartInfo.ChartInfoId
  357. chartInfo.ChartSource = "弘则研究"
  358. startDate := chartInfo.StartDate
  359. endDate := chartInfo.EndDate
  360. // 兼容日期错误
  361. {
  362. if strings.Count(startDate, "-") == 1 {
  363. startDate = startDate + "-01"
  364. }
  365. if strings.Count(endDate, "-") == 1 {
  366. endDate = endDate + "-01"
  367. }
  368. }
  369. // 相关性图表信息
  370. correlationChart := new(chart_info_correlation.ChartInfoCorrelation)
  371. if e := correlationChart.GetItemById(chartInfoId); e != nil {
  372. msg = "获取失败"
  373. errMsg = "获取图表相关性信息失败, Err:" + e.Error()
  374. return
  375. }
  376. if correlationChart.PeriodData == "" || correlationChart.CorrelationData == "" {
  377. msg = "获取失败"
  378. errMsg = "相关性图表数据有误"
  379. return
  380. }
  381. //chartInfo.CorrelationLeadUnit = correlationChart.LeadUnit
  382. xData := make([]int, 0)
  383. yData := make([]float64, 0)
  384. if e := json.Unmarshal([]byte(correlationChart.PeriodData), &xData); e != nil {
  385. msg = "获取失败"
  386. errMsg = "相关性图表X轴数据有误, Err:" + e.Error()
  387. return
  388. }
  389. if e := json.Unmarshal([]byte(correlationChart.CorrelationData), &yData); e != nil {
  390. msg = "获取失败"
  391. errMsg = "相关性图表X轴数据有误, Err:" + e.Error()
  392. return
  393. }
  394. // 获取指标信息
  395. edbInfoMappingA, e := chartEdbMappingModel.GetChartEdbMappingByEdbInfoId(correlationChart.EdbInfoIdFirst)
  396. if e != nil {
  397. msg = "获取失败"
  398. errMsg = "获取相关性图表, A指标mapping信息失败, Err:" + e.Error()
  399. return
  400. }
  401. edbInfoMappingB, e := chartEdbMappingModel.GetChartEdbMappingByEdbInfoId(correlationChart.EdbInfoIdSecond)
  402. if e != nil {
  403. msg = "获取失败"
  404. errMsg = "获取相关性图表, B指标mapping信息失败, Err:" + e.Error()
  405. return
  406. }
  407. xEdbIdValue := xData
  408. yDataList := make([]chart_info.YData, 0)
  409. yDate := "0000-00-00"
  410. yDataList = append(yDataList, chart_info.YData{
  411. Date: yDate,
  412. Value: yData,
  413. })
  414. // 完善指标信息
  415. edbList, e := correlation.GetChartEdbInfoFormat(chartInfo.ChartInfoId, edbInfoMappingA, edbInfoMappingB)
  416. if e != nil {
  417. msg = "获取失败"
  418. errMsg = "获取相关性图表, 完善指标信息失败, Err:" + e.Error()
  419. return
  420. }
  421. baseEdbInfo := edbList[0] //现货指标
  422. chartInfo.UnitEn = baseEdbInfo.UnitEn
  423. correlationInfo := new(chart_info.CorrelationInfo)
  424. correlationInfo.LeadValue = correlationChart.LeadValue
  425. correlationInfo.LeadUnit = correlationChart.LeadUnit
  426. correlationInfo.StartDate = correlationChart.StartDate.Format(utils.FormatDate)
  427. correlationInfo.EndDate = correlationChart.EndDate.Format(utils.FormatDate)
  428. correlationInfo.LeadValue = correlationChart.LeadValue
  429. correlationInfo.EdbInfoIdFirst = correlationChart.EdbInfoIdFirst
  430. correlationInfo.EdbInfoIdSecond = correlationChart.EdbInfoIdSecond
  431. // 访问记录-仅普通用户记录
  432. ok, _, _ := user.GetAdminByUserInfo(userInfo)
  433. if !ok {
  434. go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
  435. }
  436. // 用户是否有收藏该图表
  437. {
  438. ob := new(yb_my_chart.YbMyChart)
  439. cond := `user_id = ? AND chart_info_id = ?`
  440. pars := make([]interface{}, 0)
  441. pars = append(pars, userInfo.UserID, chartInfo.ChartInfoId)
  442. exists, e := ob.FetchByCondition(cond, pars)
  443. if e != nil && e != utils.ErrNoRow {
  444. msg = `操作失败`
  445. errMsg = "获取用户图表失败, Err: " + e.Error()
  446. return
  447. }
  448. myChartInfo := new(responseModel.MyChartItem)
  449. if exists != nil && exists.MyChartID > 0 {
  450. myChartInfo.MyChartID = exists.MyChartID
  451. myChartInfo.MyChartClassifyID = exists.MyChartClassifyID
  452. myChartInfo.ChartInfoID = exists.ChartInfoID
  453. myChartInfo.ChartName = exists.ChartName
  454. myChartInfo.UniqueCode = exists.UniqueCode
  455. myChartInfo.ChartImage = exists.ChartImage
  456. myChartInfo.UserID = exists.UserID
  457. myChartInfo.ReportID = exists.ReportID
  458. myChartInfo.ReportChapterID = exists.ReportChapterID
  459. myChartInfo.CreateTime = utils.TimeTransferString(utils.FormatDateTime, exists.CreateTime)
  460. }
  461. resp.MyChartInfo = myChartInfo
  462. }
  463. resp.ChartInfo = chartInfo
  464. resp.EdbInfoList = edbList
  465. resp.XEdbIdValue = xEdbIdValue
  466. resp.YDataList = yDataList
  467. resp.CorrelationChartInfo = correlationInfo
  468. isOk = true
  469. return
  470. }
  471. // RefreshCorrelationChartInfo 刷新相关性图表信息
  472. // @Tags 图库模块
  473. // @Summary 刷新图表信息
  474. // @Description 刷新图表信息
  475. // @Security ApiKeyAuth
  476. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  477. // @Accept json
  478. // @Product json
  479. // @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
  480. // @Success 200 {string} string "操作成功"
  481. // @failure 400 {string} string "操作失败"
  482. // @Router /my_chart/correlation/refreshChartInfo [post]
  483. func RefreshCorrelationChartInfo(c *gin.Context) {
  484. // 参数校验
  485. var req chartInfoModel.RefreshChartInfoReq
  486. if c.ShouldBind(&req) != nil {
  487. response.Fail("参数异常", c)
  488. return
  489. }
  490. chartInfoId := req.ChartInfoId
  491. if chartInfoId == 0 {
  492. response.Fail("参数有误", c)
  493. return
  494. }
  495. userInfo := user.GetInfoByClaims(c)
  496. ok, _, err := user.GetAdminByUserInfo(userInfo)
  497. if err != nil {
  498. response.FailMsg("刷新失败", "RefreshChartInfo-获取系统用户信息失败"+err.Error(), c)
  499. return
  500. }
  501. if !ok {
  502. // 普通用户刷新频率限制-每个用户/图/天/2次
  503. cacheKey := utils.HZ_CHART_LIB_DETAIL + "YB_REFRESH_LIMIT_" + strconv.Itoa(chartInfoId) + "_" + strconv.Itoa(int(userInfo.UserID))
  504. countUserRefresh, _ := global.Redis.Get(context.TODO(), cacheKey).Int()
  505. if countUserRefresh >= 2 {
  506. response.Ok("目前已是最新数据", c)
  507. return
  508. }
  509. countUserRefresh += 1
  510. now := time.Now()
  511. today := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.Local)
  512. sub := today.Sub(now)
  513. _ = global.Redis.SetEX(context.TODO(), cacheKey, countUserRefresh, sub)
  514. }
  515. // 图表信息校验
  516. chartInfo, err := chartInfoModel.GetChartInfoById(chartInfoId)
  517. if err != nil {
  518. if err == utils.ErrNoRow {
  519. response.Fail("图表已被删除,无需刷新", c)
  520. return
  521. }
  522. response.FailMsg("刷新失败", "刷新失败, Err:"+err.Error(), c)
  523. return
  524. }
  525. // 刷新图表
  526. _ = correlation.ChartInfoRefresh(chartInfo.ChartInfoId)
  527. //清除图表缓存
  528. {
  529. key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
  530. _ = global.Redis.Del(context.TODO(), key)
  531. }
  532. response.OkData("刷新成功", "", c)
  533. }