123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?php /** Created by Anton on 07.05.2020. */
- namespace app\controllers;
- use Yii;
- use yii\web\Controller;
- use yii\web\Response;
- use app\models\entity\Accounts;
- use app\models\entity\Tasks;
- use app\models\entity\LocomotiveSeries;
- use app\models\entity\ProjectsLocotech;
- use app\models\entity\TaskStatusNames;
- /**
- * Class OtherController
- * Апи для СОУ
- * @package app\controllers
- */
- class SouController extends Controller
- {
- public function beforeAction($action)
- {
- Yii::$app->response->format = Response::FORMAT_JSON;
- return parent::beforeAction($action);
- }
- /**
- * Возвращает массив данных о пользователях указанной смены
- * @param int shiftId
- *
- * @return array
- */
- public function actionWorker_list_current_shift()
- {
- $shiftId = (int)Yii::$app->request->get('shiftId');
- $employees = Accounts::findAll(['shift_id' => $shiftId]);
- $response = [];
- foreach ($employees as $employee) {
- $task = Tasks::findOne([
- 'assignees_arr' => $employee->id,
- 'parent_id' => 0,
- 'status' => 3//Активные задачи
- ]);
- $response[] = [
- 'id' => $employee->id,
- 'fio' => $employee->name,
- 'status' => $employee->isOnline(),
- 'job_list' => $employee->getPositionsNames(),
- 'task' => $task->tasktypes->name,
- ];
- }
- return $response;
- }
- /**
- * Возвращает список задач поставленных сотруднику за последние 12 часов
- * @param int userId
- *
- * @return array
- * @throws \Exception
- */
- public function actionList_task_worker_12()
- {
- $userId = (int)Yii::$app->request->get('userId');
- $date = new \DateTime();
- $twelveHours = new \DateInterval('PT12H');
- $date->sub($twelveHours);
- $tasks = Tasks::find()
- ->where(['assignees_arr' => $userId])
- ->andWhere(['>=', 'assigned', $date->format('Y-m-d H:i:s')])
- ->all()
- ;
- $statuses = TaskStatusNames::find()->indexBy('id')->all();
- $response = [];
- $date = $date->format('Y-m-d H:i:s');
- foreach ($tasks as $task) {
- if (($task->assigned && $task->assigned > $date) || $task->created > $date) {
- $response[] = [
- 'id' => $task->id,
- 'name' => $task->tasktypes->name,
- 'status' => $statuses[$task->status]->name,
- 'desc' => $task->tasktypes->description,
- 'start' => $task->accepted_time,
- 'finish' => $task->finished_time,
- 'norm' => $task->tasktypes->time_to_complete_minutes,
- 'command' => (bool)$task->parent_id
- ];
- }
- }
- //Сортируем по accepted_time
- // usort($response, function($a, $b) {
- // return ($a['start'] < $b['start']);
- // });
- return $response;
- }
- /**
- * Возвращает данны о локомотивах за последние hours часов (что бы это не значило)
- * @param int hours
- *
- * @return array
- * @throws \Exception
- */
- public function actionLocomotive_list()
- {
- $hours = (int)Yii::$app->request->get('hours');
- $hoursInterval = new \DateInterval('PT' . $hours . 'H');
- $date = new \DateTime();
- $date->sub($hoursInterval);
- $projects = ProjectsLocotech::find()
- ->where(['>=', 'created', $date->format('Y-m-d H:i:s')])
- ->all()
- ;
- $response = [];
- foreach ($projects as $project) {
- $locomotive = LocomotiveSeries::findOne(['name' => $project->loco_type]);
- $statuses = TaskStatusNames::find()->indexBy('id')->all();
- $response[] = [
- 'id' => $locomotive->id,
- 'status' => $statuses[$project->status]->name,
- 'series' => $project->loco_type,
- 'number' => $project->loco_number,
- 'production_date' => $project->created,
- 'start' => $project->getAcceptedTime(),
- 'finish' => $project->getFinishedTime()
- ];
- }
- return $response;
- }
- /**
- * Возвращает все задачи и команды конкретного локомотива, отсортированных по рабочим
- * @param string locoSeries
- * @param int locoNumber
- * @param int hours
- *
- * @return array
- * @throws \Exception
- */
- public function actionList_task_worker_locomotive()
- {
- $locoSeries = (string)Yii::$app->request->get('locoSeries');
- $locoNumber = (int)Yii::$app->request->get('locoNumber');
- $hours = (int)Yii::$app->request->get('hours');
- $projects = ProjectsLocotech::findAll([
- 'loco_type' => $locoSeries,
- 'loco_number' => $locoNumber
- ]);
- //Все задачи для локомотива
- $tasks = [];
- foreach ($projects as $project) {
- $tasks = array_merge($tasks, $project->getTask()->all());
- }
- $statuses = TaskStatusNames::find()->indexBy('id')->all();
- $date = new \DateTime();
- $interval = new \DateInterval('PT' . $hours . 'H');
- $date->sub($interval);
- $dateString = $date->format('Y-m-d H:i:s');
- $response = [];
- /** @var Tasks $task */
- foreach ($tasks as $task) {
- if ($hours) {
- if ($task->created < $dateString) continue;
- }
- $user = Accounts::findOne($task->assignees_arr);
- $response[] = [
- 'id' => $task->id,
- 'fio' => $user->name,
- 'name' => $task->tasktypes->name,
- 'command' => (bool)$task->parent_id,
- 'status' => $statuses[$task->status]->name,
- 'start' => $task->accepted_time,
- 'finish' => $task->finished_time,
- 'norm' => $task->tasktypes->time_to_complete_minutes,
- 'assigned' => $task->assigned,
- 'project_id' => $task->input_id,
- 'parent_id' => $task->parent_id
- ];
- }
- return $response;
- }
- /**
- * Возвращает все должности с пользователями связанными с ними
- * @param int shiftId
- *
- * @return array
- * @throws \Exception
- */
- public function actionPositions()
- {
- $shiftId = (int)Yii::$app->request->get('shiftId');
- $allUsers = Accounts::findAll(['shift_id' => $shiftId]);
- $response = [];
- foreach ($allUsers as $user) {
- /** @var Accounts $user */
- foreach ($user->getPositionsList() as $positionInfo) {
- $response[] = [
- 'id' => $positionInfo['id'],
- 'fio' => $user->name,
- 'position' => $positionInfo['name'],
- 'main' => (bool)$positionInfo['main'],
- 'online' => $user->isOnline(),
- ];
- }
- }
- return $response;
- }
- }
|