1.优化分红方式

This commit is contained in:
2026-05-29 12:01:00 +08:00
parent f286fcc56f
commit d8673fb2c5
13 changed files with 457 additions and 60 deletions

View File

@@ -21,7 +21,7 @@ class Admin extends Backend
/**
* 分红比例余量查询(表单提示用)
*/
protected array $noNeedPermission = ['commissionShareRemainder'];
protected array $noNeedPermission = ['commissionShareRemainder', 'groupMeta'];
protected ?object $model = null;
@@ -107,11 +107,38 @@ class Admin extends Backend
$parentAdminId = intval($request->get('parent_admin_id', 0));
$excludeId = intval($request->get('exclude_id', 0));
$isTopLevelGroup = ($request->get('is_top_level') ?? $request->post('is_top_level')) === '1'
|| ($request->get('is_top_level') ?? $request->post('is_top_level')) === 1
|| ($request->get('is_top_level') ?? $request->post('is_top_level')) === true;
$channelId = intval($request->get('channel_id', 0));
if ($isTopLevelGroup) {
if ($channelId <= 0) {
return $this->success('', [
'used_rate' => '0.00',
'remaining_rate' => '100.00',
'parent_has_no_share' => false,
'is_top_level' => true,
]);
}
$stats = AdminCommissionDistributionService::getChannelRootShareRemainder(
$channelId,
$excludeId > 0 ? $excludeId : null
);
return $this->success('', [
'used_rate' => $stats['used_rate'],
'remaining_rate' => $stats['remaining_rate'],
'parent_has_no_share' => bccomp($stats['remaining_rate'], '0', 2) <= 0,
'is_top_level' => true,
]);
}
if ($parentAdminId <= 0) {
return $this->success('', [
'used_rate' => '0.00',
'remaining_rate' => '100.00',
'parent_has_no_share' => false,
'is_top_level' => false,
]);
}
@@ -128,6 +155,43 @@ class Admin extends Backend
'used_rate' => $stats['used_rate'],
'remaining_rate' => $stats['remaining_rate'],
'parent_has_no_share' => bccomp($stats['remaining_rate'], '0', 2) <= 0,
'is_top_level' => false,
]);
}
/**
* 查询角色组是否为顶级pid=0供管理员表单联动
*/
public function groupMeta(Request $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
$groupId = intval($request->get('group_id', 0));
if ($groupId <= 0) {
return $this->error(__('Invalid parameters'));
}
$group = Db::name('admin_group')->where('id', $groupId)->field(['id', 'pid', 'channel_id'])->find();
if (!is_array($group)) {
return $this->error(__('Record not found'));
}
if (!$this->auth->isSuperAdmin()) {
$authGroups = $this->getManageableGroupIds();
if (!in_array($groupId, $authGroups, true)) {
return $this->error(__('You have no permission'));
}
}
$pid = intval($group['pid'] ?? 0);
return $this->success('', [
'is_top_level' => $pid === 0,
'pid' => $pid,
'channel_id' => $group['channel_id'] ?? null,
]);
}
@@ -272,7 +336,7 @@ class Admin extends Backend
}
}
$parentErr = $this->normalizeParentAndShareFields($data, null);
$parentErr = $this->normalizeParentAndShareFields($data, null, $data['group_arr'] ?? []);
if ($parentErr !== null) {
return $this->error($parentErr);
}
@@ -429,7 +493,7 @@ class Admin extends Backend
if (!$this->auth->isSuperAdmin()) {
unset($data['parent_admin_id']);
}
$parentErr = $this->normalizeParentAndShareFields($data, intval($id));
$parentErr = $this->normalizeParentAndShareFields($data, intval($id), $editGroupArr ?? []);
if ($parentErr !== null) {
return $this->error($parentErr);
}
@@ -463,8 +527,18 @@ class Admin extends Backend
$rowData = $row->toArray();
$enriched = $this->enrichAdminRows([$rowData]);
$rowData = $enriched[0] ?? $rowData;
$groupArr = is_array($rowData['group_arr'] ?? null) ? $rowData['group_arr'] : [];
$rowData['primary_group_is_top_level'] = $this->isPrimaryGroupTopLevel($groupArr);
$parentId = intval($rowData['parent_admin_id'] ?? 0);
if ($parentId > 0) {
if ($rowData['primary_group_is_top_level']) {
$channelId = intval($rowData['channel_id'] ?? 0);
if ($channelId > 0) {
$rowData['root_share_remainder'] = AdminCommissionDistributionService::getChannelRootShareRemainder(
$channelId,
intval($id)
);
}
} elseif ($parentId > 0) {
$remainder = AdminCommissionDistributionService::getShareRemainder($parentId, intval($id));
$rowData['share_remainder'] = $remainder;
}
@@ -594,10 +668,51 @@ class Admin extends Backend
}
/**
* @param array<string, mixed> $data
* @param array<int|string> $groupIds
*/
private function normalizeParentAndShareFields(array &$data, ?int $editAdminId): ?string
private function isPrimaryGroupTopLevel(array $groupIds): bool
{
if ($groupIds === []) {
return false;
}
$gid = intval($groupIds[0]);
if ($gid <= 0) {
return false;
}
$pid = Db::name('admin_group')->where('id', $gid)->value('pid');
return intval($pid ?? 0) === 0;
}
/**
* @param array<string, mixed> $data
* @param array<int|string> $groupIds
*/
private function normalizeParentAndShareFields(array &$data, ?int $editAdminId, array $groupIds = []): ?string
{
if ($this->isPrimaryGroupTopLevel($groupIds)) {
$data['parent_admin_id'] = null;
$channelId = $data['channel_id'] ?? null;
if ($channelId === null || $channelId === '') {
$channelId = $this->resolveChannelIdFromPrimaryGroup($groupIds);
if ($channelId !== null && $channelId !== '') {
$data['channel_id'] = $channelId;
}
}
$channelIdInt = intval($channelId ?? 0);
$shareErr = AdminCommissionDistributionService::validateChannelRootCommissionShareRate(
$channelIdInt,
$data['commission_share_rate'] ?? null,
$editAdminId
);
if ($shareErr !== null) {
return $shareErr;
}
$data['commission_share_rate'] = bcadd(strval($data['commission_share_rate'] ?? '0'), '0', 2);
return null;
}
$parentId = isset($data['parent_admin_id']) && $data['parent_admin_id'] !== '' && $data['parent_admin_id'] !== null
? intval($data['parent_admin_id'])
: 0;
@@ -608,6 +723,7 @@ class Admin extends Backend
if ($parentId <= 0) {
$data['parent_admin_id'] = null;
$data['commission_share_rate'] = null;
return null;
}