<?php
class Table_ProductGoods
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_ProductGoods();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_ProductLog();
    }

    /**
     * @param $row
     * @return mixed
     */
    public function add($row)
    {
        return $this->_table->insert($row);
    }


    /**
     * @param $row
     * @param $value
     * @param string $field
     * @return mixed
     */
    public function update($row, $value, $field = "pg_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
        return $this->_table->update($row, $where);
    }

    /**
     * @param $value
     * @param string $field
     * @return mixed
     */
    public function delete($value, $field = "pg_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
        return $this->_table->delete($where);
    }

    /**
     * @param $value
     * @param string $field
     * @param string $colums
     * @return mixed
     */
    public function getByField($value, $field = 'pg_id', $colums = "*")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $colums);
        $select->where("{$field} = ?", $value);
        return $this->_table->getAdapter()->fetchRow($select);
    }

    public function getAll()
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, "*");
        return $this->_table->getAdapter()->fetchAll($select);
    }

    public function getProductByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $order = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinInner('product','product.product_id='.$table.".product_id");
        $select->joinInner('customer','customer.customer_id=product.customer_id');

        if(isset($condition["product_sku"]) && $condition["product_sku"] != ""){
            $select->where("product.product_sku = ?",$condition["product_sku"]);
        }
        if (isset($condition["pg_id_array"]) && $condition["pg_id_array"] != "" && is_array($condition["pg_id_array"])) {
            $select->where("pg_id IN (?)", $condition["pg_id_array"]);
        }
        if(isset($condition["product_id"]) && $condition["product_id"] != ""){
            $select->where("product_id = ?",$condition["product_id"]);
        }
        if(isset($condition["goods_code"]) && $condition["goods_code"] != ""){
            $select->where("goods_code = ?",$condition["goods_code"]);
        }
        if(isset($condition["goods_model"]) && $condition["goods_model"] != ""){
            $select->where("goods_model = ?",$condition["goods_model"]);
        }
        if(isset($condition["pg_status"]) && $condition["pg_status"] !== ""){
            $select->where("pg_status = ?",$condition["pg_status"]);
        }
        if(isset($condition["hs_code"]) && $condition["hs_code"] !== ""){
            $select->where("product.hs_code = ?",$condition["hs_code"]);
        }
        if(isset($condition['pg_id_gt']) && $condition['pg_id_gt'] !== ''){
            $select->where("pg_id > ?",$condition["pg_id_gt"]);
        }//customer_code
        if(isset($condition['customer_code']) && $condition['customer_code'] !== ''){
            $select->where("product.customer_code = ?",$condition["customer_code"]);
        }
        if(isset($condition['fml_status']) && $condition['fml_status'] !== '' && !is_array($condition['fml_status'])){
            $select->where("fml_status = ?",$condition["fml_status"]);
        }elseif(isset($condition['fml_status']) && $condition['fml_status'] !== '' && is_array($condition['fml_status'])){
            if(in_array('6' , $condition['fml_status'])){
                $select->where("fml_status NOT IN (?)",$condition["fml_status"]);
            }else{
                $select->where("fml_status IN (?)",$condition["fml_status"]);
            }
        }
        if(isset($condition['customer_code_Arr'])&&!empty($condition['customer_code_Arr'])){
            $select->where("product.customer_code in (?)",$condition['customer_code_Arr']);
        }
        if(isset($condition["zzc_status"]) && $condition["zzc_status"] !== ""){
            $select->where("zzc_status = ?",$condition["zzc_status"]);
        }
        $select->where("product.customer_code not in (?)",array('E0207','E0346'));
        if(isset($condition["customer_platform"]) && $condition["customer_platform"] != ""){
            $select->where("customer.customer_platform = ?",$condition["customer_platform"]);
        }
        if(isset($condition["pg_status_neq"]) && $condition["pg_status_neq"] !== ""){
            $select->where("pg_status <> ?",$condition["pg_status_neq"]);
        }


        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            if (!empty($order)) {
                $select->order($order);
            }
            if ($pageSize > 0 and $page > 0) {
                $start = ($page - 1) * $pageSize;
                $select->limit($pageSize, $start);
            }
            $sql = $select->__toString();
            return $this->_table->getAdapter()->fetchAll($sql);
        }
    }

    /**
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        
        if(isset($condition["goods_code"]) && $condition["goods_code"] != ""){
            $select->where("goods_code = ?",$condition["goods_code"]);
        }
        if(isset($condition["goods_key"]) && $condition["goods_key"] != ""){
            $select->where("goods_key = ?",$condition["goods_key"]);
        }
        if(isset($condition["product_id"]) && $condition["product_id"] != ""){
            $select->where("product_id = ?",$condition["product_id"]);
        }
        if(isset($condition["pg_status"]) && $condition["pg_status"] !== ""){
            $select->where("pg_status = ?",$condition["pg_status"]);
        }
        if(isset($condition["hs_key"]) && $condition["hs_key"] !== ""){
        	$select->where("hs_key = ?",$condition["hs_key"]);
        }
        if(isset($condition['_string']) && $condition['_string'] != ''){
            $select->where($condition['_string']);
        }
        if(isset($condition['not_goods_code'])&&!empty($condition['not_goods_code'])){
            $select->where(" goods_code not in (?)",$condition['not_goods_code']);
        }
        if(isset($condition["fml_status"]) && $condition["fml_status"] != ""){
            $select->where("fml_status = ?",$condition["fml_status"]);
        }
        if(isset($condition['fml_status_lt'])&&$condition['fml_status_lt']!=""){
            $select->where("fml_status > ?",$condition["fml_status_lt"]);
        }
        if(isset($condition['zzc_status_lt'])&&$condition['zzc_status_lt']!=""){
            $select->where("zzc_status > ?",$condition["zzc_status_lt"]);
        }
        if(isset($condition['zzc_status_neq'])&&$condition['zzc_status_neq']!=""){
            $select->where("zzc_status <> ?",$condition["zzc_status_neq"]);
        }
        if(isset($condition['pg_status_neq'])&&$condition['pg_status_neq']!=""){
            $select->where("pg_status <> ?",$condition["pg_status_neq"]);
        }
        
        
        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            if (!empty($orderBy)) {
                $select->order($orderBy);
            }
            if ($pageSize > 0 and $page > 0) {
                $start = ($page - 1) * $pageSize;
                $select->limit($pageSize, $start);
            }
            $sql = $select->__toString();
            return $this->_table->getAdapter()->fetchAll($sql);
        }
    }
}