<?php

/**
 * realoem操作基类
 */
class EtkBasic
{
    public $oQuery = null;
    public function __construct($oConfig = null) {
        $this->oQuery = $oConfig == null ?  new LibDb(mvc::$cfg['DB']['Etk']): $oConfig;
    }

    /**
     * 获取表记录
     * @param $sTable
     * @param $aField
     * @param string $sWhere
     * @param bool $sFirst
     * @return array
     */
    public function Get($sTable, $aField, $sWhere = '', $sFirst = false)
    {
        if ($sTable && is_array($aField) && !empty($aField))
        {
            $sQuery = 'select %s from %s where 1 = 1 %s';
            $sQuery = sprintf($sQuery, implode(',', $aField), LibFc::Escape($sTable), $sWhere);
            $oQuery = $this->oQuery->Query($sQuery);
            if (!$oQuery) {
                return LibFc::ReturnData(false, $sQuery.'查询SQL语句执行失败。');
            }

            $aReturn = [];
            while ($aValue = $this->oQuery->Fetch())
            {
                if ($sFirst) {
                    return LibFc::ReturnData(true, $aValue);
                }

                $aReturn[] = $aValue;
            }
            return LibFc::ReturnData(true, $aReturn);
        }
        return LibFc::ReturnData(false, $sTable.'查询提供的参数不正确。');
    }

	/**
	  * @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->oQuery->Query($sql);
			if($resFlag)
			{
				$arr = array();
				while($row = $this->oQuery->Fetch())
				{
					$arr[] = $row[$field];
				}
				return LibFc::ReturnData(true, $arr);
			}
			return LibFc::ReturnData(false, $table.'查询SQL语句执行失败。');
		}
		return LibFc::ReturnData(false, $table.'查询提供的参数不正确。');
	}

    /**
     * 添加表记录
     * @param $sTable
     * @param $aValue
     * @return array
     */
    public function Add($sTable, $aValue)
    {
        if (empty($sTable) || !is_array($aValue) || empty($aValue)) {
            return LibFc::ReturnData(false, $sTable.'添加记录提供的参数不正确。');
        }

        $aKey = []; $aVal = [];
        foreach ($aValue as $sKey => $sVal)
        {
            $aKey[] = LibFc::Escape($sKey);
            $aVal[] = LibFc::Escape($sVal, true);
        }
        $sQuery = 'insert into %s (`%s`) values (\'%s\')';
        $sQuery = sprintf($sQuery, LibFc::Escape($sTable), implode('`,`', $aKey), implode("','", $aVal));

        $this->oQuery->Begin();
        $oQuery = $this->oQuery->Query($sQuery);
        $sCount = $this->oQuery->RowCount();
        if ($oQuery && $sCount)
        {
            $sValue = $this->oQuery->InsertID();
            $this->oQuery->End();

            return LibFc::ReturnData(true, $sValue);
        }
        return LibFc::ReturnData(false, $sTable.'添加记录SQL语句执行失败。');
    }

    /**
     * 修改表记录
     * @param $sTable
     * @param $aValue
     * @param $sWhere
     * @return array
     */
    public function Set($sTable, $aValue, $sWhere)
    {
        if (empty($sTable) || !is_array($aValue) || empty($aValue)) {
            return LibFc::ReturnData(false, $sTable.'修改记录提供的参数不正确。');
        }

        $aKeyIn = [];
        foreach ($aValue as $sKey => $sVal) {
            $aKeyIn[] = sprintf("`%s`='%s'", LibFc::Escape($sKey), LibFc::Escape($sVal, true));
        }
        $sQuery = 'update %s set %s where 1 %s';
        $sQuery = sprintf($sQuery, LibFc::Escape($sTable), implode(',', $aKeyIn), $sWhere);

        $this->oQuery->Begin();
        $oQuery = $this->oQuery->Query($sQuery);
        if ($oQuery)
        {
            $sCount = $this->oQuery->RowCount();
            $this->oQuery->End();

            return LibFc::ReturnData(true, $sCount);
        }
        return LibFc::ReturnData(false, $sTable.'修改记录SQL语句执行失败。');
    }

    /**
     * 删除表记录
     * @param $sTable
     * @param $sWhere
     * @return array
     */
    public function Del($sTable, $sWhere)
    {
        if (empty($sTable) || empty($sWhere)) {
            return LibFc::ReturnData(false, $sTable.'删除记录提供的参数不正确。');
        }
        $sQuery = 'delete from %s where 1 =1 %s';
        $sQuery = sprintf($sQuery, LibFc::Escape($sTable), $sWhere);

        $this->oQuery->Begin();
        $oQuery = $this->oQuery->Query($sQuery);
        if ($oQuery)
        {
            $sCount = $this->oQuery->RowCount();
            $this->oQuery->End();

            return LibFc::ReturnData(true, $sCount);
        }
        return LibFc::ReturnData(false, $sTable.'删除记录SQL语句执行失败。');
    }
}