优化数据归属问题

This commit is contained in:
2026-04-23 15:08:37 +08:00
parent 378be9909d
commit 0373234750
29 changed files with 1993 additions and 75 deletions

View File

@@ -4,6 +4,7 @@ namespace app\admin\controller;
use Throwable;
use app\common\controller\Backend;
use app\common\service\ChannelSettlementService;
use support\think\Db;
use support\Response;
use Webman\Http\Request as WebmanRequest;
@@ -16,7 +17,7 @@ class Channel extends Backend
/**
* 预览接口与手动结算共用「手动结算」按钮权限(避免额外菜单节点)
*/
protected array $noNeedPermission = ['manualSettlePreview', 'channelAdminShareList', 'saveChannelAdminShare'];
protected array $noNeedPermission = ['manualSettlePreview', 'channelAdminShareList', 'saveChannelAdminShare', 'batchSettlePending', 'settleStats'];
/**
* Channel模型对象
@@ -314,7 +315,7 @@ class Channel extends Backend
return $this->error(__('You have no permission'));
}
$payload = $this->buildManualSettlePayload($row->toArray());
$payload = ChannelSettlementService::buildSettlePayload($row->toArray());
if (is_string($payload)) {
return $this->error($payload);
}
@@ -611,63 +612,80 @@ class Channel extends Backend
return $this->error(__('You have no permission'));
}
$remark = (string) $request->post('remark', '');
$remark = trim((string) $request->post('remark', ''));
$payload = $this->buildManualSettlePayload($row->toArray());
if (is_string($payload)) {
return $this->error($payload);
}
$settlementNo = $payload['settlement_no'];
if (Db::name('agent_settlement_period')->where('settlement_no', $settlementNo)->value('id')) {
return $this->error('结算单号已存在,请稍后重试');
}
$shareRows = $this->resolveCommissionSharesForChannel((int) $row['id']);
if ($shareRows === []) {
return $this->error('渠道下无可用管理员分配比例,无法生成佣金记录');
}
$now = time();
Db::startTrans();
try {
$periodId = (int) 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' => 2,
'remark' => trim($remark) !== '' ? $remark : ('手动结算-渠道#' . $row['id'] . '-' . $row['name']),
'create_time' => $now,
'update_time' => $now,
]);
$commissionRows = $this->buildCommissionRowsForSplit(
$shareRows,
(int) $row['id'],
$periodId,
(string) $payload['calc_base_amount'],
(string) $payload['commission_amount'],
trim($remark) !== '' ? $remark : ('手动结算佣金-CH' . $row['id']),
$now
);
if ($commissionRows === []) {
throw new \RuntimeException('分配比例拆分失败,未生成佣金记录');
if ($this->auth->isSuperAdmin()) {
$res = ChannelSettlementService::settleBySuperAdmin((int) $row['id'], intval($this->auth->id), $remark, false);
if (($res['ok'] ?? false) !== true) {
return $this->error((string) ($res['msg'] ?? '结算失败'));
}
Db::name('agent_commission_record')->insertAll($commissionRows);
Db::name('channel')->where('id', $row['id'])->update([
'update_time' => $now,
]);
Db::commit();
} catch (Throwable $e) {
Db::rollback();
return $this->error($e->getMessage());
return $this->success('超管结算完成,渠道分红余额已入账');
}
$res = ChannelSettlementService::settleDividendByChannelAdmin((int) $row['id'], intval($this->auth->id), $remark);
if (($res['ok'] ?? false) !== true) {
return $this->error((string) ($res['msg'] ?? '结算失败'));
}
return $this->success('渠道分红已结算完成');
}
return $this->success('手动结算已完成,已生成结算周期与佣金记录');
/**
* 超管批量结算全部待结算渠道(可作为“提前结算”入口)
*/
public function batchSettlePending(WebmanRequest $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
if (!$this->auth->isSuperAdmin()) {
return $this->error(__('You have no permission'));
}
$res = ChannelSettlementService::settleAllDueChannels(intval($this->auth->id));
return $this->success('批量结算完成', $res);
}
/**
* 渠道结算统计卡片
*/
public function settleStats(WebmanRequest $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
$query = Db::name('channel');
if (!$this->auth->isSuperAdmin()) {
$query->where('id', 'in', $this->currentChannelIds ?: [0]);
}
$rows = $query->field(['id', 'status', 'carryover_balance'])->select()->toArray();
$total = count($rows);
$enabled = 0;
$disabled = 0;
$carryoverPositiveCount = 0;
$carryoverTotal = '0.00';
$carryoverPositiveTotal = '0.00';
foreach ($rows as $row) {
$status = intval($row['status'] ?? 0);
if ($status === 1) {
$enabled++;
} else {
$disabled++;
}
$carry = bcadd(strval($row['carryover_balance'] ?? '0'), '0', 2);
$carryoverTotal = bcadd($carryoverTotal, $carry, 2);
if (bccomp($carry, '0', 2) > 0) {
$carryoverPositiveCount++;
$carryoverPositiveTotal = bcadd($carryoverPositiveTotal, $carry, 2);
}
}
return $this->success('', [
'channel_total' => $total,
'enabled_count' => $enabled,
'disabled_count' => $disabled,
'carryover_positive_count' => $carryoverPositiveCount,
'carryover_total' => $carryoverTotal,
'carryover_positive_total' => $carryoverPositiveTotal,
]);
}
/**