<?php
class Table_ShipCustomerWeightPrice
{
    protected $_table = null;

    public function __construct()
    {
        $this->_table = new DbTable_ShipCustomerWeightPrice();
        $this->_tableHistory = new DbTable_ShipCustomerWeightPriceHistory();
        $this->_tableTemp = new DbTable_ShipCustomerWeightPriceTemp();
    }

    public function getAdapter()
    {
        return $this->_table->getAdapter();
    }

    public static function getInstance()
    {
        return new Table_ShipCustomerWeightPrice();
    }

    /**
     * @param $row
     * @return mixeds
     */
    public function add($row)
    {
        return $this->_table->insert($row);
    }


    /**
     * @param $row
     * @param $value
     * @param string $field
     * @return mixed
     */
    public function update($row, $value, $field = "scwp_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 = "scwp_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 = 'scwp_id', $columns = "*") {
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table, $columns);
        $select->where("{$field} = ?", $value);
        return $this->_table->getAdapter()->fetchRow($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["scwp_weight"]) && $condition["scwp_weight"] != ""){
            $select->where("scwp_weight = ?",$condition["scwp_weight"]);
        }
        if(isset($condition["customer_code"]) && $condition["customer_code"] != ""){
            $select->where("customer_code = ?",$condition["customer_code"]);
        }
        if(isset($condition["scwp_exces_weight"]) && $condition["scwp_exces_weight"] != ""){
            $select->where("scwp_exces_weight = ?",$condition["scwp_exces_weight"]);
        }
        if(isset($condition["scwp_cost"]) && $condition["scwp_cost"] != ""){
            $select->where("scwp_cost = ?",$condition["scwp_cost"]);
        }
        if(isset($condition["scwp_exces_cost"]) && $condition["scwp_exces_cost"] != ""){
            $select->where("scwp_exces_cost = ?",$condition["scwp_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["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["scwp_fee_type"]) && $condition["scwp_fee_type"] !== ""){
            $select->where("scwp_fee_type = ?",$condition["scwp_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["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"]);
        }
        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("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);
        }
    }

    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. ' (scwp_id,sm_code,area,scwp_weight,scwp_price,scwp_cost,scwp_exces_weight,scwp_exces_price,scwp_exces_cost,currency_code,scwp_fee_type,sm_id,warehouse_id,one_ticket_fee,cost_currency_code,one_ticket_cbcost,price_batch_number,date_start,date_end,customer_code) SELECT scwp_id,sm_code,area,scwp_weight,scwp_price,scwp_cost,scwp_exces_weight,scwp_exces_price,scwp_exces_cost,currency_code,scwp_fee_type,sm_id,warehouse_id,one_ticket_fee,cost_currency_code,one_ticket_cbcost,price_batch_number,date_start,date_end,customer_code FROM '.$table.' WHERE '.$table.'.scwp_id IN ('.implode(',',$value).')';
        $this->_table->getAdapter()->query($sql);
    }

    public function batchDelete($value, $field = "scwp_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field} IN(?)", $value);
        return $this->_table->delete($where);
    }

    /**
     * [batchUpdate 批量更新]
     * @param  [type] $row   [description]
     * @param  [type] $value [description]
     * @param  string $field [description]
     * @return [type]        [description]
     */
    public function batchUpdate($row, $value, $field = "scwp_id")
    {
        $where = $this->_table->getAdapter()->quoteInto("{$field} IN (?)", $value);
        return $this->_table->update($row, $where);
    }

    /**
     * [batchCopyTempToActive 把临时表数据更新到正式数据]
     * @param  [type] $value [description]
     * @param  string $field [description]
     * @return [type]        [description]
     */
    public function batchCopyTempToActive($value, $field = "scwpt_id"){
        if(empty($value) || !is_array($value)){
            return false;
        }
        $table  = $this->_table->info('name');
        $tableTemp = $this->_tableTemp->info('name');
        $sql = 'INSERT INTO '.$table. ' (sm_code,area,scwp_weight,scwp_price,scwp_cost,scwp_exces_weight,scwp_exces_price,scwp_exces_cost,currency_code,scwp_fee_type,sm_id,warehouse_id,one_ticket_fee,cost_currency_code,one_ticket_cbcost,price_batch_number,date_start,date_end,customer_code) SELECT sm_code,area,scwp_weight,scwp_price,scwp_cost,scwp_exces_weight,scwp_exces_price,scwp_exces_cost,currency_code,scwp_fee_type,sm_id,warehouse_id,one_ticket_fee,cost_currency_code,one_ticket_cbcost,price_batch_number,date_start,date_end,customer_code FROM '.$tableTemp.' WHERE '.$tableTemp.'.scwpt_id IN ('.implode(',',$value).')';
        return $this->_table->getAdapter()->query($sql);
    }
}