<?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'=>'','record_time'=>'','type'=>'year']){
        $time_data = '';
        if($data['type'] == 'year'){
            $time_data = date('Y-m',strtotime($data['record_time']));
        }else if($data['type'] == 'month'){
            $time_data = date('Y-m-d',strtotime($data['record_time']));
        }else if($data['type'] == 'day'){
            $time_data = date('Y-m-d H:i:s',strtotime($data['record_time']));
        }else{
            return $this->msg(10001,'type参数错误');
        }
        $user_data = Db::table('app_card_skip_data')->where([
            'CONVERT(varchar(10), record_time, 120)' => "",  
            'jump_num' => $data['num'],  
            'jump_time' => $data['time'],  
            'jump_kcal' => $data['kcal'],  
            'aud_id' => $data['aud_id'],  
            'acd_id' => 5,  
            'jump_type' => $data['num']  
        ]);

        dump($user_data);
    }

    



    

    ################################################################跳绳数据卡片接口################################################################
    ################################################################跳绳数据卡片接口################################################################


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

    // 数据记录
    function generateRandomJumpData($n = 666) {  
        $return_data = [];  
        $startDate = new \DateTime('2022-01-01 00:00:00');  
        $endDate = new \DateTime('2024-12-31 23:59:59');  
      
        // 为了保证在$startDate和$endDate之间随机生成日期，先确定两个日期之间的天数差  
        $diff = $endDate->diff($startDate)->format('%a');  
      
        for ($i = 0; $i < $n; $i++) {  
            // 生成随机日期  
            $randomDateTime = $startDate->modify(sprintf('+%d seconds', rand(0, $endDate->getTimestamp() - $startDate->getTimestamp())))->format('Y-m-d H:i:s');
            // $randomDate = $startDate->modify(sprintf('+%d days', rand(0, $diff)))->format('Y-m-d');  
      
            // 生成随机jump_num  
            $jump_num = rand(30, 70);  
      
            // 生成随机jump_time  
            $jump_time = rand(60, 200);  
      
            // 生成随机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  
            ];  
        }  
        $user_data = Db::table('app_card_skip_data')->insertAll($return_data);
        dump($user_data);
        dump($return_data);
        die;
    } 
   



    
    

}