1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039 |
- package models
- import (
- "errors"
- "eta/eta_chart_lib/models/mgo"
- "eta/eta_chart_lib/utils"
- "fmt"
- "strconv"
- "time"
- "github.com/beego/beego/v2/client/orm"
- "go.mongodb.org/mongo-driver/bson"
- "github.com/nosixtools/solarlunar"
- )
- type ChartInfo struct {
- ChartInfoId int `json:"-" orm:"column(chart_info_id);pk"`
- ChartName string `description:"来源名称"`
- ChartNameEn string `description:"英文图表名称"`
- ChartClassifyId int `json:"-" description:"图表分类id"`
- SysUserId int `json:"-"`
- SysUserRealName string `json:"-"`
- UniqueCode string `description:"图表唯一编码"`
- CreateTime time.Time `json:"-"`
- ModifyTime time.Time `json:"-"`
- DateType int `description:"日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"`
- StartDate string `description:"自定义开始日期"`
- EndDate string `description:"自定义结束日期"`
- IsSetName int `description:"设置名称"`
- EdbInfoIds string `description:"指标id"`
- ChartType int `description:"生成样式:1:曲线图,2:季节性图"`
- Calendar string `description:"公历/农历"`
- SeasonStartDate string `description:"季节性图开始日期"`
- SeasonEndDate string `description:"季节性图开始日期"`
- ChartImage string `description:"图表图片"`
- BarConfig string `description:"柱方图的配置,json数据" json:"-"`
- XMin string `description:"图表X轴最小值"`
- XMax string `description:"图表X轴最大值"`
- LeftMin string `description:"图表左侧最小值"`
- LeftMax string `description:"图表左侧最大值"`
- RightMin string `description:"图表右侧最小值"`
- RightMax string `description:"图表右侧最大值"`
- Right2Min string `description:"图表右侧2最小值"`
- Right2Max string `description:"图表右侧2最大值"`
- MinMaxSave int `description:"是否手动保存过上下限:0-否;1-是"`
- Source int `description:"1:ETA图库;2:商品价格曲线"`
- UnitEn string `description:"英文图表单位"`
- Unit string `description:"图表单位"`
- ExtraConfig string `description:"图表额外配置,json数据" json:"-"`
- ChartSource string `description:"图表来源str"`
- ChartSourceEn string `description:"图表来源(英文)"`
- SeasonExtraConfig string `description:"季节性图表中的配置,json数据"`
- StartYear int `description:"当选择的日期类型为最近N年类型时,即date_type=20, 用start_year表示N"`
- ChartThemeId int `description:"图表应用主题ID"`
- ChartThemeStyle string `description:"图表应用主题样式"`
- SourcesFrom string `description:"图表来源"`
- Instructions string `description:"图表说明"`
- MarkersLines string `description:"标识线"`
- MarkersAreas string `description:"标识区"`
- }
- func GetChartInfoByUniqueCode(uniqueCode string) (item *ChartInfo, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM chart_info WHERE unique_code=? `
- err = o.Raw(sql, uniqueCode).QueryRow(&item)
- return
- }
- type ChartEdbInfoMapping struct {
- EdbInfoId int `description:"指标id"`
- SourceName string `description:"来源名称"`
- Source int `description:"来源id"`
- EdbCode string `description:"指标编码"`
- EdbName string `description:"指标名称"`
- EdbAliasName string `description:"指标名称(别名)"`
- EdbAliasNameEn string `description:"英文指标名称(别名)"`
- EdbNameEn string `description:"英文指标名称"`
- Frequency string `description:"频率"`
- FrequencyEn string `description:"英文频率"`
- Unit string `description:"单位"`
- UnitEn string `description:"英文单位"`
- StartDate string `description:"起始日期"`
- EndDate string `description:"终止日期"`
- ModifyTime string `description:"指标最后更新时间"`
- ChartEdbMappingId int `description:"图表指标id" json:"-"`
- ChartInfoId int `description:"图表id"`
- MaxData float64 `description:"上限"`
- MinData float64 `description:"下限"`
- IsOrder bool `description:"true:正序,false:逆序"`
- IsAxis int `description:"1:左轴,0:右轴"`
- EdbInfoType int `description:"1:标准指标,0:领先指标"`
- EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
- LeadValue int `description:"领先值"`
- LeadUnit string `description:"领先单位"`
- LeadUnitEn string `description:"领先英文单位"`
- ChartStyle string `description:"图表类型"`
- ChartColor string `description:"颜色"`
- ChartWidth float64 `description:"线条大小"`
- DataList interface{}
- EdbInfoCategoryType int `description:"0:普通指标,1:预测指标"`
- PredictChartColor string `description:"预测数据的颜色"`
- ChartType int `description:"生成样式:1:曲线图,2:季节性图,3:面积图,4:柱状图,5:散点图,6:组合图"`
- LatestDate string `description:"数据最新日期"`
- LatestValue float64 `description:"数据最新值"`
- MoveLatestDate string `description:"移动后的数据最新日期"`
- UniqueCode string `description:"指标唯一编码"`
- MinValue float64 `json:"-" description:"最小值"`
- MaxValue float64 `json:"-" description:"最大值"`
- IsNullData bool `json:"-" description:"是否空数据"`
- MappingSource int `description:"1:ETA图库;2:商品价格曲线"`
- SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
- SubSourceName string `description:"子数据来源名称"`
- IndicatorCode string `description:"指标代码"`
- StockCode string `description:"证券代码"`
- IsConvert int `description:"是否数据转换 0不转 1转"`
- ConvertType int `description:"数据转换类型 1乘 2除 3对数"`
- ConvertValue float64 `description:"数据转换值"`
- ConvertUnit string `description:"数据转换单位"`
- ConvertEnUnit string `description:"数据转换单位"`
- ClassifyId int `description:"分类id"`
- IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
- }
- func GetChartEdbMappingList(chartInfoId int) (list []*ChartEdbInfoMapping, err error) {
- o := orm.NewOrmUsingDB("data")
- aField := `a.chart_edb_mapping_id,a.chart_info_id,a.edb_info_id,a.create_time,a.modify_time,a.unique_code,a.max_data,a.min_data,a.is_order,a.is_axis,a.edb_info_type,a.lead_value,a.lead_unit,a.chart_style,a.chart_color,a.predict_chart_color,a.chart_width,a.source as mapping_source,a.edb_alias_name`
- sql := ` SELECT ` + aField + `,b.source_name,b.source,b.edb_code,b.edb_name,b.edb_name_en,b.frequency,b.unit,b.unit_en,b.start_date,b.end_date,b.modify_time,b.edb_type,b.latest_date,b.latest_value,b.unique_code,b.edb_info_type AS edb_info_category_type,b.sub_source,
- a.is_convert, a.convert_type, a.convert_value, a.convert_unit, a.convert_en_unit
- FROM chart_edb_mapping AS a
- INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
- WHERE chart_info_id=?
- ORDER BY chart_edb_mapping_id ASC `
- _, err = o.Raw(sql, chartInfoId).QueryRows(&list)
- return
- }
- type EdbDataList struct {
- EdbDataId int `json:"-" description:" 指标数据ID"`
- EdbInfoId int `json:"-" description:"指标ID"`
- DataTime string `description:"数据日期"`
- DataTimestamp int64 `description:"数据日期"`
- Value float64 `description:"数据值"`
- }
- // GetEdbDataList 获取指标的数据(日期正序返回)
- func GetEdbDataList(source, subSource, edbInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
- // 自有数据需要额外处理(从mongo获取)
- if source == utils.DATA_SOURCE_BUSINESS && utils.UseMongo {
- return getEdbDataListByMongo(source, subSource, edbInfoId, startDate, endDate)
- }
- if source == utils.DATA_SOURCE_THS && subSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY && utils.UseMongo {
- return getThsHfEdbDataListByMongo(source, subSource, edbInfoId, startDate, endDate)
- }
- return getEdbDataListByMysql(source, subSource, edbInfoId, startDate, endDate)
- }
- // getEdbDataListByMysql
- // @Description: 从mysql中获取指标的数据(日期正序返回)
- // @author: Roc
- // @datetime 2024-05-08 16:49:48
- // @param source int
- // @param subSource int
- // @param edbInfoId int
- // @param startDate string
- // @param endDate string
- // @return list []*EdbDataList
- // @return err error
- func getEdbDataListByMysql(source, subSource, edbInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
- tableName := GetEdbDataTableName(source, subSource)
- if tableName == "" {
- err = errors.New("无效的渠道:" + strconv.Itoa(source))
- list = make([]*EdbDataList, 0)
- return list, err
- }
- var pars []interface{}
- var condition string
- if startDate != "" {
- condition += ` AND data_time>=? `
- pars = append(pars, startDate)
- }
- if endDate != "" {
- condition += ` AND data_time<=? `
- pars = append(pars, endDate)
- }
- sql := `SELECT edb_data_id,edb_info_id,data_time,value,data_timestamp FROM %s WHERE edb_info_id=? `
- if condition != "" {
- sql += condition
- }
- sql += ` ORDER BY data_time ASC `
- sql = fmt.Sprintf(sql, tableName)
- o := orm.NewOrmUsingDB("data")
- _, err = o.Raw(sql, edbInfoId, pars).QueryRows(&list)
- return
- }
- // getEdbDataListByMongo
- // @Description: 从mongo中获取指标的数据(日期正序返回)
- // @author: Roc
- // @datetime 2024-05-08 16:49:59
- // @param source int
- // @param subSource int
- // @param edbInfoId int
- // @param startDate string
- // @param endDate string
- // @return list []*EdbDataList
- // @return err error
- func getEdbDataListByMongo(source, subSource, edbInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
- list = make([]*EdbDataList, 0)
- mogDataObj := mgo.EdbDataBusiness{}
- // 构建查询条件
- queryConditions := bson.M{
- "edb_info_id": edbInfoId,
- }
- // 日期
- dateCondition, err := mgo.BuildDateCondition(startDate, endDate)
- if err != nil {
- return
- }
- if len(dateCondition) > 0 {
- queryConditions["data_time"] = dateCondition
- }
- // 获取列表数据
- tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, []string{"data_time"})
- if tmpErr != nil {
- err = tmpErr
- return
- }
- for k, v := range tmpDataList {
- list = append(list, &EdbDataList{
- EdbDataId: k + 1,
- EdbInfoId: v.EdbInfoId,
- DataTime: v.DataTime.Format(utils.FormatDate),
- DataTimestamp: v.DataTimestamp,
- Value: v.Value,
- })
- }
- return
- }
- type EdbDataItemList struct {
- EdbDataId int `json:"-" description:" 指标数据ID"`
- EdbInfoId int `json:"-" description:"指标ID"`
- DataTime string `description:"数据日期"`
- DataTimestamp int64 `description:"数据日期"`
- Value float64 `description:"数据值"`
- }
- type EdbDataItems struct {
- Items []*EdbDataList
- Year int
- BetweenDay int `json:"-" description:"公历与农历之间相差的天数"`
- CuttingDataTimestamp int64 `description:"切割的时间戳"`
- }
- type EdbDataResult struct {
- List []*EdbDataItems
- }
- type QuarterData struct {
- Year string
- DataList []*EdbDataList
- CuttingDataTimestamp int64 `description:"切割的时间戳"`
- ChartLegend string
- Years string
- }
- type QuarterXDateItem struct {
- StartDate time.Time
- EndDate time.Time
- ShowName string
- ChartLegend string
- CuttingDataTimestamp int64 `description:"切割的时间戳"`
- }
- type ChartInfoDetailResp struct {
- ChartInfo *ChartInfo
- EdbInfoList []*ChartEdbInfoMapping
- XEdbIdValue []int `description:"柱方图的x轴数据,指标id"`
- YDataList []YData `description:"柱方图的y轴数据"`
- XDataList []XData `description:"商品价格曲线的X轴数据"`
- CorrelationChartInfo *CorrelationInfo `description:"相关性图表信息"`
- DataResp interface{} `description:"图表数据,根据图的类型而定的,没有确定的数据格式"`
- WaterMark string `description:"水印"`
- IsCollect bool `description:"是否收藏"`
- }
- // XData 商品价格曲线的的x轴数据
- type XData struct {
- Name string `description:"别名"`
- NameEn string `description:"英文别名"`
- IsHide int `description:"是否隐藏,0不隐藏,1隐藏"`
- }
- // YData 柱方图的y轴数据
- type YData struct {
- Date string `description:"数据日期"`
- ConfigDate time.Time `description:"配置的日期" json:"-"`
- Color string `description:"数据颜色"`
- Name string `description:"别名"`
- NameEn string `description:"英文别名"`
- Value []float64 `description:"每个指标的值"`
- NoDataEdbList []int `description:"没有数据的指标列表"`
- XEdbInfoIdList []int `description:"对应X轴的指标id列表"`
- EdbValMap map[int]float64 `description:"指标与值的对应" json:"-"`
- M []int `description:"对应开始日期的间隔值" json:"-"`
- NameList []string `description:"每个值对应的名称"`
- EnNameList []string `description:"每个值对应的英文名称"`
- Unit string `description:"中文单位名称"`
- UnitEn string `description:"英文单位名称"`
- SeriesEdb struct {
- SeriesId int `description:"因子指标系列ID"`
- EdbInfoId int `description:"指标ID"`
- } `description:"对应的系列指标"`
- }
- // RollingCorrelationChartDataResp 滚动相关性图
- type RollingCorrelationChartDataResp struct {
- XDateTimeValue []string `description:"滚动相关性图的x轴数据,日期数据"`
- YDataList []YData `description:"滚动相关性图的y轴数据"`
- }
- // 指标季度数据计算(公历转农历)
- func AddCalculateQuarterV5(dataList []*EdbDataList) (result *EdbDataResult, err error) {
- var errMsg string
- defer func() {
- if errMsg != "" {
- fmt.Println("errMsg:", errMsg)
- }
- }()
- if len(dataList) <= 0 {
- result.List = make([]*EdbDataItems, 0)
- return
- }
- endDate := dataList[len(dataList)-1].DataTime
- endDateForm, err := time.Parse(utils.FormatDate, endDate)
- if err != nil {
- return result, err
- }
- thisMonth := int(endDateForm.Month())
- result = new(EdbDataResult)
- var yearArr []int
- yearMap := make(map[int]int)
- var cureentDate time.Time
- if thisMonth < 11 {
- for k, v := range dataList {
- dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
- if err != nil {
- errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
- return result, err
- }
- if k == len(dataList)-1 {
- cureentDate = dateTime
- }
- year := dateTime.Year()
- if _, ok := yearMap[year]; !ok {
- yearArr = append(yearArr, year)
- }
- yearMap[year] = year
- }
- } else {
- for k, v := range dataList {
- dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
- if err != nil {
- errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
- return result, err
- }
- if k == len(dataList)-1 {
- cureentDate = dateTime
- }
- year := dateTime.Year() + 1
- if _, ok := yearMap[year]; !ok {
- yearArr = append(yearArr, year)
- }
- yearMap[year] = year
- }
- }
- //排序
- fmt.Println("yearArr:", yearArr)
- thisYear := cureentDate.Year()
- //thisMonth := int(cureentDate.Month())
- fmt.Println("thisMonth:", thisMonth)
- for ky, vy := range yearArr {
- fmt.Println("line 432:", ky, vy, thisYear, thisMonth)
- if thisMonth < 11 {
- currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
- currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
- currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
- if err != nil {
- errMsg = "生成当前春节失败,Err:" + err.Error()
- return result, err
- }
- preYear := vy
- preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
- preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
- preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
- if err != nil {
- errMsg = "生成历史年份春节失败,Err:" + err.Error()
- return result, err
- }
- day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
- items := new(EdbDataItems)
- items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
- items.Year = preYear
- for _, v := range dataList {
- dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
- if err != nil {
- errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
- return result, err
- }
- newDate := dateTime.AddDate(0, 0, int(day))
- selectDateStr := strconv.Itoa(thisYear) + "-11" + "-30"
- selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
- if newDate.Before(selectDate) || newDate == selectDate {
- timestamp := newDate.UnixNano() / 1e6
- item := new(EdbDataList)
- item.DataTime = newDate.Format(utils.FormatDate)
- item.EdbInfoId = v.EdbInfoId
- item.Value = v.Value
- item.EdbDataId = v.EdbDataId
- item.DataTimestamp = timestamp
- items.Items = append(items.Items, item)
- }
- }
- result.List = append(result.List, items)
- } else {
- nextYear := thisYear + 1
- nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
- nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
- nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
- if err != nil {
- errMsg = "生成当前春节失败,Err:" + err.Error()
- return result, err
- }
- preYear := vy
- preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
- preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
- preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
- if err != nil {
- errMsg = "生成历史年份春节失败,Err:" + err.Error()
- return result, err
- }
- day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
- fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
- items := new(EdbDataItems)
- items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
- items.Year = preYear - 1
- fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
- if ky+1 < len(yearArr) {
- for _, v := range dataList {
- dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
- if err != nil {
- errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
- return result, err
- }
- newDate := dateTime.AddDate(0, 0, int(day))
- selectDateStr := strconv.Itoa(nextYear) + "-05" + "-31"
- selectDate, _ := time.Parse(utils.FormatDate, selectDateStr)
- if newDate.Before(selectDate) || newDate == selectDate {
- timestamp := newDate.UnixNano() / 1e6
- item := new(EdbDataList)
- item.DataTime = newDate.Format(utils.FormatDate)
- item.EdbInfoId = v.EdbInfoId
- item.Value = v.Value
- item.EdbDataId = v.EdbDataId
- item.DataTimestamp = timestamp
- items.Items = append(items.Items, item)
- }
- }
- result.List = append(result.List, items)
- } else {
- for _, v := range dataList {
- dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
- if err != nil {
- errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
- return result, err
- }
- timestamp := dateTime.UnixNano() / 1e6
- item := new(EdbDataList)
- item.DataTime = dateTime.Format(utils.FormatDate)
- item.EdbInfoId = v.EdbInfoId
- item.Value = v.Value
- item.EdbDataId = v.EdbDataId
- item.DataTimestamp = timestamp
- items.Items = append(items.Items, item)
- }
- result.List = append(result.List, items)
- }
- }
- }
- return
- }
- // AddCalculateQuarterV6 指标季度数据计算(季节性图表)
- func AddCalculateQuarterV6(dataList []*EdbDataList) (result *EdbDataResult, err error) {
- var errMsg string
- defer func() {
- if errMsg != "" {
- fmt.Println("errMsg:", errMsg)
- }
- }()
- endDate := dataList[len(dataList)-1].DataTime
- endDateForm, err := time.Parse(utils.FormatDate, endDate)
- if err != nil {
- return result, err
- }
- thisMonth := int(endDateForm.Month())
- result = new(EdbDataResult)
- var yearArr []int
- yearMap := make(map[int]int)
- var cureentDate time.Time
- if thisMonth < 11 {
- for k, v := range dataList {
- dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
- if err != nil {
- errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
- return result, err
- }
- if k == len(dataList)-1 {
- cureentDate = dateTime
- }
- year := dateTime.Year()
- if _, ok := yearMap[year]; !ok {
- yearArr = append(yearArr, year)
- }
- yearMap[year] = year
- }
- } else {
- for k, v := range dataList {
- dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
- if err != nil {
- errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
- return result, err
- }
- if k == len(dataList)-1 {
- cureentDate = dateTime
- }
- year := dateTime.Year() + 1
- if _, ok := yearMap[year]; !ok {
- yearArr = append(yearArr, year)
- }
- yearMap[year] = year
- }
- }
- //排序
- fmt.Println("yearArr:", yearArr)
- thisYear := cureentDate.Year()
- //thisMonth := int(cureentDate.Month())
- fmt.Println("thisMonth:", thisMonth)
- for ky, vy := range yearArr {
- fmt.Printf("line 432:ky:%d, vy:%d, thisYear:%d, thisMonth:%d", ky, vy, thisYear, thisMonth)
- fmt.Println("")
- if thisMonth < 11 {
- currentYearCjnl := strconv.Itoa(thisYear) + "-01-01" //当前年份春节农历
- currentYearCjgl := solarlunar.LunarToSolar(currentYearCjnl, false) //当前年份春节公历
- currentYearCjglDate, err := time.Parse(utils.FormatDate, currentYearCjgl)
- if err != nil {
- errMsg = "生成当前春节失败,Err:" + err.Error()
- return result, err
- }
- preYear := vy
- preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
- preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
- preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
- if err != nil {
- errMsg = "生成历史年份春节失败,Err:" + err.Error()
- return result, err
- }
- day := currentYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
- fmt.Println("day:", day, currentYearCjglDate, preYearCjglDate)
- items := new(EdbDataItems)
- items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
- items.Year = preYear
- for _, v := range dataList {
- dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
- if err != nil {
- errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
- return result, err
- }
- newDate := dateTime.AddDate(0, 0, int(day))
- timestamp := newDate.UnixNano() / 1e6
- item := new(EdbDataList)
- item.DataTime = newDate.Format(utils.FormatDate)
- item.EdbInfoId = v.EdbInfoId
- item.Value = v.Value
- item.EdbDataId = v.EdbDataId
- item.DataTimestamp = timestamp
- items.Items = append(items.Items, item)
- }
- result.List = append(result.List, items)
- } else {
- nextYear := thisYear + 1
- nextYearCjnl := strconv.Itoa(nextYear) + "-01-01" //当前年份春节农历
- nextYearCjgl := solarlunar.LunarToSolar(nextYearCjnl, false) //当前年份春节公历
- nextYearCjglDate, err := time.Parse(utils.FormatDate, nextYearCjgl)
- if err != nil {
- errMsg = "生成当前春节失败,Err:" + err.Error()
- return result, err
- }
- preYear := vy
- preYearCjnl := strconv.Itoa(preYear) + "-01-01" //之前年份春节农历
- preYearCjgl := solarlunar.LunarToSolar(preYearCjnl, false) //之前年份春节公历
- preYearCjglDate, err := time.Parse(utils.FormatDate, preYearCjgl)
- if err != nil {
- errMsg = "生成历史年份春节失败,Err:" + err.Error()
- return result, err
- }
- day := nextYearCjglDate.Sub(preYearCjglDate).Hours() / float64(24)
- fmt.Println("day:", day, nextYearCjglDate, preYearCjglDate)
- items := new(EdbDataItems)
- items.BetweenDay = int(day) //公历日期换算成农历,需要减除的天数
- items.Year = preYear
- fmt.Println("preYear:", preYear, "ky:", ky, "yearArrLen:", len(yearArr))
- //if ky+1 < len(yearArr) {
- for _, v := range dataList {
- dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
- if err != nil {
- errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
- return result, err
- }
- newDate := dateTime.AddDate(0, 0, int(day))
- timestamp := newDate.UnixNano() / 1e6
- item := new(EdbDataList)
- item.DataTime = newDate.Format(utils.FormatDate)
- item.EdbInfoId = v.EdbInfoId
- item.Value = v.Value
- item.EdbDataId = v.EdbDataId
- item.DataTimestamp = timestamp
- items.Items = append(items.Items, item)
- }
- result.List = append(result.List, items)
- /*} else {
- for _, v := range dataList {
- dateTime, err := time.Parse(utils.FormatDate, v.DataTime)
- if err != nil {
- errMsg = "time.Parse Err:" + err.Error() + ";DataTime:" + v.DataTime
- return result, err
- }
- timestamp := dateTime.UnixNano() / 1e6
- item := new(EdbDataList)
- item.DataTime = dateTime.Format(utils.FormatDate)
- item.EdbInfoId = v.EdbInfoId
- item.Value = v.Value
- item.EdbDataId = v.EdbDataId
- item.DataTimestamp = timestamp
- items.Items = append(items.Items, item)
- }
- result.List = append(result.List, items)
- }*/
- }
- }
- return
- }
- type CorrelationInfo struct {
- LeadValue int `description:"领先值"`
- LeadUnit string `description:"领先单位"`
- StartDate string `description:"开始日期"`
- EndDate string `description:"结束日期"`
- EdbInfoIdFirst int `description:"A指标ID"`
- EdbInfoIdSecond int `description:"B指标ID"`
- PeriodData string `description:"X轴-期数数据"`
- CorrelationData string `description:"Y轴-相关性系数"`
- AnalysisMode int `description:"分析模式: 0-单因子; 1-多因子"`
- }
- type SeasonExtraItem struct {
- ChartLegend []SeasonChartLegend `description:"自定义的图例名称"`
- XStartDate string `description:"横坐标显示的起始日"`
- XEndDate string `description:"横坐标显示的截止日"`
- JumpYear int `description:"横坐标日期是否跨年,1跨年,0不跨年"`
- RightAxis SeasonRightAxis `description:"自定义右轴指标"`
- MaxMinLimits MaxMinLimits `description:"自定义同期上下限"`
- SamePeriodAverage SamePeriodAverage `description:"自定义同期均线"`
- SamePeriodStandardDeviation SamePeriodStandardDeviation `description:"自定义同期标准差"`
- }
- // 自定义右轴指标
- type SeasonRightAxis struct {
- IndicatorType int `description:"右轴指标类型 1:左轴指标同比,2:指标库,3:预测指标 "`
- Style string `description:"生成样式"`
- Shape string `description:"形状"`
- ChartColor string `description:"图表颜色"`
- Size int `description:"大小"`
- Legend string `description:"图例名称"`
- NumFormat int `description:"数值格式 1:百分比 2:小数"`
- IsConnected int `description:"是否连接 0不连接 1连接"`
- LineColor string `description:"线条颜色"`
- LineWidth float64 `description:"线条宽度"`
- LineStyle string `description:"线条样式"`
- IsShow bool `description:"是否显示"`
- IsAdd bool `description:"是否添加"`
- }
- // 自定义同期上下限
- type MaxMinLimits struct {
- Color string `description:"颜色"`
- Year int `description:"上下限取值范围"`
- Legend string `description:"图例名称"`
- IsShow bool `description:"是否显示"`
- IsAdd bool `description:"是否添加"`
- }
- // 自定义同期均线
- type SamePeriodAverage struct {
- Color string `description:"颜色"`
- Year int `description:"均线取值范围"`
- Legend string `description:"图例名称"`
- LineType string `description:"线型"`
- LineWidth float64 `description:"线宽"`
- IsShow bool `description:"是否显示"`
- IsAdd bool `description:"是否添加"`
- }
- // 自定义同期均线
- type SamePeriodAverageResp struct {
- Color string `description:"颜色"`
- Year int `description:"均线取值范围"`
- Legend string `description:"图例名称"`
- LineType string `description:"线型"`
- LineWidth float64 `description:"线宽"`
- IsShow bool `description:"是否显示"`
- List []*SamePeriodAverageData `description:"自定义均线列表"`
- IsAdd bool `description:"是否添加"`
- }
- type SamePeriodAverageData struct {
- DataTime string `description:"数据日期"`
- DataTimestamp int64 `description:"数据日期时间戳"`
- Value float64 `description:"均值"`
- }
- // 自定义同期标准差
- type SamePeriodStandardDeviation struct {
- Color string `description:"颜色"`
- Year int `description:"标准差取值范围"`
- Legend string `description:"图例名称"`
- Multiple float64 `description:"标准差倍数"`
- IsShow bool `description:"是否显示"`
- IsAdd bool `description:"是否添加"`
- }
- type SamePeriodStandardDeviationResp struct {
- Color string `description:"颜色"`
- Year int `description:"标准差取值范围"`
- Legend string `description:"图例名称"`
- Multiple float64 `description:"标准差倍数"`
- IsShow bool `description:"是否显示"`
- List []*MaxMinLimitsData `description:"自定义标准差列表"`
- IsAdd bool `description:"是否添加"`
- }
- type SeasonChartResp struct {
- MaxMinLimits MaxMinLimitsResp `description:"自定义上下限"`
- SamePeriodAverage SamePeriodAverageResp `description:"自定义同期均线"`
- SamePeriodStandardDeviation SamePeriodStandardDeviationResp `description:"自定义同期标准差线"`
- RightAxis SeasonRightAxisResp `description:"自定义右轴指标"`
- }
- // 自定义右轴指标
- type SeasonRightAxisResp struct {
- SeasonRightAxis
- EdbInfoList []*ChartEdbInfoMapping
- }
- type MaxMinLimitsResp struct {
- List []*MaxMinLimitsData `description:"自定义上下限列表"`
- Color string `description:"颜色"`
- Year int `description:"上下限取值范围"`
- Legend string `description:"图例名称"`
- IsShow bool `description:"是否显示"`
- IsAdd bool `description:"是否添加"`
- }
- type MaxMinLimitsData struct {
- DataTime string `description:"数据日期"`
- DataTimestamp int64 `description:"数据日期时间戳"`
- MaxValue float64 `description:"最大值"`
- MinValue float64 `description:"最小值"`
- }
- type SeasonChartLegend struct {
- Name string
- Value string
- }
- type QuarterDataList []*QuarterData
- func (m QuarterDataList) Len() int {
- return len(m)
- }
- func (m QuarterDataList) Less(i, j int) bool {
- return m[i].Years < m[j].Years
- }
- func (m QuarterDataList) Swap(i, j int) {
- m[i], m[j] = m[j], m[i]
- }
- type ChartDwCollectReq struct {
- UniqueCode string
- Token string
- }
- // 截面组合图额外配置
- type ChartSectionExtraConf struct {
- DateConfList []*ChartSectionDateConfItem
- IsHeap int `description:"是否堆积(1.堆积,0不堆积)"`
- XDataList []XData `description:"横轴名称设置"`
- UnitList *ChartSectionCombineUnit `description:"纵轴单位设置"`
- BaseChartSeriesName string `description:"基准系列名称"`
- SortType int `description:"排序类型,0默认,1升序,2降序"`
- }
- // 截面组合图额外配置
- type ChartSectionAllExtraConf struct {
- ChartSectionExtraConf
- SeriesList []*ChartSectionSeriesItem
- }
- type ChartSectionDateConfItem struct {
- MoveForward int `description:"前移的期数"`
- EdbInfoId int `description:"指标ID"`
- EdbName string `description:"指标名称"`
- EdbNameEn string `description:"指标名称英文"`
- EdbInfoType int `description:"指标类型"`
- Frequency string `description:"频度"`
- EndDate string `description:"最新日期"`
- StaticDate string `description:"固定日期"`
- DateType int `description:"日期类型:0 指标日期,1系统日期, 2固定日期"`
- DateConfName string `description:"引用日期名称"` // 引用日期名称不能重复
- DateConfNameEn string `description:"引用日期英文名称"`
- DateChange []*ChartSectionDateChange
- }
- // 截面组合图引用日期配置
- type ChartSectionDateChange struct {
- Year int
- Month int
- Day int
- Frequency string `description:"频度变换"`
- FrequencyDay string `description:"频度的固定日期"`
- ChangeType int `description:"日期变换类型1日期位移,2指定频率"`
- }
- // 截面组合图系列配置
- type ChartSectionSeriesItem struct {
- ChartSeriesId int `description:"系列ID"`
- SeriesName string `description:"系列名称"` //系列名称不可同名
- SeriesNameEn string `description:"系列英文名称"`
- ChartStyle string `description:"图表类型"`
- ChartColor string `description:"颜色"`
- ChartWidth int `description:"线条大小"`
- IsPoint int `description:"是否用数据点展示(0 否,1是)"`
- IsNumber int `description:"是否用数值展示(0 否,1是)"`
- IsAxis int `description:"1:左轴,0:右轴"`
- MaxData float64 `description:"上限"`
- MinData float64 `description:"下限"`
- //IsOrder bool `description:"true:正序,false:逆序"`
- EdbInfoList []*ChartSectionSeriesEdbConf
- DataList []float64
- NoDataEdbIndex []int
- }
- type ChartSectionSeriesEdbConf struct {
- ChartSeriesEdbMappingId int `description:"映射ID"`
- ChartSeriesId int `description:"系列ID"`
- //ChartInfoId int `description:"图表ID"`
- EdbInfoId int `description:"指标id"`
- DateConf *ChartSectionSeriesDateConfItem
- EdbName string `description:"中文别名"`
- EdbNameEn string `description:"英文别名"`
- EdbInfoType int `description:"指标类型"`
- Unit string `description:"单位"`
- UnitEn string `description:"英文单位"`
- DateConfName string `description:"引用日期名称"`
- DateConfType int `description:"日期类型,0指标最新日期, 1引用日期"`
- }
- type ChartSectionCombineDataResp struct {
- DateConfList []*ChartSectionDateConfItem
- IsHeap int `description:"是否堆积(1.堆积,0不堆积)"`
- XDataList []XData `description:"横轴名称设置"`
- UnitList *ChartSectionCombineUnit `description:"纵轴单位设置"`
- BaseChartSeriesName string `description:"基准系列名称"`
- SortType int `description:"排序类型,0默认,1升序,2降序"`
- SeriesList []*ChartSectionSeriesItem
- LeftMin string `description:"图表左侧最小值"`
- LeftMax string `description:"图表左侧最大值"`
- RightMin string `description:"图表右侧最小值"`
- RightMax string `description:"图表右侧最大值"`
- Right2Min string `description:"图表右侧最小值"`
- Right2Max string `description:"图表右侧最大值"`
- }
- // 系列里的指标日期配置
- type ChartSectionSeriesDateConfItem struct {
- MoveForward int `description:"前移的期数"`
- DateChange []*ChartSectionDateChange
- }
- // PreviewSectionCombineChartReq 预览截面组合图的请求
- type PreviewSectionCombineChartReq struct {
- ChartName string `description:"图表名称"`
- ChartClassifyId int `description:"分类id"`
- ExtraConfig string `description:"图表额外配置信息,json字符串"`
- }
- type ChartSectionCombineUnit struct {
- LeftName string `description:"左轴单位"`
- LeftNameEn string `description:"左轴英文单位"`
- RightName string `description:"右轴单位"`
- RightNameEn string `description:"右轴英文单位"`
- RightTwoName string `description:"右2轴单位"`
- RightTwoNameEn string `description:"右2轴英文单位"`
- }
- // 时序组合图额外配置
- type ChartTimeCombineExtraConf struct {
- IsHeap int `description:"是否堆积(1.堆积,0不堆积)"`
- }
- type ChartTimeCombineDataResp struct {
- IsHeap int `description:"是否堆积(1.堆积,0不堆积)"`
- }
- func GetChartInfoById(chartInfoId int) (item *ChartInfo, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM chart_info WHERE chart_info_id=? `
- err = o.Raw(sql, chartInfoId).QueryRow(&item)
- return
- }
- type MarkersLine struct {
- Axis int `json:"axis" description:"1左轴 2右轴 3横轴"`
- AxisName string `json:"axisName" description:"轴的名称,例如'左轴'"`
- MarkerType string `json:"markerType" description:"标识线或标识区"`
- MarkLineType int `json:"markLineType" description:"1:固定 2:指标计算"`
- Value string `json:"value" description:"连线指向的数值,例如'4000'"`
- FromValue string `json:"fromValue" description:"连线的起始点,可以为空"`
- ToValue string `json:"toValue" description:"连线的结束点,可以为空"`
- LineWidth float64 `json:"lineWidth" description:"连线的宽度"`
- DashStyle string `json:"dashStyle" description:"连线的虚线样式,例如'ShortDashDot'"`
- Color string `json:"color" description:"连线的颜色"`
- Text string `json:"text" description:"连线旁边显示的文本"`
- TextPosition string `json:"textPosition" description:"文本的显示位置,例如'bottom'"`
- TextColor string `json:"textColor" description:"文本颜色"`
- TextFontSize int `json:"textFontSize" description:"文本的字号大小"`
- IsShow bool `json:"isShow" description:"是否显示连线及文本"`
- EdbType int `json:"edbType" description:"指标类型 0图中第一个指标 1其他指标 前端回显用"`
- EdbInfoId int `json:"edbInfoId" description:"指标id"`
- Calculation int `json:"calculation" description:"计算方式 1区间均值 2区间均值加N倍标准差 3区间个数分位 4区间数值分位"`
- CalculationValue int `json:"calculationValue" description:"计算方式对应的值 2就是几倍标准差 3就是分位值 4就是数值值·"`
- TimeIntervalType int `json:"timeInterval" description:"时间区间 0跟随图表 1自定义"`
- StartDate MarkersLineTime `json:"startTime" description:"开始时间"`
- EndDate MarkersLineTime `json:"endTime" description:"结束时间"`
- }
- type MarkersLineTime struct {
- TimeType int `json:"timeType" description:"时间类型 1固定 2动态 3至今(仅结束时间)"`
- Date string `json:"date" description:"日期"`
- Conf EdbDateChangeConf `json:"conf" description:"动态时间配置"`
- }
- // EdbDateExtraConf
- // @Description: 导入指标日期前移和日期变换
- type EdbDateChangeConf struct {
- MoveForward int `json:"moveForward" description:"前移的期数"`
- BaseDate int `json:"baseDate" description:"基准日期 0系统日期 1指标最新日期"`
- DateChange []*EdbDateConfDateChange
- }
- type EdbDateConfDateChange struct {
- Year int `json:"year" description:"前移的期数"`
- Month int `json:"month" description:"前移的期数"`
- Day int `json:"day" description:"前移的期数"`
- Frequency string `json:"moveForward" description:"频度变换"`
- FrequencyDay string `json:"frequencyDay" description:"频度的固定日期"`
- ChangeType int `json:"changeType" description:"日期变换类型1日期位移,2指定频率"`
- }
- func getThsHfEdbDataListByMongo(source, subSource, edbInfoId int, startDate, endDate string) (list []*EdbDataList, err error) {
- list = make([]*EdbDataList, 0)
- mogDataObj := mgo.EdbDataThsHf{}
- // 构建查询条件
- queryConditions := bson.M{
- "edb_info_id": edbInfoId,
- }
- // 数据日期
- dateCondition, err := mgo.BuildDateCondition(startDate, endDate)
- if err != nil {
- return
- }
- if len(dateCondition) > 0 {
- queryConditions["data_time"] = dateCondition
- }
- // 获取列表数据
- tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, []string{"data_time"})
- if tmpErr != nil {
- err = tmpErr
- return
- }
- for k, v := range tmpDataList {
- list = append(list, &EdbDataList{
- EdbDataId: k + 1,
- EdbInfoId: v.EdbInfoId,
- DataTime: v.DataTime.Format(utils.FormatDate),
- DataTimestamp: v.DataTimestamp,
- Value: v.Value,
- })
- }
- return
- }
|