<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 18-1-22
 * Time: 下午5:14
 * To change this template use File | Settings | File Templates.
 */
class Table_EportLogisticsXml
{
    protected $_table = NULL;

    public function __construct() {
        $this->_table = new DbTable_EportLogisticsXml();
    }

    public function insert($data) {
        return $this->_table->insert($data);
    }

    public function update($data, $value, $field = 'elx_id') {
        $where = $this->_table->getAdapter()->quoteInto("{$field}=?", $value);

        return $this->_table->update($data, $where);
    }

    public function delete($value, $field = 'elx_id') {
        $where = $this->_table->getAdapter()->quoteInto("{$field}=?", $value);

        return $this->_table->delete($where);
    }

    public function getByField($value, $field = 'elx_id', $column = '*') {
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');

        $select->from($table, $column)->where("{$field}=?", $value);

        return $this->_table->getAdapter()->fetchRow($select);
    }

    public function getForUpdate($value, $field = 'elx_id', $column = '*') {
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');

        $select->from($table, $column)->where("{$field}=?", $value);
        $select->forUpdate();

        return $this->_table->getAdapter()->fetchRow($select);
    }

    public function getByCondition($condition, $column = '*', $orderBy = array(), $size = 0, $page = 0) {
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');

        $select->from($table, $column);

        if (isset($condition['elx_id_lt']) && '' !== trim($condition['elx_id_lt'])) {
            $select->where('elx_id<?', (int) trim($condition['elx_id_lt']));
        }

        if (isset($condition['elx_id_gt']) && '' !== trim($condition['elx_id_gt'])) {
            $select->where('elx_id>?', (int) trim($condition['elx_id_gt']));
        }
        if(isset($condition['elx_id_array']) && '' !==$condition['elx_id_array']){
            $select->where('elx_id in(?)', $condition['elx_id_array']);
        }

        if (isset($condition['order_code']) && '' !== trim($condition['order_code'])) {
            $select->where('order_code=?', trim($condition['order_code']));
        }

        if (isset($condition['tracking_number']) && '' !== trim($condition['tracking_number'])) {
            $select->where('tracking_number=?', trim($condition['tracking_number']));
        }

        if (isset($condition['xml_type']) && '' !== trim($condition['xml_type'])) {
            $select->where('xml_type=?', (int) trim($condition['xml_type']));
        }

        if (isset($condition['xml_status']) && '' !== trim($condition['xml_status'])) {
            $select->where('xml_status=?', (int) trim($condition['xml_status']));
        }

        if (isset($condition['order_mode']) && '' !== trim($condition['order_mode'])) {
            $select->where('order_mode=?', (int) trim($condition['order_mode']));
        }

        if (isset($condition['customer_code']) && '' !== trim($condition['customer_code'])) {
            $select->where('customer_code=?', trim($condition['customer_code']));
        }

        if (isset($condition['sm_code']) && '' !== trim($condition['sm_code'])) {
            $select->where('sm_code=?', trim($condition['sm_code']));
        }

        if (isset($condition['order_created_time_start']) && '' !== trim($condition['order_created_time_start'])) {
            $select->where('order_created_time>=?', trim($condition['order_created_time_start']));
        }

        if (isset($condition['order_created_time_end']) && '' !== trim($condition['order_created_time_end'])) {
            $select->where('order_created_time<=?', trim($condition['order_created_time_end']));
        }

        if (isset($condition['created_time_start']) && '' !== trim($condition['created_time_start'])) {
            $select->where('created_time>=?', trim($condition['created_time_start']));
        }

        if (isset($condition['created_time_end']) && '' !== trim($condition['created_time_end'])) {
            $select->where('created_time<=?', trim($condition['created_time_end']));
        }

        if (isset($condition['updated_time_start']) && '' !== trim($condition['updated_time_start'])) {
            $select->where('updated_time>=?', trim($condition['updated_time_start']));
        }

        if (isset($condition['updated_time_end']) && '' !== trim($condition['updated_time_end'])) {
            $select->where('updated_time<=?', trim($condition['updated_time_end']));
        }

        if (isset($condition['return_time_start']) && '' !== trim($condition['return_time_start'])) {
            $select->where('return_time>=?', trim($condition['return_time_start']));
        }

        if (isset($condition['return_time_end']) && '' !== trim($condition['return_time_end'])) {
            $select->where('return_time<=?', trim($condition['return_time_end']));
        }
        if (isset($condition['xml_status']) && '' !== trim($condition['xml_status'])) {
            $select->where('xml_status=?', trim($condition['xml_status']));
        }

        if ('count(*)' === $column) {
            return (int) $this->_table->getAdapter()->fetchOne($select);
        }
        else {
            if (!empty($orderBy)) {
                $select->order($orderBy);
            }

            if ($size > 0 && $page > 0) {
                $offset = ($page - 1) * $size;
                $select->limit($size, $offset);
            }
            elseif ($size > 0) {
                $select->limit($size, 0);
            }

            return $this->_table->getAdapter()->fetchAll($select);
        }
    }
}
