|
|
@@ -4,7 +4,6 @@ namespace Longyi\RewardPoints\Listeners;
|
|
|
|
|
|
use Longyi\RewardPoints\Repositories\RewardPointRepository;
|
|
|
use Longyi\RewardPoints\Models\RewardActiveRule;
|
|
|
-use Longyi\RewardPoints\Models\RewardPointHistory;
|
|
|
use Webkul\Customer\Models\Customer;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
@@ -17,9 +16,6 @@ class ReviewEvents
|
|
|
$this->rewardPointRepository = $rewardPointRepository;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 处理评价创建事件,赠送评价积分
|
|
|
- */
|
|
|
public function handleReviewCreation($review): void
|
|
|
{
|
|
|
// 验证基础条件
|
|
|
@@ -43,38 +39,37 @@ class ReviewEvents
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // 检查规则是否适用于该客户
|
|
|
+ // 【简化】只需要调用 isApplicableToCustomer,它会内部处理所有适用性检查
|
|
|
if (!$rule->isApplicableToCustomer($customer)) {
|
|
|
- Log::info('Review rule not applicable to this customer group', [
|
|
|
+ Log::info('Review rule not applicable', [
|
|
|
'review_id' => $review->id,
|
|
|
'customer_id' => $customer->id,
|
|
|
- 'customer_group_id' => $customer->customer_group_id ?? null
|
|
|
]);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // 检查是否已经为该评价发放过积分(防止重复发放)
|
|
|
- if ($this->hasReceivedReviewPoints($review->id, $customer->id)) {
|
|
|
- Log::info('Review points already granted, skipping', [
|
|
|
+ // 【简化】使用统一方法获取积分值
|
|
|
+ $points = $rule->getPointsForCustomer($customer);
|
|
|
+
|
|
|
+ if ($points <= 0) {
|
|
|
+ Log::info('Review points is 0, skipping', [
|
|
|
'review_id' => $review->id,
|
|
|
- 'customer_id' => $customer->id
|
|
|
+ 'points' => $points
|
|
|
]);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // 获取积分值
|
|
|
- $points = $this->getReviewPoints($customer, $rule);
|
|
|
- if ($points <= 0) {
|
|
|
- Log::info('Review points is 0 or negative, skipping', [
|
|
|
+ // 检查是否已经为该评价发放过积分
|
|
|
+ if ($this->hasReceivedReviewPoints($review->id, $customer->id)) {
|
|
|
+ Log::info('Review points already granted', [
|
|
|
'review_id' => $review->id,
|
|
|
- 'customer_id' => $customer->id,
|
|
|
- 'points' => $points
|
|
|
+ 'customer_id' => $customer->id
|
|
|
]);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 添加评价积分
|
|
|
- $history = $this->rewardPointRepository->addPoints(
|
|
|
+ $this->rewardPointRepository->addPoints(
|
|
|
$review->customer_id,
|
|
|
RewardActiveRule::TYPE_REVIEW,
|
|
|
$points,
|
|
|
@@ -83,11 +78,10 @@ class ReviewEvents
|
|
|
$rule
|
|
|
);
|
|
|
|
|
|
- Log::info('Review points added successfully', [
|
|
|
+ Log::info('Review points added', [
|
|
|
'review_id' => $review->id,
|
|
|
'customer_id' => $customer->id,
|
|
|
- 'points' => $points,
|
|
|
- 'history_id' => $history->history_id ?? null
|
|
|
+ 'points' => $points
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
@@ -100,19 +94,13 @@ class ReviewEvents
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- // 检查是否有客户ID
|
|
|
if (empty($review->customer_id)) {
|
|
|
- Log::info('Review has no customer_id, skipping points', [
|
|
|
+ Log::info('Review has no customer_id', [
|
|
|
'review_id' => $review->id ?? null
|
|
|
]);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- // 可选:检查评价状态是否已批准(如果评价系统有审核机制)
|
|
|
- // if (isset($review->status) && $review->status !== 'approved') {
|
|
|
- // return false;
|
|
|
- // }
|
|
|
-
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
@@ -134,33 +122,13 @@ class ReviewEvents
|
|
|
return Customer::find($customerId);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 获取评价积分值
|
|
|
- */
|
|
|
- protected function getReviewPoints(Customer $customer, RewardActiveRule $rule): int
|
|
|
- {
|
|
|
- $customerGroupId = $customer->customer_group_id;
|
|
|
-
|
|
|
- if ($customerGroupId !== null) {
|
|
|
- return $rule->getRewardPointForCustomerGroup($customerGroupId);
|
|
|
- }
|
|
|
-
|
|
|
- return (int)$rule->reward_point;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 检查是否已经为该评价发放过积分
|
|
|
*/
|
|
|
protected function hasReceivedReviewPoints(int $reviewId, int $customerId): bool
|
|
|
{
|
|
|
- // 方案1:通过订单ID字段关联(如果评价有对应的订单ID)
|
|
|
- // return RewardPointHistory::where('customer_id', $customerId)
|
|
|
- // ->where('type_of_transaction', RewardActiveRule::TYPE_REVIEW)
|
|
|
- // ->where('history_order_id', $reviewId)
|
|
|
- // ->exists();
|
|
|
-
|
|
|
- // 方案2:通过备注字段判断(需要存储评价ID)
|
|
|
- return RewardPointHistory::where('customer_id', $customerId)
|
|
|
+ // 通过备注字段判断(需要存储评价ID)
|
|
|
+ return \Longyi\RewardPoints\Models\RewardPointHistory::where('customer_id', $customerId)
|
|
|
->where('type_of_transaction', RewardActiveRule::TYPE_REVIEW)
|
|
|
->where('description', 'LIKE', "%Review ID: {$reviewId}%")
|
|
|
->exists();
|
|
|
@@ -171,22 +139,10 @@ class ReviewEvents
|
|
|
*/
|
|
|
protected function getReviewPointsDescription($review): string
|
|
|
{
|
|
|
- $productInfo = '';
|
|
|
-
|
|
|
- if (!empty($review->product_id)) {
|
|
|
- $productInfo = " for product #{$review->product_id}";
|
|
|
- }
|
|
|
+ $productInfo = !empty($review->product_id)
|
|
|
+ ? " for product #{$review->product_id}"
|
|
|
+ : '';
|
|
|
|
|
|
return "Points earned for product review{$productInfo} (Review ID: {$review->id})";
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 批量处理多个评价(如果是一次性创建多个评价的场景)
|
|
|
- */
|
|
|
- public function handleMultipleReviewsCreation(array $reviews): void
|
|
|
- {
|
|
|
- foreach ($reviews as $review) {
|
|
|
- $this->handleReviewCreation($review);
|
|
|
- }
|
|
|
- }
|
|
|
}
|