public function post_user_soap(Request $request)
 {
     $uuid = Session::get('uuid');
     $uid = Auth::user()->id;
     $calendar_date = Session::get('calendar_date');
     $user_soap = UserSoap::where('hospital_no_uuid', '=', $uuid)->first();
     $user_id = Auth::User()->id;
     $bsugar = HospitalNo::find($uuid)->blood_sugar()->where('calendar_date', '=', $calendar_date)->first();
     DB::beginTransaction();
     try {
         if ($bsugar == null && $calendar_date != null) {
             $bsugar = new BloodSugar();
             $bsugar->calendar_date = $calendar_date;
             $bsugar->early_morning = null;
             $bsugar->morning = null;
             $bsugar->breakfast_before = null;
             $bsugar->breakfast_after = null;
             $bsugar->lunch_before = null;
             $bsugar->lunch_after = null;
             $bsugar->dinner_before = null;
             $bsugar->dinner_after = null;
             $bsugar->sleep_before = null;
             $bsugar->note = null;
             $bsugar->hospital_no_uuid = $uuid;
             $bsugar->user_id = $user_id;
             $bsugar->save();
         } else {
             if ($user_soap == null) {
                 $user_soap = new UserSoap();
             }
             $user_soap->hospital_no_uuid = $uuid;
             $user_soap->s_text = $request->s_text;
             $user_soap->o_text = $request->o_text;
             $user_soap->a_text = $request->a_text;
             $user_soap->p_text = $request->p_text;
             $user_soap->e_text = $request->e_text;
             $user_soap->r_text = $request->r_text;
             $user_soap->health_date = $request->health_date;
             $user_soap->soa_nurse_class_pks = $request->soa_nurse_class_pks;
             if (isset($request["confirm"]) && $request->confirm == "true") {
                 $user_soap->is_finished = true;
             } else {
                 $user_soap->is_finished = false;
             }
             $user_soap->save();
         }
         $user_soap_history = new UserSoapHistory();
         $user_soap_history->s_text = $request->s_text;
         $user_soap_history->o_text = $request->o_text;
         $user_soap_history->a_text = $request->a_text;
         $user_soap_history->p_text = $request->p_text;
         $user_soap_history->e_text = $request->e_text;
         $user_soap_history->r_text = $request->r_text;
         $user_soap_history->health_date = $request->health_date;
         $user_soap_history->user_id = Auth::user()->id;
         $user_soap_history->created_at = $user_soap->created_at;
         $user_soap_history->soa_nurse_class_pks = $request->soa_nurse_class_pks;
         if (isset($request["confirm"]) && $request->confirm == "true") {
             $user_soap_history->is_finished = true;
         } else {
             $user_soap_history->is_finished = false;
         }
         if ($user_soap != null) {
             $user_soap_history->user_soap_pk = $user_soap->user_soap_pk;
         }
         if ($bsugar != null) {
             $user_soap_history->blood_sugar_pk = $bsugar->blood_sugar_pk;
         }
         if (isset($request["history"])) {
             $history = $user_soap->history()->find($request["history"]);
             if ($history != null) {
                 $user_soap_history->old_pk = $history->user_soap_history_pk;
                 $history->is_visible = 0;
                 $history->save();
             }
         }
         $user_soap_history->save();
         if ($user_soap->is_finished == true) {
             // 完成
             $buildcase = Buildcase::where('hospital_no_uuid', '=', $uuid)->orderBy('build_at', 'desc')->first();
             if ($buildcase) {
                 $buildcase->soap_status = 1;
                 $buildcase->save();
             }
             $buildcase = Buildcase::where('hospital_no_uuid', '=', $uuid)->where('duty', '=', $uid)->first();
             if ($buildcase) {
                 $buildcase->duty_status = 2;
                 $buildcase->save();
             }
             $buildcase = Buildcase::where('hospital_no_uuid', '=', $uuid)->where('nurse', '=', $uid)->first();
             if ($buildcase) {
                 $buildcase->nurse_status = 2;
                 $buildcase->save();
             }
             $buildcase = Buildcase::where('hospital_no_uuid', '=', $uuid)->where('dietitian', '=', $uid)->first();
             if ($buildcase) {
                 $buildcase->dietitian_status = 2;
                 $buildcase->save();
             }
         } else {
             // 暫存
             $buildcase = Buildcase::where('hospital_no_uuid', '=', $uuid)->orderBy('build_at', 'desc')->first();
             if ($buildcase) {
                 $buildcase->soap_status = 0;
                 $buildcase->save();
             }
             $buildcase = Buildcase::where('hospital_no_uuid', '=', $uuid)->where('duty', '=', $uid)->first();
             if ($buildcase) {
                 $buildcase->duty_status = 1;
                 $buildcase->save();
             }
             $buildcase = Buildcase::where('hospital_no_uuid', '=', $uuid)->where('nurse', '=', $uid)->first();
             if ($buildcase) {
                 $buildcase->nurse_status = 1;
                 $buildcase->save();
             }
             $buildcase = Buildcase::where('hospital_no_uuid', '=', $uuid)->where('dietitian', '=', $uid)->first();
             if ($buildcase) {
                 $buildcase->dietitian_status = 1;
                 $buildcase->save();
             }
         }
         DB::commit();
         return "success /bdata/{$uuid}";
     } catch (\Exception $e) {
         DB::rollback();
         return "{$e}";
     }
 }
 private function get_soap_stat($start, $end, $user_id = null)
 {
     $query = UserSoapHistory::select(DB::raw('count(*) as count, users.name as nurse, user_soap_history.user_id as pk'))->where('user_soap_history.health_date', '>=', $start)->where('user_soap_history.health_date', '<', $end)->where('user_soap_history.is_visible', '=', 1)->leftJoin('users', 'user_soap_history.user_id', '=', 'users.id');
     $data = array();
     if ($user_id != null) {
         $records = $query->where('user_soap_history.user_id', '=', $user_id)->first();
         $total_count = UserSoapHistory::select(DB::raw('count(*) as total'))->where('user_soap_history.health_date', '>=', $start)->where('user_soap_history.health_date', '<', $end)->where('user_soap_history.is_visible', '=', 1)->first();
         if ($total_count['total'] > 0) {
             array_push($data, array('nurse' => $records['nurse'], 'count' => $records['count'], 'total' => $total_count['total']));
         }
         $data['count'] = $total_count['total'];
         $data['name'] = $records['nurse'];
     } else {
         $records = $query->groupBy('user_soap_history.user_id')->get();
         $counter = 0;
         for ($i = 0; $i < count($records); $i++) {
             $count = $records[$i]['count'];
             $data[$i] = array('nurse' => $records[$i]['nurse'], 'count' => $count, 'nurse_detail' => $records[$i]['pk']);
             $counter += $count;
         }
         $data['count'] = $counter;
         $data['name'] = '全院';
     }
     return $data;
 }