public function actionApply()
 {
     $trainId = Yii::$app->request->get('id');
     $trainInfo = Train::findOne(['id' => $trainId]);
     if ($trainInfo['status'] != Train::BEGIN_SIGN_UP) {
         throw new ServerErrorHttpException('该课程的状态不是开始报名,谢谢。');
     }
     if (Yii::$app->user->isGuest) {
         return $this->redirect(['/user/register', 'train_id' => $trainId]);
     } else {
         $userId = Yii::$app->user->id;
         if (Yii::$app->user->identity->status != '1') {
             throw new ServerErrorHttpException('您目前的状态是未审核,不能报名课程,谢谢。');
         }
         $trainLevelInfo = Level::findOne($trainInfo['level_id']);
         if ($trainLevelInfo['order'] != Yii::$app->user->identity->level_order + 1) {
             throw new ServerErrorHttpException('您目前没有权限报名该级别下的课程,谢谢。');
         }
         //检查用户参与的课程,状态不是取消的都算是已经参与了报名
         $isExist = TrainUsers::getUserIsExistTrainStatus($userId, $trainInfo['level_id']);
         if (!empty($isExist)) {
             throw new ServerErrorHttpException('您已经参与了该级别下的培训课程,请耐心等待培训结果,谢谢。');
         }
         //            if (strtotime($trainInfo['sign_up_begin_time']) < time()) {
         //                throw new ServerErrorHttpException('此课程已过期。');
         //            }
         $noPassInfo = TrainUsers::findOne(['user_id' => Yii::$app->user->id, 'level_id' => Yii::$app->user->identity->level_id + 1, 'status' => TrainUsers::NO_PASS]);
         if (!empty($noPassInfo)) {
             $noPassTrainInfo = Train::findOne($noPassInfo['train_id']);
             if (!empty($noPassTrainInfo)) {
                 if ($noPassTrainInfo['end_time'] > $trainInfo['begin_time']) {
                     throw new ServerErrorHttpException('训课课程的开课时间一定要大于未通过考试培训课程结束时间。');
                 }
             }
         }
         //报名成功,给出用户的序号
         $trainUsersOrder = TrainUsers::getMaxSignUpOrder($trainId);
         if (empty($trainUsersOrder)) {
             $trainUsersOrder = 1;
         } else {
             $trainUsersOrder = $trainUsersOrder + 1;
         }
         $data = ['train_id' => $trainId, 'user_id' => $userId, 'status' => TrainUsers::APPROVED, 'practice_score' => 0, 'theory_score' => 0, 'rule_score' => 0, 'level_id' => $trainInfo['level_id'], 'orders' => $trainUsersOrder];
         $model = new TrainUsers();
         $model->setAttributes($data);
         if ($model->save()) {
             $trainUserId = $model->id;
             $trainUser = TrainUsers::findOne(['id' => $trainUserId]);
             $trainName = Train::getOneTrainNameById($trainUser['train_id']);
             $data = ['orders' => $trainUser['orders'], 'trainName' => $trainName];
             //更新级别信息的信息
             UsersLevel::updateAll(['train_id' => $trainId], ['user_id' => $userId, 'level_id' => Yii::$app->user->identity->level_id]);
             return $this->render('/train/apply-success', ['data' => $data]);
         } else {
             throw new ServerErrorHttpException('' . json_encode($model->errors, JSON_UNESCAPED_UNICODE));
         }
     }
 }
 public function actionApply()
 {
     $activityId = Yii::$app->request->get('id');
     $activityInfo = Activity::findOne(['id' => $activityId]);
     if ($activityInfo['status'] != Activity::BEGIN_SIGN_UP) {
         throw new ServerErrorHttpException('该活动的状态不是开始报名,谢谢。');
     }
     if (Yii::$app->user->isGuest) {
         return $this->redirect(['/user/register', 'activity_id' => $activityId]);
     } else {
         $userId = Yii::$app->user->id;
         $CredentialsNumber = UsersInfo::getCredentialsNumberByUserId($userId);
         if (Yii::$app->user->identity->status != '1') {
             throw new ServerErrorHttpException('系统错误,原因:您目前的状态是未审核,不能报名课程,谢谢。');
         }
         $activityLevelInfo = Level::findOne(['id' => $activityInfo['level_id']]);
         if ($activityLevelInfo['order'] != Yii::$app->user->identity->level_order + 1) {
             throw new ServerErrorHttpException('系统错误,原因:您目前没有权限报名该级别下的课程,谢谢。');
         }
         //检查用户参与的课程,状态不是取消的都算是已经参与了报名
         $isExist = ActivityUsers::getUserIsExistActivityStatus($userId, $activityInfo['level_id']);
         if (!empty($isExist)) {
             throw new ServerErrorHttpException('系统错误,原因:您已经参与了该级别下的培训课程,请耐心等待培训结果,谢谢。');
         }
         //报名成功,给出用户的序号
         $activityUsersOrder = ActivityUsers::getActivityUsersOrder($userId, $activityId);
         if (empty($activityUsersOrder)) {
             $activityUsersOrder = 1;
         }
         $transaction = Yii::$app->db->beginTransaction();
         $data = ['activity_id' => $activityId, 'user_id' => $userId, 'status' => ActivityUsers::APPROVED, 'practice_score' => 0, 'theory_score' => 0, 'rule_score' => 0, 'level_id' => $activityInfo['level_id'], 'orders' => $activityUsersOrder];
         $model = new ActivityUsers();
         $model->setAttributes($data);
         if ($model->save()) {
             $activityUserId = $model->id;
             $activityUser = ActivityUsers::findOne(['id' => $activityUserId]);
             $activityName = Activity::getOneActivityNameById($activityUser['activity_id']);
             $data = ['orders' => $activityUser['orders'], 'activityName' => $activityName];
             //新增一条用户和级别对应的信息
             $userLevelModel = new UsersLevel();
             $userLevelModel->user_id = $userId;
             $userLevelModel->activity_id = $activityId;
             $userLevelModel->level_id = $activityInfo['level_id'];
             $userLevelModel->credentials_number = $CredentialsNumber;
             if (!$userLevelModel->save()) {
                 $transaction->rollBack();
                 throw new ServerErrorHttpException('更新状态错误,原因:' . json_encode($userLevelModel->errors, JSON_UNESCAPED_UNICODE) . '!');
             } else {
                 $transaction->commit();
             }
             return $this->render('/activity/apply-success', ['data' => $data]);
         } else {
             throw new ServerErrorHttpException('系统错误,原因:' . json_encode($model->errors, JSON_UNESCAPED_UNICODE));
         }
     }
 }
 /**
  * Updates an existing TrainUsers model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $trainInfo = Train::findOne(['id', $model->train_id]);
     $sourceStatus = $model->status;
     $userInfo = Users::findOne($model->user_id);
     $model->userName = $userInfo['username'];
     $model->trainName = $trainInfo->name;
     $statusList[TrainUsers::SIGN] = ['list' => [TrainUsers::APPROVED => TrainUsers::$statusList[TrainUsers::APPROVED]]];
     $statusList[TrainUsers::APPROVED] = ['list' => [TrainUsers::ENROLL => TrainUsers::$statusList[TrainUsers::ENROLL], TrainUsers::NO_APPROVED => TrainUsers::$statusList[TrainUsers::NO_APPROVED]]];
     $statusList[TrainUsers::ENROLL] = ['list' => [TrainUsers::DOING => TrainUsers::$statusList[TrainUsers::DOING]]];
     $statusList[TrainUsers::DOING] = ['list' => [TrainUsers::END => TrainUsers::$statusList[TrainUsers::END]]];
     $statusList[TrainUsers::END] = ['list' => [TrainUsers::END => TrainUsers::$statusList[TrainUsers::END]]];
     $statusList[TrainUsers::NO_APPROVED] = ['list' => [TrainUsers::APPROVED => TrainUsers::$statusList[TrainUsers::APPROVED]]];
     $statusList[TrainUsers::CANCEL] = ['list' => [TrainUsers::CANCEL => TrainUsers::$statusList[TrainUsers::CANCEL]]];
     $statusList[TrainUsers::PASS] = ['list' => [TrainUsers::PASS => TrainUsers::$statusList[TrainUsers::PASS]]];
     $statusList[TrainUsers::NO_PASS] = ['list' => [TrainUsers::NO_PASS => TrainUsers::$statusList[TrainUsers::NO_PASS]]];
     if (Yii::$app->request->isPost) {
         $updateParams = Yii::$app->request->post();
         $transaction = Yii::$app->db->beginTransaction();
         if ($model->load($updateParams) && $model->save()) {
             if ($updateParams['TrainUsers']['status'] == TrainUsers::END) {
                 //如果结束状态,必须评分
                 if ($sourceStatus == TrainUsers::END) {
                     $transaction->rollBack();
                     throw new ServerErrorHttpException('请提交不同的状态!');
                 }
                 if ($updateParams['TrainUsers']['practice_score'] <= 0 || $updateParams['TrainUsers']['theory_score'] <= 0 || $updateParams['TrainUsers']['rule_score'] <= 0) {
                     $transaction->rollBack();
                     throw new ServerErrorHttpException('每项评分都必须大于零!');
                 }
                 if ($updateParams['TrainUsers']['practice_score'] > 100 || $updateParams['TrainUsers']['theory_score'] > 100 || $updateParams['TrainUsers']['rule_score'] > 100) {
                     $transaction->rollBack();
                     throw new ServerErrorHttpException('每项评分都不能超过100分!');
                 }
                 //根据3个单项评分计算全部评价内容包括考勤
                 $result = $this->getSource($model->train_id, $model->user_id, $model->practice_score, $model->theory_score, $model->rule_score);
                 $model->score_appraise = $result['scoreAppraise'];
                 $model->appraise_result = $result['performance'];
                 $model->attendance_appraise = $result['attendanceComment'];
                 $model->practice_comment = $result['practiceComment'];
                 $model->theory_comment = $result['theoryComment'];
                 $model->rule_comment = $result['rulesComment'];
                 $model->total_comment = $result['commentAppraise'];
                 $model->result_comment = $result['scoreAppraise'];
                 $model->status = $result['status'];
                 if ($model->save()) {
                     //如果通过更新晋级信息
                     if ($model->status == TrainUsers::PASS) {
                         $trainCode = $trainInfo['code'];
                         $trainPeriodNum = $trainInfo['period_num'];
                         $levelInfo = Level::findOne($model->level_id);
                         $certificateNumber = $this->getCertificateNumber($trainCode, $trainPeriodNum, sprintf("%04d", $model->orders), $levelInfo['code']);
                         $res = UsersLevel::updateAll(['status' => 1, 'certificate_number' => $certificateNumber, 'update_time' => date('Y-m-d H:i:s', time()), 'update_user' => Yii::$app->admin->identity->username], ['user_id' => $model->user_id, 'train_id' => $model->train_id]);
                         if ($res) {
                             //通过发短信
                             $content = "您好,您的培训课程考试已经通过,请进入教练员管理系统-我的课程查看。【教练系统】";
                             $result = $this->sendMessage($content, Messages::TRAIN_PASS, $model->user_id, $userInfo['mobile_phone'], '1');
                             if ($result != '0') {
                                 $transaction->rollBack();
                                 throw new ServerErrorHttpException('' . $result . '!');
                             } else {
                                 $transaction->commit();
                                 return $this->redirect(['view', 'id' => $model->id]);
                             }
                         } else {
                             $transaction->rollBack();
                             throw new ServerErrorHttpException(json_encode($res, JSON_UNESCAPED_UNICODE) . '!');
                         }
                     } else {
                         UsersLevel::updateAll(['status' => UsersLevel::TRAIN_NO_PASS], ['user_id' => $userInfo['id'], 'train_id' => $model->train_id]);
                         //未通过发短信
                         $content = "尊敬的学员,您没有通过培训课程,谢谢!【教练系统】";
                         $result = $this->sendMessage($content, Messages::TRAIN_NO_PASS, $model->user_id, $userInfo['mobile_phone'], '1');
                         if ($result != '0') {
                             $transaction->rollBack();
                             throw new ServerErrorHttpException('' . $result . '!');
                         }
                         $transaction->commit();
                         return $this->redirect(['view', 'id' => $model->id]);
                     }
                 } else {
                     $transaction->rollBack();
                     throw new ServerErrorHttpException(json_encode($model->errors, JSON_UNESCAPED_UNICODE) . '!');
                 }
             } else {
                 if ($updateParams['TrainUsers']['status'] == TrainUsers::ENROLL) {
                     if ($sourceStatus == TrainUsers::ENROLL) {
                         $transaction->rollBack();
                         throw new ServerErrorHttpException('请提交不同的状态!');
                     }
                     //录取发短信
                     $content = "您好,很高兴的通知您,您报名的培训班已通过审核,请按指定日期前往培训地点缴费上课。【教练系统】";
                     $result = $this->sendMessage($content, Messages::TRAIN_SIGN_SUCCESS, $model->user_id, $userInfo['mobile_phone'], '1');
                     if ($result != '0') {
                         $transaction->rollBack();
                         throw new ServerErrorHttpException('' . $result . '!');
                     }
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $model->id]);
                 } elseif ($updateParams['TrainUsers']['status'] == TrainUsers::NO_APPROVED) {
                     if ($sourceStatus == TrainUsers::NO_APPROVED) {
                         $transaction->rollBack();
                         throw new ServerErrorHttpException('请提交不同的状态!');
                     }
                     //未通过审核发短信
                     $content = "尊敬的学员,您没有通过培训课程审核,谢谢!【教练系统】";
                     $result = $this->sendMessage($content, Messages::TRAIN_SIGN_ERROR, $model->user_id, $userInfo['mobile_phone'], '1');
                     if ($result != '0') {
                         $transaction->rollBack();
                         throw new ServerErrorHttpException('' . $result . '!');
                     }
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $model->id]);
                 } elseif ($updateParams['TrainUsers']['status'] == TrainUsers::DOING) {
                     if ($sourceStatus == TrainUsers::DOING) {
                         $transaction->rollBack();
                         throw new ServerErrorHttpException('请提交不同的状态!');
                     }
                     //培训开始发短信
                     $content = "尊敬的学员您好,您已缴费成功,请根据课培训班的课程安排按时上下课,谢谢!【教练系统】";
                     $result = $this->sendMessage($content, Messages::TRAIN_DOING, $model->user_id, $userInfo['mobile_phone'], '1');
                     if ($result != '0') {
                         $transaction->rollBack();
                         throw new ServerErrorHttpException('' . $result . '!');
                     }
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $model->id]);
                 } else {
                     if ($updateParams['TrainUsers']['practice_score'] != 0 || $updateParams['TrainUsers']['theory_score'] != 0 || $updateParams['TrainUsers']['rule_score'] != 0) {
                         throw new ServerErrorHttpException('只有更新结束状态需要评分!');
                     }
                     $transaction->commit();
                     Yii::$app->getSession()->setFlash('success', '更新成功!');
                     return $this->redirect(['view', 'id' => $model->id]);
                 }
             }
         } else {
             $transaction->rollBack();
             throw new ServerErrorHttpException('' . json_encode($model->errors, JSON_UNESCAPED_UNICODE) . '!');
         }
     } else {
         return $this->render('update', ['model' => $model, 'statusList' => $statusList]);
     }
 }
 public function actionApply()
 {
     $trainId = Yii::$app->request->get('train_id');
     $trainInfo = Train::findOne(['id' => $trainId]);
     if ($trainInfo['status'] != Train::BEGIN_SIGN_UP) {
         throw new ServerErrorHttpException('该课程的状态不是开始报名,谢谢。');
     }
     if (Yii::$app->user->isGuest) {
         return $this->redirect(['/user/register', 'train_id' => $trainId]);
     } else {
         $userId = Yii::$app->user->id;
         if (Yii::$app->user->identity->status != '1') {
             throw new ServerErrorHttpException('您目前的状态是未审核,不能报名课程,谢谢。');
         }
         $trainLevelInfo = Level::findOne($trainInfo['level_id']);
         if ($trainLevelInfo['order'] != Yii::$app->user->identity->level_order + 1) {
             throw new ServerErrorHttpException('您目前没有权限报名该级别下的课程,谢谢。');
         }
         //检查用户参与的课程,状态不是取消,注册未完成,审核未通过,未通过的都算是已经参与了报名
         $isExist = TrainUsers::getUserIsExistTrainStatus($userId, $trainInfo['level_id']);
         if (!empty($isExist)) {
             throw new ServerErrorHttpException('您已经参与了该级别下的培训课程,请耐心等待培训结果,谢谢。');
         }
         //报名成功,给出用户的序号
         $trainUsersOrder = TrainUsers::getTrainUsersOrder($trainId);
         if (empty($trainUsersOrder)) {
             $trainUsersOrder = 1;
         } else {
             $trainUsersOrder = $trainUsersOrder + 1;
         }
         $transaction = Yii::$app->db->beginTransaction();
         //如果用户未完成报名流程,更新状态为审核中
         $isSignExist = TrainUsers::getUserIsExistTrainStatusSign($userId, $trainInfo['level_id']);
         if (!empty($isSignExist)) {
             TrainUsers::updateAll(['status' => TrainUsers::APPROVED], ['id' => $isSignExist['id']]);
             $trainName = Train::getOneTrainNameById($isSignExist['train_id']);
             $data = ['orders' => $isSignExist['orders'], 'trainName' => $trainName];
             $transaction->commit();
             return $this->render('/user/apply-success', ['data' => $data]);
         } else {
             //如果没有记录,新增一条
             $data = ['train_id' => $trainId, 'user_id' => $userId, 'status' => TrainUsers::APPROVED, 'practice_score' => 0, 'theory_score' => 0, 'rule_score' => 0, 'level_id' => $trainInfo['level_id'], 'orders' => $trainUsersOrder];
             $model = new TrainUsers();
             $model->setAttributes($data);
             if ($model->save()) {
                 $trainName = Train::getOneTrainNameById($model->train_id);
                 $data = ['orders' => $model->orders, 'trainName' => $trainName];
                 $transaction->commit();
                 return $this->render('/user/apply-success', ['data' => $data]);
             } else {
                 throw new ServerErrorHttpException('系统错误,原因:' . json_encode($model->errors, JSON_UNESCAPED_UNICODE));
             }
         }
     }
 }
 public function actionLevelUp()
 {
     $levelInfo = Level::findOne(['id' => Yii::$app->user->identity->level_id + 1]);
     $usersLevel = UsersLevel::findOne(['user_id' => Yii::$app->user->id, 'level_id' => Yii::$app->user->identity->level_id]);
     $usersLevelStatusCurrent = $usersLevel['status'];
     if ($usersLevelStatusCurrent != UsersLevel::SEND_CARD) {
         throw new ServerErrorHttpException('当前注册状态不符合!');
     }
     $loginDuration = Users::getLoginDuration(Yii::$app->user->id);
     $loginDurationDiff = $levelInfo['login_duration'] - $loginDuration;
     if ($loginDurationDiff > 0) {
         throw new ServerErrorHttpException('当前注册时长不符合!');
     }
     $scoreDiff = $levelInfo['score'] - Yii::$app->user->identity->score;
     if ($scoreDiff > 0) {
         throw new ServerErrorHttpException('当前一般活动积分不符合!');
     }
     $creditDiff = $levelInfo['credit'] - Yii::$app->user->identity->credit;
     if ($creditDiff > 0) {
         throw new ServerErrorHttpException('当前公益活动积分不符合!');
     }
     $transaction = Yii::$app->db->beginTransaction();
     UsersLevel::updateAll(['status' => UsersLevel::LEVEL_UP], ['user_id' => Yii::$app->user->id, 'level_id' => Yii::$app->user->identity->level_id]);
     $userInfo = UsersInfo::findOne(['user_id' => Yii::$app->user->id]);
     //新增一条用户和级别对应的信息
     $userLevelModel = new UsersLevel();
     $userLevelModel->user_id = Yii::$app->user->id;
     $userLevelModel->level_id = Yii::$app->user->identity->level_id + 1;
     $userLevelModel->credentials_number = $userInfo['credentials_number'];
     $userLevelModel->district = $userInfo['account_location'];
     $userLevelModel->receive_address = $userInfo['contact_address'];
     $userLevelModel->postcode = $userInfo['contact_postcode'];
     $userLevelModel->status = UsersLevel::NO_TRAIN;
     if (!$userLevelModel->save()) {
         $transaction->rollBack();
         throw new ServerErrorHttpException('更新状态错误,原因:' . json_encode($userLevelModel->errors, JSON_UNESCAPED_UNICODE) . '!');
     } else {
         Users::updateAll(['level_id' => Yii::$app->user->identity->level_id + 1, 'level_order' => Yii::$app->user->identity->level_order + 1], ['id' => Yii::$app->user->id]);
         $transaction->commit();
     }
     return $this->redirect('/user-center/index');
 }
 /**
  * Finds the Level model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Level the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Level::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }