requests
This commit is contained in:
63
common/models/KnowledgeLevel.php
Normal file
63
common/models/KnowledgeLevel.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use Yii;
|
||||
|
||||
/**
|
||||
* This is the model class for table "knowledge_level".
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $title
|
||||
* @property int $status
|
||||
*/
|
||||
class KnowledgeLevel extends \yii\db\ActiveRecord
|
||||
{
|
||||
const STATUS_ACTIVE = 1;
|
||||
const STATUS_DISABLE = 0;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function tableName()
|
||||
{
|
||||
return 'knowledge_level';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['title'], 'required'],
|
||||
[['status'], 'integer'],
|
||||
[['title'], 'string', 'max' => 255],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'id' => 'ID',
|
||||
'title' => 'Название',
|
||||
'status' => 'Статус',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public static function getStatus(): array
|
||||
{
|
||||
return [
|
||||
self::STATUS_ACTIVE => 'Активен',
|
||||
self::STATUS_DISABLE => 'Выключен'
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
namespace common\models;
|
||||
|
||||
use Yii;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
/**
|
||||
* This is the model class for table "position".
|
||||
@ -46,4 +47,9 @@ class Position extends \yii\db\ActiveRecord
|
||||
{
|
||||
return $this->hasMany(UserCard::class, ['position_id' => 'id']);
|
||||
}
|
||||
|
||||
public static function getList()
|
||||
{
|
||||
return ArrayHelper::map(self::find()->all(), 'id', 'name');
|
||||
}
|
||||
}
|
||||
|
159
common/models/Request.php
Normal file
159
common/models/Request.php
Normal file
@ -0,0 +1,159 @@
|
||||
<?php
|
||||
|
||||
namespace common\models;
|
||||
|
||||
use common\classes\Debug;
|
||||
use common\services\RequestService;
|
||||
use Yii;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
use yii\db\ActiveQuery;
|
||||
use yii\db\Expression;
|
||||
|
||||
/**
|
||||
* This is the model class for table "request".
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $created_at
|
||||
* @property string $updated_at
|
||||
* @property int $user_id
|
||||
* @property string $title
|
||||
* @property int $position_id
|
||||
* @property string $skill_ids
|
||||
* @property int $knowledge_level_id
|
||||
* @property string $descr
|
||||
* @property int $specialist_count
|
||||
* @property int $status
|
||||
* @property int $result_count
|
||||
*/
|
||||
class Request extends \yii\db\ActiveRecord
|
||||
{
|
||||
const STATUS_ACTIVE = 1;
|
||||
const STATUS_DISABLE = 0;
|
||||
|
||||
public int $result_count = 0;
|
||||
|
||||
public array $skills = [];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function tableName()
|
||||
{
|
||||
return 'request';
|
||||
}
|
||||
|
||||
public function behaviors()
|
||||
{
|
||||
return [
|
||||
[
|
||||
'class' => TimestampBehavior::class,
|
||||
'createdAtAttribute' => 'created_at',
|
||||
'updatedAtAttribute' => 'updated_at',
|
||||
'value' => new Expression('NOW()'),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['created_at', 'updated_at', 'skills'], 'safe'],
|
||||
[['user_id', 'title', 'position_id', 'status'], 'required'],
|
||||
[['user_id', 'position_id', 'knowledge_level_id', 'specialist_count', 'status'], 'integer'],
|
||||
[['descr'], 'string'],
|
||||
[['title'], 'string', 'max' => 255],
|
||||
[['skill_ids'], 'safe']
|
||||
];
|
||||
}
|
||||
|
||||
public function fields()
|
||||
{
|
||||
$fields = parent::fields();
|
||||
|
||||
$additionalFields = [
|
||||
'position',
|
||||
'skills',
|
||||
'result_count',
|
||||
'level' => function (Request $model) {
|
||||
return UserCard::getLevelList()[$model->knowledge_level_id];
|
||||
},
|
||||
];
|
||||
|
||||
return array_merge($fields, $additionalFields);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'id' => 'ID',
|
||||
'created_at' => 'Дата создания',
|
||||
'updated_at' => 'Дата редактирования',
|
||||
'user_id' => 'Пользователь',
|
||||
'title' => 'Заголовок',
|
||||
'position_id' => 'Специализация',
|
||||
'skill_ids' => 'Навыки',
|
||||
'knowledge_level_id' => 'Уровень',
|
||||
'descr' => 'Описание',
|
||||
'specialist_count' => 'Кол-во специалистов',
|
||||
'status' => 'Статус',
|
||||
];
|
||||
}
|
||||
|
||||
public function beforeSave($insert)
|
||||
{
|
||||
if (parent::beforeSave($insert)) {
|
||||
if ($this->skill_ids && is_array($this->skill_ids)) {
|
||||
$this->skill_ids = implode(",", $this->skill_ids);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function afterFind()
|
||||
{
|
||||
parent::afterFind();
|
||||
if ($this->skill_ids != "") {
|
||||
$this->skill_ids = explode(",", $this->skill_ids);
|
||||
}
|
||||
else {
|
||||
$this->skill_ids = [];
|
||||
}
|
||||
|
||||
$this->skills = Skill::find()->where(['id' => $this->skill_ids])->asArray()->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getPosition(): \yii\db\ActiveQuery
|
||||
{
|
||||
return $this->hasOne(Position::class, ['id' => 'position_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getUser(): \yii\db\ActiveQuery
|
||||
{
|
||||
return $this->hasOne(User::class, ['id' => 'user_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public static function getStatus(): array
|
||||
{
|
||||
return [
|
||||
self::STATUS_ACTIVE => 'Активен',
|
||||
self::STATUS_DISABLE => 'Выключен'
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@ -261,6 +261,17 @@ class UserCard extends \yii\db\ActiveRecord
|
||||
return ArrayHelper::map(self::find()->all(), 'id', 'fio');
|
||||
}
|
||||
|
||||
|
||||
public static function getListUserWithUserId($statusId = null)
|
||||
{
|
||||
$list = self::find();
|
||||
if ($statusId){
|
||||
$list->where(['status' => $statusId]);
|
||||
}
|
||||
|
||||
return ArrayHelper::map($list->all(), 'id_user', 'fio');
|
||||
}
|
||||
|
||||
public function getManager()
|
||||
{
|
||||
return $this->hasOne(Manager::class, ['user_card_id' => 'id']);
|
||||
|
336
common/services/RequestService.php
Normal file
336
common/services/RequestService.php
Normal file
@ -0,0 +1,336 @@
|
||||
<?php
|
||||
|
||||
namespace common\services;
|
||||
|
||||
use common\classes\Debug;
|
||||
use common\models\CardSkill;
|
||||
use common\models\Request;
|
||||
use common\models\UserCard;
|
||||
use yii\helpers\ArrayHelper;
|
||||
|
||||
class RequestService
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public int $id;
|
||||
|
||||
/**
|
||||
* @var Request
|
||||
*/
|
||||
public Request $model;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public array $errors = [];
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
public bool $isSave = false;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
public bool $isLoad = false;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private array $excludePool = [];
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private bool $skillsFullEntry = true;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private bool $cardAsArray = true;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private bool $checkCardLevel = true;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private bool $useCardExcludePool = false;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private bool $checkCardPosition = true;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private bool $returnCount = false;
|
||||
|
||||
|
||||
public function __construct(int $id = null)
|
||||
{
|
||||
if ($id) {
|
||||
$this->id = $id;
|
||||
$this->model = Request::findOne($id);
|
||||
} else {
|
||||
$this->model = new Request();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @return RequestService
|
||||
*/
|
||||
public function save(array $params = []): RequestService
|
||||
{
|
||||
//$this->model->load($params);
|
||||
if ($this->model->validate()) {
|
||||
$this->isSave = $this->model->save();
|
||||
$this->id = $this->model->id;
|
||||
} else {
|
||||
$this->errors = $this->model->errors;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @return RequestService
|
||||
*/
|
||||
public function load(array $params, $formName = "Request"): RequestService
|
||||
{
|
||||
if ($this->model->load($params, $formName)) {
|
||||
$this->isLoad = true;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Request|null
|
||||
*/
|
||||
public function getModel(): ?Request
|
||||
{
|
||||
return $this->model;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isFind(): bool
|
||||
{
|
||||
if ($this->model->id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @return array|Request|null
|
||||
*/
|
||||
public function getById()
|
||||
{
|
||||
return $this->model->find()->where(['id' => $this->id])->one();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $user_id
|
||||
* @return array|Request|\yii\db\ActiveRecord[]
|
||||
*/
|
||||
public function getByUserId($user_id)
|
||||
{
|
||||
return $this->model->find()->where(['user_id' => $user_id])->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $userId
|
||||
* @return $this
|
||||
*/
|
||||
public function setUserId($userId): RequestService
|
||||
{
|
||||
$this->model->user_id = $userId;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|int
|
||||
*/
|
||||
public function _search()
|
||||
{
|
||||
$model = $this->getById($this->id);
|
||||
$subQ = false;
|
||||
if (!empty($model->skill_ids)) {
|
||||
$subQ = CardSkill::find()->select('card_skill.card_id')
|
||||
->where(['skill_id' => $model->skill_ids])
|
||||
->groupBy('card_id HAVING COUNT(DISTINCT skill_id) = :count')
|
||||
->addParams([':count' => count($model->skill_ids)]);
|
||||
}
|
||||
|
||||
|
||||
$q = UserCard::find()->select('user_card.id, fio, user_card.position_id, card_skill.skill_id')
|
||||
->leftJoin('card_skill', 'card_skill.card_id = user_card.id')
|
||||
->where(['deleted_at' => null])
|
||||
->andWhere(['status' => [4, 12]])
|
||||
->andWhere(['not', ['position_id' => null]]);
|
||||
|
||||
if ($this->checkCardPosition) {
|
||||
$q->andWhere(['position_id' => $model->position_id]);
|
||||
}
|
||||
|
||||
if ($this->checkCardLevel) {
|
||||
$q->andWhere(['level' => $model->knowledge_level_id]);
|
||||
}
|
||||
|
||||
if ($this->skillsFullEntry && $subQ) {
|
||||
$q->andWhere(['user_card.id' => $subQ]);
|
||||
}
|
||||
|
||||
if ($model->skill_ids) {
|
||||
$q->andWhere(['card_skill.skill_id' => $model->skill_ids]);
|
||||
}
|
||||
|
||||
if ($this->useCardExcludePool) {
|
||||
$q->andWhere(['not', ['user_card.id' => $this->excludePool]]);
|
||||
}
|
||||
|
||||
$q->groupBy('user_card.id');
|
||||
|
||||
if ($this->cardAsArray) {
|
||||
$q->asArray();
|
||||
}
|
||||
|
||||
if ($this->returnCount) {
|
||||
return $q->count();
|
||||
} else {
|
||||
$cards = $q->all();
|
||||
}
|
||||
|
||||
if (is_array($cards)) {
|
||||
$this->excludePool = array_merge($this->excludePool, ArrayHelper::getColumn($cards, 'id'));
|
||||
}
|
||||
|
||||
return $cards;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $searchDepth
|
||||
* @return array|int
|
||||
*/
|
||||
public function search(int $searchDepth = 0)
|
||||
{
|
||||
$cards = $this->_search();
|
||||
$res = [];
|
||||
|
||||
if ($searchDepth === 1) {
|
||||
$res = $this->checkLevel(false)->useExcludePool()->_search();
|
||||
}
|
||||
|
||||
if ($searchDepth === 2) {
|
||||
$res = $this->checkLevel(false)->checkPosition(false)->useExcludePool()->_search();
|
||||
}
|
||||
|
||||
if ($searchDepth === 3) {
|
||||
$res = $this->checkLevel(false)->checkPosition(false)->setSkillsFullEntry(false)->useExcludePool()->_search();
|
||||
}
|
||||
|
||||
if ($this->returnCount) {
|
||||
if (is_array($res)) {
|
||||
return $cards;
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
return array_merge($cards, $res);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param bool $value
|
||||
* @return $this
|
||||
*/
|
||||
public function setSkillsFullEntry(bool $value): RequestService
|
||||
{
|
||||
$this->skillsFullEntry = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $value
|
||||
* @return $this
|
||||
*/
|
||||
public function asArray(bool $value = true): RequestService
|
||||
{
|
||||
$this->cardAsArray = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $value
|
||||
* @return $this
|
||||
*/
|
||||
public function checkLevel(bool $value = true): RequestService
|
||||
{
|
||||
$this->checkCardLevel = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $value
|
||||
* @return $this
|
||||
*/
|
||||
public function checkPosition(bool $value = true): RequestService
|
||||
{
|
||||
$this->checkCardPosition = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $value
|
||||
* @return $this
|
||||
*/
|
||||
public function useExcludePool(bool $value = true): RequestService
|
||||
{
|
||||
$this->useCardExcludePool = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $value
|
||||
* @return $this
|
||||
*/
|
||||
public function count(bool $value = true): RequestService
|
||||
{
|
||||
$this->returnCount = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public static function q()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $id
|
||||
* @return RequestService
|
||||
*/
|
||||
public static function run(int $id = null): RequestService
|
||||
{
|
||||
return new self($id);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user