<?php

namespace app\app\controller;

use think\Controller;
use think\Db;

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

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

    // 查找设备
    // $data = ['device_code'=>'asdkljiouoi']
    public function skip_device_check($data = ['device_code'=>'asdkljiouoi']){
        // $data = input();
        $result = Db::query("
        select 
        adcd.id,
        adcd.bind_account_id as activation_state,
        adds.is_del as device_state
        from app_device_code_data as adcd
        left join app_device_data as adds on adds.id = adcd.add_id
        where 
        adcd.machine_code = '".$data['device_code']."'
        ");

        if(count($result) == 1){
            return $this->msg(0,'success',['device_state'=>$result[0]['device_state'],'activation_state'=>$result[0]['activation_state']]);
        }else if(count($result) < 1){
            return $this->msg(10001,'未找到设备');
        }else{
            $this->abnormal_data_log_action(0,'device_check-设备查询出错,结果为'.count($result).'，合理值应为1或0','app_device_code_data,app_device_data');
            return $this->msg(10002,'未找到设备');
        }
    }

    // 绑定设备
    public function skip_bind_device(){

    }

    // 今日数据
    public function skip_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 get_skip_record_data(){}


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

    // 假随机数据插入
    public function generateRandomJumpData($n = 666) {  
        $return_data = [];  
        $startDate = new \DateTime('2024-01-01 00:00:00');  
        $endDate = new \DateTime('2024-12-31 23:59:59');  
      
        // 为了保证在$startDate和$endDate之间随机生成日期，先确定两个日期之间的天数差  
        $diff = $endDate->diff($startDate)->format('%a');  

        $startTime = strtotime('2024-01-01 00:00:00');  
        $endTime = strtotime('2024-12-31 23:59:59');

        
      
        for ($i = 0; $i < $n; $i++) {  
            // 生成随机日期  
            $randomSeconds = random_int($startTime, $endTime);
            $randomDateTime = date('Y-m-d H:i:s', $randomSeconds); 
      
            // 生成随机jump_num  
            $jump_num = rand(30, 70);  
      
            // 生成随机jump_time  
            $jump_time = rand(60, 150);  
      
            // 生成随机jump_kcal  
            $jump_kcal = rand(100, 400);  
      
            // 生成随机jump_type  
            $jump_types = ['free', 'time', 'num'];  
            $jump_type = $jump_types[array_rand($jump_types)];  
      
            // 固定的aud_id和acd_id  
            $aud_id = 11;  
            $acd_id = 5;  
      
            // 将生成的数据存入数组  
            $return_data[] = [  
                'create_time' => date('Y-m-d H:i:s'),  
                'record_time' => $randomDateTime,  
                'jump_num' => $jump_num,  
                'jump_time' => $jump_time,  
                'jump_kcal' => $jump_kcal,  
                'aud_id' => $aud_id,  
                'acd_id' => $acd_id,  
                'jump_type' => $jump_type  
            ];  
        }  
        // dump($return_data);
        // die;
        $user_data = Db::table('app_card_skip_data')->insertAll($return_data);
        dump($user_data);
        dump($return_data);
        die;
    } 


}