|
@@ -238,9 +238,12 @@ func refreshAllPredictCalculateJp(to orm.TxOrmer, edbInfoId, source, subSource i
|
|
|
return
|
|
|
}
|
|
|
//计算指标的map
|
|
|
- existDataMap := make(map[string]*EdbData, 0)
|
|
|
+ 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
|
|
@@ -257,8 +260,7 @@ func refreshAllPredictCalculateJp(to orm.TxOrmer, edbInfoId, source, subSource i
|
|
|
startDataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
|
|
|
endDataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[dataLen-1].DataTime, time.Local)
|
|
|
|
|
|
- var lastValue float64 // 最近的值
|
|
|
- var nextEndDate time.Time // 下一个节点的日期
|
|
|
+ nextEndDate := utils.GetFrequencyEndDay(startDataTime, edbFrequency) // 下一个节点的日期
|
|
|
weekDayDataList := make([]float64, 0)
|
|
|
for currStartDataTime := startDataTime; !currStartDataTime.After(endDataTime); currStartDataTime = currStartDataTime.AddDate(0, 0, 1) {
|
|
|
// 将当前数据加入到 weekDayDataList
|
|
@@ -267,10 +269,6 @@ func refreshAllPredictCalculateJp(to orm.TxOrmer, edbInfoId, source, subSource i
|
|
|
tmpValueFloat, _ := tmpValue.Round(4).Float64()
|
|
|
weekDayDataList = append(weekDayDataList, tmpValueFloat)
|
|
|
}
|
|
|
- // 如果下个节点的日期不存在,那么就先给赋值(兼容时间区间内只有一组数据的情况)
|
|
|
- if nextEndDate.IsZero() {
|
|
|
- nextEndDate = utils.GetFrequencyEndDay(currStartDataTime, edbFrequency)
|
|
|
- }
|
|
|
|
|
|
// 日期处理过滤
|
|
|
switch edbFrequency {
|
|
@@ -328,34 +326,35 @@ func refreshAllPredictCalculateJp(to orm.TxOrmer, edbInfoId, source, subSource i
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+ // 本期的数据值
|
|
|
+ lenWeekDayDataList := len(weekDayDataList)
|
|
|
+ if lenWeekDayDataList <= 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
// 当前时间段内的数据计算,得出实际值
|
|
|
var currVal float64
|
|
|
- lenWeekDayDataList := len(weekDayDataList)
|
|
|
- // 如果这个时间区间内没有数据,那么就采用上一个时间区间的值
|
|
|
- if len(weekDayDataList) <= 0 {
|
|
|
- currVal = lastValue
|
|
|
+ if formula == "期末值" { // 期末值,取区间最后一个日期的数据值
|
|
|
+ currVal = weekDayDataList[lenWeekDayDataList-1]
|
|
|
} else {
|
|
|
- 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()
|
|
|
+ // 平均值 取区间平均值
|
|
|
+ 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[currStartDataTime.Format(utils.FormatDate)]; ok {
|
|
|
+ if existData, ok := existDataMap[currStartDataTimeStr]; ok {
|
|
|
// 处理降频数据的值
|
|
|
existValStr := existData.Value
|
|
|
existValDeci, tmpErr := decimal.NewFromString(existValStr)
|
|
@@ -371,12 +370,18 @@ func refreshAllPredictCalculateJp(to orm.TxOrmer, edbInfoId, source, subSource i
|
|
|
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)
|
|
|
}
|
|
|
|
|
|
// 一轮结束后,数据清空
|
|
@@ -384,30 +389,27 @@ func refreshAllPredictCalculateJp(to orm.TxOrmer, edbInfoId, source, subSource i
|
|
|
}
|
|
|
|
|
|
// 最后已有的日期处理完成后,需要对剩余不在时间段内的数据做处理
|
|
|
- if len(weekDayDataList) > 0 {
|
|
|
+ lenWeekDayDataList := len(weekDayDataList)
|
|
|
+ if lenWeekDayDataList > 0 {
|
|
|
// 当前时间段内的数据计算,得出实际值
|
|
|
var currVal float64
|
|
|
- lenWeekDayDataList := len(weekDayDataList)
|
|
|
- // 如果这个时间区间内没有数据,那么就采用上一个时间区间的值
|
|
|
- if len(weekDayDataList) < 0 {
|
|
|
- currVal = lastValue
|
|
|
+ if formula == "期末值" {
|
|
|
+ currVal = weekDayDataList[lenWeekDayDataList-1]
|
|
|
} else {
|
|
|
- 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()
|
|
|
+ // 平均值
|
|
|
+ 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[nextEndDate.Format(utils.FormatDate)]; ok {
|
|
|
+ if existData, ok := existDataMap[nextEndDateStr]; ok {
|
|
|
// 处理降频数据的值
|
|
|
existValStr := existData.Value
|
|
|
existValDeci, tmpErr := decimal.NewFromString(existValStr)
|
|
@@ -423,12 +425,18 @@ func refreshAllPredictCalculateJp(to orm.TxOrmer, edbInfoId, source, subSource i
|
|
|
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)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -437,5 +445,21 @@ func refreshAllPredictCalculateJp(to orm.TxOrmer, edbInfoId, source, subSource i
|
|
|
_, 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
|
|
|
}
|