<?php

namespace app\app\controller;

use think\Controller;
use think\Db;

class Vitalcapacity extends Base{
    
    protected $color = ['#FF5656','#FFAB00','#5AD06D','#6492F6','#3967D6'];
    
    protected $result_end_data_mould = [
        'name'=>'',
        'value'=>'',
        'unit'=>'',
        'standard'=>'',
        'color'=>'',
        'list'=>[]
    ];

    ################################################################接口################################################################
    ################################################################接口################################################################
    ################################################################接口################################################################

    

    // 今日数据
    public function vitalcapacity_today_data($data = ['aud_id'=>11]){
        $result = Db::query("
        select 
        jump_num,
        jump_time,
        jump_kcal,
        aud_id,
        CAST(GETDATE() AS DATE) as today_date
        from app_card_skip_data
        where 
        CAST(record_time AS DATE) = CAST(GETDATE() AS DATE)
        and aud_id = ".$data['aud_id']."
        ");
        
        $return_data = ['jump_num'=>0,'jump_time'=>0,'jump_kcal'=>0];
        foreach ($result as $key => $value) {
            $return_data['jump_num'] = $value['jump_num']+$return_data['jump_num'];
            $return_data['jump_time'] = $value['jump_time']+$return_data['jump_time'];
            $return_data['jump_kcal'] = $value['jump_kcal']+$return_data['jump_kcal'];
        }
        
        return $this->msg(0,'success',$return_data);
    }

    // 数据记录
    public function skip_record_data($data = ['aud_id'=>11,'num'=>50,'time'=>60,'kcal'=>150,'record_time'=>'','jump_type'=>'free']){

        if($data['record_time'] == ''){
            $data['record_time'] = date('Y-m-d H:i:s');
        }

        $user_data = Db::table('app_card_skip_data')->insert([  
            'create_time' => date('Y-m-d H:i:s'),  
            'record_time' => $data['record_time'],  
            'jump_num' => $data['num'],  
            'jump_time' => $data['time'],  
            'jump_kcal' => $data['kcal'],  
            'aud_id' => $data['aud_id'],  
            'acd_id' => 5,  
            'jump_type' => $data['num']  
        ]);

        if($user_data){
            return $this->msg(0,'success');
        }else {
            return $this->msg(10001,'数据记录失败');
        }
    }

    // 运动曲线
    // public function skip_motion_curve($data = ['aud_id'=>'11','time_data'=>['2024']]){
    public function skip_motion_curve($data = ['aud_id'=>'11','time_data'=>['2024','04']]){
    // public function skip_motion_curve($data = ['aud_id'=>'11','time_data'=>['2024','12','31']]){
        // 确保传入的$data是一个数组且包含所需的键  
        if (!is_array($data) || !isset($data['aud_id'], $data['time_data'])) {  
            return $this->msg(10001,'参数格式错误'); // 返回false或抛出异常  
        }  
  
        $audId = $data['aud_id'];  
        $timeData = $data['time_data'];  
  
        // 根据$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(10002,'时间参数错误'); // 无效的时间数据格式  
        }  
  
        // 使用查询构造器进行查询  
        $result = Db::name('app_card_skip_data')->where($map)->field('jump_num,jump_time,jump_kcal,aud_id,record_time,jump_type,DATEPART(hour, record_time) AS hour,DATEPART(day, record_time) AS day,DATEPART(month, record_time) AS month')->order('record_time')->select();
        
        $return_data = ['jump_num'=>['max'=>0,'list'=>[]],'jump_time'=>['max'=>0,'list'=>[]],'jump_kcal'=>['max'=>0,'list'=>[]]];
        if(count($timeData) == 3){
            $key_condition = 'hour';
        }else if(count($timeData) == 2){
            $key_condition = 'day';
        }else if(count($timeData) == 1){
            $key_condition = 'month';
        }
        // dump($result);
        // die;
        foreach ($result as $key => $value) {
            if(!array_key_exists($value[$key_condition], $return_data['jump_num']['list'])){
                $return_data['jump_num']['list'][$value[$key_condition]] = $value['jump_num'];
            }else{
                $return_data['jump_num']['list'][$value[$key_condition]] = $return_data['jump_num']['list'][$value[$key_condition]] + $value['jump_num'];
            }
            if(!array_key_exists($value[$key_condition], $return_data['jump_time']['list'])){
                $return_data['jump_time']['list'][$value[$key_condition]] = $value['jump_time'];
            }else{
                $return_data['jump_time']['list'][$value[$key_condition]] = $return_data['jump_time']['list'][$value[$key_condition]] + $value['jump_time'];
            }
            if(!array_key_exists($value[$key_condition], $return_data['jump_kcal']['list'])){
                $return_data['jump_kcal']['list'][$value[$key_condition]] = $value['jump_kcal'];
            }else{
                $return_data['jump_kcal']['list'][$value[$key_condition]] = $return_data['jump_kcal']['list'][$value[$key_condition]] + $value['jump_kcal'];
            }
        }

        foreach ($return_data as $key => $value) {
            foreach ($value['list'] as $k => $v) {
                $return_data[$key]['max'] = $v>$return_data[$key]['max']?$v:$return_data[$key]['max'];
            }
        }

        return $this->msg(0,'success',$return_data);

        // dump($result);
        // dump($return_data);
        // die;
        // return $result;  
    }

    // 记录肺活量数据
    public function vitalcapacity_data_set($data = ['aud_id'=>'20','times'=>[1991,2001,2024],'token'=>'0dafb98a10995c98b5a33b7d59d986ca','record_time'=>"2024-05-08 11:49:21"]){
        if(count(input()) > 0){
            $data = input();
        }
        if(!array_key_exists('aud_id', $data) || !array_key_exists('times', $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']);
        $average_num = intval(array_sum($data['times'])/count($data['times']));

        $user_msg = Db::name('app_user_data')->where(['id'=>$data['aud_id']])->field('grade,gender')->find();
        $str = "sex = ".$user_msg['gender']." and ".$user_msg['grade']." <= ".$average_num;
        
        $user_achievement = Db::name('pc_vitalcapacity_standard')->where("sex = ".$user_msg['gender']." and ".$user_msg['grade']." <= ".$average_num)->order($user_msg['grade'] .' desc')->limit(1)->select();
        
        $result = Db::name('app_card_vitalcapacity_data')->insert([
            'one'=>$data['times'][0],
            'two'=>$data['times'][1],
            'three'=>$data['times'][2],
            'average'=>$average_num,
            'score'=>$user_achievement[0]['score'],
            'average_data'=>$average_num.','.$user_achievement[0]['level'].',无',
            'aud_id'=>$data['aud_id'],
            'acd_id'=>8,
            'create_time'=>date('Y-m-d H:i:s'),
            'record_time'=>array_key_exists('record_time', $data)?$data['record_time']:date('Y-m-d H:i:s')
        ]);
        
        
        if($result){
            return $this->msg(0,'success');
        }else{
            return $this->msg(10001,'数据记录失败');
        }

    }



    

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

    


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



}