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

namespace home\modules\contract\models;

use Yii;

/**
 * This is the model class for table "qc_contract_status".
 * 
 * 合同状态模型
 *
 * @property integer $status_id  (合同状态ID)
 * @property string $name  (合同状态名称)
 * @property integer $sort  (排序)
 * @property smallint $status  (状态：0 禁用 1开启 99逻辑删除 默认开启)
 * @property integer $user_id  (创建人，关联表qc_user)
 * @property integer $create_time  (创建时间)
 * @property integer $update_time  (更新时间)
 * @property string $ip  (创建人IP)
 * @property tinyint $active_state  (0:不为活动状态   1：为活动状态)
 *
 * @author zealxd http://blog.u-bo.com
 * @since 0.1
 */
class Status extends \yii\db\ActiveRecord
{
    
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%contract_status}}';
    }

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

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'status_id' => 'ID',
            'name' => '名称',
            'sort' => '排序',
            'status' => '状态',
            'user_id' => '创建人ID',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
            'ip' => '创建人IP',
            'active_state' => '活动状态'
        ];
    }

    /**
     * 插入更新前置操作
     */
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            //判断是否是插入操作
            if ($this->isNewRecord) {
                $this->user_id =  Yii::$app->user->identity->user_id;
                $this->ip = Yii::$app->getRequest()->getUserIP();
                $this->create_time = time();
            } else {
                $this->update_time = time();
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * 查询所有的合同状态列表
     *
     * @return array   $statusLists 返回查询结果
     */
    public static function getStatus($map = [], $field = "*", $order = ['sort' => SORT_DESC, 'status_id' => SORT_DESC] )
    {
        $map['status'] = 1;
        $statusLists = static::find()
                        ->select($field)
                        ->where($map)
                        ->orderBy($order)
                        ->asArray()
                        ->all();
        return $statusLists;
    }
    
}
