浏览代码

fix:修复计算Bug

Roc 2 年之前
父节点
当前提交
c20f129310
共有 1 个文件被更改,包括 43 次插入99 次删除
  1. 43 99
      models/supply_analysis/base_from_stock_plant_data.go

+ 43 - 99
models/supply_analysis/base_from_stock_plant_data.go

@@ -146,58 +146,49 @@ func calculateEdb(to orm.TxOrmer, varietyEdbInfo *VarietyEdbInfo, dataMap map[ti
 		deleteIdMap[v.StockPlantDataId] = v.StockPlantDataId
 	}
 
-	tmpDataList := make([]float64, 0) // 每天的值
-	var preSumValDeci decimal.Decimal // 上一期的值
-	endDate := lastDate               // 实际数据最后结束的日期
-	var latestValue float64           // 最新值
+	endDate := lastDate     // 实际数据最后结束的日期
+	var latestValue float64 // 最新值
+
+	// 影响周度产量:当前日期往前(更早日期)统计7天的影响日度产量(减产量)合计 ---- 七个数据和
+	// 周度产量变动:往前(更早日期)推第8天的日期对应的影响周度产量值 - 当前日期对应的影响周度产量值
+	// 影响月度产量:日期序列为最早检修日期所在月份开始 至 当前日期+365天的日期所在月份,每月最后一天当月的所有影响日度产量(减产量)合计
+	// 月度产量变动:上期影响月度产量值 - 当期影响月度产量值
 	switch varietyEdbInfo.Source {
 	//1:影响周度产量;2:周度产量变动;3:影响月度产量;4:月度产量变动
 	case 1, 2:
-		// 期间周度数据
-		for tmpDate := startDate; !tmpDate.After(lastDate); tmpDate = tmpDate.AddDate(0, 0, 1) {
-			//tmpData, ok := dataMap[tmpDate]
-			//if !ok {
-			//	continue
-			//}
-			//tmpDataList = append(tmpDataList, tmpData)
-			//
-			//if tmpDate.Weekday() != 0 {
-			//	// 如果不是周日,那么就退出当前循环
-			//	continue
-			//}
-			//
-			//
-			//// 日期期间汇总数据
-			//var sumVal float64
-			//for _, tmpVal := range tmpDataList {
-			//	sumVal += tmpVal
-			//}
-			//sumValDeci := decimal.NewFromFloat(sumVal)
-			//
-			//var currVal float64
-			//if varietyEdbInfo.Source == 2 { // 如果是变动的话,那么是
-			//	currVal, _ = (decimal.NewFromFloat(sumVal).Sub(preSumValDeci)).Round(4).Float64()
-			//} else {
-			//	currVal, _ = sumValDeci.Round(4).Float64()
-			//}
-			//preSumValDeci = sumValDeci       // 赋值给上一个周期的截止值
-
-			currVal, ok := dataMap[tmpDate]
-			if !ok {
-				continue
+		// 周度产量数据map
+		weekDataMap := make(map[time.Time]float64)
+
+		for currDate := startDate; !currDate.After(lastDate); currDate = currDate.AddDate(0, 0, 1) {
+			var weekVal, currVal float64
+			for i := 0; i <= 6; i++ {
+				tmpDate := currDate.AddDate(0, 0, -i)
+				tmpData, ok := dataMap[tmpDate]
+				if !ok {
+					continue
+				}
+				weekVal = weekVal + tmpData
 			}
-			preSumValDeci = decimal.NewFromFloat(currVal) // 赋值给上一个周期的截止值
-			latestValue = currVal
+			weekDataMap[currDate] = weekVal // 周度产量数据
 
-			tmpDataList = make([]float64, 0) //重置值列表,便于下一个周期计算
+			if varietyEdbInfo.Source == 1 {
+				currVal = weekVal
+			} else {
+				preVal, ok := weekDataMap[currDate.AddDate(0, 0, -7)]
+				if !ok {
+					preVal = 0
+				}
+				currVal = preVal - weekVal
+			}
 
-			timestamp := tmpDate.UnixNano() / 1e6
-			existItem, ok := existData[tmpDate]
+			latestValue = currVal
+			timestamp := currDate.UnixNano() / 1e6
+			existItem, ok := existData[currDate]
 			if !ok {
 				addList = append(addList, &BaseFromStockPlantData{
 					//StockPlantDataId: 0,
 					VarietyEdbId:  varietyEdbInfo.VarietyEdbId,
-					DataTime:      tmpDate,
+					DataTime:      currDate,
 					Value:         currVal,
 					ModifyTime:    time.Now(),
 					CreateTime:    time.Now(),
@@ -216,58 +207,10 @@ func calculateEdb(to orm.TxOrmer, varietyEdbInfo *VarietyEdbInfo, dataMap map[ti
 				delete(deleteIdMap, existItem.StockPlantDataId)
 			}
 		}
-
-		// 最后一周,不满一周时的计算
-		//lastDateWeek := lastDate.Weekday()
-		//if lastDateWeek != 0 {
-		//	// 如果最后一天不是周日,那么需要把日期调整到周日
-		//	subDay := 7 - lastDateWeek
-		//	tmpDate := lastDate.AddDate(0, 0, int(subDay))
-		//	endDate = tmpDate //数据实际结束的日期
-		//
-		//	existItem, ok := existData[tmpDate]
-		//
-		//	timestamp := tmpDate.UnixNano() / 1e6
-		//	// 日期期间汇总数据
-		//	var sumVal float64
-		//	for _, tmpVal := range tmpDataList {
-		//		sumVal += tmpVal
-		//	}
-		//	sumValDeci := decimal.NewFromFloat(sumVal)
-		//
-		//	var currVal float64
-		//	if varietyEdbInfo.Source == 2 { // 如果是变动的话,那么是
-		//		currVal, _ = (decimal.NewFromFloat(sumVal).Sub(preSumValDeci)).Round(4).Float64()
-		//	} else {
-		//		currVal, _ = sumValDeci.Round(4).Float64()
-		//	}
-		//	latestValue = currVal
-		//
-		//	if !ok {
-		//		addList = append(addList, &BaseFromStockPlantData{
-		//			//StockPlantDataId: 0,
-		//			VarietyEdbId:  varietyEdbInfo.VarietyEdbId,
-		//			DataTime:      tmpDate,
-		//			Value:         currVal,
-		//			ModifyTime:    time.Now(),
-		//			CreateTime:    time.Now(),
-		//			DataTimestamp: timestamp,
-		//		})
-		//	} else {
-		//		if existItem.Value != currVal {
-		//			existItem.Value = currVal
-		//			existItem.ModifyTime = time.Now()
-		//			_, err = to.Update(existItem, "Value", "ModifyTime")
-		//			if err != nil {
-		//				return
-		//			}
-		//		}
-		//
-		//		delete(deleteIdMap, existItem.StockPlantDataId)
-		//	}
-		//}
-
 	case 3, 4:
+		tmpDataList := make([]float64, 0)        // 每天的值
+		preSumValDeci := decimal.NewFromFloat(0) // 上一期的值
+
 		// 期间月度数据
 		for tmpDate := startDate; !tmpDate.After(lastDate); tmpDate = tmpDate.AddDate(0, 0, 1) {
 			tmpData, ok := dataMap[tmpDate]
@@ -291,10 +234,10 @@ func calculateEdb(to orm.TxOrmer, varietyEdbInfo *VarietyEdbInfo, dataMap map[ti
 			sumValDeci := decimal.NewFromFloat(sumVal)
 
 			var currVal float64
-			if varietyEdbInfo.Source == 2 { // 如果是变动的话,那么是
-				currVal, _ = (decimal.NewFromFloat(sumVal).Sub(preSumValDeci)).Round(4).Float64()
-			} else {
+			if varietyEdbInfo.Source == 3 { // 月度影响产量
 				currVal, _ = sumValDeci.Round(4).Float64()
+			} else { // 如果是月度产量变动的话
+				currVal, _ = (preSumValDeci.Sub(decimal.NewFromFloat(sumVal))).Round(4).Float64()
 			}
 			latestValue = currVal
 
@@ -344,10 +287,10 @@ func calculateEdb(to orm.TxOrmer, varietyEdbInfo *VarietyEdbInfo, dataMap map[ti
 			sumValDeci := decimal.NewFromFloat(sumVal)
 
 			var currVal float64
-			if varietyEdbInfo.Source == 2 { // 如果是变动的话,那么是
-				currVal, _ = (decimal.NewFromFloat(sumVal).Sub(preSumValDeci)).Round(4).Float64()
-			} else {
+			if varietyEdbInfo.Source == 3 { // 月度影响产量
 				currVal, _ = sumValDeci.Round(4).Float64()
+			} else { // 如果是月度产量变动的话
+				currVal, _ = (preSumValDeci.Sub(decimal.NewFromFloat(sumVal))).Round(4).Float64()
 			}
 			latestValue = currVal
 
@@ -374,6 +317,7 @@ func calculateEdb(to orm.TxOrmer, varietyEdbInfo *VarietyEdbInfo, dataMap map[ti
 				delete(deleteIdMap, existItem.StockPlantDataId)
 			}
 		}
+
 	default:
 		err = errors.New("错误的指标类型")
 		return