Explorar el Código

优化空值补全逻辑

xyxie hace 1 año
padre
commit
93eae4946f
Se han modificado 1 ficheros con 50 adiciones y 56 borrados
  1. 50 56
      models/base_from_calculate.go

+ 50 - 56
models/base_from_calculate.go

@@ -644,21 +644,6 @@ func CheckFormula2(edbInfoArr []*EdbInfo, formulaMap map[string]string, formulaS
 
 // 处理整个数据
 func HandleDateSaveDataMap(dateList []string, maxStartDate, minLatestDate time.Time, realSaveDataMap, saveDataMap map[string]map[int]float64, edbInfoIdArr []*EdbInfo, emptyType int) {
-	var startDate, endDate string
-	var startDateT, endDateT time.Time
-	if emptyType == 2 || emptyType == 3 {
-		for k, _ := range realSaveDataMap {
-			if k > endDate {
-				endDate = k
-			}
-			if k < startDate || startDate == "" {
-				startDate = k
-			}
-		}
-
-		startDateT, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
-		endDateT, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
-	}
 	for _, date := range dateList {
 		dateTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
 		// 如果当前日期早于数据的最大开始日期,那么不处理,进入下一个循环
@@ -695,9 +680,9 @@ func HandleDateSaveDataMap(dateList []string, maxStartDate, minLatestDate time.T
 				case 0:
 					handleDateDataMap(realSaveDataMap, saveDataMap, date, tmpEdbInfoId, 35)
 				case 2:
-					handleDateDataMapBefore(realSaveDataMap, saveDataMap, date, tmpEdbInfoId, startDateT, endDateT)
+					handleDateDataMapBefore(realSaveDataMap, saveDataMap, date, tmpEdbInfoId, edbInfo.StartDate, edbInfo.EndDate)
 				case 3:
-					handleDateDataMapAfter(realSaveDataMap, saveDataMap, date, tmpEdbInfoId, startDateT, endDateT)
+					handleDateDataMapAfter(realSaveDataMap, saveDataMap, date, tmpEdbInfoId, edbInfo.StartDate, edbInfo.EndDate)
 				case 4:
 					handleDateDataMapZero(saveDataMap, date, tmpEdbInfoId)
 				}
@@ -750,15 +735,34 @@ func handleDateDataMap(realSaveDataMap, saveDataMap map[string]map[int]float64,
 }
 
 // handleDateDataMapBefore 前值填充:空值优先以最近的前值填充,没有前值时,用后值填充
-func handleDateDataMapBefore(realSaveDataMap, saveDataMap map[string]map[int]float64, date string, edbInfoId int, startDateT, endDateT time.Time) {
+func handleDateDataMapBefore(realSaveDataMap, saveDataMap map[string]map[int]float64, date string, edbInfoId int, startDate, endDate string) {
 	currDate, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
 
-	// 后一天
-	nextDateDay := currDate
-
 	// 前一天
 	preDateDay := currDate
 
+	startDateT, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
+	endDateT, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
+
+	if currDate.Before(startDateT) {
+		if findDataMap, hasFindDataMap := realSaveDataMap[startDate]; hasFindDataMap { // 下一个日期有数据
+			if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
+				utils.FileLog.Info(fmt.Sprintf("date:%s, 无值,取%s的值%.4f", date, startDate, val))
+				saveDataMap[date][edbInfoId] = val
+				return
+			}
+		}
+		return
+	}
+	if currDate.After(endDateT) {
+		if findDataMap, hasFindDataMap := realSaveDataMap[endDate]; hasFindDataMap { // 下一个日期有数据
+			if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
+				utils.FileLog.Info(fmt.Sprintf("date:%s, 无值,取%s的值%.4f", date, startDate, val))
+				saveDataMap[date][edbInfoId] = val
+				return
+			}
+		}
+	}
 	for i := 1; preDateDay.After(startDateT) || preDateDay == startDateT; i++ {
 		// 上个日期的数据
 		{
@@ -766,22 +770,7 @@ func handleDateDataMapBefore(realSaveDataMap, saveDataMap map[string]map[int]flo
 			preDateDayStr := preDateDay.Format(utils.FormatDate)
 			if findDataMap, hasFindDataMap := realSaveDataMap[preDateDayStr]; hasFindDataMap { // 下一个日期有数据
 				if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
-					fmt.Println(fmt.Sprintf("date:%s, 无值,取%s的值%.4f", date, preDateDayStr, val))
-					saveDataMap[date][edbInfoId] = val
-					return
-				}
-			}
-		}
-	}
-
-	for i := 1; nextDateDay.Before(endDateT) || nextDateDay == endDateT; i++ {
-		// 下个日期的数据
-		{
-			nextDateDay = currDate.AddDate(0, 0, i)
-			nextDateDayStr := nextDateDay.Format(utils.FormatDate)
-			if findDataMap, hasFindDataMap := realSaveDataMap[nextDateDayStr]; hasFindDataMap { // 下一个日期有数据
-				if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
-					fmt.Println(fmt.Sprintf("date:%s, 无值,取%s的值%.4f", date, nextDateDayStr, val))
+					utils.FileLog.Info(fmt.Sprintf("date:%s, 无值,取%s的值%.4f", date, preDateDayStr, val))
 					saveDataMap[date][edbInfoId] = val
 					return
 				}
@@ -792,14 +781,34 @@ func handleDateDataMapBefore(realSaveDataMap, saveDataMap map[string]map[int]flo
 }
 
 // handleDateDataMapAfter 后值填充:空值优先以最近的后值填充,没有后值时,用前值填充
-func handleDateDataMapAfter(realSaveDataMap, saveDataMap map[string]map[int]float64, date string, edbInfoId int, startDateT, endDateT time.Time) {
+func handleDateDataMapAfter(realSaveDataMap, saveDataMap map[string]map[int]float64, date string, edbInfoId int, startDate, endDate string) {
 	currDate, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
 
 	// 后一天
 	nextDateDay := currDate
 
-	// 前一天
-	preDateDay := currDate
+	startDateT, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
+	endDateT, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
+
+	if currDate.Before(startDateT) {
+		if findDataMap, hasFindDataMap := realSaveDataMap[startDate]; hasFindDataMap { // 下一个日期有数据
+			if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
+				utils.FileLog.Info(fmt.Sprintf("date:%s, 无值,取%s的值%.4f", date, startDate, val))
+				saveDataMap[date][edbInfoId] = val
+				return
+			}
+		}
+		return
+	}
+	if currDate.After(endDateT) {
+		if findDataMap, hasFindDataMap := realSaveDataMap[endDate]; hasFindDataMap { // 下一个日期有数据
+			if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
+				utils.FileLog.Info(fmt.Sprintf("date:%s, 无值,取%s的值%.4f", date, startDate, val))
+				saveDataMap[date][edbInfoId] = val
+				return
+			}
+		}
+	}
 
 	for i := 1; nextDateDay.Before(endDateT) || nextDateDay == endDateT; i++ {
 		// 下个日期的数据
@@ -808,22 +817,7 @@ func handleDateDataMapAfter(realSaveDataMap, saveDataMap map[string]map[int]floa
 			nextDateDayStr := nextDateDay.Format(utils.FormatDate)
 			if findDataMap, hasFindDataMap := realSaveDataMap[nextDateDayStr]; hasFindDataMap { // 下一个日期有数据
 				if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
-					fmt.Println(fmt.Sprintf("date:%s, 无值,取%s的值%.4f", date, nextDateDayStr, val))
-					saveDataMap[date][edbInfoId] = val
-					return
-				}
-			}
-		}
-	}
-
-	for i := 1; preDateDay.After(startDateT) || preDateDay == startDateT; i++ {
-		// 上个日期的数据
-		{
-			preDateDay = currDate.AddDate(0, 0, -i)
-			preDateDayStr := preDateDay.Format(utils.FormatDate)
-			if findDataMap, hasFindDataMap := realSaveDataMap[preDateDayStr]; hasFindDataMap { // 下一个日期有数据
-				if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
-					fmt.Println(fmt.Sprintf("date:%s, 无值,取%s的值%.4f", date, preDateDayStr, val))
+					utils.FileLog.Info(fmt.Sprintf("date:%s, 无值,取%s的值%.4f", date, nextDateDayStr, val))
 					saveDataMap[date][edbInfoId] = val
 					return
 				}