优化数据归属问题
This commit is contained in:
@@ -81,11 +81,14 @@ return [
|
||||
'连胜奖励' => 'Win streak rewards',
|
||||
'连胜降低档位' => 'Streak reduction tiers',
|
||||
'钱包加减点' => 'Wallet adjust',
|
||||
'测试' => 'Test',
|
||||
'测试频道监听' => 'Test channel monitoring',
|
||||
'推送-对局公共频道' => 'Push: public game period',
|
||||
'推送-公告广播频道' => 'Push: operation notices',
|
||||
'推送-用户私有频道' => 'Push: user private',
|
||||
'渠道管理' => 'Channel management',
|
||||
'管理员提现记录' => 'Admin withdraw records',
|
||||
'一键批量结算待结算渠道' => 'Batch settle pending channels',
|
||||
'渠道结算统计' => 'Channel settlement statistics',
|
||||
|
||||
// 演示/运营公告标题(若入库为菜单展示)
|
||||
'系统维护通知(演示)' => 'Maintenance notice (demo)',
|
||||
|
||||
@@ -80,6 +80,11 @@ class Auth extends \ba\Auth
|
||||
$this->setError('Account disabled');
|
||||
return false;
|
||||
}
|
||||
$channelId = intval($this->model->channel_id ?? 0);
|
||||
if ($channelId > 0 && !$this->isChannelEnabled($channelId)) {
|
||||
$this->setError('Channel disabled');
|
||||
return false;
|
||||
}
|
||||
$this->token = $token;
|
||||
$this->loginEd = true;
|
||||
return true;
|
||||
@@ -136,6 +141,11 @@ class Auth extends \ba\Auth
|
||||
'remark' => User::formatLoginRemark($time, $ip),
|
||||
];
|
||||
$data = array_merge(compact('username', 'password', 'phone', 'email'), $data, $extend);
|
||||
$channelIdForRegister = isset($data['channel_id']) ? intval($data['channel_id']) : 0;
|
||||
if ($channelIdForRegister > 0 && !$this->isChannelEnabled($channelIdForRegister)) {
|
||||
$this->setError('Channel disabled');
|
||||
return false;
|
||||
}
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
@@ -178,6 +188,11 @@ class Auth extends \ba\Auth
|
||||
$this->setError('Account disabled');
|
||||
return false;
|
||||
}
|
||||
$channelId = intval($this->model->channel_id ?? 0);
|
||||
if ($channelId > 0 && !$this->isChannelEnabled($channelId)) {
|
||||
$this->setError('Channel disabled');
|
||||
return false;
|
||||
}
|
||||
|
||||
$userLoginRetry = config('buildadmin.user_login_retry');
|
||||
if ($userLoginRetry && $this->model->last_login_time) {
|
||||
@@ -382,4 +397,13 @@ class Auth extends \ba\Auth
|
||||
$this->setKeepTime((int)config('buildadmin.user_token_keep_time', 86400));
|
||||
return true;
|
||||
}
|
||||
|
||||
private function isChannelEnabled(int $channelId): bool
|
||||
{
|
||||
$status = Db::name('channel')->where('id', $channelId)->value('status');
|
||||
if ($status === null || $status === '') {
|
||||
return false;
|
||||
}
|
||||
return intval($status) === 1;
|
||||
}
|
||||
}
|
||||
|
||||
27
app/common/model/AdminWallet.php
Normal file
27
app/common/model/AdminWallet.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use support\think\Model;
|
||||
|
||||
class AdminWallet extends Model
|
||||
{
|
||||
protected $name = 'admin_wallet';
|
||||
|
||||
protected $autoWriteTimestamp = true;
|
||||
|
||||
protected $type = [
|
||||
'create_time' => 'integer',
|
||||
'update_time' => 'integer',
|
||||
'balance' => 'string',
|
||||
'frozen_balance' => 'string',
|
||||
'total_income' => 'string',
|
||||
'total_withdraw' => 'string',
|
||||
];
|
||||
|
||||
public function admin(): \think\model\relation\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(\app\admin\model\Admin::class, 'admin_id', 'id');
|
||||
}
|
||||
}
|
||||
|
||||
39
app/common/model/AdminWalletRecord.php
Normal file
39
app/common/model/AdminWalletRecord.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use support\think\Model;
|
||||
|
||||
class AdminWalletRecord extends Model
|
||||
{
|
||||
protected $name = 'admin_wallet_record';
|
||||
|
||||
protected $autoWriteTimestamp = false;
|
||||
|
||||
protected $createTime = 'create_time';
|
||||
|
||||
protected $updateTime = false;
|
||||
|
||||
protected $type = [
|
||||
'create_time' => 'integer',
|
||||
'amount' => 'string',
|
||||
'balance_before' => 'string',
|
||||
'balance_after' => 'string',
|
||||
];
|
||||
|
||||
public function admin(): \think\model\relation\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(\app\admin\model\Admin::class, 'admin_id', 'id');
|
||||
}
|
||||
|
||||
public function channel(): \think\model\relation\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Channel::class, 'channel_id', 'id');
|
||||
}
|
||||
|
||||
public function operatorAdmin(): \think\model\relation\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(\app\admin\model\Admin::class, 'operator_admin_id', 'id');
|
||||
}
|
||||
}
|
||||
|
||||
37
app/common/model/AdminWithdrawOrder.php
Normal file
37
app/common/model/AdminWithdrawOrder.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use support\think\Model;
|
||||
|
||||
class AdminWithdrawOrder extends Model
|
||||
{
|
||||
protected $name = 'admin_withdraw_order';
|
||||
|
||||
protected $autoWriteTimestamp = true;
|
||||
|
||||
protected $type = [
|
||||
'create_time' => 'integer',
|
||||
'update_time' => 'integer',
|
||||
'review_time' => 'integer',
|
||||
'amount' => 'string',
|
||||
'actual_amount' => 'string',
|
||||
'status' => 'integer',
|
||||
];
|
||||
|
||||
public function admin(): \think\model\relation\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(\app\admin\model\Admin::class, 'admin_id', 'id');
|
||||
}
|
||||
|
||||
public function channel(): \think\model\relation\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Channel::class, 'channel_id', 'id');
|
||||
}
|
||||
|
||||
public function reviewAdmin(): \think\model\relation\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(\app\admin\model\Admin::class, 'review_admin_id', 'id');
|
||||
}
|
||||
}
|
||||
|
||||
213
app/common/service/AdminWalletService.php
Normal file
213
app/common/service/AdminWalletService.php
Normal file
@@ -0,0 +1,213 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
use support\think\Db;
|
||||
|
||||
class AdminWalletService
|
||||
{
|
||||
public static function ensureWallet(int $adminId): array
|
||||
{
|
||||
$wallet = Db::name('admin_wallet')->where('admin_id', $adminId)->find();
|
||||
if (is_array($wallet)) {
|
||||
return $wallet;
|
||||
}
|
||||
$now = time();
|
||||
Db::name('admin_wallet')->insert([
|
||||
'admin_id' => $adminId,
|
||||
'balance' => '0.00',
|
||||
'frozen_balance' => '0.00',
|
||||
'total_income' => '0.00',
|
||||
'total_withdraw' => '0.00',
|
||||
'create_time' => $now,
|
||||
'update_time' => $now,
|
||||
]);
|
||||
return Db::name('admin_wallet')->where('admin_id', $adminId)->find() ?: [];
|
||||
}
|
||||
|
||||
public static function creditCommission(int $adminId, ?int $channelId, string $amount, string $refType, int $refId, string $remark): void
|
||||
{
|
||||
$wallet = self::ensureWallet($adminId);
|
||||
$before = strval($wallet['balance'] ?? '0.00');
|
||||
$after = bcadd($before, $amount, 2);
|
||||
$now = time();
|
||||
Db::name('admin_wallet')->where('admin_id', $adminId)->update([
|
||||
'balance' => $after,
|
||||
'total_income' => Db::raw('total_income + ' . $amount),
|
||||
'update_time' => $now,
|
||||
]);
|
||||
Db::name('admin_wallet_record')->insert([
|
||||
'admin_id' => $adminId,
|
||||
'channel_id' => $channelId,
|
||||
'biz_type' => 'commission_income',
|
||||
'direction' => 1,
|
||||
'amount' => $amount,
|
||||
'balance_before' => $before,
|
||||
'balance_after' => $after,
|
||||
'ref_type' => $refType,
|
||||
'ref_id' => $refId,
|
||||
'idempotency_key' => 'commission_income_' . $adminId . '_' . $refId,
|
||||
'operator_admin_id' => null,
|
||||
'remark' => $remark,
|
||||
'create_time' => $now,
|
||||
]);
|
||||
}
|
||||
|
||||
public static function applyWithdraw(
|
||||
int $adminId,
|
||||
int $channelId,
|
||||
string $withdrawCoin,
|
||||
string $receiveType,
|
||||
string $receiveAccount,
|
||||
string $idempotencyKey,
|
||||
string $remark
|
||||
): array
|
||||
{
|
||||
$existing = Db::name('admin_withdraw_order')->where('idempotency_key', $idempotencyKey)->find();
|
||||
if (is_array($existing)) {
|
||||
$existAdminId = intval($existing['admin_id'] ?? 0);
|
||||
if ($existAdminId !== $adminId) {
|
||||
return ['ok' => false, 'msg' => 'Idempotency key conflict'];
|
||||
}
|
||||
return [
|
||||
'ok' => true,
|
||||
'order_id' => intval($existing['id'] ?? 0),
|
||||
'order_no' => strval($existing['order_no'] ?? ''),
|
||||
'idempotent_hit' => true,
|
||||
];
|
||||
}
|
||||
|
||||
$wallet = self::ensureWallet($adminId);
|
||||
$before = strval($wallet['balance'] ?? '0.00');
|
||||
if (bccomp($before, $withdrawCoin, 2) < 0) {
|
||||
return ['ok' => false, 'msg' => '钱包余额不足'];
|
||||
}
|
||||
$after = bcsub($before, $withdrawCoin, 2);
|
||||
$beforeFrozen = strval($wallet['frozen_balance'] ?? '0.00');
|
||||
$afterFrozen = bcadd($beforeFrozen, $withdrawCoin, 2);
|
||||
$now = time();
|
||||
$orderNo = 'AWD' . date('YmdHis') . str_pad(strval($adminId), 6, '0', STR_PAD_LEFT) . strval(random_int(1000, 9999));
|
||||
|
||||
Db::name('admin_wallet')->where('admin_id', $adminId)->update([
|
||||
'balance' => $after,
|
||||
'frozen_balance' => $afterFrozen,
|
||||
'update_time' => $now,
|
||||
]);
|
||||
$orderId = Db::name('admin_withdraw_order')->insertGetId([
|
||||
'order_no' => $orderNo,
|
||||
'admin_id' => $adminId,
|
||||
'channel_id' => $channelId > 0 ? $channelId : null,
|
||||
'amount' => $withdrawCoin,
|
||||
'actual_amount' => $withdrawCoin,
|
||||
'status' => 0,
|
||||
'receive_type' => $receiveType,
|
||||
'receive_account' => $receiveAccount,
|
||||
'idempotency_key' => $idempotencyKey,
|
||||
'review_admin_id' => null,
|
||||
'review_time' => null,
|
||||
'remark' => $remark,
|
||||
'create_time' => $now,
|
||||
'update_time' => $now,
|
||||
]);
|
||||
Db::name('admin_wallet_record')->insert([
|
||||
'admin_id' => $adminId,
|
||||
'channel_id' => $channelId > 0 ? $channelId : null,
|
||||
'biz_type' => 'withdraw_freeze',
|
||||
'direction' => 2,
|
||||
'amount' => $withdrawCoin,
|
||||
'balance_before' => $before,
|
||||
'balance_after' => $after,
|
||||
'ref_type' => 'admin_withdraw_order',
|
||||
'ref_id' => $orderId,
|
||||
'idempotency_key' => 'admin_withdraw_freeze_' . $orderId,
|
||||
'operator_admin_id' => $adminId,
|
||||
'remark' => $remark !== '' ? $remark : '管理员提现申请冻结',
|
||||
'create_time' => $now,
|
||||
]);
|
||||
|
||||
return ['ok' => true, 'order_id' => $orderId, 'order_no' => $orderNo];
|
||||
}
|
||||
|
||||
public static function approveWithdraw(array $order, int $reviewAdminId, string $remark): void
|
||||
{
|
||||
$orderId = intval($order['id'] ?? 0);
|
||||
$adminId = intval($order['admin_id'] ?? 0);
|
||||
$amount = strval($order['amount'] ?? '0.00');
|
||||
$wallet = self::ensureWallet($adminId);
|
||||
$frozen = strval($wallet['frozen_balance'] ?? '0.00');
|
||||
$afterFrozen = bcsub($frozen, $amount, 2);
|
||||
$now = time();
|
||||
|
||||
Db::name('admin_wallet')->where('admin_id', $adminId)->update([
|
||||
'frozen_balance' => $afterFrozen,
|
||||
'total_withdraw' => Db::raw('total_withdraw + ' . $amount),
|
||||
'update_time' => $now,
|
||||
]);
|
||||
Db::name('admin_withdraw_order')->where('id', $orderId)->update([
|
||||
'status' => 1,
|
||||
'review_admin_id' => $reviewAdminId,
|
||||
'review_time' => $now,
|
||||
'remark' => $remark,
|
||||
'update_time' => $now,
|
||||
]);
|
||||
Db::name('admin_wallet_record')->insert([
|
||||
'admin_id' => $adminId,
|
||||
'channel_id' => $order['channel_id'] ?? null,
|
||||
'biz_type' => 'withdraw_success',
|
||||
'direction' => 2,
|
||||
'amount' => $amount,
|
||||
'balance_before' => strval($wallet['balance'] ?? '0.00'),
|
||||
'balance_after' => strval($wallet['balance'] ?? '0.00'),
|
||||
'ref_type' => 'admin_withdraw_order',
|
||||
'ref_id' => $orderId,
|
||||
'idempotency_key' => 'admin_withdraw_success_' . $orderId,
|
||||
'operator_admin_id' => $reviewAdminId,
|
||||
'remark' => $remark !== '' ? $remark : '管理员提现审核通过',
|
||||
'create_time' => $now,
|
||||
]);
|
||||
}
|
||||
|
||||
public static function rejectWithdraw(array $order, int $reviewAdminId, string $remark): void
|
||||
{
|
||||
$orderId = intval($order['id'] ?? 0);
|
||||
$adminId = intval($order['admin_id'] ?? 0);
|
||||
$amount = strval($order['amount'] ?? '0.00');
|
||||
$wallet = self::ensureWallet($adminId);
|
||||
$before = strval($wallet['balance'] ?? '0.00');
|
||||
$after = bcadd($before, $amount, 2);
|
||||
$frozen = strval($wallet['frozen_balance'] ?? '0.00');
|
||||
$afterFrozen = bcsub($frozen, $amount, 2);
|
||||
$now = time();
|
||||
|
||||
Db::name('admin_wallet')->where('admin_id', $adminId)->update([
|
||||
'balance' => $after,
|
||||
'frozen_balance' => $afterFrozen,
|
||||
'update_time' => $now,
|
||||
]);
|
||||
Db::name('admin_withdraw_order')->where('id', $orderId)->update([
|
||||
'status' => 2,
|
||||
'review_admin_id' => $reviewAdminId,
|
||||
'review_time' => $now,
|
||||
'remark' => $remark,
|
||||
'update_time' => $now,
|
||||
]);
|
||||
Db::name('admin_wallet_record')->insert([
|
||||
'admin_id' => $adminId,
|
||||
'channel_id' => $order['channel_id'] ?? null,
|
||||
'biz_type' => 'withdraw_refund',
|
||||
'direction' => 1,
|
||||
'amount' => $amount,
|
||||
'balance_before' => $before,
|
||||
'balance_after' => $after,
|
||||
'ref_type' => 'admin_withdraw_order',
|
||||
'ref_id' => $orderId,
|
||||
'idempotency_key' => 'admin_withdraw_refund_' . $orderId,
|
||||
'operator_admin_id' => $reviewAdminId,
|
||||
'remark' => $remark !== '' ? $remark : '管理员提现审核拒绝退回',
|
||||
'create_time' => $now,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
470
app/common/service/ChannelSettlementService.php
Normal file
470
app/common/service/ChannelSettlementService.php
Normal file
@@ -0,0 +1,470 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
use support\think\Db;
|
||||
use Throwable;
|
||||
|
||||
class ChannelSettlementService
|
||||
{
|
||||
public static function settleBySuperAdmin(int $channelId, int $operatorAdminId, string $remark = '', bool $auto = false): array
|
||||
{
|
||||
$channel = Db::name('channel')->where('id', $channelId)->find();
|
||||
if (!is_array($channel)) {
|
||||
return ['ok' => false, 'msg' => '渠道不存在'];
|
||||
}
|
||||
$payload = self::buildSettlePayload($channel);
|
||||
if (is_string($payload)) {
|
||||
return ['ok' => false, 'msg' => $payload];
|
||||
}
|
||||
$settlementNo = self::generateAgentSettlementNo($auto ? 'A' : 'M', $channelId, intval($payload['period_end_ts']));
|
||||
if (Db::name('agent_settlement_period')->where('settlement_no', $settlementNo)->value('id')) {
|
||||
return ['ok' => false, 'msg' => '结算单号冲突,请重试'];
|
||||
}
|
||||
$shareRows = self::resolveCommissionSharesForChannel($channelId);
|
||||
if ($shareRows === []) {
|
||||
return ['ok' => false, 'msg' => '渠道下无可用管理员分配比例,无法结算'];
|
||||
}
|
||||
$now = time();
|
||||
Db::startTrans();
|
||||
try {
|
||||
$periodId = intval(Db::name('agent_settlement_period')->insertGetId([
|
||||
'settlement_no' => $settlementNo,
|
||||
'period_start_at' => $payload['period_start_ts'],
|
||||
'period_end_at' => $payload['period_end_ts'],
|
||||
'total_bet_amount' => $payload['total_bet_amount'],
|
||||
'total_payout_amount' => $payload['total_payout_amount'],
|
||||
'platform_profit_amount' => $payload['platform_profit_amount'],
|
||||
'status' => 1,
|
||||
'remark' => $remark !== '' ? $remark : (($auto ? '自动' : '手动') . '渠道结算-CH' . $channelId),
|
||||
'create_time' => $now,
|
||||
'update_time' => $now,
|
||||
]));
|
||||
$rows = self::buildCommissionRowsForSplit(
|
||||
$shareRows,
|
||||
$channelId,
|
||||
$periodId,
|
||||
strval($payload['calc_base_amount']),
|
||||
strval($payload['commission_amount']),
|
||||
$remark !== '' ? $remark : '渠道待分红记录',
|
||||
$now
|
||||
);
|
||||
if ($rows === []) {
|
||||
throw new \RuntimeException('生成待分红记录失败');
|
||||
}
|
||||
Db::name('agent_commission_record')->insertAll($rows);
|
||||
Db::name('channel')->where('id', $channelId)->update([
|
||||
'carryover_balance' => Db::raw('carryover_balance + ' . strval($payload['commission_amount'])),
|
||||
'update_time' => $now,
|
||||
]);
|
||||
Db::commit();
|
||||
} catch (Throwable $e) {
|
||||
Db::rollback();
|
||||
return ['ok' => false, 'msg' => $e->getMessage()];
|
||||
}
|
||||
return ['ok' => true, 'payload' => $payload];
|
||||
}
|
||||
|
||||
public static function settleDividendByChannelAdmin(int $channelId, int $operatorAdminId, string $remark = ''): array
|
||||
{
|
||||
$channel = Db::name('channel')->where('id', $channelId)->find();
|
||||
if (!is_array($channel)) {
|
||||
return ['ok' => false, 'msg' => '渠道不存在'];
|
||||
}
|
||||
$carryover = strval($channel['carryover_balance'] ?? '0.00');
|
||||
if (bccomp($carryover, '0', 2) <= 0) {
|
||||
return ['ok' => false, 'msg' => '当前渠道没有分红余额,待下周期结算'];
|
||||
}
|
||||
$pendingRows = Db::name('agent_commission_record')
|
||||
->where('channel_id', $channelId)
|
||||
->where('status', 0)
|
||||
->order('id', 'asc')
|
||||
->select()
|
||||
->toArray();
|
||||
if ($pendingRows === []) {
|
||||
return ['ok' => false, 'msg' => '当前渠道没有待分红记录,待下周期结算'];
|
||||
}
|
||||
$totalPending = '0.00';
|
||||
foreach ($pendingRows as $pendingRow) {
|
||||
$totalPending = bcadd($totalPending, strval($pendingRow['commission_amount'] ?? '0.00'), 2);
|
||||
}
|
||||
if (bccomp($carryover, $totalPending, 2) < 0) {
|
||||
return ['ok' => false, 'msg' => '渠道可分红余额不足,请联系超管核对结算'];
|
||||
}
|
||||
$now = time();
|
||||
Db::startTrans();
|
||||
try {
|
||||
foreach ($pendingRows as $pendingRow) {
|
||||
$amount = strval($pendingRow['commission_amount'] ?? '0.00');
|
||||
$adminId = intval($pendingRow['admin_id'] ?? 0);
|
||||
if ($adminId <= 0 || bccomp($amount, '0', 2) <= 0) {
|
||||
continue;
|
||||
}
|
||||
AdminWalletService::creditCommission(
|
||||
$adminId,
|
||||
$channelId,
|
||||
$amount,
|
||||
'agent_commission_record',
|
||||
intval($pendingRow['id'] ?? 0),
|
||||
$remark !== '' ? $remark : '渠道分红结算入账'
|
||||
);
|
||||
}
|
||||
Db::name('agent_commission_record')
|
||||
->where('channel_id', $channelId)
|
||||
->where('status', 0)
|
||||
->update([
|
||||
'status' => 1,
|
||||
'settled_at' => $now,
|
||||
'update_time' => $now,
|
||||
'remark' => Db::raw("CONCAT(remark, ' | 渠道结算确认')"),
|
||||
]);
|
||||
Db::name('channel')->where('id', $channelId)->update([
|
||||
'carryover_balance' => bcsub($carryover, $totalPending, 2),
|
||||
'update_time' => $now,
|
||||
]);
|
||||
$periodIds = Db::name('agent_commission_record')->where('channel_id', $channelId)->where('status', 1)->column('settlement_period_id');
|
||||
if ($periodIds !== []) {
|
||||
foreach ($periodIds as $periodIdRaw) {
|
||||
$periodId = intval($periodIdRaw);
|
||||
if ($periodId <= 0) {
|
||||
continue;
|
||||
}
|
||||
$left = intval(Db::name('agent_commission_record')->where('settlement_period_id', $periodId)->where('status', 0)->count());
|
||||
if ($left === 0) {
|
||||
Db::name('agent_settlement_period')->where('id', $periodId)->update([
|
||||
'status' => 2,
|
||||
'update_time' => $now,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Db::commit();
|
||||
} catch (Throwable $e) {
|
||||
Db::rollback();
|
||||
return ['ok' => false, 'msg' => $e->getMessage()];
|
||||
}
|
||||
return ['ok' => true, 'settled_amount' => $totalPending];
|
||||
}
|
||||
|
||||
public static function settleAllDueChannels(int $operatorAdminId): array
|
||||
{
|
||||
$channels = Db::name('channel')->where('status', 1)->select()->toArray();
|
||||
$ok = 0;
|
||||
$failed = [];
|
||||
$now = time();
|
||||
foreach ($channels as $channel) {
|
||||
$channelId = intval($channel['id'] ?? 0);
|
||||
if ($channelId <= 0) {
|
||||
continue;
|
||||
}
|
||||
if (!self::isChannelDueForAutoSettle($channel, $now)) {
|
||||
continue;
|
||||
}
|
||||
$res = self::settleBySuperAdmin($channelId, $operatorAdminId, '周期自动结算', true);
|
||||
if (($res['ok'] ?? false) === true) {
|
||||
$ok++;
|
||||
continue;
|
||||
}
|
||||
$failed[] = [
|
||||
'channel_id' => $channelId,
|
||||
'msg' => strval($res['msg'] ?? '结算失败'),
|
||||
];
|
||||
}
|
||||
return ['ok_count' => $ok, 'failed' => $failed];
|
||||
}
|
||||
|
||||
private static function isChannelDueForAutoSettle(array $channel, int $now): bool
|
||||
{
|
||||
$channelId = intval($channel['id'] ?? 0);
|
||||
if ($channelId <= 0) {
|
||||
return false;
|
||||
}
|
||||
$lastEnd = self::getLastSettlementEndForChannel($channelId);
|
||||
$cycle = strval($channel['settle_cycle'] ?? 'weekly');
|
||||
$settleTime = strval($channel['settle_time'] ?? '02:00:00');
|
||||
$today = date('Y-m-d', $now);
|
||||
$targetTs = strtotime($today . ' ' . $settleTime);
|
||||
if ($targetTs === false || $now < $targetTs) {
|
||||
return false;
|
||||
}
|
||||
if ($lastEnd !== null && $lastEnd >= $targetTs) {
|
||||
return false;
|
||||
}
|
||||
if ($cycle === 'daily') {
|
||||
return true;
|
||||
}
|
||||
if ($cycle === 'weekly') {
|
||||
$weekday = intval($channel['settle_weekday'] ?? 1);
|
||||
$w = intval(date('N', $now));
|
||||
return $weekday === $w;
|
||||
}
|
||||
if ($cycle === 'monthly') {
|
||||
$monthday = intval($channel['settle_monthday'] ?? 1);
|
||||
$d = intval(date('j', $now));
|
||||
return $monthday === $d;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function buildSettlePayload(array $row): array|string
|
||||
{
|
||||
$channelId = intval($row['id'] ?? 0);
|
||||
if ($channelId <= 0) {
|
||||
return '渠道数据异常';
|
||||
}
|
||||
$endTs = time();
|
||||
$lastEnd = self::getLastSettlementEndForChannel($channelId);
|
||||
$channelCreateTs = intval($row['create_time'] ?? 0);
|
||||
$periodStartTs = $lastEnd === null ? ($channelCreateTs > 0 ? $channelCreateTs : 0) : $lastEnd;
|
||||
if ($periodStartTs >= $endTs) {
|
||||
return '结算区间无效(开始时间不早于当前)';
|
||||
}
|
||||
$stats = self::aggregateBetOrderForChannel($channelId, $periodStartTs, $lastEnd !== null, $endTs);
|
||||
$totalBet = $stats['total_bet'];
|
||||
$totalPayout = $stats['total_payout'];
|
||||
$profit = bcsub($totalBet, $totalPayout, 2);
|
||||
$mode = strval($row['agent_mode'] ?? 'turnover');
|
||||
$commission = self::computeCommissionAmounts($row, $totalBet, $profit, $mode);
|
||||
if (is_string($commission)) {
|
||||
return $commission;
|
||||
}
|
||||
return [
|
||||
'period_start_ts' => $periodStartTs,
|
||||
'period_end_ts' => $endTs,
|
||||
'period_start_at' => date('Y-m-d H:i:s', $periodStartTs),
|
||||
'period_end_at' => date('Y-m-d H:i:s', $endTs),
|
||||
'total_bet_amount' => $totalBet,
|
||||
'total_payout_amount' => $totalPayout,
|
||||
'platform_profit_amount' => $profit,
|
||||
'commission_rate' => $commission['commission_rate'],
|
||||
'calc_base_amount' => $commission['calc_base_amount'],
|
||||
'commission_amount' => $commission['commission_amount'],
|
||||
'agent_mode' => $mode,
|
||||
'commission_split' => self::buildCommissionSplitPreview(self::resolveCommissionSharesForChannel($channelId), $commission['commission_amount']),
|
||||
];
|
||||
}
|
||||
|
||||
private static function getLastSettlementEndForChannel(int $channelId): ?int
|
||||
{
|
||||
$row = Db::name('agent_commission_record')->alias('acr')
|
||||
->join('agent_settlement_period asp', 'acr.settlement_period_id = asp.id')
|
||||
->where('acr.channel_id', $channelId)
|
||||
->field('MAX(asp.period_end_at) AS m')
|
||||
->find();
|
||||
if (!is_array($row)) {
|
||||
return null;
|
||||
}
|
||||
$m = $row['m'] ?? null;
|
||||
if ($m === null || $m === '') {
|
||||
return null;
|
||||
}
|
||||
return intval($m);
|
||||
}
|
||||
|
||||
private static function aggregateBetOrderForChannel(int $channelId, int $periodStartTs, bool $hasPriorSettlement, int $endTs): array
|
||||
{
|
||||
$query = Db::name('bet_order')
|
||||
->where('channel_id', $channelId)
|
||||
->where('status', 2)
|
||||
->where('create_time', '<=', $endTs);
|
||||
if ($hasPriorSettlement) {
|
||||
$query->where('create_time', '>', $periodStartTs);
|
||||
} else {
|
||||
$query->where('create_time', '>=', $periodStartTs);
|
||||
}
|
||||
$row = $query->field('SUM(total_amount) AS tb, SUM(win_amount) AS tw, SUM(jackpot_extra_amount) AS tj')->find();
|
||||
$tb = is_array($row) && $row['tb'] !== null && $row['tb'] !== '' ? strval($row['tb']) : '0.00';
|
||||
$tw = is_array($row) && $row['tw'] !== null && $row['tw'] !== '' ? strval($row['tw']) : '0.00';
|
||||
$tj = is_array($row) && $row['tj'] !== null && $row['tj'] !== '' ? strval($row['tj']) : '0.00';
|
||||
$totalPayout = bcadd($tw, $tj, 2);
|
||||
return ['total_bet' => bcadd($tb, '0', 2), 'total_payout' => bcadd($totalPayout, '0', 2)];
|
||||
}
|
||||
|
||||
private static function computeCommissionAmounts(array $row, string $totalBet, string $platformProfit, string $mode): array|string
|
||||
{
|
||||
if ($mode === 'turnover') {
|
||||
$ratePercent = $row['turnover_share_rate'] ?? null;
|
||||
if ($ratePercent === null || $ratePercent === '') {
|
||||
return '普通返水代理未配置返水分红比例';
|
||||
}
|
||||
$rateDec = bcdiv(strval($ratePercent), '100', 4);
|
||||
return [
|
||||
'commission_rate' => $rateDec,
|
||||
'calc_base_amount' => $totalBet,
|
||||
'commission_amount' => bcmul($totalBet, $rateDec, 2),
|
||||
];
|
||||
}
|
||||
if ($mode === 'affiliate') {
|
||||
$fee = $row['affiliate_fee_rate'] ?? null;
|
||||
$rulesRaw = $row['affiliate_ladder_rules'] ?? null;
|
||||
if ($fee === null || $fee === '') {
|
||||
return '联营代理未配置成本扣除比例';
|
||||
}
|
||||
$rules = self::normalizeLadderRulesForSettlement($rulesRaw);
|
||||
if ($rules === []) {
|
||||
return '联营阶梯规则无效或为空';
|
||||
}
|
||||
if (bccomp($platformProfit, '0', 2) <= 0) {
|
||||
return ['commission_rate' => '0.0000', 'calc_base_amount' => '0.00', 'commission_amount' => '0.00'];
|
||||
}
|
||||
$afterFee = bcmul($platformProfit, bcsub('1', strval($fee), 4), 2);
|
||||
if (bccomp($afterFee, '0', 2) <= 0) {
|
||||
return ['commission_rate' => '0.0000', 'calc_base_amount' => '0.00', 'commission_amount' => '0.00'];
|
||||
}
|
||||
$shareRate = self::pickAffiliateShareRateFromLadder($rules, $platformProfit);
|
||||
$rateDec = number_format($shareRate, 6, '.', '');
|
||||
return [
|
||||
'commission_rate' => $rateDec,
|
||||
'calc_base_amount' => $afterFee,
|
||||
'commission_amount' => bcmul($afterFee, $rateDec, 2),
|
||||
];
|
||||
}
|
||||
return '未知的代理模式';
|
||||
}
|
||||
|
||||
private static function normalizeLadderRulesForSettlement(mixed $rulesRaw): array
|
||||
{
|
||||
if ($rulesRaw === null || $rulesRaw === '') {
|
||||
return [];
|
||||
}
|
||||
if (is_string($rulesRaw)) {
|
||||
$decoded = json_decode($rulesRaw, true);
|
||||
$rulesRaw = is_array($decoded) ? $decoded : [];
|
||||
}
|
||||
if (!is_array($rulesRaw)) {
|
||||
return [];
|
||||
}
|
||||
$out = [];
|
||||
foreach ($rulesRaw as $rule) {
|
||||
if (!is_array($rule)) {
|
||||
continue;
|
||||
}
|
||||
$minLoss = $rule['minLoss'] ?? ($rule['min_loss'] ?? null);
|
||||
$shareRate = $rule['shareRate'] ?? ($rule['share_rate'] ?? null);
|
||||
if ($minLoss === null || $shareRate === null || !is_numeric(strval($minLoss)) || !is_numeric(strval($shareRate))) {
|
||||
continue;
|
||||
}
|
||||
$out[] = [
|
||||
'minLoss' => number_format(floatval($minLoss), 4, '.', ''),
|
||||
'shareRate' => number_format(floatval($shareRate), 6, '.', ''),
|
||||
];
|
||||
}
|
||||
usort($out, static function (array $a, array $b): int {
|
||||
return bccomp($a['minLoss'], $b['minLoss'], 4);
|
||||
});
|
||||
return $out;
|
||||
}
|
||||
|
||||
private static function pickAffiliateShareRateFromLadder(array $rules, string $playerLoss): float
|
||||
{
|
||||
$chosen = floatval($rules[0]['shareRate']);
|
||||
foreach ($rules as $rule) {
|
||||
if (bccomp($playerLoss, strval($rule['minLoss']), 2) >= 0) {
|
||||
$chosen = floatval($rule['shareRate']);
|
||||
}
|
||||
}
|
||||
return $chosen;
|
||||
}
|
||||
|
||||
private static function generateAgentSettlementNo(string $sourceFlag, int $channelId, int $endTs): string
|
||||
{
|
||||
$flag = strtoupper(trim($sourceFlag));
|
||||
if ($flag !== 'M' && $flag !== 'A') {
|
||||
$flag = 'M';
|
||||
}
|
||||
$base = $flag . str_pad(strval(max(0, $channelId)), 6, '0', STR_PAD_LEFT) . str_pad(strval(max(0, $endTs)), 10, '0', STR_PAD_LEFT);
|
||||
return $base . strtoupper(substr(bin2hex(random_bytes(4)), 0, 2));
|
||||
}
|
||||
|
||||
private static function resolveCommissionSharesForChannel(int $channelId): array
|
||||
{
|
||||
$rows = Db::name('channel_admin_share')->alias('cas')
|
||||
->join('admin a', 'cas.admin_id = a.id')
|
||||
->field(['cas.admin_id', 'cas.share_rate'])
|
||||
->where('cas.channel_id', $channelId)
|
||||
->where('cas.status', 1)
|
||||
->where('a.status', 'enable')
|
||||
->order('cas.admin_id', 'asc')
|
||||
->select()
|
||||
->toArray();
|
||||
if ($rows === []) {
|
||||
return [];
|
||||
}
|
||||
$sum = '0.00';
|
||||
$out = [];
|
||||
foreach ($rows as $row) {
|
||||
$adminId = intval($row['admin_id'] ?? 0);
|
||||
$shareRate = bcadd(strval($row['share_rate'] ?? '0'), '0', 2);
|
||||
if ($adminId <= 0 || bccomp($shareRate, '0', 2) <= 0) {
|
||||
continue;
|
||||
}
|
||||
$sum = bcadd($sum, $shareRate, 2);
|
||||
$out[] = ['admin_id' => $adminId, 'share_rate' => $shareRate];
|
||||
}
|
||||
if ($out === [] || bccomp($sum, '100.00', 2) !== 0) {
|
||||
return [];
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
private static function buildCommissionRowsForSplit(array $shareRows, int $channelId, int $periodId, string $calcBaseAmount, string $commissionTotal, string $remark, int $now): array
|
||||
{
|
||||
$sum = '0.00';
|
||||
$rows = [];
|
||||
$lastIndex = count($shareRows) - 1;
|
||||
foreach ($shareRows as $index => $shareRow) {
|
||||
$shareRate = bcadd(strval($shareRow['share_rate'] ?? '0.00'), '0', 2);
|
||||
$shareDec = bcdiv($shareRate, '100', 4);
|
||||
$amount = $index === $lastIndex ? bcsub($commissionTotal, $sum, 2) : bcmul($commissionTotal, $shareDec, 2);
|
||||
if ($index !== $lastIndex) {
|
||||
$sum = bcadd($sum, $amount, 2);
|
||||
}
|
||||
$effectiveRate = bccomp($calcBaseAmount, '0', 2) <= 0 ? '0.0000' : bcdiv($amount, $calcBaseAmount, 6);
|
||||
$rows[] = [
|
||||
'settlement_period_id' => $periodId,
|
||||
'channel_id' => $channelId,
|
||||
'admin_id' => intval($shareRow['admin_id'] ?? 0),
|
||||
'commission_rate' => $effectiveRate,
|
||||
'calc_base_amount' => $calcBaseAmount,
|
||||
'commission_amount' => $amount,
|
||||
'status' => 0,
|
||||
'settled_at' => null,
|
||||
'remark' => $remark . ' | 分配比例=' . $shareRate . '%',
|
||||
'create_time' => $now,
|
||||
'update_time' => $now,
|
||||
];
|
||||
}
|
||||
return $rows;
|
||||
}
|
||||
|
||||
private static function buildCommissionSplitPreview(array $shareRows, string $commissionTotal): array
|
||||
{
|
||||
if ($shareRows === []) {
|
||||
return [];
|
||||
}
|
||||
$adminIds = array_map(static fn(array $row): int => intval($row['admin_id'] ?? 0), $shareRows);
|
||||
$adminNames = Db::name('admin')->where('id', 'in', $adminIds)->column('username', 'id');
|
||||
$sum = '0.00';
|
||||
$out = [];
|
||||
$lastIndex = count($shareRows) - 1;
|
||||
foreach ($shareRows as $index => $shareRow) {
|
||||
$shareRate = bcadd(strval($shareRow['share_rate'] ?? '0.00'), '0', 2);
|
||||
$shareDec = bcdiv($shareRate, '100', 4);
|
||||
$amount = $index === $lastIndex ? bcsub($commissionTotal, $sum, 2) : bcmul($commissionTotal, $shareDec, 2);
|
||||
if ($index !== $lastIndex) {
|
||||
$sum = bcadd($sum, $amount, 2);
|
||||
}
|
||||
$aid = intval($shareRow['admin_id'] ?? 0);
|
||||
$out[] = [
|
||||
'admin_id' => $aid,
|
||||
'admin_username' => strval($adminNames[$aid] ?? ('#' . $aid)),
|
||||
'share_rate' => $shareRate,
|
||||
'commission_amount' => $amount,
|
||||
];
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user