MeiRiYiCheng_1_old/YBDevice.NApi/Application/MeasureInfo/BodyFatHelperService.cs

1774 lines
77 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Furion;
using Furion.DependencyInjection;
using Furion.RemoteRequest.Extensions;
using Nirvana.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using YBDevice.Core;
using YBDevice.Entity;
using YBDevice.NApi.Application.UserInfo;
namespace YBDevice.NApi.Application.MeasureInfo
{
/// <summary>
/// 体脂计算算法
/// </summary>
public class BodyFatHelperService : IBodyFatHelperService, ITransient
{
private readonly IHttpClientFactory _clientFactory;
private static string RequestUrl = App.Configuration["BodyApiSettings:ApiUrl"];
private static string RequestaccessSecret = App.Configuration["BodyApiSettings:AccessSecret"];
private static string RequestaccessKey = App.Configuration["BodyApiSettings:AccessKey"];
public BodyFatHelperService(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
/// <summary>
/// 结果计算
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public UserMeasureModel CalcBodyFat(MeasureCalcDto model)
{
//算法使用的性别为0-女,1-男
int sex = model.sex == 2 ? 0 : 1;
int age = model.age;
double weight = model.weight.ToDouble();
int height = model.height.ToInt();
decimal dweight = weight.ToDecimal();
model.StandardWeight = standweight(height, sex).ToDecimal(2);
model.fat_w = (model.fat_r.ToDouble() * weight / 100).ToDecimal(2);
model.lbm = ((1 - model.fat_r.ToDouble() / 100) * weight).ToDecimal(2);
model.muscleval = (weight * (model.muscle.ToDouble() / 100)).ToDecimal(2);
model.proteinval = (weight * (model.protein.ToDouble() / 100)).ToDecimal(2);
if (model.imp <= 0)
{
decimal mheight = (height.ToDecimal()) / (100*1.0m);
var returndata = new UserMeasureModel
{
weight = model.height,
bmi = (((dweight / (mheight * mheight)) * 10.0m) / 10.0m).ToDecimal(1),
standardWeight = standweight(height, sex).ToString("f2"),
};
returndata.bmiLevel = bmi(model.bmi.ToFloat());
returndata.cmi = score(returndata.bmi.ToFloat()).ToDecimal(1);
double levels = (weight - returndata.standardWeight.ToDouble()) / returndata.standardWeight.ToDouble();
returndata.fatLevel = fatlevel(levels);
returndata.fatlevlval = levels.ToString("f2");
return returndata;
}
var data = new UserMeasureModel
{
bmi = model.bmi,
bmiLevel = bmi(model.bmi.ToFloat()),
fat_r = model.fat_r,
fat_rLevel = Fat_r(model.fat_r.ToFloat(), sex, age),
bodyage = model.bodyage,
bodyageLevel = bodyage(model.bodyage, age),
fat_w = model.fat_w,
fat_wLevel = Fat_r(model.fat_r.ToFloat(), sex, age),
bone = model.bone,
boneLevel = bone(model.bone.ToFloat(), weight, sex),
kcal = model.kcal < 0 ? 0 : model.kcal,
kcalLevel = kcal(model.kcal.ToFloat(), weight, age, sex),
muscle = model.muscle,
muscleLevel = muscle(model.muscle.ToFloat(), weight, age, sex),
protein = model.protein,
proteinLevel = protein(model.protein.ToFloat(), weight, age, sex),
visceral = model.visceral,
visceralLevel = visceral(model.visceral.ToFloat(), weight, age, sex),
water = model.water,
waterLevel = water(model.water.ToFloat(), weight, age, sex),
cmi = score(model.bmi.ToFloat()).ToDecimal(1),
muscleval = model.muscleval,
proteinval = model.proteinval,
lbm = model.lbm,
standardWeight = model.StandardWeight.ToString(),
sfr = model.sfr.ToDecimal(2),
sfrLevel = sfr(model.sfr.ToFloat(), weight, age, sex, height.ToInt()),
body = "0",
standardfat_r = fa_r_value(sex, age).ToJson(),
standardbmi = bmi_value(sex, age).ToJson(),
standardmuscle = muscle_value(sex, age).ToJson(),
standardmuscleval = muscleval_value(sex,age, dweight).ToJson(),
standardwater = water_value(sex, age).ToJson(),
standardbone = bone_value(sex, age, dweight).ToJson(),
standardkcal = kcal_value(sex, age, dweight).ToJson(),
standardfat_w = fat_w_value(sex,age, dweight).ToJson(),
standardviscera = visceral_value(sex, age).ToJson(),
standardprotein = protein_value(sex, age).ToJson(),
standardproteinval = proteinval_value(sex,age,dweight).ToJson(),
standardbodyage = "",
standardsfr = sfr_value(sex, age).ToJson(),
weight = model.weight.ToDecimal(),
fatlevlval = model.fatlevlval
};
data.bodylevel = body(data.fat_rLevel, data.muscleLevel);
data.musulevalLevel = data.muscleLevel;
data.proteinvalLevel = data.proteinLevel;
// double level = (weight - data.standardWeight.ToDouble()) / data.standardWeight.ToDouble();
data.fatLevel = model.IsCalc == 1 ? model.fatlevlval : fatlevel(model.fatlevlval.ToDouble());
// data.fatlevlval = level.ToString("f2");
return data;
}
/// <summary>
/// 二、四电极计算体脂
/// </summary>
/// <param name="weight">体重,单位为kg</param>
/// <param name="height">身高,单位为米</param>
/// <param name="age">年龄</param>
/// <param name="adc">阻抗</param>
/// <param name="sex">性别,1-男2-女,0-未知</param>
/// <returns></returns>
public UserMeasureModel CalcBodyFat(double weight, double height, int age, int adc, int sex)
{
//算法使用的性别为0-女,1-男
sex = sex == 2 ? 0 : 1;
var result = GetBodyfatResults(weight, height / 100, age, adc, sex);
decimal dwegiht = weight.ToDecimal();
var data = new UserMeasureModel
{
bmi = result.bmi.ToDecimal(),
bmiLevel = bmi(result.bmi.ToFloat()),
fat_r = result.bfr.ToDecimal(),
fat_rLevel = Fat_r(result.bfr.ToFloat(), sex, age),
bodyage = result.physicAge.ToInt(),
bodyageLevel = bodyage(result.physicAge.ToInt(), age),
fat_w = (result.bfr.ToDouble() * weight / 100).ToDecimal(2),
fat_wLevel = Fat_r(result.bfr.ToFloat(), sex, age),
bone = result.bm.ToDecimal(),
boneLevel = bone(result.bm.ToFloat(), weight, sex),
kcal = result.bmr.ToDecimal() < 0 ? 0 : result.bmr.ToDecimal(),
kcalLevel = kcal(result.bmr.ToFloat(), weight, age, sex),
muscle = result.rom.ToDecimal(),
muscleLevel = muscle(result.rom.ToFloat(), weight, age, sex),
protein = result.pp.ToDecimal(),
proteinLevel = protein(result.pp.ToFloat(), weight, age, sex),
visceral = result.uvi.ToDecimal(),
visceralLevel = visceral(result.uvi.ToFloat(), weight, age, sex),
water = result.moi.ToDecimal(),
waterLevel = water(result.moi.ToFloat(), weight, age, sex),
cmi = score(result.bmi.ToFloat()).ToDecimal(1),
muscleval = (weight * (result.rom.ToDouble() / 100)).ToDecimal(2),
proteinval = (weight * (result.pp.ToDouble() / 100)).ToDecimal(2),
lbm = ((1 - result.bfr.ToDouble() / 100) * weight).ToDecimal(2),
standardWeight = standweight(height.ToInt(), sex).ToString("f2"),
sfr = result.sfr.ToDecimal(2),
sfrLevel = sfr(result.sfr.ToFloat(), weight, age, sex, height.ToInt()),
body = "0",
standardfat_r = fa_r_value(sex, age).ToJson(),
standardbmi = bmi_value(sex, age).ToJson(),
standardmuscle = muscle_value(sex, age).ToJson(),
standardmuscleval = muscleval_value(sex,age, dwegiht).ToJson(),
standardwater = water_value(sex, age).ToJson(),
standardbone = bone_value(sex, age, dwegiht).ToJson(),
standardkcal = kcal_value(sex, age, dwegiht).ToJson(),
standardfat_w = fat_w_value(sex,age, dwegiht).ToJson(),
standardviscera = visceral_value(sex, age).ToJson(),
standardprotein = protein_value(sex, age).ToJson(),
standardproteinval = proteinval_value(sex,age, dwegiht).ToJson(),
standardbodyage = "",
standardsfr = sfr_value(sex, age).ToJson(),
weight = weight.ToDecimal(2),
height = height.ToDecimal(2)
};
data.bodylevel = body(data.fat_rLevel, data.muscleLevel);
data.musulevalLevel = data.muscleLevel;
data.proteinvalLevel = data.proteinLevel;
double level = (weight - data.standardWeight.ToDouble()) / data.standardWeight.ToDouble();
data.fatLevel = fatlevel(level);
data.fatlevlval = level.ToString("f2");
return data;
}
/// <summary>
/// 八电极计算体脂
/// </summary>
/// <param name="weight">体重,单位为kg</param>
/// <param name="height">身高,单位为米</param>
/// <param name="age">年龄</param>
/// <param name="sex">性别,1-男2-女,0-未知</param>
/// <param name="body">全身阻抗</param>
/// <param name="leftfoot">左脚阻抗</param>
/// <param name="lefthand">左手阻抗</param>
/// <param name="rightfoot">右脚阻抗</param>
/// <param name="righthand">右手阻抗</param>
public async Task<UserMeasureDto> CalcBody120FatAsync(decimal weight, decimal height, int age, int sex, decimal lefthand, decimal righthand, decimal leftfoot, decimal rightfoot, decimal body)
{
var requestdata = new
{
age = age,
heightCm = height,
peopleType = 0,
sex = sex,
weightKg = weight,
zLeftBodyEnCode = body,
zLeftArmEnCode = lefthand,
zLeftLegEnCode = leftfoot,
zRightArmEnCode = righthand,
zRightLegEnCode = rightfoot
};
var hashtable = HashTableHelper.Object2Hashtable(requestdata);
var strA = Common.SortRequestParam(hashtable);
var accessSecret = RequestaccessSecret;
var accessKey = RequestaccessKey;
var timestamp = Common.GetTimestamps();
strA = $"{strA}&timestamp={timestamp}&accessSecret={accessSecret}";
var sign = Md5.md5(strA, 32).ToUpper();
var url = RequestUrl;
var loggerService = App.GetService<ILoggerService>();
var response = await url
.SetHeaders(new Dictionary<string, object>
{
{ "AccessKey",accessKey },
{"Timestamp",timestamp },
{"Sign",sign}
})
.SetClientTimeout(10)
.SetBody(requestdata, "application/json", Encoding.UTF8)
.PostAsync();
var returnstr = await response.Content.ReadAsStringAsync();
var mheight = height / 100;
if (response.IsSuccessStatusCode)
{
var codedata = returnstr.ToObject<BodyReponseInfo>();
if (codedata.code != 1000)
{
loggerService.AddLogger($"八电极算法请求失败,参数:{requestdata.ToJson()},错误信息:{returnstr}");
//return new UserMeasureDto()
//{
// weight = weight,
// bmi = (((weight / (mheight * mheight)) * 10.0m) / 10.0m).ToDecimal(1),
// height = height,
//};
var returndata = new UserMeasureDto
{
weight = weight,
bmi = (((weight / (mheight * mheight)) * 10.0m) / 10.0m).ToDecimal(1),
idealweight = standweight(height.ToInt(), sex).ToDecimal(2),
height = height
};
returndata.bmiLevel = bmi(returndata.bmi.ToFloat());
returndata.cmi = score(returndata.bmi.ToFloat()).ToDecimal(1);
decimal levels = (weight - returndata.idealweight) / returndata.idealweight;
returndata.fatLevel = fatlevel(levels.ToDouble());
return returndata;
}
else
{
var responsedata = returnstr.ToObject<BodyReponseInfo<BodyResponseData>>();
var returndata = new UserMeasureDto()
{
weight = weight,
height = height
};
foreach (var item in responsedata.data)
{
var name = item.name.ToLower();
if (name == "bmi")
{
// returndata.bmi = GetBodyValue(name, item).ToDecimal();
returndata.bmi = (((weight / (mheight * mheight)) * 10.0m) / 10.0m).ToDecimal(1);
returndata.bmiLevel = GetBodyValueLevel(name, item);
}
if (name == "bodyfatrate")
{
returndata.fat_r = GetBodyValue(name, item).ToDecimal();
returndata.fat_rLevel = GetBodyValueLevel(name, item);
returndata.fat_wLevel = returndata.fat_rLevel;
}
if (name == "bonekg")
{
returndata.bone = GetBodyValue(name, item).ToDecimal();
returndata.boneLevel = GetBodyValueLevel(name, item);
}
if (name == "waterrate")
{
returndata.water = GetBodyValue(name, item).ToDecimal();
returndata.waterLevel = GetBodyValueLevel(name, item);
}
if (name == "musclekg")
{
returndata.muscleval = GetBodyValue(name, item).ToDecimal();
returndata.muscleLevel = GetBodyValueLevel(name, item);
}
if (name == "proteinrate")
{
returndata.protein = GetBodyValue(name, item).ToDecimal();
returndata.proteinLevel = GetBodyValueLevel(name, item);
}
if (name == "skeletalmusclekg")
{
returndata.SkeletalMuscle = GetBodyValue(name, item).ToDecimal();
returndata.SkeletalMuscleLevel = GetBodyValueLevel(name, item);
}
if (name == "bodyfatsubcutrate")
{
returndata.sfr = GetBodyValue(name, item).ToDecimal();
returndata.sfrLevel = GetBodyValueLevel(name, item);
}
if (name == "bmr")
{
returndata.kcal = GetBodyValue(name, item).ToDecimal();
returndata.kcalLevel = GetBodyValueLevel(name, item);
}
if (name == "vfal")
{
returndata.visceral = GetBodyValue(name, item).ToDecimal();
returndata.visceralLevel = GetBodyValueLevel(name, item);
}
if (name == "bodyage")
returndata.bodyage = GetBodyValue(name, item).ToInt();
if (name == "bodyscore")
returndata.cmi = GetBodyValue(name, item).ToDecimal();
if (name == "bodytype")
returndata.body = GetBodyValue(name, item);
if (name == "idealweightkg")
returndata.idealweight = GetBodyValue(name, item).ToDecimal();
if (name == "bodyfatkg")
returndata.fat_w = GetBodyValue(name, item).ToDecimal();
if (name == "bodyfatfreemasskg")
returndata.lbm = GetBodyValue(name, item).ToDecimal();
if (name == "musclerate")
{
returndata.muscle = GetBodyValue(name, item).ToDecimal();
}
if (name == "bodyfatsubcutkg")
returndata.sfrval = GetBodyValue(name, item).ToDecimal();
if (name == "muscleratetrunk")
returndata.muscleratetrunk = GetBodyValue(name, item).ToDecimal();
if (name == "musclerateleftleg")
returndata.musclerateleftleg = GetBodyValue(name, item).ToDecimal();
if (name == "muscleraterightleg")
returndata.muscleraterightleg = GetBodyValue(name, item).ToDecimal();
if (name == "musclerateleftarm")
returndata.musclerateleftarm = GetBodyValue(name, item).ToDecimal();
if (name == "muscleraterightarm")
returndata.muscleraterightarm = GetBodyValue(name, item).ToDecimal();
if (name == "bodyfatratetrunk")
returndata.bodyfatraterunk = GetBodyValue(name, item).ToDecimal();
if (name == "bodyfatrateleftleg")
returndata.bodyfatrateleftleg = GetBodyValue(name, item).ToDecimal();
if (name == "bodyfatraterightleg")
returndata.bodyfatraterightleg = GetBodyValue(name, item).ToDecimal();
if (name == "bodyfatrateleftarm")
returndata.bodyfatrateleftarm = GetBodyValue(name, item).ToDecimal();
if (name == "bodyfatraterightarm")
returndata.bodyfatraterightarm = GetBodyValue(name, item).ToDecimal();
if (name == "musclekgtrunk")
returndata.musclekgtrunk = GetBodyValue(name, item).ToDecimal();
if (name == "musclekgleftleg")
returndata.musclekgleftleg = GetBodyValue(name, item).ToDecimal();
if (name == "musclekgrightleg")
returndata.musclekgrightleg = GetBodyValue(name, item).ToDecimal();
if (name == "musclekgleftarm")
returndata.musclekgleftarm = GetBodyValue(name, item).ToDecimal();
if (name == "musclekgrightarm")
returndata.musclekgrightarm = GetBodyValue(name, item).ToDecimal();
if (name == "bodyfatkgtrunk")
returndata.bodyfatkgtrunk = GetBodyValue(name, item).ToDecimal();
if (name == "bodyfatkgleftleg")
returndata.bodyfatkgleftleg = GetBodyValue(name, item).ToDecimal();
if (name == "bodyfatkgrightleg")
returndata.bodyfatkgrightleg = GetBodyValue(name, item).ToDecimal();
if (name == "bodyfatkgleftarm")
returndata.bodyfatkgleftarm = GetBodyValue(name, item).ToDecimal();
if (name == "bodyfatkgrightarm")
returndata.bodyfatkgrightarm = GetBodyValue(name, item).ToDecimal();
}
//returndata.bmiLevel = bmi(returndata.bmi.ToFloat());
//returndata.muscleLevel = muscle(returndata.muscle.ToFloat(), returndata.weight.ToFloat(), age, sex);
//returndata.fat_rLevel = Fat_r(returndata.fat_r.ToFloat(), sex, age);
//returndata.fat_wLevel = Fat_r(returndata.fat_r.ToFloat(), sex, age);
//returndata.waterLevel = water(returndata.water.ToFloat(), returndata.weight.ToFloat(), age, sex);
//returndata.proteinLevel = protein(returndata.protein.ToFloat(), returndata.weight.ToFloat(), age, sex);
//returndata.boneLevel = bone(returndata.bone.ToFloat(), returndata.weight.ToFloat(), sex);
returndata.bodyageLevel = bodyage(returndata.bodyage.ToInt(), age);
//returndata.kcalLevel = kcal(returndata.kcal.ToFloat(), returndata.weight.ToFloat(), age, sex);
//returndata.visceralLevel = visceral(returndata.visceral.ToFloat(), returndata.weight.ToFloat(), age, sex);
//returndata.sfrLevel = sfr(returndata.sfr.ToFloat(), returndata.weight.ToFloat(), age, sex, height.ToInt());
var level = (weight - returndata.idealweight) / returndata.idealweight;
returndata.fatLevel = fatlevel(level.ToDouble());
return returndata;
}
}
else
{
//记录日志
loggerService.AddLogger($"八电极算法请求失败,参数:{requestdata.ToJson()},错误信息:{returnstr}");
//return new UserMeasureDto()
//{
// weight = weight,
// bmi= (((weight / (mheight * mheight)) * 10.0m) / 10.0m).ToDecimal(1)
//};
var returndata = new UserMeasureDto
{
weight = weight,
bmi = (((weight / (mheight * mheight)) * 10.0m) / 10.0m).ToDecimal(1),
idealweight = standweight(height.ToInt(), sex).ToDecimal(2),
height = height
};
returndata.bmiLevel = bmi(returndata.bmi.ToFloat());
returndata.cmi = score(returndata.bmi.ToFloat()).ToDecimal(1);
decimal levels = (weight - returndata.idealweight) / returndata.idealweight;
returndata.fatLevel = fatlevel(levels.ToDouble());
return returndata;
}
}
/// <summary>
/// 获取八电极每项值
/// </summary>
/// <param name="name"></param>
/// <param name="data"></param>
/// <returns></returns>
private static string GetBodyValue(string name, BodyResponseData data)
=> (name, data) switch
{
_ when name == "exerciseplanner" => data.value.ToString(), //训练计划
_ => data.value.ToString()
};
/// <summary>
/// 获取八电极每项值的标准
/// </summary>
/// <param name="name"></param>
/// <param name="data"></param>
/// <returns></returns>
private static string GetBodyValueLevel(string name, BodyResponseData data)
=> (name, data) switch
{
_ when name == "exerciseplanner" => "", //训练计划
_ => data.level.value.ToString().Length == 1 ? $"偏{data.level.value}" : data.level.value.ToString()
};
/// <summary>
/// 身体得分
/// </summary>
/// <param name="bmi"></param>
/// <returns></returns>
public static double score(float bmi)
=> bmi switch
{
_ when bmi < 21.6 => bmi / 21.6 * 100,
_ when bmi >= 21.6 => 21.6 / bmi * 100,
_ => 60
};
/// <summary>
/// 体型
/// </summary>
/// <param name="fat_r">体脂率</param>
/// <param name="muscle">肌肉率</param>
/// <returns></returns>
public static string body(string fat_r, string muscle)
=> (fat_r, muscle) switch
{
_ when fat_r == NewLevelModel.Error || muscle == NewLevelModel.Error => NewLevelModel.Error,
_ when (fat_r == NewLevelModel.high || fat_r == NewLevelModel.mhigh) && muscle == NewLevelModel.notenu => NewLevelModel.hidefat,
_ when (fat_r == NewLevelModel.high || fat_r == NewLevelModel.mhigh) && muscle == NewLevelModel.normal => NewLevelModel.fat,
_ when (fat_r == NewLevelModel.high || fat_r == NewLevelModel.mhigh) && muscle == NewLevelModel.fine => NewLevelModel.strongfat,
_ when (fat_r == NewLevelModel.normal) && muscle == NewLevelModel.notenu => NewLevelModel.notenumuscle,
_ when (fat_r == NewLevelModel.normal) && muscle == NewLevelModel.normal => NewLevelModel.normalfat,
_ when (fat_r == NewLevelModel.normal) && muscle == NewLevelModel.fine => NewLevelModel.normalmuscle,
_ when (fat_r == NewLevelModel.flat) && muscle == NewLevelModel.notenu => NewLevelModel.thin,
_ when (fat_r == NewLevelModel.flat) && muscle == NewLevelModel.normal => NewLevelModel.thinmuscle,
_ when (fat_r == NewLevelModel.flat) && muscle == NewLevelModel.fine => NewLevelModel.perfetmuscle,
_ => NewLevelModel.hidefat
};
/// <summary>
/// 肥胖等级
/// </summary>
/// <param name="level">level=(体重-标准体重)/标准体重</param>
/// <returns></returns>
public string fatlevel(double level)
=> level switch
{
_ when level < -0.2 => NewLevelModel.weightnotenu,
_ when level >= -0.2 && level < -0.1 => NewLevelModel.thin,
_ when level >= -0.1 && level <= 0.1 => NewLevelModel.normal,
_ when level > 0.1 && level <= 0.2 => NewLevelModel.weight,
_ when level > 0.2 => NewLevelModel.mweight,
_ => NewLevelModel.weightnotenu
};
/// <summary>
/// 皮下脂肪标准
/// </summary>
/// <param name="sfr"></param>
/// <param name="weight"></param>
/// <param name="age"></param>
/// <param name="sex"></param>
/// <param name="height"></param>
/// <returns></returns>
public static string sfr(float sfr, double weight, int age, int sex, int height)
=> (weight, age, sex, height) switch
{
_ when sfr <= 0 => NewLevelModel.Error,
_ when sex == 1 && sfr < 7 => NewLevelModel.notenu,
_ when sex == 0 && sfr < 11 => NewLevelModel.notenu,
_ when sex == 1 && sfr >= 7 && sfr < 15 => NewLevelModel.normal,
_ when sex == 0 && sfr >= 11 && sfr < 17 => NewLevelModel.normal,
_ when sex == 1 && sfr >= 15 => NewLevelModel.high,
_ when sex == 0 && sfr >= 17 => NewLevelModel.high,
_ => NewLevelModel.notenu
};
/// <summary>
/// 标准体重
/// </summary>
/// <param name="height"></param>
/// <param name="sex"></param>
/// <returns></returns>
public double standweight(int height, int sex)
=> (height, sex) switch
{
_ when sex == 1 => (height - 80) * 0.7,
_ => (height - 70) * 0.6
};
/// <summary>
/// 体重标准
/// </summary>
/// <param name="weight"></param>
/// <param name="age"></param>
/// <param name="sex"></param>
/// <param name="height"></param>
/// <returns></returns>
public static string weight(double weight, int age, int sex, int height)
=> (weight, age, sex, height) switch
{
_ when weight < 18.5 * Math.Pow(height / 100, 2) => NewLevelModel.thin,
_ when weight >= 18.5 * Math.Pow(height / 100, 2) && weight < 25 * Math.Pow(height / 100, 2) => NewLevelModel.normal,
_ when weight >= 25 * Math.Pow(height / 100, 2) && weight < 30 * Math.Pow(height / 100, 2) => NewLevelModel.fat,
_ when weight >= 30 * Math.Pow(height / 100, 2) => NewLevelModel.mfat,
_ => NewLevelModel.thin
};
/// <summary>
/// bmi标准
/// </summary>
/// <param name="bmi"></param>
/// <param name="weight"></param>
/// <param name="age"></param>
/// <param name="sex"></param>
/// <param name="height"></param>
/// <returns></returns>
public static string bmi(float bmi, double weight, int age, int sex, int height)
=> (weight, age, sex) switch
{
_ when bmi < 18.5 => NewLevelModel.thin,
_ when bmi >= 18.5 && bmi < 25 => NewLevelModel.normal,
_ when bmi >= 25 && bmi < 30 => NewLevelModel.fat,
_ when bmi >= 30 => NewLevelModel.mfat,
_ => NewLevelModel.thin
};
/// <summary>
/// 水份标准
/// </summary>
/// <param name="water"></param>
/// <param name="weight"></param>
/// <param name="age"></param>
/// <param name="sex"></param>
/// <returns></returns>
public static string water(float water, double weight, int age, int sex)
=> (water, weight, age, sex) switch
{
_ when water <= 0 => NewLevelModel.Error,
_ when sex == 1 && water < 55 => NewLevelModel.notenu,
_ when sex == 0 && water < 45 => NewLevelModel.notenu,
_ when sex == 1 && water < 65 && water >= 55 => NewLevelModel.normal,
_ when sex == 0 && water < 60 && water >= 45 => NewLevelModel.normal,
_ when sex == 1 && water >= 65 => NewLevelModel.fine,
_ when sex == 0 && water >= 60 => NewLevelModel.fine,
_ => NewLevelModel.notenu
};
/// <summary>
/// 内脂标准
/// </summary>
/// <param name="visceral"></param>
/// <param name="weight"></param>
/// <param name="age"></param>
/// <param name="sex"></param>
/// <returns></returns>
public static string visceral(float visceral, double weight, int age, int sex)
=> (visceral, weight, age, sex) switch
{
_ when visceral <= 0 => NewLevelModel.Error,
_ when visceral < 9 && visceral > 0 => NewLevelModel.normal,
_ when visceral < 14 && visceral >= 9 => NewLevelModel.alert,
_ when visceral >= 14 => NewLevelModel.danger,
_ => NewLevelModel.Error
};
/// <summary>
/// 蛋白质标准
/// </summary>
/// <param name="protein"></param>
/// <param name="weight"></param>
/// <param name="age"></param>
/// <param name="sex"></param>
/// <returns></returns>
public static string protein(float protein, double weight, int age, int sex)
=> (protein, weight, age, sex) switch
{
_ when protein <= 0 => NewLevelModel.Error,
_ when sex == 1 && protein < 16 => NewLevelModel.notenu,
_ when sex == 0 && protein < 14 => NewLevelModel.notenu,
_ when sex == 1 && protein >= 16 && protein < 18 => NewLevelModel.normal,
_ when sex == 0 && protein >= 14 && protein < 16 => NewLevelModel.normal,
_ when sex == 1 && protein >= 18 => NewLevelModel.fine,
_ when sex == 0 && protein >= 16 => NewLevelModel.fine,
_ => NewLevelModel.notenu
};
/// <summary>
/// 肌肉率标准
/// </summary>
/// <param name="muscle"></param>
/// <param name="weight"></param>
/// <param name="age"></param>
/// <param name="sex"></param>
/// <returns></returns>
public static string muscle(float muscle, double weight, int age, int sex)
=> (muscle, weight, age, sex) switch
{
_ when muscle <= 0 => NewLevelModel.Error,
_ when sex == 1 && muscle < 40 => NewLevelModel.notenu,
_ when sex == 0 && muscle < 30 => NewLevelModel.notenu,
_ when sex == 1 && muscle >= 40 && muscle < 60 => NewLevelModel.normal,
_ when sex == 0 && muscle >= 30 && muscle < 50 => NewLevelModel.normal,
_ when sex == 1 && muscle >= 60 => NewLevelModel.fine,
_ when sex == 0 && muscle >= 50 => NewLevelModel.fine,
_ => NewLevelModel.notenu
};
/// <summary>
/// 基础代谢计算标准
/// </summary>
/// <param name="kcal"></param>
/// <param name="weight"></param>
/// <param name="age"></param>
/// <param name="sex"></param>
/// <returns></returns>
public static string kcal(float kcal, double weight, int age, int sex)
=> (kcal, weight, age, sex) switch
{
_ when kcal <= 0 => NewLevelModel.Error,
_ when age > 0 && age < 3 && ((kcal < (60.9 * weight - 54) && sex == 1) || (kcal < (61.0 * weight - 51) && sex == 0)) => NewLevelModel.flat,
_ when age > 0 && age < 3 && ((kcal >= (60.9 * weight - 54) && sex == 1) || (kcal >= (61.0 * weight - 51) && sex == 0)) => NewLevelModel.fine,
_ when age >= 3 && age < 10 && ((kcal < (22.7 * weight + 495) && sex == 1) || (kcal < (22.5 * weight + 499) && sex == 0)) => NewLevelModel.flat,
_ when age >= 3 && age < 10 && ((kcal >= (22.7 * weight + 495) && sex == 1) || (kcal >= (22.5 * weight + 499) && sex == 0)) => NewLevelModel.fine,
_ when age >= 10 && age < 18 && ((kcal < (17.5 * weight + 651) && sex == 1) || (kcal < (12.2 * weight + 746) && sex == 0)) => NewLevelModel.flat,
_ when age >= 10 && age < 18 && ((kcal >= (17.5 * weight + 651) && sex == 1) || (kcal >= (12.2 * weight + 746) && sex == 0)) => NewLevelModel.fine,
_ when age >= 18 && age < 30 && ((kcal < (15.3 * weight + 679) && sex == 1) || (kcal < (14.7 * weight + 496) && sex == 0)) => NewLevelModel.flat,
_ when age >= 18 && age < 30 && ((kcal >= (15.3 * weight + 679) && sex == 1) || (kcal >= (14.7 * weight + 496) && sex == 0)) => NewLevelModel.fine,
_ when age >= 30 && ((kcal < (11.6 * weight + 879) && sex == 1) || (kcal < (8.7 * weight + 820) && sex == 0)) => NewLevelModel.flat,
_ when age >= 30 && ((kcal >= (11.6 * weight + 879) && sex == 1) || (kcal >= (8.7 * weight + 820) && sex == 0)) => NewLevelModel.fine,
_ => NewLevelModel.flat
};
/// <summary>
/// 骨重标准
/// </summary>
/// <param name="bone">骨重</param>
/// <param name="weight"></param>
/// <param name="sex"></param>
/// <returns></returns>
public static string bone(float bone, double weight, int sex)
=> (bone, weight, sex) switch
{
_ when bone <= 0 => NewLevelModel.Error,
_ when sex == 1 && ((weight < 60 && bone < 2.4) || (weight >= 60 && weight < 75 && bone < 2.8) || (weight >= 75 && bone < 3.1)) => NewLevelModel.notenu,
_ when sex == 0 && ((weight < 45 && bone < 1.7) || (weight >= 45 && weight < 60 && bone < 2.1) || (weight >= 60 && bone < 2.4)) => NewLevelModel.notenu,
_ when sex == 1 && ((weight < 60 && bone >= 2.4 && bone <= 2.6) || (weight >= 60 && weight < 75 && bone >= 2.8 && bone <= 30) || (weight >= 75 && bone >= 3.1 && bone <= 3.3)) => NewLevelModel.normal,
_ when sex == 0 && ((weight < 45 && bone >= 1.7 && bone <= 1.9) || (weight >= 45 && weight < 60 && bone <= 2.3 && bone >= 2.1) || (weight >= 60 && bone <= 2.6 && bone >= 2.4)) => NewLevelModel.normal,
_ when sex == 1 && ((weight < 60 && bone > 2.6) || (weight >= 60 && weight < 75 && bone > 3.0) || (weight >= 75 && bone > 3.3)) => NewLevelModel.fine,
_ when sex == 0 && ((weight < 45 && bone > 1.9) || (weight >= 45 && weight < 60 && bone > 2.3) || (weight >= 60 && bone > 2.6)) => NewLevelModel.fine,
_ => NewLevelModel.notenu
};
/// <summary>
/// 体龄计算方法
/// </summary>
/// <param name="bodyage"></param>
/// <param name="age"></param>
/// <returns></returns>
public static string bodyage(int bodyage, int age)
=> (bodyage, age) switch
{
_ when bodyage > age => NewLevelModel.big,
_ when bodyage == age => NewLevelModel.normal,
_ when bodyage <= 0 => NewLevelModel.Error,
_ => NewLevelModel.fine
};
/// <summary>
/// bmi 计算标准
/// </summary>
/// <param name="bmi">BMI</param>
/// <returns></returns>
public static string bmi(float bmi)
=> bmi switch
{
_ when bmi <= 0 => NewLevelModel.Error,
_ when bmi < 18.5 => NewLevelModel.thin,
_ when bmi >= 18.5 && bmi < 25 => NewLevelModel.normal,
_ when bmi >= 25 && bmi < 30 => NewLevelModel.fat,
_ when bmi >= 30 => NewLevelModel.mfat,
_ => NewLevelModel.thin
};
/// <summary>
/// 脂肪率标准计算
/// </summary>
/// <param name="fat_r">脂肪率</param>
/// <param name="sex">0-女,1-男</param>
/// <param name="age">年龄</param>
/// <returns></returns>
public static string Fat_r(float fat_r, int sex, int age)
=> (fat_r, sex, age) switch
{
_ when fat_r <= 0 => NewLevelModel.Error,
_ when sex == 1 && age < 30 && fat_r < 10 => NewLevelModel.flat,
_ when sex == 1 && age >= 30 && fat_r < 11 => NewLevelModel.flat,
_ when sex == 0 && age < 30 && fat_r < 20 => NewLevelModel.flat,
_ when sex == 0 && age >= 30 && fat_r < 21 => NewLevelModel.flat,
_ when sex == 1 && age < 30 && fat_r >= 10 && fat_r < 21 => NewLevelModel.normal,
_ when sex == 1 && age >= 30 && fat_r >= 11 && fat_r < 22 => NewLevelModel.normal,
_ when sex == 0 && age < 30 && fat_r >= 20 && fat_r < 31 => NewLevelModel.normal,
_ when sex == 0 && age >= 30 && fat_r >= 21 && fat_r < 32 => NewLevelModel.normal,
_ when sex == 1 && age < 30 && fat_r > 21 && fat_r < 26 => NewLevelModel.high,
_ when sex == 1 && age >= 30 && fat_r >= 22 && fat_r < 27 => NewLevelModel.high,
_ when sex == 0 && age < 30 && fat_r >= 31 && fat_r < 38 => NewLevelModel.high,
_ when sex == 0 && age >= 30 && fat_r >= 32 && fat_r < 39 => NewLevelModel.high,
_ when sex == 1 && age < 30 && fat_r >= 26 => NewLevelModel.mhigh,
_ when sex == 1 && age >= 30 && fat_r >= 27 => NewLevelModel.mhigh,
_ when sex == 0 && age < 30 && fat_r >= 38 => NewLevelModel.mhigh,
_ when sex == 0 && age >= 30 && fat_r >= 39 => NewLevelModel.mhigh,
_ => NewLevelModel.flat
};
/// <summary>
/// 新的指标标准
/// </summary>
public class NewLevelModel
{
/// <summary>
/// 异常值
/// </summary>
public static string Error = "异常";
/// <summary>
/// 偏大
/// </summary>
public static string big = "偏大";
/// <summary>
/// 偏瘦
/// </summary>
public static string thin = "偏瘦";
/// <summary>
/// 标准
/// </summary>
public static string normal = "标准";
/// <summary>
/// 偏胖
/// </summary>
public static string fat = "偏胖";
/// <summary>
/// 肥胖
/// </summary>
public static string mfat = "肥胖";
/// <summary>
/// 偏重
/// </summary>
public static string weight = "偏重";
/// <summary>
/// 超重
/// </summary>
public static string mweight = "超重";
/// <summary>
/// 不足
/// </summary>
public static string notenu = "不足";
/// <summary>
/// 体重不足
/// </summary>
public static string weightnotenu = "体重不足";
/// <summary>
/// 偏高
/// </summary>
public static string high = "偏高";
/// <summary>
/// 高
/// </summary>
public static string mhigh = "超高";
/// <summary>
/// 警惕
/// </summary>
public static string alert = "警惕";
/// <summary>
/// 危险
/// </summary>
public static string danger = "危险";
/// <summary>
/// 偏低
/// </summary>
public static string flat = "偏低";
/// <summary>
/// 优
/// </summary>
public static string fine = "优秀";
/// <summary>
/// 隐形肥胖
/// </summary>
public static string hidefat = "隐形肥胖";
/// <summary>
/// 结实型偏胖
/// </summary>
public static string strongfat = "结实型偏胖";
/// <summary>
/// 缺乏肌肉型
/// </summary>
public static string notenumuscle = "缺乏肌肉型";
/// <summary>
/// 标准型
/// </summary>
public static string normalfat = "标准型";
/// <summary>
/// 标准肌肉型
/// </summary>
public static string normalmuscle = "标准肌肉型";
/// <summary>
/// 偏瘦肌肉型
/// </summary>
public static string thinmuscle = "偏瘦肌肉型";
/// <summary>
/// 健美肌肉型
/// </summary>
public static string perfetmuscle = "健美肌肉型";
}
/// <summary>
/// 算法返回结果定义
/// </summary>
public class BodyfatItem
{
/// <summary>
/// bmi
/// </summary>
public string bmi;
/// <summary>
/// 骨量
/// </summary>
public string bm;
/// <summary>
/// 肌肉率
/// </summary>
public string rom;
/// <summary>
/// 水分
/// </summary>
public string moi;
/// <summary>
/// 脂肪率
/// </summary>
public string bfr;
/// <summary>
/// 皮下脂肪率
/// </summary>
public string sfr;
/// <summary>
/// 骨骼肌率
/// </summary>
public string rosm;
/// <summary>
/// 蛋白率
/// </summary>
public string pp;
/// <summary>
/// 内脏脂肪指数
/// </summary>
public string uvi;
/// <summary>
/// 基础代谢率
/// </summary>
public string bmr;
/// <summary>
/// 身体年龄
/// </summary>
public string physicAge;
/// <summary>
/// 体重
/// </summary>
public double weight;
/// <summary>
/// 身高
/// </summary>
public double height;
/// <summary>
/// 年龄
/// </summary>
public int age;
/// <summary>
/// 阻抗
/// </summary>
public int adc;
/// <summary>
/// 性别,0-女,1-男
/// </summary>
public int sex;
}
/// <summary>
/// 盛群八电极算法返回的结果
/// </summary>
public class HTBodyFatReturnData : HTBodyFatReturnItem
{
/// <summary>
/// 返回状态
/// GetBodyFatOk(所有参数有效)
/// UserZTwoLegsError/UserAgeError/UserGenderError(除BMI及李想体重外都无效)(除BMI及理想体重外都无效)
/// UserWeightError/UserHeightError(所有参数无效)
/// </summary>
public string ErrorType { get; set; }
/// <summary>
/// 体脂算法版本号,两位小数
/// </summary>
public decimal VersionNumber { get; set; }
/// <summary>
/// 更新信息
/// </summary>
public string UpdateInfo { get; set; } = "";
/// <summary>
/// 脚对脚阻抗值,范围200.0-1200.0
/// </summary>
public decimal ZTwoLegs { get; set; }
}
/// <summary>
/// 盛群八电极算法返回的测量结果
/// </summary>
public class HTBodyFatReturnItem
{
/// <summary>
/// 理想体重,若体重或身高参数有误,则返回0
/// </summary>
public decimal IdealWeightKg { get; set; }
/// <summary>
/// 人体质量指数,分辨率0.1范围10.0-90.0
/// </summary>
public decimal BMI { get; set; }
/// <summary>
/// 人体质量标准,有三个uansu,分别对应,瘦-普通,普通-偏胖,偏胖-肥胖的分界值
/// </summary>
public List<decimal> BMIRatingList { get; set; }
/// <summary>
/// 基础代谢,分辨率1,范围500-10000
/// </summary>
public int BMR { get; set; }
/// <summary>
/// 基础代谢健康标准,需先调用BMR(),一个元素,对应"偏低-达标"的分界值
/// </summary>
public List<int> BMRRatingList { get; set; }
/// <summary>
/// 脂肪率(%), 分辨率0.1, 范围5.0% ~ 75.0%
/// </summary>
public decimal BodyfatPercentage { get; set; }
/// <summary>
/// 脂肪率健康标准需先调用BodyfatPercentage(),返回的数组有四个元素,对应"偏瘦-标准"、“标准-警惕”、“警惕-偏胖”、“偏胖-肥胖”的分界值
/// </summary>
public List<decimal> BodyfatRatingList { get; set; }
/// <summary>
/// 骨量(kg), 分辨率0.1, 范围0.5 ~ 8.0
/// </summary>
public decimal BoneKg { get; set; }
/// <summary>
/// 骨量等级标准,需先调用BoneKg(),两个元素,对应"不足-标准"、“标准-优秀”的分界值
/// </summary>
public List<decimal> BoneRatingList { get; set; }
/// <summary>
/// 肌肉量(kg), 分辨率0.1, 范围10.0 ~ 120.0
/// </summary>
public decimal Muscle { get; set; }
/// <summary>
/// 肌肉量健康标准需先调用Muscle(),两个元素,对应"不足-标准"、“标准-优秀”的分界值
/// </summary>
public List<decimal> MuscleRatingList { get; set; }
/// <summary>
/// 内脏脂肪, 分辨率1, 范围1 ~ 6
/// </summary>
public int VFAL { get; set; }
/// <summary>
/// 内脏脂肪等级标准,两个元素,对应"标准-警惕"、"警惕-危险"的分界值
/// </summary>
public List<int> VFALRatingList { get; set; }
/// <summary>
/// 水分率(%), 分辨率0.1, 范围35.0% ~ 75.0%
/// </summary>
public decimal WaterPercentage { get; set; }
/// <summary>
/// 水分率健康标准,两个元素,对应"不足-标准"、“标准-优秀”
/// </summary>
public List<decimal> WaterRatingList { get; set; }
/// <summary>
/// 体龄
/// </summary>
public int BodyAge { get; set; }
/// <summary>
/// ProteinPercentage
/// </summary>
public decimal ProteinPercentage { get; set; }
/// <summary>
/// 蛋白质健康标准,两个元素,对应"不足-标准"、“标准-优秀”
/// </summary>
public List<decimal> ProteinRatingList { get; set; }
}
/// <summary>
/// 计算算法
/// </summary>
/// <param name="weight"></param>
/// <param name="height"></param>
/// <param name="age"></param>
/// <param name="adc"></param>
/// <param name="sex"></param>
/// <returns></returns>
public static BodyfatItem GetBodyfatResults(double weight, double height, int age, int adc, int sex)
{
BodyfatItem item = new BodyfatItem();
if ((weight <= 0.0) || ((weight > 220.0) || ((height <= 0.0) || ((height > 270.0) || ((age <= 0) || ((age > 120) || ((adc <= 0) || ((adc > 0x3e8) || ((sex < 0) || (sex > 1))))))))))
{
object[] arg = new object[] { weight, height, age, adc, sex };
Console.WriteLine("[input params invalid] weight:{0} height:{1} age:{2} adc:{3} sex:{4}", arg);
if (weight != 0 && height != 0)
item.bmi = (((double)((int)((weight / (height * height)) * 10.0))) / 10.0).ToString("0.0");
return item;
}
double num = ((double)((int)((weight / (height * height)) * 10.0))) / 10.0;
double num2 = 0.0;
double num3 = 0.0;
double num4 = 0.0;
double num5 = 0.0;
double num6 = 0.0;
double num7 = 0.0;
double num8 = 0.0;
double num9 = 0.0;
double num10 = 0.0;
double num11 = 0.0;
if (sex == 1)
{
num2 = ((0.015 * weight) + (((2.0 - (0.00055 * adc)) * height) / 100.0)) + 1.15;
num3 = ((((-((0.00115 * adc) + 0.01) * weight) + (((49.64 - (0.031 * adc)) * height) / 100.0)) + (adc * 0.08)) + (age * 0.04)) + 15.4;
num4 = (((0xf_4240 / (num * ((2.688 * adc) - 78.28))) - (0x274a / adc)) - (0.22 * age)) + 52.6;
num5 = ((((-930_000.0 / num) / ((1.966 * adc) - 58.46)) + (0x3378 / adc)) - (0.06 * age)) + 40.0;
num6 = 0.898 * num5;
num7 = 0.895 * num4;
num8 = 0.8 * (((100.0 - num5) - num4) - (num2 / weight));
num9 = ((((0.304 * weight) + ((25.58 * height) / 100.0)) + (0.131 * age)) + (0.005 * adc)) - 0x16;
//num10 = (((((9.0 + (0.0015 * adc)) * weight) + (((0x546 - (0.88 * adc)) * height) / 100.0)) + (0xbc / age)) + (0.748 * adc)) - 0x41d;
num10 = 370 + 21.6 * weight * (1 - num5 / 100);
num11 = ((((age * (1.0 + (0.012 * (num - 1.0)))) - 0x15) + ((30 - age) * 0.35)) + ((adc - 450) * 0.02)) + 11.0;
}
else
{
num2 = ((((2.2E-05 * adc) * weight) + (((4.99 - (0.00284 * adc)) * height) / 100.0)) + (0.0012 * adc)) + 1.15;
num3 = ((((-((0.00115 * adc) + 0.01) * weight) + (((49.64 - (0.031 * adc)) * height) / 100.0)) + (adc * 0.08)) + (age * 0.04)) + 6.0;
num4 = (((0xf_4240 / (num * ((2.467 * adc) - 75.37))) - (0x3787 / adc)) - (0.034 * age)) + 43.2;
num5 = ((((-3_030_000.0 / (num + 20.0)) / ((1.966 * adc) - 58.46)) + (0x6e10 / adc)) - (0.06 * age)) + 0x33;
num6 = (0.876 * num5) + 1.66;
num7 = (0.857 * num4) - 0.36;
num8 = 0.75 * (((100.0 - num5) - num4) - (num2 / weight));
num9 = ((((0.304 * weight) + ((25.58 * height) / 100.0)) + (0.131 * age)) + (0.005 * adc)) - 0x16;
num10 = 370 + 21.6 * weight * (1 - num5 / 100);
//num10 = ((((((0.00307 * adc) + 1.5) * weight) + (((0x5b3 - (0.989 * adc)) * height) / 100.0)) + (age * 0.9)) + (0.923 * adc)) - 950.0;
num11 = (age * (0.95 + (0.02 * (num - 21.2)))) + ((adc - 500) * 0.02);
}
item.bmi = num.ToString("0.0");
num2 = (num2 > (weight * 0.15)) ? (weight * 0.15) : num2;
item.bm = ((num2 < (weight * 0.02)) ? (weight * 0.02) : num2).ToString("0.0");
num3 = (num3 > 0x4b) ? 0x4b : num3;
item.rom = ((num3 < 15.0) ? 15.0 : num3).ToString("0.0");
num4 = (num4 > 70.0) ? 70.0 : num4;
item.moi = ((num4 < 20.0) ? 20.0 : num4).ToString("0.0");
num5 = (num5 > 50.0) ? 50.0 : num5;
item.bfr = ((num5 < 5.0) ? 5.0 : num5).ToString("0.0");
item.sfr = num6.ToString("0.0");
item.rosm = num7.ToString("0.0");
num8 = (num8 > 50.0) ? 50.0 : num8;
item.pp = ((num8 < 10.0) ? 10.0 : num8).ToString("0.0");
num9 = (num9 > 20.0) ? 20.0 : num9;
item.uvi = ((num9 < 1.0) ? 1.0 : num9).ToString("0");
item.bmr = num10.ToString("0.0");
if (age < 0x12)
{
num11 = age;
}
else
{
num11 = (num11 > (age + 10)) ? ((double)(age + 10)) : num11;
num11 = (num11 < (age - 10)) ? ((double)(age - 10)) : num11;
}
item.physicAge = num11.ToString("0");
item.weight = weight;
item.height = height;
item.age = age;
item.adc = adc;
item.sex = sex;
return item;
}
/// <summary>
/// 盛群八电极算法
/// </summary>
/// <param name="weight">体重,KG</param>
/// <param name="height">身高,CM</param>
/// <param name="age">年龄,岁</param>
/// <param name="sex">性别,1-男,2-女,0-未知</param>
/// <param name="ohm">HT_MCU上传的加密电阻值</param>
/// <returns></returns>
public async Task<HTBodyFatReturnItem> HTBodyFatResultAsync(double weight, double height, int age, int sex, int ohm)
{
var _loggerservice = App.GetService<ILoggerService>();
//算法使用的性别为,0-女,1-男
sex = sex == 1 ? 1 : 0;
var url = "http://123.57.232.130:100/HTBodyFatTwoLegs.ashx";
var request = new HttpRequestMessage(HttpMethod.Post, url);
var data = new
{
UserWeightKg = weight,
UserHeightcm = height,
UserAge = age,
UserGender = sex,
UserZTwoLegsohm = ohm
};
var postdata = data.ToJson();
request.Content = new StringContent(postdata, Encoding.UTF8, "application/json");
var client = _clientFactory.CreateClient();
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
if (result.ToLower().Contains("errortype"))
{
var jsondata = result.ToObject<HTBodyFatReturnData>();
return jsondata;
}
_loggerservice.AddLogger($"八电极算法请求成功,参数:{postdata}\r\n地址:{url}\r\n返回值:{result}");
return null;
}
else
{
_loggerservice.AddLogger($"八电极算法请求失败,参数:{postdata}\r\n地址:{url}\r\n返回值:{response.ToJson()}");
return null;
}
}
/// <summary>
/// bmi范围标准
/// </summary>
/// <param name="sex"></param>
/// <param name="age"></param>
/// <returns></returns>
public List<MeasureInfoItemValue> bmi_value(int sex, int age)
{
var list = new List<MeasureInfoItemValue>();
list.Add(new MeasureInfoItemValue
{
maxvalue = 18.5m,
minvalue = 0
});
list.Add(new MeasureInfoItemValue
{
maxvalue = 25,
minvalue = 18.5m
});
list.Add(new MeasureInfoItemValue
{
maxvalue = 30,
minvalue = 25
});
list.Add(new MeasureInfoItemValue
{
maxvalue = 50,
minvalue = 30
});
return list;
}
/// <summary>
/// 肌肉重量标准范围
/// </summary>
/// <param name="sex">性别</param>
/// <param name="age">年龄</param>
/// <param name="weight">重量,kg</param>
/// <returns></returns>
public List<MeasureInfoItemValue> muscleval_value(int sex, int age, decimal weight)
{
var list = new List<MeasureInfoItemValue>();
var values = muscle_value(sex, age);
values.ForEach(x => {
list.Add(new MeasureInfoItemValue
{
maxvalue = ((x.maxvalue / 100.0m) * weight).ToDecimal(1),
minvalue = ((x.minvalue / 100.0m) * weight).ToDecimal(1),
});
});
return list;
}
/// <summary>
/// 脂肪重量标准范围
/// </summary>
/// <param name="sex">性别</param>
/// <param name="age">年龄</param>
/// <param name="weight">重量,kg</param>
/// <returns></returns>
public List<MeasureInfoItemValue> fat_w_value(int sex, int age, decimal weight)
{
var list = new List<MeasureInfoItemValue>();
var values = fa_r_value(sex, age);
values.ForEach(x => {
list.Add(new MeasureInfoItemValue {
maxvalue = ((x.maxvalue/100.0m)*weight).ToDecimal(1),
minvalue = ((x.minvalue / 100.0m) * weight).ToDecimal(1),
});
});
return list;
}
/// <summary>
/// 脂肪率/体脂率范围标准
/// </summary>
/// <param name="sex"></param>
/// <param name="age"></param>
/// <returns></returns>
public List<MeasureInfoItemValue> fa_r_value(int sex, int age)
=> (sex, age) switch
{
_ when sex == 1 && age < 30 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
maxvalue=10,
minvalue=0
},
new MeasureInfoItemValue
{
maxvalue=21,
minvalue=10
},
new MeasureInfoItemValue
{
maxvalue=26,
minvalue=21
},
new MeasureInfoItemValue
{
maxvalue=50,
minvalue=26
}
},
_ when sex == 1 && age >= 30 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
maxvalue=11,
minvalue=0
},
new MeasureInfoItemValue
{
maxvalue=22,
minvalue=11
},
new MeasureInfoItemValue
{
maxvalue=27,
minvalue=22
},
new MeasureInfoItemValue
{
maxvalue=50,
minvalue=27
}
},
_ when sex == 0 && age < 30 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
maxvalue=20,
minvalue=0
},
new MeasureInfoItemValue
{
maxvalue=31,
minvalue=20
},
new MeasureInfoItemValue
{
maxvalue=38,
minvalue=31
},
new MeasureInfoItemValue
{
maxvalue=50,
minvalue=38
}
},
_ when sex == 0 && age >= 30 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
maxvalue=21,
minvalue=0
},
new MeasureInfoItemValue
{
maxvalue=32,
minvalue=21
},
new MeasureInfoItemValue
{
maxvalue=39,
minvalue=32
},
new MeasureInfoItemValue
{
maxvalue=50,
minvalue=39
}
},
_ => new List<MeasureInfoItemValue>()
};
/// <summary>
/// 肌肉率范围
/// </summary>
/// <param name="sex"></param>
/// <param name="age"></param>
/// <returns></returns>
public List<MeasureInfoItemValue> muscle_value(int sex, int age)
=> (sex, age) switch
{
_ when sex == 1 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
maxvalue=40,
minvalue=0
},
new MeasureInfoItemValue
{
maxvalue=60,
minvalue=40
},
new MeasureInfoItemValue
{
maxvalue=80,
minvalue=60
}
},
_ when sex == 0 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
maxvalue=30,
minvalue=0
},
new MeasureInfoItemValue
{
maxvalue=50,
minvalue=30
},
new MeasureInfoItemValue
{
maxvalue=80,
minvalue=50
}
},
_ => new List<MeasureInfoItemValue>()
};
/// <summary>
/// 水份范围
/// </summary>
/// <param name="sex"></param>
/// <param name="age"></param>
/// <returns></returns>
public List<MeasureInfoItemValue> water_value(int sex, int age)
=> (sex, age) switch
{
_ when sex == 1 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
maxvalue=55,
minvalue=0
},
new MeasureInfoItemValue
{
maxvalue=65,
minvalue=55
},
new MeasureInfoItemValue
{
maxvalue=80,
minvalue=65
}
},
_ when sex == 0 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
maxvalue=45,
minvalue=0
},
new MeasureInfoItemValue
{
maxvalue=60,
minvalue=45
},
new MeasureInfoItemValue
{
maxvalue=80,
minvalue=60
}
},
_ => new List<MeasureInfoItemValue>()
};
/// <summary>
/// 骨量范围
/// </summary>
/// <param name="sex"></param>
/// <param name="age"></param>
/// <param name="weight">体重</param>
/// <returns></returns>
public List<MeasureInfoItemValue> bone_value(int sex, int age, decimal weight)
=> (sex, age, weight) switch
{
_ when sex == 1 && weight < 60 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
maxvalue=2.4m,
minvalue=0
},
new MeasureInfoItemValue
{
maxvalue=2.6m,
minvalue=2.4m
},
new MeasureInfoItemValue
{
maxvalue=5,
minvalue=2.6m
}
},
_ when sex == 1 && weight < 75 && weight >= 60 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
maxvalue=2.8m,
minvalue=0
},
new MeasureInfoItemValue
{
maxvalue=3.0m,
minvalue=2.8m
},
new MeasureInfoItemValue
{
maxvalue=5,
minvalue=3.0m
}
},
_ when sex == 1 && weight >= 75 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
maxvalue=3.1m,
minvalue=0
},
new MeasureInfoItemValue
{
maxvalue=3.3m,
minvalue=3.1m
},
new MeasureInfoItemValue
{
maxvalue=5,
minvalue=3.3m
}
},
_ when sex == 0 && weight < 45 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
maxvalue=1.7m,
minvalue=0
},
new MeasureInfoItemValue
{
maxvalue=1.9m,
minvalue=1.7m
},
new MeasureInfoItemValue
{
maxvalue=5,
minvalue=1.9m
}
},
_ when sex == 0 && weight < 60 && weight >= 45 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
maxvalue=2.1m,
minvalue=0
},
new MeasureInfoItemValue
{
maxvalue=2.3m,
minvalue=2.1m
},
new MeasureInfoItemValue
{
maxvalue=5,
minvalue=2.3m
}
},
_ when sex == 0 && weight >= 60 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
maxvalue=2.4m,
minvalue=0
},
new MeasureInfoItemValue
{
maxvalue=2.6m,
minvalue=2.4m
},
new MeasureInfoItemValue
{
maxvalue=5,
minvalue=2.6m
}
},
_ => new List<MeasureInfoItemValue>()
};
/// <summary>
/// 基础代谢范围
/// </summary>
/// <param name="sex"></param>
/// <param name="age"></param>
/// <param name="weight"></param>
/// <returns></returns>
public List<MeasureInfoItemValue> kcal_value(int sex, int age, decimal weight)
=> (sex, age, weight) switch
{
_ when sex == 1 && age < 3 && age > 0 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
minvalue = (60.9*weight.ToDouble()-54).ToDecimal(2),
maxvalue = (60.9*weight.ToDouble()-54).ToDecimal(2)
}
},
_ when sex == 1 && age < 10 && age >= 3 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
minvalue = (22.7*weight.ToDouble()+495).ToDecimal(2),
maxvalue = (22.7*weight.ToDouble()+495).ToDecimal(2),
}
},
_ when sex == 1 && age < 18 && age >= 10 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
minvalue = (17.5*weight.ToDouble()+651).ToDecimal(2),
maxvalue = (17.5*weight.ToDouble()+651).ToDecimal(2)
}
},
_ when sex == 1 && age < 30 && age >= 18 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
minvalue = (15.3*weight.ToDouble()+679).ToDecimal(2),
maxvalue = (15.3*weight.ToDouble()+679).ToDecimal(2)
}
},
_ when sex == 1 && age >= 30 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
minvalue = (11.6*weight.ToDouble()+879).ToDecimal(2),
maxvalue = (11.6*weight.ToDouble()+879).ToDecimal(2)
}
},
_ when sex == 0 && age < 3 && age > 0 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
minvalue = (61.0*weight.ToDouble()-51).ToDecimal(2),
maxvalue = (61.0*weight.ToDouble()-51).ToDecimal(2)
}
},
_ when sex == 0 && age < 10 && age >= 3 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
minvalue = (22.5*weight.ToDouble()+499).ToDecimal(2),
maxvalue = (22.5*weight.ToDouble()+499).ToDecimal(2),
}
},
_ when sex == 0 && age < 18 && age >= 10 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
minvalue = (12.2*weight.ToDouble()+746).ToDecimal(2),
maxvalue = (12.2*weight.ToDouble()+746).ToDecimal(2)
}
},
_ when sex == 0 && age < 30 && age >= 18 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
minvalue = (14.7*weight.ToDouble()+496).ToDecimal(2),
maxvalue = (14.7*weight.ToDouble()+496).ToDecimal(2)
}
},
_ when sex == 0 && age >= 30 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
minvalue = (8.7*weight.ToDouble()+820).ToDecimal(2),
maxvalue = (8.7*weight.ToDouble()+820).ToDecimal(2)
}
},
_ => new List<MeasureInfoItemValue>()
};
/// <summary>
/// 内脂范围
/// </summary>
/// <param name="sex"></param>
/// <param name="age"></param>
/// <returns></returns>
public List<MeasureInfoItemValue> visceral_value(int sex, int age)
=> (sex, age) switch
{
_ => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
minvalue=0,
maxvalue=9
},
new MeasureInfoItemValue
{
minvalue=9,
maxvalue=14
},
new MeasureInfoItemValue
{
minvalue=14,
maxvalue=20
}
}
};
/// <summary>
/// 蛋白量标准范围
/// </summary>
/// <param name="sex"></param>
/// <param name="age"></param>
/// <param name="weight"></param>
/// <returns></returns>
public List<MeasureInfoItemValue> proteinval_value(int sex,int age,decimal weight)
{
var list = new List<MeasureInfoItemValue>();
var values = protein_value(sex, age);
values.ForEach(x => {
list.Add(new MeasureInfoItemValue
{
maxvalue = ((x.maxvalue / 100.0m) * weight).ToDecimal(1),
minvalue = ((x.minvalue / 100.0m) * weight).ToDecimal(1),
});
});
return list;
}
/// <summary>
/// 蛋白质范围
/// </summary>
/// <param name="sex"></param>
/// <param name="age"></param>
/// <returns></returns>
public List<MeasureInfoItemValue> protein_value(int sex, int age)
=> (sex, age) switch
{
_ when sex == 1 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
minvalue=0,
maxvalue=16
},
new MeasureInfoItemValue
{
minvalue=16,
maxvalue=18
},
new MeasureInfoItemValue
{
minvalue=18,
maxvalue=25
}
},
_ when sex == 0 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
minvalue=0,
maxvalue=14
},
new MeasureInfoItemValue
{
minvalue=14,
maxvalue=16
},
new MeasureInfoItemValue
{
minvalue=16,
maxvalue=25
}
},
_ => new List<MeasureInfoItemValue>()
};
/// <summary>
/// 皮下脂肪范围
/// </summary>
/// <param name="sex"></param>
/// <param name="age"></param>
/// <returns></returns>
public List<MeasureInfoItemValue> sfr_value(int sex, int age)
=> (sex, age) switch
{
_ when sex == 1 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
minvalue=0,
maxvalue=7
},
new MeasureInfoItemValue
{
minvalue=7,
maxvalue=15
},
new MeasureInfoItemValue
{
minvalue=15,
maxvalue=25
}
},
_ when sex == 0 => new List<MeasureInfoItemValue> {
new MeasureInfoItemValue
{
minvalue=0,
maxvalue=11
},
new MeasureInfoItemValue
{
minvalue=11,
maxvalue=17
},
new MeasureInfoItemValue
{
minvalue=17,
maxvalue=25
}
},
_ => new List<MeasureInfoItemValue>()
};
}
}