public function run()
 {
     DB::table('register_records')->delete();
     RegisterRecord::create(array('start' => '2015-07-02 8:30', 'status' => 0, 'fee' => 1.0, 'advice' => '', 'return_date' => '2015-08-02', 'doctor_id' => 5, 'account_id' => 1, 'user_id' => 4, 'period_id' => 1));
     RegisterRecord::create(array('start' => '2015-07-02 14:30', 'status' => 1, 'fee' => 2.0, 'advice' => '多喝水', 'return_date' => '2015-07-20', 'doctor_id' => 6, 'account_id' => 2, 'user_id' => 3, 'period_id' => 1));
     RegisterRecord::create(array('start' => '2015-07-02 16:30', 'status' => 2, 'fee' => 3.0, 'advice' => '多吃药', 'return_date' => '2015-08-20', 'doctor_id' => 7, 'account_id' => 3, 'user_id' => 2, 'period_id' => 1));
     RegisterRecord::create(array('start' => '2015-07-02 8:00', 'status' => 1, 'fee' => 4.0, 'advice' => '多运动', 'return_date' => '2015-08-01', 'doctor_id' => 8, 'account_id' => 4, 'user_id' => 1, 'period_id' => 1));
 }
 public function add_record()
 {
     $period_id = Input::get('period_id');
     $period = Period::find($period_id);
     if (!isset($period)) {
         return Response::json(array('error_code' => 2, 'message' => '无该时间段,请重新选择'));
     }
     if ($period->current >= $period->total) {
         return Response::json(array('error_code' => 3, 'message' => '已满人,请重新选择'));
     }
     $schedule = $period->schedule;
     $doctor = $schedule->doctor;
     $user_id = Session::get('user.id');
     if (Input::has('account_id')) {
         $account_id = Input::get('account_id');
         $account = RegisterAccount::find($account_id);
         if (!isset($account)) {
             return Response::json(array('error_code' => 4, 'message' => '不存在该挂号账户'));
         }
         if ($account->user_id != $user_id) {
             return Response::json(array('error_code' => 5, 'message' => '无效账户'));
         }
     } else {
         $account = RegisterAccount::where('user_id', $user_id)->first();
         if (!isset($account)) {
             return Response::json(array('error_code' => 6, 'message' => '请先申请挂号账户'));
         }
         $account_id = $account->id;
     }
     try {
         DB::beginTransaction();
         RegisterRecord::create(array('status' => 0, 'fee' => $doctor->register_fee, 'period_id' => $period->id, 'doctor_id' => $doctor->id, 'account_id' => $account_id, 'user_id' => $user_id));
         $period->current += 1;
         $period->save();
         $message = new Message();
         $message->from_uid = $user_id;
         $message->to_uid = $doctor->user->id;
         $message->content = $account->user->real_name . '挂号';
         $message->timestamp = time();
         $message->status = 3;
         $message->save();
         DB::commit();
     } catch (Exception $e) {
         DB::rollback();
         return Response::json(array('error_code' => 1, 'message' => '添加失败'));
     }
     return Response::json(array('error_code' => 0, 'message' => '添加成功'));
 }