<?php

class Table_IntoAreaDetail
{
    private static $_instance = NULL;
    private $_table = NULL;

    /**
     * 禁止直接创建实例，通过getInstance方法获取实例
     */
    public function __construct()
    {
        $this->_table = new DbTable_IntoAreaDetail();
    }

    /**
     * 禁止克隆
     */
    private function __clone()
    {
        // Intentionally left blank.
    }

    /**
     * 获取实例
     *
     * @return object
     */
    public static function getInstance()
    {
        if (NULL === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    /**
     * 添加入区派车单详情数据
     *
     * @param array $data 入区派车单详情数据
     * @return integer
     */
    public function insert($data)
    {
        return $this->_table->insert($data);
    }

    /**
     * 通过入区派车单详情ID更新数据
     *
     * @param array $data 需要更新的数据
     * @param string $value 入区派车单详情ID
     * @return integer
     */
    public function updateById($data, $value)
    {
        $where = $this->_table->getAdapter()->quoteInto('iad_id=?', (int)$value);

        return $this->_table->update($data, $where);
    }

    /**
     * 通过入区派车单详情ID删除数据
     *
     * @param string $value 入区派车单详情ID
     * @return integer
     */
    public function deleteById($value)
    {
        $where = $this->_table->getAdapter()->quoteInto('iad_id=?', (int)$value);

        return $this->_table->delete($where);
    }

    /**
     * 通过入区派车单ID删除数据
     *
     * @param string $value 入区派车单ID
     * @return integer
     */
    public function deleteByParentId($value)
    {
        $where = $this->_table->getAdapter()->quoteInto('ia_id=?', (int)$value);

        return $this->_table->delete($where);
    }

    /**
     * 通过入区派车单单号删除数据
     *
     * @param string $value 入区派车单单号
     * @return integer
     */
    public function deleteByParentCode($value)
    {
        $where = $this->_table->getAdapter()->quoteInto('ia_code=?', trim($value));

        return $this->_table->delete($where);
    }

    /**
     * 通过入区派车单详情ID获取数据
     *
     * @param string $value 入区派车单ID
     * @return mixed
     */
    public function getById($value)
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, '*')->where('iad_id=?', (int)$value);

        return $this->_table->getAdapter()->fetchRow($select);
    }

    public function getByField($value, $field = 'iad_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);
    }

    /**
     * 通过入区派车单ID获取数据
     *
     * @param string $value 入区派车单ID
     * @return mixed
     */
    public function getByParentId($value)
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, '*')->where('ia_id=?', (int)$value);

        return $this->_table->getAdapter()->fetchAll($select);
    }

    /**
     * 通过入区派车单单号获取数据
     *
     * @param string $value 入区派车单单号
     * @return mixed
     */
    public function getByParentCode($value)
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, '*')->where('ia_code=?', trim($value));

        return $this->_table->getAdapter()->fetchAll($select);
    }

    /**
     * 通过指定条件获取入区派车单详情数据
     *
     * @param array $condition 搜索条件
     * @param array $option 附件选项
     * @return mixed
     */
    public function getByCondition($condition, $option = array())
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');

        if (isset($option['count']) && !!$option['count']) { // 只需统计数
            $select->from($table, 'count(iad_id)');
        } elseif (isset($option['column']) && '' !== trim($option['column'])) { // 只需检索指定字段
            $select->from($table, trim($option['column']));
        } else { // 默认，检索所有字段
            $select->from($table, '*');
        }

        // 入区派车单详情ID
        if (isset($condition['iad_id']) && '' !== trim($condition['iad_id'])) {
            $select->where('iad_id=?', (int)trim($condition['iad_id']));
        }

        // 入区派车单ID
        if (isset($condition['ia_id']) && '' !== trim($condition['ia_id'])) {
            $select->where('ia_id=?', (int)trim($condition['ia_id']));
        }

        // 入区派车单单号
        if (isset($condition['ia_code']) && '' !== trim($condition['ia_code'])) {
            $select->where('ia_code=?', trim($condition['ia_code']));
        }

        // ASN单号
        if (isset($condition['receiving_code']) && '' !== trim($condition['receiving_code'])) {
            $select->where('receiving_code=?', trim($condition['receiving_code']));
        }

        if (isset($option['count']) && !!$option['count']) { // 返回统计数
            return (int)$this->_table->getAdapter()->fetchOne($select);
        } else { // 返回检索结果
            if (isset($option['order']) && !empty($option['order'])) { // 排序
                $select->order($option['order']);
            }

            if (isset($option['size']) && is_int($option['size']) && isset($option['page']) && is_int($option['page'])) { // 分页
                $size = $option['size'];
                $page = $option['page'];

                if ($size > 0 && $page > 0) {
                    $select->limit($size, ($page - 1) * $size);
                }
            }

            return $this->_table->getAdapter()->fetchAll($select);
        }
    }
}
