<?php

namespace app\app\controller;

use think\Controller;
use think\Db;
use app\bj\controller\Common;
use think\Log;
use \think\Validate;
use app\app\controller\Calculatebody;
use app\app\controller\Skip;

class Cardparts extends Base{
    
    protected $color = ['#FF5656','#FF5656','#5AD06D','#6492F6','#3967D6'];
    protected $parameter_aggregate_top = [
        'weight'=>'反映和衡量一个人健康状况的重要标志之一',
        'height'=>'人体纵向部分的长度，源于人体的纵向生长，受遗传因素的影响较大',
        'bmi'=>'BMI是身体质量指数,是目前国际上常用的衡量人体胖瘦程度以及是否健康的一个标准。'
    ];
    protected $parameter_aggregate_bottom = [
        'fat_r'=>'体脂率是指身体成分中，脂肪组织所占的比率。测量体脂率比单纯的只测量体重更能反映我们身体的脂肪水平（肥胖程度）。',
        'fat_w'=>'人体脂肪的重量',
        'muscle'=>'根据人体肌肉总量和人体体重、身高等相结合得到的人体的一个比例值，这个值的范围决定一个人的身体健康状况以及力量的多少。',
        'muscleval'=>'肌肉量=实际体重*肌肉率',
        'water'=>'指人体内水分比例。',
        'proteinval'=>'蛋白量=实际体重*蛋白率',
        'bone'=>'单位体积内，骨组织、骨矿物质（钙、磷等）和骨基质（骨胶原、蛋白率、无机盐等等）含量，骨量代表它们骨骼健康的情况。',
        'protein'=>'人体内蛋白率含量。',
        'kcal'=>'指人体在清醒而又极端安静的状态下，不受肌肉活动、环境温度、食物及精神紧张等影响时的能量代谢率',
        'visceral'=>'内脏脂肪指数',
        'sfr'=>'皮下脂脂肪就是贮存于皮下的脂肪组织，人体的脂肪大约有2/3贮存在皮下组织',
        'body_level'=>'肥胖的程度,表现实际体重与理想体重的差距。肥胖等级是判定肥胖症的一个指标。'
    ];
    protected $parameter_aggregate_bottom_out = ['body_level'];
    protected $parameter_aggregate_bottom_condition = ['body_level'];
    // 脂肪率&脂肪量
    protected $fat_r_w = [
        'man'=>[
            '29'=>[
                ['min_val'=>'0','max_val'=>'10','text'=>'偏低','color'=>'#ff5656'],
                ['min_val'=>'10','max_val'=>'21','text'=>'标准','color'=>'#ffab00'],
                ['min_val'=>'21','max_val'=>'26','text'=>'偏高','color'=>'#5ad06d'],
                ['min_val'=>'26','max_val'=>'50','text'=>'高','color'=>'#6492f6'],
            ],
            '30'=>[
                ['min_val'=>'0','max_val'=>'11','text'=>'偏低','color'=>'#ff5656'],
                ['min_val'=>'11','max_val'=>'22','text'=>'标准','color'=>'#ffab00'],
                ['min_val'=>'22','max_val'=>'27','text'=>'偏高','color'=>'#5ad06d'],
                ['min_val'=>'27','max_val'=>'50','text'=>'高','color'=>'#6492f6'],
            ],
        ],
        'woman'=>[
            '29'=>[
                ['min_val'=>'0','max_val'=>'20','text'=>'偏低','color'=>'#ff5656'],
                ['min_val'=>'20','max_val'=>'31','text'=>'标准','color'=>'#ffab00'],
                ['min_val'=>'31','max_val'=>'38','text'=>'偏高','color'=>'#5ad06d'],
                ['min_val'=>'38','max_val'=>'80','text'=>'高','color'=>'#6492f6'],
            ],
            '30'=>[
                ['min_val'=>'0','max_val'=>'21','text'=>'偏低','color'=>'#ff5656'],
                ['min_val'=>'21','max_val'=>'32','text'=>'标准','color'=>'#ffab00'],
                ['min_val'=>'32','max_val'=>'39','text'=>'偏高','color'=>'#5ad06d'],
                ['min_val'=>'39','max_val'=>'80','text'=>'高','color'=>'#6492f6'],
            ]
        ]
    ];
    // 肌肉率&肌肉量
    protected $muscle_muscleval = [
        'man'=>[
            ['min_val'=>'0','max_val'=>'40','text'=>'不足','color'=>'#ff5656'],
            ['min_val'=>'40','max_val'=>'60','text'=>'标准','color'=>'#ffab00'],
            ['min_val'=>'60','max_val'=>'100','text'=>'优','color'=>'#5ad06d'],
        ],
        'woman'=>[
            ['min_val'=>'0','max_val'=>'30','text'=>'不足','color'=>'#ff5656'],
            ['min_val'=>'30','max_val'=>'50','text'=>'标准','color'=>'#ffab00'],
            ['min_val'=>'50','max_val'=>'100','text'=>'优','color'=>'#5ad06d'],
        ]
    ];
    // 水分
    protected $water = [
        'man'=>[
            ['min_val'=>'0','max_val'=>'55','text'=>'不足','color'=>'#ff5656'],
            ['min_val'=>'55','max_val'=>'65','text'=>'标准','color'=>'#ffab00'],
            ['min_val'=>'65','max_val'=>'100','text'=>'优','color'=>'#5ad06d'],
        ],
        'woman'=>[
            ['min_val'=>'0','max_val'=>'45','text'=>'不足','color'=>'#ff5656'],
            ['min_val'=>'45','max_val'=>'60','text'=>'标准','color'=>'#ffab00'],
            ['min_val'=>'60','max_val'=>'100','text'=>'优','color'=>'#5ad06d'],
        ]
    ];
    // 蛋白量&蛋白率
    protected $proteinval_protein = [
        'man'=>[
            ['min_val'=>'0','max_val'=>'16','text'=>'不足','color'=>'#ff5656'],
            ['min_val'=>'16','max_val'=>'18','text'=>'标准','color'=>'#ffab00'],
            ['min_val'=>'18','max_val'=>'50','text'=>'优','color'=>'#5ad06d'],
        ],
        'woman'=>[
            ['min_val'=>'0','max_val'=>'14','text'=>'不足','color'=>'#ff5656'],
            ['min_val'=>'14','max_val'=>'16','text'=>'标准','color'=>'#ffab00'],
            ['min_val'=>'16','max_val'=>'50','text'=>'优','color'=>'#5ad06d'],
        ]
    ];
    // 骨重
    protected $bone = [
        'man'=>[
            '60'=>[
                ['min_val'=>'0','max_val'=>'2.4','text'=>'不足','color'=>'#ff5656'],
                ['min_val'=>'2.4','max_val'=>'2.6','text'=>'标准','color'=>'#ffab00'],
                ['min_val'=>'2.6','max_val'=>'6','text'=>'优','color'=>'#5ad06d'],
            ],
            '60_75'=>[
                ['min_val'=>'0','max_val'=>'2.8','text'=>'不足','color'=>'#ff5656'],
                ['min_val'=>'2.8','max_val'=>'3','text'=>'标准','color'=>'#ffab00'],
                ['min_val'=>'3','max_val'=>'6','text'=>'优','color'=>'#5ad06d'],
            ],
            '75'=>[
                ['min_val'=>'0','max_val'=>'3.1','text'=>'不足','color'=>'#ff5656'],
                ['min_val'=>'3.1','max_val'=>'3.3','text'=>'标准','color'=>'#ffab00'],
                ['min_val'=>'3.3','max_val'=>'7','text'=>'优','color'=>'#5ad06d'],
            ],
        ],
        'woman'=>[
            '45'=>[
                ['min_val'=>'0','max_val'=>'1.7','text'=>'不足','color'=>'#ff5656'],
                ['min_val'=>'1.7','max_val'=>'1.9','text'=>'标准','color'=>'#ffab00'],
                ['min_val'=>'1.9','max_val'=>'5','text'=>'优','color'=>'#5ad06d'],
            ],
            '45_60'=>[
                ['min_val'=>'0','max_val'=>'2.1','text'=>'不足','color'=>'#ff5656'],
                ['min_val'=>'2.1','max_val'=>'2.3','text'=>'标准','color'=>'#ffab00'],
                ['min_val'=>'2.3','max_val'=>'5','text'=>'优','color'=>'#5ad06d'],
            ],
            '60'=>[
                ['min_val'=>'0','max_val'=>'2.4','text'=>'不足','color'=>'#ff5656'],
                ['min_val'=>'2.4','max_val'=>'2.6','text'=>'标准','color'=>'#ffab00'],
                ['min_val'=>'2.6','max_val'=>'5','text'=>'优','color'=>'#5ad06d'],
            ],
        ]
    ];
    // 基础代谢
    protected $kcal = [
        ['min_val'=>'0','max_val'=>'','text'=>'偏低','color'=>'#ff5656'],
        ['min_val'=>'','max_val'=>'9999','text'=>'优','color'=>'#5ad06d'],
    ];
    // 内脏指数
    protected $visceral = [
        'man'=>[
            ['min_val'=>'0','max_val'=>'9','text'=>'标准','color'=>'#ff5656'],
            ['min_val'=>'9','max_val'=>'14','text'=>'警惕','color'=>'#ffab00'],
            ['min_val'=>'14','max_val'=>'50','text'=>'危险','color'=>'#5ad06d'],
        ],
        'woman'=>[
            ['min_val'=>'0','max_val'=>'9','text'=>'标准','color'=>'#ff5656'],
            ['min_val'=>'9','max_val'=>'14','text'=>'警惕','color'=>'#ffab00'],
            ['min_val'=>'14','max_val'=>'50','text'=>'危险','color'=>'#5ad06d'],
        ]
    ];
    // 皮下脂肪
    protected $sfr = [
        'man'=>[
            ['min_val'=>'0','max_val'=>'7','text'=>'不足','color'=>'#ff5656'],
            ['min_val'=>'7','max_val'=>'15','text'=>'标准','color'=>'#ffab00'],
            ['min_val'=>'15','max_val'=>'50','text'=>'偏高','color'=>'#5ad06d'],
        ],
        'woman'=>[
            ['min_val'=>'0','max_val'=>'11','text'=>'不足','color'=>'#ff5656'],
            ['min_val'=>'11','max_val'=>'17','text'=>'标准','color'=>'#ffab00'],
            ['min_val'=>'17','max_val'=>'50','text'=>'偏高','color'=>'#5ad06d'],
        ]
    ];
    
    

    // 加 bcadd(,,20)
    // 减 bcsub(,,20)
    // 乘 bcmul(,,20)
    // 除 bcdiv(,,20)
    // 计算部分内容的横线标准以及说明文字
    public function conversion_interval($data){
        // dump($data);
        // $data['gender'] = $data['gender']==2?'woman':'man';
        $gender = $data['gender']==2?'woman':'man';
        $age = $data['age']['value'];
        $weight = 0;
        $temporary_arr = [
            'top_list'=>[],
            'bottom_list'=>[],
        ];
        // dump($data);
        // dump($temporary_arr);
        // 处理格式（顶部）
        foreach ($this->parameter_aggregate_top as $key => $value) {
            $data[$key]['key_name'] = $key;
            $data[$key]['desc'] = $value;
            if($key == 'weight'){
                $weight = $data[$key]['value'];
            }
            array_push($temporary_arr['top_list'],$data[$key]);
        }
        // 处理格式（底部）
        foreach ($this->parameter_aggregate_bottom as $key => $value) {
            $data[$key]['key_name'] = $key;
            $data[$key]['desc'] = $value;
            array_push($temporary_arr['bottom_list'],$data[$key]);
        }

        // 处理顶部list
        foreach ($temporary_arr['top_list'] as $key => $value) {
            if(count($temporary_arr['top_list'][$key]['list']) > 0){
                $temporary_arr['top_list'][$key]['offset'] = $this->calculate_landing_point($temporary_arr['top_list'][$key]['list'],$temporary_arr['top_list'][$key]['value'],$temporary_arr['top_list'][$key]['standard']);
            }
        }

        // 处理底部list
        foreach ($temporary_arr['bottom_list'] as $key => $value) {
            // dump($value['key_name']);
            // 脂肪率&
            if($value['key_name'] == 'fat_r'){
                if($age < 30){
                    $temporary_arr['bottom_list'][$key]['list'] = $this->fat_r_w[$gender]['29'];
                }else{
                    $temporary_arr['bottom_list'][$key]['list'] = $this->fat_r_w[$gender]['30'];
                }
                $temporary_arr['bottom_list'][$key]['offset'] = $this->calculate_landing_point($temporary_arr['bottom_list'][$key]['list'],$temporary_arr['bottom_list'][$key]['value'],$temporary_arr['bottom_list'][$key]['standard']);
            }
            // 脂肪量
            else if($value['key_name'] == 'fat_w'){
                if($age < 30){
                    $temporary_arr['bottom_list'][$key]['list'] = $this->fat_r_w[$gender]['29'];
                }else{
                    $temporary_arr['bottom_list'][$key]['list'] = $this->fat_r_w[$gender]['30'];
                }
                $temporary_arr['bottom_list'][$key]['list'] = $this->calculate_new_standard($temporary_arr['bottom_list'][$key]['list'],$weight,$value['key_name']);
                $temporary_arr['bottom_list'][$key]['offset'] = $this->calculate_landing_point($temporary_arr['bottom_list'][$key]['list'],$temporary_arr['bottom_list'][$key]['value'],$temporary_arr['bottom_list'][$key]['standard']);
            }
            // 肌肉率
            else if($value['key_name'] == 'muscle'){
                // dump('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%');
                $temporary_arr['bottom_list'][$key]['list'] = $this->muscle_muscleval[$gender];
                $temporary_arr['bottom_list'][$key]['offset'] = $this->calculate_landing_point($temporary_arr['bottom_list'][$key]['list'],$temporary_arr['bottom_list'][$key]['value'],$temporary_arr['bottom_list'][$key]['standard']);
                // dump($temporary_arr['bottom_list'][$key]);
            }
            // 肌肉量
            else if($value['key_name'] == 'muscleval'){
                // dump('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%');
                $temporary_arr['bottom_list'][$key]['list'] = $this->muscle_muscleval[$gender];
                $temporary_arr['bottom_list'][$key]['list'] = $this->calculate_new_standard($temporary_arr['bottom_list'][$key]['list'],$weight,$value['key_name']);
                $temporary_arr['bottom_list'][$key]['offset'] = $this->calculate_landing_point($temporary_arr['bottom_list'][$key]['list'],$temporary_arr['bottom_list'][$key]['value'],$temporary_arr['bottom_list'][$key]['standard']);
                // dump($temporary_arr['bottom_list'][$key]);
            }
            // 水分
            else if($value['key_name'] == 'water'){
                $temporary_arr['bottom_list'][$key]['list'] = $this->water[$gender];
                $temporary_arr['bottom_list'][$key]['offset'] = $this->calculate_landing_point($temporary_arr['bottom_list'][$key]['list'],$temporary_arr['bottom_list'][$key]['value'],$temporary_arr['bottom_list'][$key]['standard']);
            }
            // 蛋白量
            else if($value['key_name'] == 'proteinval'){
                $temporary_arr['bottom_list'][$key]['list'] = $this->proteinval_protein[$gender];
                $temporary_arr['bottom_list'][$key]['list'] = $this->calculate_new_standard($temporary_arr['bottom_list'][$key]['list'],$weight,$value['key_name']);
                $temporary_arr['bottom_list'][$key]['offset'] = $this->calculate_landing_point($temporary_arr['bottom_list'][$key]['list'],$temporary_arr['bottom_list'][$key]['value'],$temporary_arr['bottom_list'][$key]['standard']);
            }
            // 骨重
            else if($value['key_name'] == 'bone'){
                if($gender == 'man'){
                    if($weight < 60){
                        $temporary_arr['bottom_list'][$key]['list'] = $this->bone[$gender]['60'];
                    }else if($weight >= 60 && $weight < 75){
                        $temporary_arr['bottom_list'][$key]['list'] = $this->bone[$gender]['60_75'];
                    }else{
                        $temporary_arr['bottom_list'][$key]['list'] = $this->bone[$gender]['75'];
                    }
                }else{
                    if($weight < 45){
                        $temporary_arr['bottom_list'][$key]['list'] = $this->bone[$gender]['45'];
                    }else if($weight >= 45 && $weight < 60){
                        $temporary_arr['bottom_list'][$key]['list'] = $this->bone[$gender]['45_60'];
                    }else{
                        $temporary_arr['bottom_list'][$key]['list'] = $this->bone[$gender]['60'];
                    }
                }
                $temporary_arr['bottom_list'][$key]['offset'] = $this->calculate_landing_point($temporary_arr['bottom_list'][$key]['list'],$temporary_arr['bottom_list'][$key]['value'],$temporary_arr['bottom_list'][$key]['standard']);
            }
            // 蛋白率
            else if($value['key_name'] == 'protein'){
                $temporary_arr['bottom_list'][$key]['list'] = $this->proteinval_protein[$gender];
                $temporary_arr['bottom_list'][$key]['offset'] = $this->calculate_landing_point($temporary_arr['bottom_list'][$key]['list'],$temporary_arr['bottom_list'][$key]['value'],$temporary_arr['bottom_list'][$key]['standard']);
            }
            // 基础代谢
            else if($value['key_name'] == 'kcal'){
                $temporary_arr['bottom_list'][$key]['list'] = $this->calculate_new_standard($this->kcal,$weight,$value['key_name'],$age,$gender);
                $temporary_arr['bottom_list'][$key]['offset'] = $this->calculate_landing_point($temporary_arr['bottom_list'][$key]['list'],$temporary_arr['bottom_list'][$key]['value'],$temporary_arr['bottom_list'][$key]['standard']);
            }
            // 内脏指数
            else if($value['key_name'] == 'visceral'){
                $temporary_arr['bottom_list'][$key]['list'] = $this->visceral[$gender];
                $temporary_arr['bottom_list'][$key]['offset'] = $this->calculate_landing_point($temporary_arr['bottom_list'][$key]['list'],$temporary_arr['bottom_list'][$key]['value'],$temporary_arr['bottom_list'][$key]['standard']);
            }
            // 皮下脂肪
            else if($value['key_name'] == 'sfr'){
                $temporary_arr['bottom_list'][$key]['list'] = $this->sfr[$gender];
                $temporary_arr['bottom_list'][$key]['offset'] = $this->calculate_landing_point($temporary_arr['bottom_list'][$key]['list'],$temporary_arr['bottom_list'][$key]['value'],$temporary_arr['bottom_list'][$key]['standard']);
            }
        }
        // dump($data['gender']);
        // dump($data['age']['value']);
        // // dump($temporary_arr['top_list']);
        // dump($temporary_arr['bottom_list']);
        // die;
        return $temporary_arr;
    }


    // 计算落点百分比
    public function calculate_landing_point($data,$val,$t_val){
        $num = count($data);
        $a_section = bcdiv(100,$num,2);
        $temporary_data = [];
        $num_0 = 0;
        foreach ($data as $key => $value) {
            if($t_val == $value['text']){
                $temporary_data = $value;
                $num_0 = $key;
                break;
            }
        }
        $max_num = trim($temporary_data['max_val']);
        $min_num = trim($temporary_data['min_val']);
        if($val < $temporary_data['max_val']){
            $num_1 = bcsub($val,$min_num,2);
            $num_2 = bcsub($max_num,$min_num,2);
            $num_3 = bcdiv(bcdiv($num_1,$num_2,4),$num,4)*100;
            $result = bcadd(bcmul($num_0,$a_section,2),$num_3,2);
        }else{
            $num_3 = bcdiv(1,$num,4)*100;
            $result = bcadd(bcmul($num_0,$a_section,2),$num_3,2);
        }
        return $result;
    }

    // 计算新标准
    public function calculate_new_standard($data,$w,$k,$age=0,$gender='man'){
        $temporary_arr = [];
        if($k != 'kcal'){
            foreach ($data as $key => $value) {
                array_push($temporary_arr,[
                    'min_val'=>bcmul($w,bcdiv($value['min_val'],100,2),2),
                    'max_val'=>bcmul($w,bcdiv($value['max_val'],100,2),2),
                    'text'=>$value['text'],
                    'color'=>$value['color']
                ]);
            }
        }else{
            // BMR标准值(男)	    BMR标准值(女)
            // 60.9*体重(kg)-54	    61.0*体重(kg)-51
            // 22.7*体重(kg)+495	22.5*体重(kg)+499
            // 17.5*体重(kg)+651	12.2*体重(kg)+746
            // 15.3*体重(kg)+679	14.7*体重(kg)+496
            // 11.6*体重(kg)+879	8.7*体重(kg)+820
            $vv_val = 0;
            if($age < 3){
                if($gender == 'man'){
                    $vv_val = bcsub(bcmul(60.9,$w,2),54,2);
                }else{
                    $vv_val = bcsub(bcmul(61.0,$w,2),51,2);
                }
            }else if($age >= 3 && $age < 10){
                if($gender == 'man'){
                    $vv_val = bcadd(bcmul(22.7,$w,2),495,2);
                }else{
                    $vv_val = bcadd(bcmul(22.5,$w,2),499,2);
                }
            }else if($age >= 10 && $age < 18){
                if($gender == 'man'){
                    $vv_val = bcadd(bcmul(17.5,$w,2),651,2);
                }else{
                    $vv_val = bcadd(bcmul(12.2,$w,2),746,2);
                }
            }else if($age >= 18 && $age < 30){
                if($gender == 'man'){
                    $vv_val = bcadd(bcmul(15.3,$w,2),679,2);
                }else{
                    $vv_val = bcadd(bcmul(14.7,$w,2),496,2);
                }
            }else{
                if($gender == 'man'){
                    $vv_val = bcadd(bcmul(11.6,$w,2),879,2);
                }else{
                    $vv_val = bcadd(bcmul(8.7,$w,2),820,2);
                }
            }
            $data[0]['max_val'] = $vv_val;
            $data[1]['min_val'] = $vv_val;
            $temporary_arr = $data;
        }
        return $temporary_arr;

        // dump($data);
        // dump($w);
        // dump($k);
    }
    
}