<?php

namespace app\models\entity;

/**
 * This is the model class for table "projects_locotech".
 *
 * @property int $id
 * @property int $action
 * @property int $company
 * @property int $status
 * @property string $loco_type
 * @property int $loco_number
 * @property string $depo
 * @property string $depo_service
 * @property string $created
 * @property string $tasks
 * @property int $projecttypes_id [smallint(11)]  внешний ключ к таблице projecttypes
 * @property int $type [smallint(5) unsigned]
 * @property bool $urgent [tinyint(3) unsigned]
 * @property int $object_id [int(10)]
 * @property string $uuid [varchar(255)]
 * @property int $kind [int(10)]
 * @property int $section_id [int(10) unsigned]
 * @property string $result_id [char(40)]
 * @property string $finished_time [datetime]  Время завершения проекта
 * @property int $finished_person [int(11)]  ID пользователя закрывшего проект
 *
 * @property Sections[] $sections
 */
class ProjectsLocotech extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'projects_locotech';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['action', 'company', 'status', 'loco_number'], 'integer'],
            [['created'], 'safe'],
            [['loco_type', 'depo', 'depo_service'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'action' => 'Action',
            'company' => 'Company',
            'status' => 'Status',
            'loco_type' => 'Loco Type',
            'loco_number' => 'Loco Number',
            'depo' => 'Depo',
            'depo_service' => 'Depo Service',
            'created' => 'Created',
            'tasks' => 'Tasks',
            'actions_id' => 'Actions ID',
        ];
    }

    public function getTask()
    {
        return $this->hasMany(Tasks::class, ['input_id' => 'id']);
    }

    public function getProjecttypes()
    {
        return $this->hasOne(Projecttypes::class, ['id' => 'action']);
    }

    public function getSections()
    {
        return $this->hasOne(Sections::class, ['id' => 'section_id']);
    }
    /**
     * Возвращает время первой принятой задачи проекта
     *
     * @return string|null
     */
    public function getAcceptedTime()
    {
        $task = Tasks::find()
            ->select('min(accepted_time) as accepted_time')
            ->where(['input_id' => $this->id])
            ->one()
        ;

        return $task->accepted_time;
    }

    /**
     * Возвращает время завершения проекта
     *
     * @return string|null
     */
    public function getFinishedTime()
    {
        if ($this->finished_time) return $this->finished_time;

        if ($this->status == 5) {
            $task = Tasks::find()
                ->select('max(finished_time) as finished_time')
                ->where(['input_id' => $this->id])
                ->one()
            ;

            return $task->finished_time;
        }

        return null;
    }
}