|
|
@@ -46,38 +46,40 @@ class RewardPointRepository extends Repository
|
|
|
'last_checkout' => Carbon::now()
|
|
|
]
|
|
|
);
|
|
|
-
|
|
|
- // 2. 使用 increment 方法增加积分
|
|
|
- $customerPoints->increment('mw_reward_point', $amount);
|
|
|
-
|
|
|
+
|
|
|
+ // 2. 确定积分状态
|
|
|
+ // 订单类型的积分初始状态为 PENDING,其他类型直接 COMPLETED
|
|
|
+ $status = ($type === RewardActiveRule::TYPE_ORDER)
|
|
|
+ ? RewardPointHistory::STATUS_PENDING
|
|
|
+ : RewardPointHistory::STATUS_COMPLETED;
|
|
|
+
|
|
|
+ // 3. 只有 COMPLETED 状态的积分才计入用户总积分
|
|
|
+ if ($status === RewardPointHistory::STATUS_COMPLETED) {
|
|
|
+ $customerPoints->increment('mw_reward_point', $amount);
|
|
|
+ }
|
|
|
+
|
|
|
// 3. 更新最后检查时间
|
|
|
$customerPoints->last_checkout = Carbon::now();
|
|
|
$customerPoints->save();
|
|
|
-
|
|
|
+
|
|
|
// 4. 重新查询获取最新的余额
|
|
|
$customerPoints = RewardPointCustomer::where('customer_id', $customerId)->first();
|
|
|
$newBalance = $customerPoints ? (int) $customerPoints->mw_reward_point : $amount;
|
|
|
|
|
|
- Log::info('Points added', [
|
|
|
+ /* Log::info('Points added', [
|
|
|
'customer_id' => $customerId,
|
|
|
'type' => $type,
|
|
|
'amount' => $amount,
|
|
|
'new_balance' => $newBalance
|
|
|
- ]);
|
|
|
+ ]);*/
|
|
|
|
|
|
// 5. 获取过期信息(如果没有传入规则,则查询)
|
|
|
- $expiredDay = 0;
|
|
|
- $expiredTime = null;
|
|
|
-
|
|
|
+
|
|
|
if ($rule) {
|
|
|
$expiredDay = $rule->expired_day ?? 0;
|
|
|
$expiredTime = $expiredDay > 0 ? Carbon::now()->addDays($expiredDay) : null;
|
|
|
} else {
|
|
|
- // 兼容旧代码,如果没有传入规则则查询
|
|
|
- $rule = RewardActiveRule::where('type_of_transaction', $type)
|
|
|
- ->where('status', 1)
|
|
|
- ->first();
|
|
|
- $expiredDay = $rule ? $rule->expired_day : 0;
|
|
|
+ $expiredDay = 365;
|
|
|
$expiredTime = $expiredDay > 0 ? Carbon::now()->addDays($expiredDay) : null;
|
|
|
}
|
|
|
|
|
|
@@ -94,7 +96,7 @@ class RewardPointRepository extends Repository
|
|
|
'expired_time' => $expiredTime,
|
|
|
'point_remaining' => $amount,
|
|
|
'check_time' => 1,
|
|
|
- 'status' => RewardPointHistory::STATUS_COMPLETED
|
|
|
+ 'status' => $status
|
|
|
]);
|
|
|
|
|
|
return $history;
|
|
|
@@ -128,17 +130,17 @@ class RewardPointRepository extends Repository
|
|
|
'last_checkout' => Carbon::now()
|
|
|
]
|
|
|
);
|
|
|
-
|
|
|
+
|
|
|
// 确保 $customerPoints 存在
|
|
|
if (!$customerPoints) {
|
|
|
throw new \Exception('Failed to create or retrieve customer points record');
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 2. 获取当前余额
|
|
|
$currentBalance = (int) $customerPoints->mw_reward_point;
|
|
|
$histories = [];
|
|
|
- $totalAmount = 0;
|
|
|
-
|
|
|
+ $totalAmount = 0;
|
|
|
+
|
|
|
// 3. 逐条创建历史记录(每条记录使用累加后的余额)
|
|
|
foreach ($pointsList as $index => $pointData) {
|
|
|
$amount = (int) $pointData['amount'];
|
|
|
@@ -146,22 +148,22 @@ class RewardPointRepository extends Repository
|
|
|
$detail = $pointData['detail'] ?? null;
|
|
|
$orderId = $pointData['order_id'] ?? null;
|
|
|
$rule = $pointData['rule'] ?? null;
|
|
|
-
|
|
|
+
|
|
|
// 累加总积分
|
|
|
$totalAmount += $amount;
|
|
|
-
|
|
|
+
|
|
|
// 计算当前这条记录后的余额(累加后的余额)
|
|
|
$newBalance = $currentBalance + $totalAmount;
|
|
|
-
|
|
|
+
|
|
|
// 获取过期信息
|
|
|
$expiredDay = 0;
|
|
|
$expiredTime = null;
|
|
|
-
|
|
|
+
|
|
|
if ($rule) {
|
|
|
$expiredDay = $rule->expired_day ?? 0;
|
|
|
$expiredTime = $expiredDay > 0 ? Carbon::now()->addDays($expiredDay) : null;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 创建历史记录(每条记录独立)
|
|
|
$history = $this->create([
|
|
|
'customer_id' => $customerId,
|
|
|
@@ -177,11 +179,11 @@ class RewardPointRepository extends Repository
|
|
|
'check_time' => 1,
|
|
|
'status' => RewardPointHistory::STATUS_COMPLETED
|
|
|
]);
|
|
|
-
|
|
|
+
|
|
|
$histories[] = $history;
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 4. 最后一次性更新用户总积分
|
|
|
$updated = DB::table('mw_reward_point_customer')
|
|
|
->where('customer_id', $customerId)
|
|
|
@@ -189,7 +191,7 @@ class RewardPointRepository extends Repository
|
|
|
'mw_reward_point' => DB::raw('mw_reward_point + ' . (float)$totalAmount),
|
|
|
'last_checkout' => Carbon::now()
|
|
|
]);
|
|
|
-
|
|
|
+
|
|
|
return $histories;
|
|
|
});
|
|
|
} catch (\Exception $e) {
|
|
|
@@ -214,7 +216,7 @@ class RewardPointRepository extends Repository
|
|
|
Log::warning('Customer points record not found', ['customer_id' => $customerId]);
|
|
|
return false;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
if ($customerPoints->mw_reward_point < $amount) {
|
|
|
Log::warning('Insufficient points', [
|
|
|
'customer_id' => $customerId,
|
|
|
@@ -275,6 +277,8 @@ class RewardPointRepository extends Repository
|
|
|
|
|
|
public function checkExpiredPoints()
|
|
|
{
|
|
|
+ // 只检查已过期的、状态为 COMPLETED 的、还有剩余积分的记录
|
|
|
+ // PENDING 状态的积分不会过期(因为订单还未完成)
|
|
|
$expiredHistories = $this->where('expired_time', '<', Carbon::now())
|
|
|
->where('status', RewardPointHistory::STATUS_COMPLETED)
|
|
|
->where('point_remaining', '>', 0)
|
|
|
@@ -294,11 +298,11 @@ class RewardPointRepository extends Repository
|
|
|
$history->status = RewardPointHistory::STATUS_EXPIRED;
|
|
|
$history->point_remaining = 0;
|
|
|
$history->save();
|
|
|
-
|
|
|
+
|
|
|
Log::info('Expired points processed', [
|
|
|
'history_id' => $history->history_id,
|
|
|
'customer_id' => $history->customer_id,
|
|
|
- 'points' => $history->point_remaining
|
|
|
+ 'points_expired' => $history->getOriginal('point_remaining')
|
|
|
]);
|
|
|
}
|
|
|
});
|
|
|
@@ -310,4 +314,69 @@ class RewardPointRepository extends Repository
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 确认待处理的积分(将 PENDING 状态改为 COMPLETED 并计入用户余额)
|
|
|
+ */
|
|
|
+ public function confirmPendingPoints($customerId, $orderId)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ return DB::transaction(function () use ($customerId, $orderId) {
|
|
|
+ // 查找所有 PENDING 状态的订单积分记录
|
|
|
+ $pendingHistories = $this->where('customer_id', $customerId)
|
|
|
+ ->where('history_order_id', $orderId)
|
|
|
+ ->where('type_of_transaction', RewardActiveRule::TYPE_ORDER)
|
|
|
+ ->where('status', RewardPointHistory::STATUS_PENDING)
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ if ($pendingHistories->isEmpty()) {
|
|
|
+ Log::info('No pending points to confirm', [
|
|
|
+ 'customer_id' => $customerId,
|
|
|
+ 'order_id' => $orderId
|
|
|
+ ]);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ $totalConfirmedPoints = 0;
|
|
|
+
|
|
|
+ foreach ($pendingHistories as $history) {
|
|
|
+ // 增加用户总积分
|
|
|
+ $customerPoints = RewardPointCustomer::where('customer_id', $customerId)
|
|
|
+ ->lockForUpdate()
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ if ($customerPoints) {
|
|
|
+ $customerPoints->increment('mw_reward_point', $history->point_remaining);
|
|
|
+ $customerPoints->save();
|
|
|
+
|
|
|
+ // 更新历史记录状态
|
|
|
+ $history->status = RewardPointHistory::STATUS_COMPLETED;
|
|
|
+ $history->balance = (int) $customerPoints->mw_reward_point;
|
|
|
+ $history->save();
|
|
|
+
|
|
|
+ $totalConfirmedPoints += $history->point_remaining;
|
|
|
+
|
|
|
+ Log::info('Pending points confirmed', [
|
|
|
+ 'history_id' => $history->history_id,
|
|
|
+ 'customer_id' => $customerId,
|
|
|
+ 'order_id' => $orderId,
|
|
|
+ 'points' => $history->point_remaining,
|
|
|
+ 'new_balance' => $customerPoints->mw_reward_point
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $totalConfirmedPoints;
|
|
|
+ });
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('Error confirming pending points', [
|
|
|
+ 'customer_id' => $customerId,
|
|
|
+ 'order_id' => $orderId,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
+ ]);
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|