<?php
/**
  * @method 宜配网车型库基本操作
  * @author soul
  * @copyright 2016/3/31
  */
class YpsBase
{
	public $YpsDb = null;
	public function __construct($YpsDb = null)
	{
		$this->YpsDb = $YpsDb==null ?  new LibDb (mvc::$cfg['DB']['YP']): $YpsDb;
	}
	
	/**
	  * @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->YpsDb->Query($Sql);
			if($ResFlag)
			{
				$Arr = array();
				while($Row = $this->YpsDb->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  $desidArr 语言包ID
	  * @param  $langId 语言ID
	  * @author soul
	  * @copyright 2016/3/30
	  * @return array(3) { ["status"]=> bool(true) ["data"]=> ["code"]=> int(0) } 
	  */
	public function getLang($desidArr, $langId=1)
	{
		$arr = [];
		if(!empty($desidArr))
		{
			$res = $this->get('yp_lng_des as ld join yp_lng_text as lt on  ld.txtid = lt.txtid', ['ld.desid, lt.txt'], sprintf(" and ld.desid in (%s) and ld.lngid=%d", implode(',', array_unique($desidArr)), $langId));
			foreach($res['data'] as $v)
			{
				$arr[$v['desid']] = $v['txt'];
			}
		}
		return LibFc::ReturnData(true, $arr);
	}

}