<?php
class Table_ProductSearch
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_ProductSearch();
        $this->_tableHistory = new DbTable_ProductSearch();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_ProductSearch();
    }

    /**
     * @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 = "ps_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 = "ps_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 = 'ps_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);
    }

    /**
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getLikeByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "")
    {
        // echo '<pre>';
        // print_r($condition);exit;
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $type = ($type) == 'count(*)' ? (in_array($condition['product_search_id'], array(4,5))?'COUNT(DISTINCT product_sku,customer_company_name)':$type) : $type;
        $select->from($table, $type);
        $select->where("1 =?", 1);
        if(isset($condition["product_title"]) && $condition["product_title"] != ""){
            $select->where("product_title like ?",'%'.$condition["product_title"].'%');
        }
        if(isset($condition["hs_code"]) && $condition["hs_code"] != ""){
            $select->where("hs_code like ?",'%'.$condition["hs_code"].'%');
        }
        if(isset($condition["customer.customer_company_name_like"]) && $condition["customer.customer_company_name_like"] != ""){
            $select->where("customer_company_name like ?",'%'.$condition["customer.customer_company_name_like"].'%');
        }
        if(isset($condition["product_sku"]) && $condition["product_sku"] != ""){
            $select->where("product_sku like ?",'%'.$condition["product_sku"].'%');
        }
        if(isset($condition["goods_id"]) && $condition["goods_id"] != ""){
            $select->where("goods_id like ?",'%'.$condition["goods_id"].'%');
        }
        if(isset($condition["dateFor"]) && $condition["dateFor"] != ""){
            $select->where("shipment_time >= ?",$condition["dateFor"]);
        }
        if(isset($condition["dateTo"]) && $condition["dateTo"] != ""){
            $select->where("shipment_time <= ?",$condition["dateTo"]);
        }

        /*CONDITION_END*/
        if ('count(*)' == $type || 'COUNT(DISTINCT product_sku,customer_company_name)' == $type) {
            // echo $select;exit;
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            // echo '<pre>';
            // print_r($condition);exit;
            if(in_array($condition['product_search_id'], array(4,5))){
                $select->group(array("product_sku","customer_company_name"));
            }
            if (!empty($orderBy)) {
                $select->order($orderBy);
            }
            if ($pageSize > 0 and $page > 0) {
                $start = ($page - 1) * $pageSize;
                $select->limit($pageSize, $start);
            }
            $sql = $select->__toString();
            // echo $sql;exit;
            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*/
        
        $select->where("1 =?", 1);
        if(isset($condition["ps_id"]) && $condition["ps_id"] != ""){
            $select->where("ps_id = ?",$condition["ps_id"]);
        }
        if(isset($condition["product_title"]) && $condition["product_title"] != ""){
            $select->where("product_title = ?",$condition["product_title"]);
        }
        if(isset($condition["hs_code"]) && $condition["hs_code"] != ""){
            $select->where("hs_code = ?",$condition["hs_code"]);
        }
        if(isset($condition["customer.customer_company_name_="]) && $condition["customer.customer_company_name_="] != ""){
            $select->where("customer_company_name = ?",$condition["customer_company_name"]);
        }
        if(isset($condition["product_sku"]) && $condition["product_sku"] != ""){
            $select->where("product_sku = ?",$condition["product_sku"]);
        }
        if(isset($condition["goods_id"]) && $condition["goods_id"] != ""){
            $select->where("goods_id = ?",$condition["goods_id"]);
        }
        if(isset($condition["dateFor"]) && $condition["dateFor"] != ""){
            $select->where("shipment_time >= ?",$condition["dateFor"]);
        }
        if(isset($condition["dateTo"]) && $condition["dateTo"] != ""){
            $select->where("shipment_time <= ?",$condition["dateTo"]);
        }

        /*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);
            }
            // echo $sql;exit;
            $sql = $select->__toString();
            return $this->_table->getAdapter()->fetchAll($sql);
        }
    }

}