123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <?php
- namespace app\models\procedure;
- use Yii;
- use yii\base\Model;
- use app\models\entity\Accounts;
- use app\models\entity\Projecttypes;
- use app\models\entity\ProjectsLocotech;
- use app\models\entity\ProjecttypesTasktypes;
- /*
- start_projects: BEGIN
- declare tasktype integer default 0;
- declare companyId integer default 0;
- declare projectGuid varchar(255);
- declare done integer;
- declare cur cursor for select pt.tasktype_id from projecttypes_tasktypes pt where pt.projecttype_id = projecttype order by pt.priority;
- declare continue handler for not found set done = true;
- 1 Получаем ид компании инициатора
- set companyId = (select company from accounts where id = initiator);
-
- 1.1 если null то умираем
- if (companyId is null) then leave start_projects; end if;
- 2. Проверяем на существование тип проекта
- set tasktype = (select id from projecttypes where id = projecttype and company = companyId);
- 2.1 Если null то умираем
- if (tasktype is null) then leave start_projects; end if;
- 3 генерируем uuid
- set projectGuid = (select UUID());
-
-
- 4 создаем проект
- insert into projects_locotech
- (action, tasks, company, status, loco_type, loco_number, urgent, created, uuid)
- values
- (projecttype, projectGuid, companyId, 1, locoserie, loconumber, 0, NOW(), projectcomment);
-
-
- 4.1 проверяем прошла ли запись
- set companyId = (select id from projects_locotech where tasks = projectGuid);
- if (companyId is null) then leave start_projects; end if;
-
-
- 5 чистим все таски у созданного проекта
- update projects_locotech
- set tasks = ''
- where id = companyId;
- 6. закидываем нужные таски в проект
-
- open cur;
- task_loop: loop
- fetch cur into tasktype;
- if done then leave task_loop;
- end if;
- call add_task_into_project(initiator, companyId, tasktype);
- end loop;
- close cur;
-
- select companyId into project;
- END
- */
- class StartProjectForLocoModel extends Model
- {
- private $projId;
- private $locoserie;
- private $initiator;
- private $loconumber;
- private $projecttype;
- private $tasktype = 0;
- private $companyId = 0;
- private $projectcomment;
- private $projectGuid = '';
-
- //declare cur cursor for select pt.tasktype_id from projecttypes_tasktypes pt where pt.projecttype_id = projecttype order by pt.priority;
- //declare continue handler for not found set done = true;
- public function execute($initiator, $projecttype, $locoserie, $loconumber = null, $projectcomment = null)
- {
-
- $this->initiator = $initiator;
- $this->projecttype = $projecttype;
- $this->locoserie = $locoserie;
- $this->loconumber = $loconumber;
- $this->projectcomment = $projectcomment;
-
- //1 Получаем ид компании инициатора
- //1.1 если null то умираем
- if(!$this->GetCompanyId()) {
- return false;
- }
-
- // 2. Проверяем на существование тип проекта
- // 2.1 Если null то умираем
- if(!$this->IsissetProjecttype()) {
- return false;
- }
-
- // 3 генерируем uuid
- $this->generateUuid();
- //4 создаем проект
- //4.1 проверяем прошла ли запись
- //$this->buildProject();
- // 5 чистим все таски у созданного проекта
- //$this->CleanProjectTasks();
-
- // 6. получаем список тасков на тип проекта
- // 6.1 закидываем нужные таски в проект
- $listTasks = $this->GetListTasks();
-
- if ($this->addTaskInProject($listTasks)) {
- return $this->projId;
- }
-
- return false;
- //echo "<pre>";
- //var_dump($this->GetListTasks());
- //echo "</pre>";
- //exit;
- }
-
- private function GetCompanyId()
- {
- $accountsEntity = Accounts::findOne($this->initiator);
- $this->companyId = $accountsEntity->company;
- if ( null == $this->companyId){
- return false;
- }
- return true;
- }
-
- private function IsissetProjecttype()
- {
- $projecttypesEntity = Projecttypes::find()->where(['id' => $this->projecttype ,'company' => $this->companyId])->one();
- $this->tasktype = $projecttypesEntity->id;
- if ( null == $this->tasktype){
- return false;
- }
- return true;
- }
-
- private function generateUuid()
- {
- $query = 'select UUID()';
- $uuid = Yii::$app->db->createCommand('select UUID() as uuid')->queryOne();
- $this->projectGuid = $uuid["uuid"];
- if ( null == $this->projectGuid){
- return false;
- }
- return true;
- }
-
- private function buildProject()
- {
- $projectsLocotechEntity = new ProjectsLocotech();
- $projectsLocotechEntity->action = $this->projecttype;
- $projectsLocotechEntity->tasks = $this->projectGuid;
- $projectsLocotechEntity->company = $this->companyId;
- $projectsLocotechEntity->status = 1;
- $projectsLocotechEntity->loco_type = $this->locoserie;
- $projectsLocotechEntity->loco_number = $this->loconumber;
- $projectsLocotechEntity->urgent = 0;
- $projectsLocotechEntity->created = date('Y-m-d H:i:S');
- $projectsLocotechEntity->uuid = $this->projectcomment;
- if ($projectsLocotechEntity->save()) {
- $projectsLocotechOne = ProjectsLocotech::findOne(['tasks' => $this->projectGuid]);
- $this->projId = $projectsLocotechOne->id;
- if ( null == $this->projId ){
- return false;
- }
- return true;
- }
- return false;
- }
-
- private function CleanProjectTasks()
- {
- $projectsLocotechEntity = ProjectsLocotech::findOne($this->projId);
- $projectsLocotechEntity->tasks = '';
- if ( !$projectsLocotechEntity->save()) {
- return false;
- }
- }
-
- private function GetListTasks()
- {
- $projecttypesTasktypesEntity = ProjecttypesTasktypes::find()
- ->select(['tasktype_id'])
- ->where(['projecttype_id' => $this->projecttype])
- ->orderBy(['priority' => SORT_ASC])
- ->all();
- return $projecttypesTasktypesEntity;
- }
-
- private function addTaskInProject($listTasks)
- {
- if (is_array($listTasks)) {
- foreach ( $listTasks as $task) {
- //var_dump($this->initiator, $this->projId, $task->tasktype_id); echo "\n";
- $addTaskIntoProjectModel = new AddTaskIntoProjectModel();
- $addTaskIntoProjectModel->execute($this->initiator, $this->projId, $task->tasktype_id);
- }
- }
- return true;
- }
- }
|