MappingHandlerModel.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. use yii\base\Model;
  5. use app\models\entity\Tasks;
  6. use app\models\logger;
  7. class MappingHandlerModel extends Model
  8. {
  9. public $arResult = [];
  10. private $logger;
  11. public function __construct($logger) {
  12. $this->logger = $logger;
  13. }
  14. public function setLog($text)
  15. {
  16. $this->logger->log($text,$this->className());
  17. }
  18. public function curlRequest2($params){
  19. $urlWithParam = $params['url'].'?user_id='. $params['param'] ;
  20. $curl = curl_init();
  21. curl_setopt($curl, CURLOPT_URL, $urlWithParam);
  22. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  23. curl_setopt($curl, CURLOPT_USERPWD, $params['login'] . ":" . $params['pass']);
  24. curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Accept: application/json"));
  25. curl_setopt($curl, CURLOPT_TIMEOUT, 20);
  26. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  27. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  28. curl_setopt($curl, CURLOPT_POST, 1);
  29. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  30. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  31. $result = curl_exec($curl);
  32. return $result;
  33. }
  34. public function Getisabsentworks($listWorks = null, $GWparams ){
  35. if ( null == $listWorks) {
  36. return false;
  37. }
  38. $arrayIdWorks = [];
  39. foreach( $listWorks->data as $work ) {
  40. $arrayIdWorks[] = $work->id_work;
  41. }
  42. //$arrayIdWorks[] = 'c7cfdeb5-9293-4314-9958-15d15a297532';
  43. //$asusgJob2LaunchEntity = AsusgJob2Launch::find()->where(['not in','asusg_job_uuid_',$arrayIdWorks])->all();
  44. $asusgJob2LaunchEntity = (new \yii\db\Query())
  45. ->select(['t.assignees_arr','ai.id','t.status','t.id','jl.asusg_job_uuid'])
  46. //->select(['t.assignees_arr'])
  47. ->from('`asusg_job2launch` as `jl`')
  48. ->leftJoin('`tasks` as `t`', 'jl.id = t.asusg_job_mapped_id')
  49. ->rightJoin('`accounts_internal` as `ai`', 't.assignees_arr = cast(ai.id as char)')
  50. //->where(['jl.asusg_job_uuid' => $job_uuid])
  51. ->andWhere(['in','asusg_job_uuid',$arrayIdWorks])
  52. ->andWhere(['ai.uuid' => $GWparams['param']])
  53. ->andWhere(['<>','t.parent_id', 0])
  54. ->groupBy(['jl.asusg_job_uuid'])
  55. ->all();
  56. $resultArrWork = [];
  57. foreach( $asusgJob2LaunchEntity as $work ) {
  58. $resultArrWork[] = $work["asusg_job_uuid"];
  59. }
  60. array_diff ($arrayIdWorks, $resultArrWork);
  61. foreach( $listWorks->data as $works ) {
  62. if ( in_array($works->id_work, array_diff ($arrayIdWorks, $resultArrWork))) {
  63. $result[] = $works;
  64. }
  65. //$resultArrWork[] = $work["asusg_job_uuid"];
  66. }
  67. return $result;
  68. /*
  69. echo "<pre>";
  70. var_dump($resultArrWork);
  71. echo "</pre>";
  72. */
  73. }
  74. public function getOnlineMobileUser(){
  75. $result = [];
  76. $accountsEntity = \app\models\entity\Accounts::find()->all();
  77. foreach ($accountsEntity as $account) {
  78. $last_seen_mobile = ( null != $account->last_seen_mobile )? new \DateTime($account->last_seen_mobile): $account->last_seen_mobile ;
  79. $online_mobile = $this->OnlineAccountCheck($this->TimeString($last_seen_mobile));
  80. if ( !$online_mobile ){
  81. continue;
  82. }
  83. $result[] = ['id' => $account->id, 'uuid' => $account->uuid];
  84. }
  85. return $result;
  86. }
  87. //public function startProject($params,$userInfo,$projecttypeId){
  88. public function startProject(){
  89. $arrayResultTemp = [];
  90. $projectsModel = new ProjectsModel();
  91. foreach($this->arResult['listW'] as &$item){
  92. $arrayResult = [];
  93. $arrayResult['loco_type'] = $item['info']['locomotive_series'];
  94. $arrayResult['loco_number'] = $item['info']["section_subnumber"];
  95. $arrayResult['action'] = $projecttypeId->id;
  96. $arrayResult['company'] = $this->arResult['userInfo']->company;//'//$userInfo->company;
  97. //$arrayResult['section_id'] = $item['info']["section_number"];
  98. $arrayResult['depo'] = '';
  99. $arrayResult['depo_service'] = '';
  100. $arrayResult['kind'] = '';
  101. $item['project'] = 'sdfsdfsd';//$projectsModel->ProjectsModel($arrayResult,$item['info']["section_number"]);
  102. $item['userInfo'] = $this->arResult['userInfo'];//$userInfo;
  103. $item['projecttypeId'] = $this->arResult['projecttypeId']->id;//$projecttypeId->id;
  104. }
  105. //return $params;
  106. }
  107. public function getUserInfo($user)
  108. {
  109. if (!$user){
  110. return '';
  111. }
  112. $accountsEntity = Accounts::find()->where(['uuid' => $user['param'] ])->one();
  113. return $accountsEntity;
  114. }
  115. public function getTypeProject($user)
  116. {
  117. $projettypesEntity = Projecttypes::find()->select('id')->where(['company' => $user->company, 'name' => 'Проект из мэппинга'])->one();
  118. return $projettypesEntity;
  119. }
  120. public function OnlineAccountCheck($last_seen)
  121. {
  122. $last_seen = date($last_seen);
  123. $time = (time()-strtotime($last_seen));
  124. if ($time <= Yii::$app->params['onlineMobileTimeoutSeconds'])
  125. return true;
  126. return false;
  127. }
  128. public function TimeString($timeobj)
  129. {
  130. if ($timeobj != null)
  131. return $timeobj->format('d.m.Y H:i');
  132. //return date('d.m.Y H:i', $timeobj);
  133. }
  134. public function GetworksOnLoco($listWorks)
  135. {
  136. $result = [];
  137. if (empty($listWorks)){
  138. return $result;
  139. }
  140. $arrayResult = [];
  141. foreach ($listWorks as $work){
  142. $arrayResult[] = $work->section_id;
  143. }
  144. $sectionsEntity = Sections::find()->where(['in','uuid',$arrayResult])->asArray()->all();
  145. $arrayResult = [];
  146. $arrayResultTemp = [];
  147. $arrayIdSerieTemp = [];
  148. foreach($sectionsEntity as $sec){
  149. $arrayResult[] = $sec['uuid'];
  150. $arrayResultTemp[$sec['uuid']] = $sec;
  151. $LocomotiveSeriesEntity = LocomotiveSeries::findOne(['name' => $sec['locomotive_series']]);
  152. $arrayIdSerieTemp[$sec['uuid']] = $LocomotiveSeriesEntity->id;
  153. }
  154. $tempArray = [];
  155. foreach($listWorks as $w) {
  156. if ( in_array($w->section_id,$arrayResult)) {
  157. $tempArray[] = $w;
  158. $result[$w->section_id] = [ 'info_obj' => $tempArray ,'info'=> $arrayResultTemp[$w->section_id],'id_serie' => $arrayIdSerieTemp[$w->section_id]];
  159. }
  160. }
  161. return $result;
  162. /*
  163. echo "<pre>";
  164. var_dump($result);
  165. //var_dump($sectionsEntity);
  166. echo "</pre>";
  167. var_dump(count($arrayResult),count($sectionsEntity));
  168. */
  169. }
  170. public function checkMapping($arrProj)
  171. {
  172. $tempResult = [];
  173. $result = [];
  174. foreach ( $arrProj as &$proj) {
  175. foreach ( $proj['info_obj'] as $work) {
  176. $tempResult[] = $work->work_name;
  177. }
  178. $res = AsusgJobPlan::find()->where(['in', 'name',$tempResult])->all();
  179. $proj['jobplan'] = $res;
  180. }
  181. return $arrProj;
  182. }
  183. public function addJobproject($minRes)
  184. {
  185. $projectTypes = [];
  186. foreach ( $minRes as &$proj) {
  187. foreach($proj['info_obj'] as $obj){
  188. $repairtypesEntity = Repairtypes::find()->select('id')->where(['name'=> $obj->repair_name])->one();
  189. $projectTypes[] = $repairtypesEntity->id;
  190. }
  191. $projectTypes = array_unique($projectTypes);
  192. foreach ( $projectTypes as $type) {
  193. $field = Yii::$app->params['sections_letter'][$proj['info']["letter"]];
  194. $asusgProjectEntity = new AsusgProject();
  195. $asusgProjectEntity->loco_serie_id = $proj['id_serie']; //locomotive_series
  196. $asusgProjectEntity->repair_type = $type; // repairtypes name из асусг
  197. $asusgProjectEntity->loco_number = $proj['info']["section_number"];
  198. $asusgProjectEntity->$field = $proj['info']["section_subnumber"];
  199. $asusgProjectTypeEntity = AsusgProjectType::findOne(['loco_serie_id' => $proj['id_serie'], 'repair_type' => $type]);
  200. $asusgProjectEntity->asusg_projecttype_id = $asusgProjectTypeEntity->id;
  201. $asusgProjectEntity->save();
  202. $proj['asusgProject_id'] = $asusgProjectEntity->id;
  203. }
  204. }
  205. return $minRes;
  206. }
  207. public function addJob2launch($minRes)
  208. {
  209. foreach ( $minRes as $proj) {
  210. $resultArr = [];
  211. $order = 1;
  212. foreach ( $proj['jobplan'] as $work) {
  213. $sectionTemp = '';
  214. $id_workTemp = '';
  215. foreach($proj['info_obj'] as $obj){
  216. if( $obj->work_name == $work->name){
  217. $sectionTemp = $obj->section_id;
  218. $id_workTemp = $obj->id_work;
  219. break;
  220. }
  221. }
  222. $asusgJob2LaunchEntity = new AsusgJob2Launch();
  223. $asusgJob2LaunchEntity->asusg_jobplan_id = $work->id;
  224. $asusgJob2LaunchEntity->asusg_employee_id1 = $proj['userInfo']->id;
  225. $asusgJob2LaunchEntity->asusg_job_uuid = $id_workTemp;
  226. $asusgJob2LaunchEntity->section_uuid = $sectionTemp;
  227. $asusgJob2LaunchEntity->asusg_project_id = $proj['asusgProject_id'];
  228. $asusgJob2LaunchEntity->job_order = $order;
  229. $asusgJob2LaunchEntity->save();
  230. echo "<pre>";
  231. var_dump($asusgJob2LaunchEntity->errors);
  232. //var_dump($sectionsEntity);
  233. echo "</pre>";
  234. $order++;
  235. }
  236. }
  237. }
  238. // Добавление новой ЗАДАЧИ
  239. public function addNewMainTask()
  240. {
  241. $work = Yii::$app->params['api']['mesrmntParam']['work'];
  242. /*echo "<pre> Main Task";
  243. var_dump(Yii::$app->params['api']['mesrmntParam']);
  244. echo "</pre>";
  245. return true;*/
  246. $tasksEntity = new Tasks();
  247. $tasksEntity->parent_id = 0;
  248. $tasksEntity->status = 2;
  249. $tasksEntity->type = Yii::$app->params['api']['mesrmntParam']['type'];//$tasktypesEntity->id;
  250. $tasksEntity->assignees_arr = Yii::$app->params['api']['mesrmntParam']['assignees_arr'];//Yii::$app->params['assignees_arr'];//$user->id;
  251. $tasksEntity->created = date("Y-m-d H:i;s");
  252. $tasksEntity->asusg_job_mapped_id = Yii::$app->params['api']['mesrmntParam']['asusg_job_mapped_id'];//$asusgJob2LaunchEntity->id;
  253. $tasksEntity->mapping_comment = $work["outfit"] . ': ' . $work['work_name'];
  254. $tasksEntity->text = $work["outfit"] . ': ' . $work['work_name'];
  255. if($tasksEntity->save()) {
  256. Yii::$app->params['api']['mesrmntParam']['parent_id'] = $tasksEntity->id;
  257. //$this->testIdpl3 = $tasksEntity->id;
  258. //$this->setLog('в Tasks была добавлена ЗАДАЧА => "'. $work['work_name'] . '" id => '.$tasksEntity->id);
  259. } else {
  260. //$this->setLog('в Tasks была НЕ добавлена ЗАДАЧА => "'. $work['work_name'] . '" id => '.$tasksEntity->id);
  261. }
  262. return $tasksEntity;
  263. }
  264. // Добавление новой КОМАНДЫ
  265. // param
  266. // $chp => boolean ( добавлять чекпоинты или нет )
  267. public function addNewTask($chp = false)
  268. {
  269. // $tasksEntity->id; родительская задача
  270. // $countPriority приоритет
  271. // $user->id сотрудник
  272. // $job->jobplantie->id launch
  273. Yii::$app->params['countPriority']++;
  274. $work = Yii::$app->params['api']['mesrmntParam']['work'];
  275. /*echo "<pre> Task";
  276. var_dump(Yii::$app->params['api']['mesrmntParam']);
  277. echo "</pre>";
  278. return true;*/
  279. $result = [];
  280. $arrJ = [];
  281. $tasksEntity = new Tasks();
  282. $tasksEntity->input_id = Yii::$app->params['api']['mesrmntParam']['input_id'];//$tasksEntity->id;
  283. $tasksEntity->parent_id = Yii::$app->params['api']['mesrmntParam']['parent_id'];//$tasksEntity->id;
  284. $tasksEntity->status = 2;
  285. $tasksEntity->priority = Yii::$app->params['countPriority'];//$countPriority;
  286. $tasksEntity->created = date("Y-m-d H:i:s");
  287. $tasksEntity->type = Yii::$app->params['api']['mesrmntParam']['type'];//$job->smopp_cmd_id;//Yii::$app->params['supercycletype'];
  288. $tasksEntity->assignees_arr = Yii::$app->params['api']['mesrmntParam']['assignees_arr'];//$user->id;
  289. $tasksEntity->asusg_job_mapped_id = Yii::$app->params['api']['mesrmntParam']['asusg_job_mapped_id'];//$job->jobplantie->id;
  290. $tasksEntity->name = Yii::$app->params['api']['mesrmntParam']['measurement_name'];
  291. $tasksEntity->text = $work["eq_serie"] . " ".$work["section_lit"] . ", ". $work["outfit"] . ": " . $work['work_name'];
  292. $tasksEntity->mapping_comment = $work["outfit"] . ': ' . $work['work_name'];
  293. if ( $tasksEntity->save()) {
  294. $arrJ[] = $tasksEntity->id;
  295. if( $chp ) {
  296. //$query = "call asusg_add_checkpoints_to_task(" . Yii::$app->params['api']['mesrmntParam']['assignees_arr'] . ",'" . $tasksEntity->id . "')";
  297. //$resProc = Yii::$app->db->createCommand($query)->execute();
  298. //var_dump($resProc, 'запись checkpoints',$query); echo "\n";
  299. }
  300. //$this->setLog('в Tasks была добавлена КОМАНДА => "'. $work['work_name'] . '" id => '.$tasksEntity->id);
  301. } else {
  302. //$this->setLog('в Tasks была НЕ добавлена КОМАНДА => "'. $work['work_name'] . '" id => '.$tasksEntity->id);
  303. }
  304. Yii::$app->params['countPriority']++;
  305. $result['arrJ'] = $arrJ;
  306. $result['tasksEntity'] = $tasksEntity;
  307. return $result;
  308. }
  309. }