diff --git a/app/common/repositories/store/order/StoreOrderCreateRepository.php b/app/common/repositories/store/order/StoreOrderCreateRepository.php index 9499cde..107ad6b 100644 --- a/app/common/repositories/store/order/StoreOrderCreateRepository.php +++ b/app/common/repositories/store/order/StoreOrderCreateRepository.php @@ -2,6 +2,7 @@ namespace app\common\repositories\store\order; +use app\common\model\store\product\Product; use app\common\model\user\ExchangeIntegralRecord; use app\common\model\user\ExchangeQuotaRecord; use app\common\model\user\User; @@ -1323,47 +1324,23 @@ class StoreOrderCreateRepository extends StoreOrderRepository{ ]); $user->save(); } - // 用户兑换酒道馆商品相关操作 - if($groupOrder['activity_type'] == 36){ - // 减少酒水卡积分 - if($groupOrder['exchange_integral'] > 0){ - $userInfo = User::where('uid',$user['uid'])->findOrEmpty(); - $integralChangeFront = (float)$userInfo->exchange_integral; - $userInfo->exchange_integral -= (float)$groupOrder['exchange_integral']; - $userInfo->exchange_integral = $userInfo->exchange_integral > 0 ? $userInfo->exchange_integral : 0; - $userInfo->save(); - ExchangeIntegralRecord::insert([ - 'uid' => $user['uid'], - 'product_id' => 0, - 'order_id' => 0, - 'order_product_id' => 0, - 'change_type' => 0, - 'change_quantity' => (float)$groupOrder['exchange_integral'], - 'change_front' => $integralChangeFront, - 'change_after' => (float)$userInfo->exchange_integral, - 'remark' => "兑换消费", - ]); - } - // 变更额度 - $userHoldInfo = app()->make(ExchangeQuotaRepository::class) - ->searchModel(['uid'=>$user['uid'],'quota_type'=>1]) - ->findOrEmpty(); - $changeFront = (float)$userHoldInfo->surplus_quota; - $userHoldInfo->use_quota += (float)$groupOrder['total_price']; - $userHoldInfo->surplus_quota -= (float)$groupOrder['total_price']; - $userHoldInfo->save(); - ExchangeQuotaRecord::insert([ + // 用户兑换酒道馆商品 减少酒水卡积分 + if($groupOrder['activity_type'] == 36 && $groupOrder['exchange_integral'] > 0){ + $userInfo = User::where('uid',$user['uid'])->findOrEmpty(); + $integralChangeFront = (float)$userInfo->exchange_integral; + $userInfo->exchange_integral -= (float)$groupOrder['exchange_integral']; + $userInfo->exchange_integral = $userInfo->exchange_integral > 0 ? $userInfo->exchange_integral : 0; + $userInfo->save(); + ExchangeIntegralRecord::insert([ 'uid' => $user['uid'], 'product_id' => 0, 'order_id' => 0, 'order_product_id' => 0, 'change_type' => 0, - 'change_quantity' => (float)$groupOrder['total_price'], - 'change_front' => $changeFront, - 'change_after' => (float)$userHoldInfo->surplus_quota, + 'change_quantity' => (float)$groupOrder['exchange_integral'], + 'change_front' => $integralChangeFront, + 'change_after' => (float)$userInfo->exchange_integral, 'remark' => "兑换消费", - 'source' => 2, - 'quota_type' => 1, ]); } @@ -1502,11 +1479,39 @@ class StoreOrderCreateRepository extends StoreOrderRepository{ // 进货订单 - 补货额度抵扣金额 'mer_quota_deduction' => $cart['mer_quota_deduction'] ?? 0, ]; + // 用户兑换商品 变更相关额度 + if($cart['product_type'] == 36){ + // 酒类型:0=未知,1=瓶装酒,2=封坛酒 + $wineType = (int)Product::where('product_id', $cart['product_id'])->value('wine_type'); + if($wineType > 0){ + // 额度类型:1=酒卡额度(瓶装酒),2=菜卡额度,3=封坛酒额度,4=加油卡额度 + $quotaType = $wineType == 2 ? 3 : 1; + + $userHoldInfo = app()->make(ExchangeQuotaRepository::class) + ->searchModel(['uid'=>$user['uid'],'quota_type'=>$quotaType]) + ->findOrEmpty(); + $changeFront = (float)$userHoldInfo->surplus_quota; + $userHoldInfo->use_quota += (float)$cart['total_price']; + $userHoldInfo->surplus_quota -= (float)$cart['total_price']; + $userHoldInfo->save(); + ExchangeQuotaRecord::insert([ + 'uid' => $user['uid'], + 'product_id' => 0, + 'order_id' => 0, + 'order_product_id' => 0, + 'change_type' => 0, + 'change_quantity' => (float)$cart['total_price'], + 'change_front' => $changeFront, + 'change_after' => (float)$userHoldInfo->surplus_quota, + 'remark' => "兑换消费", + 'source' => 2, + 'quota_type' => $quotaType, + ]); + } + } } $userMerchantRepository->getInfo($uid,$order['mer_id']); - app() - ->make(MerchantRepository::class) - ->incSales($order['mer_id'],$order['total_num']); + app()->make(MerchantRepository::class)->incSales($order['mer_id'],$order['total_num']); } if(count($bills) > 0){ app() diff --git a/app/common/repositories/store/product/ProductRepository.php b/app/common/repositories/store/product/ProductRepository.php index d3db7a7..6f19621 100644 --- a/app/common/repositories/store/product/ProductRepository.php +++ b/app/common/repositories/store/product/ProductRepository.php @@ -2138,8 +2138,10 @@ class ProductRepository extends BaseRepository $productType = 35;// 酒道馆进货商品 if($merchantType == 1) $productType = 36;// 用户兑换酒道馆商品 $count = $storeOrderRepository->getShopMaxCountNumber($merId,$product['product_id'], $productType); - if (($data['cart_num'] + $count) > $product['once_max_count']){ - throw new ValidateException('[超出限购总数:'. $product['once_max_count'].']'.mb_substr($product['store_name'],0,10).'...'); + $sumCount = $data['cart_num'] + $count; + if ($sumCount > $product['once_max_count']){ + throw new ValidateException("超出限购总数!本次购买数量:{$data['cart_num']},已购买数量:{$count},合计:{$sumCount};限购:{$product['once_max_count']}"); + // throw new ValidateException('[超出限购总数:'. $product['once_max_count'].']'.mb_substr($product['store_name'],0,10).'...'); } } if ($product['type'] && !$data['is_new']) throw new ValidateException('虚拟商品不可加入购物车'); @@ -2617,6 +2619,7 @@ class ProductRepository extends BaseRepository ->where('Merchant.status', 1) ->where('Merchant.mer_state', 1) ->where('Merchant.merchant_type', 1) + ->where('Product.stock', '>', 0) ->whereIn('Product.wine_type', [1,2]) ->when(isset($search['store_name']) && $search['store_name'] !== '', function ($query) use ($search) { $query->whereLike('Product.store_name', "%{$search['store_name']}%"); @@ -2642,7 +2645,8 @@ class ProductRepository extends BaseRepository 'Product.batch_unit', 'Product.image', 'Product.stock', - 'Product.once_min_count' + 'Product.once_min_count', + 'Product.wine_type' ]; // 经纬度 $lng = $search['lng'] ?? 0; diff --git a/app/common/repositories/user/ExchangeQuotaRepository.php b/app/common/repositories/user/ExchangeQuotaRepository.php index b9c3b75..2f6aa8e 100644 --- a/app/common/repositories/user/ExchangeQuotaRepository.php +++ b/app/common/repositories/user/ExchangeQuotaRepository.php @@ -138,6 +138,30 @@ class ExchangeQuotaRepository extends BaseRepository{ ]); }); } + /** + * Common: 获取信息 + * Author: wu-hui + * Time: 2024/06/04 10:42 + * @param int $uid + * @param int $quotaType 额度类型:1=酒卡额度(瓶装酒),2=菜卡额度,3=封坛酒额度,4=加油卡额度 + * @return mixed + */ + public function getInfo(int $uid,int $quotaType = 1){ + $info = $this->searchModel([ + 'uid' => $uid, + 'quota_type' => $quotaType, + ]) + ->field('id,uid,total_quota,use_quota,surplus_quota,freeze_quota,quota_type') + ->findOrEmpty() + ->toArray(); + if($info){ + // 计算可用额度 + $info['available_quota'] = (float)sprintf("%.2f", $info['surplus_quota'] - $info['freeze_quota']); + } + + return $info; + } + diff --git a/app/controller/api/Auth.php b/app/controller/api/Auth.php index e2394f0..928a7a0 100644 --- a/app/controller/api/Auth.php +++ b/app/controller/api/Auth.php @@ -209,18 +209,21 @@ class Auth extends BaseController $agentCount = app()->make(AgentRepository::class)->getSearchModel(['uid'=>$user->uid])->count(); $data['is_agent'] = $agentCount > 0; // 用户持有全部额度(冻结中 + 可用额度) - $data['available'] = (float)app()->make(ExchangeQuotaRepository::class) - ->searchModel(['uid'=>$user->uid]) - ->value('surplus_quota'); - $data['vegetable_available'] = (float)app()->make(ExchangeQuotaRepository::class) - ->searchModel(['uid'=>$user->uid,'quota_type'=>2]) - ->value('surplus_quota'); - $data['wine_available'] = (float)app()->make(ExchangeQuotaRepository::class) - ->searchModel(['uid'=>$user->uid,'quota_type'=>3]) - ->value('surplus_quota'); - $data['oil_available'] = (float)app()->make(ExchangeQuotaRepository::class) - ->searchModel(['uid'=>$user->uid,'quota_type'=>4]) - ->value('surplus_quota'); + $quotaInfo = app()->make(ExchangeQuotaRepository::class)->getInfo((int)$user->uid); + $data['available'] = $quotaInfo['surplus_quota'] ?? '0.00'; + $data['available_quota'] = $quotaInfo['available_quota'] ?? '0.00'; + + $vegetableQuotaInfo = app()->make(ExchangeQuotaRepository::class)->getInfo((int)$user->uid, (int)2); + $data['vegetable_available'] = $vegetableQuotaInfo['surplus_quota'] ?? '0.00'; + $data['vegetable_available_quota'] = $vegetableQuotaInfo['available_quota'] ?? '0.00'; + + $wineQuotaInfo = app()->make(ExchangeQuotaRepository::class)->getInfo((int)$user->uid, (int)3); + $data['wine_available'] = $wineQuotaInfo['surplus_quota'] ?? '0.00'; + $data['wine_available_quota'] = $wineQuotaInfo['available_quota'] ?? '0.00'; + + $oilQuotaInfo = app()->make(ExchangeQuotaRepository::class)->getInfo((int)$user->uid, (int)4); + $data['oil_available'] = $oilQuotaInfo['surplus_quota'] ?? '0.00'; + $data['oil_available_quota'] = $oilQuotaInfo['available_quota'] ?? '0.00'; return app('json')->success($data); } diff --git a/app/controller/api/Wine.php b/app/controller/api/Wine.php index b9c8fad..9cdbdcf 100644 --- a/app/controller/api/Wine.php +++ b/app/controller/api/Wine.php @@ -78,7 +78,7 @@ class Wine extends BaseController{ $query->field(['product_id','detail','image','price','sku','unique','value_id', 'stock']); }, 'product' => function($query){ - $query->field(['product_id','store_name','unit_name','is_batch','batch_num','batch_unit']); + $query->field(['product_id','store_name','unit_name','is_batch','batch_num','batch_unit', 'wine_type']); } ]) ->select() diff --git a/app/listener/exchangeQuota/OrderTakeEvent.php b/app/listener/exchangeQuota/OrderTakeEvent.php index 85eed4f..3b35e7f 100644 --- a/app/listener/exchangeQuota/OrderTakeEvent.php +++ b/app/listener/exchangeQuota/OrderTakeEvent.php @@ -36,10 +36,13 @@ class OrderTakeEvent{ // 酒卡额度解冻 $this->quotaThawing($order); + // 菜卡额度解冻 + $this->vegetableQuotaThawing($order); + // 封坛酒额度解冻 + $this->wineQuotaThawing($order); // 商户佣金结算 $this->merMoneyHandle($order->order_id); - - + } }catch(\Exception $e){ if($order->activity_type == 35){ @@ -74,6 +77,58 @@ class OrderTakeEvent{ $hold->save(); } } + // 菜卡额度解冻 + private function vegetableQuotaThawing($order){ + # 获取变更记录 条件:order_id=当前订单id、变更类型=增加 + $sum = (float)app()->make(ExchangeQuotaRecordRepository::class) + ->searchModel([ + 'order_id' => $order->order_id, + 'change_type' => 1, + 'source' => 0,// 仅查询购买赠送 进行解冻 + 'quota_type' => 2, + ])->sum('change_quantity'); + $refundSum = (float)app()->make(ExchangeQuotaRecordRepository::class) + ->searchModel([ + 'order_id' => $order->order_id, + 'change_type' => 0, + 'source' => 1,// 仅查询订单退款 减少内容 + 'quota_type' => 2, + ])->sum('change_quantity'); + // 剩余数量 解冻 + $surplusQuota = (float)sprintf("%.2f",$sum - $refundSum); + if($surplusQuota > 0){ + $hold = app()->make(ExchangeQuotaRepository::class)->searchModel(['uid'=>$order->uid,'quota_type'=>2])->findOrEmpty(); + $freezeQuota = sprintf("%.2f",$hold->freeze_quota - $surplusQuota); + $hold->freeze_quota = $freezeQuota < 0 ? 0 : $freezeQuota; + $hold->save(); + } + } + // 封坛酒额度解冻 + private function wineQuotaThawing($order){ + # 获取变更记录 条件:order_id=当前订单id、变更类型=增加 + $sum = (float)app()->make(ExchangeQuotaRecordRepository::class) + ->searchModel([ + 'order_id' => $order->order_id, + 'change_type' => 1, + 'source' => 0,// 仅查询购买赠送 进行解冻 + 'quota_type' => 3, + ])->sum('change_quantity'); + $refundSum = (float)app()->make(ExchangeQuotaRecordRepository::class) + ->searchModel([ + 'order_id' => $order->order_id, + 'change_type' => 0, + 'source' => 1,// 仅查询订单退款 减少内容 + 'quota_type' => 3, + ])->sum('change_quantity'); + // 剩余数量 解冻 + $surplusQuota = (float)sprintf("%.2f",$sum - $refundSum); + if($surplusQuota > 0){ + $hold = app()->make(ExchangeQuotaRepository::class)->searchModel(['uid'=>$order->uid,'quota_type'=>3])->findOrEmpty(); + $freezeQuota = sprintf("%.2f",$hold->freeze_quota - $surplusQuota); + $hold->freeze_quota = $freezeQuota < 0 ? 0 : $freezeQuota; + $hold->save(); + } + } // 商户佣金结算 private function merMoneyHandle($order_id){ $list = FinancialRecord::getDB() diff --git a/app/listener/exchangeQuota/OrderVerifyEvent.php b/app/listener/exchangeQuota/OrderVerifyEvent.php index 03d0ad1..6ab156a 100644 --- a/app/listener/exchangeQuota/OrderVerifyEvent.php +++ b/app/listener/exchangeQuota/OrderVerifyEvent.php @@ -27,6 +27,8 @@ class OrderVerifyEvent{ $this->wineCard($orderId, $order); // 菜卡额度解冻 $this->dish($orderId, $order); + // 封坛酒额度解冻 + $this->windQuota($orderId, $order); // 商户佣金结算 $this->merMoneyHandle($orderId); @@ -79,7 +81,7 @@ class OrderVerifyEvent{ Log::info('订单核销 - 酒卡额度相关处理 - 错误: '.$e->getMessage()); } } - // 酒水卡额度解冻 + // 菜卡额度解冻 public function dish($orderId, $order){ try{ # 获取变更记录 条件:order_id=当前订单id、变更类型=增加 @@ -114,6 +116,42 @@ class OrderVerifyEvent{ Log::info('订单核销 - 菜卡额度相关处理 - 错误: '.$e->getMessage()); } } + // 封坛酒额度解冻 + public function windQuota($orderId, $order){ + try{ + # 获取变更记录 条件:order_id=当前订单id、变更类型=增加 + $sum = (float)app()->make(ExchangeQuotaRecordRepository::class)->searchModel([ + 'order_id' => $orderId, + 'change_type' => 1, + 'source' => 0,// 仅查询购买赠送 进行解冻 + 'quota_type' => 3, + ])->sum('change_quantity'); + $refundSum = (float)app()->make(ExchangeQuotaRecordRepository::class) + ->searchModel([ + 'order_id' => $orderId, + 'change_type' => 0, + 'source' => 1,// 仅查询订单退款 减少内容 + 'quota_type' => 3, + ])->sum('change_quantity'); + // 剩余数量 解冻 + $surplusQuota = (float)sprintf("%.2f",$sum - $refundSum); + if($surplusQuota > 0){ + // 核销订单 会进行拆分操作 需要获取当前订单占总订单数量的比例 + $totalNum = (float)$this->getRate($orderId); + $currentTotalNum = (float)$order->total_num; + $rate = (float)sprintf("%.2f",$currentTotalNum / $totalNum * 100); + $surplusQuota = (float)sprintf("%.2f",$surplusQuota * $rate / 100); + // 解冻操作 + $hold = app()->make(ExchangeQuotaRepository::class)->searchModel(['uid'=>$order->uid,'quota_type'=>3])->findOrEmpty(); + $freezeQuota = sprintf("%.2f",$hold->freeze_quota - $surplusQuota); + $hold->freeze_quota = $freezeQuota < 0 ? 0 : $freezeQuota; + $hold->save(); + } + }catch(\Exception $e){ + Log::info('订单核销 - 菜卡额度相关处理 - 错误: '.$e->getMessage()); + } + } + // 商户佣金结算 private function merMoneyHandle($order_id){ $list = FinancialRecord::getDB()