ServiceController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. namespace app\controllers;
  3. use Yii;
  4. use app\models\entity\Accounts;
  5. use yii\web\Request;
  6. use yii\rest\Controller;
  7. use app\models\UserModel;
  8. use app\models\TasksModel;
  9. use app\models\AccountModel;
  10. use app\models\LocomotiveModel;
  11. use app\models\EmployeesModel;
  12. use app\models\ProjectsModel;
  13. use app\models\ServiceModel;
  14. use app\models\entity\Sections;
  15. use app\models\entity\AsusgJob2Launch;
  16. use app\models\entity\Jobtypes;
  17. class ServiceController extends MainController
  18. {
  19. const PROJECT_TYPE_TO_START = 129;
  20. const PROJECT_TYPE_RFID_TO_START = 132;
  21. public $returnArrayWorks = ['result' => false, 'int'=> 0, 'error'=> ''];
  22. public function beforeAction($action)
  23. {
  24. parent::beforeAction($action);
  25. //$date = strtotime('-3 weeks');
  26. //$this->threeWeeksAgo = date('Y-m-d', $date);
  27. $key = Yii::$app->request->headers->get('uuid-key');
  28. $this->checkAuth($key);
  29. $headerContent = Yii::$app->request->headers->get('content-type');
  30. //\Yii::$app->response->format = \yii\web\Response::FORMAT_XML;
  31. //if ( 'application/json' == $headerContent ) {
  32. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  33. //}
  34. return true;
  35. }
  36. /** Принять Запрос (SMoPP от ЕИПП) на проверку RFID метки секции Проверить RFIDМетку */
  37. public function actionCheckrfid()
  38. {
  39. $this->LogRequest();
  40. $params = ['action' => self::PROJECT_TYPE_RFID_TO_START, 'company' => 3, 'kind'=> 'ТР-3','loco_number' => '','depo' => '', 'depo_service'=> 'Братское'];
  41. $locomotiveModel = new LocomotiveModel();
  42. $projectsModel = new ProjectsModel();
  43. $serviceModel = new ServiceModel();
  44. $taskModel = new TasksModel();
  45. $userModel = new UserModel();
  46. $this->status(self::PROJECT_TYPE_RFID_TO_START);
  47. $arrayParams['series'] = Yii::$app->request->post('series');
  48. $arrayParams['number'] = Yii::$app->request->post('number');
  49. $arrayParams['subnumber'] = Yii::$app->request->post('subnumber');
  50. $arrayParams['dateTime'] = Yii::$app->request->post('dateTime');
  51. $arrayParams['rfid'] = Yii::$app->request->post('rfid');
  52. $arrayParams['sectionId'] = Yii::$app->request->post('sectionId');
  53. $params['diagnosticCard'] = Yii::$app->request->post('diagnosticCard');
  54. $params['dateTime'] = Yii::$app->request->post('dateTime');
  55. $params['loco_type'] = Yii::$app->request->post('series');
  56. $res = $serviceModel->addSections($arrayParams); // +
  57. $activeid = $projectsModel->addProject($params,$res->id); // Добавление новой записи в ProjectLocotech +
  58. $locomotiveModel->project_id = $activeid;
  59. $res = $locomotiveModel->getSectionRemarks($arrayParams, true); // +
  60. if ($activeid > 0) {
  61. $res = $projectsModel->StartProjectRequest($params['action'],$activeid);
  62. require_once ("../../functions.php");
  63. require_once ("doctrine/bootstrap.php");
  64. $proj = \Project::Find($activeid, $entityManager);
  65. $workers = $proj->GetWorkers();
  66. }
  67. $resultArray = [
  68. "workers" => $workers,
  69. "inspectionId" => $activeid,
  70. "inspectionIdCheck" => true,
  71. "error_code" => 200,
  72. "error_message" => "Success"
  73. ];
  74. return $resultArray;
  75. }
  76. /** Принять Запрос (SMoPP от ЕИПП) на проведение осмотра на дату и время */
  77. public function actionAddwork()
  78. {
  79. $this->LogRequest();
  80. $params = ['action' => self::PROJECT_TYPE_TO_START, 'company' => 3, 'depo_service'=> 'Братское'];
  81. $locomotiveModel = new LocomotiveModel();
  82. $projectsModel = new ProjectsModel();
  83. $serviceModel = new ServiceModel();
  84. $taskModel = new TasksModel();
  85. $userModel = new UserModel();
  86. $post = Yii::$app->request->post();
  87. if (!$serviceModel->checkPost($post)) {
  88. $resultArray = [
  89. 'error_code' => 200,
  90. 'error_message' => $serviceModel->errors['checkPost']
  91. ];
  92. return $resultArray;
  93. }
  94. $this->status(self::PROJECT_TYPE_TO_START);
  95. $arrayParams['series'] = Yii::$app->request->post('series');
  96. $arrayParams['number'] = Yii::$app->request->post('number');
  97. $arrayParams['subnumber'] = Yii::$app->request->post('subnumber');
  98. $arrayParams['dateTime'] = Yii::$app->request->post('dateTime');
  99. $arrayParams['sectionId'] = Yii::$app->request->post('sectionId');
  100. $params['series'] = Yii::$app->request->post('series');
  101. $params['number'] = Yii::$app->request->post('number');
  102. $params['subnumber'] = Yii::$app->request->post('subnumber');
  103. $params['dateTime'] = Yii::$app->request->post('dateTime');
  104. $params['diagnosticCard'] = Yii::$app->request->post('diagnosticCard');
  105. $params['loco_number'] = (int)Yii::$app->request->post('number');
  106. $params['kind'] = Yii::$app->request->post('kind');
  107. $res = $serviceModel->addSections($arrayParams);
  108. $activeid = $projectsModel->addProject($params, $res->id); // Добавление новой записи в ProjectLocotech +
  109. $locomotiveModel->project_id = $activeid;
  110. $res = $locomotiveModel->getSectionRemarks($arrayParams, true); // +
  111. if ($activeid > 0) {
  112. require_once ("../../functions.php");
  113. require_once ("doctrine/bootstrap.php");
  114. $proj = \Project::Find($activeid, $entityManager);
  115. $workers = $proj->GetWorkers();
  116. }
  117. $resultArray = [
  118. 'Employee' => $workers,
  119. 'ID' => $activeid,
  120. 'Date' => '2019-06-13 12:34:16',
  121. 'error_code' => 200,
  122. 'error_message' => 'Success',
  123. 'executeData' => $projectsModel->message,
  124. ];
  125. return $resultArray;
  126. }
  127. /** Принять Запрос (SMoPP от ЕИПП) на проведение осмотра на дату и время */
  128. public function actionGetworkstatus($user_id, $work_id)
  129. {
  130. $params = ['url' => 'http://devplatform.digitaldepot.ru:8085/Thingworx/Things/SMoPPIntegrationLibrary/Services/getUserWorks',
  131. 'param' => '0f9ed850-e473-11e2-aaad-ac162db0b20c',
  132. 'login' => 'ПройдаковИА',
  133. 'pass' => 'Ip15351112'
  134. ];
  135. $listWorks = json_decode($this->curlRequest2($params));
  136. //$arr = $this->worksStatusHandler($listWorks->data);
  137. //var_dump($listWorks->data);
  138. //$this->worksStatusHandler($user_uuid, $job_uuid);
  139. //->where(['jl.asusg_job_uuid' => '445943a8-36b8-11ea-aec2-2e728ce88125'])
  140. //->andWhere(['ai.uuid' => 'f37deefa-3849-11ea-a137-2e728ce88125'])
  141. //$this->worksStatusHandler($user_uuid, $job_uuid);
  142. return $this->worksStatusHandler($user_id, $work_id);//$this->curlRequest2($params);
  143. }
  144. public function status($active)
  145. {
  146. $date = (new \DateTime())->getTimestamp();
  147. $accounts = [
  148. [
  149. 'worker' => '7b8ebba7-c9a4-11e7-80cd-0050560110fa', 'date' => $date
  150. ], [
  151. 'worker' => 'f4a9cbc2-e472-11e2-aaad-ac162db0b20c', 'date' => $date
  152. ]
  153. ];
  154. $response = [];
  155. foreach ($accounts as $params) {
  156. $employeesModel = new EmployeesModel();
  157. $resp = $employeesModel->getEmployeeStatus($params);
  158. $resp = json_decode($resp);
  159. $response[$params['worker']] = ($resp->Status == "Уход") ? false : true;
  160. }
  161. foreach ($response as $key => $person) {
  162. if (strlen($key) > 5) {
  163. $accountEntity = \app\models\entity\Accounts::findOne(['uuid' => $key]);
  164. $accountEntity->employee_status = $person;
  165. $accountEntity->update();
  166. }
  167. }
  168. }
  169. public function actionAddworke()
  170. {
  171. $this->LogRequest();
  172. require_once ("../../functions.php");
  173. require_once ("doctrine/bootstrap.php");
  174. $res = \ProjectType::Find(self::PROJECT_TYPE_TO_START);
  175. $r = $res->GetAccounts();
  176. foreach ($r as $acc) {
  177. echo "$acc->id<br>";
  178. }
  179. die();
  180. }
  181. /* Запись запроса в таблицу логов api_log */
  182. public function Log($date, $post, $userIP, $userHost, $userAgent, $query, $method)
  183. {
  184. $serverIP = $_SERVER['SERVER_ADDR'];
  185. $sql_query = "INSERT INTO api_log SET"
  186. . " timestamp='$date',"
  187. . " post='$post',"
  188. . " user_ip='$userIP',"
  189. . " user_host='$userHost',"
  190. . " user_agent='$userAgent',"
  191. . " query='$query',"
  192. . " method='$method',"
  193. . " server_ip = '$serverIP'";
  194. $result = Yii::$app->db->createCommand($sql_query)->execute();
  195. return $result;
  196. }
  197. public function LogRequest()
  198. {
  199. return $this->Log(
  200. date('Y-m-d H:i:s'),
  201. Yii::$app->request->rawBody,
  202. Yii::$app->request->userIP,
  203. Yii::$app->request->userHost,
  204. Yii::$app->request->userAgent,
  205. Yii::$app->request->url,
  206. Yii::$app->request->method
  207. );
  208. }
  209. public function curlRequest2($params){
  210. $urlWithParam = $params['url'].'?user_id='. $params['param'] ;
  211. $curl = curl_init();
  212. curl_setopt($curl, CURLOPT_URL, $urlWithParam);
  213. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  214. curl_setopt($curl, CURLOPT_USERPWD, $params['login'] . ":" . $params['pass']);
  215. curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Accept: application/json"));
  216. curl_setopt($curl, CURLOPT_TIMEOUT, 20);
  217. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  218. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  219. curl_setopt($curl, CURLOPT_POST, 1);
  220. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  221. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  222. $result = curl_exec($curl);
  223. return $result;
  224. }
  225. public function worksStatusHandler($user_uuid, $job_uuid){
  226. //public function worksStatusHandler(){
  227. $accountsEntity = Accounts::find()->where(['uuid' => $user_uuid])->one();
  228. //var_dump($accountsEntity); exit;
  229. //if ( 0 == count($accountsEntity) ) {
  230. if ( null == $accountsEntity ) {
  231. $this->returnArrayWorks['error'] = 'Не найден сотрудник';
  232. return $this->returnArrayWorks;
  233. }
  234. $asusgJob2LaunchEntity = AsusgJob2Launch::find()->where(['asusg_job_uuid' => $job_uuid])->one();
  235. //if ( 0 == count($asusgJob2LaunchEntity) ) {
  236. if ( null == $asusgJob2LaunchEntity ) {
  237. $this->returnArrayWorks['error'] = 'Не найдена работа';
  238. return $this->returnArrayWorks;
  239. }
  240. //var_dump($asusgJob2LaunchEntity->tasks->id); exit;
  241. $row_last = (new \yii\db\Query())
  242. ->select(['t.assignees_arr','ai.id','t.status','t.id'])
  243. //->select(['t.assignees_arr'])
  244. ->from('`asusg_job2launch` as `jl`')
  245. ->leftJoin('`tasks` as `t`', 'jl.id = t.asusg_job_mapped_id')
  246. ->rightJoin('`accounts_internal` as `ai`', 't.assignees_arr = cast(ai.id as char)')
  247. ->where(['jl.asusg_job_uuid' => $job_uuid])
  248. ->andWhere(['ai.uuid' => $user_uuid])
  249. ->andWhere(['<>','t.parent_id', 0])
  250. ->all();
  251. if ( 0 == count($row_last) ) {
  252. $row_last = (new \yii\db\Query())
  253. ->select(['t.assignees_arr','t.status','t.id'])
  254. //->select(['t.assignees_arr'])
  255. //->from('`asusg_job2launch` as `jl`')
  256. ->from('`tasks` as `t`', 'jl.id = t.asusg_job_mapped_id')
  257. //->rightJoin('`accounts_internal` as `ai`', 't.assignees_arr = cast(ai.id as char)')
  258. //->where(['jl.asusg_job_uuid' => $job_uuid])
  259. //->andWhere(['ai.uuid' => $user_uuid])
  260. ->andWhere(['t.parent_id' => $asusgJob2LaunchEntity->tasks->id])
  261. ->all();
  262. }
  263. //var_dump($row_last) ;
  264. //exit;
  265. if ( 0 == count($row_last) ) {
  266. $this->returnArrayWorks['error'] = 'Работа не запланирована';
  267. return $this->returnArrayWorks;
  268. }
  269. $countWorks = count($row_last);
  270. $countWorksDone = 0;
  271. foreach($row_last as $work){
  272. if( 5 == $work['status'] ){
  273. $countWorksDone++;
  274. }
  275. }
  276. $doneWorks = (int)round((100/$countWorks)* $countWorksDone);
  277. if ( 0 <= $doneWorks ) {//var_dump('asda');
  278. $this->returnArrayWorks['result'] = true;
  279. $this->returnArrayWorks['int'] = $doneWorks;
  280. return $this->returnArrayWorks;
  281. }
  282. return $this->returnArrayWorks;
  283. //$sqlText = $row_last->createCommand()->getRawSql();
  284. //var_dump($sqlText);
  285. //var_dump($doneWorks);
  286. //exit;
  287. //$workParams = [];
  288. //foreach( $listWorks as $work){
  289. // $workParams[] = $work->id_work;
  290. // }
  291. // return AsusgJob2Launch::find()->where(['in','asusg_job_uuid',$workParams])->all();
  292. }
  293. }