| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- namespace App\Services;
- use App\Constants\HttpStatus;
- use App\Models\Backflow;
- use App\Models\Rebate;
- use App\Models\Wallet;
- use App\Models\WalletBonus;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\DB;
- use RuntimeException;
- /** Shared transaction for manual and automatic payouts. Never trusts a previously loaded reward row. */
- class RewardGrantService
- {
- public static function grant(string $type, int $id, string $operator, bool $skipPaid = false, ?string $scheduledCutoff = null): bool
- {
- if (!in_array($type, RewardGrantSettingsService::TYPES, true)) {
- throw new RuntimeException('奖励类型错误', HttpStatus::CUSTOM_ERROR);
- }
- return DB::transaction(function () use ($type, $id, $operator, $skipPaid, $scheduledCutoff) {
- $model = $type === 'rebate' ? Rebate::class : Backflow::class;
- $reward = $model::whereKey($id)->lockForUpdate()->first();
- if (!$reward) throw new RuntimeException('数据不存在', HttpStatus::CUSTOM_ERROR);
- // status 2 = 周期内提前发放过,仍可继续发放新增差额。
- if (!in_array((int) $reward->status, $model::OPEN_STATUSES, true)) {
- if ($skipPaid) return false;
- throw new RuntimeException('奖励已发放,无需再次发放', HttpStatus::CUSTOM_ERROR);
- }
- $cutoff = $type === 'rebate'
- ? Carbon::now('America/New_York')->format('Y-m-d')
- : Carbon::now('Asia/Shanghai')->format('Y-m');
- // 手动可提前发放本期;自动仍等周期结束,笔笔返不能发放未来日期。
- if (($scheduledCutoff !== null && (string) $reward->date >= $cutoff)
- || ($type === 'rebate' && (string) $reward->date > $cutoff)) {
- throw new RuntimeException('未到发放时间', HttpStatus::CUSTOM_ERROR);
- }
- if ($scheduledCutoff !== null && (string) $reward->date >= $scheduledCutoff) return false;
- $backflowDetails = '';
- if ($type === 'rebate') {
- $result = RewardBasisService::rebate($reward);
- $basis = $result['effective_amount'];
- $reward->betting_amount = $result['betting_amount'];
- $reward->effective_betting_amount = $basis;
- $reward->rebate_ratio = bccomp($basis, '0', 2) > 0 ? bcdiv($result['amount'], $basis, 6) : '0';
- $reward->audited_by = $operator;
- $changeType = '笔笔返';
- $remark = "日期:{$reward->date};有效投注:{$basis}";
- foreach ($result['details'] as $detail) {
- if ($detail['rule_id'] === null) continue;
- $remark .= ";{$detail['label']}:{$detail['effective_amount']}×{$detail['percentage']}%";
- }
- } else {
- $result = RewardBasisService::backflow($reward);
- $reward->loss_amount = $result['loss_amount'];
- $reward->backflow_ratio = '0';
- $changeType = '回水';
- $remark = "日期:{$reward->date};亏损:{$result['loss_amount']}";
- foreach ($result['details'] as $detail) {
- if ($detail['rule_id'] === null) continue;
- $backflowDetails .= ";{$detail['label']}亏损{$detail['loss_amount']}奖励{$detail['amount']}";
- }
- }
- $amount = $result['amount'];
- $needFlow = $result['need_flow'];
- $reward->details = $result['details'];
- $reward->need_flow = $needFlow;
- // 每次只发新增差额,金额回落不追回;未实际入账的打码量也不能记为已发放。
- $pay = self::positive(bcsub($amount, (string) $reward->granted_amount, 2));
- $payFlow = bccomp($pay, '0', 2) > 0 ? self::positive(bcsub($needFlow, (string) $reward->granted_need_flow, 2)) : '0.00';
- $closed = (string) $reward->date < $cutoff;
- if (!$closed && bccomp($pay, '0', 2) <= 0) {
- throw new RuntimeException("暂无可发放金额(按当前规则应发 {$amount},已发放 " . bcadd((string) $reward->granted_amount, '0', 2) . ')', HttpStatus::CUSTOM_ERROR);
- }
- $paymentRemark = ";本次发放:{$pay};累计发放:" . bcadd((string) $reward->granted_amount, $pay, 2);
- if ($type === 'backflow' && mb_strlen($remark . $backflowDetails . $paymentRemark) > 255) {
- $backflowDetails = ';档位明细见回水记录';
- }
- $remark .= $backflowDetails . $paymentRemark;
- $credit = null;
- if (bccomp($pay, '0', 2) > 0) {
- $wallet = Wallet::where('member_id', $reward->member_id)->lockForUpdate()->first();
- if (!$wallet) throw new RuntimeException('会员钱包不存在', HttpStatus::CUSTOM_ERROR);
- $before = (string) $wallet->available_balance;
- $wallet->available_balance = bcadd($before, $pay, 10);
- if (!$wallet->save()) throw new RuntimeException('余额更新失败', HttpStatus::CUSTOM_ERROR);
- $credit = BalanceLogService::addLog($reward->member_id, $pay, $before, $wallet->available_balance,
- $changeType, $reward->id, $remark);
- if (bccomp($payFlow, '0', 2) > 0) {
- WalletBonus::addData($reward->member_id, $type === 'rebate' ? 4 : 5, $pay, 0, $reward->id, $payFlow);
- // 规则配置了打码倍数时计入提现打码量
- WageringService::addRequirement($wallet, $payFlow, 'activity:' . $changeType, $reward->id, $remark);
- }
- }
- $reward->amount = $amount;
- $reward->granted_amount = bcadd((string) $reward->granted_amount, $pay, 2);
- $reward->granted_need_flow = bcadd((string) $reward->granted_need_flow, $payFlow, 2);
- $reward->status = $closed ? 1 : 2;
- $reward->grant_time = Carbon::now()->timestamp;
- if (!$reward->save()) throw new RuntimeException('发放失败', HttpStatus::CUSTOM_ERROR);
- if ($credit) RewardPayoutMessageService::send($credit, (string) $reward->date);
- return true;
- }, 3);
- }
- private static function positive(string $value): string
- {
- return bccomp($value, '0', 2) > 0 ? $value : '0.00';
- }
- }
|