123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- namespace addons\unishop\behavior;
- use addons\unishop\extend\Hashids;
- use addons\unishop\extend\Redis;
- use addons\unishop\model\Address;
- use addons\unishop\model\Config;
- use addons\unishop\model\DeliveryRule as DeliveryRuleModel;
- use addons\unishop\model\FlashProduct;
- use addons\unishop\model\FlashSale;
- use addons\unishop\model\Product;
- use app\admin\model\unishop\Coupon;
- use think\Db;
- use think\Exception;
- class OrderFlash
- {
-
- public function createOrderBefore(&$params, $extra)
- {
-
- $numbers = explode(',', $extra['number']);
- $redis = new Redis();
- $totalNumber = $redis->handler->hGet('flash_sale_' . $extra['flash_id'] . '_' . $extra['product_id'], 'number');
- $totalSold = $redis->handler->hGet('flash_sale_' . $extra['flash_id'] . '_' . $extra['product_id'], 'sold');
- $switch = $redis->handler->hGet('flash_sale_' . $extra['flash_id'] . '_' . $extra['product_id'], 'switch');
- $starttime = $redis->handler->hGet('flash_sale_' . $extra['flash_id'] . '_' . $extra['product_id'], 'starttime');
- $endtime = $redis->handler->hGet('flash_sale_' . $extra['flash_id'] . '_' . $extra['product_id'], 'endtime');
-
- if (time() < $starttime) {
- $this->error(__('Activity not started'));
- }
- if ($endtime < time()) {
- $this->error(__('Activity ended'));
- }
-
- if ($totalSold >= $totalNumber) {
- throw new Exception(__('Item sold out'));
- }
- if ($switch == FlashSale::SWITCH_NO) {
- throw new Exception(__('Item is off the shelves'));
- }
-
- $products = Db::name('unishop_product')->where(['id' => $extra['product_id']])->select();
- if (!$products) {
- throw new Exception(__('Product not exist'));
- }
- $specs = explode(',', $extra['spec']);
- foreach ($specs as &$spec) {
- $spec = str_replace('|', ',', $spec);
- }
-
- $delivery = (new DeliveryRuleModel())->cityInScopeOfDelivery($extra['city_id'], $extra['delivery_id']);
- if (!$delivery) {
- throw new Exception(__('Your receiving address is not within the scope of delivery'));
- } else {
- if ($delivery['min'] > array_sum($numbers)) {
- throw new Exception(__('You must purchase at least %s item to use this shipping method', $delivery['min']));
- }
- }
- $address = (new Address)->where(['id' => $extra['address_id'], 'user_id' => $extra['userId']])->find();
- if (!$address) {
- throw new Exception(__('Address not exist'));
- }
-
- $orderPrice = 0;
- foreach ($products as $key => $product) {
- $productInfo = (new \addons\unishop\extend\Product())->getBaseData($product, $specs[$key] ? $specs[$key] : '');
- $sold = $redis->handler->hIncrBy('flash_sale_' . $extra['flash_id'] . '_' . $extra['product_id'], 'sold', $numbers[$key]);
- if ($totalNumber < $sold) {
- $redis->handler->hIncrBy('flash_sale_' . $extra['flash_id'] . '_' . $extra['product_id'], 'sold', -$numbers[$key]);
- throw new Exception(__('Insufficient inventory,%s pieces left', $totalNumber - $totalSold));
- }
- $orderPrice = bcadd($orderPrice, bcmul($productInfo['sales_price'], $numbers[$key], 2), 2);
- $baseProductInfo[] = $productInfo;
- }
-
- $coupon = [];
- $params = [$products, $delivery, $coupon, $baseProductInfo, $address, $orderPrice, $specs, $numbers];
- }
-
- public function createOrderAfter(&$params, $extra)
- {
-
- (new FlashProduct)->where(['flash_id' => $extra['flash_id'], 'product_id' => $extra['product_id']])->setInc('sold', $extra['number']);
-
- (new Product)->where(['id' => $extra['product_id']])->setInc('no_buy_yet', $extra['number']);
- }
- }
|