<?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'=>[]
    ];

    ################################################################接口################################################################
    ################################################################接口################################################################
    ################################################################接口################################################################
    // 手动记录
    public function skip_manual_recording($data = ['aud_id'=>'25','r_time'=>'2024-04-11 10:10:15','num'=>'856','time'=>'137','type'=>'free','token'=>'0dafb98a10995c98b5a33b7d59d986ca']){
        if(count(input('post.')) > 0){
            $data = input('post.');
        }
        if(!array_key_exists('aud_id', $data) || !array_key_exists('r_time', $data) || !array_key_exists('num', $data) || !array_key_exists('time', $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']);
        if($this->validate_user_identity($data['aud_id']) === false){
            return $this->msg(10003);
        }
        return $this->skip_manual_recording_action($data);
    }
    // 今日数据
    public function skip_today_data($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']);
        // dump($data);
        // die;
        return $this->skip_today_data_action($data);
    }
    ################################################################接口################################################################
    ################################################################接口################################################################
    ################################################################接口################################################################
    ################################################################skip_manual_recording
    public function skip_manual_recording_action($data){
        $last_data_body = Db::table('app_card_body_data')->where(['aud_id'=>$data['aud_id']])->field('weight')->find();
        $data_set = [
            'create_time'=>date('Y-m-d H:i:s'),
            'last_update_time'=>date('Y-m-d H:i:s'),
            'jump_num'=>$data['num'],
            'jump_time'=>$data['time'],
            'jump_kcal'=>$this->skip_kcal_calculate($data['num'],$data['time'],$last_data_body['weight'])['totalCalories'],
            'aud_id'=>$data['aud_id'],
            'record_time'=>$data['r_time'],
            'jump_type'=>$data['type']
        ];
        $last_data_body = Db::table('app_card_skip_data')->insert($data_set);
        
        $result = [
            'today_jump_num'=>0,
            'today_jump_time'=>0,
            'today_jump_kcal'=>0,
        ];
        $all_data = Db::table('app_card_skip_data')->where(['aud_id'=>$data['aud_id']])->whereTime('record_time','today')->field('jump_num,jump_time,jump_kcal')->select();
        foreach ($all_data as $key => $value) {
            $result['today_jump_num'] = $result['today_jump_num']+$value['jump_num'];
            $result['today_jump_time'] = $result['today_jump_time']+$value['jump_time'];
            $result['today_jump_kcal'] = $result['today_jump_kcal']+$value['jump_kcal'];
        }
        $result['jump_num'] = $data['num'];
        $result['jump_time'] = $data['time'];
        $result['jump_kcal'] = $data_set['jump_kcal'];
        return $this->msg($result);
    }
    ################################################################skip_today_data
    public function skip_today_data_action($data){
        $all_data = Db::table('app_card_skip_data')->where(['aud_id'=>$data['aud_id']])->whereTime('record_time','today')->field('jump_num,jump_time,jump_kcal')->select();
        $last_data = Db::table('app_card_skip_data')->where(['aud_id'=>$data['aud_id']])->order('record_time desc')->field('jump_num,jump_time,jump_kcal,record_time')->find();
        $result = [
            'today_jump_num'=>0,
            'today_jump_time'=>0,
            'today_jump_kcal'=>0,
        ];
        foreach ($all_data as $key => $value) {
            $result['today_jump_num'] = $result['today_jump_num']+$value['jump_num'];
            $result['today_jump_time'] = $result['today_jump_time']+$value['jump_time'];
            $result['today_jump_kcal'] = $result['today_jump_kcal']+$value['jump_kcal'];
        }
        $result['jump_num'] = $last_data['jump_num'];
        $result['jump_time'] = $last_data['jump_time'];
        $result['jump_kcal'] = $last_data['jump_kcal'];
        return $this->msg($result);
    }
    // 今日数据
    // 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 skip_kcal_calculate($num=466, $time=166, $weight=70) {  
        // 加 bcadd(,,20)
        // 减 bcsub(,,20)
        // 乘 bcmul(,,20)
        // 除 bcdiv(,,20)
        // 将时间从秒转换为分钟
        $minutes = bcdiv($time,60,20);
        // 计算每分钟的跳绳次数  
        $jumpsPerMinute = bcdiv($num,$minutes,20); 
        // 根据跳绳次数确定MET值  
        $met;  
        if ($jumpsPerMinute < 100) {  
            $met = 8.8;  
        } else if ($jumpsPerMinute >= 100 && $jumpsPerMinute < 120) {  
            $met = 11.8;  
        } else {  
            $met = 12.3;
        }
        // 计算每分钟燃烧的卡路里
        $caloriesPerMinute = bcdiv(bcmul(bcmul($met,$weight,20),3.5,20),200,2);
        // 计算总卡路里消耗
        $totalCalories = bcmul($caloriesPerMinute,$minutes,2);
        // 返回结果  
        return [  
            'caloriesPerMinute' => $caloriesPerMinute,  
            'totalCalories' => $totalCalories  
        ];  
    }  


}