<?php
/**
  * @method 汽车之家车型库
  * @author soul
  * @copyright 2016/3/31
  */
class CarBase
{
	public $CarDb = null;
	public function __construct($CarDb = null)
	{
		$this->CarDb = $CarDb==null ?  new LibDb (mvc::$cfg['DB']['CAR']): $CarDb;
	}
	
	/**
	  * @method 查询表信息
	  * @param  $Table 表明 如：yp_product 或者 yp_product as a join yp_number as b on a.pid=b.pid
	  * @param  $FieldArr 需要查询的字段名称数组
	  * @param  $Where 查询条件
	  * @param  $OneRow true|false
	  * @author soul
	  * @copyright 2016/3/30
	  * @return array(3) { ["status"]=> bool(true) ["data"]=> $OneRow=false 二维数组 ; $OneRow=true 一维数组["code"]=> int(0) } 
	  */
	public function Get($Table, $FieldArr, $Where = '', $OneRow = false)
	{
		if(!empty($FieldArr) && is_array($FieldArr))
		{
			$Sql = sprintf("SELECT %s FROM %s WHERE 1=1 %s", implode(',', $FieldArr), LibFc::Escape($Table), $Where);
			//echo $Sql, '<br>';
			$ResFlag = $this->CarDb->Query($Sql);
			if($ResFlag)
			{
				$Arr = array();
				while($Row = $this->CarDb->Fetch())
				{
					if($OneRow)
					{
						$Arr = $Row;
						break;
					}
					else
					{
						$Arr[] = $Row;
					}
				}
				return LibFc::ReturnData(true, $Arr);
			}
			return LibFc::ReturnData(false, $Sql.'查询SQL语句执行失败。');
		}
		return LibFc::ReturnData(false, $Table.'查询提供的参数不正确。');
	}


	/**
	  * @method 获取指定条件下的某字段唯一值
	  * @param  $table 表明 如：yp_product 或者 yp_product as a join yp_number as b on a.pid=b.pid
	  * @param  $field 字段名称
	  * @param  $where 查询条件
	  * @author soul
	  * @copyright 2016/3/30
	  * @return 
			成功 ["status"=>true, "data"=> [111,222,....]] 
			失败 ["status"=>false, "data"=> 错误信息]  
	  */
	public function getFieldUniVal($table, $field, $where = '')
	{
		if(!empty($table) && !empty($table))
		{
			$sql = sprintf("SELECT DISTINCT %s FROM %s WHERE 1=1 %s", $field, $table, $where);
			$resFlag = $this->CarDb->Query($sql);
			if($resFlag)
			{
				$arr = array();
				while($row = $this->CarDb->Fetch())
				{
					$arr[] = $row[$field];
				}
				return LibFc::ReturnData(true, $arr);
			}
			return LibFc::ReturnData(false, $table.'查询SQL语句执行失败。');
		}
		return LibFc::ReturnData(false, $table.'查询提供的参数不正确。');
	}


	/**
	  * @method 添加记录
	  * @param  $Table 表明 如：yp_product 或者 yp_product as a join yp_number as b on a.pid=b.pid
	  * @param  $DataArr 基本信息字段=>值 数组
	  * @author soul
	  * @copyright 2016/3/30
	  * @return array(3) { ["status"]=> bool(true) ["data"]=> ID ["code"]=> int(0) } 
	  */
	public function Add($Table, $DataArr)
	{
		if(empty($Table) || !is_array($DataArr) || empty($DataArr))
		{
			return LibFc::ReturnData(false, $Table.'添加记录提供的参数不正确。');
		}
		
		$FiledArr = array();
		$ValArr = array();
		foreach($DataArr as $K=>$V)
		{
			$FiledArr[] = LibFc::Escape($K);
			$ValArr[] = sprintf("'%s'", LibFc::Escape($V, true));
		}
		$AddSql = sprintf("INSERT INTO %s (%s) VALUES(%s)", LibFc::Escape($Table), implode(',', $FiledArr), implode(',', $ValArr));
		$this->CarDb->Begin();
		$ResFlag = $this->CarDb->Query($AddSql);
		$Rows = $this->CarDb->RowCount();
		if($ResFlag && $Rows > 0)
		{
			$Id = $this->CarDb->InsertID();
			$this->CarDb->End();
			return LibFc::ReturnData(true, $Id);
		}
		return LibFc::ReturnData(false, $Table.'添加记录SQL语句执行失败。');
	}

	/**
	  * @method 根据条件修改记录
	  * @param  $Table 表明 如：yp_product 或者 yp_product as a join yp_number as b on a.pid=b.pid
	  * @param  $DataArr 基本信息字段=>值 数组
	  * @param  $Where 修改条件
	  * @author soul
	  * @copyright 2016/3/30
	  * @return array(3) { ["status"]=> bool(true) ["data"]=> 影响行数  ["code"]=> int(0) } 
	  */
	public function Set($Table, $DataArr, $Where)
	{
		if(empty($Table) || !is_array($DataArr) || empty($DataArr))
		{
			return LibFc::ReturnData(false, $Table.'修改记录提供的参数不正确。');
		}
		$ValArr = array();
		foreach($DataArr as $K=>$V)
		{
			$ValArr[] = sprintf("%s='%s'", LibFc::Escape($K), LibFc::Escape($V, true));
		}
		$SetSql = sprintf("UPDATE %s SET %s WHERE 1 %s ", LibFc::Escape($Table), implode(',', $ValArr), $Where);
		$this->CarDb->Begin();
		$ResFlag = $this->CarDb->Query($SetSql);
		if($ResFlag)
		{
			$Rows = $this->CarDb->RowCount();
			$this->CarDb->End();
			return LibFc::ReturnData(true, $Rows);
		}
		return LibFc::ReturnData(false, $Table.'修改记录SQL语句执行失败。');
	}

	/**
	  * @method 根据条件删除记录
	  * @param  $Table 表明 如：yp_product 或者 yp_product as a join yp_number as b on a.pid=b.pid
	  * @param  $Where 修改条件
	  * @author soul
	  * @copyright 2016/3/30
	  * @return array(3) { ["status"]=> bool(true) ["data"]=> 影响行数  ["code"]=> int(0) } 
	  */
	public function Del($Table, $Where)
	{
		if(empty($Table) || empty($Where))
		{
			return LibFc::ReturnData(false, $Table.'删除记录提供的参数不正确。');
		}
		$DelSql = sprintf("DELETE FROM %s WHERE 1=1 %s",  LibFc::Escape($Table), $Where );
		$this->CarDb->Begin();
		$ResFlag = $this->CarDb->Query($DelSql);
		if($ResFlag)
		{
			$Rows = $this->CarDb->RowCount();
			$this->CarDb->End();
			return LibFc::ReturnData(true, $Rows);
		}
		return LibFc::ReturnData(false,  $Table.'删除记录SQL语句执行失败。');
	}
}