chart_common.go 40 KB

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