/**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index()
 {
     // 完成
     $today_run = sportsentry::where('user_id', Auth::user()->id)->where('type', sportsentry::TYPE_RUN)->where('start_time', '>=', '2015-12-04 00:00:00')->get();
     $distance = 0;
     $calory = 0;
     $time = 0;
     foreach ($today_run as $entry) {
         $distance += $entry->value;
         $calory += $entry->calories;
         $time += $entry->last_time;
     }
     // 目标
     $target_distance = 15000;
     $target_calory = 1000;
     $target_time = 16000;
     //
     $all_run = sportsentry::where('user_id', Auth::user()->id)->where('type', sportsentry::TYPE_RUN)->orderBy('start_time')->get();
     $total_dis = 0;
     $total_cal = 0;
     $total_time = 0;
     foreach ($all_run as $r) {
         $total_dis += $r->value;
         $total_cal += $r->calories;
         $total_time += $r->last_time;
     }
     return view('player.sports.user_act', ['distance' => $distance, 'calory' => $calory, 'time' => $time, 'target_distance' => $target_distance, 'target_calory' => $target_calory, 'target_time' => $target_time, 'all_run' => $all_run, 'total_dis' => $total_dis, 'total_cal' => $total_cal, 'total_time' => $total_time]);
 }
 public function getJsonData()
 {
     $xdata = array();
     $speeddata = array();
     $elevationdata = array();
     $hrdata = array();
     $all_run = sportsentry::where('user_id', Auth::user()->id)->where('type', sportsentry::TYPE_RUN)->orderBy('start_time')->get();
     foreach ($all_run as $r) {
         array_push($speeddata, round($r->value, 0));
         array_push($elevationdata, round($r->last_time, 0));
         array_push($hrdata, round($r->calories, 0));
     }
     $speed = array("name" => "距离", "data" => $speeddata, "unit" => "m", "type" => "area", "valueDecimals" => 1);
     $elevation = array("name" => "时间", "data" => $elevationdata, "unit" => "s", "type" => "line", "valueDecimals" => 0);
     $hr = array("name" => "卡路里", "data" => $hrdata, "unit" => "cal", "type" => "area", "valueDecimals" => 0);
     $datasets = array($speed, $elevation, $hr);
     $result = array("xData" => $xdata, "datasets" => $datasets);
     return json_encode($result);
 }
 /**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index()
 {
     // health
     //$health_entries     = healthentry::where('user_id', Auth::user()->id)->orderBy('id')->get();
     // sleep
     // if(Cache::has('bs'.Auth::user()->id)){
     // $bad_sleep = Cache::get('bs'.Auth::user()->id);
     // $mid_sleep = Cache::get('ms'.Auth::user()->id);
     // $good_sleep = Cache::get('gs'.Auth::user()->id);
     // }else{
     $sleep_entries = healthentry::where('user_id', Auth::user()->id)->where('type', healthentry::TYPE_SLEEP)->where('begin_time', '>=', date('Y-m-d') . ' 00:00:00')->orderBy('id')->get();
     $bad_sleep = 0;
     $mid_sleep = 0;
     $good_sleep = 0;
     foreach ($sleep_entries as $sleep_entry) {
         if ($sleep_entry->level == healthentry::LEVEL_BAD) {
             $bad_sleep += $sleep_entry->value;
         }
         if ($sleep_entry->level == healthentry::LEVEL_MID) {
             $mid_sleep += $sleep_entry->value;
         }
         if ($sleep_entry->level == healthentry::LEVEL_GOOD) {
             $good_sleep += $sleep_entry->value;
         }
     }
     // Cache::put('bs'.Auth::user()->id, $bad_sleep, '10');
     // Cache::put('ms'.Auth::user()->id, $mid_sleep, '10');
     // Cache::put('gs'.Auth::user()->id, $good_sleep, '10');
     // }
     // temperature
     $t_entries = healthentry::where('user_id', Auth::user()->id)->where('type', healthentry::TYPE_TEMPERATURE)->where('begin_time', '>=', date('Y-m-d') . ' 00:00:00')->get();
     $avg_t = 0;
     foreach ($t_entries as $t_entry) {
         $avg_t += $t_entry->value;
     }
     if ($t_entries->count() == 0) {
         $avg_t = 0;
     } else {
         $avg_t = round($avg_t / $t_entries->count(), 1);
     }
     // heartrate
     $hr_entries = healthentry::where('user_id', Auth::user()->id)->where('type', healthentry::TYPE_HEARTRATE)->where('begin_time', '>=', date('Y-m-d') . ' 00:00:00')->get();
     $avg_hr = 0;
     foreach ($hr_entries as $hr_entry) {
         $avg_hr += $hr_entry->value;
     }
     if ($hr_entries->count() == 0) {
         $avg_hr = 0;
     } else {
         $avg_hr = round($avg_hr / $hr_entries->count(), 1);
     }
     // blood_pressure
     $bp_entries = healthentry::where('user_id', Auth::user()->id)->where('type', healthentry::TYPE_BLOODPRESSURE)->where('begin_time', '>=', date('Y-m-d') . ' 00:00:00')->get();
     $avg_bp_high = 0;
     $avg_bp_low = 0;
     foreach ($bp_entries as $bp_entry) {
         $avg_bp_high += $bp_entry->value;
         $avg_bp_low += $bp_entry->value2;
     }
     if ($bp_entries->count() == 0) {
         $avg_bp_high = 0;
         $avg_bp_low = 0;
     } else {
         $avg_bp_high = round($avg_bp_high / $bp_entries->count(), 1);
         $avg_bp_low = round($avg_bp_low / $bp_entries->count(), 1);
     }
     // sports
     $sports_entries = sportsentry::where('user_id', Auth::user()->id)->where('start_time', '>=', date('Y-m-d') . ' 00:00:00')->orderBy('id')->get();
     $running_distance = 0;
     $calories = 0;
     $running_time = 0;
     foreach ($sports_entries as $sports_entry) {
         if ($sports_entry->type == sportsentry::TYPE_RUN) {
             $running_distance += $sports_entry->value;
             $running_time += $sports_entry->last_time;
         }
         $calories += $sports_entry->calories;
     }
     if ($running_time == 0) {
         $running_speed = 0;
     } else {
         $running_speed = round($running_distance / $running_time * 3.6, 2);
     }
     // advice
     if (Cache::has('adviceOf' . Auth::user()->id)) {
         $health_advices = Cache::get('adviceOf' . Auth::user()->id);
     } else {
         $health_advices = healthadvice::where('player_id', Auth::user()->id)->get();
         Cache::put('adviceOf' . Auth::user()->id, $health_advices, '10');
     }
     return view('player.index', ['sports_entries' => $sports_entries, 'running_distance' => $running_distance, 'running_speed' => $running_speed, 'calories' => $calories, 'bad_sleep' => $bad_sleep, 'mid_sleep' => $mid_sleep, 'good_sleep' => $good_sleep, 'avg_t' => $avg_t, 'avg_hr' => $avg_hr, 'avg_bp_high' => $avg_bp_high, 'avg_bp_low' => $avg_bp_low, 'health_advices' => $health_advices]);
 }