<?php
/**
 * json数据库文件查询类
 * @Copyright (C) 2018 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
namespace Common\Vendor;
class JsonDB{
    private $closed;
    private $dat_path;
    private $is_zip =1;//是否为压缩存储
    /**
     * Description: 打开数据库
     * @param $pathName 数据文件的存放路径
     * @return mixed
     */
    public function __construct($pathName=''){
        if(empty($pathName))$pathName=ROOT_PATH.'/Public/data/area';
        if($this->is_zip && !function_exists('gzcompress'))$this->is_zip=0;
        if($this->is_zip==1)$pathName.='zip';
        $this->dat_path = $pathName.'.json';//value
        return true;
    }
    /**
     * Description: 查询一条记录
     * @param $key
     */
    public function select($param=''){
        $data=$this->readcontent();
        if(empty($param))return $data;
        if(!is_array($param))$param=array('id'=>$param);
        if(empty($param['_logic']))$param['_logic']='and';
        $param['_logic']=strtolower($param['_logic']);
        foreach($data as $k=> $v){
            $is_del=$param['_logic']=='and'?false:true;
            foreach($param as $key=> $val){
                if($key=='_logic')continue;
                if(!is_array($val)){$val=array('eq',$val);}
                if(!$this->checkdata($v[$key],$val)){
                    if($param['_logic']=='and')$is_del=true;
                }else{
                    if($param['_logic']!='and')$is_del=false;
                }
            }
            if($is_del)unset($data[$k]);        
        }
        $this->close();
        return array_values($data);
    }
    public function find($param='',$field='*'){
        $data=$this->select($param);
        if(empty($data))return false;
        $info=$data[0];
        if($field!='*' && (strpos($field,',')>0 || is_array($field))){
            if(!is_array($field))$field=explode(',',$field);
            foreach($info as $k=>$v){
                if(!in_array($k,$field))unset($info[$k]);
            }
            return $info;
        }
        return $field=='*'?$info:$info[$field];
    }
    public function checkdata($data,$exp){
        if(empty($data) || empty($exp))return false;
        $exp[0]=strtolower($exp[0]);
        $allow=array('eq','neq','like','in','notin','gt','lt','egt','elt');
        if(!in_array($exp[0],$allow))return false;
        switch($exp[0]){
            case "eq":
            return $data==$exp[1];
            break;
            case "neq":
            return $data!=$exp[1];
            break;
            case "like":
            return strpos('*'.$data,$exp[1])>0;
            break;
            case "in":
            if(!is_array($exp[1]))$exp[1]=explode(',',$exp[1]);
            return in_array($data,$exp[1]);
            break;
            case "notin":
            if(!is_array($exp[1]))$exp[1]=explode(',',$exp[1]);
            return !in_array($data,$exp[1]);
            break;
            case "gt":
            return $data>$exp[1];
            break;
            case "lt":
            return $data<$exp[1];
            break;
            case "egt":
            return $data>=$exp[1];
            break;
            case "elt":
            return $data<=$exp[1];
            break;
        }
        return false;
    }
    //读取数据库全部
    private function readcontent($path=''){
        if(empty($path))$path=$this->dat_path;
        static $cache = array();
        if(!empty($cache)){return $cache;}
        $cachename='JsonDB'.$path;
        $cachedata=S($cachename);if(!empty($cachedata))return $cachedata;        
        $contents=file_get_contents($path);if(empty($contents))return false;
        if($this->is_zip && function_exists('gzcompress')){$contents=gzuncompress($contents);}
        $cache=json_decode($contents,true);unset($contents);S($cachename,$cache,60*60*24);
        return $cache;
    }
    public function close(){}
}
?>