diff --git a/app/Console/Commands/OrderCompleteCommand.php b/app/Console/Commands/OrderCompleteCommand.php index a2a27d70..60d703d9 100644 --- a/app/Console/Commands/OrderCompleteCommand.php +++ b/app/Console/Commands/OrderCompleteCommand.php @@ -6,6 +6,8 @@ use App\Exceptions\BizException; use App\Models\Order; use App\Services\OrderService; use Illuminate\Console\Command; +use Illuminate\Support\Facades\DB; +use Throwable; class OrderCompleteCommand extends Command { @@ -36,8 +38,11 @@ class OrderCompleteCommand extends Command Order::with('packages')->completable()->chunkById(200, function ($orders) use ($orderService, &$page) { foreach ($orders as $order) { try { + DB::beginTransaction(); $orderService->confirm($order, true); + DB::commit(); } catch (BizException $e) { + DB::rollBack(); } } diff --git a/app/Services/DistributeService.php b/app/Services/DistributeService.php index c3bcbb9e..5926f22e 100644 --- a/app/Services/DistributeService.php +++ b/app/Services/DistributeService.php @@ -22,17 +22,36 @@ class DistributeService if ($order->isCancelled()) { return false; } + // 已经添加过返现记录 + if ($user->salesValueLogs()->where('order_id', $order->id)->exists()) { + return false; + } // 订单成长值 $sales_value = $order->sales_value; $user = $order->user; - // $user->salesValueLogs()->create([ - // 'order_id' => $order->id, - // 'order_user_id' => $order->user_id, - // 'type' => SalesValueLog::TYPE_INDIVIDUAL, - // 'change_sales_value' => $sales_value - // ]); - // $user->userInfo()->increment('growth_value', $sales_value); + // 用户获得成长值 + $user->salesValueLogs()->create([ + 'order_id' => $order->id, + 'order_user_id' => $order->user_id, + 'type' => SalesValueLog::TYPE_INDIVIDUAL, + 'change_sales_value' => $sales_value + ]); + $user->userInfo()->increment('growth_value', $sales_value); + // 自动升级代理 + $levels = Vip::orderBy('sort')->get(); + $level_up = ''; + foreach($levels->reverse() as $item) { + if ($user->userInfo->growth_value >= $item->growth_value) { + $level_up = $item; + } + } + if ($level_up && $level_up->slug === 'favoite') { + $user->userVip()->updateOrCreate([], [ + 'vip_id' => $level_up->id, + 'growth_value' => $level_up->growth_value + ]); + } // 上级返现 $parent_ids = array_reverse($user->userInfo->parent_ids); diff --git a/app/Services/OrderService.php b/app/Services/OrderService.php index 5874255f..2c4d676e 100644 --- a/app/Services/OrderService.php +++ b/app/Services/OrderService.php @@ -1055,6 +1055,8 @@ class OrderService 'status' => Order::STATUS_COMPLETED, 'completed_at' => now(), ]); + + (new DistributeService())->storeByOrder(); } /**