chart_common.go 44 KB

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