Dao.php 6.86 KB
<?php

/**
 * Created by JetBrains PhpStorm.
 * User: elkan
 * Date: 14-8-20
 * Time: 下午6:04
 * To change this template use File | Settings | File Templates.
 */
use YHMOrders\SqlMap\Orders;
use YHMOrders\SqlMap\Shopping;

class YHMOrders_Models_Shopping_Dao extends YHMOrders_Dao
{
    private $orderDefaultParameters = array(
        'want_invoice' => 'N', #是否开发票
        'payment_type' => 1,
        'bank_code' => '',
        'is_payed' => 'N',
        'order_status' => 100,
        'is_show' => 'Y',
        'order_remark' => ''
    );

    private $orderParameters = array(
        'order_code',
        'buyer_uid',
        'seller_uid',
        'store_id',
        'order_amount',
        'last_order_amount',
        'shipping_fee',
        'last_shipping_fee',
        'buying_way',
        'payment_id'
    );

    private $ordersGoodsParameters = array(
        'order_code', 'product_skc',
        'product_sku', 'buy_number',
        'goods_type', 'quality_level',
        'sale_price', 'last_price',
        'store_id', 'color_id', 'brand_id'
    );
        private $ordersGoodsInfo = array(
        'goods_images', 'color_name',
         'goods_name', 'small_category_id',  
        'size_name', 'max_category_id',
        'middle_category_id','gender','shipping_fee'
    );

    public function __construct()
    {
        $this->router = 'order.yhm_orders';
        $this->daoObject = $this->dao();
    }

    function create(array $orderData)
    {
        $this->addOrders($orderData);
        foreach ($orderData['goods_list'] as $goodsKey => $goods) {
            $goods['order_code'] = $orderData['order_code'];
            $this->addOrdersGoods($goods);
            $this->addOrdersStatus($goods['order_code'], 100, $orderData['buyer_uid']);
        }
    }

    /**
     * 添加商品
     * @param array $orderData
     * @return int
     */
    public function addOrders(array $orderData)
    {
        $_orderData = array();
        foreach ($this->orderParameters as $key) {
            if (!isset($orderData[$key])) {
                throw new Exception('缺少' . $key . '字段.');
            }
            $_orderData[$key] = $orderData[$key];
        }
        foreach ($this->orderDefaultParameters as $key => $val) {
            if (!isset($orderData[$key]) || empty($orderData[$key])) {
                $_orderData[$key] = $val;
            } else {
                $_orderData[$key] = $orderData[$key];
            }
        }
        return $this->daoObject->insert(Orders\Shopping::INSERT_ORDERS, $_orderData)->lastInsertId();
    }

    /**
     * 添加订单商品
     * @param array $ordersGoods
     * @return int
     * @throws Exception
     */
    public function addOrdersGoods(array $ordersGoods)
    {
        $_ordersGoods = array();
        foreach ($this->ordersGoodsParameters as $goodsKey) {
            if (!isset($ordersGoods[$goodsKey])) {
                throw new Exception('缺少订单商品字段' . $goodsKey);
            }
            $_ordersGoods[$goodsKey] = $ordersGoods[$goodsKey];
        }
        return $this->daoObject->insert(Orders\Shopping::INSERT_ORDERS_GOODS, $_ordersGoods)->lastInsertId();
    }

    /**
     * 插入订单状态
     * @param $orderCode
     * @param $orderStatus
     * @param $triggerUser
     * @return int
     */
    public function addOrdersStatus($orderCode, $orderStatus, $triggerUser)
    {
        $parameter = array(
            'order_code' => $orderCode,
            'order_status' => $orderStatus,
            'trigger_user' => $triggerUser
        );
        return $this->daoObject->insert(Orders\Shopping::INSERT_ORDERS_STATUS, $parameter)->lastInsertId();
    }

    /**
     * 修改价格
     * @param array $package
     * @return int
     * @throws Exception
     */
    public function addShoppingChangePrice(array $package)
    {
        $changeParameters = array(
            'agreement_key', 'buyer_uid', 'store_id', 'seller_uid', 'product_skc', 'sale_price', 'agreement_price', 'buy_number', 'goods_type'
        );
        $parameter = array();
        foreach ($changeParameters as $key) {
            if (empty($package[$key])) {
                throw new Exception('改价异常,缺少字段:' . $key);
            }
            $parameter[$key] = $package[$key];
        }
        return $this->daoObject->insert(Shopping::INSERT_CHANGE_PRICE, $parameter)->lastInsertId();
    }

    /**
     *  修改订单价格
     *  
     * @param array $package
     * @return int
     */
    public function addShoppingOrderChangePrice(array $package)
    {
    	$changeParameters = array(
    			'agreement_key', 'buyer_uid', 'store_id', 'seller_uid', 'product_skc', 'sale_price', 'agreement_price', 'buy_number', 'goods_type',
    			'agreement_shipping_fee','order_shipping_fee','order_amount','order_code','order_last_shipping_fee'
    	);
    	$parameter = array();
    	foreach ($changeParameters as $key) {
    		if (!isset($package[$key])) {
    			throw new Exception('改价异常,缺少字段:' . $key);
    		}
    		$parameter[$key] = $package[$key];
    	}
    	return $this->daoObject->insert(Shopping::INSERT_SHIPPING_ORDER_CHANGE_PRICE, $parameter)->lastInsertId();
    }
    
    /**
     * 获取购物改价
     * 
     * @param $agreementKey
     * @param $buyerUid
     * @return Array
     */
    public function getShoppingChangePrice($agreementKey, $buyerUid)
    {
        $parameter = array(
            'agreement_key' => $agreementKey,
            'buyer_uid' => $buyerUid
        );
        return $this->daoObject->cache(false)->fetchRow(Shopping::SELECT_CHANGE_PRICE_BY_KEY, $parameter);
    }

    /**
     * @param $agreementKey
     * @param $orderCode
     * @param $isBuyer
     * @return int
     */
    public function updateShoppingChange($agreementKey, $orderCode, $isBuyer = 'Y')
    {
        $parameter = array(
            'agreement_key' => $agreementKey,
            'order_code' => $orderCode,
            'is_buyer' => $isBuyer
        );
        return $this->daoObject->update(Shopping::UPDATE_AGREEMENT_STATUS, $parameter)->rowCount();
    }

    /**
     * 获取指定小于状态的购买数量
     * @param $productSku
     * @param $orderStatus
     * @return Array
     */
    public function getBuyGoodsNumber($productSku, $orderStatus = 900)
    {
        $parameter = array(
            'product_sku' => $productSku,
            'order_status' => $orderStatus
        );
        return $this->daoObject->cache(false)->fetchOne(Shopping::SELECT_BUY_GOODS_NUMBER, $parameter);
    }
    
   /**
     * 插入订单镜像表
     * @param $storeID
     * @return mixed
     */
    public function setBuyGoodsInfo($data)
    {
    	$filer =  array_merge($this->ordersGoodsParameters,$this->ordersGoodsInfo);
      	foreach ($filer as $k=>$v)
      	{
         	$parameter[$v]= $data[$v];
      	}
      	return $this->daoObject->insert(Orders\Shopping::INSERT_ORDERS_GOODS_INFO, $parameter)->lastInsertId();
    }

}