123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
- namespace addons\shopro\library\traits;
- use addons\shopro\exception\Exception;
- use addons\shopro\model\ActivityGoodsSkuPrice;
- use addons\shopro\model\Goods;
- use addons\shopro\model\GoodsSkuPrice;
- use addons\shopro\model\OrderItem;
- use addons\shopro\model\ScoreGoodsSkuPrice;
- trait StockSale
- {
- use StockWarning;
-
- public function cacheForwardSale($goods_list) {
- try {
-
- $break_key = -1;
- foreach ($goods_list as $key => $goods) {
- $detail = $goods['detail'];
- $activity = $detail['activity'];
-
- if (!$activity || !in_array($activity['type'], ['seckill', 'groupon']) || !$this->hasRedis()) {
- continue;
- }
-
- $redis = $this->getRedis();
- $keys = $this->getKeys([
- 'goods_id' => $detail['id'],
- 'goods_sku_price_id' => $detail['current_sku_price']['id'],
- ], [
- 'activity_id' => $activity['id'],
- 'activity_type' => $activity['type'],
- ]);
- extract($keys);
-
-
- $goodsSkuPrice = $redis->HGET($activityHashKey, $goodsSkuPriceKey);
- $goodsSkuPrice = json_decode($goodsSkuPrice, true);
-
- $stock = $goodsSkuPrice['stock'] ?? 0;
-
- $sale = $redis->HINCRBY($activityHashKey, $saleKey, $goods['goods_num']);
- if ($sale > $stock) {
-
- $break_key = $key;
- throw new \Exception('活动商品库存不足');
- }
- }
- } catch (\Exception $e) {
-
- if ($break_key >= 0) {
- foreach ($goods_list as $key => $goods) {
- if ($key > $break_key) {
- break;
- }
- $detail = $goods['detail'];
- $activity = $detail['activity'];
-
- if (!$activity || !in_array($activity['type'], ['seckill', 'groupon']) || !$this->hasRedis()) {
- continue;
- }
-
- $redis = $this->getRedis();
- $keys = $this->getKeys([
- 'goods_id' => $detail['id'],
- 'goods_sku_price_id' => $detail['current_sku_price']['id'],
- ], [
- 'activity_id' => $activity['id'],
- 'activity_type' => $activity['type'],
- ]);
- extract($keys);
- if ($redis->EXISTS($activityHashKey) && $redis->HEXISTS($activityHashKey, $saleKey)) {
- $sale = $redis->HINCRBY($activityHashKey, $saleKey, -$goods['goods_num']);
- }
- }
-
- new Exception('商品库存不足');
- }
- new Exception($e->getMessage());
- }
- }
-
- public function cacheBackSale($order) {
- $items = OrderItem::where('order_id', $order['id'])->select();
- foreach ($items as $key => $item) {
- $this->cacheBackSaleByItem($item);
- }
- }
-
- public function realForwardStockSale($order) {
- $items = OrderItem::where('order_id', $order['id'])->select();
- foreach ($items as $key => $orderItem) {
-
- Goods::where('id', $orderItem->goods_id)->setInc('sales', $orderItem->goods_num);
- $goodsSkuPrice = GoodsSkuPrice::where('id', $orderItem->goods_sku_price_id)->find();
- if ($goodsSkuPrice) {
- $goodsSkuPrice->setDec('stock', $orderItem->goods_num);
- $goodsSkuPrice->setInc('sales', $orderItem->goods_num);
-
- $this->checkStockWarning($goodsSkuPrice);
- }
- if ($orderItem->item_goods_sku_price_id) {
- if ($order['type'] == 'score') {
-
- $itemGoodsSkuPrice = ScoreGoodsSkuPrice::where('id', $orderItem->item_goods_sku_price_id)->find();
- } else {
-
- $itemGoodsSkuPrice = ActivityGoodsSkuPrice::where('id', $orderItem->item_goods_sku_price_id)->find();
- }
-
- if ($itemGoodsSkuPrice) {
- $itemGoodsSkuPrice->setDec('stock', $orderItem->goods_num);
- $itemGoodsSkuPrice->setInc('sales', $orderItem->goods_num);
- }
- }
-
-
- }
- }
-
- public function realBackStockSale($order)
- {
- $items = OrderItem::where('order_id', $order['id'])->select();
- foreach ($items as $key => $orderItem) {
-
- Goods::where('id', $orderItem->goods_id)->setDec('sales', $orderItem->goods_num);
-
- $goodsSkuPrice = GoodsSkuPrice::where('id', $orderItem->goods_sku_price_id)->find();
- if ($goodsSkuPrice) {
- $goodsSkuPrice->setInc('stock', $orderItem->goods_num);
- $goodsSkuPrice->setDec('sales', $orderItem->goods_num);
- }
- if ($orderItem->item_goods_sku_price_id) {
- if ($order['type'] == 'score') {
-
- $itemGoodsSkuPrice = ScoreGoodsSkuPrice::where('id', $orderItem->item_goods_sku_price_id)->find();
- } else {
- $itemGoodsSkuPrice = ActivityGoodsSkuPrice::where('id', $orderItem->item_goods_sku_price_id)->find();
- }
- if ($itemGoodsSkuPrice) {
- $itemGoodsSkuPrice->setInc('stock', $orderItem->goods_num);
- $itemGoodsSkuPrice->setDec('sales', $orderItem->goods_num);
- }
- }
- }
- }
-
- private function cacheBackSaleByItem($item)
- {
-
- if ((strpos($item['activity_type'], 'groupon') === false && strpos($item['activity_type'], 'seckill') === false) || !$this->hasRedis()) {
- return false;
- }
-
- $redis = $this->getRedis();
- $keys = $this->getKeys([
- 'goods_id' => $item['goods_id'],
- 'goods_sku_price_id' => $item['goods_sku_price_id'],
- ], [
- 'activity_id' => $item['activity_id'],
- 'activity_type' => $item['activity_type'],
- ]);
- extract($keys);
- if ($redis->EXISTS($activityHashKey) && $redis->HEXISTS($activityHashKey, $saleKey)) {
- $sale = $redis->HINCRBY($activityHashKey, $saleKey, -$item['goods_num']);
- }
- return true;
- }
- }
|