购物车生成订单代码欣赏

public function prepareOrderByCart()
    {
        $this->checkLogin();

        if (!$this->validate->validate('data', ['require'])) {
            $this->json->setErr(10005, L('params_format_error'));
            $this->json->Send();
        }

        $key  = C('USER.CART') . $_POST['uid'];

        $cartModel = D('Cart');
        $goodsModel = D('Goods');
        $ordersGoodsModel = D('OrdersGoods');
        $ordersModel = D('Orders');

        $ordersGood = [];
        $price = 0;
        // check has good
        foreach ($_POST['data'] as $k => $v) {
            if (!$cartModel->exists($key, $v)) {
                $this->json->setErr(10047, L('good_not_found'));
                $this->json->Send();
            }

            $count = $cartModel->getCount($key, $v);

            if (!$goodsModel->canUse($v)) {
                $this->json->setErr(10049, L('good_is_disable'));
                $this->json->Send();
            }

            if (!$goodsModel->checkStock($v, $count)) {
                $this->json->setErr(10055, L('under_stock'));
                $this->json->Send();
            }

            $p = $goodsModel->getPrice($v);

            $ordersGood[] = [
                'good_id' => $v,
                'price'   => $p,
                'count'   => $count,
            ];

            $price += $p * $count;
        }

        $totalPrice = $price;

        $shipId = D('UserShipAddress')->where(['uid' => $_POST['uid'], 'is_default' => 1, 'status' => 1])->getField('id');

        $deliverPriceInfo = D('Configs')->getKeys(['delivery', 'no_delivery']);

        if (!$deliverPriceInfo) {
            $this->json->setErr(10063, L('config_not_found'));
            $this->json->Send();
        }

        if ($deliverPriceInfo['no_delivery'] == '-1') {
            $deliverPrice = $deliverPriceInfo['delivery'];
            $price += $deliverPriceInfo['delivery'];
            $isFreeShipping = 0;

        } else {

            if ($price >= $deliverPriceInfo['no_delivery']) { 
                $deliverPrice = 0;
                $isFreeShipping = 1;
            } else {
                $deliverPrice = $deliverPriceInfo['delivery'];
                $price += $deliverPrice;
                $isFreeShipping = 0;
            }
        }

        $data = [
            'uid'         => $_POST['uid'],
            'price'       => $price, 
            'total_price' => $totalPrice,
            'create_time' => time(),
            'update_time' => time(),
            'expired_time'=> time() + C('ORDER_EXPIRED'),
            'ship_address_id' => $shipId ?: 0,
            'is_free_shipping' => $isFreeShipping,
            'deliver_price' => $deliverPrice,
            'is_cart'     => 1,
            'source'      => $_POST['source'],
        ];

        $id = D('Orders')->add($data);

        if (!$id) {
            $this->json->setErr(10041, L('add_error'));
            $this->json->Send();
        }

        $ordersModel->startTrans();

        foreach ($ordersGood as $k => $v) {
            $v['create_time'] = time();
            $v['update_time'] = time();
            $v['order_id'] = $id;
            // activity_id
            $v['activity_id'] = $goodsModel->where(['id' => $v['good_id']])->getField('activity_id');
            $v['good_type'] = $goodsModel->where(['id' => $v['good_id']])->getField('type');

            $ordersGoodsModel->add($v);

            // reduce stock

            if (!$goodsModel->reduceStock($v['good_id'], $v['count'])) {
                $ordersModel->rollback();
                // delete all order
                $ordersModel->where(['id' => $id])->delete();
                $ordersGoodsModel->where(['order_id' => $id])->delete();

                $this->json->setErr(10055, L('under_stock'));
                $this->json->Send();
            }

        }
        $ordersModel->commit();
        $this->json->setAttr("data", $id);
        $this->json->Send();
    }

完美的代码。

猜你喜欢

转载自www.cnblogs.com/jiqing9006/p/9083547.html