ProcedureModel.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. use yii\base\Model;
  5. use app\models\entity\AsusgProject;
  6. use app\models\entity\Accounts;
  7. use app\models\entity\Projecttypes;
  8. class ProcedureModel extends Model
  9. {
  10. private $projectId;
  11. private $project_id;
  12. private $locoserie;
  13. private $loconumber;
  14. private $projecttypeId;
  15. private $projectcomment;
  16. private $tmpText;
  17. private $taskId;
  18. private $commandId;
  19. private $counter;
  20. private $jobId;
  21. private $projecttype;
  22. private $companyId;
  23. private $s1;
  24. private $s2;
  25. private $s3;
  26. private $tmps;
  27. private $s1v;
  28. private $s2v;
  29. private $s3v;
  30. private $tmpsv;
  31. private $done;
  32. private $initiator;
  33. private $cur_job;
  34. private $cur_command;
  35. public function asusg_build_and_start_project_auto($initiator, $project_id)
  36. {
  37. $initiator = '27';
  38. $this->initiator = $initiator;
  39. $this->project_id = $project_id;
  40. // 1 Определяем ид компании ( companyId )
  41. // 1.1 если нет то крах
  42. $this->getCompanyId();
  43. // 2 Определяем тип роекта ( projecttype )
  44. $this->GetProjecttype();
  45. // 3. Определяем ид проекта из asusg_project ( projectId )
  46. $this->GetProjectId();
  47. // 4. Определяем ид типа проекта ( projecttypeId )
  48. // 4.1 если нет джойним asusg_project и asusg_projecttype ( projecttypeId )
  49. $this->GetProjecttypeId();
  50. // 5 определяем переменные locoserie, loconumber, tmpText
  51. $this->GetLocoProperties();
  52. // 6 получаем строку коммента
  53. $this->projectcomment = 'Проект АСУ СГ №' . $this->project_id . ': ' . $this->tmpText . ', ' . $this->locoserie);
  54. // 7 собираем все переменные initiator, projecttype, projectId, locoserie, loconumber, projectcomment и фигачим проект магия !!!! ВЫЗОВ ДРУГО ПРОЦЕДУРЫ
  55. $this->CallProcedureStartProject();
  56. echo "<pre>";
  57. var_dump($this->CallProcedureStartProject());
  58. echo "</pre>";
  59. exit;
  60. $result = $this->AsusgCurJob();
  61. //$this->AsusgCurCommand();
  62. //var_dump($this->cur_command);
  63. //$this->startProjectForLoco();
  64. $asusgProjectEntity = AsusgProject::find()->where(['id' => $this->project_id , 'status' => 1])->one();
  65. if (null !== $asusgProjectEntity) {
  66. $this->projectId = $asusgProjectEntity->id;//select id from asusg_project where id = project_id and status = 1
  67. $this->setUpdateProjects();
  68. $this->organizeCommands(); // протестил работает!
  69. }
  70. }
  71. // получение cur_job !!!!!!!!
  72. public function AsusgCurJob()
  73. {
  74. //declare cur_job cursor for
  75. $result = [];
  76. $query = "select aj2l.id, coalesce(aj2l.asusg_employee_id1, 0), coalesce(aj2l.asusg_employee_id2, 0), coalesce(aj2l.asusg_employee_id3, 0)
  77. from asusg_job2launch aj2l
  78. where aj2l.asusg_project_id =" . $this->project_id;
  79. $this->cur_job = Yii::$app->db->createCommand($query)->queryAll();
  80. if (count($result)) {
  81. //$this->logger->setLog();
  82. }
  83. return $result;
  84. }
  85. public function AsusgCurTask()
  86. {
  87. $query = 'select id from tasks where parent_id = 0 and input_id = '. $this->project_id;
  88. $result = Yii::$app->db->createCommand($query)->queryAll();
  89. if (count($result)) {
  90. //$this->logger->setLog();
  91. }
  92. return $result;
  93. }
  94. public function AsusgCurCommand($taskId)
  95. {
  96. $query = "select t.id from tasks t
  97. where t.input_id = " . $this->project_id . "
  98. and t.parent_id = " . $taskId . "
  99. order by t.project_id, t.priority";
  100. $result = Yii::$app->db->createCommand($query)->queryAll();
  101. if (count($result)) {
  102. //$this->logger->setLog();
  103. }
  104. $this->cur_command = $result;
  105. return $result;
  106. }
  107. // 1. ========================================================================================================================================
  108. private function getCompanyId()
  109. {
  110. $result = [];
  111. $accountsEntity = Accounts::findOne($this->initiator);
  112. $this->companyId = $accountsEntity->company;
  113. if (null == $this->companyId) return false;
  114. if (count($result)) {
  115. // $this->logger->setLog();
  116. }
  117. return $this->companyId;
  118. }
  119. // 2. ========================================================================================================================================
  120. private function GetProjecttype()
  121. {
  122. $this->projecttype = Projecttypes::find()
  123. ->select(['id'])
  124. ->where(['name' => 'Проект из мэппинга', 'company' => $this->companyId])
  125. ->all();
  126. }
  127. // 3. ========================================================================================================================================
  128. private function GetProjectId()
  129. {
  130. $asusgProjectEntity = AsusgProject::find()->where(['id' => $this->project_id , 'status' => 1])->one();
  131. if (null !== $asusgProjectEntity) {
  132. $this->projectId = $asusgProjectEntity->id;
  133. }
  134. }
  135. // 4. ========================================================================================================================================
  136. private function GetProjecttypeId()
  137. {
  138. $asusgProjectEntity = AsusgProject::find()->select(['asusg_projecttype_id'])->where(['id' => $this->projectId])->one();
  139. $this->projecttypeId = $asusgProjectEntity->asusg_projecttype_id;
  140. if (null == $this->projecttypeId) {
  141. $asusgProjectJoinEntity = AsusgProject::find()
  142. ->leftJoin('asusg_projecttype', 'asusg_project.loco_serie_id = asusg_projecttype.id and asusg_projecttype.repair_type = asusg_project.repair_type')
  143. ->select(['asusg_projecttype.id'])
  144. ->where(['asusg_project.id' => $this->project_id])
  145. ->one();
  146. $this->projecttypeId = $asusgProjectJoinEntity->id;
  147. }
  148. }
  149. // 5. ========================================================================================================================================
  150. private function GetLocoProperties()
  151. {
  152. $asusgProjectJoinEntity = AsusgProject::find()
  153. ->leftJoin('locomotive_series', 'asusg_project.loco_serie_id = locomotive_series.id')
  154. ->leftJoin('repairtypes', 'repairtypes.id = asusg_project.repair_type')
  155. ->select(['locomotive_series.name as lsname', 'asusg_project.loco_number', 'repairtypes.name as rtname'])
  156. ->where(['asusg_project.id' => $this->project_id])
  157. ->asArray()
  158. ->one();
  159. $this->locoserie = $asusgProjectJoinEntity['lsname'];
  160. $this->loconumber = $asusgProjectJoinEntity['loco_number'];
  161. $this->tmpText = $asusgProjectJoinEntity['rtname'];
  162. }
  163. // 7 . ========================================================================================================================================
  164. private function CallProcedureStartProject()
  165. {
  166. Yii::$app->db->createCommand('CALL coverage_dis_prof(:initiator, :projecttype , @projectId , :loconumber ,:projectcomment);')
  167. ->bindValue(':initiator' , $this->initiator)
  168. ->bindValue(':projecttype', $this->projecttype)
  169. ->bindValue(':loconumber', $this->loconumber)
  170. ->bindValue(':projectcomment', $this->projectcomment)
  171. ->execute();
  172. Yii::$app->db->createCommand("SELECT @projectId;")->queryScalar();
  173. }
  174. public function text_log()
  175. {
  176. $query = "insert into text_log (msg) values (concat(cast(initiator as char), ': Проект ', cast(project_id as char), '/', cast(projectId as char), ' запущен в работу.'))";
  177. Yii::$app->db->createCommand($query)->queryAll();
  178. }
  179. public function wwwer()
  180. {
  181. $this->s1 = 'А';
  182. $this->s2 = 'Б';
  183. $this->s3 = 'В';
  184. if ($this->s2v < $this->s1v) {
  185. $this->tmpsv = $this->s1v;
  186. $this->s1v = $this->s2v;
  187. $this->s2v = $this->tmpsv;
  188. $this->tmps = $this->s1;
  189. $this->s1 = $this->s2;
  190. $this->s2 = $this->tmps;
  191. }
  192. if ($this->s3v < $this->s2v) {
  193. $this->tmpsv = $this->s2v;
  194. $this->s2v = $this->s3v;
  195. $this->s3v = $this->tmpsv;
  196. $this->tmps = $this->s2;
  197. $this->s2 = $this->s3;
  198. $this->s3 = $this->tmps;
  199. }
  200. if ($this->s2v < $this->s1v) {
  201. $this->tmpsv = $this->s1v;
  202. $this->s1v = $this->s2v;
  203. $this->s2v = $this->tmpsv;
  204. $this->tmps = $this->s1;
  205. $this->s1 = $this->s2;
  206. $this->s2 = $this->tmps;
  207. }
  208. if ($this->s1v > 0) {
  209. foreach ($this->cur_job as $jobId) {
  210. // break;
  211. if ($jobId->asusg_employee_id1 != 0 || $jobId->asusg_employee_id2 != 0 || $jobId->asusg_employee_id3 != 0) {
  212. $query = "call asusg_add_job_to_project_for_section_by_employee(initiator, project_id, jobId, s1)";
  213. } else {
  214. $query = "call asusg_add_job_to_project_for_section(initiator, project_id, jobId, s1)";
  215. }
  216. Yii::$app->db->createCommand($query)->queryAll();
  217. }
  218. }
  219. if ($this->s2v > 0) {
  220. foreach($this->cur_job as $jobId) {
  221. // break;
  222. if ($jobId->asusg_employee_id1 != 0 || $jobId->asusg_employee_id2 != 0 || $jobId->asusg_employee_id3 != 0) {
  223. $query = "call asusg_add_job_to_project_for_section_by_employee(" . $this->initiator . ", " . $this->project_id . ", " . $jobId->id . ", " . $jobId->asusg_employee_id2 .")";
  224. } else {
  225. $query = "call asusg_add_job_to_project_for_section(" . $this->initiator . ", " . $this->project_id . ", " . $jobId->id . ", " . $jobId->asusg_employee_id2 .")";
  226. }
  227. Yii::$app->db->createCommand($query)->queryAll();
  228. }
  229. }
  230. if ($this->s3v > 0) {
  231. foreach($this->cur_job as $jobId) {
  232. // break;
  233. if ( $jobId->asusg_employee_id1 != 0 || $jobId->asusg_employee_id2 != 0 || $jobId->asusg_employee_id3 != 0) {
  234. $query = "call asusg_add_job_to_project_for_section_by_employee(" . $this->initiator . ", " . $this->project_id . ", " . $jobId->id . ", " . $jobId->asusg_employee_id3 .")";
  235. } else {
  236. $query = "call asusg_add_job_to_project_for_section(" . $this->initiator . ", " . $this->project_id . ", " . $jobId->id . ", " . $jobId->asusg_employee_id3 .")";
  237. }
  238. Yii::$app->db->createCommand($query)->queryAll();
  239. }
  240. }
  241. }
  242. public function setUpdateProjects()
  243. {
  244. $query = "UPDATE asusg_project SET status = 2, started = NOW() WHERE id = " . $this->project_id;
  245. $query2 = "UPDATE projects_locotech SET tasks = coalesce(right(tasks, length(tasks) - 1), '')
  246. WHERE id = " . $this->projectId;
  247. $query3 = "INSERT INTO text_log (msg) VALUES (concat(cast(initiator as char), ': Проект ', cast(project_id as char), '/', cast(projectId as char), ' подготовлен. Упорядочиваю команды.'))";
  248. var_dump($query,'<br>');
  249. var_dump($query2,'<br>');
  250. var_dump($query3,'<br>');
  251. //Yii::$app->db->createCommand($query)->queryAll();
  252. //Yii::$app->db->createCommand($query2)->queryAll();
  253. //Yii::$app->db->createCommand($query3)->queryAll();
  254. }
  255. public function organizeCommands()
  256. {
  257. #Упорядочить команды в задачах по project_id, установить project_id = 0.
  258. foreach ($this->AsusgCurTask() as $taskId) {
  259. $counter = 0;
  260. foreach ($this->AsusgCurCommand($taskId["id"]) as $commandId) {
  261. $query = "UPDATE tasks
  262. SET priority = " . $counter . ", project_id = 0
  263. WHERE id = " . $commandId['id'];
  264. var_dump($query,'<br>');
  265. //Yii::$app->db->createCommand($query)->queryAll();
  266. $counter++;
  267. }
  268. }
  269. }
  270. }