<?php

namespace catchAdmin\system\model;

use catcher\base\CatchModel as Model;
use think\facade\Db;
use think\facade\Cache;
use catchAdmin\system\model\SysDictType;

class SysDictData extends Model
{
    // 表名
    public $name = 'sys_dict_data';
    // 数据库字段映射
    public $field = array(
        'id',
        // 字典类型id
        'type_id',
        // 值
        'value',
        // 编码
        'code',
        // 排序
        'sort',
        // 备注
        'remark',
        // 状态(字典 0正常 1停用 2删除)
        'status',
        // 创建人ID
        'creator_id',
        // 创建时间
        'created_at',
        // 更新时间
        'updated_at',
        // 软删除
        'deleted_at',
    );
    public function getList()
	{
		return $this->catchSearch()
                    ->order('sort', 'asc')
                    ->paginate();
    }

    /**
     * 获取字典值
     * @param String type_code 字典类型code 
     * @param String code 字典值 code 
     */
    public function getValueByCode($type_code, $code)
    {
        if (!isset($type_code) || !isset($code)) {
            return false;
        }
        // 有缓存,使用缓存
        if (Cache::has("dict_data_{$type_code}_{$code}")) {
            return Cache::get("dict_data_{$type_code}_{$code}");
        }
        // 查出字典类型id
        // $type_id = Db::table("sys_dict_type")->where('code', $type_code)->value('id');
        $type_id = (new SysDictType)->getTypeIdByCode($type_code);

        return $this->where('type_id', $type_id)
                    ->where('code', $code)
                    ->cache("dict_data_{$type_code}_{$code}", 60) //缓存
                    ->value('value');

    }
    //根据类型中的唯一编码获取所有类型
    public function getTypesByCode($type_code)
    {
        //获取类型id
        // $type_id = Db::table("sys_dict_type")->where('code',$type_code)->value('id');
        $type_id = (new SysDictType)->getTypeIdByCode($type_code);
        // 获取该类型下的所有字典
        $type_data = $this->where('type_id', $type_id)->order('sort', 'asc')->column('type_id,value,code,remark,sort');
        $options = [];
        foreach ($type_data as $key => $value) {
            $option = array(
                'value'=>$value['code'],
                'text'=>$value['value'],
                'remark'=>$value['remark']
            );
            array_push($options,$option);
         }
        return $options;
    }
    //根据类型中的唯一编码获取所有类型
    public function getTypesByCodeWithRemark($type_code,$remark)
    {
        //获取类型id
        // $type_id = Db::table("sys_dict_type")->where('code',$type_code)->value('id');
        $type_id = (new SysDictType)->getTypeIdByCode($type_code);
        // 获取该类型下的所有字典
        $type_data = $this->where('type_id', $type_id)->whereIn('remark', $remark)->order('sort', 'asc')->column('type_id,value,code,remark,sort');
        
        $options = [];
        foreach ($type_data as $key => $value) {
            $option = array(
                'value'=>(int)$value['code'],
                'text'=>$value['value'],
                'remark'=>$value['remark']
            );
            array_push($options,$option);
         }
        return $options;
    }
    /**
     * 获取字典备注
     * @param String type_code 字典类型code 
     * @param String code 字典值 code 
     */
    public function getRemarkByCode($type_code, $value)
    {
        if (!isset($type_code) || !isset($value)) {
            return false;
        }
        $type_id = Db::table("sys_dict_type")->where('code', $type_code)->value('id');
        return $this->where('type_id', $type_id)
                    ->where('code', $value)
                    ->cache(true, 60)
                    ->value('remark');
    }
    /**
     * 词汇查询
     *
     * @time 2020年06月17日
     * @param $query
     * @param $value
     * @param $data
     * @return mixed
     */
    public function searchTypeIdAttr($query, $value, $data)
    {
        return $query->where('type_id', $value);
    }
    public function searchValueAttr($query, $value, $data)
    {
        return $query->whereLike('value', $value);
    }
    public function searchCodeAttr($query, $value, $data)
    {
        return $query->whereLike('code', $value);
    }
}