MappingWorksModel.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. use app\models\entity\Tasks;
  5. use app\models\entity\AsusgJob2Launch;
  6. class MappingworksModel extends MainModel
  7. {
  8. private static $_instance;
  9. public $returnArrayWorks = ['result' => false, 'int'=> 0, 'error'=> ''];
  10. private $url = '/Thingworx/Things/2050UserLibraryExp/Services/';
  11. private $execMethod = 'work_to_execute';
  12. private $doneMethod = 'work_done';
  13. public static function workToExecute($tid,$test = 0)
  14. {
  15. $result = false;
  16. self::$_instance = new self();
  17. $GWparams = [
  18. 'url' => 'http://' . Yii::$app->params['api']['domain'] . self::$_instance->url,
  19. 'param' => [],
  20. 'login' => 'ПройдаковИА',
  21. 'pass' => 'Ip15351112'
  22. ];
  23. $tasksEntity = Tasks::findOne($tid);
  24. if ( null == $tasksEntity->asusg_job_mapped_id ) {
  25. Logs::addlog(2,'работы не нашел => '.$tid.' ->' . $tasksEntity->asusg_job_mapped_id);
  26. return $result;
  27. }
  28. Logs::addlog(2,'принял работу => '.$tid.', завершение команды => ' . $tasksEntity->accounts->uuid);
  29. Logs::addlog(2,'принял работу => '.$tid.', завершение команды => ' . $tasksEntity->asusgjob2launch->asusg_job_uuid);
  30. if( null == $tasksEntity->accounts->uuid || null == $tasksEntity->asusgjob2launch->asusg_job_uuid ) {
  31. Logs::addlog(2,'не нашел uuid-ы. id = ' . $tid);
  32. return $result;
  33. }
  34. $status = self::$_instance->worksStatusHandler( $tasksEntity->accounts->uuid, $tasksEntity->asusgjob2launch->asusg_job_uuid );
  35. $GWparams['param'] = ['id_work' => $tasksEntity->asusgjob2launch->asusg_job_uuid, 'user_id' => $tasksEntity->accounts->uuid];
  36. if ( $status ) {
  37. $GWparams['url'] .= self::$_instance->doneMethod;
  38. //Logs::addlog(4,json_encode($GWparams),true);
  39. ElasticLog::eipp($GWparams, $tasksEntity->asusgjob2launch->asusg_job_uuid, $tasksEntity->accounts->uuid);
  40. $execResult = self::$_instance->curlRequestUni($GWparams,'post');
  41. ElasticLog::eipp($execResult, $tasksEntity->asusgjob2launch->asusg_job_uuid, $tasksEntity->accounts->uuid, false);
  42. //Logs::addlog(4,$execResult);
  43. LogEippResponse::addLog($time . "done1" . $tasksEntity->asusgjob2launch->asusg_job_uuid, json_encode($GWparams) ,'Попытка закрыть, параметры');
  44. LogEippResponse::addLog($time . "done2" . $tasksEntity->asusgjob2launch->asusg_job_uuid, $execResult ,'Попытка закрыть, ответ от ЕИПП');
  45. Logs::addlog(4,'ФИО : '. $tasksEntity->accounts->name. ', Заказ наряд: '. $tasksEntity->asusgjob2launch->outfit . ', Секция: ' . $tasksEntity->project->sections->section_number.$tasksEntity->project->sections->letter);
  46. $result = self::$_instance->checkResponse($execResult);
  47. //$result = 'true';
  48. Logs::addlog(4,$result);
  49. LogEippResponse::addLog($time . "done4" . $tasksEntity->asusgjob2launch->asusg_job_uuid, $result ,'Работа должна закрыться');
  50. return $result;
  51. }
  52. Logs::addlog(2,'execMethod');
  53. $GWparams['url'] .= self::$_instance->execMethod;
  54. ElasticLog::eipp($GWparams, $tasksEntity->asusgjob2launch->asusg_job_uuid, $tasksEntity->accounts->uuid);
  55. $execResult = self::$_instance->curlRequestUni($GWparams,'post');
  56. ElasticLog::eipp($execResult, $tasksEntity->asusgjob2launch->asusg_job_uuid, $tasksEntity->accounts->uuid, false);
  57. LogEippResponse::addLog($time . "done" . $tasksEntity->asusgjob2launch->asusg_job_uuid, $execResult ,'Работа не должна закрыться');
  58. //var_dump($execResult);
  59. //$result = self::$_instance->checkResponse($execResult);
  60. // ЗАГЛУШКА
  61. $result = true;
  62. //var_dump($result);
  63. return $result;
  64. }
  65. public static function workToExecuteBegin($tid,$test = 0)
  66. {
  67. $result = false;
  68. self::$_instance = new self();
  69. $GWparams = [
  70. 'url' => 'http://' . Yii::$app->params['api']['domain'] . self::$_instance->url,
  71. 'param' => [],
  72. 'login' => 'ПройдаковИА',
  73. 'pass' => 'Ip15351112'
  74. ];
  75. $tasksEntity = Tasks::findOne($tid);
  76. if ( null == $tasksEntity->asusg_job_mapped_id ) {
  77. Logs::addlog(2,'работы не нашел => '.$tid.' ->' . $tasksEntity->asusg_job_mapped_id);
  78. return $result;
  79. }
  80. Logs::addlog(2,'принял работу => '.$tid.', завершение команды => ' . $tasksEntity->accounts->uuid);
  81. Logs::addlog(2,'принял работу => '.$tid.', завершение команды => ' . $tasksEntity->asusgjob2launch->asusg_job_uuid);
  82. if( null == $tasksEntity->accounts->uuid || null == $tasksEntity->asusgjob2launch->asusg_job_uuid ) {
  83. Logs::addlog(2,'не нашел uuid-ы. id = ' . $tid);
  84. return $result;
  85. }
  86. //$status = self::$_instance->worksStatusHandler( $tasksEntity->accounts->uuid, $tasksEntity->asusgjob2launch->asusg_job_uuid );
  87. $GWparams['param'] = ['id_work' => $tasksEntity->asusgjob2launch->asusg_job_uuid, 'user_id' => $tasksEntity->accounts->uuid];
  88. Logs::addlog(2,'execMethod');
  89. $GWparams['url'] .= self::$_instance->execMethod;
  90. ElasticLog::eipp($GWparams, $tasksEntity->asusgjob2launch->asusg_job_uuid, $tasksEntity->accounts->uuid);
  91. $execResult = self::$_instance->curlRequestUni($GWparams,'post');
  92. ElasticLog::eipp($execResult, $tasksEntity->asusgjob2launch->asusg_job_uuid, $tasksEntity->accounts->uuid, false);
  93. LogEippResponse::addLog($time . "done" . $tasksEntity->asusgjob2launch->asusg_job_uuid, $execResult ,'Работа не должна закрыться');
  94. //var_dump($execResult);
  95. //$result = self::$_instance->checkResponse($execResult);
  96. // ЗАГЛУШКА
  97. $result = true;
  98. //var_dump($result);
  99. return $result;
  100. }
  101. public static function checkResponse($execResult)
  102. {
  103. $conf = ['true' => true, 'false' => false];
  104. $execResult = json_decode($execResult,true);
  105. if ( is_array($execResult)) {
  106. foreach( $execResult['rows'] as $row) {
  107. $rowArray = json_decode($row["result"],true);
  108. foreach($rowArray as $key => $r) {
  109. if ( $key == 'result' and null != $r) {
  110. return $rowArray["result"];
  111. }
  112. }
  113. }
  114. }
  115. return false;
  116. }
  117. public function worksStatusHandler($user_uuid, $job_uuid){
  118. //public function worksStatusHandler(){
  119. /*
  120. $accountsEntity = Accounts::find()->where(['uuid' => $user_uuid])->one();
  121. //var_dump($accountsEntity); exit;
  122. //if ( 0 == count($accountsEntity) ) {
  123. if ( null == $accountsEntity ) {
  124. self::$_instance->returnArrayWorks['error'] = 'Не найден сотрудник';
  125. return false;//self::$_instance->returnArrayWorks;
  126. }
  127. */
  128. $asusgJob2LaunchEntity = AsusgJob2Launch::find()->where(['asusg_job_uuid' => $job_uuid])->one();
  129. //if ( 0 == count($asusgJob2LaunchEntity) ) {
  130. if ( null == $asusgJob2LaunchEntity ) {
  131. self::$_instance->returnArrayWorks['error'] = 'Не найдена работа';
  132. return false;//self::$_instance->returnArrayWorks;
  133. }
  134. //var_dump($asusgJob2LaunchEntity->tasks->id); exit;
  135. $qu = 1;
  136. $parLog = 'jl.asusg_job_uuid' . $job_uuid;
  137. $row_last = (new \yii\db\Query())
  138. ->select(['t.assignees_arr','ai.id','t.status','t.id'])
  139. //->select(['t.assignees_arr'])
  140. ->from('`asusg_job2launch` as `jl`')
  141. ->leftJoin('`tasks` as `t`', 'jl.id = t.asusg_job_mapped_id')
  142. ->rightJoin('`accounts_internal` as `ai`', 't.assignees_arr = cast(ai.id as char)')
  143. ->where(['jl.asusg_job_uuid' => $job_uuid])
  144. //->andWhere(['ai.uuid' => $user_uuid])
  145. ->andWhere(['<>','t.parent_id', 0])
  146. ->all();
  147. if ( 0 == count($row_last) ) {
  148. $qu = 2;
  149. $parLog = 't.parent_id = '.$asusgJob2LaunchEntity->tasks->id;
  150. $row_last = (new \yii\db\Query())
  151. ->select(['t.assignees_arr','t.status','t.id'])
  152. //->select(['t.assignees_arr'])
  153. //->from('`asusg_job2launch` as `jl`')
  154. ->from('`tasks` as `t`', 'jl.id = t.asusg_job_mapped_id')
  155. //->rightJoin('`accounts_internal` as `ai`', 't.assignees_arr = cast(ai.id as char)')
  156. //->where(['jl.asusg_job_uuid' => $job_uuid])
  157. //->andWhere(['ai.uuid' => $user_uuid])
  158. ->andWhere(['t.parent_id' => $asusgJob2LaunchEntity->tasks->id])
  159. ->all();
  160. }
  161. //var_dump($row_last) ;
  162. //exit;
  163. if ( 0 == count($row_last) ) {
  164. self::$_instance->returnArrayWorks['error'] = 'Работа не запланирована';
  165. return false;//self::$_instance->returnArrayWorks;
  166. }
  167. $countWorks = count($row_last);
  168. $countWorksDone = 1;
  169. foreach($row_last as $work){
  170. //Logs::addlog(2,'status =>' . $work['status']);
  171. if( 5 == $work['status'] ){
  172. $countWorksDone++;
  173. }
  174. }
  175. Logs::addlog(2,'Кол-во команд =>'.count($row_last). ', Сработал запрос №=' . $qu . ', переменная для проверки ' . $parLog);
  176. $doneWorks = (int)round((100/$countWorks)* $countWorksDone);
  177. Logs::addlog(2,'worksStatusHandler2 =>'.$doneWorks . ': (100/'.$countWorks.')* '.$countWorksDone);
  178. if ( 100 != $doneWorks ) {
  179. return false;
  180. }
  181. /*
  182. if ( 0 <= $doneWorks ) {//var_dump('asda');
  183. self::$_instance->returnArrayWorks['result'] = true;
  184. self::$_instance->returnArrayWorks['int'] = $doneWorks;
  185. return self::$_instance->returnArrayWorks;
  186. }
  187. */
  188. return true;//self::$_instance->returnArrayWorks;
  189. //$sqlText = $row_last->createCommand()->getRawSql();
  190. //var_dump($sqlText);
  191. //var_dump($doneWorks);
  192. //exit;
  193. //$workParams = [];
  194. //foreach( $listWorks as $work){
  195. // $workParams[] = $work->id_work;
  196. // }
  197. // return AsusgJob2Launch::find()->where(['in','asusg_job_uuid',$workParams])->all();
  198. }
  199. }