add api/company/get-personal

This commit is contained in:
iIronside 2023-11-24 16:07:12 +03:00
parent cd9f828f60
commit 932ea915de
11 changed files with 331 additions and 6 deletions

View File

@ -61,6 +61,14 @@ class ProjectTask extends ActiveRecord
];
}
/**
* @return int[]
*/
public static function openTaskStatusList(): array
{
return [self::STATUS_ACTIVE, self::STATUS_AT_WORK];
}
/**
* @param $priority
* @return string

View File

@ -2,8 +2,6 @@
namespace common\models;
use Yii;
/**
* This is the model class for table "test_task".
*

View File

@ -0,0 +1,57 @@
<?php
namespace frontend\modules\api\controllers;
use frontend\modules\api\models\company\form\CompanyIdForm;
use frontend\modules\api\services\PersonnelService;
use Yii;
class CompanyController extends ApiController
{
public PersonnelService $personnelService;
public function __construct(
$id,
$module,
PersonnelService $personnelService,
$config = []
)
{
$this->personnelService = $personnelService;
parent::__construct($id, $module, $config);
}
/**
* @OA\Get(path="/company/get-personal",
* summary="Персонал компании",
* description="Метод для получения персонала компании",
* security={
* {"bearerAuth": {}}
* },
* tags={"Company"},
* @OA\Parameter(
* name="company_id",
* description="ID компании",
* in="query",
* required=true,
* @OA\Schema(
* type="integer",
* )
* ),
* @OA\Response(
* response=200,
* description="Возвращает масив объектов сотрудников",
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(ref="#/components/schemas/CompanyPersonnelDtoExampleArr"),
* ),
* ),
* )
*
* @return CompanyIdForm|array
*/
public function actionGetPersonal(): CompanyIdForm|array
{
return $this->personnelService->getPersonnel(Yii::$app->request->get());
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace frontend\modules\api\models;
namespace frontend\modules\api\models\company;
/**
*

View File

@ -0,0 +1,106 @@
<?php
namespace frontend\modules\api\models\company\dto;
/**
*
* @OA\Schema(
* schema="CompanyPersonnelDto",
* @OA\Property(
* property="userId",
* type="int",
* example=95,
* description="Идентификатор пользователя"
* ),
* @OA\Property(
* property="fio",
* type="string",
* example="Кочетков Валерий Александрович",
* description="ФИО пользователя"
* ),
* @OA\Property(
* property="position",
* type="string",
* example="Back end разработчик",
* description="Должность пользователя"
* ),
* @OA\Property(
* property="level",
* type="int",
* example="Middle",
* description="Уровень компетенций"
* ),
* @OA\Property(
* property="projectName",
* type="string",
* example="Проект 1",
* description="Название проекта на котором работает"
* ),
* @OA\Property(
* property="openTaskCount",
* type="int",
* example="5",
* description="Количество открытых задач на проекте"
* ),
* @OA\Property(
* property="hoursWorkedForCurrentMonth",
* type="int",
* example="5",
* description="Количество часов отработанных в текущем месяце"
* ),
*)
*
* @OA\Schema(
* schema="CompanyPersonnelDtoExampleArr",
* type="array",
* example={
* {"userId": 23, "fio": "Кочетков Валерий Александрович", "position": "Back end разработчик", "level": 2, "projectName": "Проект 1", "openTaskCount": 4, "hoursWorkedForCurrentMonth": 30},
* {"userId": 16, "fio": "Шишкина Милана Андреевна", "position": "Back end разработчик", "level": 1, "projectName": "Проект 2", "openTaskCount": 8, "hoursWorkedForCurrentMonth": 15},
* },
* @OA\Items(
* type="object",
* @OA\Property(
* property="userId",
* type="integer",
* ),
* @OA\Property(
* property="fio",
* type="string",
* ),
* @OA\Property(
* property="position",
* type="string",
* ),
* @OA\Property(
* property="level",
* type="integer",
* ),
* @OA\Property(
* property="projectName",
* type="string",
* ),
* @OA\Property(
* property="openTaskCount",
* type="integer",
* ),
* @OA\Property(
* property="hoursWorkedForCurrentMonth",
* type="integer",
* ),
* ),
*)
*
*
*
*/
class CompanyPersonnelDto
{
public $userId;
public $fio;
public $position;
public $level;
public $projectName;
public $openTaskCount;
public $hoursWorkedForCurrentMonth;
}

View File

@ -0,0 +1,30 @@
<?php
namespace frontend\modules\api\models\company\form;
use frontend\modules\api\models\company\Company;
use yii\base\Model;
class CompanyIdForm extends Model
{
public $company_id;
/**
* @return array
*/
public function rules()
{
return [
[['company_id'], 'required'],
[['company_id'], 'exist', 'skipOnError' => false, 'targetClass' => Company::class, 'targetAttribute' => ['company_id' => 'id']],
];
}
/**
* @return string
*/
public function formName(): string
{
return '';
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace frontend\modules\api\models\company\mappers;
use frontend\modules\api\models\company\dto\CompanyPersonnelDto;
use frontend\modules\api\models\project\ProjectUser;
use frontend\modules\api\services\TaskService;
class CompanyPersonnelMapper
{
/**
* @param ProjectUser $projectUser
* @return CompanyPersonnelDto
*/
public static function map(ProjectUser $projectUser): CompanyPersonnelDto
{
$dto = new CompanyPersonnelDto();
$dto->userId = $projectUser->user_id;
$dto->fio = $projectUser->card->fio ?? null;
$dto->position = $projectUser->card->position->name ?? null;
$dto->level = $projectUser->card->level ?? null;
$dto->projectName = $projectUser->project->name;
$dto->openTaskCount = TaskService::getOpenTaskCount($projectUser->user_id, $projectUser->project_id);
$dto->hoursWorkedForCurrentMonth = TaskService::getHoursWorkedForCurrentMonth($projectUser->user_id, $projectUser->project_id);
return $dto;
}
/**
* @param array $projectUsers
* @return array
*/
public static function mapAll(array $projectUsers): array
{
return array_map(function (ProjectUser $projectUser) {
return self::map($projectUser);
}, array_values($projectUsers));
}
}

View File

@ -2,11 +2,10 @@
namespace frontend\modules\api\models\project;
use frontend\modules\api\models\Company;
use frontend\modules\api\models\company\Company;
use yii\db\ActiveQuery;
use yii\helpers\Url;
use yii\web\Link;
use yii\web\Linkable;
/**
*

View File

@ -2,7 +2,7 @@
namespace frontend\modules\api\models\project;
use frontend\modules\api\models\Company;
use frontend\modules\api\models\company\Company;
use yii\db\ActiveQuery;
use yii\helpers\Url;
use yii\web\Link;

View File

@ -0,0 +1,35 @@
<?php
namespace frontend\modules\api\services;
use frontend\modules\api\models\company\form\CompanyIdForm;
use frontend\modules\api\models\company\mappers\CompanyPersonnelMapper;
use frontend\modules\api\models\project\Project;
class PersonnelService
{
/**
* @param array $params
* @return array|CompanyIdForm
*/
public function getPersonnel(array $params): CompanyIdForm|array
{
$form = new CompanyIdForm();
$form->load($params);
if (!$form->validate()){
return $form;
}
$projects = Project::find()->where(['company_id' => $form->company_id])->all();
$personals = [];
/** @var Project $project */
foreach ($projects as $project) {
$personals += CompanyPersonnelMapper::mapAll($project->projectUsers);
}
return $personals;
}
}

View File

@ -2,10 +2,13 @@
namespace frontend\modules\api\services;
use common\models\Entity;
use common\models\forms\TasksImportForm;
use common\services\ImportProjectTaskService;
use DateTime;
use frontend\modules\api\models\project\ProjectTask;
use frontend\modules\api\models\project\ProjectTaskUser;
use frontend\modules\api\models\Timer;
use yii\web\BadRequestHttpException;
class TaskService
@ -17,6 +20,54 @@ class TaskService
$this->importProjectTaskService = $importProjectTaskService;
}
public static function getOpenTaskCount(int $user_id, int $project_id): bool|int|string|null
{
return ProjectTask::find()
->where(['user_id' => $user_id])
->andWhere(['project_id' => $project_id])
->andWhere(['in', 'status', ProjectTask::openTaskStatusList()])
->count();
}
public static function getHoursWorkedForCurrentMonth(int $user_id, int $project_id)
{
$projectTaskIdArr = ProjectTask::find()
->select('id')
->where(['user_id' => $user_id])
->andWhere(['project_id' => $project_id])
->column();
$firstMonthDay = new DateTime('first day of this month');
$firstMonthDay->setTime(00, 00, 01);
$firstMonthDay = $firstMonthDay->format('Y-m-d H:i:s');
$lastMonthDay = new DateTime('last day of this month');
$lastMonthDay->setTime(23, 59, 00);
$lastMonthDay = $lastMonthDay->format('Y-m-d H:i:s');
$timers = Timer::find()
->where(['user_id' => $user_id])
->andWhere(['entity_type' => Entity::ENTITY_TYPE_TASK])
->andWhere(['in', 'entity_id', $projectTaskIdArr])
->andWhere(['between', 'created_at', $firstMonthDay, $lastMonthDay ])
->all();
$hours = 0;
/** @var Timer $timer */
foreach ($timers as $timer) {
// Create two new DateTime-objects...
$date1 = new DateTime($timer->created_at);
$date2 = new DateTime($timer->stopped_at);
$diff = $date2->diff($date1);
$hours += $diff->h + ($diff->days*24);
}
return $hours;
}
public function createTask($taskParams)
{
$task = new ProjectTask();