<?php

namespace app\app\controller;

use think\Controller;
use think\Db;

class Vitalcapacity extends Base{
    
    protected $color = ['无效'=>'#FF5656','不及格'=>'#FF5656','及格'=>'#FFAB00','良好'=>'#5AD06D','优秀'=>'#6492F6','牛逼'=>'#3967D6'];
    protected $curve_data_format = ['one_val'=>['第一次','容积/ml','#009DFF'],'two_val'=>['第二次','容积/ml','#009DFF'],'three_val'=>['第三次','容积/ml','#009DFF'],'average_val'=>['平均','容积/ml','#009DFF'],'score_val'=>['分数','分/分','#009DFF'],];
    protected $standard0 = ['优秀','良好','及格','不及格','无效'];
    protected $standard = [
        ['min_val'=>'90','max_val'=>'100','text'=>'优秀','color'=>'#6492F6'],
        ['min_val'=>'80','max_val'=>'89','text'=>'良好','color'=>'#5AD06D'],
        ['min_val'=>'66','max_val'=>'79','text'=>'及格','color'=>'#FFAB00'],
        ['min_val'=>'10','max_val'=>'65','text'=>'不及格','color'=>'#FF5656'],
        ['min_val'=>'0','max_val'=>'9','text'=>'无效','color'=>'#FF5656'],
    ];
    
    protected $result_end_data_mould = [
        'name'=>'',
        'value'=>'',
        'unit'=>'',
        'standard'=>'',
        'color'=>'',
        'list'=>[]
    ];

    // 加 bcadd(,,20)
    // 减 bcsub(,,20)
    // 乘 bcmul(,,20)
    // 除 bcdiv(,,20)
    ################################################################接口################################################################
    ################################################################接口################################################################
    ################################################################接口################################################################

    // 数据记录
    public function vitalcapacity_save_record_data($data = ['aud_id'=>'25','one'=>'1500','two'=>'1200','three'=>'1456','time'=>'2024-05-25 10:10:15','token'=>'0dafb98a10995c98b5a33b7d59d986ca']){
        if(count(input('post.')) > 0){
            $data = input('post.');
        }
        if(!array_key_exists('aud_id', $data) || !array_key_exists('time', $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['one'] = $this->isPositiveNumber($data['one']) === false?0:$data['one'];
        $data['two'] = $this->isPositiveNumber($data['one']) === false?0:$data['two'];
        $data['three'] = $this->isPositiveNumber($data['one']) === false?0:$data['three'];
        return $this->vitalcapacity_save_record_data_action($data);
    }
    // 数据报告
    public function vitalcapacity_data_report($data = ['aud_id'=>'25','token'=>'0dafb98a10995c98b5a33b7d59d986ca']){
        if(count(input('post.')) > 0){
            $data = input('post.');
        }
        if(!array_key_exists('aud_id', $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->vitalcapacity_data_report_action($data);
    }
    // 曲线
    public function vitalcapacity_curve_chart($data = ['aud_id'=>'25','time'=>'2024-05-25','token'=>'0dafb98a10995c98b5a33b7d59d986ca']){
        if(count(input('post.')) > 0){
            $data = input('post.');
        }
        if(!array_key_exists('aud_id', $data) || !array_key_exists('time', $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->vitalcapacity_curve_chart_action($data);
    }

    ################################################################业务################################################################

    ################################################################vitalcapacity_save_record_data
    public function vitalcapacity_save_record_data_action($data){
        $temporary_arr['aud_id'] = $data['aud_id'];
        $temporary_arr['record_time'] = $data['time'];
        $temporary_arr['one'] = $data['one'];
        $temporary_arr['two'] = $data['two'];
        $temporary_arr['three'] = $data['three'];
        $temporary_arr['average'] = bcdiv(bcadd(bcadd($data['one'],$data['two'],2),$data['three'],2),3,2);
        $temporary_arr['create_time'] = date('Y-m-d H:i:s');
        $temporary_arr['one_val'] = $data['one'];
        $temporary_arr['two_val'] = $data['two'];
        $temporary_arr['three_val'] = $data['three'];
        $temporary_arr['average_val'] = $temporary_arr['average'];
        $user_msg = Db::name('app_user_data')->where(['id'=>$data['aud_id']])->field('grade,gender')->find();
        if($user_msg){
            $achievement_judge_data = $this->vitalcapacity_achievement_judge(['average'=>$temporary_arr['average'],'grade'=>$user_msg['grade'],'gender'=>$user_msg['gender']]);
            
            $temporary_arr['score'] = $achievement_judge_data['score'].','.$achievement_judge_data['level'].','.$this->color[$achievement_judge_data['level']];
            $temporary_arr['score_val'] = $achievement_judge_data['score'];
        }else{
            return $this->msg(10004);
        }
        $result = Db::table('app_card_vitalcapacity_data')->insert($temporary_arr);
        if($result){
            $time = $result[0]['record_time'];
            $time = strtotime($time);  
            $time = date('Y年m月d日 H:i:s', $time);  
            
            return $this->msg(['average'=>$temporary_arr['average'].'ml','level'=>$achievement_judge_data['level'],'time'=>$time,'list'=>$this->standard0,'offset'=>$temporary_arr['score_val']]);
        }else{
            return $this->msg(10002);
        }
    }
    ################################################################vitalcapacity_data_report
    public function vitalcapacity_data_report_action($data){
        $result = Db::table('app_card_vitalcapacity_data')->where(['aud_id'=>$data['aud_id']])->order('record_time desc')->field('record_time,score,average')->limit(1)->select();
        if(count($result) <= 0){
            return $this->msg(10004);
        }else{
            $time = $result[0]['record_time'];
            $time = strtotime($time);  
            $time = date('Y年m月d日 H:i:s', $time);  
            $o_l = explode(',',$result[0]['score']);
            return $this->msg(['average'=>$result[0]['average'].'ml','level'=>$o_l[1],'time'=>$time,'list'=>$this->standard0,'offset'=>$o_l[0]]);
        }

    }

    ################################################################vitalcapacity_save_record_data
    public function vitalcapacity_curve_chart_action($data){
        $audid = $data['aud_id'];  
        $timeData = explode('-', $data['time']);  
  
        // 根据$timeData的长度构建不同的查询条件  
        $map = ['aud_id' => $audid];  
        switch (count($timeData)) {  
            case 3: // 年月日  
                $map['record_time'] = ['between', [date('Y-m-d 00:00:00', strtotime($timeData[0] . '-' . $timeData[1] . '-' . $timeData[2])), date('Y-m-d 23:59:59', strtotime($timeData[0] . '-' . $timeData[1] . '-' . $timeData[2]))]];  
                break;  
            case 2: // 年月  
                $map['record_time'] = ['between', [date('Y-m-01 00:00:00', strtotime($timeData[0] . '-' . $timeData[1])), date('Y-m-t 23:59:59', strtotime($timeData[0] . '-' . $timeData[1]))]];  
                break;  
            case 1: // 年  
                $map['record_time'] = ['between', [date('Y-01-01 00:00:00', strtotime($timeData[0])), date('Y-12-31 23:59:59', strtotime($timeData[0]))]];  
                break;  
            default:  
                return $this->msg(10005); // 无效的时间数据格式  
        }  
  
        // 使用查询构造器进行查询  
        $result = Db::name('app_card_vitalcapacity_data')->where($map)->field('one_val,two_val,three_val,average_val,score_val,aud_id,record_time,DATEPART(hour, record_time) AS hour,DATEPART(day, record_time) AS day,DATEPART(month, record_time) AS month')->order('record_time')->select();
        
        $return_data = [];
        if(count($timeData) == 3){
            $key_condition = 'hour';
        }else if(count($timeData) == 2){
            $key_condition = 'day';
        }else if(count($timeData) == 1){
            $key_condition = 'month';
        }
        // dump($map);
        // dump($result);
        // die;
        foreach ($this->curve_data_format as $key => $value) {
            $temporary_arr['title'] = $value[0];
            $temporary_arr['key'] = $key;
            $temporary_arr['line']['categories'] = [];
            $temporary_arr['line']['series'][0]['color'] = $value[2];
            $temporary_arr['line']['series'][0]['name'] = $value[0];
            $temporary_arr['line']['series'][0]['data'] = [];
            foreach ($result as $k => $v) {
                if($key_condition == 'hour'){
                    if(in_array($result[$k][$key_condition].'时',$temporary_arr['line']['categories'])){
                        $num = array_search($result[$k][$key_condition].'时', $temporary_arr['line']['categories']);
                        $temporary_arr['line']['series'][0]['data'][$num] = bcadd($temporary_arr['line']['series'][0]['data'][$num],$result[$k][$key],2);
                    }else{
                        array_push($temporary_arr['line']['categories'],$result[$k][$key_condition].'时');
                        array_push($temporary_arr['line']['series'][0]['data'],$result[$k][$key]);
                    }
                }else if($key_condition = 'day'){
                    if(in_array($result[$k]['month'].'/'.$result[$k][$key_condition],$temporary_arr['line']['categories'])){
                        $num = array_search($result[$k]['month'].'/'.$result[$k][$key_condition], $temporary_arr['line']['categories']);
                        $temporary_arr['line']['series'][0]['data'][$num] = bcadd($temporary_arr['line']['series'][0]['data'][$num],$result[$k][$key],2);
                    }else{
                        array_push($temporary_arr['line']['categories'],$result[$k]['month'].'/'.$result[$k][$key_condition]);
                        array_push($temporary_arr['line']['series'][0]['data'],$result[$k][$key]);
                    }
                    
                }else{
                    if(in_array($result[$k]['month'].'月',$temporary_arr['line']['categories'])){
                        $num = array_search($result[$k]['month'].'月', $temporary_arr['line']['categories']);
                        $temporary_arr['line']['series'][0]['data'][$num] = bcadd($temporary_arr['line']['series'][0]['data'][$num],$result[$k][$key],2);
                    }else{
                        array_push($temporary_arr['line']['categories'],$result[$k]['month'].'月');
                        array_push($temporary_arr['line']['series'][0]['data'],$result[$k][$key]);
                    }
                }
            }
            // dump($temporary_arr);
            array_push($return_data,$temporary_arr);
        }
        foreach ($return_data[1]['line']['series'][0]['data'] as $key => $value) {
            $return_data[1]['line']['series'][0]['data'][$key] = bcdiv($return_data[1]['line']['series'][0]['data'][$key],60,2);
        }
        return $this->msg($return_data);
    }


    

    ################################################################肺活量数据卡片接口################################################################
    ################################################################肺活量数据卡片接口################################################################

    // 肺活量成绩判断
    public function vitalcapacity_achievement_judge($data){
        $str = "sex = ".$data['gender']." and ".$data['grade']." <= ".$data['average']; 
        $user_achievement = Db::name('pc_vitalcapacity_standard')->where($str)->order($data['grade'] .' desc')->field('level,score,'.$data['grade'])->limit(1)->select();
        if(count($user_achievement)<=0){
            $user_achievement = ['level'=>'无效','score'=>'0'];
        }
        return $user_achievement[0];
    }
    // 肺活量判断区间
    public function vitalcapacity_standard_interval($data){
        // 缓存一周
        
        
    }


    ################################################################other################################################################
    ################################################################other################################################################
    ################################################################other################################################################



}