package models import ( "errors" "eta/eta_index_lib/utils" "fmt" "github.com/beego/beego/v2/client/orm" "github.com/shopspring/decimal" "strconv" "strings" "time" ) // SavePredictCalculateJp 预测降频值 func SavePredictCalculateJp(reqEdbInfoId, classifyId int, edbName, frequency, unit, formula string, fromEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName, lang string) (edbInfo *EdbInfo, latestDateStr string, latestValue float64, err error, errMsg string) { o := orm.NewOrm() to, err := o.Begin() if err != nil { return } defer func() { if err != nil { fmt.Println("SavePredictCalculateJp,Err:" + err.Error()) _ = to.Rollback() } else { _ = to.Commit() } }() fmt.Println("reqEdbInfoId:", reqEdbInfoId) if reqEdbInfoId <= 0 { edbInfo = &EdbInfo{ //EdbInfoId: 0, EdbInfoType: 1, SourceName: utils.DATA_SOURCE_NAME_PREDICT_CALCULATE_JP, Source: utils.DATA_SOURCE_PREDICT_CALCULATE_JP, EdbCode: edbCode, EdbName: edbName, EdbNameSource: edbName, Frequency: frequency, Unit: unit, //StartDate: "", //EndDate: "", ClassifyId: classifyId, SysUserId: sysUserId, SysUserRealName: sysUserRealName, UniqueCode: uniqueCode, CreateTime: time.Now(), ModifyTime: time.Now(), MinValue: 0, MaxValue: 0, CalculateFormula: formula, EdbType: 2, Sort: GetAddEdbMaxSortByClassifyId(classifyId, utils.PREDICT_EDB_INFO_TYPE), MoveType: 0, MoveFrequency: "", NoUpdate: 0, ServerUrl: "", EdbNameEn: edbName, UnitEn: unit, LatestDate: "", LatestValue: 0, ChartImage: "", } newEdbInfoId, tmpErr := to.Insert(edbInfo) if tmpErr != nil { err = tmpErr return } edbInfo.EdbInfoId = int(newEdbInfoId) // 添加关联关系 { calculateMappingItem := &EdbInfoCalculateMapping{ EdbInfoCalculateMappingId: 0, EdbInfoId: edbInfo.EdbInfoId, Source: edbInfo.Source, SourceName: edbInfo.SourceName, EdbCode: edbCode, FromEdbInfoId: fromEdbInfo.EdbInfoId, FromEdbCode: fromEdbInfo.EdbCode, FromEdbName: fromEdbInfo.EdbName, FromSource: fromEdbInfo.Source, FromSourceName: fromEdbInfo.SourceName, FromTag: "", Sort: 1, CreateTime: time.Now(), ModifyTime: time.Now(), } _, err = to.Insert(calculateMappingItem) if err != nil { return } } } else { edbInfo, err = GetEdbInfoById(reqEdbInfoId) if err != nil { if err.Error() == utils.ErrNoRow() { errMsg = `获取指标信息失败` } return } if edbInfo.EdbInfoType != 1 { errMsg = `该指标不是预测指标` err = errors.New(errMsg) return } latestDateStr = edbInfo.LatestDate latestValue = edbInfo.LatestValue oldCalculateFormula := edbInfo.CalculateFormula //修改指标信息 switch lang { case utils.EnLangVersion: edbInfo.EdbNameEn = edbName edbInfo.UnitEn = unit default: edbInfo.EdbName = edbName edbInfo.Unit = unit edbInfo.EdbNameSource = edbName } edbInfo.Frequency = frequency edbInfo.ClassifyId = classifyId edbInfo.CalculateFormula = formula edbInfo.ModifyTime = time.Now() _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "ModifyTime", "EdbNameEn", "UnitEn") if err != nil { return } //判断计算指标是否被更换 var existCondition string var existPars []interface{} existCondition += " AND edb_info_id=? AND from_edb_info_id=? " existPars = append(existPars, edbInfo.EdbInfoId, fromEdbInfo.EdbInfoId) count, tmpErr := GetEdbInfoCalculateCountByCondition(existCondition, existPars) if tmpErr != nil { err = errors.New("判断指标是否改变失败,Err:" + tmpErr.Error()) return } if count > 0 && formula == oldCalculateFormula { // 指标未被替换,无需重新计算 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, edbInfo.SubSource) sql = ` DELETE FROM ` + tableName + ` WHERE edb_info_id = ? ` _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec() if err != nil { return } //关联关系 { calculateMappingItem := &EdbInfoCalculateMapping{ EdbInfoCalculateMappingId: 0, EdbInfoId: edbInfo.EdbInfoId, Source: edbInfo.Source, SourceName: edbInfo.SourceName, EdbCode: edbCode, FromEdbInfoId: fromEdbInfo.EdbInfoId, FromEdbCode: fromEdbInfo.EdbCode, FromEdbName: fromEdbInfo.EdbName, FromSource: fromEdbInfo.Source, FromSourceName: fromEdbInfo.SourceName, FromTag: "", Sort: 1, CreateTime: time.Now(), ModifyTime: time.Now(), } _, err = to.Insert(calculateMappingItem) if err != nil { return } } } // 计算数据 latestDateStr, latestValue, err = refreshAllPredictCalculateJp(to, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, fromEdbInfo, edbCode, edbInfo.Frequency, formula) return } // RefreshAllPredictCalculateJp 刷新全部预测降频值数据 func RefreshAllPredictCalculateJp(edbInfoId, source, subSource int, fromEdbInfo *EdbInfo, edbCode, edbFrequency, formula string) (latestDateStr string, latestValue float64, err error) { o := orm.NewOrm() to, err := o.Begin() if err != nil { return } defer func() { if err != nil { fmt.Println("RefreshAllCalculateJp,Err:" + err.Error()) _ = to.Rollback() } else { _ = to.Commit() } }() // 计算数据 latestDateStr, latestValue, err = refreshAllPredictCalculateJp(to, edbInfoId, source, subSource, fromEdbInfo, edbCode, edbFrequency, formula) return } // refreshAllPredictCalculateJp 刷新预测降频数据 func refreshAllPredictCalculateJp(to orm.TxOrmer, edbInfoId, source, subSource int, fromEdbInfo *EdbInfo, edbCode, edbFrequency, formula string) (latestDateStr string, latestValue float64, err error) { edbInfoIdStr := strconv.Itoa(edbInfoId) //获取源指标数据 fmt.Println("EdbInfoId:", fromEdbInfo.EdbInfoId) dataList, err := GetPredictEdbDataListAll(fromEdbInfo, 1) if err != nil { return } var dateArr []string dataMap := make(map[string]*EdbInfoSearchData) fromDataMap := make(map[string]float64) //来源指指标数据 for _, v := range dataList { dateArr = append(dateArr, v.DataTime) dataMap[v.DataTime] = v fromDataMap[v.DataTime] = v.Value } fmt.Println("source:", source) //获取降频指标所有数据 existDataList, err := GetAllEdbDataListByTo(to, edbInfoId, source, subSource) if err != nil { return } //计算指标的map existDataMap := make(map[string]*EdbData) // 已经入库的日期map existDelDateMap := make(map[string]string) for _, v := range existDataList { existDataMap[v.DataTime] = v existDelDateMap[v.DataTime] = v.DataTime } latestDateStr = fromEdbInfo.LatestDate tableName := GetEdbDataTableName(utils.DATA_SOURCE_PREDICT_CALCULATE_JP, subSource) addSql := ` INSERT INTO ` + tableName + ` (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values ` var isAdd bool //existMap := make(map[string]string) dataLen := len(dataList) if dataLen <= 0 { return } startDataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local) endDataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[dataLen-1].DataTime, time.Local) nextEndDate := utils.GetFrequencyEndDay(startDataTime, edbFrequency) // 下一个节点的日期 weekDayDataList := make([]float64, 0) for currStartDataTime := startDataTime; !currStartDataTime.After(endDataTime); currStartDataTime = currStartDataTime.AddDate(0, 0, 1) { // 将当前数据加入到 weekDayDataList if tmpData, ok := dataMap[currStartDataTime.Format(utils.FormatDate)]; ok { tmpValue := decimal.NewFromFloat(tmpData.Value) tmpValueFloat, _ := tmpValue.Round(4).Float64() weekDayDataList = append(weekDayDataList, tmpValueFloat) } // 日期处理过滤 switch edbFrequency { case "周度": if currStartDataTime.Weekday() != 0 { //不是周日,代表需要进入下一个循环获取数据并计算 continue } else { //记录下一个结束节点的日期 nextEndDate = currStartDataTime.AddDate(0, 0, 7) } case "旬度": nextDay := currStartDataTime.AddDate(0, 0, 1) if nextDay.Day() != 1 && nextDay.Day() != 11 && nextDay.Day() != 21 { //不是每月10、20、最后一天,代表需要进入下一个循环获取数据并计算 continue } else { //记录下一个结束节点的日期 if nextDay.Day() == 1 || nextDay.Day() == 11 { //月初或者月末的时候,加10天就好了 nextEndDate = nextDay.AddDate(0, 0, 9) } else { tmpNextMonth := nextDay.AddDate(0, 1, 0) nextEndDate = time.Date(tmpNextMonth.Year(), tmpNextMonth.Month(), 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1) } } case "月度": nextDay := currStartDataTime.AddDate(0, 0, 1) if nextDay.Day() != 1 { //不是每月最后一天,代表需要进入下一个循环获取数据并计算 continue } else { //记录下一个结束节点的日期 nextEndDate = nextDay.AddDate(0, 1, -1) } case "季度": nextDay := currStartDataTime.AddDate(0, 0, 1) if (nextDay.Month() == 1 || nextDay.Month() == 4 || nextDay.Month() == 7 || nextDay.Month() == 10) && nextDay.Day() == 1 { //记录下一个结束节点的日期 nextEndDate = nextDay.AddDate(0, 3, -1) } else { //不是3,6,9,12 月份的最后一天,代表需要进入下一个循环获取数据并计算 continue } case "年度": if currStartDataTime.Month() == 12 && currStartDataTime.Day() == 31 { //记录下一个结束节点的日期 nextEndDate = currStartDataTime.AddDate(1, 0, 0) } else { //不是每年的12-31日,代表需要进入下一个循环获取数据并计算 continue } default: err = errors.New("错误的频度:" + edbFrequency) return } // 本期的数据值 lenWeekDayDataList := len(weekDayDataList) if lenWeekDayDataList <= 0 { continue } // 当前时间段内的数据计算,得出实际值 var currVal float64 if formula == "期末值" { // 期末值,取区间最后一个日期的数据值 currVal = weekDayDataList[lenWeekDayDataList-1] } else { // 平均值 取区间平均值 sumValDeci := decimal.NewFromFloat(0) for _, v := range weekDayDataList { tmpValDeci := decimal.NewFromFloat(v) sumValDeci = sumValDeci.Add(tmpValDeci) } lenDeci := decimal.NewFromInt(int64(lenWeekDayDataList)) currVal, _ = sumValDeci.Div(lenDeci).Round(4).Float64() } // 给实际日期数据的值赋值 if fromEdbInfo.LatestDate == currStartDataTime.Format(utils.FormatDate) { latestValue = currVal } currStartDataTimeStr := currStartDataTime.Format(utils.FormatDate) // 判断降频指标是否存在数据 if existData, ok := existDataMap[currStartDataTimeStr]; 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, subSource, existData.EdbDataId, fmt.Sprint(currVal)) if err != nil { return } } // 移除待删除的日期 delete(existDelDateMap, currStartDataTimeStr) } else { // 直接入库 timestamp := currStartDataTime.UnixNano() / 1e6 timestampStr := fmt.Sprintf("%d", timestamp) addSql += GetAddSql(edbInfoIdStr, edbCode, currStartDataTime.Format(utils.FormatDate), timestampStr, fmt.Sprint(currVal)) isAdd = true // 移除待删除的日期 delete(existDelDateMap, currStartDataTimeStr) } // 一轮结束后,数据清空 weekDayDataList = make([]float64, 0) } // 最后已有的日期处理完成后,需要对剩余不在时间段内的数据做处理 lenWeekDayDataList := len(weekDayDataList) if lenWeekDayDataList > 0 { // 当前时间段内的数据计算,得出实际值 var currVal float64 if formula == "期末值" { currVal = weekDayDataList[lenWeekDayDataList-1] } else { // 平均值 sumValDeci := decimal.NewFromFloat(0) for _, v := range weekDayDataList { tmpValDeci := decimal.NewFromFloat(v) sumValDeci = sumValDeci.Add(tmpValDeci) } lenDeci := decimal.NewFromInt(int64(lenWeekDayDataList)) currVal, _ = sumValDeci.Div(lenDeci).Round(4).Float64() } nextEndDateStr := nextEndDate.Format(utils.FormatDate) // 判断降频指标是否存在数据 if existData, ok := existDataMap[nextEndDateStr]; 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, subSource, existData.EdbDataId, fmt.Sprint(currVal)) if err != nil { return } } // 移除待删除的日期 delete(existDelDateMap, nextEndDateStr) } else { // 直接入库 timestamp := nextEndDate.UnixNano() / 1e6 timestampStr := fmt.Sprintf("%d", timestamp) addSql += GetAddSql(edbInfoIdStr, edbCode, nextEndDate.Format(utils.FormatDate), timestampStr, fmt.Sprint(currVal)) isAdd = true // 移除待删除的日期 delete(existDelDateMap, nextEndDateStr) } } if isAdd { addSql = strings.TrimRight(addSql, ",") _, err = to.Raw(addSql).Exec() } // 移除不存在的日期数据 if len(existDelDateMap) > 0 { removeDateList := make([]string, 0) //需要移除的日期 for k := range existDelDateMap { 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 }