<?php
/**
  * @method 宜配网非产品库表基本操作
  * @author soul
  * @copyright 2016/3/31
  */
class MainBase
{
	public $MainDb = null;
	public function __construct($MainDb = null)
	{
		$this->MainDb = $MainDb==null ?  new LibDb (mvc::$cfg['DB']['MAIN']): $MainDb;
	}
	
	/**
	  * @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->MainDb->Query($Sql);
			if($ResFlag)
			{
				$Arr = array();
				while($Row = $this->MainDb->Fetch())
				{
					if($OneRow)
					{
						$Arr = $Row;
						break;
					}
					else
					{
						$Arr[] = $Row;
					}
				}
				return LibFc::ReturnData(true, $Arr);
			}
			return LibFc::ReturnData(false, $Table.'查询SQL语句执行失败。');
		}
		return LibFc::ReturnData(false, $Table.'查询提供的参数不正确。');
	}

	/**
	  * @method 查询表信息
	  * @param  $Sql 
	  * @param  $OneRow true|false
	  * @author soul
	  * @copyright 2016/3/30
	  * @return array(3) { ["status"]=> bool(true) ["data"]=> $OneRow=true 二维数组 ; $OneRow=true 一维数组["code"]=> int(0) } 
	  */
	public function GetDataBySql($Sql, $OneRow = false)
	{
		if(!empty($Sql))
		{
			$ResFlag = $this->MainDb->Query($Sql);
			if($ResFlag)
			{
				$Arr = array();
				while($Row = $this->MainDb->Fetch())
				{
					if($OneRow)
					{
						$Arr = $Row;
						break;
					}
					else
					{
						$Arr[] = $Row;
					}
				}
				return LibFc::ReturnData(true, $Arr);
			}
			return LibFc::ReturnData(false, $Sql.'查询SQL语句执行失败。');
		}
		return LibFc::ReturnData(false, $Sql.'查询提供的参数不正确。');
	}

	/**
	  * @method 获取Enum字段的所有值
	  * @param  $Table string  表
	  * @param  $Filed string  字段
	  * @param  $Cache true|false 默认使用缓存
	  * @author soul
	  * @copyright 2016/3/30
	  * @return [ ["status"]=> true ["data"]=>一维数组["code"]=> ]
	  */
	public function GetEnum($Table, $Filed, $Cache = true)
	{
		$Arr = [];
		if(!empty($Table) && !empty($Filed))
		{
			$CacheFile = mvc::$cfg['AUTOLOAD']['Main']['path'].'cache/enum.php';
			if(empty($Arr[$Table][$Filed]) || !$Cache || (filemtime($CacheFile) + mvc::$cfg['CACHE_TIME'] ) > time())
			{
				$Sql = sprintf("SHOW COLUMNS from %s like '%s'", LibFc::Escape($Table), LibFc::Escape($Filed));
				$Res = $this->GetDataBySql($Sql, true);
				$Arr[$Table][$Filed] = LibFc::GetEnum($Res['data']['Type']);
				LibFc::ArraySave($Arr, $CacheFile);
			}
			else 
			{
				$Arr = include($CacheFile);
			}
		}
		return LibFc::ReturnData(true, $Arr[$Table][$Filed]);
	}

	/**
	  * @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->MainDb->Begin();
		$ResFlag = $this->MainDb->Query($AddSql);
		$Rows = $this->MainDb->RowCount();
		if($ResFlag && $Rows > 0)
		{
			$Id = $this->MainDb->InsertID();
			$this->MainDb->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->MainDb->Begin();
		$ResFlag = $this->MainDb->Query($SetSql);
		if($ResFlag)
		{
			$Rows = $this->MainDb->RowCount();
			$this->MainDb->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->MainDb->Begin();
		$ResFlag = $this->MainDb->Query($DelSql);
		if($ResFlag)
		{
			$Rows = $this->MainDb->RowCount();
			$this->MainDb->End();
			return LibFc::ReturnData(true, $Rows);
		}
		return LibFc::ReturnData(false,  $Table.'删除记录SQL语句执行失败。');
	}
}