<?php
class Table_OutArea {
  private static $_instance = NULL;
  private $_table           = NULL;

  /**
   * 禁止直接创建实例，通过getInstance方法获取实例
   */
  private function __construct() {
    $this->_table = new DbTable_OutArea();
  }

  /**
   * 禁止克隆
   */
  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('oa_id=?', (int) $value);

    return $this->_table->update($data, $where);
  }

  /**
   * 通过入区派车单单号更新数据
   * 
   * @param array  $data     需要更新的数据
   * @param string $value    入区派车单单号
   * @return integer
   */
  public function updateByCode($data, $value) {
    $where = $this->_table->getAdapter()->quoteInto('oa_code=?', trim($value));

    return $this->_table->update($data, $where);
  }

  /**
   * 通过入区派车单ID删除数据
   * 
   * @param string $value    入区派车单ID
   * @return integer
   */
  public function deleteById($value) {
    $where = $this->_table->getAdapter()->quoteInto('oa_id=?', (int) $value);

    return $this->_table->delete($where);
  }

  /**
   * 通过入区派车单单号删除数据
   * 
   * @param string $value    入区派车单单号
   * @return integer
   */
  public function deleteByCode($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('oa_id=?', (int) $value);

    return $this->_table->getAdapter()->fetchRow($select);
  }

  /**
   * 通过入区派车单单号获取数据
   * 
   * @param string $value    入区派车单单号
   * @return mixed
   */
  public function getByCode($value) {
    $select = $this->_table->getAdapter()->select();
    $table  = $this->_table->info('name');
    $select->from($table, '*')->where('oa_code=?', trim($value));

    return $this->_table->getAdapter()->fetchRow($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(oa_id)');
    }
    elseif (isset($option['column']) && '' !== trim($option['column'])) { // 只需检索指定字段
      $select->from($table, trim($option['column']));
    }
    else { // 默认，检索所有字段
      $select->from($table, '*');
    }

    // 出区派车单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']));
    }

    // 项目名称
    if (isset($condition['ie_port']) && '' !== trim($condition['ie_port'])) {
      $select->where('ie_port=?', trim($condition['ie_port']));
    }

    // 车牌号
    if (isset($condition['car_number']) && '' !== trim($condition['car_number'])) {
      $select->where('car_number=?', trim($condition['car_number']));
    }

    // 柜型
    if (isset($condition['container_type']) && '' !== trim($condition['container_type'])) {
      $select->where('container_type=?', (int) trim($condition['container_type']));
    }

    // 入区派车单状态
    if (isset($condition['status']) && '' !== trim($condition['status'])) {
      $select->where('status=?', (int) trim($condition['status']));
    }

    // 创建开始时间
    if (isset($condition['created_time_from']) && '' !== trim($condition['created_time_from'])) {
      $select->where('created_time>=?', trim($condition['created_time_from']) . ' 00:00:00');
    }

    // 创建结束时间
    if (isset($condition['created_time_to']) && '' !== trim($condition['created_time_to'])) {
      $select->where('created_time<=?', trim($condition['created_time_to']) . ' 23:59:59');
    }

    // 确认开始时间
    if (isset($condition['confirmed_time_from']) && '' !== trim($condition['confirmed_time_from'])) {
      $select->where('confirmed_time>=?', trim($condition['confirmed_time_from']) . ' 00:00:00');
    }

    // 确认结束时间
    if (isset($condition['confirmed_time_to']) && '' !== trim($condition['confirmed_time_to'])) {
      $select->where('confirmed_time<=?', trim($condition['confirmed_time_to']) . ' 23:59:59');
    }

    // 创建者
    if (isset($condition['creator']) && '' !== trim($condition['creator'])) {
      $select->where('creator=?', (int) trim($condition['creator']));
    }

    // 创建者IP
    if (isset($condition['creator_ip']) && '' !== trim($condition['creator_ip'])) {
      $select->where('creator_ip=?', trim($condition['creator_ip']));
    }

    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);
    }
  }
}
