123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- <?php
- namespace app\controllers;
- //use yii\base\Model;
- //
- //use app\models\entity\ProjectsLocotech;
- //use app\models\entity\Tasks;
- //use app\models\entity\Tasktypes;
- //
- //use app\models\entity\Jobtypes;
- //use app\models\entity\AccountsJobtypes;
- use Yii;
- use yii\web\Controller;
- use app\models\ProjectsModel;
- use app\models\entity\Accounts;
- use app\models\entity\Sections;
- use app\models\entity\Repairtypes;
- use app\models\entity\Projecttypes;
- use app\models\entity\AsusgProject;
- use app\models\MappingHandlerModel;
- use app\models\entity\AsusgJobPlan;
- use app\models\entity\AsusgJob2Launch;
- use app\models\entity\AsusgProjectType;
- use app\models\entity\LocomotiveSeries;
- class Mappinghandler2Controller extends Controller
- {
- // запускается периодически через крон
-
- public function actionGetworks()
- {
- $model = new MappingHandlerModel();
-
- $GWparams = ['url' => 'http://devplatform.digitaldepot.ru:8085/Thingworx/Things/SMoPPIntegrationLibrary/Services/getUserWorks',
- 'param' => '0f9ed850-e473-11e2-aaad-ac162db0b20c',
- 'login' => 'ПройдаковИА',
- 'pass' => 'Ip15351112'
- ];
-
-
- // Запрос к асусг список работ
-
- $listWorks = json_decode($this->curlRequest2($GWparams));
-
- // Список отсутствующих работ
-
- $listIsabsentWorks = $this->Getisabsentworks($listWorks,$GWparams);
-
- // Группировка работ по локомотивам/секциям
-
- //$listW = $this->GetworksOnLoco($listIsabsentWorks);
- $model->arResult['listW'] = $this->GetworksOnLoco($listIsabsentWorks);
-
- //$onlineUser = $this->getOnlineMobileUser();
-
- // временный костыль
- //$userInfo = $this->getUserInfo($GWparams);
- $model->arResult['userInfo'] = $this->getUserInfo($GWparams);
- // временный костыль
-
-
- // возвращает тип проекта
- //$projecttypeId = $this->getTypeProject($userInfo);
- $model->arResult['projecttypeId'] = $this->getTypeProject($userInfo);
-
- //->addProject()
-
- // Запускаем проект
-
- echo "<pre>";
- var_dump($model->ara);
- echo "</pre>";
-
- //$startedProj = $this->startProject($listW,$userInfo,$projecttypeId);
- $model->startProject();
-
-
- echo "<pre>";
- var_dump($model->ara);
- echo "</pre>";
-
- /*
- // проверить есть ли для таких работ инструкции
-
- //$minRes = $this->checkMapping($startedProj);
- $this->checkMapping($model);
-
-
- // если что-то есть то запланировать asusg_job2launch
- //$minRes = $this->addJobproject($minRes);
- $this->addJobproject($model);
-
- //$this->addJob2launch($minRes);
- $this->addJob2launch($model);
- */
- echo "<pre>";
- //var_dump($listIsabsentWorks);
- echo "</pre>";
-
- //return $listWorks;
- }
-
- public function curlRequest2($params){
-
- $urlWithParam = $params['url'].'?user_id='. $params['param'] ;
-
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $urlWithParam);
- curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
- curl_setopt($curl, CURLOPT_USERPWD, $params['login'] . ":" . $params['pass']);
- curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Accept: application/json"));
- curl_setopt($curl, CURLOPT_TIMEOUT, 20);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- $result = curl_exec($curl);
- return $result;
- }
-
- public function Getisabsentworks($listWorks = null, $GWparams ){
-
- if ( null == $listWorks) {
- return false;
- }
- $arrayIdWorks = [];
- foreach( $listWorks->data as $work ) {
- $arrayIdWorks[] = $work->id_work;
- }
- //$arrayIdWorks[] = 'c7cfdeb5-9293-4314-9958-15d15a297532';
- //$asusgJob2LaunchEntity = AsusgJob2Launch::find()->where(['not in','asusg_job_uuid_',$arrayIdWorks])->all();
-
- $asusgJob2LaunchEntity = (new \yii\db\Query())
- ->select(['t.assignees_arr','ai.id','t.status','t.id','jl.asusg_job_uuid'])
- //->select(['t.assignees_arr'])
- ->from('`asusg_job2launch` as `jl`')
- ->leftJoin('`tasks` as `t`', 'jl.id = t.asusg_job_mapped_id')
- ->rightJoin('`accounts_internal` as `ai`', 't.assignees_arr = cast(ai.id as char)')
- //->where(['jl.asusg_job_uuid' => $job_uuid])
- ->andWhere(['in','asusg_job_uuid',$arrayIdWorks])
- ->andWhere(['ai.uuid' => $GWparams['param']])
- ->andWhere(['<>','t.parent_id', 0])
- ->groupBy(['jl.asusg_job_uuid'])
- ->all();
-
- $resultArrWork = [];
- foreach( $asusgJob2LaunchEntity as $work ) {
- $resultArrWork[] = $work["asusg_job_uuid"];
- }
- array_diff ($arrayIdWorks, $resultArrWork);
-
- foreach( $listWorks->data as $works ) {
- if ( in_array($works->id_work, array_diff ($arrayIdWorks, $resultArrWork))) {
- $result[] = $works;
- }
- //$resultArrWork[] = $work["asusg_job_uuid"];
-
- }
- return $result;
- /*
- echo "<pre>";
- var_dump($resultArrWork);
- echo "</pre>";
- */
-
- }
- public function getOnlineMobileUser(){
-
- $result = [];
- $accountsEntity = \app\models\entity\Accounts::find()->all();
- foreach ($accountsEntity as $account) {
-
- $last_seen_mobile = ( null != $account->last_seen_mobile )? new \DateTime($account->last_seen_mobile): $account->last_seen_mobile ;
- $online_mobile = $this->OnlineAccountCheck($this->TimeString($last_seen_mobile));
-
- if ( !$online_mobile ){
- continue;
- }
- $result[] = ['id' => $account->id, 'uuid' => $account->uuid];
- }
-
- return $result;
- }
-
- public function startProject($params,$userInfo,$projecttypeId){
-
- $arrayResultTemp = [];
- $projectsModel = new ProjectsModel();
- foreach($params as &$item){
-
- $arrayResult = [];
- $arrayResult['loco_type'] = $item['info']['locomotive_series'];
- $arrayResult['loco_number'] = $item['info']["section_subnumber"];
- $arrayResult['action'] = $projecttypeId->id;
- $arrayResult['company'] = $userInfo->company;
- //$arrayResult['section_id'] = $item['info']["section_number"];
- $arrayResult['depo'] = '';
- $arrayResult['depo_service'] = '';
- $arrayResult['kind'] = '';
- $item['project'] = 'sdfsdfsd';//$projectsModel->ProjectsModel($arrayResult,$item['info']["section_number"]);
- $item['userInfo'] = $userInfo;
- $item['projecttypeId'] = $projecttypeId->id;
- }
- return $params;
-
- }
-
- public function getUserInfo($user)
- {
- if (!$user){
- return '';
- }
- $accountsEntity = Accounts::find()->where(['uuid' => $user['param'] ])->one();
- return $accountsEntity;
- }
-
- public function getTypeProject($user)
- {
- $projettypesEntity = Projecttypes::find()->select('id')->where(['company' => $user->company, 'name' => 'Проект из мэппинга'])->one();
- return $projettypesEntity;
-
- }
-
- public function OnlineAccountCheck($last_seen)
- {
- $last_seen = date($last_seen);
- $time = (time()-strtotime($last_seen));
- if ($time <= Yii::$app->params['onlineMobileTimeoutSeconds'])
- return true;
- return false;
- }
-
- public function TimeString($timeobj)
- {
- if ($timeobj != null)
- return $timeobj->format('d.m.Y H:i');
- //return date('d.m.Y H:i', $timeobj);
- }
-
-
- public function GetworksOnLoco($listWorks)
- {
- $result = [];
- if (empty($listWorks)){
- return $result;
- }
- $arrayResult = [];
- foreach ($listWorks as $work){
- $arrayResult[] = $work->section_id;
- }
- $sectionsEntity = Sections::find()->where(['in','uuid',$arrayResult])->asArray()->all();
- $arrayResult = [];
- $arrayResultTemp = [];
- $arrayIdSerieTemp = [];
- foreach($sectionsEntity as $sec){
- $arrayResult[] = $sec['uuid'];
- $arrayResultTemp[$sec['uuid']] = $sec;
-
- $LocomotiveSeriesEntity = LocomotiveSeries::findOne(['name' => $sec['locomotive_series']]);
- $arrayIdSerieTemp[$sec['uuid']] = $LocomotiveSeriesEntity->id;
- }
- $tempArray = [];
- foreach($listWorks as $w) {
- if ( in_array($w->section_id,$arrayResult)) {
- $tempArray[] = $w;
- $result[$w->section_id] = [ 'info_obj' => $tempArray ,'info'=> $arrayResultTemp[$w->section_id],'id_serie' => $arrayIdSerieTemp[$w->section_id]];
- }
- }
- return $result;
- /*
- echo "<pre>";
- var_dump($result);
- //var_dump($sectionsEntity);
- echo "</pre>";
- var_dump(count($arrayResult),count($sectionsEntity));
- */
-
- }
-
- public function checkMapping($arrProj)
- {
- $tempResult = [];
- $result = [];
-
- foreach ( $arrProj as &$proj) {
- foreach ( $proj['info_obj'] as $work) {
- $tempResult[] = $work->work_name;
- }
- $res = AsusgJobPlan::find()->where(['in', 'name',$tempResult])->all();
- $proj['jobplan'] = $res;
- }
- return $arrProj;
- }
-
- public function addJobproject($minRes)
- {
- $projectTypes = [];
- foreach ( $minRes as &$proj) {
-
- foreach($proj['info_obj'] as $obj){
- $repairtypesEntity = Repairtypes::find()->select('id')->where(['name'=> $obj->repair_name])->one();
- $projectTypes[] = $repairtypesEntity->id;
- }
- $projectTypes = array_unique($projectTypes);
-
- foreach ( $projectTypes as $type) {
-
- $field = Yii::$app->params['sections_letter'][$proj['info']["letter"]];
- $asusgProjectEntity = new AsusgProject();
- $asusgProjectEntity->loco_serie_id = $proj['id_serie']; //locomotive_series
- $asusgProjectEntity->repair_type = $type; // repairtypes name из асусг
- $asusgProjectEntity->loco_number = $proj['info']["section_number"];
- $asusgProjectEntity->$field = $proj['info']["section_subnumber"];
- $asusgProjectTypeEntity = AsusgProjectType::findOne(['loco_serie_id' => $proj['id_serie'], 'repair_type' => $type]);
- $asusgProjectEntity->asusg_projecttype_id = $asusgProjectTypeEntity->id;
- $asusgProjectEntity->save();
- $proj['asusgProject_id'] = $asusgProjectEntity->id;
- }
- }
- return $minRes;
- }
-
- public function addJob2launch($minRes)
- {
-
- foreach ( $minRes as $proj) {
- $resultArr = [];
- $order = 1;
- foreach ( $proj['jobplan'] as $work) {
- $sectionTemp = '';
- $id_workTemp = '';
- foreach($proj['info_obj'] as $obj){
- if( $obj->work_name == $work->name){
- $sectionTemp = $obj->section_id;
- $id_workTemp = $obj->id_work;
- break;
- }
- }
-
- $asusgJob2LaunchEntity = new AsusgJob2Launch();
- $asusgJob2LaunchEntity->asusg_jobplan_id = $work->id;
- $asusgJob2LaunchEntity->asusg_employee_id1 = $proj['userInfo']->id;
- $asusgJob2LaunchEntity->asusg_job_uuid = $id_workTemp;
- $asusgJob2LaunchEntity->section_uuid = $sectionTemp;
- $asusgJob2LaunchEntity->asusg_project_id = $proj['asusgProject_id'];
- $asusgJob2LaunchEntity->job_order = $order;
- $asusgJob2LaunchEntity->save();
- echo "<pre>";
- var_dump($asusgJob2LaunchEntity->errors);
- //var_dump($sectionsEntity);
- echo "</pre>";
- $order++;
- }
-
- }
-
-
- }
-
-
-
- }
|