AsusgBuildProjectFromMappingModel.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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\Companies;
  7. use app\models\entity\AsusgProject;
  8. use app\models\entity\Projecttypes;
  9. use app\models\entity\ProjectsLocotech;
  10. use app\models\entity\JobtypesTasktypes;
  11. use app\models\entity\ProjecttypesTasktypes;
  12. /*
  13. build_project: BEGIN
  14. declare companyId integer default 0;
  15. declare tmpGuid varchar(255);
  16. declare current_task integer;
  17. declare current_jobtype integer;
  18. declare jobtype integer;
  19. declare taskPriority integer;
  20. declare taskName varchar(255);
  21. declare command integer;
  22. declare done integer;
  23. declare asusg_job integer;
  24. declare projectId integer;
  25. declare locoserieId integer;
  26. declare cur_job_mapped cursor for select ajm.asusg_jobplan_id from asusg_job_mapped ajm where ajm.asusg_project_id = projectId order by ajm.job_order;
  27. declare cur_job2cmd cursor for select a.smopp_cmd_id from asusg_job2command a where a.asusg_job_id = asusg_job order by a.cmd_order;
  28. declare continue handler for not found set done = true;
  29. 1 //получяю ид компании
  30. set companyId = (select company from accounts where id = initiator and cmdlevel > 1);
  31. if (companyId is null) then leave build_project; end if;
  32. set locoserieId = (select id from locomotive_series where name like locoserie);
  33. if (locoserieId is null) then leave build_project; end if;
  34. 2 // Создание проекта АСУ СГ.
  35. set tmpGuid = (select UUID());
  36. insert into asusg_project
  37. (text, loco_serie_id, repair_type, loco_number, section_a, section_b, section_c)
  38. values
  39. (tmpGuid, locoserieId, repairtype, loconumber, section1, section2, section3);
  40. set projectId = (select id from asusg_project where text = tmpGuid);
  41. if (projectId is null) then leave build_project; end if;
  42. 3 // генерю проект
  43. set tmpGuid = (select UUID());
  44. insert into projects_locotech
  45. (action, tasks, company, status, loco_type, loco_number, urgent, created, uuid)
  46. values
  47. (projecttype, tmpGuid, companyId, 1, locoserie, loconumber, 0, NOW(), 'Запрос из АСУ СГ');
  48. 4 //переопределяю ид компании
  49. set companyId = (select id from projects_locotech where tasks = tmpGuid);
  50. if (companyId is null) then leave build_project; end if;
  51. 5 // прикрепляю команды к проекту
  52. update projects_locotech
  53. set tasks = ''
  54. where id = companyId;
  55. #Проект создан, можно добавлять задачи.
  56. set current_task = 0;
  57. set current_jobtype = 0;
  58. set taskPriority = 1;
  59. 6 // генерю таски к проекту
  60. open cur_job2cmd;
  61. task_loop: loop
  62. fetch cur_job2cmd into command;
  63. if done then leave task_loop;
  64. end if;
  65. set jobtype = (select jobtype_id from jobtypes_tasktypes where tasktype_id = command);
  66. if (jobtype is null) or (jobtype <> current_jobtype) then
  67. set current_jobtype = coalesce(jobtype, 0);
  68. set taskName = (select name from jobtypes where id = current_jobtype);
  69. set taskName = concat('Должность: ', coalesce(taskName, '(не задано)'));
  70. set tmpGuid = (select UUID());
  71. insert into tasks
  72. (status, parent_id, type, input_id, text, project_id, assignees_arr, priority, assignedby, created)
  73. values (1, 0, tasktype, companyId, tmpGuid, 0, 0, 0, 0, NOW());
  74. set current_task = (select id from tasks where text like tmpGuid);
  75. update projects_locotech
  76. set tasks = concat(tasks, ',', cast(current_task as char))
  77. where id = companyId;
  78. set taskPriority = 1;
  79. update tasks
  80. set text = taskName
  81. where id = current_task;
  82. end if;
  83. #set tmpGuid = (select UUID());
  84. insert into tasks
  85. (status, parent_id, type, input_id, text, project_id, assignees_arr, priority, assignedby, created)
  86. values (1, current_task, command, companyId, taskName, 0, 0, taskPriority, 0, NOW());
  87. #values (1, current_task, command, companyId, tmpGuid, 0, 0, taskPriority, 0, NOW());
  88. set taskPriority = taskPriority + 1;
  89. end loop;
  90. close cur_job2cmd;
  91. END
  92. */
  93. class AsusgBuildProjectFromMappingModel extends Model
  94. {
  95. private $companyId = 0;
  96. private $tmpGuid;
  97. private $current_task;
  98. private $current_jobtype;
  99. private $jobtype;
  100. private $taskPriority;
  101. private $taskNameprivate;
  102. private $command;
  103. private $done;
  104. private $initiator;
  105. private $projecttype;
  106. private $tasktype;
  107. private $locoserie;
  108. private $loconumber;
  109. private $asusg_job;
  110. //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;
  111. //declare continue handler for not found set done = true;
  112. public static function execute($initiator, $projecttype, $tasktype, $locoserie, $loconumber, $repairtype,$section1,$section2,$section3) {
  113. $this->initiator = $initiator,
  114. $this->projecttype = $projecttype,
  115. $this->tasktype = $tasktype,
  116. $this->locoserie = $locoserie,
  117. $this->loconumber = $loconumber,
  118. $this->asusg_job = $asusg_job,
  119. $this->repairtype = $repairtype,
  120. $this->section1 = $section1;
  121. $this->section2 = $section2;
  122. $this->section3 = $section3;
  123. // 1. получяю ид компании
  124. if(!$this->GetCompanyId()) {
  125. return false;
  126. }
  127. // 2. Создание проекта АСУ СГ.
  128. if(!$this->GenerateAsusgProject()) {
  129. return false;
  130. }
  131. // 3. генерю проект
  132. if(!$this->GenerateProject()) {
  133. return false;
  134. }
  135. // 4. переопределяю ид компании
  136. if(!$this->RedefineCompanyId()) {
  137. return false;
  138. }
  139. // 5. прикрепляю команды к проекту
  140. if(!$this->attachCommands()) {
  141. return false;
  142. }
  143. // 6. генерю таски к проекту
  144. if(!$this->attachCommandsToProj()) {
  145. return false;
  146. }
  147. }
  148. private function GetCompanyId(){
  149. $companiesEntity = Companies::find()->where(['id' => $this->initiator])->andWhere(['>','cmdlevel',1])->one();
  150. $this->companyId = $companiesEntity->id;
  151. if ( null == $this->companyId) {
  152. return false;
  153. }
  154. return true;
  155. }
  156. private function GetUuid()
  157. {
  158. $uuid = Yii::$app->db->createCommand('select UUID() as uuid')->queryOne();
  159. $this->tmpGuid = $uuid["uuid"];
  160. if ( $this->tmpGuid ) {
  161. return true;
  162. }
  163. true false;
  164. }
  165. private function GenerateProject(){
  166. $this->GetUuid();
  167. $projectsLocotech = new ProjectsLocotech();
  168. $projectsLocotech->action = $this->projecttype;
  169. $projectsLocotech->tasks = $this->tmpGuid;
  170. $projectsLocotech->company = $this->companyId;
  171. $projectsLocotech->status = 1;
  172. $projectsLocotech->loco_type = $this->locoserie;
  173. $projectsLocotech->loco_number = $this->loconumber;
  174. $projectsLocotech->urgent = 0;
  175. $projectsLocotech->created = date("Y-m-d H:i:s");
  176. $projectsLocotech->uuid = 'Задача из АСУ СГ';
  177. $projectsLocotech->save();
  178. }
  179. private function GenerateAsusgProject(){
  180. $this->GetUuid();
  181. $AsusgProjectEntity = new AsusgProject();
  182. $AsusgProjectEntity->text = $this->tmpGuid;
  183. $AsusgProjectEntity->loco_serie_id = $this->locoserieId;
  184. $AsusgProjectEntity->repair_type = $this->repairtype;
  185. $AsusgProjectEntity->loco_number = $this->loconumber;
  186. $AsusgProjectEntity->section_a = $this->section1;
  187. $AsusgProjectEntity->section_b = $this->section2;
  188. $AsusgProjectEntity->section_c = $this->section3;
  189. $AsusgProjectEntity->save();
  190. $asuproj = AsusgProject::find()->where(['text' => $this->tmpGuid])->one();
  191. if ( null == $asuproj->projectId) {
  192. return false;
  193. }
  194. return true;
  195. }
  196. private function RedefineCompanyId(){
  197. $ProjectsLocotech = ProjectsLocotech::find()->where(['tasks' => $this->tmpGuid])->one();
  198. $rhis->companyId = $ProjectsLocotech->id;
  199. if ( null == $rhis->companyId ) {
  200. return false;
  201. }
  202. return true;
  203. }
  204. private function attachCommands() {
  205. ProjectsLocotech::updateAll(['tasks' = ''],['id' => $this->companyId]);
  206. $this->current_task = 0;
  207. $this->current_jobtype = 0;
  208. $this->taskPriority = 1;
  209. return true;
  210. }
  211. private function attachCommandsToProj() {
  212. $AsusgJob2CommandEntity = AsusgJob2Command::find()->where(['asusg_job_id' = $tgis->asusg_job])->orderBy(['cmd_order'=> SORT_ASC])->all();
  213. foreach( $AsusgJob2CommandEntity as $comand) {
  214. $JobtypesTasktypes = JobtypesTasktypes::find()->where(['tasktype_id' => $comand])->all();
  215. $this->jobtype = $JobtypesTasktypes->jobtype_id;
  216. if ( (null == $this->jobtype) || ($this->jobtype <> $this->current_jobtype) ) {
  217. //...
  218. }
  219. }
  220. }
  221. }