<?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);
    }

    /**
     * @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["product_id"]) && $condition["product_id"] != ""){
            $select->where("product_id = ?",$condition["product_id"]);
        }
        if(isset($condition["pl_type"]) && $condition["pl_type"] != ""){
            $select->where("pl_type = ?",$condition["pl_type"]);
        }
        if(isset($condition["user_id"]) && $condition["user_id"] != ""){
            $select->where("user_id = ?",$condition["user_id"]);
        }
        if(isset($condition["customer_id"]) && $condition["customer_id"] != ""){
            $select->where("customer_id = ?",$condition["customer_id"]);
        }
        if(isset($condition["pl_statu_pre"]) && $condition["pl_statu_pre"] != ""){
            $select->where("pl_statu_pre = ?",$condition["pl_statu_pre"]);
        }
        if(isset($condition["pl_statu_now"]) && $condition["pl_statu_now"] != ""){
            $select->where("pl_statu_now = ?",$condition["pl_statu_now"]);
        }
        if(isset($condition["pl_note"]) && $condition["pl_note"] != ""){
            $select->where("pl_note = ?",$condition["pl_note"]);
        }
        if(isset($condition["pl_ip"]) && $condition["pl_ip"] != ""){
            $select->where("pl_ip = ?",$condition["pl_ip"]);
        } */
        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']);
        }
        /*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);
        }
    }
}