<?php
class Table_ShipWeightPriceTemp
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_ShipWeightPriceTemp();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_ShipWeightPriceTemp();
    }

    /**
     * @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 = "swp_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 = "swp_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field}= ?", $value);
        return $this->_table->delete($where);
    }

    public function batchDelete($value, $field = "swp_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field} IN(?)", $value);		
        return $this->_table->delete($where);
    }

    public function batchCopyToHistory($value, $field = "swp_id")
    {
        if(empty($value)){
            return false;
        }
        $table  = $this->_table->info('name');
        $tableHistory = $this->_tableHistory->info('name');
        $sql = 'REPLACE INTO '.$tableHistory. ' (swp_id,warehouse_code,sm_code,`area`,swp_weight,swp_price,swp_cost,swp_exces_weight,swp_exces_price,swp_exces_cost,currency_code,swp_fee_type,sm_id,warehouse_id,one_ticket_fee,cost_currency_code,one_ticket_cbcost,price_batch_number,date_start,date_end) SELECT swp_id,warehouse_code,sm_code,`area`,swp_weight,swp_price,swp_cost,swp_exces_weight,swp_exces_price,swp_exces_cost,currency_code,swp_fee_type,sm_id,warehouse_id,one_ticket_fee,cost_currency_code,one_ticket_cbcost,price_batch_number,date_start,date_end FROM '.$table.' WHERE '.$table.'.swp_id IN ('.implode(',',$value).')';
        return $this->_table->getAdapter()->query($sql);
    }

    /**
     * @param $value
     * @param string $field
     * @param string $colums
     * @return mixed
     */
    public function getByField($value, $field = 'swp_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->where("1 =?", 1);
        /*CONDITION_START*/
        
        if(isset($condition["swp_weight"]) && $condition["swp_weight"] != ""){
            $select->where("swp_weight = ?",$condition["swp_weight"]);
        }
        if(isset($condition["swp_exces_weight"]) && $condition["swp_exces_weight"] != ""){
            $select->where("swp_exces_weight = ?",$condition["swp_exces_weight"]);
        }
        if(isset($condition["swp_cost"]) && $condition["swp_cost"] != ""){
            $select->where("swp_cost = ?",$condition["swp_cost"]);
        }
        if(isset($condition["swp_exces_cost"]) && $condition["swp_exces_cost"] != ""){
            $select->where("swp_exces_cost = ?",$condition["swp_exces_cost"]);
        }
        if(isset($condition["currency_code"]) && $condition["currency_code"] != ""){
            $select->where("currency_code = ?",$condition["currency_code"]);
        }
        if(isset($condition["area"]) && $condition["area"] != ""){
            $select->where("area = ?",$condition["area"]);
        }
        if(isset($condition["sm_code"]) && $condition["sm_code"] != ""){
            $select->where("sm_code = ?",$condition["sm_code"]);
        }
        if(isset($condition["swp_fee_type"]) && $condition["swp_fee_type"] !== ""){
            $select->where("swp_fee_type = ?",$condition["swp_fee_type"]);
        }
        if(isset($condition["sm_id"]) && $condition["sm_id"] != ""){
            $select->where("sm_id = ?",$condition["sm_id"]);
        }
        if(isset($condition["warehouse_id"]) && $condition["warehouse_id"] != ""){
            $select->where("warehouse_id = ?",$condition["warehouse_id"]);
        }
        if(isset($condition["price_batch_number"]) && $condition["price_batch_number"] !== ""){
            $select->where("price_batch_number = ?",$condition["price_batch_number"]);
        }
        if(isset($condition["date_start_day"]) && $condition["date_start_day"] !== ""){
            $select->where("date_start <= ?",$condition["date_start_day"]);
        }
        if(isset($condition["date_start_no_day"]) && $condition["date_start_no_day"] !== ""){
            $select->where("date_start != ?",$condition["date_start_no_day"]);
        }
        if(isset($condition["date_end_no_day"]) && $condition["date_end_no_day"] !== ""){
            $select->where("date_end != ?",$condition["date_end_no_day"]);
        }
        /*
        //没有填结束日期 截止到当前时间
        if(isset($condition["date_start"]) && !empty($condition['date_start'])){
        	$select->where("UNIX_TIMESTAMP(date_start) <= ?", strtotime($condition["date_start"]));
        	if(!isset($condition["date_end"]) || empty($condition['date_end'])){
        		$select->where("UNIX_TIMESTAMP(date_end) >= ?", strtotime(date('Y-m-d', time())));
        	}
        }
        if(isset($condition["date_end"]) && !empty($condition['date_end'])){
        	$select->where("UNIX_TIMESTAMP(date_end) >= ?", strtotime($condition["date_end"]));
        }
        */
        if(isset($condition["date_start"]) && !empty($condition['date_start'])){
        	$select->where("date_start <= ?", $condition["date_start"]);
        }
        if(isset($condition["date_end"]) && !empty($condition['date_end'])){
        	$select->where("date_end >= ?", $condition["date_end"]);
        }
        /*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);
        }
    }

    /**
     * @desc 物流计费
     */
    public function getByInCondition($condition = array(), $type = '*', $orderBy = "", $limit='')
    {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $type);
        $select->where("1 =?", 1);
        /*CONDITION_START*/
        if (isset($condition["area_arr"]) && is_array($condition["area_arr"])) {
            $select->where("area in(?)", $condition["area_arr"]);
        }
        if (isset($condition["sm_code"]) && $condition["sm_code"] != "") {
            $select->where("sm_code = ?", $condition["sm_code"]);
        }
        if (isset($condition["swp_fee_type"]) && $condition["swp_fee_type"] !== "") {
            $select->where("swp_fee_type = ?", $condition["swp_fee_type"]);
        }
        if (isset($condition["sm_id"]) && $condition["sm_id"] != "") {
            $select->where("sm_id = ?", $condition["sm_id"]);
        }
        if (isset($condition["warehouse_id_arr"]) && is_array($condition["warehouse_id_arr"])) {
            $select->where("warehouse_id in(?)", $condition["warehouse_id_arr"]);
        }
        if (isset($condition["price_batch_number"]) && $condition["price_batch_number"] != "") {
            $select->where("price_batch_number = ?", $condition["price_batch_number"]);
        }
        //处理有效期
        $select->where("date_start <= ?", date('Y-m-d'));
        $select->where("date_end >= ?", date('Y-m-d'));
        
        /*CONDITION_END*/
        if ('count(*)' == $type) {
            return $this->_table->getAdapter()->fetchOne($select);
        } else {
            if (!empty($orderBy)) {
                $select->order($orderBy);
            }
            if (!empty($limit)) {
            	$select->limit($limit);
            }
            $sql = $select->__toString();
            return $this->_table->getAdapter()->fetchAll($sql);
        }
    }

}