<?php
/**
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司 
 * @license   http://www.u-bo.com/license/
 */

namespace qcbase\models;

use Yii;
use yii\db\ActiveRecord;
use yii\db\Query as query;
use yii\helpers\Url;
use common\components\Tool;
use common\models\User;

/**
 * This is the model class for table "{{%task_status}}".
 * 
 * 任务状态表 ({{%task_status}})
 *
 * @property smallint $status_id  (ID)
 * @property string $name  (状态名称)
 * @property string $remark  (备注)
 * @property smallint $sort  (排序)
 * @property integer $create_time  (创建时间)
 * @property smallint $status  (状态)
 * @property integer $update_time  (更新时间)
 * @property integer $user_id  (创建人ID)
 * @property string $ip  (创建人IP)
 * @property tinyint $list_show  (是否在列表页显示)
 *
 * @author xiaobai http://blog.u-bo.com
 * @since 0.1
 */
class TaskStatus extends ActiveRecord
{   
    public $taskstatus = [];//状态
    
    const STATUS_ENABLE = 1;//启用
    const STATUS_DISABLE = 0;//禁用
    const LIST_SHOW = 1;//显示
    const LIST_HIDDEN = 0;//隐藏

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%task_status}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['sort', 'status', 'list_show'], 'integer'],
            [['name', 'remark', 'sort', 'status', 'list_show'],'required'],
            [['name'], 'string', 'max' => 50],
            [['remark'], 'string', 'max' => 255],
            [['ip'], 'string', 'max' => 15],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'status_id' =>'ID',
            'name' => '任务状态名',
            'remark' => '任务备注',
            'sort' => '排序',
            'create_time' => '创建时间',
            'status' => '状态',
            'update_time' => '更新时间',
            'user_id' => '创建人',
            'ip' => 'ip地址',
            'list_show' => '此状态任务是否在列表显示',
        ];
    }

    /**
     * 获取任务状态
     * 
     * @param type $map 查询条件
     * @param type $order 排序
     * @param type $field 查询字段 所有（*）
     * @return type  返回值
     */
    public static function getTaskStatus($map = [], $order = ['create_time' => SORT_DESC], $field = "*")
    {
        $status = static::find()
            ->select($field)
            ->where($map)
            ->orderBy($order)
            ->asArray()
            ->all();
        return $status; 
    }

	/**
	 * 获取下拉列表
	 * 
	 * @return type
	 */
	public static function getDropDownList()
	{
		$rows = self::find()
				->select('status_id, name')
				->orderBy(['sort' => SORT_DESC, 'status_id' => SORT_DESC])
				->asArray()
				->all();
		foreach ($rows as $row) {
			$list[$row['status_id']] = $row['name'];
		}
		return $list;
	}
    
    /**
     * 获取状态数组
     */
    public static function getStatusList()
    {
        return [
            self::STATUS_ENABLE => '启用',
            self::STATUS_DISABLE => '禁用',
        ];
    }

    /**
     * 获取显示数组
     */
    public static function getListShowList()
    {
        return [
            self::LIST_SHOW => '显示',
            self::LIST_HIDDEN => '隐藏',
        ];
    }
    
    /**
     * 公共搜索所需信息 
     */
    public function search()
    {
        return [
            'name' => '任务状态名称',
        ];
    }

    /**
     * 获取在列表显示的状态
     */
    public static function getShowListStatus()
    {
        $status = self::find()
            ->select('status_id')
            ->where(['list_show' => self::LIST_SHOW])
            ->column();
        return $status;
    }
    
    /**
     * 插入更新前置操作
     */
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            //判断是否是插入操作
            if ($this->isNewRecord) {
                $this->user_id =  Yii::$app->user->identity->user_id;
                $this->create_time = time();
                $this->ip = Yii::$app->getRequest()->getUserIP();
            }
            $this->update_time = time();
            $this->ip = Yii::$app->getRequest()->getUserIP();
            return true;
        } else {
            return false;
        }
    }
}
