优化每日推送创建(更新)用户资产

This commit is contained in:
2026-04-22 16:59:42 +08:00
parent ca0a9e75e0
commit 7af40bdd1f

View File

@@ -146,6 +146,36 @@ class Playx extends Api
return MallUserAsset::where('id', $assetId)->find(); return MallUserAsset::where('id', $assetId)->find();
} }
/**
* 按每日推送记录同步用户资产主信息applyAssetDelta=true 时才落资产增量。
*/
private function syncAssetByDailyPush(string $playxUserId, string $username, string $date, int $newLocked, int $todayLimit, bool $applyAssetDelta): ?MallUserAsset
{
$asset = $this->ensureAssetForPlayx($playxUserId, $username);
if (!$asset) {
return null;
}
$asset->playx_user_id = $playxUserId;
$uname = trim($username);
if ($uname !== '') {
$asset->username = $uname;
}
if ($applyAssetDelta) {
if ($asset->today_limit_date !== $date) {
$asset->today_claimed = 0;
$asset->today_limit_date = $date;
}
$asset->locked_points = intval($asset->locked_points ?? 0) + $newLocked;
$asset->today_limit = $todayLimit;
}
$asset->save();
return $asset;
}
/** /**
* Daily Push API - PlayX 调用商城接收 T+1 数据 * Daily Push API - PlayX 调用商城接收 T+1 数据
* POST /api/v1/mall/dailyPush * POST /api/v1/mall/dailyPush
@@ -221,6 +251,16 @@ class Playx extends Api
$exists = MallDailyPush::where('user_id', $playxUserId)->where('date', $date)->find(); $exists = MallDailyPush::where('user_id', $playxUserId)->where('date', $date)->find();
if ($exists) { if ($exists) {
$newLocked = 0;
if ($yesterdayWinLossNet < 0) {
$newLocked = intval(round(abs(floatval($yesterdayWinLossNet)) * $returnRatio));
}
$todayLimit = intval(round(floatval($yesterdayTotalDeposit) * $unlockRatio));
$asset = $this->syncAssetByDailyPush($playxUserId, $username, $date, $newLocked, $todayLimit, false);
if (!$asset) {
return $this->error(__('Failed to ensure PlayX user asset'));
}
$results[] = [ $results[] = [
'user_id' => $playxUserId, 'user_id' => $playxUserId,
'deduped' => true, 'deduped' => true,
@@ -249,26 +289,11 @@ class Playx extends Api
} }
$todayLimit = intval(round(floatval($yesterdayTotalDeposit) * $unlockRatio)); $todayLimit = intval(round(floatval($yesterdayTotalDeposit) * $unlockRatio));
$asset = $this->ensureAssetForPlayx($playxUserId, $username); $asset = $this->syncAssetByDailyPush($playxUserId, $username, $date, $newLocked, $todayLimit, true);
if (!$asset) { if (!$asset) {
throw new \RuntimeException(__('Failed to ensure PlayX user asset')); throw new \RuntimeException(__('Failed to ensure PlayX user asset'));
} }
if ($asset->today_limit_date !== $date) {
$asset->today_claimed = 0;
$asset->today_limit_date = $date;
}
$asset->locked_points = intval($asset->locked_points ?? 0) + $newLocked;
$asset->today_limit = $todayLimit;
$asset->playx_user_id = $playxUserId;
$uname = trim($username);
if ($uname !== '') {
$asset->username = $uname;
}
$asset->save();
Db::commit(); Db::commit();
$results[] = [ $results[] = [
@@ -306,6 +331,18 @@ class Playx extends Api
$exists = MallDailyPush::where('user_id', $playxUserId)->where('date', $date)->find(); $exists = MallDailyPush::where('user_id', $playxUserId)->where('date', $date)->find();
if ($exists) { if ($exists) {
$newLocked = 0;
$returnRatio = config('playx.return_ratio', 0.1);
$unlockRatio = config('playx.unlock_ratio', 0.1);
if ($yesterdayWinLossNet < 0) {
$newLocked = intval(round(abs(floatval($yesterdayWinLossNet)) * $returnRatio));
}
$todayLimit = intval(round(floatval($yesterdayTotalDeposit) * $unlockRatio));
$asset = $this->syncAssetByDailyPush($playxUserId, strval($body['username'] ?? ''), $date, $newLocked, $todayLimit, false);
if (!$asset) {
return $this->error(__('Failed to ensure PlayX user asset'));
}
return $this->success('', [ return $this->success('', [
'request_id' => $requestId, 'request_id' => $requestId,
'accepted' => true, 'accepted' => true,
@@ -335,26 +372,11 @@ class Playx extends Api
} }
$todayLimit = intval(round(floatval($yesterdayTotalDeposit) * $unlockRatio)); $todayLimit = intval(round(floatval($yesterdayTotalDeposit) * $unlockRatio));
$asset = $this->ensureAssetForPlayx($playxUserId, strval($body['username'] ?? '')); $asset = $this->syncAssetByDailyPush($playxUserId, strval($body['username'] ?? ''), $date, $newLocked, $todayLimit, true);
if (!$asset) { if (!$asset) {
throw new \RuntimeException(__('Failed to ensure PlayX user asset')); throw new \RuntimeException(__('Failed to ensure PlayX user asset'));
} }
if ($asset->today_limit_date !== $date) {
$asset->today_claimed = 0;
$asset->today_limit_date = $date;
}
$asset->locked_points = intval($asset->locked_points ?? 0) + $newLocked;
$asset->today_limit = $todayLimit;
$asset->playx_user_id = $playxUserId;
$uname = trim(strval($body['username'] ?? ''));
if ($uname !== '') {
$asset->username = $uname;
}
$asset->save();
Db::commit(); Db::commit();
} catch (\Throwable $e) { } catch (\Throwable $e) {
Db::rollback(); Db::rollback();