chart_common.go 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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. "hongze/hongze_yb/services/chart/cross_variety"
  22. future_goodServ "hongze/hongze_yb/services/chart/future_good"
  23. "hongze/hongze_yb/services/chart/line_equation"
  24. "hongze/hongze_yb/services/chart/line_feature"
  25. "hongze/hongze_yb/services/user"
  26. "hongze/hongze_yb/utils"
  27. "io/ioutil"
  28. "strconv"
  29. "strings"
  30. "time"
  31. )
  32. // CommonChartInfoDetailFromUniqueCode 获取图表详情(通用)
  33. // @Tags 图库模块
  34. // @Summary 获取图表详情
  35. // @Description 获取图表详情
  36. // @Security ApiKeyAuth
  37. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  38. // @Accept json
  39. // @Product json
  40. // @Param DateType query string false "时间段:1-00年至今; 2-10年至今; 3-15年至今; 4-21年至今; 5-指定区间; 6-指定年月至今; 7-18年至今; 8-19年至今; 9-20年至今"
  41. // @Param ClassifyId query string false "图表分类ID"
  42. // @Success 200 {object} chart_info.ChartInfoDetailResp
  43. // @failure 400 {string} string "图表详情获取失败"
  44. // @Router /my_chart/common/detail [get]
  45. func CommonChartInfoDetailFromUniqueCode(c *gin.Context) {
  46. // 图表ID
  47. reqChartInfoId := c.DefaultQuery("ChartInfoId", "")
  48. if reqChartInfoId == "" {
  49. response.Fail("参数有误:图表ID", c)
  50. return
  51. }
  52. chartInfoId, _ := strconv.Atoi(reqChartInfoId)
  53. reqMyChartClassifyId := c.DefaultQuery("MyChartClassifyId", "")
  54. myChartClassifyId, _ := strconv.Atoi(reqMyChartClassifyId)
  55. chartInfo := new(chartInfoModel.ChartInfoView)
  56. chartInfo, err := chartInfoModel.GetChartInfoViewById(chartInfoId)
  57. if err != nil {
  58. if err == utils.ErrNoRow {
  59. response.Custom(4003, "图表不存在,请刷新页面", c)
  60. return
  61. }
  62. response.FailMsg(`获取失败`, "获取图表信息失败, Err:"+err.Error(), c)
  63. return
  64. }
  65. var resp *chart_info.ChartInfoDetailResp
  66. var isOk bool
  67. var msg, errMsg string
  68. switch chartInfo.Source {
  69. case utils.CHART_SOURCE_DEFAULT:
  70. // 获取主题样式
  71. chartTheme, err := chart.GetChartThemeConfig(chartInfo.ChartThemeId, chartInfo.Source, chartInfo.ChartType)
  72. if err != nil {
  73. response.FailMsg("获取失败", "获取主题信息失败, Err:"+err.Error(), c)
  74. return
  75. }
  76. chartInfo.ChartThemeStyle = chartTheme.Config
  77. chartInfo.ChartThemeId = chartTheme.ChartThemeID
  78. case utils.CHART_SOURCE_FUTURE_GOOD, utils.CHART_SOURCE_FUTURE_GOOD_PROFIT, utils.CHART_SOURCE_CORRELATION, utils.CHART_SOURCE_ROLLING_CORRELATION, utils.CHART_SOURCE_LINE_EQUATION, utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE, utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY:
  79. // 获取主题样式
  80. chartTheme, err := chart.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, 1)
  81. if err != nil {
  82. response.FailMsg("获取失败", "获取主题信息失败, Err:"+err.Error(), c)
  83. return
  84. }
  85. chartInfo.ChartThemeStyle = chartTheme.Config
  86. chartInfo.ChartThemeId = chartTheme.ChartThemeID
  87. case utils.CHART_SOURCE_CROSS_HEDGING:
  88. // 获取主题样式
  89. chartTheme, err := chart.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, 5)
  90. if err != nil {
  91. response.FailMsg("获取失败", "获取主题信息失败, Err:"+err.Error(), c)
  92. return
  93. }
  94. chartInfo.ChartThemeStyle = chartTheme.Config
  95. chartInfo.ChartThemeId = chartTheme.ChartThemeID
  96. }
  97. switch chartInfo.Source {
  98. case utils.CHART_SOURCE_DEFAULT:
  99. resp, isOk, msg, errMsg = getChartInfoDetail(chartInfo, myChartClassifyId, user.GetInfoByClaims(c))
  100. case utils.CHART_SOURCE_FUTURE_GOOD:
  101. resp, isOk, msg, errMsg = getFutureGoodChartInfoDetail(chartInfo, myChartClassifyId, user.GetInfoByClaims(c))
  102. case utils.CHART_SOURCE_FUTURE_GOOD_PROFIT:
  103. resp, isOk, msg, errMsg = getFutureGoodProfitChartInfoDetail(chartInfo, myChartClassifyId, user.GetInfoByClaims(c))
  104. case utils.CHART_SOURCE_CORRELATION, utils.CHART_SOURCE_ROLLING_CORRELATION:
  105. resp, isOk, msg, errMsg = getCorrelationChartInfoDetail(chartInfo, myChartClassifyId, user.GetInfoByClaims(c))
  106. case utils.CHART_SOURCE_LINE_EQUATION:
  107. resp, isOk, msg, errMsg = getChartInfoDetailFromUniqueCode(chartInfo, myChartClassifyId, user.GetInfoByClaims(c))
  108. case utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE, utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY:
  109. resp, isOk, msg, errMsg = getLineFeatureChartInfoDetailFromUniqueCode(chartInfo, myChartClassifyId, user.GetInfoByClaims(c))
  110. case utils.CHART_SOURCE_CROSS_HEDGING:
  111. resp, isOk, msg, errMsg = GetCrossVarietyChartInfoDetailFromUniqueCode(chartInfo, myChartClassifyId, user.GetInfoByClaims(c))
  112. default:
  113. msg := "错误的图表"
  114. errMsg := "错误的图表"
  115. response.FailMsg(msg, errMsg, c)
  116. return
  117. }
  118. if !isOk {
  119. response.FailMsg(msg, errMsg, c)
  120. return
  121. }
  122. // 图表的指标来源
  123. sourceNameList, sourceNameEnList := chart.GetEdbSourceByEdbInfoIdList(resp.EdbInfoList)
  124. resp.ChartInfo.ChartSource = strings.Join(sourceNameList, ",")
  125. resp.ChartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  126. response.OkData("获取成功", resp, c)
  127. return
  128. }
  129. // getFutureGoodChartInfoDetail 获取商品价格曲线图表详情
  130. func getFutureGoodChartInfoDetail(chartInfo *chartInfoModel.ChartInfoView, myChartClassifyId int, userInfo user.UserInfo) (resp *chart_info.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  131. resp = new(chart_info.ChartInfoDetailResp)
  132. // 获取图表信息
  133. var err error
  134. chartInfoId := chartInfo.ChartInfoId
  135. startDate := chartInfo.StartDate
  136. endDate := chartInfo.EndDate
  137. // 兼容日期错误
  138. {
  139. if strings.Count(startDate, "-") == 1 {
  140. startDate = startDate + "-01"
  141. }
  142. if strings.Count(endDate, "-") == 1 {
  143. endDate = endDate + "-01"
  144. }
  145. }
  146. //edbInfoMapping, err := chartEdbMappingModel.GetEtaEdbChartEdbMapping(chartInfoId)
  147. edbInfoMappingList, err := chartEdbMappingModel.GetEtaEdbChartEdbMappingList(chartInfoId)
  148. if err != nil {
  149. msg = "获取失败"
  150. errMsg = "获取图表,现货指标信息失败,Err:" + err.Error()
  151. return
  152. }
  153. futureGoodEdbInfoMapping, err := chartEdbMappingModel.GetFutureGoodEdbChartEdbMapping(chartInfoId)
  154. if err != nil {
  155. msg = "获取失败"
  156. errMsg = "获取图表的期货商品指标信息失败,Err:" + err.Error()
  157. return
  158. }
  159. // 商品价格曲线图的一些配置
  160. var barConfig request.FutureGoodBarChartInfoReq
  161. //barChartInfoSort := request.BarChartInfoSortReq{}
  162. if chartInfo.BarConfig == `` {
  163. msg = "商品价格曲线图未配置"
  164. errMsg = "商品价格曲线图未配置"
  165. return
  166. }
  167. err = json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  168. if err != nil {
  169. msg = "商品价格曲线图配置异常"
  170. errMsg = "商品价格曲线图配置异常"
  171. return
  172. }
  173. baseEdbInfoId := barConfig.BaseEdbInfoId
  174. var baseEdbInfoMapping *chartEdbMappingModel.ChartEdbInfoMapping
  175. // todo 兼容历史数据,
  176. if baseEdbInfoId == 0 {
  177. // 默认取第一个现货指标
  178. baseEdbInfoId = edbInfoMappingList[0].EdbInfoId
  179. baseEdbInfoMapping = &edbInfoMappingList[0].ChartEdbInfoMapping
  180. barConfig.BaseEdbInfoId = baseEdbInfoId
  181. } else {
  182. baseEdbInfoMapping, err = chartEdbMappingModel.GetChartEdbMappingByEdbInfoId(baseEdbInfoId)
  183. if err != nil {
  184. msg = "获取失败"
  185. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  186. return
  187. }
  188. }
  189. // 获取图表中的指标数据
  190. barConfigEdbInfoIdList, edbList, xEdbIdValue, xDataList, yDataList, sourceArr, err := future_goodServ.GetChartEdbData(chartInfoId, startDate, endDate, baseEdbInfoMapping, edbInfoMappingList, futureGoodEdbInfoMapping, barConfig, true)
  191. if err != nil {
  192. msg = "获取失败"
  193. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  194. return
  195. }
  196. if len(edbList) <= 0 {
  197. msg = "商品价格曲线图表指标异常"
  198. errMsg = "商品价格曲线图表异常"
  199. return
  200. }
  201. sourceArr = append(sourceArr, "弘则研究")
  202. chartInfo.ChartSource = strings.Join(sourceArr, ",")
  203. for _, v := range edbList {
  204. // 指标别名
  205. if barConfigEdbInfoIdList != nil && len(barConfigEdbInfoIdList) > 0 {
  206. for _, reqEdb := range barConfigEdbInfoIdList {
  207. if v.EdbInfoId == reqEdb.EdbInfoId {
  208. v.EdbAliasName = reqEdb.Name
  209. v.EdbAliasNameEn = reqEdb.NameEn
  210. }
  211. }
  212. }
  213. }
  214. baseEdbInfo := baseEdbInfoMapping //现货指标
  215. chartInfo.UnitEn = baseEdbInfo.UnitEn
  216. // 访问记录-仅普通用户记录
  217. ok, _, _ := user.GetAdminByUserInfo(userInfo)
  218. if !ok {
  219. go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
  220. }
  221. // 用户是否有收藏该图表
  222. {
  223. ob := new(yb_my_chart.YbMyChart)
  224. cond := `user_id = ? AND chart_info_id = ?`
  225. pars := make([]interface{}, 0)
  226. pars = append(pars, userInfo.UserID, chartInfo.ChartInfoId)
  227. exists, e := ob.FetchByCondition(cond, pars)
  228. if e != nil && e != utils.ErrNoRow {
  229. msg = `操作失败`
  230. errMsg = "获取用户图表失败, Err: " + e.Error()
  231. return
  232. }
  233. myChartInfo := new(responseModel.MyChartItem)
  234. if exists != nil && exists.MyChartID > 0 {
  235. myChartInfo.MyChartID = exists.MyChartID
  236. myChartInfo.MyChartClassifyID = exists.MyChartClassifyID
  237. myChartInfo.ChartInfoID = exists.ChartInfoID
  238. myChartInfo.ChartName = exists.ChartName
  239. myChartInfo.UniqueCode = exists.UniqueCode
  240. myChartInfo.ChartImage = exists.ChartImage
  241. myChartInfo.UserID = exists.UserID
  242. myChartInfo.ReportID = exists.ReportID
  243. myChartInfo.ReportChapterID = exists.ReportChapterID
  244. myChartInfo.CreateTime = utils.TimeTransferString(utils.FormatDateTime, exists.CreateTime)
  245. }
  246. resp.MyChartInfo = myChartInfo
  247. }
  248. xDataListFinal := make([]chart_info.XData, 0)
  249. for _, v := range xDataList {
  250. tmp := chart_info.XData{
  251. Name: v.Name,
  252. NameEn: v.NameEn,
  253. IsHide: v.IsHide,
  254. }
  255. xDataListFinal = append(xDataListFinal, tmp)
  256. }
  257. resp.ChartInfo = chartInfo
  258. resp.EdbInfoList = edbList
  259. resp.XEdbIdValue = xEdbIdValue
  260. resp.XDataList = xDataListFinal
  261. resp.YDataList = yDataList
  262. isOk = true
  263. return
  264. }
  265. // getFutureGoodProfitChartInfoDetail 获取商品利润曲线图表详情
  266. func getFutureGoodProfitChartInfoDetail(chartInfo *chartInfoModel.ChartInfoView, myChartClassifyId int, userInfo user.UserInfo) (resp *chart_info.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  267. resp = new(chart_info.ChartInfoDetailResp)
  268. // 获取图表信息
  269. var err error
  270. chartInfoId := chartInfo.ChartInfoId
  271. // 商品利润曲线图的一些配置
  272. var extraConf request.ChartInfoReq
  273. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConf)
  274. if err != nil {
  275. msg = "商品利润曲线图配置异常"
  276. errMsg = "商品利润曲线图配置异常,Err:" + err.Error()
  277. return
  278. }
  279. if len(extraConf.EdbInfoIdList) == 0 {
  280. extraConf.EdbInfoIdList = append(extraConf.EdbInfoIdList, extraConf.BaseEdbInfoId)
  281. }
  282. edbList := make([]*chartEdbMappingModel.ChartEdbInfoMappingList, 0)
  283. edbInfoMappingList, err := chartEdbMappingModel.GetEtaEdbChartEdbMappingList(chartInfoId)
  284. if err != nil {
  285. msg = "获取失败"
  286. errMsg = "获取图表,现货指标信息失败,Err:" + err.Error()
  287. return
  288. }
  289. edbList = edbInfoMappingList
  290. baseEdbInfo := new(chartEdbMappingModel.ChartEdbInfoMapping)
  291. for _, v := range edbInfoMappingList {
  292. if v.EdbInfoId == extraConf.BaseEdbInfoId {
  293. baseEdbInfo = &v.ChartEdbInfoMapping
  294. }
  295. }
  296. futureGoodEdbInfoMappingList, err := chartEdbMappingModel.GetFutureGoodEdbChartEdbMappingList(chartInfoId)
  297. if err != nil {
  298. msg = "获取失败"
  299. errMsg = "获取图表的期货商品指标信息失败,Err:" + err.Error()
  300. return
  301. }
  302. edbList = append(edbList, futureGoodEdbInfoMappingList...)
  303. if len(edbList) <= 0 {
  304. msg = "商品利润曲线图表指标异常"
  305. errMsg = "商品利润曲线图表指标异常"
  306. return
  307. }
  308. xDataList := make([]chart_info.XData, 0)
  309. yDataList := make([]chart_info.YData, 0)
  310. // 查找商品利润图表的扩展信息
  311. chartInfoFutureGoodProfit := new(chart_info_future_good_profit.ChartInfoFutureGoodProfit)
  312. if err = chartInfoFutureGoodProfit.GetItemById(chartInfoId); err != nil {
  313. errMsg = "获取相关性图表失败, Err: " + err.Error()
  314. return
  315. }
  316. err = json.Unmarshal([]byte(chartInfoFutureGoodProfit.XValue), &xDataList)
  317. if err != nil {
  318. msg = "获取失败"
  319. errMsg = "转换X轴数据失败, Err: " + err.Error()
  320. return
  321. }
  322. err = json.Unmarshal([]byte(chartInfoFutureGoodProfit.YValue), &yDataList)
  323. if err != nil {
  324. msg = "获取失败"
  325. errMsg = "转换Y轴数据失败, Err: " + err.Error()
  326. return
  327. }
  328. chartInfo.UnitEn = baseEdbInfo.UnitEn
  329. sourceArr := []string{"同花顺"}
  330. if baseEdbInfo.SourceName != "同花顺" {
  331. sourceArr = append(sourceArr, baseEdbInfo.SourceName)
  332. }
  333. sourceArr = append(sourceArr, "弘则研究")
  334. chartInfo.ChartSource = strings.Join(sourceArr, ",")
  335. warnEdbList := make([]string, 0)
  336. for _, v := range edbList {
  337. if v.IsNullData {
  338. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  339. }
  340. }
  341. // 访问记录-仅普通用户记录
  342. ok, _, _ := user.GetAdminByUserInfo(userInfo)
  343. if !ok {
  344. go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
  345. }
  346. // 用户是否有收藏该图表
  347. {
  348. ob := new(yb_my_chart.YbMyChart)
  349. cond := `user_id = ? AND chart_info_id = ?`
  350. pars := make([]interface{}, 0)
  351. pars = append(pars, userInfo.UserID, chartInfo.ChartInfoId)
  352. exists, e := ob.FetchByCondition(cond, pars)
  353. if e != nil && e != utils.ErrNoRow {
  354. msg = `操作失败`
  355. errMsg = "获取用户图表失败, Err: " + e.Error()
  356. return
  357. }
  358. myChartInfo := new(responseModel.MyChartItem)
  359. if exists != nil && exists.MyChartID > 0 {
  360. myChartInfo.MyChartID = exists.MyChartID
  361. myChartInfo.MyChartClassifyID = exists.MyChartClassifyID
  362. myChartInfo.ChartInfoID = exists.ChartInfoID
  363. myChartInfo.ChartName = exists.ChartName
  364. myChartInfo.UniqueCode = exists.UniqueCode
  365. myChartInfo.ChartImage = exists.ChartImage
  366. myChartInfo.UserID = exists.UserID
  367. myChartInfo.ReportID = exists.ReportID
  368. myChartInfo.ReportChapterID = exists.ReportChapterID
  369. myChartInfo.CreateTime = utils.TimeTransferString(utils.FormatDateTime, exists.CreateTime)
  370. }
  371. resp.MyChartInfo = myChartInfo
  372. }
  373. resp.ChartInfo = chartInfo
  374. resp.EdbInfoList = edbList
  375. resp.DataResp = chart_info.ProfitFutureGoodChartResp{
  376. YDataList: yDataList,
  377. XDataList: xDataList,
  378. ProfitName: chartInfoFutureGoodProfit.ProfitName,
  379. ProfitNameEn: chartInfoFutureGoodProfit.ProfitNameEn,
  380. Extra: extraConf,
  381. }
  382. isOk = true
  383. return
  384. }
  385. // getChartInfoDetailFromUniqueCode 获取拟合方程图表详情
  386. func getChartInfoDetailFromUniqueCode(chartInfo *chartInfoModel.ChartInfoView, myChartClassifyId int, userInfo user.UserInfo) (resp *chart_info.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  387. resp = new(chart_info.ChartInfoDetailResp)
  388. // 获取图表信息
  389. var err error
  390. if chartInfo.ExtraConfig == `` {
  391. msg = "获取失败"
  392. errMsg = "获取配置信息失败"
  393. return
  394. }
  395. var lineChartInfoConfig request.LineChartInfoReq
  396. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &lineChartInfoConfig)
  397. if err != nil {
  398. msg = "获取失败"
  399. errMsg = "获取图表配置信息失败, Err:" + err.Error()
  400. return
  401. }
  402. var getAData, getBData, getR2Data bool
  403. switch lineChartInfoConfig.Source {
  404. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_ONE:
  405. getAData = true
  406. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_TWO:
  407. getBData = true
  408. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_THREE:
  409. getR2Data = true
  410. }
  411. edbList, dataResp, sourceArr, err, errMsg := line_equation.GetChartEdbData(chartInfo.ChartInfoId, lineChartInfoConfig, getAData, getBData, getR2Data)
  412. if err != nil {
  413. if errMsg == `` {
  414. errMsg = "获取失败"
  415. }
  416. return
  417. }
  418. var resultResp interface{}
  419. switch lineChartInfoConfig.Source {
  420. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_ONE:
  421. resultResp = dataResp.AData
  422. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_TWO:
  423. resultResp = dataResp.BData
  424. case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_THREE:
  425. resultResp = dataResp.R2Data
  426. }
  427. sourceArr = append(sourceArr, "弘则研究")
  428. chartInfo.ChartSource = strings.Join(sourceArr, ",")
  429. baseEdbInfo := edbList[0] //现货指标
  430. chartInfo.UnitEn = baseEdbInfo.UnitEn
  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.DataResp = resultResp
  466. isOk = true
  467. return
  468. }
  469. // GetLineFeatureChartInfoDetailFromUniqueCode 根据编码获取统计特征图表详情
  470. func getLineFeatureChartInfoDetailFromUniqueCode(chartInfo *chartInfoModel.ChartInfoView, myChartClassifyId int, userInfo user.UserInfo) (resp *chart_info.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  471. resp = new(chart_info.ChartInfoDetailResp)
  472. // 获取图表信息
  473. var err error
  474. // 获取图表关联指标
  475. edbMappingList, err := chartEdbMappingModel.GetMappingListByChartInfoId(chartInfo.ChartInfoId)
  476. if err != nil {
  477. msg = "获取失败"
  478. errMsg = "获取图表关联指标信息失败,Err:" + err.Error()
  479. return
  480. }
  481. if len(edbMappingList) != 1 {
  482. msg = "获取失败"
  483. errMsg = fmt.Sprint("获取图表关联指标信息异常,数量:", len(edbMappingList))
  484. return
  485. }
  486. edbMapping := edbMappingList[0]
  487. var edbList []*chartEdbMappingModel.ChartEdbInfoMappingList
  488. var resultResp interface{}
  489. sourceArr := make([]string, 0)
  490. switch chartInfo.Source {
  491. case utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION:
  492. calculateValue, tmpErr := strconv.Atoi(chartInfo.ExtraConfig)
  493. if tmpErr != nil {
  494. msg = "获取失败"
  495. errMsg = "格式化配置项失败,Err:" + tmpErr.Error()
  496. return
  497. }
  498. startDate, endDate := utils.GetDateByDateType(chartInfo.DateType, chartInfo.StartDate, chartInfo.EndDate)
  499. edbList, resultResp, sourceArr, err, msg = line_feature.GetStandardDeviationData(0, startDate, endDate, edbMapping, calculateValue)
  500. case utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE:
  501. var percentileConfig request.Percentile
  502. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &percentileConfig)
  503. if err != nil {
  504. msg = "获取失败"
  505. errMsg = "格式化配置项失败,Err:" + err.Error()
  506. return
  507. }
  508. startDate, endDate := utils.GetDateByDateType(chartInfo.DateType, chartInfo.StartDate, chartInfo.EndDate)
  509. edbList, resultResp, sourceArr, err, msg = line_feature.GetPercentileData(0, startDate, endDate, edbMapping, percentileConfig.CalculateValue, percentileConfig.CalculateUnit)
  510. case utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY:
  511. var frequencyDistributionConfig request.FrequencyDistribution
  512. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &frequencyDistributionConfig)
  513. if err != nil {
  514. msg = "获取失败"
  515. errMsg = "格式化配置项失败,Err:" + err.Error()
  516. return
  517. }
  518. // 获取图表中的指标数据
  519. edbList, resultResp, sourceArr, err, errMsg = line_feature.GetFrequencyDistributionData(0, edbMapping, frequencyDistributionConfig.DateType, frequencyDistributionConfig.FrequencyValue, frequencyDistributionConfig.StartDate, frequencyDistributionConfig.EndDate)
  520. default:
  521. msg = `错误的图表`
  522. errMsg = fmt.Sprint("错误的图表来源,source", chartInfo.Source)
  523. return
  524. }
  525. if err != nil {
  526. if msg == `` {
  527. msg = "获取失败"
  528. }
  529. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  530. return
  531. }
  532. sourceArr = append(sourceArr, "弘则研究")
  533. chartInfo.ChartSource = strings.Join(sourceArr, ",")
  534. //baseEdbInfo := edbList[0] //现货指标
  535. //chartInfo.UnitEn = baseEdbInfo.UnitEn
  536. // 访问记录-仅普通用户记录
  537. ok, _, _ := user.GetAdminByUserInfo(userInfo)
  538. if !ok {
  539. go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
  540. }
  541. // 用户是否有收藏该图表
  542. {
  543. ob := new(yb_my_chart.YbMyChart)
  544. cond := `user_id = ? AND chart_info_id = ?`
  545. pars := make([]interface{}, 0)
  546. pars = append(pars, userInfo.UserID, chartInfo.ChartInfoId)
  547. exists, e := ob.FetchByCondition(cond, pars)
  548. if e != nil && e != utils.ErrNoRow {
  549. msg = `操作失败`
  550. errMsg = "获取用户图表失败, Err: " + e.Error()
  551. return
  552. }
  553. myChartInfo := new(responseModel.MyChartItem)
  554. if exists != nil && exists.MyChartID > 0 {
  555. myChartInfo.MyChartID = exists.MyChartID
  556. myChartInfo.MyChartClassifyID = exists.MyChartClassifyID
  557. myChartInfo.ChartInfoID = exists.ChartInfoID
  558. myChartInfo.ChartName = exists.ChartName
  559. myChartInfo.UniqueCode = exists.UniqueCode
  560. myChartInfo.ChartImage = exists.ChartImage
  561. myChartInfo.UserID = exists.UserID
  562. myChartInfo.ReportID = exists.ReportID
  563. myChartInfo.ReportChapterID = exists.ReportChapterID
  564. myChartInfo.CreateTime = utils.TimeTransferString(utils.FormatDateTime, exists.CreateTime)
  565. }
  566. resp.MyChartInfo = myChartInfo
  567. }
  568. resp.ChartInfo = chartInfo
  569. resp.EdbInfoList = edbList
  570. resp.DataResp = resultResp
  571. isOk = true
  572. return
  573. }
  574. // GetCrossVarietyChartInfoDetailFromUniqueCode
  575. // @Description: 获取跨品种分析图表
  576. // @author: Roc
  577. // @datetime 2023-11-29 09:38:23
  578. // @param chartInfo *chartInfoModel.ChartInfoView
  579. // @param myChartClassifyId int
  580. // @param userInfo user.UserInfo
  581. // @return resp *chart_info.ChartInfoDetailResp
  582. // @return isOk bool
  583. // @return msg string
  584. // @return errMsg string
  585. func GetCrossVarietyChartInfoDetailFromUniqueCode(chartInfo *chartInfoModel.ChartInfoView, myChartClassifyId int, userInfo user.UserInfo) (resp *chart_info.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  586. resp = new(chart_info.ChartInfoDetailResp)
  587. // 获取图表信息
  588. var err error
  589. if chartInfo.ExtraConfig == `` {
  590. msg = "图表配置信息异常"
  591. errMsg = "图表配置信息异常"
  592. return
  593. }
  594. var config request.ChartConfigReq
  595. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &config)
  596. if err != nil {
  597. msg = "解析跨品种分析配置失败"
  598. errMsg = "解析跨品种分析配置失败,Err:" + err.Error()
  599. return
  600. }
  601. var edbList []*chartEdbMappingModel.ChartEdbInfoMappingList
  602. var resultResp interface{}
  603. sourceArr := make([]string, 0)
  604. // 获取图表x轴y轴
  605. edbList, resultResp, sourceArr, err, msg, _ = cross_variety.GetChartData(0, config)
  606. if err != nil {
  607. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  608. return
  609. }
  610. if err != nil {
  611. if msg == `` {
  612. msg = "获取失败"
  613. }
  614. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  615. return
  616. }
  617. sourceArr = append(sourceArr, "弘则研究")
  618. chartInfo.ChartSource = strings.Join(sourceArr, ",")
  619. //baseEdbInfo := edbList[0] //现货指标
  620. //chartInfo.UnitEn = baseEdbInfo.UnitEn
  621. // 访问记录-仅普通用户记录
  622. ok, _, _ := user.GetAdminByUserInfo(userInfo)
  623. if !ok {
  624. go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
  625. }
  626. // 用户是否有收藏该图表
  627. {
  628. ob := new(yb_my_chart.YbMyChart)
  629. cond := `user_id = ? AND chart_info_id = ?`
  630. pars := make([]interface{}, 0)
  631. pars = append(pars, userInfo.UserID, chartInfo.ChartInfoId)
  632. exists, e := ob.FetchByCondition(cond, pars)
  633. if e != nil && e != utils.ErrNoRow {
  634. msg = `操作失败`
  635. errMsg = "获取用户图表失败, Err: " + e.Error()
  636. return
  637. }
  638. myChartInfo := new(responseModel.MyChartItem)
  639. if exists != nil && exists.MyChartID > 0 {
  640. myChartInfo.MyChartID = exists.MyChartID
  641. myChartInfo.MyChartClassifyID = exists.MyChartClassifyID
  642. myChartInfo.ChartInfoID = exists.ChartInfoID
  643. myChartInfo.ChartName = exists.ChartName
  644. myChartInfo.UniqueCode = exists.UniqueCode
  645. myChartInfo.ChartImage = exists.ChartImage
  646. myChartInfo.UserID = exists.UserID
  647. myChartInfo.ReportID = exists.ReportID
  648. myChartInfo.ReportChapterID = exists.ReportChapterID
  649. myChartInfo.CreateTime = utils.TimeTransferString(utils.FormatDateTime, exists.CreateTime)
  650. }
  651. resp.MyChartInfo = myChartInfo
  652. }
  653. resp.ChartInfo = chartInfo
  654. resp.EdbInfoList = edbList
  655. resp.DataResp = resultResp
  656. isOk = true
  657. return
  658. }
  659. // RefreshFutureGoodChartInfo 刷新商品价格曲线图表信息
  660. // @Tags 图库模块
  661. // @Summary 刷新图表信息
  662. // @Description 刷新图表信息
  663. // @Security ApiKeyAuth
  664. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  665. // @Accept json
  666. // @Product json
  667. // @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
  668. // @Success 200 {string} string "操作成功"
  669. // @failure 400 {string} string "操作失败"
  670. // @Router /my_chart/future_good/refreshChartInfo [post]
  671. func RefreshFutureGoodChartInfo(c *gin.Context) {
  672. // 参数校验
  673. var req chartInfoModel.RefreshChartInfoReq
  674. if c.ShouldBind(&req) != nil {
  675. response.Fail("参数异常", c)
  676. return
  677. }
  678. chartInfoId := req.ChartInfoId
  679. if chartInfoId == 0 {
  680. response.Fail("参数有误", c)
  681. return
  682. }
  683. //userInfo := user.GetInfoByClaims(c)
  684. //ok, _, err := user.GetAdminByUserInfo(userInfo)
  685. //if err != nil {
  686. // response.FailMsg("刷新失败", "RefreshChartInfo-获取系统用户信息失败"+err.Error(), c)
  687. // return
  688. //}
  689. //if !ok {
  690. // // 普通用户刷新频率限制-每个用户/图/天/2次
  691. // cacheKey := utils.HZ_CHART_LIB_DETAIL + "YB_REFRESH_LIMIT_" + strconv.Itoa(chartInfoId) + "_" + strconv.Itoa(int(userInfo.UserID))
  692. // fmt.Println("refreshCacheKey:", cacheKey)
  693. // countUserRefresh, _ := global.Redis.Get(context.TODO(), cacheKey).Int()
  694. // if countUserRefresh >= 2 {
  695. // response.Ok("目前已是最新数据", c)
  696. // return
  697. // }
  698. // countUserRefresh += 1
  699. // now := time.Now()
  700. // today := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.Local)
  701. // sub := today.Sub(now)
  702. // _ = global.Redis.SetEX(context.TODO(), cacheKey, countUserRefresh, sub)
  703. //}
  704. // 图表信息校验
  705. chartInfo, err := chartInfoModel.GetChartInfoById(chartInfoId)
  706. if err != nil {
  707. if err == utils.ErrNoRow {
  708. response.Fail("图表已被删除,无需刷新", c)
  709. return
  710. }
  711. response.FailMsg("刷新失败", "刷新失败, Err:"+err.Error(), c)
  712. return
  713. }
  714. // 刷新图表
  715. if err = future_goodServ.FutureGoodChartInfoRefresh(chartInfo.ChartInfoId); err != nil {
  716. errContent := fmt.Sprint("ErrMsg: 刷新图表关联指标信息失败, " + err.Error())
  717. if global.CONFIG.Serve.RunMode == "release" {
  718. go alarm_msg.SendAlarmMsg("刷新图表报错"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+errContent, 3)
  719. //go services.SendEmail("弘则研报小程序-release-刷新图表报错", errContent, utils.EmailSendToUsers)
  720. } else {
  721. global.LOG.Info(errContent)
  722. }
  723. }
  724. //清除图表缓存
  725. {
  726. key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
  727. _ = global.Redis.Del(context.TODO(), key)
  728. }
  729. response.OkData("刷新成功", "", c)
  730. }
  731. // FutureGoodChartInfoSave 编辑商品价格图表信息
  732. // @Tags 图库模块
  733. // @Summary 编辑图表信息
  734. // @Description 编辑图表信息
  735. // @Security ApiKeyAuth
  736. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  737. // @Accept json
  738. // @Product json
  739. // @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
  740. // @Success 200 {string} string "操作成功"
  741. // @failure 400 {string} string "操作失败"
  742. // @Router /my_chart/editChartInfo [post]
  743. func FutureGoodChartInfoSave(c *gin.Context) {
  744. // 参数校验
  745. var req chartInfoModel.SaveChartInfoReq
  746. if c.ShouldBind(&req) != nil {
  747. response.Fail("参数异常", c)
  748. return
  749. }
  750. if req.ChartInfoId <= 0 {
  751. response.Fail("参数异常", c)
  752. return
  753. }
  754. // 操作权限校验
  755. userInfo := user.GetInfoByClaims(c)
  756. ok, adminInfo, err := user.GetAdminByUserInfo(userInfo)
  757. if err != nil {
  758. response.Fail("操作人信息有误", c)
  759. return
  760. }
  761. if !ok {
  762. response.Fail("非内部人员无权进行操作", c)
  763. return
  764. }
  765. // 图表信息校验
  766. chartItem, err := chartInfoModel.GetChartInfoById(req.ChartInfoId)
  767. if err != nil {
  768. if err == utils.ErrNoRow {
  769. response.Fail("图表已被删除,请刷新页面!", c)
  770. return
  771. }
  772. response.FailMsg("操作失败", "获取图表信息失败, Err:"+err.Error(), c)
  773. return
  774. }
  775. if !utils.InArrayByInt([]int{utils.CHART_SOURCE_FUTURE_GOOD, utils.CHART_SOURCE_FUTURE_GOOD_PROFIT}, chartItem.Source) {
  776. response.Fail("该图不是商品价格/利润曲线图!", c)
  777. return
  778. }
  779. chartItem.LeftMin = req.LeftMin
  780. chartItem.LeftMax = req.LeftMax
  781. err = chartItem.Update([]string{"LeftMin", "LeftMax"})
  782. if err != nil {
  783. response.FailMsg("保存失败", "保存失败,Err:"+err.Error(), c)
  784. return
  785. }
  786. // 清除图表缓存
  787. cacheKey := utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode
  788. _ = global.Redis.Del(context.TODO(), cacheKey)
  789. // 新增操作日志
  790. {
  791. chartLog := new(chart_info_log.ChartInfoLog)
  792. chartLog.ChartName = chartItem.ChartName
  793. chartLog.ChartInfoId = req.ChartInfoId
  794. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  795. chartLog.SysUserId = int(adminInfo.AdminID)
  796. chartLog.SysUserRealName = adminInfo.RealName
  797. chartLog.UniqueCode = chartItem.UniqueCode
  798. chartLog.CreateTime = time.Now()
  799. bodyBytes, _ := ioutil.ReadAll(c.Request.Body)
  800. chartLog.Content = string(bodyBytes)
  801. chartLog.Status = "修改配置项"
  802. chartLog.Method = c.Request.URL.String()
  803. go chartLog.Create()
  804. }
  805. response.OkData("操作成功", "", c)
  806. }
  807. // getCorrelationChartInfoDetail 获取相关性图表详情
  808. func getCorrelationChartInfoDetail(chartInfo *chartInfoModel.ChartInfoView, myChartClassifyId int, userInfo user.UserInfo) (resp *chart_info.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
  809. resp = new(chart_info.ChartInfoDetailResp)
  810. // 获取图表信息
  811. //var err error
  812. chartInfoId := chartInfo.ChartInfoId
  813. chartInfo.ChartSource = "弘则研究"
  814. startDate := chartInfo.StartDate
  815. endDate := chartInfo.EndDate
  816. // 兼容日期错误
  817. {
  818. if strings.Count(startDate, "-") == 1 {
  819. startDate = startDate + "-01"
  820. }
  821. if strings.Count(endDate, "-") == 1 {
  822. endDate = endDate + "-01"
  823. }
  824. }
  825. // 相关性图表信息
  826. correlationChart := new(chart_info_correlation.ChartInfoCorrelation)
  827. if e := correlationChart.GetItemById(chartInfoId); e != nil {
  828. msg = "获取失败"
  829. errMsg = "获取图表相关性信息失败, Err:" + e.Error()
  830. return
  831. }
  832. // 获取指标信息
  833. edbInfoMappingA, e := chartEdbMappingModel.GetChartEdbMappingByEdbInfoId(correlationChart.EdbInfoIdFirst)
  834. if e != nil {
  835. msg = "获取失败"
  836. errMsg = "获取相关性图表, A指标mapping信息失败, Err:" + e.Error()
  837. return
  838. }
  839. edbInfoMappingB := new(chartEdbMappingModel.ChartEdbInfoMapping)
  840. if correlationChart.AnalysisMode != 1 {
  841. edbInfoMappingB, e = chartEdbMappingModel.GetChartEdbMappingByEdbInfoId(correlationChart.EdbInfoIdSecond)
  842. if e != nil {
  843. msg = "获取失败"
  844. errMsg = "获取相关性图表, B指标mapping信息失败, Err:" + e.Error()
  845. return
  846. }
  847. }
  848. var dataResp interface{} // 绘图数据返回(目前是滚动相关性的图)
  849. var xEdbIdValue []int
  850. var yDataList []chart_info.YData
  851. if correlationChart.AnalysisMode != 1 {
  852. switch chartInfo.Source {
  853. case utils.CHART_SOURCE_CORRELATION: // 相关性图
  854. moveUnitDays, ok := utils.FrequencyDaysMap[correlationChart.CalculateUnit]
  855. if !ok {
  856. msg = "错误的分析周期"
  857. errMsg = "相关性图表数据有误"
  858. return
  859. }
  860. st := time.Now().AddDate(0, 0, -correlationChart.CalculateValue*moveUnitDays).Format(utils.FormatDate)
  861. ed := time.Now().Format(utils.FormatDate)
  862. xEdbIdValue, yDataList, e = correlation.GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, correlationChart.LeadValue, correlationChart.LeadUnit, st, ed)
  863. if e != nil {
  864. msg = "获取失败"
  865. errMsg = "获取相关性图表, 图表计算值失败, Err:" + e.Error()
  866. return
  867. }
  868. case utils.CHART_SOURCE_ROLLING_CORRELATION: // 滚动相关性图
  869. st, ed := utils.GetDateByDateType(correlationChart.DateType, correlationChart.StartDate.Format(utils.FormatDate), correlationChart.EndDate.Format(utils.FormatDate))
  870. dataResp, e = correlation.GetRollingCorrelationChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, correlationChart.LeadValue, correlationChart.LeadUnit, correlationChart.CalculateValue, correlationChart.CalculateUnit, st, ed, chartInfo.ChartName, chartInfo.ChartNameEn)
  871. }
  872. } else {
  873. xEdbIdValue, yDataList, e = correlation.GetFactorChartDataByChartId(chartInfoId, chartInfo.ExtraConfig)
  874. if e != nil {
  875. msg = "获取失败"
  876. errMsg = "获取相关性图表, 图表计算值失败, Err:" + e.Error()
  877. return
  878. }
  879. }
  880. // 完善指标信息
  881. edbList, e := correlation.GetChartEdbInfoFormat(chartInfo.ChartInfoId, edbInfoMappingA, edbInfoMappingB)
  882. if e != nil {
  883. msg = "获取失败"
  884. errMsg = "获取相关性图表, 完善指标信息失败, Err:" + e.Error()
  885. return
  886. }
  887. baseEdbInfo := edbList[0] //现货指标
  888. chartInfo.UnitEn = baseEdbInfo.UnitEn
  889. correlationInfo := new(chart_info.CorrelationInfo)
  890. correlationInfo.LeadValue = correlationChart.LeadValue
  891. correlationInfo.LeadUnit = correlationChart.LeadUnit
  892. correlationInfo.StartDate = correlationChart.StartDate.Format(utils.FormatDate)
  893. correlationInfo.EndDate = correlationChart.EndDate.Format(utils.FormatDate)
  894. correlationInfo.LeadValue = correlationChart.LeadValue
  895. correlationInfo.EdbInfoIdFirst = correlationChart.EdbInfoIdFirst
  896. correlationInfo.EdbInfoIdSecond = correlationChart.EdbInfoIdSecond
  897. correlationInfo.AnalysisMode = correlationChart.AnalysisMode
  898. // 访问记录-仅普通用户记录
  899. ok, _, _ := user.GetAdminByUserInfo(userInfo)
  900. if !ok {
  901. go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
  902. }
  903. // 用户是否有收藏该图表
  904. {
  905. ob := new(yb_my_chart.YbMyChart)
  906. cond := `user_id = ? AND chart_info_id = ?`
  907. pars := make([]interface{}, 0)
  908. pars = append(pars, userInfo.UserID, chartInfo.ChartInfoId)
  909. exists, e := ob.FetchByCondition(cond, pars)
  910. if e != nil && e != utils.ErrNoRow {
  911. msg = `操作失败`
  912. errMsg = "获取用户图表失败, Err: " + e.Error()
  913. return
  914. }
  915. myChartInfo := new(responseModel.MyChartItem)
  916. if exists != nil && exists.MyChartID > 0 {
  917. myChartInfo.MyChartID = exists.MyChartID
  918. myChartInfo.MyChartClassifyID = exists.MyChartClassifyID
  919. myChartInfo.ChartInfoID = exists.ChartInfoID
  920. myChartInfo.ChartName = exists.ChartName
  921. myChartInfo.UniqueCode = exists.UniqueCode
  922. myChartInfo.ChartImage = exists.ChartImage
  923. myChartInfo.UserID = exists.UserID
  924. myChartInfo.ReportID = exists.ReportID
  925. myChartInfo.ReportChapterID = exists.ReportChapterID
  926. myChartInfo.CreateTime = utils.TimeTransferString(utils.FormatDateTime, exists.CreateTime)
  927. }
  928. resp.MyChartInfo = myChartInfo
  929. }
  930. resp.ChartInfo = chartInfo
  931. resp.EdbInfoList = edbList
  932. resp.XEdbIdValue = xEdbIdValue
  933. resp.YDataList = yDataList
  934. resp.CorrelationChartInfo = correlationInfo
  935. resp.DataResp = dataResp
  936. isOk = true
  937. return
  938. }
  939. // RefreshCrossVarietyChartInfo 刷新跨品种分析图表信息
  940. // @Tags 图库模块
  941. // @Summary 刷新跨品种分析图表信息
  942. // @Description 刷新跨品种分析图表信息
  943. // @Security ApiKeyAuth
  944. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  945. // @Accept json
  946. // @Product json
  947. // @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
  948. // @Success 200 {string} string "操作成功"
  949. // @failure 400 {string} string "操作失败"
  950. // @Router /my_chart/cross_variety/refreshChartInfo [post]
  951. func RefreshCrossVarietyChartInfo(c *gin.Context) {
  952. // 参数校验
  953. var req chartInfoModel.RefreshChartInfoReq
  954. if c.ShouldBind(&req) != nil {
  955. response.Fail("参数异常", c)
  956. return
  957. }
  958. chartInfoId := req.ChartInfoId
  959. if chartInfoId == 0 {
  960. response.Fail("参数有误", c)
  961. return
  962. }
  963. userInfo := user.GetInfoByClaims(c)
  964. ok, _, err := user.GetAdminByUserInfo(userInfo)
  965. if err != nil {
  966. response.FailMsg("刷新失败", "RefreshChartInfo-获取系统用户信息失败"+err.Error(), c)
  967. return
  968. }
  969. if !ok {
  970. // 普通用户刷新频率限制-每个用户/图/天/2次
  971. cacheKey := utils.HZ_CHART_LIB_DETAIL + "YB_REFRESH_LIMIT_" + strconv.Itoa(chartInfoId) + "_" + strconv.Itoa(int(userInfo.UserID))
  972. countUserRefresh, _ := global.Redis.Get(context.TODO(), cacheKey).Int()
  973. if countUserRefresh >= 2 {
  974. response.Ok("目前已是最新数据", c)
  975. return
  976. }
  977. countUserRefresh += 1
  978. now := time.Now()
  979. today := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.Local)
  980. sub := today.Sub(now)
  981. _ = global.Redis.SetEX(context.TODO(), cacheKey, countUserRefresh, sub)
  982. }
  983. // 图表信息校验
  984. chartInfo, err := chartInfoModel.GetChartInfoById(chartInfoId)
  985. if err != nil {
  986. if err == utils.ErrNoRow {
  987. response.Fail("图表已被删除,无需刷新", c)
  988. return
  989. }
  990. response.FailMsg("刷新失败", "刷新失败, Err:"+err.Error(), c)
  991. return
  992. }
  993. var config request.ChartConfigReq
  994. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &config)
  995. if err != nil {
  996. response.FailMsg("解析跨品种分析配置失败", "解析跨品种分析配置失败, Err:"+err.Error(), c)
  997. return
  998. }
  999. // 获取关联的指标信息
  1000. _, _, edbInfoIdList, err := cross_variety.GetXYEdbIdList(config.TagX, config.TagY, config.VarietyList)
  1001. if err != nil {
  1002. response.FailMsg("获取指标信息失败", "刷新失败,获取指标信息失败, Err:"+err.Error(), c)
  1003. return
  1004. }
  1005. // 刷新图表
  1006. //_ = correlation.ChartInfoRefresh(chartInfo.ChartInfoId)
  1007. err, errMsg := chart.EdbInfoRefreshAllFromBase(edbInfoIdList, false)
  1008. if err != nil {
  1009. errContent := fmt.Sprint("刷新图表关联指标信息失败 errMsg:", errMsg, "err: "+err.Error())
  1010. if global.CONFIG.Serve.RunMode == "release" {
  1011. go alarm_msg.SendAlarmMsg("刷新图表报错"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+errContent, 3)
  1012. //go services.SendEmail("弘则研报小程序-release-刷新图表报错", errContent, utils.EmailSendToUsers)
  1013. } else {
  1014. global.LOG.Info(errContent)
  1015. }
  1016. response.FailMsg("刷新失败", "刷新失败,获取指标信息失败, Err:"+err.Error(), c)
  1017. return
  1018. }
  1019. //清除图表缓存
  1020. {
  1021. key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
  1022. _ = global.Redis.Del(context.TODO(), key)
  1023. }
  1024. response.OkData("刷新成功", "", c)
  1025. }
  1026. // RefreshCorrelationChartInfo 刷新相关性图表信息
  1027. // @Tags 图库模块
  1028. // @Summary 刷新图表信息
  1029. // @Description 刷新图表信息
  1030. // @Security ApiKeyAuth
  1031. // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
  1032. // @Accept json
  1033. // @Product json
  1034. // @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
  1035. // @Success 200 {string} string "操作成功"
  1036. // @failure 400 {string} string "操作失败"
  1037. // @Router /my_chart/correlation/refreshChartInfo [post]
  1038. func RefreshCorrelationChartInfo(c *gin.Context) {
  1039. // 参数校验
  1040. var req chartInfoModel.RefreshChartInfoReq
  1041. if c.ShouldBind(&req) != nil {
  1042. response.Fail("参数异常", c)
  1043. return
  1044. }
  1045. chartInfoId := req.ChartInfoId
  1046. if chartInfoId == 0 {
  1047. response.Fail("参数有误", c)
  1048. return
  1049. }
  1050. userInfo := user.GetInfoByClaims(c)
  1051. ok, _, err := user.GetAdminByUserInfo(userInfo)
  1052. if err != nil {
  1053. response.FailMsg("刷新失败", "RefreshChartInfo-获取系统用户信息失败"+err.Error(), c)
  1054. return
  1055. }
  1056. if !ok {
  1057. // 普通用户刷新频率限制-每个用户/图/天/2次
  1058. cacheKey := utils.HZ_CHART_LIB_DETAIL + "YB_REFRESH_LIMIT_" + strconv.Itoa(chartInfoId) + "_" + strconv.Itoa(int(userInfo.UserID))
  1059. countUserRefresh, _ := global.Redis.Get(context.TODO(), cacheKey).Int()
  1060. if countUserRefresh >= 2 {
  1061. response.Ok("目前已是最新数据", c)
  1062. return
  1063. }
  1064. countUserRefresh += 1
  1065. now := time.Now()
  1066. today := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.Local)
  1067. sub := today.Sub(now)
  1068. _ = global.Redis.SetEX(context.TODO(), cacheKey, countUserRefresh, sub)
  1069. }
  1070. // 图表信息校验
  1071. chartInfo, err := chartInfoModel.GetChartInfoById(chartInfoId)
  1072. if err != nil {
  1073. if err == utils.ErrNoRow {
  1074. response.Fail("图表已被删除,无需刷新", c)
  1075. return
  1076. }
  1077. response.FailMsg("刷新失败", "刷新失败, Err:"+err.Error(), c)
  1078. return
  1079. }
  1080. // 刷新图表
  1081. //_ = correlation.ChartInfoRefresh(chartInfo.ChartInfoId)
  1082. if err = chart.ChartInfoRefreshV2(chartInfo.ChartInfoId); err != nil {
  1083. errContent := fmt.Sprint("ErrMsg: 刷新图表关联指标信息失败, " + err.Error())
  1084. if global.CONFIG.Serve.RunMode == "release" {
  1085. go alarm_msg.SendAlarmMsg("刷新图表报错"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+errContent, 3)
  1086. //go services.SendEmail("弘则研报小程序-release-刷新图表报错", errContent, utils.EmailSendToUsers)
  1087. } else {
  1088. global.LOG.Info(errContent)
  1089. }
  1090. }
  1091. //清除图表缓存
  1092. {
  1093. key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
  1094. _ = global.Redis.Del(context.TODO(), key)
  1095. }
  1096. response.OkData("刷新成功", "", c)
  1097. }