<?php
/**
  * @method 宜配网产品库表基本操作
  * @author soul
  * @copyright 2016/3/31
  */
class OthBase
{
	protected $OthDb = null;
	public function __construct($OthDb = null)
	{
		$this->OthDb = $OthDb==null ?  new LibDb (mvc::$cfg['DB']['B2COTH']): $OthDb;
	}
	
	/**
	  * @method 查询表信息
	  * @param  $Table 表明 如：Oth_product 或者 Oth_product as a join Oth_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->OthDb->Query($Sql);
			if($ResFlag)
			{
				$Arr = array();
				while($Row = $this->OthDb->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  $Table 表明 如：Oth_product 或者 Oth_product as a join Oth_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->OthDb->Begin();
		$ResFlag = $this->OthDb->Query($AddSql);
		$Rows = $this->OthDb->RowCount();
		if($ResFlag && $Rows > 0)
		{
			$Id = $this->OthDb->InsertID();
			$this->OthDb->End();
			return LibFc::ReturnData(true, $Id);
		}
		return LibFc::ReturnData(false, $Table.'添加记录SQL语句执行失败。');
	}

	/**
	  * @method 根据条件修改记录
	  * @param  $Table 表明 如：Oth_product 或者 Oth_product as a join Oth_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->OthDb->Begin();
		$ResFlag = $this->OthDb->Query($SetSql);
		if($ResFlag)
		{
			$Rows = $this->OthDb->RowCount();
			$this->OthDb->End();
			return LibFc::ReturnData(true, $Rows);
		}
		return LibFc::ReturnData(false, $Table.'修改记录SQL语句执行失败。');
	}

	/**
	  * @method 根据条件删除记录
	  * @param  $Table 表明 如：Oth_product 或者 Oth_product as a join Oth_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->OthDb->Begin();
		$ResFlag = $this->OthDb->Query($DelSql);
		if($ResFlag)
		{
			$Rows = $this->OthDb->RowCount();
			$this->OthDb->End();
			return LibFc::ReturnData(true, $Rows);
		}
		return LibFc::ReturnData(false,  $Table.'删除记录SQL语句执行失败。');
	}
}