123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212 |
- package chart
- import (
- "context"
- "encoding/json"
- "fmt"
- "github.com/gin-gonic/gin"
- "hongze/hongze_yb/controller/response"
- "hongze/hongze_yb/global"
- "hongze/hongze_yb/models/request"
- responseModel "hongze/hongze_yb/models/response"
- "hongze/hongze_yb/models/response/chart_info"
- chartEdbMappingModel "hongze/hongze_yb/models/tables/chart_edb_mapping"
- chartInfoModel "hongze/hongze_yb/models/tables/chart_info"
- "hongze/hongze_yb/models/tables/chart_info_correlation"
- "hongze/hongze_yb/models/tables/chart_info_future_good_profit"
- "hongze/hongze_yb/models/tables/chart_info_log"
- "hongze/hongze_yb/models/tables/yb_my_chart"
- "hongze/hongze_yb/services/alarm_msg"
- "hongze/hongze_yb/services/chart"
- "hongze/hongze_yb/services/chart/correlation"
- "hongze/hongze_yb/services/chart/cross_variety"
- future_goodServ "hongze/hongze_yb/services/chart/future_good"
- "hongze/hongze_yb/services/chart/line_equation"
- "hongze/hongze_yb/services/chart/line_feature"
- "hongze/hongze_yb/services/user"
- "hongze/hongze_yb/utils"
- "io/ioutil"
- "strconv"
- "strings"
- "time"
- )
- // CommonChartInfoDetailFromUniqueCode 获取图表详情(通用)
- // @Tags 图库模块
- // @Summary 获取图表详情
- // @Description 获取图表详情
- // @Security ApiKeyAuth
- // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
- // @Accept json
- // @Product json
- // @Param DateType query string false "时间段:1-00年至今; 2-10年至今; 3-15年至今; 4-21年至今; 5-指定区间; 6-指定年月至今; 7-18年至今; 8-19年至今; 9-20年至今"
- // @Param ClassifyId query string false "图表分类ID"
- // @Success 200 {object} chart_info.ChartInfoDetailResp
- // @failure 400 {string} string "图表详情获取失败"
- // @Router /my_chart/common/detail [get]
- func CommonChartInfoDetailFromUniqueCode(c *gin.Context) {
- // 图表ID
- reqChartInfoId := c.DefaultQuery("ChartInfoId", "")
- if reqChartInfoId == "" {
- response.Fail("参数有误:图表ID", c)
- return
- }
- chartInfoId, _ := strconv.Atoi(reqChartInfoId)
- reqMyChartClassifyId := c.DefaultQuery("MyChartClassifyId", "")
- myChartClassifyId, _ := strconv.Atoi(reqMyChartClassifyId)
- chartInfo := new(chartInfoModel.ChartInfoView)
- chartInfo, err := chartInfoModel.GetChartInfoViewById(chartInfoId)
- if err != nil {
- if err == utils.ErrNoRow {
- response.Custom(4003, "图表不存在,请刷新页面", c)
- return
- }
- response.FailMsg(`获取失败`, "获取图表信息失败, Err:"+err.Error(), c)
- return
- }
- var resp *chart_info.ChartInfoDetailResp
- var isOk bool
- var msg, errMsg string
- switch chartInfo.Source {
- case utils.CHART_SOURCE_DEFAULT:
- // 获取主题样式
- chartTheme, err := chart.GetChartThemeConfig(chartInfo.ChartThemeId, chartInfo.Source, chartInfo.ChartType)
- if err != nil {
- response.FailMsg("获取失败", "获取主题信息失败, Err:"+err.Error(), c)
- return
- }
- chartInfo.ChartThemeStyle = chartTheme.Config
- chartInfo.ChartThemeId = chartTheme.ChartThemeID
- 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:
- // 获取主题样式
- chartTheme, err := chart.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, 1)
- if err != nil {
- response.FailMsg("获取失败", "获取主题信息失败, Err:"+err.Error(), c)
- return
- }
- chartInfo.ChartThemeStyle = chartTheme.Config
- chartInfo.ChartThemeId = chartTheme.ChartThemeID
- case utils.CHART_SOURCE_CROSS_HEDGING:
- // 获取主题样式
- chartTheme, err := chart.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, 5)
- if err != nil {
- response.FailMsg("获取失败", "获取主题信息失败, Err:"+err.Error(), c)
- return
- }
- chartInfo.ChartThemeStyle = chartTheme.Config
- chartInfo.ChartThemeId = chartTheme.ChartThemeID
- }
- switch chartInfo.Source {
- case utils.CHART_SOURCE_DEFAULT:
- resp, isOk, msg, errMsg = getChartInfoDetail(chartInfo, myChartClassifyId, user.GetInfoByClaims(c))
- case utils.CHART_SOURCE_FUTURE_GOOD:
- resp, isOk, msg, errMsg = getFutureGoodChartInfoDetail(chartInfo, myChartClassifyId, user.GetInfoByClaims(c))
- case utils.CHART_SOURCE_FUTURE_GOOD_PROFIT:
- resp, isOk, msg, errMsg = getFutureGoodProfitChartInfoDetail(chartInfo, myChartClassifyId, user.GetInfoByClaims(c))
- case utils.CHART_SOURCE_CORRELATION, utils.CHART_SOURCE_ROLLING_CORRELATION:
- resp, isOk, msg, errMsg = getCorrelationChartInfoDetail(chartInfo, myChartClassifyId, user.GetInfoByClaims(c))
- case utils.CHART_SOURCE_LINE_EQUATION:
- resp, isOk, msg, errMsg = getChartInfoDetailFromUniqueCode(chartInfo, myChartClassifyId, user.GetInfoByClaims(c))
- case utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE, utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY:
- resp, isOk, msg, errMsg = getLineFeatureChartInfoDetailFromUniqueCode(chartInfo, myChartClassifyId, user.GetInfoByClaims(c))
- case utils.CHART_SOURCE_CROSS_HEDGING:
- resp, isOk, msg, errMsg = GetCrossVarietyChartInfoDetailFromUniqueCode(chartInfo, myChartClassifyId, user.GetInfoByClaims(c))
- default:
- msg := "错误的图表"
- errMsg := "错误的图表"
- response.FailMsg(msg, errMsg, c)
- return
- }
- if !isOk {
- response.FailMsg(msg, errMsg, c)
- return
- }
- // 图表的指标来源
- sourceNameList, sourceNameEnList := chart.GetEdbSourceByEdbInfoIdList(resp.EdbInfoList)
- resp.ChartInfo.ChartSource = strings.Join(sourceNameList, ",")
- resp.ChartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
- response.OkData("获取成功", resp, c)
- return
- }
- // getFutureGoodChartInfoDetail 获取商品价格曲线图表详情
- func getFutureGoodChartInfoDetail(chartInfo *chartInfoModel.ChartInfoView, myChartClassifyId int, userInfo user.UserInfo) (resp *chart_info.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
- resp = new(chart_info.ChartInfoDetailResp)
- // 获取图表信息
- var err error
- chartInfoId := chartInfo.ChartInfoId
- startDate := chartInfo.StartDate
- endDate := chartInfo.EndDate
- // 兼容日期错误
- {
- if strings.Count(startDate, "-") == 1 {
- startDate = startDate + "-01"
- }
- if strings.Count(endDate, "-") == 1 {
- endDate = endDate + "-01"
- }
- }
- //edbInfoMapping, err := chartEdbMappingModel.GetEtaEdbChartEdbMapping(chartInfoId)
- edbInfoMappingList, err := chartEdbMappingModel.GetEtaEdbChartEdbMappingList(chartInfoId)
- if err != nil {
- msg = "获取失败"
- errMsg = "获取图表,现货指标信息失败,Err:" + err.Error()
- return
- }
- futureGoodEdbInfoMapping, err := chartEdbMappingModel.GetFutureGoodEdbChartEdbMapping(chartInfoId)
- if err != nil {
- msg = "获取失败"
- errMsg = "获取图表的期货商品指标信息失败,Err:" + err.Error()
- return
- }
- // 商品价格曲线图的一些配置
- var barConfig request.FutureGoodBarChartInfoReq
- //barChartInfoSort := request.BarChartInfoSortReq{}
- if chartInfo.BarConfig == `` {
- msg = "商品价格曲线图未配置"
- errMsg = "商品价格曲线图未配置"
- return
- }
- err = json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
- if err != nil {
- msg = "商品价格曲线图配置异常"
- errMsg = "商品价格曲线图配置异常"
- return
- }
- baseEdbInfoId := barConfig.BaseEdbInfoId
- var baseEdbInfoMapping *chartEdbMappingModel.ChartEdbInfoMapping
- // todo 兼容历史数据,
- if baseEdbInfoId == 0 {
- // 默认取第一个现货指标
- baseEdbInfoId = edbInfoMappingList[0].EdbInfoId
- baseEdbInfoMapping = &edbInfoMappingList[0].ChartEdbInfoMapping
- barConfig.BaseEdbInfoId = baseEdbInfoId
- } else {
- baseEdbInfoMapping, err = chartEdbMappingModel.GetChartEdbMappingByEdbInfoId(baseEdbInfoId)
- if err != nil {
- msg = "获取失败"
- errMsg = "获取图表,指标信息失败,Err:" + err.Error()
- return
- }
- }
- // 获取图表中的指标数据
- barConfigEdbInfoIdList, edbList, xEdbIdValue, xDataList, yDataList, sourceArr, err := future_goodServ.GetChartEdbData(chartInfoId, startDate, endDate, baseEdbInfoMapping, edbInfoMappingList, futureGoodEdbInfoMapping, barConfig, true)
- if err != nil {
- msg = "获取失败"
- errMsg = "获取图表,指标信息失败,Err:" + err.Error()
- return
- }
- if len(edbList) <= 0 {
- msg = "商品价格曲线图表指标异常"
- errMsg = "商品价格曲线图表异常"
- return
- }
- sourceArr = append(sourceArr, "弘则研究")
- chartInfo.ChartSource = strings.Join(sourceArr, ",")
- for _, v := range edbList {
- // 指标别名
- if barConfigEdbInfoIdList != nil && len(barConfigEdbInfoIdList) > 0 {
- for _, reqEdb := range barConfigEdbInfoIdList {
- if v.EdbInfoId == reqEdb.EdbInfoId {
- v.EdbAliasName = reqEdb.Name
- v.EdbAliasNameEn = reqEdb.NameEn
- }
- }
- }
- }
- baseEdbInfo := baseEdbInfoMapping //现货指标
- chartInfo.UnitEn = baseEdbInfo.UnitEn
- // 访问记录-仅普通用户记录
- ok, _, _ := user.GetAdminByUserInfo(userInfo)
- if !ok {
- go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
- }
- // 用户是否有收藏该图表
- {
- ob := new(yb_my_chart.YbMyChart)
- cond := `user_id = ? AND chart_info_id = ?`
- pars := make([]interface{}, 0)
- pars = append(pars, userInfo.UserID, chartInfo.ChartInfoId)
- exists, e := ob.FetchByCondition(cond, pars)
- if e != nil && e != utils.ErrNoRow {
- msg = `操作失败`
- errMsg = "获取用户图表失败, Err: " + e.Error()
- return
- }
- myChartInfo := new(responseModel.MyChartItem)
- if exists != nil && exists.MyChartID > 0 {
- myChartInfo.MyChartID = exists.MyChartID
- myChartInfo.MyChartClassifyID = exists.MyChartClassifyID
- myChartInfo.ChartInfoID = exists.ChartInfoID
- myChartInfo.ChartName = exists.ChartName
- myChartInfo.UniqueCode = exists.UniqueCode
- myChartInfo.ChartImage = exists.ChartImage
- myChartInfo.UserID = exists.UserID
- myChartInfo.ReportID = exists.ReportID
- myChartInfo.ReportChapterID = exists.ReportChapterID
- myChartInfo.CreateTime = utils.TimeTransferString(utils.FormatDateTime, exists.CreateTime)
- }
- resp.MyChartInfo = myChartInfo
- }
- xDataListFinal := make([]chart_info.XData, 0)
- for _, v := range xDataList {
- tmp := chart_info.XData{
- Name: v.Name,
- NameEn: v.NameEn,
- IsHide: v.IsHide,
- }
- xDataListFinal = append(xDataListFinal, tmp)
- }
- resp.ChartInfo = chartInfo
- resp.EdbInfoList = edbList
- resp.XEdbIdValue = xEdbIdValue
- resp.XDataList = xDataListFinal
- resp.YDataList = yDataList
- isOk = true
- return
- }
- // getFutureGoodProfitChartInfoDetail 获取商品利润曲线图表详情
- func getFutureGoodProfitChartInfoDetail(chartInfo *chartInfoModel.ChartInfoView, myChartClassifyId int, userInfo user.UserInfo) (resp *chart_info.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
- resp = new(chart_info.ChartInfoDetailResp)
- // 获取图表信息
- var err error
- chartInfoId := chartInfo.ChartInfoId
- // 商品利润曲线图的一些配置
- var extraConf request.ChartInfoReq
- err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConf)
- if err != nil {
- msg = "商品利润曲线图配置异常"
- errMsg = "商品利润曲线图配置异常,Err:" + err.Error()
- return
- }
- if len(extraConf.EdbInfoIdList) == 0 {
- extraConf.EdbInfoIdList = append(extraConf.EdbInfoIdList, extraConf.BaseEdbInfoId)
- }
- edbList := make([]*chartEdbMappingModel.ChartEdbInfoMappingList, 0)
- edbInfoMappingList, err := chartEdbMappingModel.GetEtaEdbChartEdbMappingList(chartInfoId)
- if err != nil {
- msg = "获取失败"
- errMsg = "获取图表,现货指标信息失败,Err:" + err.Error()
- return
- }
- edbList = edbInfoMappingList
- baseEdbInfo := new(chartEdbMappingModel.ChartEdbInfoMapping)
- for _, v := range edbInfoMappingList {
- if v.EdbInfoId == extraConf.BaseEdbInfoId {
- baseEdbInfo = &v.ChartEdbInfoMapping
- }
- }
- futureGoodEdbInfoMappingList, err := chartEdbMappingModel.GetFutureGoodEdbChartEdbMappingList(chartInfoId)
- if err != nil {
- msg = "获取失败"
- errMsg = "获取图表的期货商品指标信息失败,Err:" + err.Error()
- return
- }
- edbList = append(edbList, futureGoodEdbInfoMappingList...)
- if len(edbList) <= 0 {
- msg = "商品利润曲线图表指标异常"
- errMsg = "商品利润曲线图表指标异常"
- return
- }
- xDataList := make([]chart_info.XData, 0)
- yDataList := make([]chart_info.YData, 0)
- // 查找商品利润图表的扩展信息
- chartInfoFutureGoodProfit := new(chart_info_future_good_profit.ChartInfoFutureGoodProfit)
- if err = chartInfoFutureGoodProfit.GetItemById(chartInfoId); err != nil {
- errMsg = "获取相关性图表失败, Err: " + err.Error()
- return
- }
- err = json.Unmarshal([]byte(chartInfoFutureGoodProfit.XValue), &xDataList)
- if err != nil {
- msg = "获取失败"
- errMsg = "转换X轴数据失败, Err: " + err.Error()
- return
- }
- err = json.Unmarshal([]byte(chartInfoFutureGoodProfit.YValue), &yDataList)
- if err != nil {
- msg = "获取失败"
- errMsg = "转换Y轴数据失败, Err: " + err.Error()
- return
- }
- chartInfo.UnitEn = baseEdbInfo.UnitEn
- sourceArr := []string{"同花顺"}
- if baseEdbInfo.SourceName != "同花顺" {
- sourceArr = append(sourceArr, baseEdbInfo.SourceName)
- }
- sourceArr = append(sourceArr, "弘则研究")
- chartInfo.ChartSource = strings.Join(sourceArr, ",")
- warnEdbList := make([]string, 0)
- for _, v := range edbList {
- if v.IsNullData {
- warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
- }
- }
- // 访问记录-仅普通用户记录
- ok, _, _ := user.GetAdminByUserInfo(userInfo)
- if !ok {
- go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
- }
- // 用户是否有收藏该图表
- {
- ob := new(yb_my_chart.YbMyChart)
- cond := `user_id = ? AND chart_info_id = ?`
- pars := make([]interface{}, 0)
- pars = append(pars, userInfo.UserID, chartInfo.ChartInfoId)
- exists, e := ob.FetchByCondition(cond, pars)
- if e != nil && e != utils.ErrNoRow {
- msg = `操作失败`
- errMsg = "获取用户图表失败, Err: " + e.Error()
- return
- }
- myChartInfo := new(responseModel.MyChartItem)
- if exists != nil && exists.MyChartID > 0 {
- myChartInfo.MyChartID = exists.MyChartID
- myChartInfo.MyChartClassifyID = exists.MyChartClassifyID
- myChartInfo.ChartInfoID = exists.ChartInfoID
- myChartInfo.ChartName = exists.ChartName
- myChartInfo.UniqueCode = exists.UniqueCode
- myChartInfo.ChartImage = exists.ChartImage
- myChartInfo.UserID = exists.UserID
- myChartInfo.ReportID = exists.ReportID
- myChartInfo.ReportChapterID = exists.ReportChapterID
- myChartInfo.CreateTime = utils.TimeTransferString(utils.FormatDateTime, exists.CreateTime)
- }
- resp.MyChartInfo = myChartInfo
- }
- resp.ChartInfo = chartInfo
- resp.EdbInfoList = edbList
- resp.DataResp = chart_info.ProfitFutureGoodChartResp{
- YDataList: yDataList,
- XDataList: xDataList,
- ProfitName: chartInfoFutureGoodProfit.ProfitName,
- ProfitNameEn: chartInfoFutureGoodProfit.ProfitNameEn,
- Extra: extraConf,
- }
- isOk = true
- return
- }
- // getChartInfoDetailFromUniqueCode 获取拟合方程图表详情
- func getChartInfoDetailFromUniqueCode(chartInfo *chartInfoModel.ChartInfoView, myChartClassifyId int, userInfo user.UserInfo) (resp *chart_info.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
- resp = new(chart_info.ChartInfoDetailResp)
- // 获取图表信息
- var err error
- if chartInfo.ExtraConfig == `` {
- msg = "获取失败"
- errMsg = "获取配置信息失败"
- return
- }
- var lineChartInfoConfig request.LineChartInfoReq
- err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &lineChartInfoConfig)
- if err != nil {
- msg = "获取失败"
- errMsg = "获取图表配置信息失败, Err:" + err.Error()
- return
- }
- var getAData, getBData, getR2Data bool
- switch lineChartInfoConfig.Source {
- case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_ONE:
- getAData = true
- case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_TWO:
- getBData = true
- case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_THREE:
- getR2Data = true
- }
- edbList, dataResp, sourceArr, err, errMsg := line_equation.GetChartEdbData(chartInfo.ChartInfoId, lineChartInfoConfig, getAData, getBData, getR2Data)
- if err != nil {
- if errMsg == `` {
- errMsg = "获取失败"
- }
- return
- }
- var resultResp interface{}
- switch lineChartInfoConfig.Source {
- case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_ONE:
- resultResp = dataResp.AData
- case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_TWO:
- resultResp = dataResp.BData
- case utils.CHART_MULTIPLE_GRAPH_LINE_EQUATION_THREE:
- resultResp = dataResp.R2Data
- }
- sourceArr = append(sourceArr, "弘则研究")
- chartInfo.ChartSource = strings.Join(sourceArr, ",")
- baseEdbInfo := edbList[0] //现货指标
- chartInfo.UnitEn = baseEdbInfo.UnitEn
- // 访问记录-仅普通用户记录
- ok, _, _ := user.GetAdminByUserInfo(userInfo)
- if !ok {
- go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
- }
- // 用户是否有收藏该图表
- {
- ob := new(yb_my_chart.YbMyChart)
- cond := `user_id = ? AND chart_info_id = ?`
- pars := make([]interface{}, 0)
- pars = append(pars, userInfo.UserID, chartInfo.ChartInfoId)
- exists, e := ob.FetchByCondition(cond, pars)
- if e != nil && e != utils.ErrNoRow {
- msg = `操作失败`
- errMsg = "获取用户图表失败, Err: " + e.Error()
- return
- }
- myChartInfo := new(responseModel.MyChartItem)
- if exists != nil && exists.MyChartID > 0 {
- myChartInfo.MyChartID = exists.MyChartID
- myChartInfo.MyChartClassifyID = exists.MyChartClassifyID
- myChartInfo.ChartInfoID = exists.ChartInfoID
- myChartInfo.ChartName = exists.ChartName
- myChartInfo.UniqueCode = exists.UniqueCode
- myChartInfo.ChartImage = exists.ChartImage
- myChartInfo.UserID = exists.UserID
- myChartInfo.ReportID = exists.ReportID
- myChartInfo.ReportChapterID = exists.ReportChapterID
- myChartInfo.CreateTime = utils.TimeTransferString(utils.FormatDateTime, exists.CreateTime)
- }
- resp.MyChartInfo = myChartInfo
- }
- resp.ChartInfo = chartInfo
- resp.EdbInfoList = edbList
- resp.DataResp = resultResp
- isOk = true
- return
- }
- // GetLineFeatureChartInfoDetailFromUniqueCode 根据编码获取统计特征图表详情
- func getLineFeatureChartInfoDetailFromUniqueCode(chartInfo *chartInfoModel.ChartInfoView, myChartClassifyId int, userInfo user.UserInfo) (resp *chart_info.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
- resp = new(chart_info.ChartInfoDetailResp)
- // 获取图表信息
- var err error
- // 获取图表关联指标
- edbMappingList, err := chartEdbMappingModel.GetMappingListByChartInfoId(chartInfo.ChartInfoId)
- if err != nil {
- msg = "获取失败"
- errMsg = "获取图表关联指标信息失败,Err:" + err.Error()
- return
- }
- if len(edbMappingList) != 1 {
- msg = "获取失败"
- errMsg = fmt.Sprint("获取图表关联指标信息异常,数量:", len(edbMappingList))
- return
- }
- edbMapping := edbMappingList[0]
- var edbList []*chartEdbMappingModel.ChartEdbInfoMappingList
- var resultResp interface{}
- sourceArr := make([]string, 0)
- switch chartInfo.Source {
- case utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION:
- calculateValue, tmpErr := strconv.Atoi(chartInfo.ExtraConfig)
- if tmpErr != nil {
- msg = "获取失败"
- errMsg = "格式化配置项失败,Err:" + tmpErr.Error()
- return
- }
- startDate, endDate := utils.GetDateByDateType(chartInfo.DateType, chartInfo.StartDate, chartInfo.EndDate)
- edbList, resultResp, sourceArr, err, msg = line_feature.GetStandardDeviationData(0, startDate, endDate, edbMapping, calculateValue)
- case utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE:
- var percentileConfig request.Percentile
- err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &percentileConfig)
- if err != nil {
- msg = "获取失败"
- errMsg = "格式化配置项失败,Err:" + err.Error()
- return
- }
- startDate, endDate := utils.GetDateByDateType(chartInfo.DateType, chartInfo.StartDate, chartInfo.EndDate)
- edbList, resultResp, sourceArr, err, msg = line_feature.GetPercentileData(0, startDate, endDate, edbMapping, percentileConfig.CalculateValue, percentileConfig.CalculateUnit)
- case utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY:
- var frequencyDistributionConfig request.FrequencyDistribution
- err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &frequencyDistributionConfig)
- if err != nil {
- msg = "获取失败"
- errMsg = "格式化配置项失败,Err:" + err.Error()
- return
- }
- // 获取图表中的指标数据
- edbList, resultResp, sourceArr, err, errMsg = line_feature.GetFrequencyDistributionData(0, edbMapping, frequencyDistributionConfig.DateType, frequencyDistributionConfig.FrequencyValue, frequencyDistributionConfig.StartDate, frequencyDistributionConfig.EndDate)
- default:
- msg = `错误的图表`
- errMsg = fmt.Sprint("错误的图表来源,source", chartInfo.Source)
- return
- }
- if err != nil {
- if msg == `` {
- msg = "获取失败"
- }
- errMsg = "获取图表,指标信息失败,Err:" + err.Error()
- return
- }
- sourceArr = append(sourceArr, "弘则研究")
- chartInfo.ChartSource = strings.Join(sourceArr, ",")
- //baseEdbInfo := edbList[0] //现货指标
- //chartInfo.UnitEn = baseEdbInfo.UnitEn
- // 访问记录-仅普通用户记录
- ok, _, _ := user.GetAdminByUserInfo(userInfo)
- if !ok {
- go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
- }
- // 用户是否有收藏该图表
- {
- ob := new(yb_my_chart.YbMyChart)
- cond := `user_id = ? AND chart_info_id = ?`
- pars := make([]interface{}, 0)
- pars = append(pars, userInfo.UserID, chartInfo.ChartInfoId)
- exists, e := ob.FetchByCondition(cond, pars)
- if e != nil && e != utils.ErrNoRow {
- msg = `操作失败`
- errMsg = "获取用户图表失败, Err: " + e.Error()
- return
- }
- myChartInfo := new(responseModel.MyChartItem)
- if exists != nil && exists.MyChartID > 0 {
- myChartInfo.MyChartID = exists.MyChartID
- myChartInfo.MyChartClassifyID = exists.MyChartClassifyID
- myChartInfo.ChartInfoID = exists.ChartInfoID
- myChartInfo.ChartName = exists.ChartName
- myChartInfo.UniqueCode = exists.UniqueCode
- myChartInfo.ChartImage = exists.ChartImage
- myChartInfo.UserID = exists.UserID
- myChartInfo.ReportID = exists.ReportID
- myChartInfo.ReportChapterID = exists.ReportChapterID
- myChartInfo.CreateTime = utils.TimeTransferString(utils.FormatDateTime, exists.CreateTime)
- }
- resp.MyChartInfo = myChartInfo
- }
- resp.ChartInfo = chartInfo
- resp.EdbInfoList = edbList
- resp.DataResp = resultResp
- isOk = true
- return
- }
- // GetCrossVarietyChartInfoDetailFromUniqueCode
- // @Description: 获取跨品种分析图表
- // @author: Roc
- // @datetime 2023-11-29 09:38:23
- // @param chartInfo *chartInfoModel.ChartInfoView
- // @param myChartClassifyId int
- // @param userInfo user.UserInfo
- // @return resp *chart_info.ChartInfoDetailResp
- // @return isOk bool
- // @return msg string
- // @return errMsg string
- func GetCrossVarietyChartInfoDetailFromUniqueCode(chartInfo *chartInfoModel.ChartInfoView, myChartClassifyId int, userInfo user.UserInfo) (resp *chart_info.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
- resp = new(chart_info.ChartInfoDetailResp)
- // 获取图表信息
- var err error
- if chartInfo.ExtraConfig == `` {
- msg = "图表配置信息异常"
- errMsg = "图表配置信息异常"
- return
- }
- var config request.ChartConfigReq
- err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &config)
- if err != nil {
- msg = "解析跨品种分析配置失败"
- errMsg = "解析跨品种分析配置失败,Err:" + err.Error()
- return
- }
- var edbList []*chartEdbMappingModel.ChartEdbInfoMappingList
- var resultResp interface{}
- sourceArr := make([]string, 0)
- // 获取图表x轴y轴
- edbList, resultResp, sourceArr, err, msg, _ = cross_variety.GetChartData(0, config)
- if err != nil {
- errMsg = "获取图表,指标信息失败,Err:" + err.Error()
- return
- }
- if err != nil {
- if msg == `` {
- msg = "获取失败"
- }
- errMsg = "获取图表,指标信息失败,Err:" + err.Error()
- return
- }
- sourceArr = append(sourceArr, "弘则研究")
- chartInfo.ChartSource = strings.Join(sourceArr, ",")
- //baseEdbInfo := edbList[0] //现货指标
- //chartInfo.UnitEn = baseEdbInfo.UnitEn
- // 访问记录-仅普通用户记录
- ok, _, _ := user.GetAdminByUserInfo(userInfo)
- if !ok {
- go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
- }
- // 用户是否有收藏该图表
- {
- ob := new(yb_my_chart.YbMyChart)
- cond := `user_id = ? AND chart_info_id = ?`
- pars := make([]interface{}, 0)
- pars = append(pars, userInfo.UserID, chartInfo.ChartInfoId)
- exists, e := ob.FetchByCondition(cond, pars)
- if e != nil && e != utils.ErrNoRow {
- msg = `操作失败`
- errMsg = "获取用户图表失败, Err: " + e.Error()
- return
- }
- myChartInfo := new(responseModel.MyChartItem)
- if exists != nil && exists.MyChartID > 0 {
- myChartInfo.MyChartID = exists.MyChartID
- myChartInfo.MyChartClassifyID = exists.MyChartClassifyID
- myChartInfo.ChartInfoID = exists.ChartInfoID
- myChartInfo.ChartName = exists.ChartName
- myChartInfo.UniqueCode = exists.UniqueCode
- myChartInfo.ChartImage = exists.ChartImage
- myChartInfo.UserID = exists.UserID
- myChartInfo.ReportID = exists.ReportID
- myChartInfo.ReportChapterID = exists.ReportChapterID
- myChartInfo.CreateTime = utils.TimeTransferString(utils.FormatDateTime, exists.CreateTime)
- }
- resp.MyChartInfo = myChartInfo
- }
- resp.ChartInfo = chartInfo
- resp.EdbInfoList = edbList
- resp.DataResp = resultResp
- isOk = true
- return
- }
- // RefreshFutureGoodChartInfo 刷新商品价格曲线图表信息
- // @Tags 图库模块
- // @Summary 刷新图表信息
- // @Description 刷新图表信息
- // @Security ApiKeyAuth
- // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
- // @Accept json
- // @Product json
- // @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
- // @Success 200 {string} string "操作成功"
- // @failure 400 {string} string "操作失败"
- // @Router /my_chart/future_good/refreshChartInfo [post]
- func RefreshFutureGoodChartInfo(c *gin.Context) {
- // 参数校验
- var req chartInfoModel.RefreshChartInfoReq
- if c.ShouldBind(&req) != nil {
- response.Fail("参数异常", c)
- return
- }
- chartInfoId := req.ChartInfoId
- if chartInfoId == 0 {
- response.Fail("参数有误", c)
- return
- }
- //userInfo := user.GetInfoByClaims(c)
- //ok, _, err := user.GetAdminByUserInfo(userInfo)
- //if err != nil {
- // response.FailMsg("刷新失败", "RefreshChartInfo-获取系统用户信息失败"+err.Error(), c)
- // return
- //}
- //if !ok {
- // // 普通用户刷新频率限制-每个用户/图/天/2次
- // cacheKey := utils.HZ_CHART_LIB_DETAIL + "YB_REFRESH_LIMIT_" + strconv.Itoa(chartInfoId) + "_" + strconv.Itoa(int(userInfo.UserID))
- // fmt.Println("refreshCacheKey:", cacheKey)
- // countUserRefresh, _ := global.Redis.Get(context.TODO(), cacheKey).Int()
- // if countUserRefresh >= 2 {
- // response.Ok("目前已是最新数据", c)
- // return
- // }
- // countUserRefresh += 1
- // now := time.Now()
- // today := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.Local)
- // sub := today.Sub(now)
- // _ = global.Redis.SetEX(context.TODO(), cacheKey, countUserRefresh, sub)
- //}
- // 图表信息校验
- chartInfo, err := chartInfoModel.GetChartInfoById(chartInfoId)
- if err != nil {
- if err == utils.ErrNoRow {
- response.Fail("图表已被删除,无需刷新", c)
- return
- }
- response.FailMsg("刷新失败", "刷新失败, Err:"+err.Error(), c)
- return
- }
- // 刷新图表
- if err = future_goodServ.FutureGoodChartInfoRefresh(chartInfo.ChartInfoId); err != nil {
- errContent := fmt.Sprint("ErrMsg: 刷新图表关联指标信息失败, " + err.Error())
- if global.CONFIG.Serve.RunMode == "release" {
- go alarm_msg.SendAlarmMsg("刷新图表报错"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+errContent, 3)
- //go services.SendEmail("弘则研报小程序-release-刷新图表报错", errContent, utils.EmailSendToUsers)
- } else {
- global.LOG.Info(errContent)
- }
- }
- //清除图表缓存
- {
- key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
- _ = global.Redis.Del(context.TODO(), key)
- }
- response.OkData("刷新成功", "", c)
- }
- // FutureGoodChartInfoSave 编辑商品价格图表信息
- // @Tags 图库模块
- // @Summary 编辑图表信息
- // @Description 编辑图表信息
- // @Security ApiKeyAuth
- // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
- // @Accept json
- // @Product json
- // @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
- // @Success 200 {string} string "操作成功"
- // @failure 400 {string} string "操作失败"
- // @Router /my_chart/editChartInfo [post]
- func FutureGoodChartInfoSave(c *gin.Context) {
- // 参数校验
- var req chartInfoModel.SaveChartInfoReq
- if c.ShouldBind(&req) != nil {
- response.Fail("参数异常", c)
- return
- }
- if req.ChartInfoId <= 0 {
- response.Fail("参数异常", c)
- return
- }
- // 操作权限校验
- userInfo := user.GetInfoByClaims(c)
- ok, adminInfo, err := user.GetAdminByUserInfo(userInfo)
- if err != nil {
- response.Fail("操作人信息有误", c)
- return
- }
- if !ok {
- response.Fail("非内部人员无权进行操作", c)
- return
- }
- // 图表信息校验
- chartItem, err := chartInfoModel.GetChartInfoById(req.ChartInfoId)
- if err != nil {
- if err == utils.ErrNoRow {
- response.Fail("图表已被删除,请刷新页面!", c)
- return
- }
- response.FailMsg("操作失败", "获取图表信息失败, Err:"+err.Error(), c)
- return
- }
- if !utils.InArrayByInt([]int{utils.CHART_SOURCE_FUTURE_GOOD, utils.CHART_SOURCE_FUTURE_GOOD_PROFIT}, chartItem.Source) {
- response.Fail("该图不是商品价格/利润曲线图!", c)
- return
- }
- chartItem.LeftMin = req.LeftMin
- chartItem.LeftMax = req.LeftMax
- err = chartItem.Update([]string{"LeftMin", "LeftMax"})
- if err != nil {
- response.FailMsg("保存失败", "保存失败,Err:"+err.Error(), c)
- return
- }
- // 清除图表缓存
- cacheKey := utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode
- _ = global.Redis.Del(context.TODO(), cacheKey)
- // 新增操作日志
- {
- chartLog := new(chart_info_log.ChartInfoLog)
- chartLog.ChartName = chartItem.ChartName
- chartLog.ChartInfoId = req.ChartInfoId
- chartLog.ChartClassifyId = chartItem.ChartClassifyId
- chartLog.SysUserId = int(adminInfo.AdminID)
- chartLog.SysUserRealName = adminInfo.RealName
- chartLog.UniqueCode = chartItem.UniqueCode
- chartLog.CreateTime = time.Now()
- bodyBytes, _ := ioutil.ReadAll(c.Request.Body)
- chartLog.Content = string(bodyBytes)
- chartLog.Status = "修改配置项"
- chartLog.Method = c.Request.URL.String()
- go chartLog.Create()
- }
- response.OkData("操作成功", "", c)
- }
- // getCorrelationChartInfoDetail 获取相关性图表详情
- func getCorrelationChartInfoDetail(chartInfo *chartInfoModel.ChartInfoView, myChartClassifyId int, userInfo user.UserInfo) (resp *chart_info.ChartInfoDetailResp, isOk bool, msg, errMsg string) {
- resp = new(chart_info.ChartInfoDetailResp)
- // 获取图表信息
- //var err error
- chartInfoId := chartInfo.ChartInfoId
- chartInfo.ChartSource = "弘则研究"
- startDate := chartInfo.StartDate
- endDate := chartInfo.EndDate
- // 兼容日期错误
- {
- if strings.Count(startDate, "-") == 1 {
- startDate = startDate + "-01"
- }
- if strings.Count(endDate, "-") == 1 {
- endDate = endDate + "-01"
- }
- }
- // 相关性图表信息
- correlationChart := new(chart_info_correlation.ChartInfoCorrelation)
- if e := correlationChart.GetItemById(chartInfoId); e != nil {
- msg = "获取失败"
- errMsg = "获取图表相关性信息失败, Err:" + e.Error()
- return
- }
- // 获取指标信息
- edbInfoMappingA, e := chartEdbMappingModel.GetChartEdbMappingByEdbInfoId(correlationChart.EdbInfoIdFirst)
- if e != nil {
- msg = "获取失败"
- errMsg = "获取相关性图表, A指标mapping信息失败, Err:" + e.Error()
- return
- }
- edbInfoMappingB := new(chartEdbMappingModel.ChartEdbInfoMapping)
- if correlationChart.AnalysisMode != 1 {
- edbInfoMappingB, e = chartEdbMappingModel.GetChartEdbMappingByEdbInfoId(correlationChart.EdbInfoIdSecond)
- if e != nil {
- msg = "获取失败"
- errMsg = "获取相关性图表, B指标mapping信息失败, Err:" + e.Error()
- return
- }
- }
- var dataResp interface{} // 绘图数据返回(目前是滚动相关性的图)
- var xEdbIdValue []int
- var yDataList []chart_info.YData
- if correlationChart.AnalysisMode != 1 {
- switch chartInfo.Source {
- case utils.CHART_SOURCE_CORRELATION: // 相关性图
- moveUnitDays, ok := utils.FrequencyDaysMap[correlationChart.CalculateUnit]
- if !ok {
- msg = "错误的分析周期"
- errMsg = "相关性图表数据有误"
- return
- }
- st := time.Now().AddDate(0, 0, -correlationChart.CalculateValue*moveUnitDays).Format(utils.FormatDate)
- ed := time.Now().Format(utils.FormatDate)
- xEdbIdValue, yDataList, e = correlation.GetChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, correlationChart.LeadValue, correlationChart.LeadUnit, st, ed)
- if e != nil {
- msg = "获取失败"
- errMsg = "获取相关性图表, 图表计算值失败, Err:" + e.Error()
- return
- }
- case utils.CHART_SOURCE_ROLLING_CORRELATION: // 滚动相关性图
- st, ed := utils.GetDateByDateType(correlationChart.DateType, correlationChart.StartDate.Format(utils.FormatDate), correlationChart.EndDate.Format(utils.FormatDate))
- dataResp, e = correlation.GetRollingCorrelationChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB, correlationChart.LeadValue, correlationChart.LeadUnit, correlationChart.CalculateValue, correlationChart.CalculateUnit, st, ed, chartInfo.ChartName, chartInfo.ChartNameEn)
- }
- } else {
- xEdbIdValue, yDataList, e = correlation.GetFactorChartDataByChartId(chartInfoId, chartInfo.ExtraConfig)
- if e != nil {
- msg = "获取失败"
- errMsg = "获取相关性图表, 图表计算值失败, Err:" + e.Error()
- return
- }
- }
- // 完善指标信息
- edbList, e := correlation.GetChartEdbInfoFormat(chartInfo.ChartInfoId, edbInfoMappingA, edbInfoMappingB)
- if e != nil {
- msg = "获取失败"
- errMsg = "获取相关性图表, 完善指标信息失败, Err:" + e.Error()
- return
- }
- baseEdbInfo := edbList[0] //现货指标
- chartInfo.UnitEn = baseEdbInfo.UnitEn
- correlationInfo := new(chart_info.CorrelationInfo)
- correlationInfo.LeadValue = correlationChart.LeadValue
- correlationInfo.LeadUnit = correlationChart.LeadUnit
- correlationInfo.StartDate = correlationChart.StartDate.Format(utils.FormatDate)
- correlationInfo.EndDate = correlationChart.EndDate.Format(utils.FormatDate)
- correlationInfo.LeadValue = correlationChart.LeadValue
- correlationInfo.EdbInfoIdFirst = correlationChart.EdbInfoIdFirst
- correlationInfo.EdbInfoIdSecond = correlationChart.EdbInfoIdSecond
- correlationInfo.AnalysisMode = correlationChart.AnalysisMode
- // 访问记录-仅普通用户记录
- ok, _, _ := user.GetAdminByUserInfo(userInfo)
- if !ok {
- go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
- }
- // 用户是否有收藏该图表
- {
- ob := new(yb_my_chart.YbMyChart)
- cond := `user_id = ? AND chart_info_id = ?`
- pars := make([]interface{}, 0)
- pars = append(pars, userInfo.UserID, chartInfo.ChartInfoId)
- exists, e := ob.FetchByCondition(cond, pars)
- if e != nil && e != utils.ErrNoRow {
- msg = `操作失败`
- errMsg = "获取用户图表失败, Err: " + e.Error()
- return
- }
- myChartInfo := new(responseModel.MyChartItem)
- if exists != nil && exists.MyChartID > 0 {
- myChartInfo.MyChartID = exists.MyChartID
- myChartInfo.MyChartClassifyID = exists.MyChartClassifyID
- myChartInfo.ChartInfoID = exists.ChartInfoID
- myChartInfo.ChartName = exists.ChartName
- myChartInfo.UniqueCode = exists.UniqueCode
- myChartInfo.ChartImage = exists.ChartImage
- myChartInfo.UserID = exists.UserID
- myChartInfo.ReportID = exists.ReportID
- myChartInfo.ReportChapterID = exists.ReportChapterID
- myChartInfo.CreateTime = utils.TimeTransferString(utils.FormatDateTime, exists.CreateTime)
- }
- resp.MyChartInfo = myChartInfo
- }
- resp.ChartInfo = chartInfo
- resp.EdbInfoList = edbList
- resp.XEdbIdValue = xEdbIdValue
- resp.YDataList = yDataList
- resp.CorrelationChartInfo = correlationInfo
- resp.DataResp = dataResp
- isOk = true
- return
- }
- // RefreshCrossVarietyChartInfo 刷新跨品种分析图表信息
- // @Tags 图库模块
- // @Summary 刷新跨品种分析图表信息
- // @Description 刷新跨品种分析图表信息
- // @Security ApiKeyAuth
- // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
- // @Accept json
- // @Product json
- // @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
- // @Success 200 {string} string "操作成功"
- // @failure 400 {string} string "操作失败"
- // @Router /my_chart/cross_variety/refreshChartInfo [post]
- func RefreshCrossVarietyChartInfo(c *gin.Context) {
- // 参数校验
- var req chartInfoModel.RefreshChartInfoReq
- if c.ShouldBind(&req) != nil {
- response.Fail("参数异常", c)
- return
- }
- chartInfoId := req.ChartInfoId
- if chartInfoId == 0 {
- response.Fail("参数有误", c)
- return
- }
- userInfo := user.GetInfoByClaims(c)
- ok, _, err := user.GetAdminByUserInfo(userInfo)
- if err != nil {
- response.FailMsg("刷新失败", "RefreshChartInfo-获取系统用户信息失败"+err.Error(), c)
- return
- }
- if !ok {
- // 普通用户刷新频率限制-每个用户/图/天/2次
- cacheKey := utils.HZ_CHART_LIB_DETAIL + "YB_REFRESH_LIMIT_" + strconv.Itoa(chartInfoId) + "_" + strconv.Itoa(int(userInfo.UserID))
- countUserRefresh, _ := global.Redis.Get(context.TODO(), cacheKey).Int()
- if countUserRefresh >= 2 {
- response.Ok("目前已是最新数据", c)
- return
- }
- countUserRefresh += 1
- now := time.Now()
- today := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.Local)
- sub := today.Sub(now)
- _ = global.Redis.SetEX(context.TODO(), cacheKey, countUserRefresh, sub)
- }
- // 图表信息校验
- chartInfo, err := chartInfoModel.GetChartInfoById(chartInfoId)
- if err != nil {
- if err == utils.ErrNoRow {
- response.Fail("图表已被删除,无需刷新", c)
- return
- }
- response.FailMsg("刷新失败", "刷新失败, Err:"+err.Error(), c)
- return
- }
- var config request.ChartConfigReq
- err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &config)
- if err != nil {
- response.FailMsg("解析跨品种分析配置失败", "解析跨品种分析配置失败, Err:"+err.Error(), c)
- return
- }
- // 获取关联的指标信息
- _, _, edbInfoIdList, err := cross_variety.GetXYEdbIdList(config.TagX, config.TagY, config.VarietyList)
- if err != nil {
- response.FailMsg("获取指标信息失败", "刷新失败,获取指标信息失败, Err:"+err.Error(), c)
- return
- }
- // 刷新图表
- //_ = correlation.ChartInfoRefresh(chartInfo.ChartInfoId)
- err, errMsg := chart.EdbInfoRefreshAllFromBase(edbInfoIdList, false)
- if err != nil {
- errContent := fmt.Sprint("刷新图表关联指标信息失败 errMsg:", errMsg, "err: "+err.Error())
- if global.CONFIG.Serve.RunMode == "release" {
- go alarm_msg.SendAlarmMsg("刷新图表报错"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+errContent, 3)
- //go services.SendEmail("弘则研报小程序-release-刷新图表报错", errContent, utils.EmailSendToUsers)
- } else {
- global.LOG.Info(errContent)
- }
- response.FailMsg("刷新失败", "刷新失败,获取指标信息失败, Err:"+err.Error(), c)
- return
- }
- //清除图表缓存
- {
- key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
- _ = global.Redis.Del(context.TODO(), key)
- }
- response.OkData("刷新成功", "", c)
- }
- // RefreshCorrelationChartInfo 刷新相关性图表信息
- // @Tags 图库模块
- // @Summary 刷新图表信息
- // @Description 刷新图表信息
- // @Security ApiKeyAuth
- // @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
- // @Accept json
- // @Product json
- // @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
- // @Success 200 {string} string "操作成功"
- // @failure 400 {string} string "操作失败"
- // @Router /my_chart/correlation/refreshChartInfo [post]
- func RefreshCorrelationChartInfo(c *gin.Context) {
- // 参数校验
- var req chartInfoModel.RefreshChartInfoReq
- if c.ShouldBind(&req) != nil {
- response.Fail("参数异常", c)
- return
- }
- chartInfoId := req.ChartInfoId
- if chartInfoId == 0 {
- response.Fail("参数有误", c)
- return
- }
- userInfo := user.GetInfoByClaims(c)
- ok, _, err := user.GetAdminByUserInfo(userInfo)
- if err != nil {
- response.FailMsg("刷新失败", "RefreshChartInfo-获取系统用户信息失败"+err.Error(), c)
- return
- }
- if !ok {
- // 普通用户刷新频率限制-每个用户/图/天/2次
- cacheKey := utils.HZ_CHART_LIB_DETAIL + "YB_REFRESH_LIMIT_" + strconv.Itoa(chartInfoId) + "_" + strconv.Itoa(int(userInfo.UserID))
- countUserRefresh, _ := global.Redis.Get(context.TODO(), cacheKey).Int()
- if countUserRefresh >= 2 {
- response.Ok("目前已是最新数据", c)
- return
- }
- countUserRefresh += 1
- now := time.Now()
- today := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.Local)
- sub := today.Sub(now)
- _ = global.Redis.SetEX(context.TODO(), cacheKey, countUserRefresh, sub)
- }
- // 图表信息校验
- chartInfo, err := chartInfoModel.GetChartInfoById(chartInfoId)
- if err != nil {
- if err == utils.ErrNoRow {
- response.Fail("图表已被删除,无需刷新", c)
- return
- }
- response.FailMsg("刷新失败", "刷新失败, Err:"+err.Error(), c)
- return
- }
- // 刷新图表
- //_ = correlation.ChartInfoRefresh(chartInfo.ChartInfoId)
- if err = chart.ChartInfoRefreshV2(chartInfo.ChartInfoId); err != nil {
- errContent := fmt.Sprint("ErrMsg: 刷新图表关联指标信息失败, " + err.Error())
- if global.CONFIG.Serve.RunMode == "release" {
- go alarm_msg.SendAlarmMsg("刷新图表报错"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+errContent, 3)
- //go services.SendEmail("弘则研报小程序-release-刷新图表报错", errContent, utils.EmailSendToUsers)
- } else {
- global.LOG.Info(errContent)
- }
- }
- //清除图表缓存
- {
- key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
- _ = global.Redis.Del(context.TODO(), key)
- }
- response.OkData("刷新成功", "", c)
- }
|