chart_common.go 32 KB

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