|
@@ -0,0 +1,549 @@
|
|
|
+package models
|
|
|
+
|
|
|
+import (
|
|
|
+ "encoding/json"
|
|
|
+ "errors"
|
|
|
+ "fmt"
|
|
|
+ "github.com/beego/beego/v2/client/orm"
|
|
|
+ "github.com/shopspring/decimal"
|
|
|
+ "hongze/hongze_edb_lib/utils"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+// AddCalculateCorrelation 扩散指数
|
|
|
+func AddCalculateCorrelation(req *EdbInfoCalculateBatchSaveReq, edbCode, uniqueCode string, sysUserId int, sysUserRealName string) (edbInfo *EdbInfo, err error) {
|
|
|
+ o := orm.NewOrm()
|
|
|
+ to, err := o.Begin()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("AddCalculateCorrelation,Err:" + err.Error())
|
|
|
+ _ = to.Rollback()
|
|
|
+ } else {
|
|
|
+ _ = to.Commit()
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ if req.EdbInfoId > 0 {
|
|
|
+ err = errors.New("无法新增")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ edbInfo = new(EdbInfo)
|
|
|
+ edbInfo.Source = utils.DATA_SOURCE_CALCULATE_CORRELATION
|
|
|
+ edbInfo.SourceName = utils.DATA_SOURCE_NAME_CALCULATE_CORRELATION
|
|
|
+ edbInfo.EdbCode = edbCode
|
|
|
+ edbInfo.EdbName = req.EdbName
|
|
|
+ edbInfo.EdbNameSource = req.EdbName
|
|
|
+ edbInfo.Frequency = req.Frequency
|
|
|
+ edbInfo.Unit = req.Unit
|
|
|
+ edbInfo.ClassifyId = req.ClassifyId
|
|
|
+ edbInfo.SysUserId = sysUserId
|
|
|
+ edbInfo.SysUserRealName = sysUserRealName
|
|
|
+ edbInfo.CreateTime = time.Now()
|
|
|
+ edbInfo.ModifyTime = time.Now()
|
|
|
+ edbInfo.UniqueCode = uniqueCode
|
|
|
+ edbInfo.CalculateFormula = req.Formula
|
|
|
+ edbInfo.EdbType = 2
|
|
|
+ newEdbInfoId, tmpErr := to.Insert(edbInfo)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ edbInfo.EdbInfoId = int(newEdbInfoId)
|
|
|
+
|
|
|
+ //关联关系
|
|
|
+ tagMap := make(map[string]int)
|
|
|
+ relationEdbInfoList := make([]*EdbInfo, 0)
|
|
|
+ calculateMappingItemList := make([]*EdbInfoCalculateMapping, 0)
|
|
|
+ for _, v := range req.EdbInfoIdArr {
|
|
|
+ tmpEdbInfo, tmpErr := GetEdbInfoById(v.EdbInfoId)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ relationEdbInfoList = append(relationEdbInfoList, tmpEdbInfo)
|
|
|
+
|
|
|
+ calculateMappingItem := new(EdbInfoCalculateMapping)
|
|
|
+ calculateMappingItem.CreateTime = time.Now()
|
|
|
+ calculateMappingItem.ModifyTime = time.Now()
|
|
|
+ calculateMappingItem.Sort = 1
|
|
|
+ calculateMappingItem.EdbCode = edbCode
|
|
|
+ calculateMappingItem.EdbInfoId = edbInfo.EdbInfoId
|
|
|
+ calculateMappingItem.FromEdbInfoId = tmpEdbInfo.EdbInfoId
|
|
|
+ calculateMappingItem.FromEdbCode = tmpEdbInfo.EdbCode
|
|
|
+ calculateMappingItem.FromEdbName = tmpEdbInfo.EdbName
|
|
|
+ calculateMappingItem.FromSource = tmpEdbInfo.Source
|
|
|
+ calculateMappingItem.FromSourceName = tmpEdbInfo.SourceName
|
|
|
+ calculateMappingItem.FromTag = v.FromTag
|
|
|
+ calculateMappingItem.Source = edbInfo.Source
|
|
|
+ calculateMappingItem.SourceName = edbInfo.SourceName
|
|
|
+ calculateMappingItemList = append(calculateMappingItemList, calculateMappingItem)
|
|
|
+
|
|
|
+ tagMap[v.FromTag] = v.EdbInfoId
|
|
|
+ }
|
|
|
+ _, err = to.InsertMulti(len(calculateMappingItemList), calculateMappingItemList)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ //计算数据
|
|
|
+ err = refreshAllCalculateCorrelation(to, edbInfo.EdbInfoId, edbInfo.Source, relationEdbInfoList, edbInfo.EdbCode, edbInfo.CalculateFormula, tagMap)
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// EditCalculateCorrelation 修改扩散指数数据
|
|
|
+func EditCalculateCorrelation(edbInfo *EdbInfo, req *EdbInfoCalculateBatchEditReq) (err error) {
|
|
|
+ o := orm.NewOrm()
|
|
|
+ to, err := o.Begin()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("EditCalculateCorrelation,Err:" + err.Error())
|
|
|
+ _ = to.Rollback()
|
|
|
+ } else {
|
|
|
+ _ = to.Commit()
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ //修改指标信息
|
|
|
+ edbInfo.EdbName = req.EdbName
|
|
|
+ edbInfo.EdbNameSource = req.EdbName
|
|
|
+ edbInfo.Frequency = req.Frequency
|
|
|
+ edbInfo.Unit = req.Unit
|
|
|
+ edbInfo.ClassifyId = req.ClassifyId
|
|
|
+ edbInfo.CalculateFormula = req.Formula
|
|
|
+ edbInfo.ModifyTime = time.Now()
|
|
|
+ _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "ModifyTime")
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ //删除,计算指标关联的,基础指标的关联关系
|
|
|
+ sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? `
|
|
|
+ _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
|
|
|
+ if err != nil {
|
|
|
+ err = errors.New("删除计算指标关联关系失败,Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //清空原有数据
|
|
|
+ tableName := GetEdbDataTableName(edbInfo.Source)
|
|
|
+ sql = ` DELETE FROM ` + tableName + ` WHERE edb_info_id = ? `
|
|
|
+ _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ //关联关系
|
|
|
+ tagMap := make(map[string]int)
|
|
|
+ relationEdbInfoList := make([]*EdbInfo, 0)
|
|
|
+ calculateMappingItemList := make([]*EdbInfoCalculateMapping, 0)
|
|
|
+ for _, v := range req.EdbInfoIdArr {
|
|
|
+ tmpEdbInfo, tmpErr := GetEdbInfoById(v.EdbInfoId)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ relationEdbInfoList = append(relationEdbInfoList, tmpEdbInfo)
|
|
|
+
|
|
|
+ calculateMappingItem := new(EdbInfoCalculateMapping)
|
|
|
+ calculateMappingItem.CreateTime = time.Now()
|
|
|
+ calculateMappingItem.ModifyTime = time.Now()
|
|
|
+ calculateMappingItem.Sort = 1
|
|
|
+ calculateMappingItem.EdbCode = edbInfo.EdbCode
|
|
|
+ calculateMappingItem.EdbInfoId = edbInfo.EdbInfoId
|
|
|
+ calculateMappingItem.FromEdbInfoId = tmpEdbInfo.EdbInfoId
|
|
|
+ calculateMappingItem.FromEdbCode = tmpEdbInfo.EdbCode
|
|
|
+ calculateMappingItem.FromEdbName = tmpEdbInfo.EdbName
|
|
|
+ calculateMappingItem.FromSource = tmpEdbInfo.Source
|
|
|
+ calculateMappingItem.FromSourceName = tmpEdbInfo.SourceName
|
|
|
+ calculateMappingItem.FromTag = v.FromTag
|
|
|
+ calculateMappingItem.Source = edbInfo.Source
|
|
|
+ calculateMappingItem.SourceName = edbInfo.SourceName
|
|
|
+ calculateMappingItemList = append(calculateMappingItemList, calculateMappingItem)
|
|
|
+
|
|
|
+ tagMap[v.FromTag] = v.EdbInfoId
|
|
|
+ }
|
|
|
+ _, err = to.InsertMulti(len(calculateMappingItemList), calculateMappingItemList)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ //计算数据
|
|
|
+ err = refreshAllCalculateCorrelation(to, edbInfo.EdbInfoId, edbInfo.Source, relationEdbInfoList, edbInfo.EdbCode, edbInfo.CalculateFormula, tagMap)
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func RefreshAllCalculateCorrelation(edbInfo *EdbInfo) (err error) {
|
|
|
+ edbInfoCalculateDetailList, err := GetEdbInfoCalculateDetailList(edbInfo.EdbInfoId)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ tagMap := make(map[string]int)
|
|
|
+ relationEdbInfoList := make([]*EdbInfo, 0)
|
|
|
+ for _, v := range edbInfoCalculateDetailList {
|
|
|
+ tagMap[v.FromTag] = v.FromEdbInfoId
|
|
|
+ fromEdbInfo, _ := GetEdbInfoById(v.FromEdbInfoId)
|
|
|
+ relationEdbInfoList = append(relationEdbInfoList, fromEdbInfo)
|
|
|
+ }
|
|
|
+ o := orm.NewOrm()
|
|
|
+ to, err := o.Begin()
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("RefreshAllCalculateCorrelation,Err:" + err.Error())
|
|
|
+ _ = to.Rollback()
|
|
|
+ } else {
|
|
|
+ _ = to.Commit()
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ // 计算数据
|
|
|
+ err = refreshAllCalculateCorrelation(to, edbInfo.EdbInfoId, edbInfo.Source, relationEdbInfoList, edbInfo.EdbCode, edbInfo.CalculateFormula, tagMap)
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// CorrelationConfig 扩散指数配置
|
|
|
+type CorrelationConfig struct {
|
|
|
+ DateType int `description:"扩散指标日期;1:全部指标日期并集;2:部分指标日期并集"`
|
|
|
+ CheckList []string `description:"选中的数据,A,B,C"`
|
|
|
+}
|
|
|
+
|
|
|
+// refreshAllCalculateCorrelation 刷新扩散指数数据
|
|
|
+func refreshAllCalculateCorrelation(to orm.TxOrmer, edbInfoId, source int, relationEdbInfoList []*EdbInfo, edbCode, calculateFormula string, tagMap map[string]int) (err error) {
|
|
|
+ edbInfoIdStr := strconv.Itoa(edbInfoId)
|
|
|
+ tableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE_CORRELATION)
|
|
|
+
|
|
|
+ // 获取扩散指标关联的指标id
|
|
|
+ checkEdbInfoIdMap := make(map[int]int)
|
|
|
+ {
|
|
|
+ var config CorrelationConfig
|
|
|
+ err = json.Unmarshal([]byte(calculateFormula), &config)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if config.DateType == 1 {
|
|
|
+ for _, tmpEdbInfoId := range tagMap {
|
|
|
+ checkEdbInfoIdMap[tmpEdbInfoId] = tmpEdbInfoId
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ for _, v := range config.CheckList {
|
|
|
+ if tmpEdbInfoId, ok := tagMap[v]; ok {
|
|
|
+ checkEdbInfoIdMap[tmpEdbInfoId] = tmpEdbInfoId
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取扩散指数指标所有数据
|
|
|
+ existDataList, err := GetAllEdbDataListByTo(to, edbInfoId, source)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //计算指标的map
|
|
|
+ existDataMap := make(map[string]*EdbData, 0)
|
|
|
+ removeDateMap := make(map[string]string)
|
|
|
+ for _, v := range existDataList {
|
|
|
+ existDataMap[v.DataTime] = v
|
|
|
+ removeDateMap[v.DataTime] = ``
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取来源指标的数据
|
|
|
+ relationEdbDataMap := make(map[int]map[string]float64)
|
|
|
+ // 获取选择指标的 需要数据的 开始日期和结束日期
|
|
|
+ var startDate, endDate time.Time
|
|
|
+ for _, v := range relationEdbInfoList {
|
|
|
+ var condition string
|
|
|
+ var pars []interface{}
|
|
|
+ condition += " AND edb_info_id=? "
|
|
|
+ pars = append(pars, v.EdbInfoId)
|
|
|
+ tmpDataList, tmpErr := GetEdbDataListAllByTo(to, condition, pars, v.Source, 1)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if tmpDataList != nil {
|
|
|
+ if _, ok2 := checkEdbInfoIdMap[v.EdbInfoId]; ok2 {
|
|
|
+ lenTmpDataList := len(tmpDataList)
|
|
|
+ if lenTmpDataList > 0 {
|
|
|
+ tmpStartTime, _ := time.ParseInLocation(utils.FormatDate, tmpDataList[0].DataTime, time.Local)
|
|
|
+ tmpEndTime, _ := time.ParseInLocation(utils.FormatDate, tmpDataList[lenTmpDataList-1].DataTime, time.Local)
|
|
|
+
|
|
|
+ if startDate.IsZero() || tmpStartTime.Before(startDate) {
|
|
|
+ startDate = tmpStartTime
|
|
|
+ }
|
|
|
+
|
|
|
+ if tmpEndTime.IsZero() || tmpEndTime.After(endDate) {
|
|
|
+ endDate = tmpEndTime
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 用上期的数据补充当期的数据处理
|
|
|
+ handleDataMap := make(map[string]float64)
|
|
|
+ err = HandleDataByPreviousData(tmpDataList, handleDataMap)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ relationEdbDataMap[v.EdbInfoId] = handleDataMap
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ addSql := ` INSERT INTO ` + tableName + ` (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
|
|
|
+ var isAdd bool
|
|
|
+
|
|
|
+ for currDate := startDate.AddDate(0, 0, 1); !currDate.After(endDate); currDate = currDate.AddDate(0, 0, 1) {
|
|
|
+ currDateStr := currDate.Format(utils.FormatDate)
|
|
|
+
|
|
|
+ //环差指数列表
|
|
|
+ tmpValList := make([]float64, 0)
|
|
|
+ for _, dataMap := range relationEdbDataMap {
|
|
|
+ currVal, ok := dataMap[currDateStr]
|
|
|
+ if !ok {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ perVal, ok := dataMap[currDate.AddDate(0, 0, -1).Format(utils.FormatDate)]
|
|
|
+ if !ok {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ var tmpVal float64
|
|
|
+ if currVal > perVal {
|
|
|
+ tmpVal = 1
|
|
|
+ } else if currVal == perVal {
|
|
|
+ tmpVal = 0.5
|
|
|
+ } else {
|
|
|
+ tmpVal = 0
|
|
|
+ }
|
|
|
+ tmpValList = append(tmpValList, tmpVal)
|
|
|
+ }
|
|
|
+
|
|
|
+ lenTmpValList := len(tmpValList)
|
|
|
+ if lenTmpValList <= 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ currValDeci := decimal.NewFromFloat(0)
|
|
|
+ for _, tmpVal := range tmpValList {
|
|
|
+ currValDeci = currValDeci.Add(decimal.NewFromFloat(tmpVal))
|
|
|
+ }
|
|
|
+ currVal, _ := currValDeci.Div(decimal.NewFromInt(int64(lenTmpValList))).Round(4).Float64()
|
|
|
+
|
|
|
+ // 判断扩散指数指标是否存在数据
|
|
|
+ if existData, ok := existDataMap[currDateStr]; ok {
|
|
|
+ // 处理扩散指数数据的值
|
|
|
+ existValStr := existData.Value
|
|
|
+ existValDeci, tmpErr := decimal.NewFromString(existValStr)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ existVal, _ := existValDeci.Round(4).Float64()
|
|
|
+ // 判断扩散指数数据的值 与 当前计算出来的结果, 如果两个数据结果不相等的话,那么就修改咯
|
|
|
+ if existVal != currVal {
|
|
|
+ err = ModifyEdbDataById(source, existData.EdbDataId, fmt.Sprint(currVal))
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 直接入库
|
|
|
+ timestamp := currDate.UnixNano() / 1e6
|
|
|
+ timestampStr := fmt.Sprintf("%d", timestamp)
|
|
|
+ addSql += GetAddSql(edbInfoIdStr, edbCode, currDateStr, timestampStr, fmt.Sprint(currVal))
|
|
|
+ isAdd = true
|
|
|
+ }
|
|
|
+
|
|
|
+ delete(removeDateMap, currDateStr)
|
|
|
+ }
|
|
|
+
|
|
|
+ if isAdd {
|
|
|
+ addSql = strings.TrimRight(addSql, ",")
|
|
|
+ _, err = to.Raw(addSql).Exec()
|
|
|
+ }
|
|
|
+
|
|
|
+ // 移除不存在的日期数据
|
|
|
+ if len(removeDateMap) > 0 {
|
|
|
+ removeDateList := make([]string, 0) //需要移除的日期
|
|
|
+ for k := range removeDateMap {
|
|
|
+ removeDateList = append(removeDateList, k)
|
|
|
+ }
|
|
|
+ removeDateStr := strings.Join(removeDateList, `","`)
|
|
|
+ removeDateStr = `"` + removeDateStr + `"`
|
|
|
+ sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
|
|
|
+ _, err = to.Raw(sql, edbInfoId).Exec()
|
|
|
+ if err != nil {
|
|
|
+ err = fmt.Errorf("删除扩散指数指标数据失败,Err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// GetRollingCorrelationChartDataByEdbInfo 滚动相关性计算
|
|
|
+func GetRollingCorrelationChartDataByEdbInfo(edbInfoMappingA, edbInfoMappingB *EdbInfo, leadValue int, leadUnit string, calculateValue int, calculateUnit string, startDate, endDate string) (dateData map[string]float64, err error) {
|
|
|
+ yData := make([]float64, 0)
|
|
|
+
|
|
|
+ baseEdbInfo := edbInfoMappingA
|
|
|
+ changeEdbInfo := edbInfoMappingB
|
|
|
+
|
|
|
+ // 获取时间基准指标在时间区间内的值
|
|
|
+ aDataList := make([]*EdbInfoSearchData, 0)
|
|
|
+ switch baseEdbInfo.EdbInfoType {
|
|
|
+ case 0:
|
|
|
+ var condition string
|
|
|
+ var pars []interface{}
|
|
|
+ condition += " AND edb_info_id=? "
|
|
|
+ pars = append(pars, baseEdbInfo.EdbInfoId)
|
|
|
+
|
|
|
+ //获取来源指标的数据
|
|
|
+ aDataList, err = GetEdbDataListAll(condition, pars, baseEdbInfo.Source, 1)
|
|
|
+ case 1:
|
|
|
+ aDataList, err = GetPredictEdbDataListAllByStartDate(baseEdbInfo, 1, "")
|
|
|
+ default:
|
|
|
+ err = errors.New(fmt.Sprint("获取失败,指标base类型异常", baseEdbInfo.EdbInfoType))
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取变频指标所有日期的值, 插值法完善数据
|
|
|
+ bDataList := make([]*EdbInfoSearchData, 0)
|
|
|
+ switch changeEdbInfo.EdbInfoType {
|
|
|
+ case 0:
|
|
|
+ var condition string
|
|
|
+ var pars []interface{}
|
|
|
+ condition += " AND edb_info_id=? "
|
|
|
+ pars = append(pars, changeEdbInfo.EdbInfoId)
|
|
|
+
|
|
|
+ //获取来源指标的数据
|
|
|
+ bDataList, err = GetEdbDataListAll(condition, pars, changeEdbInfo.Source, 1)
|
|
|
+ case 1:
|
|
|
+ bDataList, err = GetPredictEdbDataListAllByStartDate(changeEdbInfo, 1, "")
|
|
|
+ default:
|
|
|
+ err = errors.New(fmt.Sprint("获取失败,指标change类型异常", baseEdbInfo.EdbInfoType))
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 数据平移变频指标领先/滞后的日期(单位天)
|
|
|
+ frequencyDaysMap := map[string]int{
|
|
|
+ "天": 1, "周": 7, "月": 30, "季": 90, "年": 365,
|
|
|
+ }
|
|
|
+ // 2023-03-17 时间序列始终以指标A为基准, 始终是B进行平移
|
|
|
+ //baseDataList := make([]*data_manage.EdbDataList, 0)
|
|
|
+ baseDataMap := make(map[string]float64)
|
|
|
+ changeDataList := make([]*EdbInfoSearchData, 0)
|
|
|
+ changeDataMap := make(map[string]float64)
|
|
|
+
|
|
|
+ // A指标不管三七二十一,先变个频再说
|
|
|
+ {
|
|
|
+ _, e := HandleDataByLinearRegression(aDataList, baseDataMap)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //baseDataList = tmpNewChangeDataList
|
|
|
+ }
|
|
|
+ // A指标不管三七二十一,先变个频再说
|
|
|
+ {
|
|
|
+ tmpNewChangeDataList, e := HandleDataByLinearRegression(bDataList, changeDataMap)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ changeDataList = tmpNewChangeDataList
|
|
|
+
|
|
|
+ // 平移下日期
|
|
|
+ moveUnitDays := frequencyDaysMap[leadUnit]
|
|
|
+ _, changeDataMap = MoveDataDaysToNewDataList(changeDataList, leadValue*moveUnitDays)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 计算计算时,需要多少个日期内数据
|
|
|
+ calculateDay := frequencyDaysMap[calculateUnit] * calculateValue
|
|
|
+
|
|
|
+ // 计算 每个日期的相关性值
|
|
|
+ {
|
|
|
+ startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
|
|
|
+ endDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
|
|
|
+
|
|
|
+ for currDay := startDateTime; !currDay.After(endDateTime); currDay = currDay.AddDate(0, 0, 1) {
|
|
|
+ coordinateData := make([]utils.Coordinate, 0)
|
|
|
+ // 取出对应的基准日期的值
|
|
|
+ for i := 0; i < calculateDay; i++ {
|
|
|
+ iDay := currDay.AddDate(0, 0, i).Format(utils.FormatDate)
|
|
|
+ tmpCoordinate := utils.Coordinate{
|
|
|
+ X: baseDataMap[iDay],
|
|
|
+ Y: changeDataMap[iDay],
|
|
|
+ }
|
|
|
+ coordinateData = append(coordinateData, tmpCoordinate)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 公式计算出领先/滞后频度对应点的相关性系数
|
|
|
+ var ratio float64
|
|
|
+ if len(coordinateData) > 0 {
|
|
|
+ ratio = utils.ComputeCorrelation(coordinateData)
|
|
|
+ }
|
|
|
+ yData = append(yData, ratio)
|
|
|
+ dateData[currDay.AddDate(0, 0, calculateDay).Format(utils.FormatDate)] = ratio
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// MoveDataDaysToNewDataList 平移指标数据生成新的数据序列
|
|
|
+func MoveDataDaysToNewDataList(dataList []*EdbInfoSearchData, moveDay int) (newDataList []EdbInfoSearchData, dateDataMap map[string]float64) {
|
|
|
+ dateMap := make(map[time.Time]float64)
|
|
|
+ var minDate, maxDate time.Time
|
|
|
+ dateDataMap = make(map[string]float64)
|
|
|
+
|
|
|
+ for _, v := range dataList {
|
|
|
+ currDate, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
|
|
|
+ if minDate.IsZero() || currDate.Before(minDate) {
|
|
|
+ minDate = currDate
|
|
|
+ }
|
|
|
+ if maxDate.IsZero() || currDate.After(maxDate) {
|
|
|
+ maxDate = currDate
|
|
|
+ }
|
|
|
+ dateMap[currDate] = v.Value
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理领先、滞后数据
|
|
|
+ newDateMap := make(map[time.Time]float64)
|
|
|
+ for currDate, value := range dateMap {
|
|
|
+ newDate := currDate.AddDate(0, 0, moveDay)
|
|
|
+ newDateMap[newDate] = value
|
|
|
+ }
|
|
|
+ minDate = minDate.AddDate(0, 0, moveDay)
|
|
|
+ maxDate = maxDate.AddDate(0, 0, moveDay)
|
|
|
+
|
|
|
+ // 获取日期相差日
|
|
|
+ dayNum := utils.GetTimeSubDay(minDate, maxDate)
|
|
|
+
|
|
|
+ for i := 0; i <= dayNum; i++ {
|
|
|
+ currDate := minDate.AddDate(0, 0, i)
|
|
|
+ tmpValue, ok := newDateMap[currDate]
|
|
|
+ if !ok {
|
|
|
+ //找不到数据,那么就用前面的数据吧
|
|
|
+ if len(newDataList)-1 < 0 {
|
|
|
+ tmpValue = 0
|
|
|
+ } else {
|
|
|
+ tmpValue = newDataList[len(newDataList)-1].Value
|
|
|
+ }
|
|
|
+ }
|
|
|
+ tmpData := EdbInfoSearchData{
|
|
|
+ DataTime: currDate.Format(utils.FormatDate),
|
|
|
+ Value: tmpValue,
|
|
|
+ }
|
|
|
+ dateDataMap[tmpData.DataTime] = tmpData.Value
|
|
|
+ newDataList = append(newDataList, tmpData)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|