<?php

class Table_BgProduct
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_BgProduct();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_BgProduct();
    }

    /**
     * @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 = "cp_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 = "cp_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 = 'cp_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["cp_id"]) && $condition["cp_id"] != "") {
            $select->where("cp_id = ?", $condition["cp_id"]);
        }
        if (isset($condition["customer_name"]) && $condition["customer_name"] != "") {
            $select->where("customer_name = ?", $condition["customer_name"]);
        }
        if (isset($condition["trade_co"]) && $condition["trade_co"] != "") {
            $select->where("trade_co = ?", $condition["trade_co"]);
        }
        if (isset($condition["goods_id"]) && $condition["goods_id"] != "") {
            $select->where("goods_id = ?", $condition["goods_id"]);
        }
        if (isset($condition["gmodel"]) && $condition["gmodel"] != "") {
            $select->where("gmodel = ?", $condition["gmodel"]);
        }
        if (isset($condition["gname"]) && $condition["gname"] != "") {
            $select->where("gname = ?", $condition["gname"]);
        }
        if (isset($condition["hs_code"]) && $condition["hs_code"] != "") {
            $select->where("hs_code = ?", $condition["hs_code"]);
        }
        if (isset($condition["origin_country"]) && $condition["origin_country"] != "") {
            $select->where("origin_country = ?", $condition["origin_country"]);
        }
        if (isset($condition["gqty"]) && $condition["gqty"] != "") {
            $select->where("gqty = ?", $condition["gqty"]);
        }
        if (isset($condition["first_qty"]) && $condition["first_qty"] != "") {
            $select->where("first_qty = ?", $condition["first_qty"]);
        }
        if (isset($condition["second_qty"]) && $condition["second_qty"] != "") {
            $select->where("second_qty = ?", $condition["second_qty"]);
        }
        if (isset($condition["gunit"]) && $condition["gunit"] != "") {
            $select->where("gunit = ?", $condition["gunit"]);
        }
        if (isset($condition["first_unit"]) && $condition["first_unit"] != "") {
            $select->where("first_unit = ?", $condition["first_unit"]);
        }
        if (isset($condition["second_unit"]) && $condition["second_unit"] != "") {
            $select->where("second_unit = ?", $condition["second_unit"]);
        }
        if (isset($condition["contr_item"]) && $condition["contr_item"] != "") {
            $select->where("contr_item = ?", $condition["contr_item"]);
        }
        if (isset($condition["status"]) && $condition["status"] != "") {
            $select->where("status = ?", $condition["status"]);
        }
        if (isset($condition["add_time"]) && $condition["add_time"] != "") {
            $select->where("add_time = ?", $condition["add_time"]);
        }
        /*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);
        }
    }
}