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}";
     }
 }
 public function ajaxpost(Request $request)
 {
     //      $pid = Request::input('personid');
     $pid = Input::get('personid');
     if (empty($pid)) {
         $msg = "请输入: <strong>患者[身份证]</strong>...";
     } else {
         $user = Buildcase::where('personid', '=', $pid)->orderBy('personid', 'ASC')->orderBy('build_at', 'DESC')->first();
         if ($user) {
             $duty = $user->duty;
             $dutyname = '';
             $nursename = '';
             $dietitianname = '';
             if ($duty) {
                 $dutyname = '前次[责任卫教]: <strong>' . User::findOrFail($user->duty)->name . '</strong>';
             }
             $nurse = $user->nurse;
             if ($nurse) {
                 $nursename = ', [护理卫教]: <strong>' . User::findOrFail($user->nurse)->name . '</strong>';
             }
             $dietitian = $user->dietitian;
             if ($dietitian) {
                 $dietitianname = ', [营养卫教]: <strong>' . User::findOrFail($user->dietitian)->name . '</strong>';
             }
         }
         if (empty($duty) && empty($nurse) && empty($dietitian)) {
             $msg = "该患者无建案纪录...";
         } else {
             $msg = $dutyname . $nursename . $dietitianname;
         }
     }
     $data = array('status' => 'success', 'msg' => $msg);
     return response()->json($data);
 }
 public function page($uuid = null, $end = null)
 {
     $users = Auth::user();
     if ($uuid == null) {
         $patient = Patientprofile::where('user_id', '=', $users->id)->first();
         if ($patient != null) {
             $hospital_no = $patient->hospital_no;
         } else {
             $err_msg = '请重新登入!';
         }
         if (isset($hospital_no) && $hospital_no != null) {
             $uuid = $hospital_no->hospital_no_uuid;
         } else {
             $err_msg = '没有血糖资料!';
         }
     } else {
         $hospital_no = null;
         $hospital_no = HospitalNo::find($uuid);
         if ($hospital_no != null) {
             $patient = $hospital_no->patient;
             //                    if($hospital_no -> patient_user_id != $users -> id){
             //                        $user_feature = Feature::where('href', '=', '/patient') -> first() -> hasfeatures() -> where('user_id', '=', $users -> id)->first();
             //                        if($user_feature == null){
             //                            $hospital_no = null;
             //                        }
             //                    }
         }
         if ($hospital_no == null) {
             $err_msg = '您没有权限查看此血糖资料!';
         }
     }
     if (!isset($hospital_no) || $hospital_no == null) {
         return view('bdata.error', compact('err_msg'));
     }
     if ($end > date('Y-m-d')) {
         return redirect('/bdata/' . $uuid);
     }
     if ($end == null) {
         $data['next'] = null;
         $end = date('Y-m-d');
     } else {
         $next = date('Y-m-d', strtotime("1 month", strtotime($end)));
         if ($end == date('Y-m-d')) {
             $data['next'] = null;
         } else {
             if ($next == date('Y-m-d')) {
                 $data['next'] = "/bdata/" . $uuid;
             } else {
                 $data['next'] = "/bdata/" . $uuid . "/" . $next;
             }
         }
     }
     $start = date('Y-m-d', strtotime("-1 month", strtotime($end)));
     $data['previous'] = "/bdata/" . $uuid . "/" . $start;
     if ($hospital_no->count() == 0) {
         $invalid = true;
         return view('bdata.bdata', compact('invalid'));
     }
     $data['displayname'] = $hospital_no->hospital_no_displayname;
     $data['patient_displayname'] = User::find($hospital_no->patient_user_id)->name;
     $data['patient_bday'] = $patient->pp_birthday;
     $data['patient_age'] = $patient->pp_age;
     $data['patient_id'] = $patient->pp_patientid;
     $blood_records = $hospital_no->blood_sugar()->where('calendar_date', '<=', $end)->where('calendar_date', '>', $start)->orderBy('calendar_date', 'DESC')->get();
     $stat = $this->get_stat($blood_records);
     $notes = $this->get_notes($blood_records);
     $blood_records = $this->fillup($blood_records, $start, $end);
     $data['food_categories'] = FoodCategory::all();
     $food_records = $this->get_has_food($uuid);
     $soap_link = "";
     //            if(isset($user_feature) && $user_feature != null){
     $soap_link = '/soap/' . $uuid;
     //            }
     $contact_data = $hospital_no->contact_info;
     if ($contact_data != null) {
         $duty = Buildcase::where('hospital_no_uuid', '=', $uuid)->orderBy('duty_at', 'desc')->first();
         if ($duty == null) {
             $duty = $hospital_no->nurse_user_id;
         } else {
             $duty = $duty->duty;
         }
         $contact_data["nurse_name"] = User::find($duty)->name;
         $contact_data["trace_method"] = $this->convert_trace_method($contact_data["trace_method"]);
         $contact_data["contact_time"] = $this->convert_contact_time($contact_data["contact_time"]);
         $contact_data["phone"] = User::find($hospital_no->patient_user_id)->phone;
         $contact_data["email"] = User::find($hospital_no->patient_user_id)->email;
     }
     $goal = $hospital_no->hba1c_goal;
     $goal_matrix = $hospital_no->hba1c_goal_matrix();
     Session::put('uuid', $uuid);
     return view('bdata.bdata', compact('blood_records', 'data', 'food_categories', 'stat', 'food_records', 'soap_link', 'notes', 'contact_data', 'goal', 'goal_matrix'));
 }
 public function ccreate($patientid)
 {
     $pps = Patientprofile::where('pp_patientid', '=', $patientid)->first();
     if (!is_null($pps)) {
         $err_msg = '患者资料已存在!!';
         return view('patient.create', compact('err_msg'));
     } else {
         $carbon = Carbon::today();
         // $format = $carbon->format('Y-m-d H:i:s');
         $year = $carbon->year;
         $bsms = BSM::orderBy('bm_order')->get();
         $areas = Patientprofile::$_area;
         //            $doctors = Patientprofile::$_doctor;
         $doctors = User::where('position', '=', '门诊医生')->orWhere('position', '=', '住院医生')->orderBy('name', 'ASC')->lists('name', 'id');
         $doctors = array('' => '不详') + $doctors;
         $doctor = Buildcase::where('personid', '=', $patientid)->first()->doctor;
         // 建案医生
         $sources = Patientprofile::$_source;
         $occupations = Patientprofile::$_occupation;
         $languages = Patientprofile::$_language;
         $actkind = Patientprofile::$_actkind;
         $err_msg = null;
         EventController::SaveEvent('patientprofile', 'create(创建)');
         return view('patient.create', compact('err_msg', 'year', 'bsms', 'areas', 'doctors', 'doctor', 'sources', 'occupations', 'languages', 'actkind', 'patientid'));
     }
 }