MappingHandler2Controller.php 13 KB

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