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

namespace common\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 "{{%project_status}}".
 * 
 * 项目状态表
 *
 *@property smallint $status_id  (状态id)
 *@property string $name  (状态名称)
 *@property integer $sort  (排序  默认0)
 *@property integer $create_time  (创建时间)
 *@property integer $update_time  (更新时间)
 *@property string $ip  (创建人ip)
 *@property integer $user_id  (创建人id)
 *@property smallint $status  (状态 1：启用 0：禁用  99：逻辑删除   默认启用)
 *
 * @author xiaobai http://blog.u-bo.com
 * @since 0.1
 */
class ProjectStatus extends ActiveRecord
{   
    const STATUS_ENABLE = 1;//启用
    const STATUS_DISABLE = 0;//禁用
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%project_status}}';
    }

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

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'status_id' => 'ID',
            'name' => '项目状态名',
            'sort' => '排序',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
            'ip' => 'ip地址',
            'user_id' => '创建人id',
            'status' => '状态',
        ];
    }
    
    /**
     * 获取项目状态
     * 
     * @param type $map 查询条件
     * @param type $order 排序
     * @param type $field 查询字段 所有（*）
     * @return type  返回值
     */
    public static function getProjectStatus($map = [], $order = ['create_time' => SORT_DESC], $field = "*")
    {
        $projectstatusList = static::find()
                ->select($field)
                ->where($map)
                ->orderBy($order)
                ->asArray()
                ->all();
        return $projectstatusList; 
    }
    
    /**
     * 根据ID返回状态名称
     *
     * @param string $id
     * @return static|null
     */
    public static function findById($id)
    {
        return static::find()
                ->select('name')
                ->where(['status_id' => $id])
                ->scalar();
    }

    /**
     * 
     * @return type返回状态
     */
    public static function getStatusList()
    {
        return [
            self::STATUS_ENABLE => '启用',
            self::STATUS_DISABLE => '禁用',
        ];
    }
    
      /**
     * 公共搜索所需信息 
     */
    public function search()
    {
        return [
            'name' => '项目状态名称',
        ];
    }
    
    /**
     * 插入更新前置操作
     */
    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;
        }
    }
}
