StartProjectForLocoModel.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace app\models\procedure;
  3. use Yii;
  4. use yii\base\Model;
  5. use app\models\entity\Accounts;
  6. use app\models\entity\Projecttypes;
  7. use app\models\entity\ProjectsLocotech;
  8. use app\models\entity\ProjecttypesTasktypes;
  9. /*
  10. start_projects: BEGIN
  11. declare tasktype integer default 0;
  12. declare companyId integer default 0;
  13. declare projectGuid varchar(255);
  14. declare done integer;
  15. declare cur cursor for select pt.tasktype_id from projecttypes_tasktypes pt where pt.projecttype_id = projecttype order by pt.priority;
  16. declare continue handler for not found set done = true;
  17. 1 Получаем ид компании инициатора
  18. set companyId = (select company from accounts where id = initiator);
  19. 1.1 если null то умираем
  20. if (companyId is null) then leave start_projects; end if;
  21. 2. Проверяем на существование тип проекта
  22. set tasktype = (select id from projecttypes where id = projecttype and company = companyId);
  23. 2.1 Если null то умираем
  24. if (tasktype is null) then leave start_projects; end if;
  25. 3 генерируем uuid
  26. set projectGuid = (select UUID());
  27. 4 создаем проект
  28. insert into projects_locotech
  29. (action, tasks, company, status, loco_type, loco_number, urgent, created, uuid)
  30. values
  31. (projecttype, projectGuid, companyId, 1, locoserie, loconumber, 0, NOW(), projectcomment);
  32. 4.1 проверяем прошла ли запись
  33. set companyId = (select id from projects_locotech where tasks = projectGuid);
  34. if (companyId is null) then leave start_projects; end if;
  35. 5 чистим все таски у созданного проекта
  36. update projects_locotech
  37. set tasks = ''
  38. where id = companyId;
  39. 6. закидываем нужные таски в проект
  40. open cur;
  41. task_loop: loop
  42. fetch cur into tasktype;
  43. if done then leave task_loop;
  44. end if;
  45. call add_task_into_project(initiator, companyId, tasktype);
  46. end loop;
  47. close cur;
  48. select companyId into project;
  49. END
  50. */
  51. class StartProjectForLocoModel extends Model
  52. {
  53. private $projId;
  54. private $locoserie;
  55. private $initiator;
  56. private $loconumber;
  57. private $projecttype;
  58. private $tasktype = 0;
  59. private $companyId = 0;
  60. private $projectcomment;
  61. private $projectGuid = '';
  62. //declare cur cursor for select pt.tasktype_id from projecttypes_tasktypes pt where pt.projecttype_id = projecttype order by pt.priority;
  63. //declare continue handler for not found set done = true;
  64. public function execute($initiator, $projecttype, $locoserie, $loconumber = null, $projectcomment = null)
  65. {
  66. $this->initiator = $initiator;
  67. $this->projecttype = $projecttype;
  68. $this->locoserie = $locoserie;
  69. $this->loconumber = $loconumber;
  70. $this->projectcomment = $projectcomment;
  71. //1 Получаем ид компании инициатора
  72. //1.1 если null то умираем
  73. if(!$this->GetCompanyId()) {
  74. return false;
  75. }
  76. // 2. Проверяем на существование тип проекта
  77. // 2.1 Если null то умираем
  78. if(!$this->IsissetProjecttype()) {
  79. return false;
  80. }
  81. // 3 генерируем uuid
  82. $this->generateUuid();
  83. //4 создаем проект
  84. //4.1 проверяем прошла ли запись
  85. //$this->buildProject();
  86. // 5 чистим все таски у созданного проекта
  87. //$this->CleanProjectTasks();
  88. // 6. получаем список тасков на тип проекта
  89. // 6.1 закидываем нужные таски в проект
  90. $listTasks = $this->GetListTasks();
  91. if ($this->addTaskInProject($listTasks)) {
  92. return $this->projId;
  93. }
  94. return false;
  95. //echo "<pre>";
  96. //var_dump($this->GetListTasks());
  97. //echo "</pre>";
  98. //exit;
  99. }
  100. private function GetCompanyId()
  101. {
  102. $accountsEntity = Accounts::findOne($this->initiator);
  103. $this->companyId = $accountsEntity->company;
  104. if ( null == $this->companyId){
  105. return false;
  106. }
  107. return true;
  108. }
  109. private function IsissetProjecttype()
  110. {
  111. $projecttypesEntity = Projecttypes::find()->where(['id' => $this->projecttype ,'company' => $this->companyId])->one();
  112. $this->tasktype = $projecttypesEntity->id;
  113. if ( null == $this->tasktype){
  114. return false;
  115. }
  116. return true;
  117. }
  118. private function generateUuid()
  119. {
  120. $query = 'select UUID()';
  121. $uuid = Yii::$app->db->createCommand('select UUID() as uuid')->queryOne();
  122. $this->projectGuid = $uuid["uuid"];
  123. if ( null == $this->projectGuid){
  124. return false;
  125. }
  126. return true;
  127. }
  128. private function buildProject()
  129. {
  130. $projectsLocotechEntity = new ProjectsLocotech();
  131. $projectsLocotechEntity->action = $this->projecttype;
  132. $projectsLocotechEntity->tasks = $this->projectGuid;
  133. $projectsLocotechEntity->company = $this->companyId;
  134. $projectsLocotechEntity->status = 1;
  135. $projectsLocotechEntity->loco_type = $this->locoserie;
  136. $projectsLocotechEntity->loco_number = $this->loconumber;
  137. $projectsLocotechEntity->urgent = 0;
  138. $projectsLocotechEntity->created = date('Y-m-d H:i:S');
  139. $projectsLocotechEntity->uuid = $this->projectcomment;
  140. if ($projectsLocotechEntity->save()) {
  141. $projectsLocotechOne = ProjectsLocotech::findOne(['tasks' => $this->projectGuid]);
  142. $this->projId = $projectsLocotechOne->id;
  143. if ( null == $this->projId ){
  144. return false;
  145. }
  146. return true;
  147. }
  148. return false;
  149. }
  150. private function CleanProjectTasks()
  151. {
  152. $projectsLocotechEntity = ProjectsLocotech::findOne($this->projId);
  153. $projectsLocotechEntity->tasks = '';
  154. if ( !$projectsLocotechEntity->save()) {
  155. return false;
  156. }
  157. }
  158. private function GetListTasks()
  159. {
  160. $projecttypesTasktypesEntity = ProjecttypesTasktypes::find()
  161. ->select(['tasktype_id'])
  162. ->where(['projecttype_id' => $this->projecttype])
  163. ->orderBy(['priority' => SORT_ASC])
  164. ->all();
  165. return $projecttypesTasktypesEntity;
  166. }
  167. private function addTaskInProject($listTasks)
  168. {
  169. if (is_array($listTasks)) {
  170. foreach ( $listTasks as $task) {
  171. //var_dump($this->initiator, $this->projId, $task->tasktype_id); echo "\n";
  172. $addTaskIntoProjectModel = new AddTaskIntoProjectModel();
  173. $addTaskIntoProjectModel->execute($this->initiator, $this->projId, $task->tasktype_id);
  174. }
  175. }
  176. return true;
  177. }
  178. }