<?php
class Table_BgPriEntryDataDetailBodyDetail
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_BgPriEntryDataDetailBodyDetail();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_BgPriEntryDataDetailBodyDetail();
    }

    /**
     * @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 = "bpedbdd_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 = "bpedbdd_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 = 'bpedbdd_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["bpedbdd_id"]) && $condition["bpedbdd_id"] != ""){
            $select->where("bpedbdd_id = ?",$condition["bpedbdd_id"]);
        }
                if(isset($condition["bpedbh_id"]) && $condition["bpedbh_id"] != ""){
            $select->where("bpedbh_id = ?",$condition["bpedbh_id"]);
        }
                if(isset($condition["g_no"]) && $condition["g_no"] != ""){
            $select->where("g_no = ?",$condition["g_no"]);
        }
                if(isset($condition["class_mark"]) && $condition["class_mark"] != ""){
            $select->where("class_mark = ?",$condition["class_mark"]);
        }
                if(isset($condition["code_ts"]) && $condition["code_ts"] != ""){
            $select->where("code_ts = ?",$condition["code_ts"]);
        }
                if(isset($condition["contr_item"]) && $condition["contr_item"] != ""){
            $select->where("contr_item = ?",$condition["contr_item"]);
        }
                if(isset($condition["decl_price"]) && $condition["decl_price"] != ""){
            $select->where("decl_price = ?",$condition["decl_price"]);
        }
                if(isset($condition["duty_mode"]) && $condition["duty_mode"] != ""){
            $select->where("duty_mode = ?",$condition["duty_mode"]);
        }
                if(isset($condition["factor"]) && $condition["factor"] != ""){
            $select->where("factor = ?",$condition["factor"]);
        }
                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["gno"]) && $condition["gno"] != ""){
            $select->where("gno = ?",$condition["gno"]);
        }
                if(isset($condition["origin_country"]) && $condition["origin_country"] != ""){
            $select->where("origin_country = ?",$condition["origin_country"]);
        }
                if(isset($condition["trade_curr"]) && $condition["trade_curr"] != ""){
            $select->where("trade_curr = ?",$condition["trade_curr"]);
        }
                if(isset($condition["decl_total"]) && $condition["decl_total"] != ""){
            $select->where("decl_total = ?",$condition["decl_total"]);
        }
                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["use_to"]) && $condition["use_to"] != ""){
            $select->where("use_to = ?",$condition["use_to"]);
        }
                if(isset($condition["work_usd"]) && $condition["work_usd"] != ""){
            $select->where("work_usd = ?",$condition["work_usd"]);
        }
                if(isset($condition["exg_no"]) && $condition["exg_no"] != ""){
            $select->where("exg_no = ?",$condition["exg_no"]);
        }
                if(isset($condition["exg_version"]) && $condition["exg_version"] != ""){
            $select->where("exg_version = ?",$condition["exg_version"]);
        }
                if(isset($condition["destination_country"]) && $condition["destination_country"] != ""){
            $select->where("destination_country = ?",$condition["destination_country"]);
        }
                if(isset($condition["ciq_code"]) && $condition["ciq_code"] != ""){
            $select->where("ciq_code = ?",$condition["ciq_code"]);
        }
                if(isset($condition["decl_goods_ename"]) && $condition["decl_goods_ename"] != ""){
            $select->where("decl_goods_ename = ?",$condition["decl_goods_ename"]);
        }
                if(isset($condition["orig_place_code"]) && $condition["orig_place_code"] != ""){
            $select->where("orig_place_code = ?",$condition["orig_place_code"]);
        }
                if(isset($condition["purpose"]) && $condition["purpose"] != ""){
            $select->where("purpose = ?",$condition["purpose"]);
        }
                if(isset($condition["prod_valid_dt"]) && $condition["prod_valid_dt"] != ""){
            $select->where("prod_valid_dt = ?",$condition["prod_valid_dt"]);
        }
                if(isset($condition["prod_qgp"]) && $condition["prod_qgp"] != ""){
            $select->where("prod_qgp = ?",$condition["prod_qgp"]);
        }
                if(isset($condition["goods_attr"]) && $condition["goods_attr"] != ""){
            $select->where("goods_attr = ?",$condition["goods_attr"]);
        }
                if(isset($condition["stuff"]) && $condition["stuff"] != ""){
            $select->where("stuff = ?",$condition["stuff"]);
        }
                if(isset($condition["uncode"]) && $condition["uncode"] != ""){
            $select->where("uncode = ?",$condition["uncode"]);
        }
                if(isset($condition["dang_name"]) && $condition["dang_name"] != ""){
            $select->where("dang_name = ?",$condition["dang_name"]);
        }
                if(isset($condition["dang_pack_type"]) && $condition["dang_pack_type"] != ""){
            $select->where("dang_pack_type = ?",$condition["dang_pack_type"]);
        }
                if(isset($condition["dang_pack_spec"]) && $condition["dang_pack_spec"] != ""){
            $select->where("dang_pack_spec = ?",$condition["dang_pack_spec"]);
        }
                if(isset($condition["eng_man_ent_cnm"]) && $condition["eng_man_ent_cnm"] != ""){
            $select->where("eng_man_ent_cnm = ?",$condition["eng_man_ent_cnm"]);
        }
                if(isset($condition["no_dang_flag"]) && $condition["no_dang_flag"] != ""){
            $select->where("no_dang_flag = ?",$condition["no_dang_flag"]);
        }
                if(isset($condition["dest_code"]) && $condition["dest_code"] != ""){
            $select->where("dest_code = ?",$condition["dest_code"]);
        }
                if(isset($condition["goods_spec"]) && $condition["goods_spec"] != ""){
            $select->where("goods_spec = ?",$condition["goods_spec"]);
        }
                if(isset($condition["goods_model"]) && $condition["goods_model"] != ""){
            $select->where("goods_model = ?",$condition["goods_model"]);
        }
                if(isset($condition["goods_brand"]) && $condition["goods_brand"] != ""){
            $select->where("goods_brand = ?",$condition["goods_brand"]);
        }
                if(isset($condition["produce_date"]) && $condition["produce_date"] != ""){
            $select->where("produce_date = ?",$condition["produce_date"]);
        }
                if(isset($condition["prod_batch_no"]) && $condition["prod_batch_no"] != ""){
            $select->where("prod_batch_no = ?",$condition["prod_batch_no"]);
        }
                if(isset($condition["district_code"]) && $condition["district_code"] != ""){
            $select->where("district_code = ?",$condition["district_code"]);
        }
                if(isset($condition["ciq_name"]) && $condition["ciq_name"] != ""){
            $select->where("ciq_name = ?",$condition["ciq_name"]);
        }
                if(isset($condition["mnufctr_reg_no"]) && $condition["mnufctr_reg_no"] != ""){
            $select->where("mnufctr_reg_no = ?",$condition["mnufctr_reg_no"]);
        }
                if(isset($condition["mnufctr_reg_name"]) && $condition["mnufctr_reg_name"] != ""){
            $select->where("mnufctr_reg_name = ?",$condition["mnufctr_reg_name"]);
        }
                if(isset($condition["add_time"]) && $condition["add_time"] != ""){
            $select->where("add_time = ?",$condition["add_time"]);
        }
                if(isset($condition["update_time"]) && $condition["update_time"] != ""){
            $select->where("update_time = ?",$condition["update_time"]);
        }
                if(isset($condition["rcep_orig_place_code"]) && $condition["rcep_orig_place_code"] != ""){
            $select->where("rcep_orig_place_code = ?",$condition["rcep_orig_place_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);
        }
    }
}