using Furion;
using Furion.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YB.DeviceStand.Domain;
using YB.DeviceStand.Domain.Shared;
namespace YB.DeviceStand.Application.BodyFat
{
///
/// 体脂计算算法
///
public class BodyFatHelperService : IBodyFatHelperService, ITransient
{
private readonly IHttp _http;
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(IHttp http)
{
_http = http;
}
///
/// 2/4电极体脂计算
///
///
///
public BodyFat4CalcResultS2SDto Calc4BodyFat(BodyFat4CalcS2SDto data)
{
//算法使用的性别为0-女,1-男
int sex = data.sex == UserSex.Female ? 0 : 1;
double weight = (double)data.weight;
double height = (double)data.height;
int adc = (int)data.adc;
var result = GetBodyfatResults(weight, height, data.age, adc, sex);
}
///
/// 8电极体脂计算
///
///
///
public Task CalcBody120FatAsync(BodyFat8CalcS2SDto data)
{
}
///
/// 计算算法
///
///
///
///
///
///
///
private BodyfatItem GetBodyfatResults(double weight, double height, int age, int adc, int sex)
{
BodyfatItem bodyfatItem = new BodyfatItem();
double mheight = height / 100;//米为单位的身高
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 };
if (weight != 0 && height != 0)
bodyfatItem.bmi = (((double)((int)((weight / (mheight * mheight)) * 10.0))) / 10.0).ToString("0.0");
return bodyfatItem;
}
double num = (double)(int)(weight / (mheight * mheight) * 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 * (double)adc) * height / 100.0 - 1.15;
num3 = (0.0 - (0.00115 * (double)adc + 0.01)) * weight + (49.64 - 0.031 * (double)adc) * height / 100.0 + (double)adc * 0.08 + (double)age * 0.04 + 15.4;
num4 = 1000000.0 / (num * (2.688 * (double)adc - 78.28)) - (double)(10058 / adc) - 0.22 * (double)age + 52.6;
num5 = -930000.0 / num / (1.966 * (double)adc - 58.46) + (double)(13176 / adc) - 0.06 * (double)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 * (double)age + 0.005 * (double)adc + 22.0;
num10 = (9.0 + 0.0015 * (double)adc) * weight + (1350.0 - 0.88 * (double)adc) * height / 100.0 + (double)(188 / age) + 0.748 * (double)adc - 1053.0;
num11 = (double)age * (1.0 + 0.012 * (num - 1.0)) - 21.0 + (double)(30 - age) * 0.35 + (double)(adc - 450) * 0.02 + 11.0;
}
else
{
num2 = 2.2E-05 * (double)adc * weight + (4.99 - 0.00284 * (double)adc) * height / 100.0 + 0.0012 * (double)adc - 4.45;
num3 = (0.0 - (0.00115 * (double)adc + 0.01)) * weight + (49.64 - 0.031 * (double)adc) * height / 100.0 + (double)adc * 0.08 + (double)age * 0.04 + 6.0;
num4 = 1000000.0 / (num * (2.467 * (double)adc - 75.37)) - (double)(14215 / adc) - 0.034 * (double)age + 43.2;
num5 = -3030000.0 / (num + 20.0) / (1.966 * (double)adc - 58.46) + (double)(28176 / adc) - 0.06 * (double)age + 51.0;
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 * (double)age + 0.005 * (double)adc + 22.0;
num10 = (0.00307 * (double)adc + 1.5) * weight + (1459.0 - 0.989 * (double)adc) * height / 100.0 + (double)age * 0.9 + 0.923 * (double)adc - 950.0;
num11 = (double)age * (0.95 + 0.02 * (num - 21.2)) + (double)(adc - 500) * 0.02;
}
bodyfatItem.bmi = num.ToString("0.0");
num2 = ((num2 > weight * 0.15) ? (weight * 0.15) : num2);
bodyfatItem.bm = ((num2 < weight * 0.02) ? (weight * 0.02) : num2).ToString("0.0");
num3 = ((num3 > 75.0) ? 75.0 : num3);
bodyfatItem.rom = ((num3 < 15.0) ? 15.0 : num3).ToString("0.0");
num4 = ((num4 > 70.0) ? 70.0 : num4);
bodyfatItem.moi = ((num4 < 20.0) ? 20.0 : num4).ToString("0.0");
num5 = ((num5 > 50.0) ? 50.0 : num5);
bodyfatItem.bfr = ((num5 < 5.0) ? 5.0 : num5).ToString("0.0");
bodyfatItem.sfr = num6 <= 0 ? "0" : num6.ToString("0.0");
bodyfatItem.rosm = num7.ToString("0.0");
num8 = ((num8 > 50.0) ? 50.0 : num8);
bodyfatItem.pp = ((num8 < 10.0) ? 10.0 : num8).ToString("0.0");
num9 = ((num9 > 20.0) ? 20.0 : num9);
bodyfatItem.uvi = ((num9 < 1.0) ? 1.0 : num9).ToString("0");
bodyfatItem.bmr = num10 <= 0 ? "0" : num10.ToString("0");
if (age < 18)
{
num11 = age;
}
else
{
num11 = ((num11 > (double)(age + 10)) ? ((double)(age + 10)) : num11);
num11 = ((num11 < (double)(age - 10)) ? ((double)(age - 10)) : num11);
}
bodyfatItem.physicAge = num11.ToString("0");
bodyfatItem.weight = weight;
bodyfatItem.height = height;
bodyfatItem.age = age;
bodyfatItem.adc = adc;
bodyfatItem.sex = sex;
return bodyfatItem;
}
///
/// 2/4电极旧的计算算法
///
///
///
///
///
///
///
private static BodyfatItem Old_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 bmi = ((double)((int)((weight / (height * height)) * 10.0))) / 10.0;
double bm = 0.0;//骨量
double rom = 0.0;//肌肉率
double moi = 0.0;//水份
double bfr = 0.0;//脂肪率
double sfr = 0.0;//皮下脂肪率
double rosm = 0.0;//骨骼肌率
double pp = 0.0;//蛋白率
double uvi = 0.0;//内脏脂肪指数
double bmr = 0.0;//基础代谢率
double physicAge = 0.0;//身体年龄
if (sex == 1)
{
bm = ((0.015 * weight) + (((2.0 - (0.00055 * adc)) * height) / 100.0)) + 1.15;
rom = ((((-((0.00115 * adc) + 0.01) * weight) + (((49.64 - (0.031 * adc)) * height) / 100.0)) + (adc * 0.08)) + (age * 0.04)) + 15.4;
moi = (((0xf_4240 / (bmi * ((2.688 * adc) - 78.28))) - (0x274a / adc)) - (0.22 * age)) + 52.6;
bfr = ((((-930_000.0 / bmi) / ((1.966 * adc) - 58.46)) + (0x3378 / adc)) - (0.06 * age)) + 40.0;
sfr = 0.898 * bfr;
rosm = 0.895 * moi;
pp = 0.8 * (((100.0 - bfr) - moi) - (bm / weight));
uvi = ((((0.304 * weight) + ((25.58 * height) / 100.0)) + (0.131 * age)) + (0.005 * adc)) - 0x16;
//bmr = (((((9.0 + (0.0015 * adc)) * weight) + (((0x546 - (0.88 * adc)) * height) / 100.0)) + (0xbc / age)) + (0.748 * adc)) - 0x41d;
bmr = 370 + 21.6 * weight * (1 - bfr / 100);
physicAge = ((((age * (1.0 + (0.012 * (bmi - 1.0)))) - 0x15) + ((30 - age) * 0.35)) + ((adc - 450) * 0.02)) + 11.0;
}
else
{
bm = ((((2.2E-05 * adc) * weight) + (((4.99 - (0.00284 * adc)) * height) / 100.0)) + (0.0012 * adc)) + 1.15;
rom = ((((-((0.00115 * adc) + 0.01) * weight) + (((49.64 - (0.031 * adc)) * height) / 100.0)) + (adc * 0.08)) + (age * 0.04)) + 6.0;
moi = (((0xf_4240 / (bmi * ((2.467 * adc) - 75.37))) - (0x3787 / adc)) - (0.034 * age)) + 43.2;
bfr = ((((-3_030_000.0 / (bmi + 20.0)) / ((1.966 * adc) - 58.46)) + (0x6e10 / adc)) - (0.06 * age)) + 0x33;
sfr = (0.876 * bfr) + 1.66;
rosm = (0.857 * moi) - 0.36;
pp = 0.75 * (((100.0 - bfr) - moi) - (bm / weight));
uvi = ((((0.304 * weight) + ((25.58 * height) / 100.0)) + (0.131 * age)) + (0.005 * adc)) - 0x16;
bmr = 370 + 21.6 * weight * (1 - bfr / 100);
//bmr = ((((((0.00307 * adc) + 1.5) * weight) + (((0x5b3 - (0.989 * adc)) * height) / 100.0)) + (age * 0.9)) + (0.923 * adc)) - 950.0;
physicAge = (age * (0.95 + (0.02 * (bmi - 21.2)))) + ((adc - 500) * 0.02);
}
item.bmi = bmi.ToString("0.0");
bm = (bm > (weight * 0.15)) ? (weight * 0.15) : bm;
item.bm = ((bm < (weight * 0.02)) ? (weight * 0.02) : bm).ToString("0.0");
rom = (rom > 0x4b) ? 0x4b : rom;
item.rom = ((rom < 15.0) ? 15.0 : rom).ToString("0.0");
moi = (moi > 70.0) ? 70.0 : moi;
item.moi = ((moi < 20.0) ? 20.0 : moi).ToString("0.0");
bfr = (bfr > 50.0) ? 50.0 : bfr;
item.bfr = ((bfr < 5.0) ? 5.0 : bfr).ToString("0.0");
item.sfr = sfr.ToString("0.0");
item.rosm = rosm.ToString("0.0");
pp = (pp > 50.0) ? 50.0 : pp;
item.pp = ((pp < 10.0) ? 10.0 : pp).ToString("0.0");
uvi = (uvi > 20.0) ? 20.0 : uvi;
item.uvi = ((uvi < 1.0) ? 1.0 : uvi).ToString("0");
item.bmr = bmr.ToString("0.0");
if (age < 0x12)
{
physicAge = age;
}
else
{
physicAge = (physicAge > (age + 10)) ? ((double)(age + 10)) : physicAge;
physicAge = (physicAge < (age - 10)) ? ((double)(age - 10)) : physicAge;
}
item.physicAge = physicAge.ToString("0");
item.weight = weight;
item.height = height;
item.age = age;
item.adc = adc;
item.sex = sex;
return item;
}
}
}