<?php
class Table_OutAreaDetail {
  private static $_instance = NULL;
  private $_table           = NULL;

  /**
   * 禁止直接创建实例，通过getInstance方法获取实例
   */
  private function __construct() {
    $this->_table = new DbTable_OutAreaDetail();
  }

  /**
   * 禁止克隆
   */
  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('oad_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('oad_id=?', (int) $value);

    return $this->_table->delete($where);
  }

  /**
   * 通过入区派车单ID删除数据
   * 
   * @param string $value    入区派车单ID
   * @return integer
   */
  public function deleteByParentId($value) {
    $where = $this->_table->getAdapter()->quoteInto('oa_id=?', (int) $value);

    return $this->_table->delete($where);
  }

  /**
   * 通过入区派车单单号删除数据
   * 
   * @param string $value    入区派车单单号
   * @return integer
   */
  public function deleteByParentCode($value) {
    $where = $this->_table->getAdapter()->quoteInto('oa_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('oad_id=?', (int) $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('oa_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('oa_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(oad_id)');
    }
    elseif (isset($option['column']) && '' !== trim($option['column'])) { // 只需检索指定字段
      $select->from($table, trim($option['column']));
    }
    else { // 默认，检索所有字段
      $select->from($table, '*');
    }

    // 出区派车单详情ID
    if (isset($condition['oad_id']) && '' !== trim($condition['oad_id'])) {
      $select->where('oad_id=?', (int) trim($condition['oad_id']));
    }

    // 出区派车单ID
    if (isset($condition['oa_id']) && '' !== trim($condition['oa_id'])) {
      $select->where('oa_id=?', (int) trim($condition['oa_id']));
    }

    // 出区派车单单号
    if (isset($condition['oa_code']) && '' !== trim($condition['oa_code'])) {
      $select->where('oa_code=?', trim($condition['oa_code']));
    }

    // ASN单号
    if (isset($condition['sb_code']) && '' !== trim($condition['sb_code'])) {
      $select->where('sb_code=?', trim($condition['sb_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);
    }
  }
}
