1); if (companyId is null) then leave build_project; end if; set locoserieId = (select id from locomotive_series where name like locoserie); if (locoserieId is null) then leave build_project; end if; 2 // Создание проекта АСУ СГ. set tmpGuid = (select UUID()); insert into asusg_project (text, loco_serie_id, repair_type, loco_number, section_a, section_b, section_c) values (tmpGuid, locoserieId, repairtype, loconumber, section1, section2, section3); set projectId = (select id from asusg_project where text = tmpGuid); if (projectId is null) then leave build_project; end if; 3 // генерю проект set tmpGuid = (select UUID()); insert into projects_locotech (action, tasks, company, status, loco_type, loco_number, urgent, created, uuid) values (projecttype, tmpGuid, companyId, 1, locoserie, loconumber, 0, NOW(), 'Запрос из АСУ СГ'); 4 //переопределяю ид компании set companyId = (select id from projects_locotech where tasks = tmpGuid); if (companyId is null) then leave build_project; end if; 5 // прикрепляю команды к проекту update projects_locotech set tasks = '' where id = companyId; #Проект создан, можно добавлять задачи. set current_task = 0; set current_jobtype = 0; set taskPriority = 1; 6 // генерю таски к проекту open cur_job2cmd; task_loop: loop fetch cur_job2cmd into command; if done then leave task_loop; end if; set jobtype = (select jobtype_id from jobtypes_tasktypes where tasktype_id = command); if (jobtype is null) or (jobtype <> current_jobtype) then set current_jobtype = coalesce(jobtype, 0); set taskName = (select name from jobtypes where id = current_jobtype); set taskName = concat('Должность: ', coalesce(taskName, '(не задано)')); set tmpGuid = (select UUID()); insert into tasks (status, parent_id, type, input_id, text, project_id, assignees_arr, priority, assignedby, created) values (1, 0, tasktype, companyId, tmpGuid, 0, 0, 0, 0, NOW()); set current_task = (select id from tasks where text like tmpGuid); update projects_locotech set tasks = concat(tasks, ',', cast(current_task as char)) where id = companyId; set taskPriority = 1; update tasks set text = taskName where id = current_task; end if; #set tmpGuid = (select UUID()); insert into tasks (status, parent_id, type, input_id, text, project_id, assignees_arr, priority, assignedby, created) values (1, current_task, command, companyId, taskName, 0, 0, taskPriority, 0, NOW()); #values (1, current_task, command, companyId, tmpGuid, 0, 0, taskPriority, 0, NOW()); set taskPriority = taskPriority + 1; end loop; close cur_job2cmd; END */ class AsusgBuildProjectFromMappingModel extends Model { private $companyId = 0; private $tmpGuid; private $current_task; private $current_jobtype; private $jobtype; private $taskPriority; private $taskNameprivate; private $command; private $done; private $initiator; private $projecttype; private $tasktype; private $locoserie; private $loconumber; private $asusg_job; //declare cur cursor for select a.smopp_cmd_id from asusg_job2command a where a.asusg_job_id = asusg_job order by a.cmd_order; //declare continue handler for not found set done = true; public static function execute($initiator, $projecttype, $tasktype, $locoserie, $loconumber, $repairtype,$section1,$section2,$section3) { $this->initiator = $initiator, $this->projecttype = $projecttype, $this->tasktype = $tasktype, $this->locoserie = $locoserie, $this->loconumber = $loconumber, $this->asusg_job = $asusg_job, $this->repairtype = $repairtype, $this->section1 = $section1; $this->section2 = $section2; $this->section3 = $section3; // 1. получяю ид компании if(!$this->GetCompanyId()) { return false; } // 2. Создание проекта АСУ СГ. if(!$this->GenerateAsusgProject()) { return false; } // 3. генерю проект if(!$this->GenerateProject()) { return false; } // 4. переопределяю ид компании if(!$this->RedefineCompanyId()) { return false; } // 5. прикрепляю команды к проекту if(!$this->attachCommands()) { return false; } // 6. генерю таски к проекту if(!$this->attachCommandsToProj()) { return false; } } private function GetCompanyId(){ $companiesEntity = Companies::find()->where(['id' => $this->initiator])->andWhere(['>','cmdlevel',1])->one(); $this->companyId = $companiesEntity->id; if ( null == $this->companyId) { return false; } return true; } private function GetUuid() { $uuid = Yii::$app->db->createCommand('select UUID() as uuid')->queryOne(); $this->tmpGuid = $uuid["uuid"]; if ( $this->tmpGuid ) { return true; } true false; } private function GenerateProject(){ $this->GetUuid(); $projectsLocotech = new ProjectsLocotech(); $projectsLocotech->action = $this->projecttype; $projectsLocotech->tasks = $this->tmpGuid; $projectsLocotech->company = $this->companyId; $projectsLocotech->status = 1; $projectsLocotech->loco_type = $this->locoserie; $projectsLocotech->loco_number = $this->loconumber; $projectsLocotech->urgent = 0; $projectsLocotech->created = date("Y-m-d H:i:s"); $projectsLocotech->uuid = 'Задача из АСУ СГ'; $projectsLocotech->save(); } private function GenerateAsusgProject(){ $this->GetUuid(); $AsusgProjectEntity = new AsusgProject(); $AsusgProjectEntity->text = $this->tmpGuid; $AsusgProjectEntity->loco_serie_id = $this->locoserieId; $AsusgProjectEntity->repair_type = $this->repairtype; $AsusgProjectEntity->loco_number = $this->loconumber; $AsusgProjectEntity->section_a = $this->section1; $AsusgProjectEntity->section_b = $this->section2; $AsusgProjectEntity->section_c = $this->section3; $AsusgProjectEntity->save(); $asuproj = AsusgProject::find()->where(['text' => $this->tmpGuid])->one(); if ( null == $asuproj->projectId) { return false; } return true; } private function RedefineCompanyId(){ $ProjectsLocotech = ProjectsLocotech::find()->where(['tasks' => $this->tmpGuid])->one(); $rhis->companyId = $ProjectsLocotech->id; if ( null == $rhis->companyId ) { return false; } return true; } private function attachCommands() { ProjectsLocotech::updateAll(['tasks' = ''],['id' => $this->companyId]); $this->current_task = 0; $this->current_jobtype = 0; $this->taskPriority = 1; return true; } private function attachCommandsToProj() { $AsusgJob2CommandEntity = AsusgJob2Command::find()->where(['asusg_job_id' = $tgis->asusg_job])->orderBy(['cmd_order'=> SORT_ASC])->all(); foreach( $AsusgJob2CommandEntity as $comand) { $JobtypesTasktypes = JobtypesTasktypes::find()->where(['tasktype_id' => $comand])->all(); $this->jobtype = $JobtypesTasktypes->jobtype_id; if ( (null == $this->jobtype) || ($this->jobtype <> $this->current_jobtype) ) { //... } } } }