|
|
@@ -13,7 +13,7 @@ use Illuminate\Support\Facades\Log;
|
|
|
class GrowthValueService
|
|
|
{
|
|
|
/**
|
|
|
- * 处理订单完成事件,增加成长值并检查升级
|
|
|
+ * 处理订单完成事件,增加成长值
|
|
|
*/
|
|
|
public function handleOrderCompleted($order)
|
|
|
{
|
|
|
@@ -30,12 +30,9 @@ class GrowthValueService
|
|
|
['customer_id' => $customerId],
|
|
|
[
|
|
|
'growth_value' => 0,
|
|
|
- 'growth_level' => 'V0',
|
|
|
- 'level_updated_at' => now(),
|
|
|
]
|
|
|
);
|
|
|
|
|
|
- $levelBefore = $growthValueCustomer->growth_level;
|
|
|
$hasFirstOrder = !is_null($growthValueCustomer->first_order_completed_at);
|
|
|
|
|
|
if (!$hasFirstOrder) {
|
|
|
@@ -56,14 +53,6 @@ class GrowthValueService
|
|
|
$growthValueCustomer->growth_value_expires_at = $expiresAt;
|
|
|
}
|
|
|
|
|
|
- $newLevel = GrowthValueLevel::getLevelByGrowthValue($newGrowthValue, true);
|
|
|
- $levelAfter = $newLevel ? $newLevel->level_code : 'V0';
|
|
|
-
|
|
|
- if ($levelBefore !== $levelAfter) {
|
|
|
- $growthValueCustomer->growth_level = $levelAfter;
|
|
|
- $growthValueCustomer->level_updated_at = now();
|
|
|
- }
|
|
|
-
|
|
|
$growthValueCustomer->save();
|
|
|
|
|
|
GrowthValueHistory::create([
|
|
|
@@ -72,22 +61,18 @@ class GrowthValueService
|
|
|
'order_amount' => $orderAmount,
|
|
|
'growth_value_earned' => $earnedGrowthValue,
|
|
|
'total_growth_value' => $newGrowthValue,
|
|
|
- 'level_before' => $levelBefore,
|
|
|
- 'level_after' => $levelAfter,
|
|
|
'event_type' => GrowthValueHistory::EVENT_TYPE_ORDER,
|
|
|
'description' => "订单 #{$order->increment_id} 完成,获得 {$earnedGrowthValue} 成长值",
|
|
|
'expires_at' => $expiresAt,
|
|
|
'created_at' => now(),
|
|
|
]);
|
|
|
|
|
|
- Log::info('Growth value added and level checked', [
|
|
|
+ Log::info('Growth value added', [
|
|
|
'customer_id' => $customerId,
|
|
|
'order_id' => $order->id,
|
|
|
'order_amount' => $orderAmount,
|
|
|
'earned_growth_value' => $earnedGrowthValue,
|
|
|
'new_total' => $newGrowthValue,
|
|
|
- 'level_before' => $levelBefore,
|
|
|
- 'level_after' => $levelAfter,
|
|
|
]);
|
|
|
});
|
|
|
} catch (\Exception $e) {
|
|
|
@@ -151,16 +136,12 @@ class GrowthValueService
|
|
|
foreach ($expiredRecords as $record) {
|
|
|
try {
|
|
|
DB::transaction(function () use ($record) {
|
|
|
- $levelBefore = $record->growth_level;
|
|
|
-
|
|
|
GrowthValueHistory::create([
|
|
|
'customer_id' => $record->customer_id,
|
|
|
'order_id' => null,
|
|
|
'order_amount' => 0,
|
|
|
'growth_value_earned' => -$record->growth_value,
|
|
|
'total_growth_value' => 0,
|
|
|
- 'level_before' => $levelBefore,
|
|
|
- 'level_after' => 'V0',
|
|
|
'event_type' => GrowthValueHistory::EVENT_TYPE_EXPIRATION,
|
|
|
'description' => '成长值过期清零',
|
|
|
'expires_at' => null,
|
|
|
@@ -168,15 +149,12 @@ class GrowthValueService
|
|
|
]);
|
|
|
|
|
|
$record->growth_value = 0;
|
|
|
- $record->growth_level = 'V0';
|
|
|
- $record->level_updated_at = now();
|
|
|
$record->growth_value_expires_at = null;
|
|
|
$record->save();
|
|
|
|
|
|
- Log::info('Growth value expired and level reset', [
|
|
|
+ Log::info('Growth value expired', [
|
|
|
'customer_id' => $record->customer_id,
|
|
|
'expired_value' => $record->growth_value,
|
|
|
- 'level_before' => $levelBefore,
|
|
|
]);
|
|
|
});
|
|
|
} catch (\Exception $e) {
|
|
|
@@ -193,51 +171,48 @@ class GrowthValueService
|
|
|
*/
|
|
|
public function getCustomerGrowthValue($customerId)
|
|
|
{
|
|
|
- $growthValueCustomer = GrowthValueCustomer::with('levelConfig')->where('customer_id', $customerId)->first();
|
|
|
- $customer = \Webkul\Customer\Models\Customer::find($customerId);
|
|
|
+ $growthValueCustomer = GrowthValueCustomer::where('customer_id', $customerId)->first();
|
|
|
+ $customer = \Webkul\Customer\Models\Customer::with('group')->find($customerId);
|
|
|
|
|
|
if (!$growthValueCustomer) {
|
|
|
- $defaultLevel = GrowthValueLevel::where('level_code', 'V0')->first();
|
|
|
+ $defaultGroup = GrowthValueLevel::where('code', 'general')->first();
|
|
|
return [
|
|
|
'growth_value' => 0,
|
|
|
- 'growth_level' => 'V0',
|
|
|
- 'level_name' => $defaultLevel ? $defaultLevel->level_name : '普通会员',
|
|
|
- 'level_icon' => $defaultLevel ? $defaultLevel->icon : null,
|
|
|
- 'discount_rate' => $defaultLevel ? $defaultLevel->discount_rate : 0,
|
|
|
- 'benefits' => $defaultLevel ? $defaultLevel->benefits : [],
|
|
|
+ 'current_group' => $customer ? $customer->group : null,
|
|
|
+ 'growth_level_name' => $defaultGroup ? $defaultGroup->growth_level_name : 'V0',
|
|
|
+ 'growth_discount_rate' => $defaultGroup ? $defaultGroup->growth_discount_rate : 0,
|
|
|
+ 'growth_benefits' => $defaultGroup ? $defaultGroup->growth_benefits : [],
|
|
|
'next_level' => null,
|
|
|
'points_to_next_level' => null,
|
|
|
'expires_at' => null,
|
|
|
'first_order_completed' => false,
|
|
|
- 'customer_group' => $customer ? $customer->group : null,
|
|
|
];
|
|
|
}
|
|
|
|
|
|
- $levelConfig = $growthValueCustomer->levelConfig;
|
|
|
+ $currentGroup = $customer ? $customer->group : null;
|
|
|
|
|
|
- $nextLevel = GrowthValueLevel::getNextLevel(
|
|
|
- $growthValueCustomer->growth_level,
|
|
|
+ $nextGroup = GrowthValueLevel::getNextLevel(
|
|
|
+ $customer ? $customer->customer_group_id : null,
|
|
|
!is_null($growthValueCustomer->first_order_completed_at)
|
|
|
);
|
|
|
|
|
|
return [
|
|
|
'growth_value' => $growthValueCustomer->growth_value,
|
|
|
- 'growth_level' => $growthValueCustomer->growth_level,
|
|
|
- 'level_name' => $levelConfig ? $levelConfig->level_name : '普通会员',
|
|
|
- 'level_icon' => $levelConfig ? $levelConfig->icon : null,
|
|
|
- 'discount_rate' => $levelConfig ? $levelConfig->discount_rate : 0,
|
|
|
- 'benefits' => $levelConfig ? $levelConfig->benefits : [],
|
|
|
- 'next_level' => $nextLevel ? [
|
|
|
- 'level_code' => $nextLevel->level_code,
|
|
|
- 'level_name' => $nextLevel->level_name,
|
|
|
- 'min_growth_value' => $nextLevel->min_growth_value,
|
|
|
- 'icon' => $nextLevel->icon,
|
|
|
+ 'current_group' => $currentGroup,
|
|
|
+ 'growth_level_name' => $currentGroup ? $currentGroup->growth_level_name : 'V0',
|
|
|
+ 'growth_discount_rate' => $currentGroup ? $currentGroup->growth_discount_rate : 0,
|
|
|
+ 'growth_benefits' => $currentGroup ? $currentGroup->growth_benefits : [],
|
|
|
+ 'growth_level_icon' => $currentGroup ? $currentGroup->growth_level_icon : null,
|
|
|
+ 'next_level' => $nextGroup ? [
|
|
|
+ 'group_id' => $nextGroup->id,
|
|
|
+ 'group_name' => $nextGroup->name,
|
|
|
+ 'growth_level_name' => $nextGroup->growth_level_name,
|
|
|
+ 'min_growth_value' => $nextGroup->min_growth_value,
|
|
|
+ 'icon' => $nextGroup->growth_level_icon,
|
|
|
] : null,
|
|
|
- 'points_to_next_level' => $nextLevel ? ($nextLevel->min_growth_value - $growthValueCustomer->growth_value) : null,
|
|
|
+ 'points_to_next_level' => $nextGroup ? ($nextGroup->min_growth_value - $growthValueCustomer->growth_value) : null,
|
|
|
'expires_at' => $growthValueCustomer->growth_value_expires_at,
|
|
|
'first_order_completed' => !is_null($growthValueCustomer->first_order_completed_at),
|
|
|
- 'customer_group' => $customer ? $customer->group : null,
|
|
|
- 'level_updated_at' => $growthValueCustomer->level_updated_at,
|
|
|
];
|
|
|
}
|
|
|
|
|
|
@@ -252,24 +227,12 @@ class GrowthValueService
|
|
|
['customer_id' => $customerId],
|
|
|
[
|
|
|
'growth_value' => 0,
|
|
|
- 'growth_level' => 'V0',
|
|
|
- 'level_updated_at' => now(),
|
|
|
]
|
|
|
);
|
|
|
|
|
|
- $levelBefore = $growthValueCustomer->growth_level;
|
|
|
$currentGrowthValue = $growthValueCustomer->growth_value;
|
|
|
$newGrowthValue = max(0, $currentGrowthValue + $amount);
|
|
|
|
|
|
- $hasFirstOrder = !is_null($growthValueCustomer->first_order_completed_at);
|
|
|
- $newLevel = GrowthValueLevel::getLevelByGrowthValue($newGrowthValue, $hasFirstOrder);
|
|
|
- $levelAfter = $newLevel ? $newLevel->level_code : 'V0';
|
|
|
-
|
|
|
- if ($levelBefore !== $levelAfter) {
|
|
|
- $growthValueCustomer->growth_level = $levelAfter;
|
|
|
- $growthValueCustomer->level_updated_at = now();
|
|
|
- }
|
|
|
-
|
|
|
$growthValueCustomer->growth_value = $newGrowthValue;
|
|
|
$growthValueCustomer->save();
|
|
|
|
|
|
@@ -279,8 +242,6 @@ class GrowthValueService
|
|
|
'order_amount' => 0,
|
|
|
'growth_value_earned' => $amount,
|
|
|
'total_growth_value' => $newGrowthValue,
|
|
|
- 'level_before' => $levelBefore,
|
|
|
- 'level_after' => $levelAfter,
|
|
|
'event_type' => GrowthValueHistory::EVENT_TYPE_ADMIN_ADJUST,
|
|
|
'description' => $description,
|
|
|
'expires_at' => null,
|
|
|
@@ -291,8 +252,6 @@ class GrowthValueService
|
|
|
'customer_id' => $customerId,
|
|
|
'amount' => $amount,
|
|
|
'new_total' => $newGrowthValue,
|
|
|
- 'level_before' => $levelBefore,
|
|
|
- 'level_after' => $levelAfter,
|
|
|
]);
|
|
|
|
|
|
return $history;
|
|
|
@@ -308,12 +267,10 @@ class GrowthValueService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取所有等级配置
|
|
|
+ * 获取所有配置了成长值的会员组
|
|
|
*/
|
|
|
- public function getAllLevels()
|
|
|
+ public function getAllGrowthLevels()
|
|
|
{
|
|
|
- return GrowthValueLevel::where('is_active', true)
|
|
|
- ->orderBy('sort_order')
|
|
|
- ->get();
|
|
|
+ return GrowthValueLevel::getAllGrowthLevels();
|
|
|
}
|
|
|
}
|