<?php
class Table_TrackingErrorLog
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_TrackingErrorLog();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_TrackingErrorLog();
    }

    /**
     * @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 = "te_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 = "te_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 = 'te_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->joinLeft("order_operation_time",'order_operation_time.order_code=tracking_error_log.order_code',array('submit_time','order_code'));
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        if(isset($condition["dateFor"]) && $condition["dateFor"] != ""){
            $select->where("order_operation_time.submit_time >= ?",$condition["dateFor"]);
        }
        if(isset($condition["dateTo"]) && $condition["dateTo"] != ""){
            $select->where("order_operation_time.submit_time <= ?",$condition["dateTo"]);
        }
        if (isset($condition["order_code"]) && $condition["order_code"] != "") {
            $select->where("tracking_error_log.order_code = ?", $condition["order_code"]);
        }
        if (isset($condition["sm_code"]) && $condition["sm_code"] != "") {
            $select->where("tracking_error_log.sm_code = ?", $condition["sm_code"]);
        }
        if (isset($condition["update_time_start"]) && $condition["update_time_start"] != "") {
            $select->where("tracking_error_log.update_time >= ?", $condition["update_time_start"].'00:00:00');
        }
         if (isset($condition["update_time_end"]) && $condition["update_time_end"] != "") {
            $select->where("tracking_error_log.update_time >= ?", $condition["update_time_end"].'23:59:59');
        }
        if (isset($condition["type"]) && $condition["type"] != "") {
            $select->where("type = ?", $condition["type"]);
        }
        /*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);
        }
    }
}