<?php

namespace app\app\controller;

use think\Controller;

class Calculatebody extends Controller{
    

    function calculate_body_data_result($data = ['weight'=>52.5,'height'=>165,'age'=>30,'adc'=>550,'gender'=>1]){
        $data['gender'] = $data['gender'] == 0 ? 1 : $data['gender'];  

        // 加 bcadd(,,20)
        // 减 bcsub(,,20)
        // 乘 bcmul(,,20)
        // 除 bcdiv(,,20)
        $result = $this->calculate_body_data($data['height'],$data['weight'],$data['age'],$data['gender'],$data['adc']);
        // dump($result);
        // dump('=========================================');
        // dump(1);
        // die;

        // $result['fat_w'] = $result['fat_r'] / 100 * $data['weight'];
        $result['fat_w'] = bcmul(bcdiv($result['fat_r'],'100',20),$data['weight'],2);
        // $result['proteinval'] = $result['protein'] / 100 * $data['weight'];
        $result['proteinval'] = bcmul(bcdiv($result['protein'],'100',20),$data['weight'],3);
        //肌肉量=体重-脂肪量-骨量
        // $result['muscleval'] = $result['weight'] - $result['fat_w'] - $result['bone'];
        $result['muscleval'] = bcsub(bcsub($result['weight'],$result['fat_w'],20),$result['bone'],2);
        // $result['muscle'] = $result['muscleval'] / $data['weight'] * 100.0;
        $result['muscle'] = bcmul(bcdiv($result['muscleval'],$data['weight'],20),'100.0',2);
        // 水份=肌肉量-蛋白量
        // $result['water'] = $result['muscleval'] - $result['proteinval'];
        $result['water'] = bcsub($result['muscleval'],$result['proteinval'],3);
        // $result['lbm'] = (1 - $result['fat_r'] / 100) * $data['weight'];
        $result['lbm'] = bcmul(bcsub('1',bcdiv($result['fat_r'],'100',20),20),$data['weight'],2);


        $return_data['体重'] = $data['weight'];
        $return_data['身高'] = $data['height'];
        $return_data['BMI'] = $result['bmi'];
        $return_data['age'] = $result['age'];
        if($result['bmi']<21.6){
            $return_data['身体得分'] = bcmul(bcdiv($result['bmi'],'21.6',20),'100',0);
        }else{
            $return_data['身体得分'] = bcmul(bcdiv('21.6',$result['bmi'],20),'100',0);
        }

        $return_data['脂肪量'][0] = $result['fat_w'];

        $return_data['脂肪率'][0] = $result['sfr'];
        if(($data['gender']==1 && $data['age']<30 && $result['sfr']<10) || ($data['gender']==1 && $data['age']>=30 && $result['sfr']<11) || ($data['gender']==2 && $data['age']<30 && $result['sfr']<20) || ($data['gender']==2 && $data['age']>=30 && $result['sfr']<21)){
            $return_data['脂肪率'][1] = '偏低';
            $return_data['脂肪量'][1] = '偏低';
        }else if(($data['gender']==1 && $data['age']<30 && $result['sfr']>=10 && $result['sfr']<21) || ($data['gender']==1 && $data['age']>=30 && $result['sfr']>=11 && $result['sfr']<22) || ($data['gender']==2 && $data['age']<30 && $result['sfr']>=20 && $result['sfr']<31) || ($data['gender']==2 && $data['age']>=30 && $result['sfr']>=21 && $result['sfr']<32)){
            $return_data['脂肪率'][1] = '标准';
            $return_data['脂肪量'][1] = '标准';
        }else if(($data['gender']==1 && $data['age']<30 && $result['sfr']>=21 && $result['sfr']<26) || ($data['gender']==1 && $data['age']>=30 && $result['sfr']>=22 && $result['sfr']<27) || ($data['gender']==2 && $data['age']<30 && $result['sfr']>=31 && $result['sfr']<38) || ($data['gender']==2 && $data['age']>=30 && $result['sfr']>=32 && $result['sfr']<39)){
            $return_data['脂肪率'][1] = '偏高';
            $return_data['脂肪量'][1] = '偏高';
        }else if(($data['gender']==1 && $data['age']<30 && $result['sfr']>=26) || ($data['gender']==1 && $data['age']>=30 && $result['sfr']>=27) || ($data['gender']==2 && $data['age']<30 && $result['sfr']<38) || ($data['gender']==2 && $data['age']>=30 && $result['sfr']<39)){
            $return_data['脂肪率'][1] = '高';
            $return_data['脂肪量'][1] = '高';
        }
        
        $return_data['肌肉量'][0] = $result['muscleval'];
        
        $return_data['肌肉率'][0] = $result['muscle'];
        if(($data['gender']==1 && $result['muscle']<40) || ($data['gender']==2 && $result['muscle']<30)){
            $return_data['肌肉量'][1] = '不足';
            $return_data['肌肉率'][1] = '不足';
        }else if(($data['gender']==1 && $result['muscle']>=40 && $result['muscle']<60) || ($data['gender']==2 && $result['muscle']>=30 && $result['muscle']<50)){
            $return_data['肌肉量'][1] = '标准';
            $return_data['肌肉率'][1] = '标准';
        }else if(($data['gender']==1 && $result['muscle']>=60) || ($data['gender']==2 && $result['muscle']>=50)){
            $return_data['肌肉量'][1] = '优';
            $return_data['肌肉率'][1] = '优';
        }
        
        $return_data['水分'][0] = $result['water'];
        if(($data['gender']==1 && $result['water']<55) || ($data['gender']==2 && $result['water']<45)){
            $return_data['水分'][1] = '不足';
        }else if(($data['gender']==1 && $result['water']>=55 && $result['water']<65) || ($data['gender']==2 && $result['water']>=45 && $result['water']<60)){
            $return_data['水分'][1] = '标准';
        }else if(($data['gender']==1 && $result['water']>65) || ($data['gender']==2 && $result['water']>60)){
            $return_data['水分'][1] = '优';
        }

        $return_data['蛋白量'][0] = $result['proteinval'];

        $return_data['蛋白率'][0] = $result['protein'];
        if(($data['gender']==1 && $result['protein']<16) || ($data['gender']==2 && $result['protein']<14)){
            $return_data['蛋白量'][1] = '不足';
            $return_data['蛋白率'][1] = '不足';
        }else if(($data['gender']==1 && $result['protein']>=16 && $result['protein']<18) || ($data['gender']==2 && $result['protein']>=14 && $result['protein']<16)){
            $return_data['蛋白量'][1] = '标准';
            $return_data['蛋白率'][1] = '标准';
        }else if(($data['gender']==1 && $result['protein']>18) || ($data['gender']==2 && $result['protein']>16)){
            $return_data['蛋白量'][1] = '优';
            $return_data['蛋白率'][1] = '优';
        }

        $return_data['骨重'][0] = $result['bone'];
        if(($data['gender']==1 && $data['weight']<60 && $result['bone']<2.4) || ($data['gender']==1 && $data['weight']>=60 && $data['weight']<75 && $result['bone']<2.8) || ($data['gender']==1 && $data['weight']>=75 && $result['bone']<3.1) || ($data['gender']==2 && $data['weight']<45 && $result['bone']<1.7) || ($data['gender']==2 && $data['weight']>=45 && $data['weight']<60 && $result['bone']<2.1) || ($data['gender']==2 && $data['weight']>=60 && $result['bone']<2.4)){
            $return_data['骨重'][1] = '不足';
        }else if(($data['gender']==1 && $data['weight']<60 && $result['bone']>=2.4 && $result['bone']<=2.6) || ($data['gender']==1 && $data['weight']>=60 && $data['weight']<75 && $result['bone']>=2.8 && $result['bone']<=3) || ($data['gender']==1 && $data['weight']>=75 && $result['bone']>=3.1 && $result['bone']<=3.3) || ($data['gender']==2 && $data['weight']<45 && $result['bone']>=1.7 && $result['bone']<=1.9) || ($data['gender']==2 && $data['weight']>=45 && $data['weight']<60 && $result['bone']>=2.1 && $result['bone']<=2.3) || ($data['gender']==2 && $data['weight']>=60 && $result['bone']>=2.4 && $result['bone']<=2.6)){
            $return_data['骨重'][1] = '标准';
        }else if(($data['gender']==1 && $data['weight']<60 && $result['bone']>2.6) || ($data['gender']==1 && $data['weight']>=60 && $data['weight']<75 && $result['bone']>3) || ($data['gender']==1 && $data['weight']>=75 && $result['bone']<3.3) || ($data['gender']==2 && $data['weight']<45 && $result['bone']>1.9) || ($data['gender']==2 && $data['weight']>=45 && $data['weight']<60 && $result['bone']>2.3) || ($data['gender']==2 && $data['weight']>=60 && $result['bone']>2.6)){
            $return_data['骨重'][1] = '优';
        }
        
        $return_data['基础代谢'][0] = $result['kcal'];
        
        
        if(($data['gender']==1 && $data['age']>0 && $data['age']<3 && (60.9*$data['weight']-54)>$result['kcal']) || ($data['gender']==1 && $data['age']>=3 && $data['age']<10 && (22.7*$data['weight']+495)>$result['kcal']) || ($data['gender']==1 && $data['age']>=10 && $data['age']<18 && (17.5*$data['weight']+651)>$result['kcal']) || ($data['gender']==1 && $data['age']>=18 && $data['age']<30 && (15.3*$data['weight']+679)>$result['kcal']) || ($data['gender']==1 && $data['age']>=30 && (11.6*$data['weight']+879)>$result['kcal']) || ($data['gender']==2 && $data['age']>0 && $data['age']<3 && (61*$data['weight']-51)>$result['kcal']) || ($data['gender']==2 && $data['age']>=3 && $data['age']<10 && (22.5*$data['weight']+499)>$result['kcal']) || ($data['gender']==2 && $data['age']>=10 && $data['age']<18 && (12.2*$data['weight']+746)>$result['kcal']) || ($data['gender']==2 && $data['age']>=18 && $data['age']<30 && (14.7*$data['weight']+496)>$result['kcal']) || ($data['gender']==2 && $data['age']>=30 && (8.7*$data['weight']+820)>$result['kcal'])){
            $return_data['基础代谢'][1] = '偏低';
        }else if(($data['gender']==1 && $data['age']>0 && $data['age']<3 && (60.9*$data['weight']-54)<=$result['kcal']) || ($data['gender']==1 && $data['age']>=3 && $data['age']<10 && (22.7*$data['weight']+495)<=$result['kcal']) || ($data['gender']==1 && $data['age']>=10 && $data['age']<18 && (17.5*$data['weight']+651)<=$result['kcal']) || ($data['gender']==1 && $data['age']>=18 && $data['age']<30 && (15.3*$data['weight']+679)<=$result['kcal']) || ($data['gender']==1 && $data['age']>=30 && (11.6*$data['weight']+879)<=$result['kcal']) || ($data['gender']==2 && $data['age']>0 && $data['age']<3 && (61*$data['weight']-51)<=$result['kcal']) || ($data['gender']==2 && $data['age']>=3 && $data['age']<10 && (22.5*$data['weight']+499)<=$result['kcal']) || ($data['gender']==2 && $data['age']>=10 && $data['age']<18 && (12.2*$data['weight']+746)<=$result['kcal']) || ($data['gender']==2 && $data['age']>=18 && $data['age']<30 && (14.7*$data['weight']+496)<=$result['kcal']) || ($data['gender']==2 && $data['age']>=30 && (8.7*$data['weight']+820)<=$result['kcal'])){
            $return_data['基础代谢'][1] = '优';
        }

        $return_data['内脏指数'][0] = $result['visceral'];
        if($result['visceral']<9){
            $return_data['内脏指数'][1] = '标准';
        }else if($result['visceral']>=9 && $result['visceral']<14){
            $return_data['内脏指数'][1] = '警惕';
        }else if($result['visceral']>=14){
            $return_data['内脏指数'][1] = '危险';
        }

        $return_data['皮下脂肪'][0] = $result['sfr'];
        if(($data['gender']==1 && $result['sfr']<7) || ($data['gender']==2 && $result['sfr']<11)){
            $return_data['皮下脂肪'][1] = '不足';
        }else if(($data['gender']==1 && $result['sfr']>=7 && $result['sfr']<15) || ($data['gender']==2 && $result['sfr']>=11 && $result['sfr']<17)){
            $return_data['皮下脂肪'][1] = '标准';
        }else if(($data['gender']==1 && $result['sfr']>=15) || ($data['gender']==2 && $result['sfr']>=17)){
            $return_data['皮下脂肪'][1] = '偏高';
        }


        // 脂肪率:偏低 标准 偏高 高
        // 肌肉率:不足 标准 优
        
        
        if($data['age']>=16){
            if($result['standard_level']<-0.2){
                $return_data['肥胖等级'] = '体重不足';
            }else if($result['standard_level']>=-0.2 && $result['standard_level']<-0.1){
                $return_data['肥胖等级'] = '偏瘦';
            }else if($result['standard_level']>=-0.1 && $result['standard_level']<=0.1){
                $return_data['肥胖等级'] = '标准';
            }else if($result['standard_level']>0.1 && $result['standard_level']<=0.2){
                $return_data['肥胖等级'] = '偏重';
            }else if($result['standard_level']>0.2){
                $return_data['肥胖等级'] = '超重';
            }else{
                $return_data['肥胖等级'] = '暂无数据';
            }
        }else{
            $return_data['肥胖等级'] = '儿童';
        }
        

        if($data['age']>=16){
            if(($return_data['脂肪率'][1] == '高' || $return_data['脂肪率'][1] == '偏高') && $return_data['肌肉率'][1] == '不足'){
                $return_data['身体类型'] = '隐形肥胖';
            }else if(($return_data['脂肪率'][1] == '高' || $return_data['脂肪率'][1] == '偏高') && $return_data['肌肉率'][1] == '标准'){
                $return_data['身体类型'] = '偏胖';
            }else if(($return_data['脂肪率'][1] == '高' || $return_data['脂肪率'][1] == '偏高') && $return_data['肌肉率'][1] == '优'){
                $return_data['身体类型'] = '结实型偏胖';
            }else if($return_data['脂肪率'][1] == '标准' && $return_data['肌肉率'][1] == '不足'){
                $return_data['身体类型'] = '缺乏肌肉型';
            }else if($return_data['脂肪率'][1] == '标准' && $return_data['肌肉率'][1] == '标准'){
                $return_data['身体类型'] = '标准型';
            }else if($return_data['脂肪率'][1] == '标准' && $return_data['肌肉率'][1] == '优'){
                $return_data['身体类型'] = '标准肌肉型';
            }else if($return_data['脂肪率'][1] == '偏低' && $return_data['肌肉率'][1] == '不足'){
                $return_data['身体类型'] = '偏瘦';
            }else if($return_data['脂肪率'][1] == '偏低' && $return_data['肌肉率'][1] == '标准'){
                $return_data['身体类型'] = '偏瘦肌肉型';
            }else if($return_data['脂肪率'][1] == '偏低' && $return_data['肌肉率'][1] == '优'){
                $return_data['身体类型'] = '健美肌肉型';
            }else{
                $return_data['身体类型'] = '暂无数据';
            }
        }else{
            $return_data['身体类型'] = '儿童';
        }
        
        
        // dump($return_data);
        // die;
        // $result_end['fat_r'] = $result['fat_r'];
        // $result_end['muscle'] = $result['muscle'];
        // $result_end['water'] = $result['water'];
        // $result_end['bone'] = $result['bone'];
        // $result_end['kcal'] = $result['kcal'];
        // $result_end['fat_w'] = $result['fat_w'];
        // $result_end['visceral'] = $result['visceral'];
        // $result_end['protein'] = $result['protein'];
        // $result_end['bodyage'] = $result['bodyage'];
        // $result_end['bmi'] = $result['bmi'];
        // // $result_end['cmi'] = $result['cmi'];
        // $result_end['sfr'] = $result['sfr'];
        // // $result_end['sfrval'] = $result['sfrval'];
        // $result_end['skeletalmuscle'] = $result['skeletalmuscle'];
        // $result_end['muscleval'] = $result['muscleval'];
        // $result_end['proteinval'] = $result['proteinval'];
        // $result_end['lbm'] = $result['lbm'];
        // $result_end['weight'] = $result['weight'];
        // $result_end['height'] = $result['height'];
        // dump($return_data);
        // dump('=========================================');
        return $return_data;
        // dump($result_end);
        // die;
    }


    // 计算身体数据，BMI、脂肪率、脂肪量、肌肉率、肌肉量....
    function calculate_body_data($height,$weight,$age,$gender,$impedance){

        $result_data = [];  
        $mheight = bcdiv($height, '100', 20); // 假设我们保留20位小数  
        $gender = $gender == 0 ? 1 : $gender;  
        
        if (($weight <= 0) || ($weight > 220) || ($height <= 0) || ($height > 270) || ($age <= 0) || ($age > 120) || ($impedance <= 0) || ($impedance > 1000) || !in_array($gender, [1, 2])) {  
            if ($weight != 0 && $height != 0) {  
                // 计算BMI  
                // dump(bcmul($mheight, $mheight,20));
                $bmi = bcmul(bcdiv($weight, bcmul($mheight, $mheight,20), 20), '10.0', 20); // 先算除法再算乘法  
                $bmi = bcdiv($bmi, '10.0', 2); // 最后除以10，保留一位小数  
                $result_data['bmi'] = $bmi;  
                $result_data['bone'] = 0;
                $result_data['muscle'] = 0;
                $result_data['water'] = 0;
                $result_data['fat_r'] = 0;
                $result_data['sfr'] = 0;
                $result_data['skeletalmuscle'] = 0;
                $result_data['protein'] = 0;
                $result_data['visceral'] = 0;
                $result_data['kcal'] = 0;
                $result_data['bodyage'] = 0;
                $result_data['weight'] = $weight;
                $result_data['height'] = $height;
                $result_data['age'] = $age;
                $result_data['adc'] = $impedance;
                $result_data['gender'] = $gender;
                return $result_data;  
            }  
        } 
        
        // $num = bcmul(bcdiv($weight, bcmul($mheight, $mheight,20), 20), '10.0', 20);
        // $num = bcdiv($num, '10.0', 2); // 确保最后结果也保留20位小数 
        $num = intval(bcmul(bcdiv($weight,bcmul($mheight,$mheight,20),20),'10',20))/10;
        $num2 = 0.0;
        $num3 = 0.0;
        $num4 = 0.0;
        $num5 = 0.0;
        $num6 = 0.0;
        $num7 = 0.0;
        $num8 = 0.0;
        $num9 = 0.0;
        $num10 = 0.0;
        $num11 = 0.0;

        $standard_weight = 0.0;
        $standard_level = 0.0;
        
        
        // dump($gender);
        // 加 bcadd(,,20)
        // 减 bcsub(,,20)
        // 乘 bcmul(,,20)
        // 除 bcdiv(,,20)
        if ($gender == 1){
            // $num2 = 0.015 * $weight + (2.0 - 0.00055 * $impedance) * $height / 100 - 1.15;
            $num2 = bcsub(bcadd(bcmul('0.015',$weight,20),bcdiv(bcmul(bcsub('2.0',bcmul('0.00055',$impedance,20),20),$height,20),'100.0',20),20),'1.15',1);
            // $num3 = (0.0 - (0.00115 * $impedance + 0.01)) * $weight + (49.64 - 0.031 * $impedance) * $height / 100.0 + $impedance * 0.08 + $age * 0.04 + 15.4;
            $num3 = bcsub(bcsub(bcsub(bcsub(bcmul(bcsub('0.0',bcadd(bcmul('0.00115',$impedance,20),'0.01',20),20),$weight,20),bcdiv(bcmul(bcsub('49.64',bcmul('0.031',$impedance,20)),$height,20),'100.0',20),20),bcmul($impedance,'0.08'),20),bcmul($age,'0.04'),20),'15.4',20);
            // $num4 = 1000000.0/($num*(2.688*$impedance-78.28))-(10058/$impedance)-0.22*$age+52.6;
            $num4 = bcadd(bcsub(bcsub(bcdiv('1000000.0',bcmul($num,bcsub(bcmul('2.688',$impedance,20),'78.28',20),20),20),bcdiv('10058',$impedance,20),20),bcmul('0.22',$age,20),20),'52.6',20);
            // $num5 = -930000.0 / $num / (1.966 * $impedance - 58.46) + (13176 / $impedance) - 0.06 * $age + 40.0;
            $num5 = bcadd(bcsub(bcadd(bcdiv(bcdiv('-930000.0',$num,20),bcsub(bcmul('1.966',$impedance,20),'58.46',20),20),bcdiv('13176',$impedance,0),20),bcmul('0.06',$age,20),20),'40.0',20);
            // $num6 = 0.898 * $num5;
            $num6 = bcmul('0.898',$num5,1);
            // $num7 = 0.895 * $num4;
            $num7 = bcmul('0.895',$num4,20);
            // $num8 = 0.8 * (100.0 - $num5 - $num4 - $num2 / $weight);
            $num8 = bcmul('0.8',bcsub(bcsub(bcsub('100.0',$num5,20),$num4,20),bcdiv($num2,$weight,20),20),1);
            // $num9 = 0.304 * $weight - 25.58 * $height / 100.0 + 0.131 * $age + 0.005 * $impedance + 22.0;
            $num9 = bcadd(bcadd(bcadd(bcsub(bcmul('0.304',$weight,20),bcdiv(bcmul('25.58',$height,20),'100.0',20),20),bcmul('0.131',$age,20),20),bcmul('0.005',$impedance,20),20),'22.0',0);
            // $num10 = (9.0 + 0.0015 * $impedance) * $weight + (1350.0 - 0.88 * $impedance) * $height / 100.0 + (188 / $age) + 0.748 * $impedance - 1053.0;
            $num10 = bcsub(bcadd(bcadd(bcadd(bcmul(bcadd('9.0',bcmul('0.0015',$impedance,20),20),$weight,20),bcdiv(bcmul(bcsub('1350.0',bcmul('0.88',$impedance,20),20),$height,20),'100.0',20),20),bcdiv('188',$age,20),20),bcmul('0.748',$impedance,20),20),'1053.0',0);
            // $num11 = $age * (1.0 + 0.012 * ($num - 1.0)) - 21.0 + (30 - $age) * 0.35 + ($impedance - 450) * 0.02 + 11.0;
            $num11 = bcadd(bcadd(bcadd(bcsub(bcmul($age,bcadd('1.0',bcmul('0.012',bcsub($num,'1.0',20),20),20),20),'21.0',20),bcmul(bcsub('30',$age,20),'0.35',20),20),bcmul(bcsub($impedance,'450',20),'0.02',20),20),'11.0',0);

            // $standard_weight = ($height-80)*0.7;
            $standard_weight = bcmul(bcsub($height,'80',20),'0.7',20);
            
        }else{
            // $num2 = 2.2E-05 * $impedance * $weight + (4.99 - 0.00284 * $impedance) * $height / 100.0 + 0.0012 * $impedance - 4.45;
            $num2 = bcsub(bcadd(bcadd(bcmul(bcmul('0.000022',$impedance,20),$weight,20),bcdiv(bcmul(bcsub('4.99',bcmul('0.00284',$impedance,20),20),$height,20),'100.0',20),20),bcmul('0.0012',$impedance,20),20),'4.45',1);
            // $num3 = (0.0 - (0.00115 * $impedance + 0.01)) * $weight + (49.64 - 0.031 * $impedance) * $height / 100.0 + $impedance * 0.08 + $age * 0.04 + 6.0;
            $num3 = bcadd(bcadd(bcadd(bcadd(bcmul(bcsub('0.0',bcadd(bcmul('0.00115',$impedance,20),'0.01',20),20),$weight,20),bcdiv(bcmul(bcsub('49.64',bcmul('0.031',$impedance,20),20),$height,20),'100.0',20),20),bcmul($impedance,'0.08',20),20),bcmul($age,'0.04',20),20),'6.0',2);
            // $num4 = 1000000.0 / ($num * (2.467 * $impedance - 75.37)) - (14215 / $impedance) - 0.034 * $age + 43.2;
            $num4 = bcadd(bcsub(bcsub(bcdiv('1000000.0',bcmul($num,bcsub(bcmul('2.467',$impedance,20),'75.37',20),20),20),bcdiv('14215',$impedance,20),20),bcmul('0.034',$age,20),20),'43.2',20);
            // $num5 = -3030000.0 / ($num + 20.0) / (1.966 * $impedance - 58.46) + (28176 / $impedance) - 0.06 * $age + 51.0;
            $num5 = bcadd(bcsub(bcadd(bcdiv(bcdiv('-3030000.0',bcadd($num,'20.0',20),20),bcsub(bcmul('1.966',$impedance,20),'58.46',20),20),bcdiv('28176',$impedance,20),20),bcmul('0.06',$age,20),20),'51.0',20);
            // $num6 = 0.876 * $num5 + 1.66;
            $num6 = bcadd(bcmul('0.876',$num5,20),'1.66',1);
            // $num7 = 0.857 * $num4 - 0.36;
            $num7 = bcsub(bcmul('0.857',$num4,20),'0.36',20);
            // $num8 = 0.75 * (100.0 - $num5 - $num4 - $num2 / $weight);
            $num8 = bcmul('0.75',bcsub('100.0',bcsub($num5,bcsub($num4,bcdiv($num2,$weight,20),20),20),20),20);
            // $num9 = 0.304 * $weight - 25.58 * $height / 100.0 + 0.131 * $age + 0.005 * $impedance + 22.0;
            $num9 =  bcadd(bcadd(bcadd(bcsub(bcmul(0.304,$weight,20),bcdiv(bcmul(25.58,$height,20),'100.0',20),20),bcmul('0.131',$age,20),20),bcmul('0.005',$impedance,20),20),'22.0',0);
            // $num10 = (0.00307 * $impedance + 1.5) * $weight + (1459.0 - 0.989 * $impedance) * $height / 100.0 + $age * 0.9 + 0.923 * $impedance - 950.0;
            $num10 = bcsub(bcadd(bcadd(bcadd(bcmul(bcadd(bcmul('0.00307',$impedance,20),'1.5',20),$weight,20),bcdiv(bcmul(bcsub('1459.0',bcmul('0.989',$impedance,20),20),$height,20),'100.0',20),20),bcmul($age,'0.9',20),20),bcmul('0.923',$impedance,20),20),'950.0',0);
            // $num11 = $age * (0.95 + 0.02 * ($num - 21.2)) + ($impedance - 500) * 0.02;
            $num11 = bcadd(bcmul($age,bcadd(0.95,bcmul('0.02',bcsub($num,'21.2',20),20),20),20),bcmul(bcsub($impedance,'500',20),'0.02',20),20);

            // $standard_weight = ($height-80)*0.7;
            $standard_weight = bcmul(bcsub($height,'80',20),'0.6',20);
        }

        $result_data['bmi'] = $num;
        // $num2 = (($num2 > $weight * 0.15) ? ($weight * 0.15) : $num2);
        if(bccomp($num2, bcmul($weight,'0.15',20), 20) === 1){
            $num2 = bcmul($weight,'0.15',1);
        }
        // $result_data['bone'] = ($num2 < $weight * 0.02) ? ($weight * 0.02) : $num2;
        if(bccomp($num2, bcmul($weight,'0.02',20), 20) === -1){
            $result_data['bone'] = bcmul($weight,'0.02',1);
        }else{
            $result_data['bone'] = $num2;
        }
        // $num3 = (($num3 > 75.0) ? 75.0 : $num3);
        if(bccomp($num3, '75.0', 20) === 1){
            $num3 = '75.0';
        }
        // $result_data['muscle'] = ($num3 < 15.0) ? 15.0 : $num3;
        // dump($num3);
        if(bccomp($num3, '15.0', 20) === -1){
            $result_data['muscle'] = '15.0';
        }else{
            $result_data['muscle'] = $num3;
        }
        // dump($num4);
        // $num4 = (($num4 > 70.0) ? 70.0 : $num4);
        if(bccomp($num4, '70.0', 20) === 1){
            $num4 = '70.0';
        }
        // $result_data['water'] = ($num4 < 20.0) ? 20.0 : $num4;
        if(bccomp($num4, '20.0', 20) === -1){
            $result_data['water'] = '20.0';
        }else{
            $result_data['water'] = $num4;
        }
        // $num5 = (($num5 > 50.0) ? 50.0 : $num5);
        if(bccomp($num5, '50.0', 20) === 1){
            $num5 = '50.0';
        }
        // $result_data['fat_r'] = ($num5 < 5.0) ? 5.0 : $num5;
        
        if(bccomp($num5, '5.0', 20) === -1){
            $result_data['fat_r'] = '5.0';
        }else{
            // $result_data['fat_r'] = $num5;
            // dump($num5);
            $result_data['fat_r'] = substr($num5, 0, strpos($num5, ".") + 3);
        }
        // $result_data['sfr'] = $num6 <= 0 ? "0" : $num6;
        if(bccomp($num6, '0.0', 20) === -1){
            $result_data['sfr'] = '0.0';
        }else{
            $result_data['sfr'] = $num6;
        }
        // $result_data['skeletalmuscle'] = $num7;
        $result_data['skeletalmuscle'] = substr($num7, 0, strpos($num7, ".") + 3);
        // $num8 = (($num8 > 50.0) ? 50.0 : $num8);
        if(bccomp($num8, '50.0', 20) === 1){
            $num8 = '50.0';
        }
        // $result_data['protein'] = ($num8 < 10.0) ? 10.0 : $num8;
        if(bccomp($num8, '10.0', 20) === -1){
            $result_data['protein'] = '10.0';
        }else{
            $result_data['protein'] = $num8;
        }
        // $num9 = (($num9 > 20.0) ? 20.0 : $num9);
        if(bccomp($num9, '20.0', 20) === 1){
            $num9 = '20.0';
        }
        // $result_data['visceral'] = ($num9 < 1.0) ? 1.0 : $num9;
        if(bccomp($num9, '1.0', 20) === -1){
            $result_data['visceral'] = '1.0';
        }else{
            $result_data['visceral'] = $num9;
        }
        // $result_data['kcal'] = $num10 <= 0 ? "0" : $num10;
        if(bccomp($num10, '0', 20) !== 1){
            $result_data['kcal'] = '0';
        }else{
            $result_data['kcal'] = $num10;
        }
        
        // $standard_level = ($weight-$standard_weight)/$standard_weight;
        $standard_level = bcdiv(bcsub($weight,$standard_weight,20),$standard_weight,2);
        // 加 bcadd(,,20)
        // 减 bcsub(,,20)
        // dump($num11);
        if ($age < 18){
            $num11 = $age;
        }else{
            // dump($num11);
            // $num11 = (($num11 > ($age + 10)) ? (($age + 10)) : $num11);
            if(bccomp($num11, $age + 10, 20) === 1){
                $num11 = $age + 10;
            }
            // $num11 = (($num11 < ($age - 10)) ? (($age - 10)) : $num11);
            if(bccomp($num11, $age - 10, 20) === -1){
                $num11 = $age - 10;
            }
        }
        // dump($age);
        
        $result_data['bodyage'] = $num11;
        $result_data['weight'] = $weight;
        $result_data['height'] = $height;
        $result_data['age'] = $age;
        $result_data['adc'] = $impedance;
        $result_data['gender'] = $gender;
        $result_data['standard_level'] = $standard_level;
        return $result_data;
    }
    // 计算脂肪率
    function calculate_fat_r(){
        dump(123);
    }
    // 计算脂肪量
    function calculate_zhifangliang(){
        
    }
    // 计算肌肉率
    function calculate_jiroulv(){
        
    }
    // 计算肌肉量
    function calculate_jirouliang(){
        
    }
    // 计算水分
    function calculate_shuifen(){
        
    }
    // 计算蛋白量
    function calculate_danbailiang(){
        
    }
    // 计算骨重
    function calculate_guzhong(){
        
    }
    // 计算蛋白率
    function calculate_danbailv(){
        
    }
    // 计算基础代谢
    function calculate_jichudaixie(){
        
    }
    // 计算内脏指数
    function calculate_neizangzhishu(){
        
    }
    // 计算皮下脂肪
    function calculate_pixiazhifang(){
        
    }
    // 计算肥胖等级
    function calculate_feipangdengji(){
        
    }


    
    public function msg($code,$msg='',$data=[]){
        return json(['code'=>$code,'msg'=>$msg,'data'=>$data]);
    }

}