/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params, $pagesize = 20)
 {
     ///应用组用户只可查看所在组的应用的模板
     $task_con = "";
     if (Yii::$app->user->getIdentity()->admin != 1) {
         $app_con = "svr_gid in (0";
         $query = DcmdUserGroup::find()->andWhere(['uid' => Yii::$app->user->getId()])->asArray()->all();
         if ($query) {
             foreach ($query as $item) {
                 $app_con .= "," . $item['gid'];
             }
         }
         $app_con .= ")";
         $query = DcmdApp::find()->where($app_con)->asArray()->all();
         $task_con = "app_id in (0";
         foreach ($query as $item) {
             $task_con .= "," . $item['app_id'];
         }
         $task_con .= ")";
     }
     $query = DcmdTaskTemplate::find()->where($task_con)->orderBy('task_tmpt_name');
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pagesize' => $pagesize]]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['task_tmpt_id' => $this->task_tmpt_id, 'task_cmd_id' => $this->task_cmd_id, 'svr_id' => $this->svr_id, 'app_id' => $this->app_id, 'update_env' => $this->update_env, 'concurrent_rate' => $this->concurrent_rate, 'timeout' => $this->timeout, 'process' => $this->process, 'auto' => $this->auto, 'utime' => $this->utime, 'ctime' => $this->ctime, 'opr_uid' => $this->opr_uid]);
     $query->andFilterWhere(['like', 'task_tmpt_name', $this->task_tmpt_name])->andFilterWhere(['like', 'task_cmd', $this->task_cmd])->andFilterWhere(['like', 'svr_name', $this->svr_name])->andFilterWhere(['like', 'task_arg', $this->task_arg])->andFilterWhere(['like', 'comment', $this->comment]);
     return $dataProvider;
 }
 /**
 * Creates a new DcmdTask model.
  If creation is successful, the browser will be redirected to the 'view' page.
 * @return mixed
 */
 public function actionCreate($task_tmpt_id)
 {
     ///同一业务组普通用户可以创建任务
     if (Yii::$app->user->getIdentity()->admin != 1) {
         $query = DcmdTaskTemplate::findOne($task_tmpt_id);
         $temp = DcmdApp::findOne($query['app_id']);
         $query = DcmdUserGroup::findOne(['uid' => Yii::$app->user->getId(), 'gid' => $temp['svr_gid']]);
         if ($query == NULL) {
             Yii::$app->getSession()->setFlash('error', "对不起, 你没有权限!");
             return $this->redirect(array('dcmd-task-template/view', 'id' => $task_tmpt_id));
         }
     } else {
         ///统一系统组的admin才可创建
         $query = DcmdTaskTemplate::findOne($task_tmpt_id);
         $temp = DcmdApp::findOne($query['app_id']);
         $query = DcmdUserGroup::findOne(['uid' => Yii::$app->user->getId(), 'gid' => $temp['sa_gid']]);
         if ($query == NULL) {
             Yii::$app->getSession()->setFlash('error', "对不起, 你没有权限!");
             return $this->redirect(array('dcmd-task-template/view', 'id' => $task_tmpt_id));
         }
     }
     if (Yii::$app->request->post()) {
         ///提交新建任务
         $post_task = Yii::$app->request->post()['DcmdTask'];
         $dcmd_task = new DcmdTask();
         $dcmd_task->task_name = Yii::$app->request->post()['task_cmd_prv'] . '-' . $post_task['task_name'];
         $dcmd_task->task_cmd = $post_task['task_cmd'];
         $dcmd_task->depend_task_id = 0;
         $dcmd_task->depend_task_name = "NULL";
         $dcmd_task->app_id = $post_task['app_id'];
         $dcmd_task->app_name = $post_task['app_name'];
         $dcmd_task->svr_id = $post_task['svr_id'];
         $dcmd_svr = DcmdService::findOne($post_task['svr_id']);
         $dcmd_task->node_multi_pool = $dcmd_svr->node_multi_pool;
         $dcmd_task->err_msg = " ";
         $dcmd_task->svr_name = $post_task['svr_name'];
         $dcmd_task->svr_path = $post_task['svr_path'];
         $dcmd_task->tag = $post_task['tag'];
         $dcmd_task->update_env = $post_task['update_env'];
         $dcmd_task->update_tag = $post_task['update_tag'];
         $dcmd_task->state = 0;
         $dcmd_task->freeze = 0;
         $dcmd_task->valid = 1;
         $dcmd_task->pause = 0;
         $dcmd_task->concurrent_rate = $post_task['concurrent_rate'];
         $dcmd_task->timeout = $post_task['timeout'];
         $dcmd_task->auto = $post_task['auto'];
         $dcmd_task->process = $post_task['process'];
         $dcmd_task->utime = date('Y-m-d H:i:s');
         $dcmd_task->ctime = $dcmd_task->utime;
         $dcmd_task->opr_uid = Yii::$app->user->getId();
         $dcmd_task->comment = $post_task['comment'];
         $arg = array();
         $opr_log = new DcmdOprLog();
         $opr_log->log_table = "dcmd_task";
         $opr_log->opr_type = 1;
         $opr_log->sql_statement = "insert task:" . $dcmd_task->task_name;
         $opr_log->ctime = date('Y-m-d H:i:s');
         $opr_log->opr_uid = Yii::$app->user->getId();
         $opr_log->save();
         foreach (Yii::$app->request->post() as $k => $v) {
             if (substr($k, 0, 3) == "Arg") {
                 $arg[substr($k, 3)] = $v;
             }
         }
         $dcmd_task->task_arg = arrToXml($arg);
         if ($dcmd_task->save()) {
             ///设备池默认属性
             $svr_pool_attr_def = array();
             $tmp_query = DcmdServicePoolAttrDef::find()->asArray()->all();
             foreach ($tmp_query as $item) {
                 $svr_pool_attr_def[$item['attr_name']] = $item['def_value'];
             }
             ///保存服务池子信息
             if (array_key_exists("selection", Yii::$app->request->post())) {
                 $svr_pool = Yii::$app->request->post()["selection"];
                 foreach ($svr_pool as $k => $svr_pool_id) {
                     $svr_query = DcmdService::findOne($dcmd_task->svr_id);
                     $svr_pool_query = DcmdServicePool::findOne($svr_pool_id);
                     $svr_pool_node_query = DcmdServicePoolNode::find()->andWhere(['svr_pool_id' => $svr_pool_id])->asArray()->all();
                     $dcmd_task_service_pool = new DcmdTaskServicePool();
                     $dcmd_task_service_pool->task_id = $dcmd_task->task_id;
                     $dcmd_task_service_pool->task_cmd = $dcmd_task->task_cmd;
                     $dcmd_task_service_pool->svr_pool = $svr_pool_query['svr_pool'];
                     $dcmd_task_service_pool->svr_pool_id = $svr_pool_id;
                     $dcmd_task_service_pool->env_ver = $svr_pool_query['env_ver'];
                     $dcmd_task_service_pool->repo = $svr_pool_query['repo'];
                     $dcmd_task_service_pool->run_user = $svr_query['run_user'];
                     $dcmd_task_service_pool->undo_node = count($svr_pool_node_query);
                     $dcmd_task_service_pool->doing_node = 0;
                     $dcmd_task_service_pool->finish_node = 0;
                     $dcmd_task_service_pool->fail_node = 0;
                     $dcmd_task_service_pool->ignored_fail_node = 0;
                     $dcmd_task_service_pool->ignored_doing_node = 0;
                     $dcmd_task_service_pool->state = 0;
                     $dcmd_task_service_pool->utime = date('Y-m-d H:i:s');
                     $dcmd_task_service_pool->ctime = $dcmd_task_service_pool->utime;
                     $dcmd_task_service_pool->opr_uid = Yii::$app->user->getId();
                     if (!$dcmd_task_service_pool->save()) {
                         Yii::$app->getSession()->setFlash('error', "保存服务池子失败");
                     } else {
                         $tm = date('Y-m-d H:i:s');
                         ///保存服务池属性
                         $svr_pool_attr = array();
                         $tmp_query = DcmdServicePoolAttr::find()->andWhere(['svr_pool_id' => $svr_pool_id])->asArray()->all();
                         foreach ($tmp_query as $item) {
                             $svr_pool_attr[$item['attr_name']] = $item['attr_value'];
                         }
                         foreach ($svr_pool_attr_def as $name => $value) {
                             $dcmd_task_service_pool_attr = new DcmdTaskServicePoolAttr();
                             $dcmd_task_service_pool_attr->task_id = $dcmd_task->task_id;
                             $dcmd_task_service_pool_attr->app_id = $dcmd_task->app_id;
                             $dcmd_task_service_pool_attr->svr_id = $dcmd_task->svr_id;
                             $dcmd_task_service_pool_attr->svr_pool_id = $svr_pool_id;
                             $dcmd_task_service_pool_attr->attr_name = $name;
                             if (array_key_exists($name, $svr_pool_attr)) {
                                 $dcmd_task_service_pool_attr->attr_value = $svr_pool_attr[$name];
                             } else {
                                 $dcmd_task_service_pool_attr->attr_value = $value;
                             }
                             $dcmd_task_service_pool_attr->utime = $tm;
                             $dcmd_task_service_pool_attr->ctime = $tm;
                             $dcmd_task_service_pool_attr->opr_uid = Yii::$app->user->getId();
                             $dcmd_task_service_pool_attr->save();
                         }
                     }
                 }
             }
             ///选择服务池子
             return $this->redirect(array('select-service-pool-node', 'task_id' => $dcmd_task->task_id));
         } else {
             var_dump($dcmd_task->getErrors());
             exit;
         }
     }
     ///新建任务
     $query = DcmdTaskTemplate::findOne($task_tmpt_id);
     $model = new DcmdTask();
     $app = "";
     $args = "";
     $task_cmd_prv = "";
     if ($query) {
         $task_cmd = DcmdTaskCmd::findOne(['task_cmd' => $query['task_cmd']]);
         $model->task_cmd = $query['task_cmd'];
         ///$task_cmd->ui_name;
         $model->depend_task_id = 0;
         $model->depend_task_name = "";
         $model->app_id = $query['app_id'];
         $qapp = DcmdApp::findOne($query['app_id']);
         $model->app_name = $qapp['app_name'];
         $model->svr_id = $query['svr_id'];
         $model->svr_name = $query['svr_name'];
         $model->concurrent_rate = $query['concurrent_rate'];
         $model->timeout = $query['timeout'];
         $model->auto = $query['auto'];
         $model->process = $query['process'];
         $model->update_env = $query['update_env'];
         $task_cmd_prv = $task_cmd->ui_name . '-' . date("YmdHis");
         $ret = DcmdService::findOne($model->svr_id);
         $model->svr_path = $ret['svr_path'];
         $args = $this->showTaskArg($query['task_arg'], $query['task_cmd_id']);
         if ($ret) {
             $app_id = $ret['app_id'];
             $ret = DcmdApp::findOne($app_id);
             if ($ret) {
                 $app = $ret['app_name'];
             }
         }
     }
     ///获取任务模板的服务池子
     $query = DcmdTaskTemplateServicePool::find()->andWhere(['task_tmpt_id' => $task_tmpt_id])->asArray()->all();
     $svr_pool = "svr_pool_id in (0";
     foreach ($query as $item) {
         $svr_pool .= "," . $item['svr_pool_id'];
     }
     $svr_pool .= ")";
     $query = DcmdServicePool::find()->where($svr_pool);
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $searchModel = new DcmdServicePoolSearch();
     return $this->render('create', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'model' => $model, 'app' => $app, 'args' => $args, 'task_cmd_prv' => $task_cmd_prv]);
 }
 /**
  * Finds the DcmdTaskTemplate model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return DcmdTaskTemplate the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = DcmdTaskTemplate::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 /**
  * Creates a new DcmdTaskTemplateServicePool model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate($task_tmpt_id)
 {
     if (Yii::$app->user->getIdentity()->admin != 1) {
         Yii::$app->getSession()->setFlash('success', NULL);
         Yii::$app->getSession()->setFlash('error', "对不起,你没有权限!");
         return $this->redirect(array('dcmd-task-template/view', 'id' => $task_tmpt_id));
     }
     if (Yii::$app->request->post()) {
         $tm = date('Y-m-d H:i:s');
         if (array_key_exists("selection", Yii::$app->request->post())) {
             foreach (Yii::$app->request->post()['selection'] as $k => $v) {
                 $dcmd_task_template_service_pool = new DcmdTaskTemplateServicePool();
                 $dcmd_task_template_service_pool->task_tmpt_id = $task_tmpt_id;
                 $dcmd_task_template_service_pool->utime = $tm;
                 $dcmd_task_template_service_pool->ctime = $tm;
                 $dcmd_task_template_service_pool->opr_uid = Yii::$app->user->getId();
                 $dcmd_task_template_service_pool->svr_pool_id = $v;
                 $dcmd_task_template_service_pool->save();
             }
             Yii::$app->getSession()->setFlash('success', "添加服务池子成功!");
         } else {
             Yii::$app->getSession()->setFlash('success', "没有选择服务池!");
         }
         return $this->redirect(['dcmd-task-template/view', 'id' => $task_tmpt_id]);
     }
     $model = new DcmdTaskTemplateServicePool();
     $query = DcmdTaskTemplate::findOne($task_tmpt_id);
     $svr_id = 0;
     if ($query) {
         $svr_id = $query['svr_id'];
     }
     ///获取未添加的服务池子列表
     $query = DcmdTaskTemplateServicePool::find()->andWhere(['task_tmpt_id' => $task_tmpt_id])->asArray()->all();
     $exist_svr_pool = "svr_pool_id not in ( 0";
     if ($query) {
         foreach ($query as $item) {
             $exist_svr_pool .= "," . $item['svr_pool_id'];
         }
     }
     $exist_svr_pool .= ") and svr_id=" . $svr_id;
     $query = DcmdServicePool::find()->where($exist_svr_pool);
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $searchModel = new DcmdServicePoolSearch();
     ///获取任务模板名称
     $task_tmpt_name = "";
     $query = DcmdTaskTemplate::findOne($task_tmpt_id);
     if ($query) {
         $task_tmpt_name = $query['task_tmpt_name'];
     }
     $model->task_tmpt_id = $task_tmpt_id;
     return $this->render('create', ['model' => $model, 'task_tmpt_id' => $task_tmpt_id, 'task_tmpt_name' => $task_tmpt_name, 'searchModel' => $searchModel, 'dataProvider' => $dataProvider]);
 }
 /**
  * Deletes an existing DcmdTaskCmd model.
  * If deletion is successful, the browser will be redirected to the 'index' page.
  * @param integer $id
  * @return mixed
  */
 public function actionDelete($id)
 {
     if (Yii::$app->user->getIdentity()->admin != 1) {
         Yii::$app->getSession()->setFlash('success', NULL);
         Yii::$app->getSession()->setFlash('error', "对不起,你没有权限!");
         return $this->redirect(array('dcmd-task-cmd/index'));
     }
     ///确定是否有任务模板使用
     $ret = DcmdTaskTemplate::findOne(['task_cmd_id' => $id]);
     if ($ret) {
         Yii::$app->getSession()->setFlash('error', '有任务模板使用该脚本, 删除失败!');
         return $this->redirect(['index']);
     }
     ///delete from dcmd_task_cmd_arg
     DcmdTaskCmdArg::deleteAll('task_cmd_id = ' . $id);
     $this->findModel($id)->delete();
     Yii::$app->getSession()->setFlash('success', '删除成功!');
     return $this->redirect(['index']);
 }