<?php

class Table_DesMessageHead
{
    protected $_table = null;

    public function __construct() {
        $this->_table = new DbTable_DesMessageHead();
    }

    public function getAdapter() {
        return $this->_table->getAdapter();
    }

    public static function getInstance() {
        return new Table_DesMessageHead();
    }

    /**
     * @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 = "dmh_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 = "dmh_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 = 'dmh_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["dmh_id"]) && $condition["dmh_id"] != "") {
            $select->where("dmh_id = ?", $condition["dmh_id"]);
        }
        if (isset($condition["dmh_no"]) && $condition["dmh_no"] != "") {
            $select->where("dmh_no = ?", $condition["dmh_no"]);
        }
        if (isset($condition["seq_no"]) && $condition["seq_no"] != "") {
            $select->where("seq_no = ?", $condition["seq_no"]);
        }
        if (isset($condition["ieflag"]) && $condition["ieflag"] != "") {
            $select->where("ieflag = ?", $condition["ieflag"]);
        }
        if (isset($condition["traf_name"]) && $condition["traf_name"] != "") {
            $select->where("traf_name = ?", $condition["traf_name"]);
        }
        if (isset($condition["type"]) && $condition["type"] != "") {
            $select->where("type = ?", $condition["type"]);
        }
        if (isset($condition["bill_no"]) && $condition["bill_no"] != "") {
            $select->where("bill_no = ?", $condition["bill_no"]);
        }
        if (isset($condition["add_time_start"]) && $condition["add_time_start"] != "") {
            $select->where("add_time >= ?", $condition["add_time_start"]);
        }
        if (isset($condition["add_time_end"]) && $condition["add_time_end"] != "") {
            $select->where("add_time <= ?", $condition["add_time_end"]);
        }

        /*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);
        }
    }

    public function getLeftJoinByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "") {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->joinLeft("bg_pri_entry_data_detail_body_head as bpedhh", "bpedhh.bpedbh_id={$table}.bpedbh_id", array('bpd_ref_no'));
        $select->joinLeft("bg_pri_entry_data as bped", "bped.bped_id=bpedhh.bped_id", array('ref_no'));
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        if (isset($condition["dmh_id"]) && $condition["dmh_id"] != "") {
            $select->where("dmh_id = ?", $condition["dmh_id"]);
        }
        if (isset($condition["dmh_no"]) && $condition["dmh_no"] != "") {
            $select->where("dmh_no = ?", $condition["dmh_no"]);
        }
        if (isset($condition["seq_no"]) && $condition["seq_no"] != "") {
            $select->where("seq_no = ?", $condition["seq_no"]);
        }
        if (isset($condition["ieflag"]) && $condition["ieflag"] != "") {
            $select->where("ieflag = ?", $condition["ieflag"]);
        }
        if (isset($condition["traf_name"]) && $condition["traf_name"] != "") {
            $select->where("traf_name = ?", $condition["traf_name"]);
        }
        if (isset($condition["type"]) && $condition["type"] != "") {
            $select->where("type = ?", $condition["type"]);
        }
        if (isset($condition["bill_no"]) && $condition["bill_no"] != "") {
            $select->where("bill_no = ?", $condition["bill_no"]);
        }
        if (isset($condition["ref_no"]) && $condition["ref_no"] != "") {
            $select->where("bped.ref_no = ?", $condition["ref_no"]);
        }
        if (isset($condition["bpd_ref_no"]) && $condition["bpd_ref_no"] != "") {
            $select->where("bpedhh.bpd_ref_no = ?", $condition["bpd_ref_no"]);
        }
        if (isset($condition["add_time_start"]) && $condition["add_time_start"] != "") {
            $select->where("add_time >= ?", $condition["add_time_start"]);
        }
        if (isset($condition["add_time_end"]) && $condition["add_time_end"] != "") {
            $select->where("add_time <= ?", $condition["add_time_end"]);
        }
        /*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);
        }
    }


}