diff --git a/app/common/repositories/store/order/StoreGroupOrderRepository.php b/app/common/repositories/store/order/StoreGroupOrderRepository.php index 013a698..28b0b26 100644 --- a/app/common/repositories/store/order/StoreGroupOrderRepository.php +++ b/app/common/repositories/store/order/StoreGroupOrderRepository.php @@ -157,6 +157,9 @@ class StoreGroupOrderRepository extends BaseRepository 'nickname' => $uid ? $order->user->nickname : '系统', 'user_type' => $storeOrderStatusRepository::U_TYPE_SYSTEM, ]; + + //订单取消事件 + event('order.cancel', compact('order')); } $groupOrder->save(); $storeOrderStatusRepository->batchCreateLog($orderStatus); diff --git a/app/listener/exchangeQuota/OrderCancelEvent.php b/app/listener/exchangeQuota/OrderCancelEvent.php index 3bb0f8b..ca9e95b 100644 --- a/app/listener/exchangeQuota/OrderCancelEvent.php +++ b/app/listener/exchangeQuota/OrderCancelEvent.php @@ -11,34 +11,230 @@ use app\common\repositories\user\ExchangeQuotaRepository; use think\facade\Log; class OrderCancelEvent{ - private $groupOrder; + private $order; public function handle($params){ - Log::info('订单取消 - 开始: '.var_export($params,1)); - // $this->groupOrder = $params['order'] ?? []; + $this->order = $params['order'] ?? []; + // Log::info('订单取消 - 开始: '.var_export($this->order,1)); try{ // 根据订单类型进行对应的处理 - // switch((int)$this->groupOrder->activity_type){ - // // 酒道馆和小酒馆兑换订单 - // case 36:$this->quotaAndIntegralHandle();break; - // // 进货订单 - // case 35:$this->merQuotaHandle();break; - // } - - + switch((int)$this->order->activity_type){ + // 酒道馆和小酒馆兑换订单 + case 36:$this->quotaAndIntegralHandle();break; + // 进货订单 + case 35:$this->merQuotaHandle();break; + } }catch(\Exception $e){ $data = [ - // 'uid' => $this->groupOrder->uid, - // 'group_order_id' => $this->groupOrder->group_order_id, + 'uid' => $this->order->uid, + 'order_id' => $this->order->order_id, 'msg' => $e->getMessage() ]; Log::info('订单取消 - 错误: '.var_export($data,1)); } } + // 获取订单信息 + private function getOrderInfo($field = '*', $orderProductField = '*'){ + return app()->make(StoreOrderRepository::class) + ->getSearch(['order_id' => $this->order->order_id]) + ->field($field) + ->with([ + 'orderProduct' => function($query) use ($orderProductField){ + return $query->field($orderProductField)->with(['product'=>function($sql){ + $sql->field('product_id,wine_type')->bind(['wine_type']); + }]); + }, + 'merchant' => function($query){ + return $query->field('mer_id,mer_name,merchant_type,merchant_sub_type,shop_mer_id'); + }, + ]) + ->findOrEmpty() + ->toArray(); + } + // 酒道馆和小酒馆兑换订单 + private function quotaAndIntegralHandle(){ + // 获取订单列表 + $orderField = 'order_id,group_order_id,uid,mer_id'; + $orderProductField = [ + 'uid', + 'order_product_id', + 'order_id', + 'product_id', + 'total_price', + 'exchange_integral', + 'exchange_integral_price', + 'wine_diff_money', + 'wine_diff_money_price', + 'quota_integral', + 'quota_integral_price', + 'quota_integral_diff', + 'quota_integral_diff_money' + ]; + $orderInfo = $this->getOrderInfo($orderField, $orderProductField); + // 用户持有酒水卡积分 + $userInfo = User::where('uid', $this->order->uid)->findOrEmpty(); + $holdQuotaInfo = app()->make(ExchangeQuotaRepository::class)->getHoldInfo($this->order->uid, 1);// 酒卡额度 + $holdWineQuotaInfo = app()->make(ExchangeQuotaRepository::class)->getHoldInfo($this->order->uid, 3);// 封坛酒额度 + $merInfo = $orderInfo['merchant'] ?? []; + $quotaIntegralHoldInfo = app()->make(ExchangeQuotaRepository::class)->getHoldInfo($orderInfo['uid'], 5, $merInfo['shop_mer_id']); + // 处理 + $exchangeIntegralRecordData = []; + $exchangeQuotaRecordData = []; + // 循环订单商品 + foreach($orderInfo['orderProduct'] as $productInfo){ + if($merInfo['merchant_sub_type'] == 2 && $productInfo['quota_integral'] > 0){ + // 小酒馆 减少惠民积分 + $changeFront = (float)$quotaIntegralHoldInfo->surplus_quota; + $quotaIntegralHoldInfo->use_quota -= (float)$productInfo['quota_integral']; + $quotaIntegralHoldInfo->surplus_quota += (float)$productInfo['quota_integral']; + $exchangeQuotaRecordData[] = [ + 'uid' => $productInfo['uid'], + 'product_id' => $productInfo['product_id'], + 'order_id' => $productInfo['order_id'], + 'order_product_id' => $productInfo['order_product_id'], + 'change_type' => 1, + 'change_quantity' => (float)$productInfo['quota_integral'], + 'change_front' => $changeFront, + 'change_after' => (float)$quotaIntegralHoldInfo->surplus_quota, + 'mer_id' => $orderInfo['mer_id'], + 'remark' => '订单超时未支付自动取消返还', + 'quota_type' => 5, + 'source' => 1, + ]; + } + else if($merInfo['merchant_sub_type'] != 2){ + // 酒道馆 减少酒水卡积分 + if($productInfo['exchange_integral'] > 0){ + $integralChangeFront = (float)$userInfo->exchange_integral; + $userInfo->exchange_integral += (float)$productInfo['exchange_integral']; + $userInfo->exchange_integral = $userInfo->exchange_integral > 0 ? $userInfo->exchange_integral : 0; + $exchangeIntegralRecordData[] = [ + 'uid' => $productInfo['uid'], + 'product_id' => $productInfo['product_id'], + 'order_id' => $productInfo['order_id'], + 'order_product_id' => $productInfo['order_product_id'], + 'change_type' => 1, + 'change_quantity' => (float)$productInfo['exchange_integral'], + 'change_front' => $integralChangeFront, + 'change_after' => (float)$userInfo->exchange_integral, + 'remark' => "订单超时未支付自动取消返还", + ]; + } + // 酒卡额度处理 酒类型:0=未知,1=瓶装酒,2=封坛酒 + $wineType = (int)$productInfo['wine_type'] ?? 1; + if($wineType == 1){ + // 瓶装酒额度变更 + $changeFront = (float)$holdQuotaInfo->surplus_quota; + $holdQuotaInfo->use_quota -= (float)$productInfo['total_price']; + $holdQuotaInfo->surplus_quota += (float)$productInfo['total_price']; + $exchangeQuotaRecordData[] = [ + 'uid' => $productInfo['uid'], + 'product_id' => $productInfo['product_id'], + 'order_id' => $productInfo['order_id'], + 'order_product_id' => $productInfo['order_product_id'], + 'change_type' => 1, + 'change_quantity' => (float)$productInfo['total_price'], + 'change_front' => $changeFront, + 'change_after' => (float)$quotaIntegralHoldInfo->surplus_quota, + 'mer_id' => $orderInfo['mer_id'], + 'remark' => '订单超时未支付自动取消返还', + 'quota_type' => 1, + 'source' => 1, + ]; + } + else if($wineType == 2){ + // 封坛酒额度变更 + $changeFront = (float)$holdWineQuotaInfo->surplus_quota; + $holdWineQuotaInfo->use_quota -= (float)$productInfo['total_price']; + $holdWineQuotaInfo->surplus_quota += (float)$productInfo['total_price']; + $exchangeQuotaRecordData[] = [ + 'uid' => $productInfo['uid'], + 'product_id' => $productInfo['product_id'], + 'order_id' => $productInfo['order_id'], + 'order_product_id' => $productInfo['order_product_id'], + 'change_type' => 1, + 'change_quantity' => (float)$productInfo['total_price'], + 'change_front' => $changeFront, + 'change_after' => (float)$quotaIntegralHoldInfo->surplus_quota, + 'mer_id' => $orderInfo['mer_id'], + 'remark' => '订单超时未支付自动取消返还', + 'quota_type' => 3, + 'source' => 1, + ]; + } + } + } + // 保存用户修改 + $quotaIntegralHoldInfo->save(); + $userInfo->save(); + $holdQuotaInfo->save(); + $holdWineQuotaInfo->save(); + if(count($exchangeIntegralRecordData) > 0) ExchangeIntegralRecord::insertAll($exchangeIntegralRecordData); + if(count($exchangeQuotaRecordData) > 0) ExchangeQuotaRecord::insertAll($exchangeQuotaRecordData); + return true; + } + // 进货订单处理 + private function merQuotaHandle(){ + if((int)$this->order->with_goods_mer_id <= 0) return true; + // 获取订单列表 + $orderField = 'order_id,group_order_id,uid,mer_id,mer_quota_title,mer_quota_other,with_goods_mer_id'; + $orderProductField = [ + 'uid', + 'order_product_id', + 'order_id', + 'product_id', + 'total_price', + 'mer_quota_title', + 'mer_quota_other' + ]; + $orderInfo = $this->getOrderInfo($orderField, $orderProductField); + // 循环处理 + $quotaHoldInfo = app()->make(MerchantQuotaRepository::class)->getQuotaInfo($this->order->with_goods_mer_id); + $quotaRecordInsertData = []; + // 判断:是否存在冠名品牌额度 + if((float)$orderInfo['mer_quota_title'] > 0){ + // 变更持有信息 + $changeFront = (float)sprintf("%.2f", $quotaHoldInfo->title_brand_limit - $quotaHoldInfo->title_brand_used); + $quotaHoldInfo->title_brand_used -= (float)$orderInfo['mer_quota_title']; + $quotaHoldInfo->title_brand_total -= (float)$orderInfo['mer_quota_title']; + //变更记录 + $quotaRecordInsertData[] = [ + 'mer_id' => $orderInfo['with_goods_mer_id'], + 'change_type' => (int)0, + 'change_front' => $changeFront, + 'change_quantity' => (float)$orderInfo['mer_quota_title'], + 'change_after' => (float)sprintf("%.2f", $quotaHoldInfo->title_brand_limit - $quotaHoldInfo->title_brand_used), + 'source' => 4, + 'order_id' => $orderInfo['order_id'], + 'quota_type' => 2 + ]; + } + // 判断:是否存在其他品牌额度 + if((float)$orderInfo['mer_quota_other'] > 0){ + // 变更持有信息 + $changeFront = (float)sprintf("%.2f", $quotaHoldInfo->other_brand_limit - $quotaHoldInfo->other_brand_used); + $quotaHoldInfo->other_brand_used -= (float)$orderInfo['mer_quota_other']; + $quotaHoldInfo->other_brand_total -= (float)$orderInfo['mer_quota_other']; + //变更记录 + $quotaRecordInsertData[] = [ + 'mer_id' => $orderInfo['with_goods_mer_id'], + 'change_type' => (int)0, + 'change_front' => $changeFront, + 'change_quantity' => (float)$orderInfo['mer_quota_title'], + 'change_after' => (float)sprintf("%.2f", $quotaHoldInfo->other_brand_limit - $quotaHoldInfo->other_brand_used), + 'source' => 4, + 'order_id' => $orderInfo['order_id'], + 'quota_type' => 3 + ]; + } + $quotaHoldInfo->save(); + if(count($quotaRecordInsertData) > 0) MerchantQuotaRecord::insertAll($quotaRecordInsertData); + return true; + } diff --git a/app/listener/exchangeQuota/OrderCreateEvent.php b/app/listener/exchangeQuota/OrderCreateEvent.php index cb5f3e3..f36e649 100644 --- a/app/listener/exchangeQuota/OrderCreateEvent.php +++ b/app/listener/exchangeQuota/OrderCreateEvent.php @@ -88,8 +88,8 @@ class OrderCreateEvent{ if($merInfo['merchant_sub_type'] == 2 && $productInfo['quota_integral'] > 0){ // 小酒馆 减少惠民积分 $changeFront = (float)$quotaIntegralHoldInfo->surplus_quota; - $quotaIntegralHoldInfo->use_quota += (float)$productInfo['quota_integral'];// 总额度 - $quotaIntegralHoldInfo->surplus_quota -= (float)$productInfo['quota_integral'];// 剩余额度 + $quotaIntegralHoldInfo->use_quota += (float)$productInfo['quota_integral']; + $quotaIntegralHoldInfo->surplus_quota -= (float)$productInfo['quota_integral']; $exchangeQuotaRecordData[] = [ 'uid' => $productInfo['uid'], 'product_id' => $productInfo['product_id'], @@ -105,22 +105,24 @@ class OrderCreateEvent{ 'source' => 2, ]; } - else if($merInfo['merchant_sub_type'] != 2 && $productInfo['exchange_integral'] > 0){ + else if($merInfo['merchant_sub_type'] != 2){ // 酒道馆 减少酒水卡积分 - $integralChangeFront = (float)$userInfo->exchange_integral; - $userInfo->exchange_integral -= (float)$productInfo['exchange_integral']; - $userInfo->exchange_integral = $userInfo->exchange_integral > 0 ? $userInfo->exchange_integral : 0; - $exchangeIntegralRecordData[] = [ - 'uid' => $productInfo['uid'], - 'product_id' => $productInfo['product_id'], - 'order_id' => $productInfo['order_id'], - 'order_product_id' => $productInfo['order_product_id'], - 'change_type' => 0, - 'change_quantity' => (float)$productInfo['exchange_integral'], - 'change_front' => $integralChangeFront, - 'change_after' => (float)$userInfo->exchange_integral, - 'remark' => "兑换商品减少", - ]; + if($productInfo['exchange_integral'] > 0){ + $integralChangeFront = (float)$userInfo->exchange_integral; + $userInfo->exchange_integral -= (float)$productInfo['exchange_integral']; + $userInfo->exchange_integral = $userInfo->exchange_integral > 0 ? $userInfo->exchange_integral : 0; + $exchangeIntegralRecordData[] = [ + 'uid' => $productInfo['uid'], + 'product_id' => $productInfo['product_id'], + 'order_id' => $productInfo['order_id'], + 'order_product_id' => $productInfo['order_product_id'], + 'change_type' => 0, + 'change_quantity' => (float)$productInfo['exchange_integral'], + 'change_front' => $integralChangeFront, + 'change_after' => (float)$userInfo->exchange_integral, + 'remark' => "兑换商品减少", + ]; + } // 酒卡额度处理 酒类型:0=未知,1=瓶装酒,2=封坛酒 $wineType = (int)$productInfo['wine_type'] ?? 1; if($wineType == 1){ diff --git a/crmeb/listens/AutoCancelGroupOrderListen.php b/crmeb/listens/AutoCancelGroupOrderListen.php index b49bc91..3b64b39 100644 --- a/crmeb/listens/AutoCancelGroupOrderListen.php +++ b/crmeb/listens/AutoCancelGroupOrderListen.php @@ -23,11 +23,12 @@ class AutoCancelGroupOrderListen extends TimerService implements ListenerInterfa $timer = ((int)systemConfig('auto_close_order_timer')) ?: 15; $time = date('Y-m-d H:i:s', strtotime("- $timer minutes")); $groupOrderIds = $storeGroupOrderRepository->getTimeOutIds($time); + // Log::info('自动关闭订单失败 - 处理内容'.var_export($groupOrderIds,1)); foreach ($groupOrderIds as $id) { try { $storeGroupOrderRepository->cancel($id); } catch (\Exception $e) { - // Log::info('自动关闭订单失败'.var_export(['id' => $id,'msg' => $e->getMessage()],1)); + Log::info('自动关闭订单失败'.var_export(['id' => $id,'msg' => $e->getMessage()],1)); } } }); diff --git a/crmeb/listens/SyncBroadcastStatusListen.php b/crmeb/listens/SyncBroadcastStatusListen.php index 5d99869..863bda4 100644 --- a/crmeb/listens/SyncBroadcastStatusListen.php +++ b/crmeb/listens/SyncBroadcastStatusListen.php @@ -39,7 +39,7 @@ class SyncBroadcastStatusListen extends TimerService implements ListenerInterfac if ($e instanceof \EasyWeChat\Core\Exceptions\HttpException && $e->getCode() == '48001') { Cache::set('_sys_break_b_room', 1, 3600); } - Log::error('同步直播间:' . $e->getMessage()); + // Log::error('同步直播间:' . $e->getMessage()); } }); }