/**
  * Creates a new Application model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Application();
     $appModel = new AppApplicant(['scenario' => 'primary-create']);
     $coAppModel = new AppApplicant(['scenario' => 'co-create']);
     $appFormModel = new AppForm();
     //加载数据并验证
     if ($model->load(Yii::$app->request->post()) && $appModel->load(Yii::$app->request->post()) && $coAppModel->load(Yii::$app->request->post()) && $appFormModel->load(Yii::$app->request->post()) && $model->validate() && $appModel->validate() && ($model->is_joint_application == 1 ? $coAppModel->validate() : true) && ($model->is_joint_application == 1 ? $appModel->email_address != $coAppModel->email_address : true)) {
         //开启事务
         $transaction = Yii::$app->db->beginTransaction();
         try {
             if (!$appModel->id_user) {
                 $user = User::createUser(Dict::USER_ROLE_CLIENT, ['User' => ['first_name' => $appModel->first_name, 'last_name' => $appModel->last_name, 'mid_name' => $appModel->mid_name, 'email' => $appModel->email_address], 'Client' => ['id_group' => $model->id_group]]);
                 //设置applicant的id_user
                 $appModel->id_user = $user->id_user;
                 SendEmail::email(DictEmail::EMAIL_INVITE_USER, [$user]);
             }
             if ($model->is_joint_application && !$coAppModel->id_user) {
                 $user = User::createUser(Dict::USER_ROLE_CLIENT, ['User' => ['first_name' => $coAppModel->first_name, 'last_name' => $coAppModel->last_name, 'mid_name' => $coAppModel->mid_name, 'email' => $coAppModel->email_address], 'Client' => ['id_group' => $model->id_group]]);
                 //设置co-applicant的id_user
                 $coAppModel->id_user = $user->id_user;
                 SendEmail::email(DictEmail::EMAIL_INVITE_USER, [$user]);
             }
             $model->application_name = $appModel->first_name . ($model->is_joint_application ? ' and ' . $coAppModel->first_name : '') . "'s application";
             $model->charge_fee = Advisor::findOne(['id_user' => Yii::$app->user->getId()])->will_charge_fee;
             if (!$model->save()) {
                 $transaction->rollBack();
             }
             $user = Yii::$app->user->getIdentity();
             Active::operationLog($model->id_application, [$user->first_name, $user->last_name], DictActive::CREATE_APPLICATION, $user->id);
             $appModel->id_application = $model->id_application;
             $appModel->id_app_applicant_type = 1;
             if (!$appModel->save()) {
                 $transaction->rollBack();
             }
             if ($model->is_joint_application == 1) {
                 $coAppModel->id_application = $model->id_application;
                 $coAppModel->id_app_applicant_type = 2;
                 if (!$coAppModel->save()) {
                     $transaction->rollBack();
                 }
             }
             $appFormModel->id_application = $model->id_application;
             if (!$appFormModel->save()) {
                 $transaction->rollBack();
             }
             //初始化任务
             AppTask::initialize($model->id_application);
             // 将AppTaskCheckpoint中的模板转换为实例
             AppTaskCheckpoint::initialize($model->appTask);
             //创建AppUser记录
             AppUser::createByApplication($model);
             //send email to notificate advisor create application success.method email([$task_type,$id_application,$id_app_owner_task],$notify_type)
             SendEmail::email(DictEmail::EMAIL_TASK_NOTIFICATION, [DictTask::TYPE_APPLICATION_FORM, $model->id_application, 0]);
             //id_app_owner_task_type:1(app-form);0:表示没有传递id_app_owner_task
             $transaction->commit();
         } catch (Exception $e) {
             $transaction->rollBack();
         }
         return $this->redirect(['/app-form/basic-info', 'id_application' => $model->id_application]);
     }
     return $this->render('create', ['model' => $model, 'appModel' => $appModel, 'coAppModel' => $coAppModel, 'appFormModel' => $appFormModel]);
 }