chart_common.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. package chart
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "hongze/hongze_yb/controller/response"
  8. "hongze/hongze_yb/global"
  9. "hongze/hongze_yb/models/request"
  10. responseModel "hongze/hongze_yb/models/response"
  11. "hongze/hongze_yb/models/response/chart_info"
  12. chartEdbMappingModel "hongze/hongze_yb/models/tables/chart_edb_mapping"
  13. chartInfoModel "hongze/hongze_yb/models/tables/chart_info"
  14. "hongze/hongze_yb/models/tables/chart_info_correlation"
  15. "hongze/hongze_yb/models/tables/chart_info_future_good_profit"
  16. "hongze/hongze_yb/models/tables/chart_info_log"
  17. "hongze/hongze_yb/models/tables/yb_my_chart"
  18. "hongze/hongze_yb/services/alarm_msg"
  19. "hongze/hongze_yb/services/chart"
  20. "hongze/hongze_yb/services/chart/correlation"
  21. future_goodServ "hongze/hongze_yb/services/chart/future_good"
  22. "hongze/hongze_yb/services/chart/line_equation"
  23. "hongze/hongze_yb/services/user"
  24. "hongze/hongze_yb/utils"
  25. "io/ioutil"
  26. "strconv"
  27. "strings"
  28. "time"
  29. )
  30. // CommonChartInfoDetailFromUniqueCode 获取图表详情(通用)
  31. // @Tags 图库模块
  32. // @Summary 获取图表详情
  33. // @Description 获取图表详情
  34. // @Security ApiKeyAuth
  35. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  36. // @Accept json
  37. // @Product json
  38. // @Param DateType query string false "时间段:1-00年至今; 2-10年至今; 3-15年至今; 4-21年至今; 5-指定区间; 6-指定年月至今; 7-18年至今; 8-19年至今; 9-20年至今"
  39. // @Param ClassifyId query string false "图表分类ID"
  40. // @Success 200 {object} chart_info.ChartInfoDetailResp
  41. // @failure 400 {string} string "图表详情获取失败"
  42. // @Router /my_chart/common/detail [get]
  43. func CommonChartInfoDetailFromUniqueCode(c *gin.Context) {
  44. // 图表ID
  45. reqChartInfoId := c.DefaultQuery("ChartInfoId", "")
  46. if reqChartInfoId == "" {
  47. response.Fail("参数有误:图表ID", c)
  48. return
  49. }
  50. chartInfoId, _ := strconv.Atoi(reqChartInfoId)
  51. reqMyChartClassifyId := c.DefaultQuery("MyChartClassifyId", "")
  52. myChartClassifyId, _ := strconv.Atoi(reqMyChartClassifyId)
  53. chartInfo := new(chartInfoModel.ChartInfoView)
  54. chartInfo, err := chartInfoModel.GetChartInfoViewById(chartInfoId)
  55. if err != nil {
  56. if err == utils.ErrNoRow {
  57. response.Custom(4003, "图表不存在,请刷新页面", c)
  58. return
  59. }
  60. response.FailMsg(`获取失败`, "获取图表信息失败, Err:"+err.Error(), c)
  61. return
  62. }
  63. var resp *chart_info.ChartInfoDetailResp
  64. var isOk bool
  65. var msg, errMsg string
  66. switch chartInfo.Source {
  67. case utils.CHART_SOURCE_DEFAULT:
  68. resp, isOk, msg, errMsg = getChartInfoDetail(chartInfo, myChartClassifyId, user.GetInfoByClaims(c))
  69. case utils.CHART_SOURCE_FUTURE_GOOD:
  70. resp, isOk, msg, errMsg = getFutureGoodChartInfoDetail(chartInfo, myChartClassifyId, user.GetInfoByClaims(c))
  71. case utils.CHART_SOURCE_FUTURE_GOOD_PROFIT:
  72. resp, isOk, msg, errMsg = getFutureGoodProfitChartInfoDetail(chartInfo, myChartClassifyId, user.GetInfoByClaims(c))
  73. case utils.CHART_SOURCE_CORRELATION, utils.CHART_SOURCE_ROLLING_CORRELATION:
  74. resp, isOk, msg, errMsg = getCorrelationChartInfoDetail(chartInfo, myChartClassifyId, user.GetInfoByClaims(c))
  75. case utils.CHART_SOURCE_LINE_EQUATION:
  76. resp, isOk, msg, errMsg = getChartInfoDetailFromUniqueCode(chartInfo, myChartClassifyId, user.GetInfoByClaims(c))
  77. default:
  78. msg := "错误的图表"
  79. errMsg := "错误的图表"
  80. response.FailMsg(msg, errMsg, c)
  81. return
  82. }
  83. if !isOk {
  84. response.FailMsg(msg, errMsg, c)
  85. return
  86. }
  87. // 图表的指标来源
  88. sourceNameList, sourceNameEnList := chart.GetEdbSourceByEdbInfoIdList(resp.EdbInfoList)
  89. resp.ChartInfo.ChartSource = strings.Join(sourceNameList, ",")
  90. resp.ChartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  91. response.OkData("获取成功", resp, c)
  92. return
  93. }
  94. // getFutureGoodChartInfoDetail 获取商品价格曲线图表详情
  95. func getFutureGoodChartInfoDetail(chartInfo *chartInfoModel.ChartInfoView, myChartClassifyId int, userInfo user.UserInfo) (resp *chart_info.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  96. resp = new(chart_info.ChartInfoDetailResp)
  97. // 获取图表信息
  98. var err error
  99. chartInfoId := chartInfo.ChartInfoId
  100. startDate := chartInfo.StartDate
  101. endDate := chartInfo.EndDate
  102. // 兼容日期错误
  103. {
  104. if strings.Count(startDate, "-") == 1 {
  105. startDate = startDate + "-01"
  106. }
  107. if strings.Count(endDate, "-") == 1 {
  108. endDate = endDate + "-01"
  109. }
  110. }
  111. edbInfoMapping, err := chartEdbMappingModel.GetEtaEdbChartEdbMapping(chartInfoId)
  112. if err != nil {
  113. msg = "获取失败"
  114. errMsg = "获取图表,现货指标信息失败,Err:" + err.Error()
  115. return
  116. }
  117. futureGoodEdbInfoMapping, err := chartEdbMappingModel.GetFutureGoodEdbChartEdbMapping(chartInfoId)
  118. if err != nil {
  119. msg = "获取失败"
  120. errMsg = "获取图表的期货商品指标信息失败,Err:" + err.Error()
  121. return
  122. }
  123. // 商品价格曲线图的一些配置
  124. var barConfig request.BarChartInfoReq
  125. barChartInfoDateList := make([]request.BarChartInfoDateReq, 0)
  126. //barChartInfoSort := request.BarChartInfoSortReq{}
  127. if chartInfo.BarConfig == `` {
  128. msg = "商品价格曲线图未配置"
  129. errMsg = "商品价格曲线图未配置"
  130. return
  131. }
  132. err = json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  133. if err != nil {
  134. msg = "商品价格曲线图配置异常"
  135. errMsg = "商品价格曲线图配置异常"
  136. return
  137. }
  138. barChartInfoDateList = barConfig.DateList
  139. //barChartInfoSort = barConfig.Sort
  140. // 获取图表中的指标数据
  141. barConfigEdbInfoIdList, edbList, xEdbIdValue, xDataList, yDataList, sourceArr, err := future_goodServ.GetChartEdbData(chartInfoId, startDate, endDate, edbInfoMapping, futureGoodEdbInfoMapping, barChartInfoDateList)
  142. if err != nil {
  143. msg = "获取失败"
  144. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  145. return
  146. }
  147. if len(edbList) <= 0 {
  148. msg = "商品价格曲线图表指标异常"
  149. errMsg = "商品价格曲线图表异常"
  150. return
  151. }
  152. sourceArr = append(sourceArr, "弘则研究")
  153. chartInfo.ChartSource = strings.Join(sourceArr, ",")
  154. for _, v := range edbList {
  155. // 指标别名
  156. if barConfigEdbInfoIdList != nil && len(barConfigEdbInfoIdList) > 0 {
  157. for _, reqEdb := range barConfigEdbInfoIdList {
  158. if v.EdbInfoId == reqEdb.EdbInfoId {
  159. v.EdbAliasName = reqEdb.Name
  160. v.EdbAliasNameEn = reqEdb.NameEn
  161. }
  162. }
  163. }
  164. }
  165. baseEdbInfo := edbList[0] //现货指标
  166. chartInfo.UnitEn = baseEdbInfo.UnitEn
  167. // 访问记录-仅普通用户记录
  168. ok, _, _ := user.GetAdminByUserInfo(userInfo)
  169. if !ok {
  170. go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
  171. }
  172. // 用户是否有收藏该图表
  173. {
  174. ob := new(yb_my_chart.YbMyChart)
  175. cond := `user_id = ? AND chart_info_id = ?`
  176. pars := make([]interface{}, 0)
  177. pars = append(pars, userInfo.UserID, chartInfo.ChartInfoId)
  178. exists, e := ob.FetchByCondition(cond, pars)
  179. if e != nil && e != utils.ErrNoRow {
  180. msg = `操作失败`
  181. errMsg = "获取用户图表失败, Err: " + e.Error()
  182. return
  183. }
  184. myChartInfo := new(responseModel.MyChartItem)
  185. if exists != nil && exists.MyChartID > 0 {
  186. myChartInfo.MyChartID = exists.MyChartID
  187. myChartInfo.MyChartClassifyID = exists.MyChartClassifyID
  188. myChartInfo.ChartInfoID = exists.ChartInfoID
  189. myChartInfo.ChartName = exists.ChartName
  190. myChartInfo.UniqueCode = exists.UniqueCode
  191. myChartInfo.ChartImage = exists.ChartImage
  192. myChartInfo.UserID = exists.UserID
  193. myChartInfo.ReportID = exists.ReportID
  194. myChartInfo.ReportChapterID = exists.ReportChapterID
  195. myChartInfo.CreateTime = utils.TimeTransferString(utils.FormatDateTime, exists.CreateTime)
  196. }
  197. resp.MyChartInfo = myChartInfo
  198. }
  199. resp.ChartInfo = chartInfo
  200. resp.EdbInfoList = edbList
  201. resp.XEdbIdValue = xEdbIdValue
  202. resp.XDataList = xDataList
  203. resp.YDataList = yDataList
  204. isOk = true
  205. return
  206. }
  207. // getFutureGoodProfitChartInfoDetail 获取商品利润曲线图表详情
  208. func getFutureGoodProfitChartInfoDetail(chartInfo *chartInfoModel.ChartInfoView, myChartClassifyId int, userInfo user.UserInfo) (resp *chart_info.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  209. resp = new(chart_info.ChartInfoDetailResp)
  210. // 获取图表信息
  211. var err error
  212. chartInfoId := chartInfo.ChartInfoId
  213. // 商品利润曲线图的一些配置
  214. var extraConf request.ChartInfoReq
  215. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConf)
  216. if err != nil {
  217. msg = "商品利润曲线图配置异常"
  218. errMsg = "商品利润曲线图配置异常,Err:" + err.Error()
  219. return
  220. }
  221. edbList := make([]*chartEdbMappingModel.ChartEdbInfoMappingList, 0)
  222. edbInfoMapping, err := chartEdbMappingModel.GetEtaEdbChartEdbMapping(chartInfoId)
  223. if err != nil {
  224. msg = "获取失败"
  225. errMsg = "获取图表,现货指标信息失败,Err:" + err.Error()
  226. return
  227. }
  228. edbList = append(edbList, edbInfoMapping)
  229. futureGoodEdbInfoMappingList, err := chartEdbMappingModel.GetFutureGoodEdbChartEdbMappingList(chartInfoId)
  230. if err != nil {
  231. msg = "获取失败"
  232. errMsg = "获取图表的期货商品指标信息失败,Err:" + err.Error()
  233. return
  234. }
  235. edbList = append(edbList, futureGoodEdbInfoMappingList...)
  236. if len(edbList) <= 0 {
  237. msg = "商品利润曲线图表指标异常"
  238. errMsg = "商品利润曲线图表指标异常"
  239. return
  240. }
  241. xDataList := make([]chart_info.XData, 0)
  242. yDataList := make([]chart_info.YData, 0)
  243. // 查找商品利润图表的扩展信息
  244. chartInfoFutureGoodProfit := new(chart_info_future_good_profit.ChartInfoFutureGoodProfit)
  245. if err = chartInfoFutureGoodProfit.GetItemById(chartInfoId); err != nil {
  246. errMsg = "获取相关性图表失败, Err: " + err.Error()
  247. return
  248. }
  249. err = json.Unmarshal([]byte(chartInfoFutureGoodProfit.XValue), &xDataList)
  250. if err != nil {
  251. msg = "获取失败"
  252. errMsg = "转换X轴数据失败, Err: " + err.Error()
  253. return
  254. }
  255. err = json.Unmarshal([]byte(chartInfoFutureGoodProfit.YValue), &yDataList)
  256. if err != nil {
  257. msg = "获取失败"
  258. errMsg = "转换Y轴数据失败, Err: " + err.Error()
  259. return
  260. }
  261. baseEdbInfo := edbList[0] //现货指标
  262. chartInfo.UnitEn = baseEdbInfo.UnitEn
  263. sourceArr := []string{"同花顺"}
  264. if baseEdbInfo.SourceName != "同花顺" {
  265. sourceArr = append(sourceArr, baseEdbInfo.SourceName)
  266. }
  267. sourceArr = append(sourceArr, "弘则研究")
  268. chartInfo.ChartSource = strings.Join(sourceArr, ",")
  269. warnEdbList := make([]string, 0)
  270. for _, v := range edbList {
  271. if v.IsNullData {
  272. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  273. }
  274. }
  275. // 访问记录-仅普通用户记录
  276. ok, _, _ := user.GetAdminByUserInfo(userInfo)
  277. if !ok {
  278. go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
  279. }
  280. // 用户是否有收藏该图表
  281. {
  282. ob := new(yb_my_chart.YbMyChart)
  283. cond := `user_id = ? AND chart_info_id = ?`
  284. pars := make([]interface{}, 0)
  285. pars = append(pars, userInfo.UserID, chartInfo.ChartInfoId)
  286. exists, e := ob.FetchByCondition(cond, pars)
  287. if e != nil && e != utils.ErrNoRow {
  288. msg = `操作失败`
  289. errMsg = "获取用户图表失败, Err: " + e.Error()
  290. return
  291. }
  292. myChartInfo := new(responseModel.MyChartItem)
  293. if exists != nil && exists.MyChartID > 0 {
  294. myChartInfo.MyChartID = exists.MyChartID
  295. myChartInfo.MyChartClassifyID = exists.MyChartClassifyID
  296. myChartInfo.ChartInfoID = exists.ChartInfoID
  297. myChartInfo.ChartName = exists.ChartName
  298. myChartInfo.UniqueCode = exists.UniqueCode
  299. myChartInfo.ChartImage = exists.ChartImage
  300. myChartInfo.UserID = exists.UserID
  301. myChartInfo.ReportID = exists.ReportID
  302. myChartInfo.ReportChapterID = exists.ReportChapterID
  303. myChartInfo.CreateTime = utils.TimeTransferString(utils.FormatDateTime, exists.CreateTime)
  304. }
  305. resp.MyChartInfo = myChartInfo
  306. }
  307. resp.ChartInfo = chartInfo
  308. resp.EdbInfoList = edbList
  309. resp.DataResp = chart_info.ProfitFutureGoodChartResp{
  310. YDataList: yDataList,
  311. XDataList: xDataList,
  312. ProfitName: chartInfoFutureGoodProfit.ProfitName,
  313. ProfitNameEn: chartInfoFutureGoodProfit.ProfitNameEn,
  314. Extra: extraConf,
  315. }
  316. isOk = true
  317. return
  318. }
  319. // getChartInfoDetailFromUniqueCode 获取拟合方程图表详情
  320. func getChartInfoDetailFromUniqueCode(chartInfo *chartInfoModel.ChartInfoView, myChartClassifyId int, userInfo user.UserInfo) (resp *chart_info.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  321. resp = new(chart_info.ChartInfoDetailResp)
  322. // 获取图表信息
  323. var err error
  324. if chartInfo.ExtraConfig == `` {
  325. msg = "获取失败"
  326. errMsg = "获取配置信息失败"
  327. return
  328. }
  329. var lineChartInfoConfig request.LineChartInfoReq
  330. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &lineChartInfoConfig)
  331. if err != nil {
  332. msg = "获取失败"
  333. errMsg = "获取图表配置信息失败, Err:" + err.Error()
  334. return
  335. }
  336. var getAData, getBData, getR2Data bool
  337. switch lineChartInfoConfig.Source {
  338. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_ONE:
  339. getAData = true
  340. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_TWO:
  341. getBData = true
  342. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_THREE:
  343. getR2Data = true
  344. }
  345. edbList, dataResp, sourceArr, err, errMsg := line_equation.GetChartEdbData(chartInfo.ChartInfoId, lineChartInfoConfig, getAData, getBData, getR2Data)
  346. if err != nil {
  347. if errMsg == `` {
  348. errMsg = "获取失败"
  349. }
  350. return
  351. }
  352. var resultResp interface{}
  353. switch lineChartInfoConfig.Source {
  354. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_ONE:
  355. resultResp = dataResp.AData
  356. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_TWO:
  357. resultResp = dataResp.BData
  358. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_THREE:
  359. resultResp = dataResp.R2Data
  360. }
  361. sourceArr = append(sourceArr, "弘则研究")
  362. chartInfo.ChartSource = strings.Join(sourceArr, ",")
  363. baseEdbInfo := edbList[0] //现货指标
  364. chartInfo.UnitEn = baseEdbInfo.UnitEn
  365. // 访问记录-仅普通用户记录
  366. ok, _, _ := user.GetAdminByUserInfo(userInfo)
  367. if !ok {
  368. go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
  369. }
  370. // 用户是否有收藏该图表
  371. {
  372. ob := new(yb_my_chart.YbMyChart)
  373. cond := `user_id = ? AND chart_info_id = ?`
  374. pars := make([]interface{}, 0)
  375. pars = append(pars, userInfo.UserID, chartInfo.ChartInfoId)
  376. exists, e := ob.FetchByCondition(cond, pars)
  377. if e != nil && e != utils.ErrNoRow {
  378. msg = `操作失败`
  379. errMsg = "获取用户图表失败, Err: " + e.Error()
  380. return
  381. }
  382. myChartInfo := new(responseModel.MyChartItem)
  383. if exists != nil && exists.MyChartID > 0 {
  384. myChartInfo.MyChartID = exists.MyChartID
  385. myChartInfo.MyChartClassifyID = exists.MyChartClassifyID
  386. myChartInfo.ChartInfoID = exists.ChartInfoID
  387. myChartInfo.ChartName = exists.ChartName
  388. myChartInfo.UniqueCode = exists.UniqueCode
  389. myChartInfo.ChartImage = exists.ChartImage
  390. myChartInfo.UserID = exists.UserID
  391. myChartInfo.ReportID = exists.ReportID
  392. myChartInfo.ReportChapterID = exists.ReportChapterID
  393. myChartInfo.CreateTime = utils.TimeTransferString(utils.FormatDateTime, exists.CreateTime)
  394. }
  395. resp.MyChartInfo = myChartInfo
  396. }
  397. resp.ChartInfo = chartInfo
  398. resp.EdbInfoList = edbList
  399. resp.DataResp = resultResp
  400. isOk = true
  401. return
  402. }
  403. // RefreshFutureGoodChartInfo 刷新商品价格曲线图表信息
  404. // @Tags 图库模块
  405. // @Summary 刷新图表信息
  406. // @Description 刷新图表信息
  407. // @Security ApiKeyAuth
  408. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  409. // @Accept json
  410. // @Product json
  411. // @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
  412. // @Success 200 {string} string "操作成功"
  413. // @failure 400 {string} string "操作失败"
  414. // @Router /my_chart/future_good/refreshChartInfo [post]
  415. func RefreshFutureGoodChartInfo(c *gin.Context) {
  416. // 参数校验
  417. var req chartInfoModel.RefreshChartInfoReq
  418. if c.ShouldBind(&req) != nil {
  419. response.Fail("参数异常", c)
  420. return
  421. }
  422. chartInfoId := req.ChartInfoId
  423. if chartInfoId == 0 {
  424. response.Fail("参数有误", c)
  425. return
  426. }
  427. //userInfo := user.GetInfoByClaims(c)
  428. //ok, _, err := user.GetAdminByUserInfo(userInfo)
  429. //if err != nil {
  430. // response.FailMsg("刷新失败", "RefreshChartInfo-获取系统用户信息失败"+err.Error(), c)
  431. // return
  432. //}
  433. //if !ok {
  434. // // 普通用户刷新频率限制-每个用户/图/天/2次
  435. // cacheKey := utils.HZ_CHART_LIB_DETAIL + "YB_REFRESH_LIMIT_" + strconv.Itoa(chartInfoId) + "_" + strconv.Itoa(int(userInfo.UserID))
  436. // fmt.Println("refreshCacheKey:", cacheKey)
  437. // countUserRefresh, _ := global.Redis.Get(context.TODO(), cacheKey).Int()
  438. // if countUserRefresh >= 2 {
  439. // response.Ok("目前已是最新数据", c)
  440. // return
  441. // }
  442. // countUserRefresh += 1
  443. // now := time.Now()
  444. // today := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.Local)
  445. // sub := today.Sub(now)
  446. // _ = global.Redis.SetEX(context.TODO(), cacheKey, countUserRefresh, sub)
  447. //}
  448. // 图表信息校验
  449. chartInfo, err := chartInfoModel.GetChartInfoById(chartInfoId)
  450. if err != nil {
  451. if err == utils.ErrNoRow {
  452. response.Fail("图表已被删除,无需刷新", c)
  453. return
  454. }
  455. response.FailMsg("刷新失败", "刷新失败, Err:"+err.Error(), c)
  456. return
  457. }
  458. // 刷新图表
  459. if err = future_goodServ.FutureGoodChartInfoRefresh(chartInfo.ChartInfoId); err != nil {
  460. errContent := fmt.Sprint("ErrMsg: 刷新图表关联指标信息失败, " + err.Error())
  461. if global.CONFIG.Serve.RunMode == "release" {
  462. go alarm_msg.SendAlarmMsg("刷新图表报错"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+errContent, 3)
  463. //go services.SendEmail("弘则研报小程序-release-刷新图表报错", errContent, utils.EmailSendToUsers)
  464. } else {
  465. global.LOG.Info(errContent)
  466. }
  467. }
  468. //清除图表缓存
  469. {
  470. key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
  471. _ = global.Redis.Del(context.TODO(), key)
  472. }
  473. response.OkData("刷新成功", "", c)
  474. }
  475. // FutureGoodChartInfoSave 编辑商品价格图表信息
  476. // @Tags 图库模块
  477. // @Summary 编辑图表信息
  478. // @Description 编辑图表信息
  479. // @Security ApiKeyAuth
  480. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  481. // @Accept json
  482. // @Product json
  483. // @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
  484. // @Success 200 {string} string "操作成功"
  485. // @failure 400 {string} string "操作失败"
  486. // @Router /my_chart/editChartInfo [post]
  487. func FutureGoodChartInfoSave(c *gin.Context) {
  488. // 参数校验
  489. var req chartInfoModel.SaveChartInfoReq
  490. if c.ShouldBind(&req) != nil {
  491. response.Fail("参数异常", c)
  492. return
  493. }
  494. if req.ChartInfoId <= 0 {
  495. response.Fail("参数异常", c)
  496. return
  497. }
  498. // 操作权限校验
  499. userInfo := user.GetInfoByClaims(c)
  500. ok, adminInfo, err := user.GetAdminByUserInfo(userInfo)
  501. if err != nil {
  502. response.Fail("操作人信息有误", c)
  503. return
  504. }
  505. if !ok {
  506. response.Fail("非内部人员无权进行操作", c)
  507. return
  508. }
  509. // 图表信息校验
  510. chartItem, err := chartInfoModel.GetChartInfoById(req.ChartInfoId)
  511. if err != nil {
  512. if err == utils.ErrNoRow {
  513. response.Fail("图表已被删除,请刷新页面!", c)
  514. return
  515. }
  516. response.FailMsg("操作失败", "获取图表信息失败, Err:"+err.Error(), c)
  517. return
  518. }
  519. if !utils.InArrayByInt([]int{utils.CHART_SOURCE_FUTURE_GOOD, utils.CHART_SOURCE_FUTURE_GOOD_PROFIT}, chartItem.Source) {
  520. response.Fail("该图不是商品价格/利润曲线图!", c)
  521. return
  522. }
  523. chartItem.LeftMin = req.LeftMin
  524. chartItem.LeftMax = req.LeftMax
  525. err = chartItem.Update([]string{"LeftMin", "LeftMax"})
  526. if err != nil {
  527. response.FailMsg("保存失败", "保存失败,Err:"+err.Error(), c)
  528. return
  529. }
  530. // 清除图表缓存
  531. cacheKey := utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode
  532. _ = global.Redis.Del(context.TODO(), cacheKey)
  533. // 新增操作日志
  534. {
  535. chartLog := new(chart_info_log.ChartInfoLog)
  536. chartLog.ChartName = chartItem.ChartName
  537. chartLog.ChartInfoId = req.ChartInfoId
  538. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  539. chartLog.SysUserId = int(adminInfo.AdminID)
  540. chartLog.SysUserRealName = adminInfo.RealName
  541. chartLog.UniqueCode = chartItem.UniqueCode
  542. chartLog.CreateTime = time.Now()
  543. bodyBytes, _ := ioutil.ReadAll(c.Request.Body)
  544. chartLog.Content = string(bodyBytes)
  545. chartLog.Status = "修改配置项"
  546. chartLog.Method = c.Request.URL.String()
  547. go chartLog.Create()
  548. }
  549. response.OkData("操作成功", "", c)
  550. }
  551. // getCorrelationChartInfoDetail 获取相关性图表详情
  552. func getCorrelationChartInfoDetail(chartInfo *chartInfoModel.ChartInfoView, myChartClassifyId int, userInfo user.UserInfo) (resp *chart_info.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  553. resp = new(chart_info.ChartInfoDetailResp)
  554. // 获取图表信息
  555. //var err error
  556. chartInfoId := chartInfo.ChartInfoId
  557. chartInfo.ChartSource = "弘则研究"
  558. startDate := chartInfo.StartDate
  559. endDate := chartInfo.EndDate
  560. // 兼容日期错误
  561. {
  562. if strings.Count(startDate, "-") == 1 {
  563. startDate = startDate + "-01"
  564. }
  565. if strings.Count(endDate, "-") == 1 {
  566. endDate = endDate + "-01"
  567. }
  568. }
  569. // 相关性图表信息
  570. correlationChart := new(chart_info_correlation.ChartInfoCorrelation)
  571. if e := correlationChart.GetItemById(chartInfoId); e != nil {
  572. msg = "获取失败"
  573. errMsg = "获取图表相关性信息失败, Err:" + e.Error()
  574. return
  575. }
  576. // 获取指标信息
  577. edbInfoMappingA, e := chartEdbMappingModel.GetChartEdbMappingByEdbInfoId(correlationChart.EdbInfoIdFirst)
  578. if e != nil {
  579. msg = "获取失败"
  580. errMsg = "获取相关性图表, A指标mapping信息失败, Err:" + e.Error()
  581. return
  582. }
  583. edbInfoMappingB, e := chartEdbMappingModel.GetChartEdbMappingByEdbInfoId(correlationChart.EdbInfoIdSecond)
  584. if e != nil {
  585. msg = "获取失败"
  586. errMsg = "获取相关性图表, B指标mapping信息失败, Err:" + e.Error()
  587. return
  588. }
  589. var dataResp interface{} // 绘图数据返回(目前是滚动相关性的图)
  590. var xEdbIdValue []int
  591. var xDateTimeValue []string
  592. var yDataList []chart_info.YData
  593. switch chartInfo.Source {
  594. case utils.CHART_SOURCE_CORRELATION: // 相关性图
  595. moveUnitDays, ok := utils.FrequencyDaysMap[correlationChart.CalculateUnit]
  596. if !ok {
  597. msg = "错误的分析周期"
  598. errMsg = "相关性图表数据有误"
  599. return
  600. }
  601. startDate := time.Now().AddDate(0, 0, -correlationChart.CalculateValue*moveUnitDays).Format(utils.FormatDate)
  602. endDate := time.Now().Format(utils.FormatDate)
  603. xEdbIdValue, yDataList, e = correlation.GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, correlationChart.LeadValue, correlationChart.LeadUnit, startDate, endDate)
  604. if e != nil {
  605. msg = "获取失败"
  606. errMsg = "获取相关性图表, 图表计算值失败, Err:" + e.Error()
  607. return
  608. }
  609. case utils.CHART_SOURCE_ROLLING_CORRELATION: // 滚动相关性图
  610. moveUnitDays, ok := utils.FrequencyDaysMap[correlationChart.BaseCalculateUnit]
  611. if !ok {
  612. msg = "错误的分析周期"
  613. errMsg = "错误的分析周期"
  614. return
  615. }
  616. startDate := time.Now().AddDate(0, 0, -correlationChart.BaseCalculateValue*moveUnitDays).Format(utils.FormatDate)
  617. endDate := time.Now().Format(utils.FormatDate)
  618. xDateTimeValue, yDataList, e = correlation.GetRollingCorrelationChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, correlationChart.LeadValue, correlationChart.LeadUnit, correlationChart.CalculateValue, correlationChart.CalculateUnit, startDate, endDate)
  619. dataResp = chart_info.RollingCorrelationChartDataResp{
  620. XDateTimeValue: xDateTimeValue,
  621. YDataList: yDataList,
  622. }
  623. }
  624. // 完善指标信息
  625. edbList, e := correlation.GetChartEdbInfoFormat(chartInfo.ChartInfoId, edbInfoMappingA, edbInfoMappingB)
  626. if e != nil {
  627. msg = "获取失败"
  628. errMsg = "获取相关性图表, 完善指标信息失败, Err:" + e.Error()
  629. return
  630. }
  631. baseEdbInfo := edbList[0] //现货指标
  632. chartInfo.UnitEn = baseEdbInfo.UnitEn
  633. correlationInfo := new(chart_info.CorrelationInfo)
  634. correlationInfo.LeadValue = correlationChart.LeadValue
  635. correlationInfo.LeadUnit = correlationChart.LeadUnit
  636. correlationInfo.StartDate = correlationChart.StartDate.Format(utils.FormatDate)
  637. correlationInfo.EndDate = correlationChart.EndDate.Format(utils.FormatDate)
  638. correlationInfo.LeadValue = correlationChart.LeadValue
  639. correlationInfo.EdbInfoIdFirst = correlationChart.EdbInfoIdFirst
  640. correlationInfo.EdbInfoIdSecond = correlationChart.EdbInfoIdSecond
  641. // 访问记录-仅普通用户记录
  642. ok, _, _ := user.GetAdminByUserInfo(userInfo)
  643. if !ok {
  644. go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
  645. }
  646. // 用户是否有收藏该图表
  647. {
  648. ob := new(yb_my_chart.YbMyChart)
  649. cond := `user_id = ? AND chart_info_id = ?`
  650. pars := make([]interface{}, 0)
  651. pars = append(pars, userInfo.UserID, chartInfo.ChartInfoId)
  652. exists, e := ob.FetchByCondition(cond, pars)
  653. if e != nil && e != utils.ErrNoRow {
  654. msg = `操作失败`
  655. errMsg = "获取用户图表失败, Err: " + e.Error()
  656. return
  657. }
  658. myChartInfo := new(responseModel.MyChartItem)
  659. if exists != nil && exists.MyChartID > 0 {
  660. myChartInfo.MyChartID = exists.MyChartID
  661. myChartInfo.MyChartClassifyID = exists.MyChartClassifyID
  662. myChartInfo.ChartInfoID = exists.ChartInfoID
  663. myChartInfo.ChartName = exists.ChartName
  664. myChartInfo.UniqueCode = exists.UniqueCode
  665. myChartInfo.ChartImage = exists.ChartImage
  666. myChartInfo.UserID = exists.UserID
  667. myChartInfo.ReportID = exists.ReportID
  668. myChartInfo.ReportChapterID = exists.ReportChapterID
  669. myChartInfo.CreateTime = utils.TimeTransferString(utils.FormatDateTime, exists.CreateTime)
  670. }
  671. resp.MyChartInfo = myChartInfo
  672. }
  673. resp.ChartInfo = chartInfo
  674. resp.EdbInfoList = edbList
  675. resp.XEdbIdValue = xEdbIdValue
  676. resp.YDataList = yDataList
  677. resp.CorrelationChartInfo = correlationInfo
  678. resp.DataResp = dataResp
  679. isOk = true
  680. return
  681. }
  682. // RefreshCorrelationChartInfo 刷新相关性图表信息
  683. // @Tags 图库模块
  684. // @Summary 刷新图表信息
  685. // @Description 刷新图表信息
  686. // @Security ApiKeyAuth
  687. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  688. // @Accept json
  689. // @Product json
  690. // @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
  691. // @Success 200 {string} string "操作成功"
  692. // @failure 400 {string} string "操作失败"
  693. // @Router /my_chart/correlation/refreshChartInfo [post]
  694. func RefreshCorrelationChartInfo(c *gin.Context) {
  695. // 参数校验
  696. var req chartInfoModel.RefreshChartInfoReq
  697. if c.ShouldBind(&req) != nil {
  698. response.Fail("参数异常", c)
  699. return
  700. }
  701. chartInfoId := req.ChartInfoId
  702. if chartInfoId == 0 {
  703. response.Fail("参数有误", c)
  704. return
  705. }
  706. userInfo := user.GetInfoByClaims(c)
  707. ok, _, err := user.GetAdminByUserInfo(userInfo)
  708. if err != nil {
  709. response.FailMsg("刷新失败", "RefreshChartInfo-获取系统用户信息失败"+err.Error(), c)
  710. return
  711. }
  712. if !ok {
  713. // 普通用户刷新频率限制-每个用户/图/天/2次
  714. cacheKey := utils.HZ_CHART_LIB_DETAIL + "YB_REFRESH_LIMIT_" + strconv.Itoa(chartInfoId) + "_" + strconv.Itoa(int(userInfo.UserID))
  715. countUserRefresh, _ := global.Redis.Get(context.TODO(), cacheKey).Int()
  716. if countUserRefresh >= 2 {
  717. response.Ok("目前已是最新数据", c)
  718. return
  719. }
  720. countUserRefresh += 1
  721. now := time.Now()
  722. today := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.Local)
  723. sub := today.Sub(now)
  724. _ = global.Redis.SetEX(context.TODO(), cacheKey, countUserRefresh, sub)
  725. }
  726. // 图表信息校验
  727. chartInfo, err := chartInfoModel.GetChartInfoById(chartInfoId)
  728. if err != nil {
  729. if err == utils.ErrNoRow {
  730. response.Fail("图表已被删除,无需刷新", c)
  731. return
  732. }
  733. response.FailMsg("刷新失败", "刷新失败, Err:"+err.Error(), c)
  734. return
  735. }
  736. // 刷新图表
  737. //_ = correlation.ChartInfoRefresh(chartInfo.ChartInfoId)
  738. if err = chart.ChartInfoRefreshV2(chartInfo.ChartInfoId); err != nil {
  739. errContent := fmt.Sprint("ErrMsg: 刷新图表关联指标信息失败, " + err.Error())
  740. if global.CONFIG.Serve.RunMode == "release" {
  741. go alarm_msg.SendAlarmMsg("刷新图表报错"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+errContent, 3)
  742. //go services.SendEmail("弘则研报小程序-release-刷新图表报错", errContent, utils.EmailSendToUsers)
  743. } else {
  744. global.LOG.Info(errContent)
  745. }
  746. }
  747. //清除图表缓存
  748. {
  749. key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
  750. _ = global.Redis.Del(context.TODO(), key)
  751. }
  752. response.OkData("刷新成功", "", c)
  753. }