实现了热量值计算
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package com.sw.dualscreen.presentation
|
||||
|
||||
|
||||
import android.text.TextUtils
|
||||
import com.sw.dualscreen.model.response.FoodInfo
|
||||
import com.sw.dualscreen.model.response.UserNutritionData
|
||||
|
||||
object UserNutritionUtils {
|
||||
|
||||
/**
|
||||
* 计算热量信息
|
||||
*/
|
||||
fun calculateNutrition(
|
||||
foodInfo: FoodInfo,
|
||||
userModel: UserNutritionData,
|
||||
weight: Double,
|
||||
dinnerType: String
|
||||
): CalcResultInfo {
|
||||
// 1. 初始化变量
|
||||
var foodKcal = 0.0
|
||||
var totalKcal = 0.0
|
||||
var (vegetable, meat, fruits, grain) = List(4) { 0.0 }
|
||||
|
||||
// 2. 处理食物信息
|
||||
foodKcal = calculateValue(foodInfo.stFoodInfoMaterial?.energyKcal, weight)
|
||||
|
||||
grain = calculateValue(foodInfo.stFoodInfoPagodaAPPVO?.grainValue(), weight)
|
||||
fruits = calculateValue(foodInfo.stFoodInfoPagodaAPPVO?.fruitsValue(), weight)
|
||||
vegetable = calculateValue(foodInfo.stFoodInfoPagodaAPPVO?.vegetableValue(), weight)
|
||||
meat = calculateValue(foodInfo.stFoodInfoPagodaAPPVO?.meatValue(), weight)
|
||||
|
||||
// 3. 合并用户数据
|
||||
grain += userModel.stFoodInfoPagoda?.grainValue() ?: 0.0
|
||||
fruits += userModel.stFoodInfoPagoda?.fruitsValue() ?: 0.0
|
||||
vegetable = vegetable.plus(userModel.stFoodInfoPagoda?.vegetableValue() ?: 0.0)
|
||||
meat += userModel.stFoodInfoPagoda?.meatValue() ?: 0.0
|
||||
|
||||
totalKcal = foodKcal + userModel.energyValue()
|
||||
val fruitsAndVeg = fruits + vegetable
|
||||
|
||||
// 4. 判断当餐最大热量
|
||||
val maxKcal =
|
||||
userModel.totalEnergyCalculateScoreValue() * calculateDinnerTypeRatio(dinnerType) / 10
|
||||
|
||||
val pagoda = userModel.stFoodInfoPagoda
|
||||
val calcResult = CalcResultInfo(
|
||||
totalKcal = maxKcal to totalKcal,
|
||||
grain = parseMax(pagoda?.grainRecommend) to grain,
|
||||
fruits = parseMax(pagoda?.fruitsRecommend) + parseMax(pagoda?.vegetableRecommend) to fruitsAndVeg,
|
||||
meat = parseMax(pagoda?.meatRecommend) to meat
|
||||
)
|
||||
return calcResult
|
||||
}
|
||||
|
||||
fun parseMax(recommend: String?): Double {
|
||||
if (TextUtils.isEmpty(recommend) || !recommend!!.contains("-")) return 0.0
|
||||
val (min, max) = recommend.split("-")
|
||||
return max.toDouble()
|
||||
}
|
||||
|
||||
private fun calculateDinnerTypeRatio(dinnerType: String): Int {
|
||||
return when (dinnerType) {
|
||||
"早餐", "晚餐" -> 3
|
||||
"午餐" -> 4
|
||||
else -> 0
|
||||
}
|
||||
}
|
||||
|
||||
private fun calculateValue(nutrient: Double?, weight: Double): Double {
|
||||
return weight * (nutrient ?: 1.0)
|
||||
}
|
||||
|
||||
data class CalcResultInfo(
|
||||
var totalKcal: Pair<Double, Double>,
|
||||
var grain: Pair<Double, Double>,
|
||||
var fruits: Pair<Double, Double>,
|
||||
var meat: Pair<Double, Double>,
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user