<?php

class Table_InvtManifestLog
{
    protected $_table = null;

    public function __construct() {
        $this->_table = new DbTable_InvtManifestLog();
    }

    public function getAdapter() {
        return $this->_table->getAdapter();
    }

    public static function getInstance() {
        return new Table_InvtManifestLog();
    }

    /**
     * @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 = "iml_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 = "iml_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 = 'iml_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["iml_id"]) && $condition["iml_id"] != "") {
            $select->where("iml_id = ?", $condition["iml_id"]);
        }
        if (isset($condition["im_id"]) && $condition["im_id"] != "") {
            $select->where("im_id = ?", $condition["im_id"]);
        }
        if (isset($condition["operater_id"]) && $condition["operater_id"] != "") {
            $select->where("operater_id = ?", $condition["operater_id"]);
        }
        if (isset($condition["add_time"]) && $condition["add_time"] != "") {
            $select->where("add_time = ?", $condition["add_time"]);
        }
        if (isset($condition["content"]) && $condition["content"] != "") {
            $select->where("content = ?", $condition["content"]);
        }
        if (isset($condition["status_from"]) && $condition["status_from"] != "") {
            $select->where("status_from = ?", $condition["status_from"]);
        }
        if (isset($condition["status_to"]) && $condition["status_to"] != "") {
            $select->where("status_to = ?", $condition["status_to"]);
        }
        if (isset($condition["ip"]) && $condition["ip"] != "") {
            $select->where("ip = ?", $condition["ip"]);
        }
        /*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);
        }
    }

    /**
     * @param array $condition
     * @param string $type
     * @param int $pageSize
     * @param int $page
     * @param string $orderBy
     * @return array|string
     */
    public function getJoinLeftByCondition($condition = array(), $type = '*', $pageSize = 0, $page = 1, $orderBy = "") {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from(['l'=>$table], $type);
        $select->joinLeft(['u'=>'user'],'l.operater_id=u.user_id');
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        if (isset($condition["iml_id"]) && $condition["iml_id"] != "") {
            $select->where("iml_id = ?", $condition["iml_id"]);
        }
        if (isset($condition["im_id"]) && $condition["im_id"] != "") {
            $select->where("im_id = ?", $condition["im_id"]);
        }
        if (isset($condition["operater_id"]) && $condition["operater_id"] != "") {
            $select->where("operater_id = ?", $condition["operater_id"]);
        }
        if (isset($condition["status_from"]) && $condition["status_from"] != "") {
            $select->where("status_from = ?", $condition["status_from"]);
        }
        if (isset($condition["status_to"]) && $condition["status_to"] != "") {
            $select->where("status_to = ?", $condition["status_to"]);
        }
        /*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);
        }
    }
}