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

namespace qcbase\models;

use Yii;

/**
 * This is the model class for table "{{%config}}".
 * 
 * 配置信息表
 *
 * @property integer $config_id  (ID)
 * @property string $name  (名字)
 * @property string $title  (配置名)
 * @property text $content  (配置内容 json格式数据)
 * 
 * @author legendjw http://blog.u-bo.com
 * @since 0.1
 */
class Config extends \yii\db\ActiveRecord
{
    //基本配置
    public $web_name;
    public $web_url;
    public $web_logo;
    public $wap_logo;
    public $web_keywords;
    public $web_description;
    public $web_info;
    public $web_record;

    //系统配置
    public $page_count;
    public $sys_upload_type;
    public $sys_upload_size;
    public $mail_host;
    public $mail_post;
    public $mail_user;
    public $mail_pwd;
    public $mail_subject;
    public $mail_content;

    //任务配置
    public $task_lead_permission;//负责任务的权限
    public $task_join_permission;//参与任务的权限
    public $task_follow_permission;//关注任务的权限

    public $project_business_contact_dept; //商务联系人选择部门

    public $reimbursement_verify_select; //报销人是否可以选择审核人
    public $reimbursement_user_select; //默认审核人选择为当前部门的领导/部门审核人
    public $reimbursement_verify_show; //报销审核人角色列表页默认显示何种审核状态的报销
    public $reimbursement_reim_show; //报销人角色列表页默认显示何种审核状态的报销
    public $reimbursement_verify_order; //报销审核人角色列表页排序规则
    public $reimbursement_reim_order; //报销人角色列表页排序规则

    public $application_verify_select; //费用申请人是否可以选择审核人
    public $application_user_select; //默认审核人选择为当前部门的领导/部门审核人
    public $application_verify_show; //申请单审核人角色列表页默认显示何种审核状态的申请单
    public $application_reim_show; //报销人角色列表页默认显示何种审核状态的申请单
    public $application_verify_order; //申请单审核人角色列表页排序规则
    public $application_reim_order; //报销人角色列表页排序规则

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

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['web_name', 'web_logo', 'wap_logo', 'web_url', 'web_keywords', 'web_description', 'web_info', 'page_count', 'sys_upload_type', 'sys_upload_size', 'mail_host', 'mail_post', 'mail_user', 'mail_pwd'], 'required'],
            [['web_record', 'reimbursement_user_select', 'application_user_select'], 'string'],
            [['page_count', 'sys_upload_size', 'mail_post', 'reimbursement_verify_select', 'application_verify_select'], 'integer', 'min' => 0],
            [['mail_subject', 'mail_content', 'task_lead_permission', 'task_join_permission', 'task_follow_permission', 'project_business_contact_dept', 'reimbursement_verify_show', 'reimbursement_reim_show', 'reimbursement_verify_order', 'reimbursement_reim_order', 'application_verify_show', 'application_reim_show', 'application_verify_order', 'application_reim_order' ], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'config_id' => 'Config',
            'title' => '配置名称',
            'name' => '配置文件名称',
            'content' => '配置内容',

            'web_name' => '网站名称',
            'web_url' => '网站地址',
            'web_keywords' => '默认关键字（SEO）',
            'web_description' => '站点描述（SEO）',
            'web_info' => '底部信息',
            'web_record' => '网站备案号',

            'page_count' => '默认分页数量',
            'sys_upload_type' => '允许上传的文件类型(用|分隔)',
            'sys_upload_size' => '允许上传文件的最大值(KB)',
            'mail_host' => '邮件服务器地址',
            'mail_post' => '邮件发送端口',
            'mail_user' => '发送邮箱账号',
            'mail_pwd' => '发送邮箱密码',
            'mail_subject' => '邮件主题',
            'mail_content' => '邮件内容',

            'task_lead_permission' => '负责任务所拥有的权限',
            'task_join_permission' => '参与任务所拥有的权限',
            'task_follow_permission' => '关注任务所拥有的权限',

            'project_business_contact_dept' => '商务联系人选择部门',

            'reimbursement_verify_select' => '是否可以选择审核人',
            'reimbursement_user_select' => '默认审核人选择为当前部门的',
            'reimbursement_verify_show' => '列表页默认显示何种审核状态的报销',
            'reimbursement_reim_show' => '列表页默认显示何种审核状态的报销',
            'reimbursement_verify_order' => '列表页排序规则',
            'reimbursement_reim_order' => '列表页排序规则',

            'application_verify_select' => '是否可以选择审核人',
            'application_user_select' => '默认审核人选择为当前部门的',
            'application_verify_show' => '列表页默认显示何种审核状态的申请单',
            'application_reim_show' => '列表页默认显示何种审核状态的申请单',
            'application_verify_order' => '列表页排序规则',
            'application_reim_order' => '列表页排序规则',
        ];
    }

    /**
     * 获取配置
     * 
     * @param string $name 配置名称
     * @return type 获取配置info
     */
    public static function getConfig($name)
    {
        $where = ['name' => $name];
        $config = static::find()
            ->where($where)
            ->asArray()
            ->one();

        $data = json_decode($config['content'], true);

        return $data;
    }

    /**
     * 设置缓存
     */
    public static function setCache()
    {
        $cache = Yii::$app->cache;
        $configs = self::find()->asArray()->all();
        foreach ($configs as $c) {
            $config = json_decode($c['content'], true);
            $cache->set($c['name'] . '_config', $config);
        }
    }

    /**
     * 更新配置缓存
     *
     * @author HuLimin
     */
    public static function updateConfigCache()
    {
        //基本配置
        $basic_config = self::getInfo();
        //系统配置
        $system_config = self::getInfo($map = ['name' => 'system_config']);
        Yii::$app->cache->set('basic_config', $basic_config);
        Yii::$app->cache->set('system_config', $basic_config);
    }

}
