/**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     //
     if ($request->hasFile('sports_file')) {
         // 上传文件
         $xml = $request->file('sports_file');
         if ($xml->getClientOriginalExtension() != 'xml') {
             return 'File extension is not xml!';
         }
         $new_name = 'xml_' . date('Y-m-d H:m:s') . Auth::user()->id . '.xml';
         $xml->move(base_path() . '/public/file/sports/', $new_name);
         $obj = simplexml_load_file($_SERVER['DOCUMENT_ROOT'] . '/file/sports/' . $new_name);
         for ($i = 0; $i < count($obj->xpath('sportsentry')); ++$i) {
             $e = $obj->xpath('sportsentry')[$i];
             $entry = new sportsentry();
             $entry->user_id = Auth::user()->id;
             $entry->type = $e->xpath('type')[0];
             $entry->value = $e->xpath('value')[0];
             $entry->last_time = $e->xpath('last_time')[0];
             $entry->calories = $e->xpath('calories')[0];
             $entry->start_time = $e->xpath('start_time')[0];
             $entry->save();
         }
     } else {
         // 手动输入
         $entry = new sportsentry();
         $entry->user_id = Auth::user()->id;
         $entry->type = sportsentry::TYPE_RUN;
         $entry->value = $request->input('dis');
         $entry->last_time = $request->input('l_h') * 3600 + $request->input('l_m') * 60 + $request->input('l_s');
         $entry->calories = $request->input('cal');
         $entry->start_time = $request->input('date') . ' ' . $request->input('s_h') . ':' . $request->input('s_m') . ':' . $request->input('s_s');
         $entry->save();
     }
     //        return Redirect::back();
     return Redirect::to('/player/sports/data');
 }
 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]);
 }