<?php

namespace app\app\controller;

use think\Controller;
use think\Db;
use \think\Validate;
use app\app\controller\Calculatebody;
use app\app\controller\Skip;

class Pagingcontrast extends Base{
    
    protected $color = ['#FF5656','#FFAB00','#5AD06D','#6492F6','#3967D6'];
    protected $db_name = ['2'=>'app_card_body_data','6'=>'app_card_skip_data','8'=>'app_card_vitalcapacity_data'];
    protected $request_result = [
        '2'=>['height'=>['身高','cm'],'weight'=>['体重','kg'],'age'=>['年龄','岁'],'bmi'=>['BMI',''],'fat_w'=>['脂肪量','kg'],'fat_r'=>['脂肪率','%'],'muscleval'=>['肌肉量','kg'],'muscle'=>['肌肉率','%'],'proteinval'=>['蛋白量','kg'],'protein'=>['蛋白率','%'],'water'=>['水分',''],'bone'=>['骨重','kg'],'visceral'=>['内脏指数',''],'sfr'=>['皮下脂肪','%'],'kcal'=>['基础代谢','kcal'],'un_fat_w_weight'=>['去脂体重','kg'],'body_age'=>['体龄',''],'body_level'=>['肥胖等级',''],'body_type'=>['体型','']],
        '6'=>[],
        '8'=>[]
    ];

    protected $pagesize = 10;

    ################################################请求接口################################################
    ################################################请求接口################################################
    ################################################请求接口################################################
    // 获取记录信息（分组）（包含身体、跳绳、肺活量）
    public function get_all_record_data_group($data = ['aud_id'=>'25','s_time'=>'2024-04-01 10:10:10','e_time'=>'2024-06-12 10:10:10','token'=>'0dafb98a10995c98b5a33b7d59d986ca','type'=>'2']){
        if(count(input('post.')) > 0){
            $data = input('post.');
        }
        if(!array_key_exists('aud_id', $data) || !array_key_exists('s_time', $data) || !array_key_exists('e_time', $data) || !array_key_exists('token', $data) || !array_key_exists('type', $data)){
            return $this->msg(10001);
        }
        if($this->token_time_validate($data['token']) === false){
            return $this->msg(20001);
        }
        unset($data['token']);
        if(array_key_exists($data['type'],$this->db_name)){
            return $this->jump_transfer_interface_record($data,'group');
        }else{
            return $this->msg(10005);
        }
    }
    // 获取记录信息（分页）（包含身体、跳绳、肺活量）
    public function get_all_record_data_page($data = ['aud_id'=>'25','page'=>1,'token'=>'0dafb98a10995c98b5a33b7d59d986ca','type'=>'2']){
        if(count(input('post.')) > 0){
            $data = input('post.');
        }
        if(!array_key_exists('aud_id', $data) || !array_key_exists('page', $data) || !array_key_exists('token', $data) || !array_key_exists('type', $data)){
            return $this->msg(10001);
        }
        if($this->token_time_validate($data['token']) === false){
            return $this->msg(20001);
        }
        unset($data['token']);
        if(array_key_exists($data['type'],$this->db_name)){
            return $this->jump_transfer_interface_record($data,'page');
        }else{
            return $this->msg(10005);
        }
    }
    // 获取详细历史数据信息（包含身体、跳绳、肺活量）
    public function get_all_record_detailed_information($data = ['id'=>'30','type'=>'2','token'=>'0dafb98a10995c98b5a33b7d59d986ca']){
        if(count(input('post.')) > 0){
            $data = input('post.');
        }
        if(!array_key_exists('id', $data) || !array_key_exists('type', $data) || !array_key_exists('token', $data)){
            return $this->msg(10001);
        }
        if($this->token_time_validate($data['token']) === false){
            return $this->msg(20001);
        }
        unset($data['token']);
        
        return $this->jump_transfer_interface_detailed($data);
    }
    // 数据对比（包含身体、跳绳、肺活量）
    public function get_all_card_data_contrast($data = ['before_id'=>'33','after_id'=>'35','type'=>'2','token'=>'0dafb98a10995c98b5a33b7d59d986ca']){
        if(count(input('post.')) > 0){
            $data = input('post.');
        }
        if(!array_key_exists('before_id', $data) || !array_key_exists('after_id', $data) || !array_key_exists('type', $data) || !array_key_exists('token', $data)){
            return $this->msg(10001);
        }
        if($this->token_time_validate($data['token']) === false){
            return $this->msg(20001);
        }
        unset($data['token']);
        // $data = explode(',',$data['id_arr']);
        // dump($data);
        // die;
        return $this->jump_transfer_interface_data_contrast($data);
    }
    ################################################请求接口################################################
    ################################################请求接口################################################
    ################################################请求接口################################################

    public function jump_transfer_interface_record($data,$str){
        if($data['type'] == '2'){
            return $this->get_body_record_data($data,$str);
        }else if($data['type'] == '6'){
            return $this->get_body_record_data($data,$str);
        }else if($data['type'] == '8'){
            return $this->get_body_record_data($data,$str);
        }
    }
    public function jump_transfer_interface_detailed($data){
        if($data['type'] == '2'){
            return $this->get_body_detaile_data_msg($data);
        }else if($data['type'] == '6'){
            // return $this->get_body_record_data($data,$str);
        }else if($data['type'] == '8'){
            // return $this->get_body_record_data($data,$str);
        }
    }
    public function jump_transfer_interface_data_contrast($data){
        if($data['type'] == '2'){
            return $this->get_body_data_contrast($data);
        }else if($data['type'] == '6'){
            // return $this->get_body_record_data($data,$str);
        }else if($data['type'] == '8'){
            // return $this->get_body_record_data($data,$str);
        }
    }

    ################################################业务部分################################################
    
    // 获取记录信息列表(分组)-身体
    // 获取记录信息列表(分页)-身体
    public function get_body_record_data($data,$type){
        $return_result = [];
        if($type == 'group'){
            $result = Db::query("
            select 
            id,
            CONVERT(varchar(10), record_time, 120) AS r_t,
            CONVERT(varchar(19), record_time, 120) AS record_time,
            height as v1,
            weight as v2,
            bmi as v3
            from app_card_body_data 
            where aud_id='".$data['aud_id']."'
            and record_time between '".$data['s_time']."' and '".$data['e_time']."' 
            and is_del = 0
            order by record_time desc");
            
            foreach ($result as $key => $value) {
                array_push($return_result, [
                    'id'=>$value['id'],
                    'v1'=>$value['v1'],
                    'v2'=>$value['v2'],
                    'v3'=>$value['v3'],
                    'v1_name'=>'身高',
                    'v2_name'=>'体重',
                    'v3_name'=>'BMI',
                    'r_t'=>str_replace('-', '/', $value['r_t'])
                ]);
            }
        }else{
            $result = Db::table('app_card_body_data')->where(['aud_id'=>$data['aud_id'],'is_del'=>0])->field("id,record_time,REPLACE(record_time, '-', '/') AS b_time,height,weight,bmi")->order('record_time desc')->page($data['page'],$this->pagesize)->select();
            $return_result['totalrows'] = Db::table('app_card_body_data')->where(['aud_id'=>$data['aud_id']])->count();
            $return_result['rows'] = [];
            $return_result['pageno'] = $data['page'];
            $return_result['pagesize'] = $this->pagesize;
            $return_result['totalpage'] = ceil($return_result['totalrows']/$this->pagesize);
            foreach ($result as $key => $value) {
                array_push($return_result['rows'],[
                    'id'=>$value['id'],
                    'v1_name'=>'身高',
                    'v1'=>$value['height'],
                    'v2_name'=>'体重',
                    'v2'=>$value['weight'],
                    'v3_name'=>'BMI',
                    'v3'=>$value['bmi'],
                    'record_time'=>$value['b_time'],
                ]);
            }
        }
        return $this->msg($return_result);
    }
    // 获取详细历史数据信息
    public function get_body_detaile_data_msg($data){
        $result = Db::table($this->db_name[$data['type']])->where(['id'=>$data['id']])->find();
        if($result){
            $result_data = [];
            foreach ($this->request_result['2'] as $key => $value) {
                $temporary_arr['key_name'] = $key;
                $temporary_arr['name'] = $value[0];
                if($key == 'un_fat_w_weight'){
                    $temporary_arr['value'] = bcsub($result['weight'],explode(',',$result['fat_r'])[0],2);
                }else{
                    $temporary_arr['value'] = explode(',',$result[$key])[0];
                }
                $temporary_arr['unit'] = $value[1];
                array_push($result_data,$temporary_arr);
            }
            return $this->msg($result_data);
        }else{
            return $this->msg(10004);
        }
        
    }
    // 数据对比
    public function get_body_data_contrast($data){
        // dump($data);
        $data2 = [$data['before_id'],$data['after_id']];
        $data3 = implode(',',$data2);
        $calculate_arr = [];
        $result = Db::query("
        select 
        acbd.id,
        acbd.height_data as height,
        acbd.weight_data as weight,
        acbd.bmi_data as bmi,
        acbd.fat_r,
        acbd.fat_w,
        acbd.muscle,
        acbd.muscleval,
        acbd.water,
        acbd.proteinval,
        acbd.bone,
        acbd.protein,
        acbd.kcal,
        acbd.visceral,
        acbd.sfr,
        acbd.record_time,
        REPLACE(CONVERT(varchar(10), acbd.record_time, 23), '-', '/') AS b_time,
        aud.nickname,
        aud.gender,
        aud.birthday,
        aud.head_pic
        from app_card_body_data as acbd
        left join app_user_data as aud on acbd.aud_id=aud.id  
        where acbd.id in ($data3)
        and acbd.is_del = 0
        ");
        // dump($result);
        // die;
        if(!$result || count($result)<2){
            return $this->msg(10004);
        }
        // 调整顺序
        foreach ($result as $key => $value) {
            if($value['id'] == $data2[0]){
                $calculate_arr['before'] = $value;
            }else{
                $calculate_arr['after'] = $value;
            }
        }
        // dump($calculate_arr);
        // die;

        $return_data['time'] = $calculate_arr['before']['b_time'].'-'.$calculate_arr['after']['b_time'];
        $return_data['headimg'] = $calculate_arr['before']['head_pic'];
        $return_data['name'] = $calculate_arr['before']['nickname'];
        $return_data['gender'] = $calculate_arr['before']['gender'];
        $return_data['age'] = $this->calculate_age($calculate_arr['before']['birthday']);
        $return_data['day'] = abs($this->daysSince($calculate_arr['before']['record_time'],$calculate_arr['after']['record_time']));

        $return_data['list'] = [];
        
        foreach ($calculate_arr['before'] as $key => $value) {
            if(in_array($key, ['height','weight','bmi','fat_r','fat_w','muscle','muscleval','water','proteinval','bone','protein','kcal','visceral','sfr'])){
                $before_arr = explode(',', $value);
                $after_arr = explode(',', $calculate_arr['after'][$key]);
                // $calculate_arr['before'][$key] = explode(',', $value);
                 
                array_push($return_data['list'], [
                    'firstresult'=>[
                        'color'=>'',
                        'level'=>$before_arr[1],
                        'value'=>$before_arr[0],
                        'title'=>$this->request_result['2'][$key][0],
                        'unit'=>$this->request_result['2'][$key][1],
                        'name'=>$key,
                    ],
                    'secondresult'=>[
                        'color'=>'',
                        'level'=>$after_arr[1],
                        'value'=>$after_arr[0],
                        'title'=>$this->request_result['2'][$key][0],
                        'unit'=>$this->request_result['2'][$key][1],
                        'name'=>$key,
                    ],
                    'diffval'=>bcsub($before_arr[0],$after_arr[0],2),
                ]);
            }
        }
        return $this->msg($return_data);
    }

    
    
}