chart_common.go 44 KB

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