<?php
$GLOBALS['mainDbReadLink'] = null; //全局 读库DB变量,只连接一次数据库
$GLOBALS['mainDbWriteLink'] = null; //全局 写库DB变量,只连接一次数据库
/**
  * @class 模块基类
  */
class mod
{
	public $dbRead = null;
	public $dbWrite = null;
	public $e = []; //元对象
	public function __construct($e=[])
	{
		if(!$GLOBALS['mainDbReadLink']) {
			$GLOBALS['mainDbReadLink'] = new LibDb(mvc::$cfg['db']['main_read']);
		}
		if(!$GLOBALS['mainDbWriteLink']) {
			if(!isset(mvc::$cfg['db']['main_write']) || md5(mvc::$cfg['db']['main_write']) == md5(mvc::$cfg['db']['main_read'])){
				//如果写库没有配置，或者配置与读库相同，那么使用同一个连接
				$GLOBALS['mainDbWriteLink'] = $GLOBALS['mainDbReadLink'];
			}else{
				$GLOBALS['mainDbWriteLink'] = new LibDb(mvc::$cfg['db']['main_write']);
			}
		}
		$this->dbRead = $GLOBALS['mainDbReadLink'];
		$this->dbWrite = $GLOBALS['mainDbWriteLink'];
		print_r($GLOBALS);
		$this->e = $e;
	}
	/**
	 * 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){
		$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 getOne($field,$where){
		$sql = sprintf("SELECT %s FROM (%s) T_ WHERE %s LIMIT 0,1", $field, $this->e['view'],$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->One($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);
		}
	}
	/**
	 * 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){
		if($where!=''){
			return $this->dbWrite->Update($this->e['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){
		$result = 0;
		if($this->dbWrite->Insert($this->e['table'],$data)){
			$result = $this->dbWrite->InsertID();
		}
		return $result;
	}
	/**
	 * Method 添加或者更新一行数据(存在就更新)
	 * @param array $data 
	 * @param string $where 查询sql条件,不带带 where 关键字
	 * @return array 数据集
	 */
	public function replaceRow($data){
		return $this->dbWrite->Replace($this->e['table'],$data);
	}
	/**
	 * Method 检查POST提交的数据
	 * @param [array] $notArr 忽略检查的字段
	 * @return array 数据集
	 */
	public function checkValueForm($params,$notArr=[]){
		if($params){
			$data = [];
			//只提取 f_ 开头的内容
			foreach($params as $key=>$value){
				$value = substr($value,0,$this->e['fields'][$key]['maxlength']);
				$data[$key] = trim($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']]==''))
						return ['error'=> sprintf('%s(%s) 是必须项 (is must).',$field['name'],$field['field'])];
				}
			}
			return ['data'=>$data];
		}
		return ['error'=> '没有数据提交 (No data submitted)'];
	}

}

