SouController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php /** Created by Anton on 07.05.2020. */
  2. namespace app\controllers;
  3. use Yii;
  4. use yii\web\Controller;
  5. use yii\web\Response;
  6. use app\models\entity\Accounts;
  7. use app\models\entity\Tasks;
  8. use app\models\entity\LocomotiveSeries;
  9. use app\models\entity\ProjectsLocotech;
  10. use app\models\entity\TaskStatusNames;
  11. /**
  12. * Class OtherController
  13. * Апи для СОУ
  14. * @package app\controllers
  15. */
  16. class SouController extends Controller
  17. {
  18. public function beforeAction($action)
  19. {
  20. Yii::$app->response->format = Response::FORMAT_JSON;
  21. return parent::beforeAction($action);
  22. }
  23. /**
  24. * Возвращает массив данных о пользователях указанной смены
  25. * @param int shiftId
  26. *
  27. * @return array
  28. */
  29. public function actionWorker_list_current_shift()
  30. {
  31. $shiftId = (int)Yii::$app->request->get('shiftId');
  32. $employees = Accounts::findAll(['shift_id' => $shiftId]);
  33. $response = [];
  34. foreach ($employees as $employee) {
  35. $task = Tasks::findOne([
  36. 'assignees_arr' => $employee->id,
  37. 'parent_id' => 0,
  38. 'status' => 3//Активные задачи
  39. ]);
  40. $response[] = [
  41. 'id' => $employee->id,
  42. 'fio' => $employee->name,
  43. 'status' => $employee->isOnline(),
  44. 'job_list' => $employee->getPositionsNames(),
  45. 'task' => $task->tasktypes->name,
  46. ];
  47. }
  48. return $response;
  49. }
  50. /**
  51. * Возвращает список задач поставленных сотруднику за последние 12 часов
  52. * @param int userId
  53. *
  54. * @return array
  55. * @throws \Exception
  56. */
  57. public function actionList_task_worker_12()
  58. {
  59. $userId = (int)Yii::$app->request->get('userId');
  60. $date = new \DateTime();
  61. $twelveHours = new \DateInterval('PT12H');
  62. $date->sub($twelveHours);
  63. $tasks = Tasks::find()
  64. ->where(['assignees_arr' => $userId])
  65. ->andWhere(['>=', 'assigned', $date->format('Y-m-d H:i:s')])
  66. ->all()
  67. ;
  68. $statuses = TaskStatusNames::find()->indexBy('id')->all();
  69. $response = [];
  70. $date = $date->format('Y-m-d H:i:s');
  71. foreach ($tasks as $task) {
  72. if (($task->assigned && $task->assigned > $date) || $task->created > $date) {
  73. $response[] = [
  74. 'id' => $task->id,
  75. 'name' => $task->tasktypes->name,
  76. 'status' => $statuses[$task->status]->name,
  77. 'desc' => $task->tasktypes->description,
  78. 'start' => $task->accepted_time,
  79. 'finish' => $task->finished_time,
  80. 'norm' => $task->tasktypes->time_to_complete_minutes,
  81. 'command' => (bool)$task->parent_id
  82. ];
  83. }
  84. }
  85. //Сортируем по accepted_time
  86. // usort($response, function($a, $b) {
  87. // return ($a['start'] < $b['start']);
  88. // });
  89. return $response;
  90. }
  91. /**
  92. * Возвращает данны о локомотивах за последние hours часов (что бы это не значило)
  93. * @param int hours
  94. *
  95. * @return array
  96. * @throws \Exception
  97. */
  98. public function actionLocomotive_list()
  99. {
  100. $hours = (int)Yii::$app->request->get('hours');
  101. $hoursInterval = new \DateInterval('PT' . $hours . 'H');
  102. $date = new \DateTime();
  103. $date->sub($hoursInterval);
  104. $projects = ProjectsLocotech::find()
  105. ->where(['>=', 'created', $date->format('Y-m-d H:i:s')])
  106. ->all()
  107. ;
  108. $response = [];
  109. foreach ($projects as $project) {
  110. $locomotive = LocomotiveSeries::findOne(['name' => $project->loco_type]);
  111. $statuses = TaskStatusNames::find()->indexBy('id')->all();
  112. $response[] = [
  113. 'id' => $locomotive->id,
  114. 'status' => $statuses[$project->status]->name,
  115. 'series' => $project->loco_type,
  116. 'number' => $project->loco_number,
  117. 'production_date' => $project->created,
  118. 'start' => $project->getAcceptedTime(),
  119. 'finish' => $project->getFinishedTime()
  120. ];
  121. }
  122. return $response;
  123. }
  124. /**
  125. * Возвращает все задачи и команды конкретного локомотива, отсортированных по рабочим
  126. * @param string locoSeries
  127. * @param int locoNumber
  128. * @param int hours
  129. *
  130. * @return array
  131. * @throws \Exception
  132. */
  133. public function actionList_task_worker_locomotive()
  134. {
  135. $locoSeries = (string)Yii::$app->request->get('locoSeries');
  136. $locoNumber = (int)Yii::$app->request->get('locoNumber');
  137. $hours = (int)Yii::$app->request->get('hours');
  138. $projects = ProjectsLocotech::findAll([
  139. 'loco_type' => $locoSeries,
  140. 'loco_number' => $locoNumber
  141. ]);
  142. //Все задачи для локомотива
  143. $tasks = [];
  144. foreach ($projects as $project) {
  145. $tasks = array_merge($tasks, $project->getTask()->all());
  146. }
  147. $statuses = TaskStatusNames::find()->indexBy('id')->all();
  148. $date = new \DateTime();
  149. $interval = new \DateInterval('PT' . $hours . 'H');
  150. $date->sub($interval);
  151. $dateString = $date->format('Y-m-d H:i:s');
  152. $response = [];
  153. /** @var Tasks $task */
  154. foreach ($tasks as $task) {
  155. if ($hours) {
  156. if ($task->created < $dateString) continue;
  157. }
  158. $user = Accounts::findOne($task->assignees_arr);
  159. $response[] = [
  160. 'id' => $task->id,
  161. 'fio' => $user->name,
  162. 'name' => $task->tasktypes->name,
  163. 'command' => (bool)$task->parent_id,
  164. 'status' => $statuses[$task->status]->name,
  165. 'start' => $task->accepted_time,
  166. 'finish' => $task->finished_time,
  167. 'norm' => $task->tasktypes->time_to_complete_minutes,
  168. 'assigned' => $task->assigned,
  169. 'project_id' => $task->input_id,
  170. 'parent_id' => $task->parent_id
  171. ];
  172. }
  173. return $response;
  174. }
  175. /**
  176. * Возвращает все должности с пользователями связанными с ними
  177. * @param int shiftId
  178. *
  179. * @return array
  180. * @throws \Exception
  181. */
  182. public function actionPositions()
  183. {
  184. $shiftId = (int)Yii::$app->request->get('shiftId');
  185. $allUsers = Accounts::findAll(['shift_id' => $shiftId]);
  186. $response = [];
  187. foreach ($allUsers as $user) {
  188. /** @var Accounts $user */
  189. foreach ($user->getPositionsList() as $positionInfo) {
  190. $response[] = [
  191. 'id' => $positionInfo['id'],
  192. 'fio' => $user->name,
  193. 'position' => $positionInfo['name'],
  194. 'main' => (bool)$positionInfo['main'],
  195. 'online' => $user->isOnline(),
  196. ];
  197. }
  198. }
  199. return $response;
  200. }
  201. }