<?php
$GLOBALS['mainDbReadLink'] = null; //全局 读库DB变量,只连接一次数据库
$GLOBALS['mainDbWriteLink'] = null; //全局 写库DB变量,只连接一次数据库
/**
  * @class 模块基类
  */
class mod
{
	public $dbRead = null;
	public $dbWrite = null;
	public $e = []; //元对象
	public function __construct($e=[],$readConfig=null,$writeConfig=null)
	{
		if(!$readConfig) $readConfig = mvc::$cfg['db']['main_read'];
		if(!$writeConfig) $writeConfig = isset(mvc::$cfg['db']['main_write']) ? mvc::$cfg['db']['main_write'] : $readConfig;
		if(!$GLOBALS['mainDbReadLink']) {
			$GLOBALS['mainDbReadLink'] = new LibDb($readConfig);
		}
		if(!$GLOBALS['mainDbWriteLink']) {
			if(md5(serialize($writeConfig)) == md5(serialize($readConfig))){
				//写库配置与读库相同，那么使用同一个连接
				$GLOBALS['mainDbWriteLink'] = $GLOBALS['mainDbReadLink'];
			}else{
				$GLOBALS['mainDbWriteLink'] = new LibDb($writeConfig);
			}
		}
		$this->dbRead = $GLOBALS['mainDbReadLink'];
		if($_SESSION['user']['isadmin']){ //简单的写操作权限控制
			$this->dbWrite = $GLOBALS['mainDbWriteLink'];
		}
		$this->e = $e;
	}

	/** 
	  * @method 开启事务
	  */
	public function begin() 
	{
		return $this->dbWrite->Begin();
	}
	/** 
	  * @method 结束事件
	  */
	public function end() 
	{
		return $this->dbWrite->End();
	}

	/**
	 * Method 取多行数据
	 * @param string $table 表
	 * @param string $fields 字段
	 * @param string $where 查询sql条件,不带带 where 关键字
	 * @return array 数据集
	 */
	public function getTable($fields,$where){
		if($where=='') $where = 1;
		$sql = sprintf("SELECT %s FROM (%s) T__ WHERE %s", $fields, $this->e['view'],$where);
		return $this->dbRead->Table($sql);
	}
	/**
	 * Method 取多行数据,指定表
	 * @param string $table 表
	 * @param string $fields 字段
	 * @param string $where 查询sql条件,不带带 where 关键字
	 * @return array 数据集
	 */
	public function getTableSelectTable($table,$fields,$where){
		if($where=='') $where = 1;
		$sql = sprintf("SELECT %s FROM %s WHERE %s", $fields, $table,$where);
		return $this->dbRead->Table($sql);
	}
	/**
	 * Method 通过主键,取一行数据
	 * @param string $fields 查询sql条件
	 * @param string $key 主键的值
	 * @return array 数据集
	 */
	public function getRowByKey($fields,$key){
		return $this->getRow($fields, $this->e['key']."='".$key."'");
	}
	/**
	 * Method 通过主键,取单元格数据
	 * @param string $field 查询sql条件
	 * @param string $key 主键的值
	 * @return array 数据集
	 */
	public function getOneByKey($field,$key){
		return $this->getOne($field, $this->e['key']."='".$key."'");
	}
	/**
	 * Method 取一行数据
	 * @param string $fields 字段
	 * @param string $where 查询sql条件,不带带 where 关键字
	 * @return array 数据集
	 */
	public function getRow($fields,$where=1){
		$sql = sprintf("SELECT %s FROM (%s) T__ WHERE %s LIMIT 0,1", $fields, $this->e['view'],$where);
		return $this->dbRead->Row($sql);
	}
	/**
	 * Method 取一行数据,指定表
	 * @param string $fields 字段
	 * @param string $where 查询sql条件,不带带 where 关键字
	 * @return array 数据集
	 */
	public function getRowSelectTable($table,$fields,$where=1){
		$sql = sprintf("SELECT %s FROM %s WHERE %s LIMIT 0,1", $fields, $table,$where);
		return $this->dbRead->Row($sql);
	}
	/**
	 * Method 取单元格数据
	 * @param string $fields 字段
	 * @param string $where 查询sql条件,不带带 where 关键字
	 * @return array 数据集
	 */
	public function getOne($field,$where){
		if($where=='') $where = 1;
		$sql = sprintf("SELECT %s FROM (%s) T__ WHERE %s LIMIT 0,1", $field, $this->e['view'],$where);
		return $this->dbRead->One($sql);
	}
	public function getOneSelectTable($table,$field,$where){
		if($where=='') $where = 1;
		$sql = sprintf("SELECT %s FROM %s WHERE %s LIMIT 0,1", $field, $table,$where);
		return $this->dbRead->One($sql);
	}
	/**
	 * Method 是否存在
	 * @param string $fields 字段
	 * @param string $where 查询sql条件,不带带 where 关键字
	 * @return array 数据集
	 */
	public function exists($where){
		$sql = sprintf("SELECT 1 FROM (%s) T__ WHERE %s LIMIT 0,1", $this->e['view'], $where);
		$result = $this->dbRead->One($sql);
		return !empty($result);
	}
	/**
	 * Method 删除记录
	 * @param string $where 查询sql条件,不带带 where 关键字
	 * @return array 数据集
	 */
	public function deleteRows($where)
	{
		if ($where != '') {
			$sql = sprintf("DELETE FROM %s WHERE %s", $this->e['table'], $where);
			return $this->dbWrite->Query($sql);
		}
	}
	/**
	 * Method 执行SQL
	 */
	public function doQuery($sql)
	{
		if($sql != '') {
			return $this->dbWrite->Query($sql);
		}
	}
	/**
	 * Method 删除记录
	 * @param string $key 主键的值
	 * @param string $where 查询sql条件,不带带 where 关键字
	 * @return array 数据集
	 */
	public function deleteRowByKey($key){
		if($key!=''){
			$sql = sprintf("DELETE FROM %s WHERE %s LIMIT 1", $this->e['table'],$this->e['key']."='".$key."'");
			return $this->dbWrite->Query($sql);
		}
	}
	public function deleteRowsSelectTable($table,$where){
		if($where!=''){
			$sql = sprintf("DELETE FROM %s WHERE %s", $table,$where);
			return $this->dbWrite->Query($sql);
		}
	}
	/**
	 * Method 修改一行数据
	 * @param array $data 
	 * @param string $key 主键的值
	 * @return array 数据集
	 */
	public function setRowByKey($data,$key){
		if($key!=''){
			return $this->setRow($data, $this->e['key']."='".$key."'");
		}
	}
	/**
	 * Method 修改单元格数据
	 * @param string $field 字段
	 * @param string $value 值
	 * @param string $key 主键的值
	 * @return array 数据集
	 */
	public function setOneByKey($field,$value,$key){
		return $this->setRowByKey([$field=>$value],$key);
	}
	/**
	 * Method 修改一行数据
	 * @param array $data 
	 * @param string $where 查询sql条件,不带带 where 关键字
	 * @return array 数据集
	 */
	public function setRow($data,$where){
		return $this->setRowSelectTable($this->e['table'],$data,$where);
	}
	/**
	 * Method 修改一行数据
	 * @param array $data 
	 * @param string $where 查询sql条件,不带带 where 关键字
	 * @return array 数据集
	 */
	public function setRowSelectTable($table,$data,$where){
		if($where!=''){
			$data['update_time'] = LibTimer::nowDateTime();
			return $this->dbWrite->Update($table,$data,$where);
		}
	}
	/**
	 * Method 修改单元格数据
	 * @param string $field 字段
	 * @param string $value 值
	 * @param string $where 查询sql条件,不带带 where 关键字
	 * @return array 数据集
	 */
	public function setOne($field,$value,$where){
		return $this->setRow([$field=>$value],$where);
	}
	/**
	 * Method 添加一行数据
	 * @param array $data 
	 * @param string $where 查询sql条件,不带带 where 关键字
	 * @return array 数据集
	 */
	public function addRow($data){
		return $this->addRowSelectTable($this->e['table'],$data);
	}
	/**
	 * Method 添加一行数据
	 * @param array $data 
	 * @param string $where 查询sql条件,不带带 where 关键字
	 * @return array 数据集
	 */
	public function addRowSelectTable($table,$data){
		$result = 0;
		if(!is_array($data[0])){
			$data['create_time'] = LibTimer::nowDateTime();
			$data['update_time'] = $data['create_time'];
		}else{
			foreach($data as $k=>&$row){
				$row['create_time'] = LibTimer::nowDateTime();
				$row['update_time'] = $row['create_time'];
			}
		}
		if($this->dbWrite->Insert($table,$data)){
			$result = $this->dbWrite->InsertID();
		}
		return $result;
	}
	/**
	 * Method 添加或者更新一行数据(存在就更新)
	 * @param array $data 
	 * @param string $where 查询sql条件,不带带 where 关键字
	 * @return array 数据集
	 */
	public function replaceRow($data,$where){
		if($this->getRow('1',$where)){
			$this->setRow($data,$where);
		}else{
			$this->addRow($data);
		}
	}
	/**
	 * Method 添加或者更新一行数据(存在就更新)
	 * @param array $data 
	 * @param string $where 查询sql条件,不带带 where 关键字
	 * @return array 数据集
	 */
	public function replaceRowSelectTable($table,$data,$where){
		if($this->getRowSelectTable($table,'1',$where)){
			return $this->setRowSelectTable($table,$data,$where);
		}else{
			return $this->addRowSelectTable($table,$data);
		}
	}
	/**
	 * Method 检查POST提交的数据
	 * @param [array] $notArr 忽略检查的字段
	 * @return array 数据集
	 */
	public function checkValueForm($params,$notArr=[]){
		$result = [];
		$result['error'] = '';
		$data = [];
		if($params){
			//只提取 f_ 开头的内容
			foreach($params as $key=>$value){
				$isArray = is_array($value);
				$value = ($isArray) ? $value : trim(substr($value,0,$this->e['fields'][$key]['maxlength']));
				$data[$key] = $value; 
			}
			//检查必须项
			foreach($this->e['fields'] as $key=>$field){
				if(!in_array($field['field'],$notArr)){
					if(isset($field['editor']['required']) && $field['editor']['required'] 
						&& (!isset($data[$field['field']]) || ($data[$field['field']]=='' || (is_array($data[$field['field']]) && count($data[$field['field']])<1) ))
					){ $result['error'] = sprintf('[%s](%s) 是必须项.',$field['name'],$field['field']); break;}
				}
			}
		}else{
			$result['error'] = '没有数据提交';
		}
		$result['data'] = $data;
		return $result;
	}

}

