1.将部门修改为渠道,并且所有dice_表关联渠道表
2.将所有配置表,记录表设置关联渠道 3.优化后台页面设置
This commit is contained in:
20
server/app/dice/basic/DiceBaseLogic.php
Normal file
20
server/app/dice/basic/DiceBaseLogic.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\dice\basic;
|
||||
|
||||
use plugin\saiadmin\basic\think\BaseLogic;
|
||||
|
||||
/**
|
||||
* 大富翁逻辑层基类:删除均为硬删除
|
||||
*/
|
||||
class DiceBaseLogic extends BaseLogic
|
||||
{
|
||||
/**
|
||||
* @param mixed $ids
|
||||
*/
|
||||
public function destroy($ids): bool
|
||||
{
|
||||
return $this->model->destroy($ids, true);
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ use app\dice\model\play_record\DicePlayRecord;
|
||||
use app\dice\model\reward\DiceRewardConfig;
|
||||
use plugin\saiadmin\basic\BaseController;
|
||||
use plugin\saiadmin\service\Permission;
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
use support\think\Db;
|
||||
|
||||
@@ -22,7 +23,7 @@ class DiceDashboardController extends BaseController
|
||||
* 工作台卡片统计:玩家注册、充值、提现、游玩次数(含较上周对比)
|
||||
*/
|
||||
#[Permission('工作台数据统计', 'core:console:list')]
|
||||
public function statistics(): Response
|
||||
public function statistics(Request $request): Response
|
||||
{
|
||||
$thisWeekStart = date('Y-m-d 00:00:00', strtotime('monday this week'));
|
||||
$thisWeekEnd = date('Y-m-d 23:59:59', strtotime('sunday this week'));
|
||||
@@ -30,11 +31,12 @@ class DiceDashboardController extends BaseController
|
||||
$lastWeekEnd = date('Y-m-d 23:59:59', strtotime('sunday last week'));
|
||||
|
||||
$adminInfo = $this->adminInfo ?? null;
|
||||
$filterDeptId = $request->input('dept_id');
|
||||
|
||||
$playerQueryThis = DicePlayer::whereBetween('create_time', [$thisWeekStart, $thisWeekEnd]);
|
||||
$playerQueryLast = DicePlayer::whereBetween('create_time', [$lastWeekStart, $lastWeekEnd]);
|
||||
AdminScopeHelper::applyAdminScope($playerQueryThis, $adminInfo);
|
||||
AdminScopeHelper::applyAdminScope($playerQueryLast, $adminInfo);
|
||||
AdminScopeHelper::applyAdminScope($playerQueryThis, $adminInfo, $filterDeptId);
|
||||
AdminScopeHelper::applyAdminScope($playerQueryLast, $adminInfo, $filterDeptId);
|
||||
$playerThis = $playerQueryThis->count();
|
||||
$playerLast = $playerQueryLast->count();
|
||||
|
||||
@@ -44,8 +46,8 @@ class DiceDashboardController extends BaseController
|
||||
$chargeQueryLast = DicePlayerWalletRecord::where('type', 0)
|
||||
->where('coin', '>', 0)
|
||||
->whereBetween('create_time', [$lastWeekStart, $lastWeekEnd]);
|
||||
AdminScopeHelper::applyAdminScope($chargeQueryThis, $adminInfo);
|
||||
AdminScopeHelper::applyAdminScope($chargeQueryLast, $adminInfo);
|
||||
AdminScopeHelper::applyAdminScope($chargeQueryThis, $adminInfo, $filterDeptId);
|
||||
AdminScopeHelper::applyAdminScope($chargeQueryLast, $adminInfo, $filterDeptId);
|
||||
$chargeThis = $chargeQueryThis->sum('coin');
|
||||
$chargeLast = $chargeQueryLast->sum('coin');
|
||||
|
||||
@@ -53,15 +55,15 @@ class DiceDashboardController extends BaseController
|
||||
->whereBetween('create_time', [$thisWeekStart, $thisWeekEnd]);
|
||||
$withdrawQueryLast = DicePlayerWalletRecord::where('type', 1)
|
||||
->whereBetween('create_time', [$lastWeekStart, $lastWeekEnd]);
|
||||
AdminScopeHelper::applyAdminScope($withdrawQueryThis, $adminInfo);
|
||||
AdminScopeHelper::applyAdminScope($withdrawQueryLast, $adminInfo);
|
||||
AdminScopeHelper::applyAdminScope($withdrawQueryThis, $adminInfo, $filterDeptId);
|
||||
AdminScopeHelper::applyAdminScope($withdrawQueryLast, $adminInfo, $filterDeptId);
|
||||
$withdrawThis = $withdrawQueryThis->sum(Db::raw('ABS(coin)'));
|
||||
$withdrawLast = $withdrawQueryLast->sum(Db::raw('ABS(coin)'));
|
||||
|
||||
$playQueryThis = DicePlayRecord::whereBetween('create_time', [$thisWeekStart, $thisWeekEnd]);
|
||||
$playQueryLast = DicePlayRecord::whereBetween('create_time', [$lastWeekStart, $lastWeekEnd]);
|
||||
AdminScopeHelper::applyAdminScope($playQueryThis, $adminInfo);
|
||||
AdminScopeHelper::applyAdminScope($playQueryLast, $adminInfo);
|
||||
AdminScopeHelper::applyAdminScope($playQueryThis, $adminInfo, $filterDeptId);
|
||||
AdminScopeHelper::applyAdminScope($playQueryLast, $adminInfo, $filterDeptId);
|
||||
$playThis = $playQueryThis->count();
|
||||
$playLast = $playQueryLast->count();
|
||||
|
||||
@@ -86,13 +88,11 @@ class DiceDashboardController extends BaseController
|
||||
* 近期玩家充值统计(近10天每日充值金额)
|
||||
*/
|
||||
#[Permission('工作台数据统计', 'core:console:list')]
|
||||
public function rechargeChart(): Response
|
||||
public function rechargeChart(Request $request): Response
|
||||
{
|
||||
$adminInfo = $this->adminInfo ?? null;
|
||||
$allowedIds = AdminScopeHelper::getAllowedAdminIds($adminInfo);
|
||||
$adminCondition = '';
|
||||
if ($allowedIds !== null) {
|
||||
if (empty($allowedIds)) {
|
||||
$deptCondition = $this->buildWalletSqlDeptCondition($adminInfo, $request->input('dept_id'));
|
||||
if ($deptCondition === '__empty__') {
|
||||
$data = [];
|
||||
foreach (range(0, 9) as $n) {
|
||||
$data[] = ['recharge_date' => date('Y-m-d', strtotime("-{$n} days")), 'recharge_amount' => 0];
|
||||
@@ -102,9 +102,6 @@ class DiceDashboardController extends BaseController
|
||||
'recharge_amount' => array_map('floatval', array_column($data, 'recharge_amount')),
|
||||
'recharge_date' => array_column($data, 'recharge_date'),
|
||||
]);
|
||||
}
|
||||
$idsStr = implode(',', array_map('intval', $allowedIds));
|
||||
$adminCondition = " AND w.admin_id IN ({$idsStr})";
|
||||
}
|
||||
$sql = "
|
||||
SELECT
|
||||
@@ -117,7 +114,7 @@ class DiceDashboardController extends BaseController
|
||||
UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) a
|
||||
) d
|
||||
LEFT JOIN dice_player_wallet_record w
|
||||
ON DATE(w.create_time) = d.date AND w.type = 0 AND w.coin > 0 {$adminCondition}
|
||||
ON DATE(w.create_time) = d.date AND w.type = 0 AND w.coin > 0 {$deptCondition}
|
||||
GROUP BY d.date
|
||||
ORDER BY d.date ASC
|
||||
";
|
||||
@@ -132,13 +129,11 @@ class DiceDashboardController extends BaseController
|
||||
* 月度玩家充值汇总(当年1-12月每月充值金额)
|
||||
*/
|
||||
#[Permission('工作台数据统计', 'core:console:list')]
|
||||
public function rechargeBarChart(): Response
|
||||
public function rechargeBarChart(Request $request): Response
|
||||
{
|
||||
$adminInfo = $this->adminInfo ?? null;
|
||||
$allowedIds = AdminScopeHelper::getAllowedAdminIds($adminInfo);
|
||||
$adminCondition = '';
|
||||
if ($allowedIds !== null) {
|
||||
if (empty($allowedIds)) {
|
||||
$deptCondition = $this->buildWalletSqlDeptCondition($adminInfo, $request->input('dept_id'));
|
||||
if ($deptCondition === '__empty__') {
|
||||
$data = [];
|
||||
for ($m = 1; $m <= 12; $m++) {
|
||||
$data[] = ['recharge_month' => sprintf('%02d月', $m), 'recharge_amount' => 0];
|
||||
@@ -147,9 +142,6 @@ class DiceDashboardController extends BaseController
|
||||
'recharge_amount' => array_map('floatval', array_column($data, 'recharge_amount')),
|
||||
'recharge_month' => array_column($data, 'recharge_month'),
|
||||
]);
|
||||
}
|
||||
$idsStr = implode(',', array_map('intval', $allowedIds));
|
||||
$adminCondition = " AND w.admin_id IN ({$idsStr})";
|
||||
}
|
||||
$sql = "
|
||||
SELECT
|
||||
@@ -163,7 +155,7 @@ class DiceDashboardController extends BaseController
|
||||
LEFT JOIN dice_player_wallet_record w
|
||||
ON YEAR(w.create_time) = YEAR(CURDATE())
|
||||
AND MONTH(w.create_time) = m.month_num
|
||||
AND w.type = 0 AND w.coin > 0 {$adminCondition}
|
||||
AND w.type = 0 AND w.coin > 0 {$deptCondition}
|
||||
GROUP BY m.month_num
|
||||
ORDER BY m.month_num ASC
|
||||
";
|
||||
@@ -179,7 +171,7 @@ class DiceDashboardController extends BaseController
|
||||
* 返回:玩家账号(DicePlayer.username)、充值金额(coin)、充值时间(create_time)
|
||||
*/
|
||||
#[Permission('工作台数据统计', 'core:console:list')]
|
||||
public function walletRecordList(): Response
|
||||
public function walletRecordList(Request $request): Response
|
||||
{
|
||||
$adminInfo = $this->adminInfo ?? null;
|
||||
$query = DicePlayerWalletRecord::with([
|
||||
@@ -190,7 +182,7 @@ class DiceDashboardController extends BaseController
|
||||
->where('type', 0)
|
||||
->order('create_time', 'desc')
|
||||
->limit(50);
|
||||
AdminScopeHelper::applyAdminScope($query, $adminInfo);
|
||||
AdminScopeHelper::applyAdminScope($query, $adminInfo, $request->input('dept_id'));
|
||||
$list = $query->select();
|
||||
$rows = [];
|
||||
foreach ($list as $row) {
|
||||
@@ -209,13 +201,13 @@ class DiceDashboardController extends BaseController
|
||||
* 返回:玩家账号(username)、余额(coin)、抽奖券(total_ticket_count)
|
||||
*/
|
||||
#[Permission('工作台数据统计', 'core:console:list')]
|
||||
public function newPlayerList(): Response
|
||||
public function newPlayerList(Request $request): Response
|
||||
{
|
||||
$adminInfo = $this->adminInfo ?? null;
|
||||
$query = DicePlayer::field('username,coin,total_ticket_count,create_time')
|
||||
->order('create_time', 'desc')
|
||||
->limit(50);
|
||||
AdminScopeHelper::applyAdminScope($query, $adminInfo);
|
||||
AdminScopeHelper::applyAdminScope($query, $adminInfo, $request->input('dept_id'));
|
||||
$list = $query->select();
|
||||
$rows = [];
|
||||
foreach ($list as $row) {
|
||||
@@ -234,7 +226,7 @@ class DiceDashboardController extends BaseController
|
||||
* 返回:玩家账号、中奖档位、赢取平台币、游玩时间
|
||||
*/
|
||||
#[Permission('工作台数据统计', 'core:console:list')]
|
||||
public function playRecordList(): Response
|
||||
public function playRecordList(Request $request): Response
|
||||
{
|
||||
$adminInfo = $this->adminInfo ?? null;
|
||||
$query = DicePlayRecord::with([
|
||||
@@ -246,7 +238,7 @@ class DiceDashboardController extends BaseController
|
||||
->field('id,player_id,reward_tier,win_coin,create_time')
|
||||
->order('create_time', 'desc')
|
||||
->limit(50);
|
||||
AdminScopeHelper::applyAdminScope($query, $adminInfo);
|
||||
AdminScopeHelper::applyAdminScope($query, $adminInfo, $request->input('dept_id'));
|
||||
$list = $query->select();
|
||||
$tierLabels = $this->buildRewardTierLabels();
|
||||
$rows = [];
|
||||
@@ -290,4 +282,23 @@ class DiceDashboardController extends BaseController
|
||||
}
|
||||
return round((($current - $last) / $last) * 100, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 钱包流水 SQL 渠道条件;非超管无渠道时返回 __empty__
|
||||
*/
|
||||
private function buildWalletSqlDeptCondition(?array $adminInfo, $requestDeptId): string
|
||||
{
|
||||
if (AdminScopeHelper::getDeptId($adminInfo) !== null) {
|
||||
$deptId = AdminScopeHelper::getDeptId($adminInfo);
|
||||
if ($deptId <= 0) {
|
||||
return '__empty__';
|
||||
}
|
||||
return ' AND w.dept_id = ' . $deptId;
|
||||
}
|
||||
$target = AdminScopeHelper::resolveBusinessDeptId($adminInfo, $requestDeptId);
|
||||
if ($target !== null && $target > 0) {
|
||||
return ' AND w.dept_id = ' . $target;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,9 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\controller\ante_config;
|
||||
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use app\dice\logic\ante_config\DiceAnteConfigLogic;
|
||||
use app\dice\model\ante_config\DiceAnteConfig;
|
||||
use app\dice\validate\ante_config\DiceAnteConfigValidate;
|
||||
use plugin\saiadmin\basic\BaseController;
|
||||
use plugin\saiadmin\service\Permission;
|
||||
@@ -34,10 +36,32 @@ class DiceAnteConfigController extends BaseController
|
||||
['is_default', ''],
|
||||
]);
|
||||
$query = $this->logic->search($where);
|
||||
AdminScopeHelper::applyConfigScope($query, $this->adminInfo ?? null, $request->input('dept_id'));
|
||||
$data = $this->logic->getList($query);
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 底注下拉选项(按渠道),供一键测试权重等使用
|
||||
*/
|
||||
#[Permission('底注配置列表', 'dice:ante_config:index:index')]
|
||||
public function getOptions(Request $request): Response
|
||||
{
|
||||
$query = DiceAnteConfig::field('id,name,title,mult,is_default')->order('mult', 'asc')->order('id', 'asc');
|
||||
AdminScopeHelper::applyConfigScope($query, $this->adminInfo ?? null, $request->input('dept_id'));
|
||||
$list = $query->select();
|
||||
$data = $list->map(static function ($item) {
|
||||
return [
|
||||
'id' => (int) $item['id'],
|
||||
'name' => (string) ($item['name'] ?? ''),
|
||||
'title' => (string) ($item['title'] ?? ''),
|
||||
'mult' => (int) ($item['mult'] ?? 0),
|
||||
'is_default' => (int) ($item['is_default'] ?? 0),
|
||||
];
|
||||
})->toArray();
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
#[Permission('底注配置读取', 'dice:ante_config:index:read')]
|
||||
public function read(Request $request): Response
|
||||
{
|
||||
@@ -52,6 +76,7 @@ class DiceAnteConfigController extends BaseController
|
||||
{
|
||||
$data = $request->post();
|
||||
$this->validate('save', $data);
|
||||
AdminScopeHelper::prepareConfigSaveData($data, $this->adminInfo ?? null, $request->input('dept_id'), $data);
|
||||
$result = $this->logic->add($data);
|
||||
return $result ? $this->success('add success') : $this->fail('add failed');
|
||||
}
|
||||
@@ -60,8 +85,19 @@ class DiceAnteConfigController extends BaseController
|
||||
public function update(Request $request): Response
|
||||
{
|
||||
$data = $request->post();
|
||||
if ($data === [] || $data === null) {
|
||||
$data = $request->all();
|
||||
}
|
||||
$this->validate('update', $data);
|
||||
$result = $this->logic->edit($data['id'], $data);
|
||||
$requestDeptId = AdminScopeHelper::pickRequestDeptId($request->input('dept_id'), is_array($data) ? $data : []);
|
||||
$model = $this->logic->read($data['id'] ?? 0);
|
||||
if ($model) {
|
||||
$recordDeptId = is_array($model) ? ($model['dept_id'] ?? null) : ($model->dept_id ?? null);
|
||||
if (! AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $recordDeptId, $requestDeptId)) {
|
||||
return $this->fail('no permission to update this record');
|
||||
}
|
||||
}
|
||||
$result = $this->logic->edit($data['id'], $data, $this->adminInfo ?? null, $requestDeptId);
|
||||
return $result ? $this->success('update success') : $this->fail('update failed');
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\controller\config;
|
||||
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use plugin\saiadmin\basic\BaseController;
|
||||
use app\dice\logic\config\DiceConfigLogic;
|
||||
use app\dice\validate\config\DiceConfigValidate;
|
||||
@@ -42,6 +43,7 @@ class DiceConfigController extends BaseController
|
||||
['title', ''],
|
||||
]);
|
||||
$query = $this->logic->search($where);
|
||||
AdminScopeHelper::applyConfigScope($query, $this->adminInfo ?? null, $request->input('dept_id'));
|
||||
$data = $this->logic->getList($query);
|
||||
return $this->success($data);
|
||||
}
|
||||
@@ -74,6 +76,7 @@ class DiceConfigController extends BaseController
|
||||
{
|
||||
$data = $request->post();
|
||||
$this->validate('save', $data);
|
||||
AdminScopeHelper::prepareConfigSaveData($data, $this->adminInfo ?? null, $request->input('dept_id'), $data);
|
||||
$result = $this->logic->add($data);
|
||||
if ($result) {
|
||||
return $this->success('add success');
|
||||
@@ -91,8 +94,20 @@ class DiceConfigController extends BaseController
|
||||
public function update(Request $request): Response
|
||||
{
|
||||
$data = $request->post();
|
||||
if ($data === [] || $data === null) {
|
||||
$data = $request->all();
|
||||
}
|
||||
$this->validate('update', $data);
|
||||
$result = $this->logic->edit($data['id'], $data);
|
||||
$requestDeptId = AdminScopeHelper::pickRequestDeptId($request->input('dept_id'), is_array($data) ? $data : []);
|
||||
$deptId = AdminScopeHelper::resolveConfigDeptId($this->adminInfo ?? null, $requestDeptId);
|
||||
$model = $this->logic->read($data['id'] ?? 0);
|
||||
if ($model) {
|
||||
$recordDeptId = is_array($model) ? ($model['dept_id'] ?? null) : ($model->dept_id ?? null);
|
||||
if (! AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $recordDeptId, $requestDeptId)) {
|
||||
return $this->fail('no permission to update this record');
|
||||
}
|
||||
}
|
||||
$result = $this->logic->edit($data['id'], $data, $this->adminInfo ?? null, $requestDeptId);
|
||||
if ($result) {
|
||||
return $this->success('update success');
|
||||
} else {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\controller\game;
|
||||
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use app\dice\logic\game\DiceGameLogic;
|
||||
use app\dice\validate\game\DiceGameValidate;
|
||||
use plugin\saiadmin\basic\BaseController;
|
||||
@@ -33,6 +34,7 @@ class DiceGameController extends BaseController
|
||||
['status', ''],
|
||||
]);
|
||||
$query = $this->logic->search($where);
|
||||
AdminScopeHelper::applyConfigScope($query, $this->adminInfo ?? null, $request->input('dept_id'));
|
||||
$data = $this->logic->getList($query);
|
||||
return $this->success($data);
|
||||
}
|
||||
@@ -54,6 +56,7 @@ class DiceGameController extends BaseController
|
||||
{
|
||||
$data = $request->post();
|
||||
$this->validate('save', $data);
|
||||
AdminScopeHelper::prepareConfigSaveData($data, $this->adminInfo ?? null, $request->input('dept_id'), $data);
|
||||
$result = $this->logic->add($data);
|
||||
if (!$result) {
|
||||
return $this->fail('add failed');
|
||||
@@ -65,8 +68,19 @@ class DiceGameController extends BaseController
|
||||
public function update(Request $request): Response
|
||||
{
|
||||
$data = $request->post();
|
||||
if ($data === [] || $data === null) {
|
||||
$data = $request->all();
|
||||
}
|
||||
$this->validate('update', $data);
|
||||
$result = $this->logic->edit($data['id'], $data);
|
||||
$requestDeptId = AdminScopeHelper::pickRequestDeptId($request->input('dept_id'), is_array($data) ? $data : []);
|
||||
$model = $this->logic->read($data['id'] ?? 0);
|
||||
if ($model) {
|
||||
$recordDeptId = is_array($model) ? ($model['dept_id'] ?? null) : ($model->dept_id ?? null);
|
||||
if (! AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $recordDeptId, $requestDeptId)) {
|
||||
return $this->fail('no permission to update this record');
|
||||
}
|
||||
}
|
||||
$result = $this->logic->edit($data['id'], $data, $this->adminInfo ?? null, $requestDeptId);
|
||||
if (!$result) {
|
||||
return $this->fail('update failed');
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
namespace app\dice\controller\lottery_pool_config;
|
||||
|
||||
use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig;
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use plugin\saiadmin\basic\BaseController;
|
||||
use app\dice\logic\lottery_pool_config\DiceLotteryPoolConfigLogic;
|
||||
use app\dice\validate\lottery_pool_config\DiceLotteryPoolConfigValidate;
|
||||
@@ -37,9 +38,10 @@ class DiceLotteryPoolConfigController extends BaseController
|
||||
#[Permission('色子奖池配置列表', 'dice:lottery_pool_config:index:index')]
|
||||
public function getOptions(Request $request): Response
|
||||
{
|
||||
$list = DiceLotteryPoolConfig::field('id,name,t1_weight,t2_weight,t3_weight,t4_weight,t5_weight')
|
||||
->order('id', 'asc')
|
||||
->select();
|
||||
$query = DiceLotteryPoolConfig::field('id,name,t1_weight,t2_weight,t3_weight,t4_weight,t5_weight')
|
||||
->order('id', 'asc');
|
||||
AdminScopeHelper::applyConfigScope($query, $this->adminInfo ?? null, $request->input('dept_id'));
|
||||
$list = $query->select();
|
||||
$data = $list->map(function ($item) {
|
||||
return [
|
||||
'id' => (int) $item['id'],
|
||||
@@ -67,6 +69,7 @@ class DiceLotteryPoolConfigController extends BaseController
|
||||
['type', ''],
|
||||
]);
|
||||
$query = $this->logic->search($where);
|
||||
AdminScopeHelper::applyConfigScope($query, $this->adminInfo ?? null, $request->input('dept_id'));
|
||||
$data = $this->logic->getList($query);
|
||||
return $this->success($data);
|
||||
}
|
||||
@@ -99,6 +102,7 @@ class DiceLotteryPoolConfigController extends BaseController
|
||||
{
|
||||
$data = $request->post();
|
||||
$this->validate('save', $data);
|
||||
AdminScopeHelper::prepareConfigSaveData($data, $this->adminInfo ?? null, $request->input('dept_id'), $data);
|
||||
$result = $this->logic->add($data);
|
||||
if ($result) {
|
||||
return $this->success('add success');
|
||||
@@ -116,8 +120,19 @@ class DiceLotteryPoolConfigController extends BaseController
|
||||
public function update(Request $request): Response
|
||||
{
|
||||
$data = $request->post();
|
||||
if ($data === [] || $data === null) {
|
||||
$data = $request->all();
|
||||
}
|
||||
$this->validate('update', $data);
|
||||
$result = $this->logic->edit($data['id'], $data);
|
||||
$requestDeptId = AdminScopeHelper::pickRequestDeptId($request->input('dept_id'), is_array($data) ? $data : []);
|
||||
$model = $this->logic->read($data['id'] ?? 0);
|
||||
if ($model) {
|
||||
$recordDeptId = is_array($model) ? ($model['dept_id'] ?? null) : ($model->dept_id ?? null);
|
||||
if (! AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $recordDeptId, $requestDeptId)) {
|
||||
return $this->fail('no permission to update this record');
|
||||
}
|
||||
}
|
||||
$result = $this->logic->edit($data['id'], $data, $this->adminInfo ?? null, $requestDeptId);
|
||||
if ($result) {
|
||||
return $this->success('update success');
|
||||
} else {
|
||||
@@ -152,7 +167,8 @@ class DiceLotteryPoolConfigController extends BaseController
|
||||
#[Permission('色子奖池配置列表', 'dice:lottery_pool_config:index:getCurrentPool')]
|
||||
public function getCurrentPool(Request $request): Response
|
||||
{
|
||||
$data = $this->logic->getCurrentPool();
|
||||
$deptId = AdminScopeHelper::resolveConfigDeptId($this->adminInfo ?? null, $request->input('dept_id'));
|
||||
$data = $this->logic->getCurrentPool($deptId);
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
@@ -163,7 +179,12 @@ class DiceLotteryPoolConfigController extends BaseController
|
||||
public function updateCurrentPool(Request $request): Response
|
||||
{
|
||||
$data = $request->post();
|
||||
$this->logic->updateCurrentPool($data);
|
||||
if ($data === [] || $data === null) {
|
||||
$data = $request->all();
|
||||
}
|
||||
$requestDeptId = AdminScopeHelper::pickRequestDeptId($request->input('dept_id'), is_array($data) ? $data : []);
|
||||
$deptId = AdminScopeHelper::resolveConfigDeptId($this->adminInfo ?? null, $requestDeptId);
|
||||
$this->logic->updateCurrentPool($data, $deptId);
|
||||
return $this->success('save success');
|
||||
}
|
||||
|
||||
@@ -173,7 +194,8 @@ class DiceLotteryPoolConfigController extends BaseController
|
||||
#[Permission('色子奖池配置修改', 'dice:lottery_pool_config:index:resetProfitAmount')]
|
||||
public function resetProfitAmount(Request $request): Response
|
||||
{
|
||||
$this->logic->resetProfitAmount();
|
||||
$deptId = AdminScopeHelper::resolveConfigDeptId($this->adminInfo ?? null, $request->input('dept_id'));
|
||||
$this->logic->resetProfitAmount($deptId);
|
||||
return $this->success('reset success');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ class DicePlayRecordController extends BaseController
|
||||
['direction', ''],
|
||||
]);
|
||||
$query = $this->logic->search($where);
|
||||
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null);
|
||||
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null, $request->input('dept_id'));
|
||||
$query->with([
|
||||
'dicePlayer',
|
||||
'diceLotteryPoolConfig',
|
||||
@@ -78,7 +78,7 @@ class DicePlayRecordController extends BaseController
|
||||
public function getPlayerOptions(Request $request): Response
|
||||
{
|
||||
$query = DicePlayer::field('id,username');
|
||||
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null);
|
||||
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null, $request->input('dept_id'));
|
||||
$list = $query->select();
|
||||
$data = $list->map(function ($item) {
|
||||
return ['id' => $item['id'], 'username' => $item['username'] ?? ''];
|
||||
@@ -92,9 +92,15 @@ class DicePlayRecordController extends BaseController
|
||||
#[Permission('玩家抽奖记录列表', 'dice:play_record:index:index')]
|
||||
public function getLotteryConfigOptions(Request $request): Response
|
||||
{
|
||||
$list = DiceLotteryPoolConfig::field('id,name')->select();
|
||||
$query = DiceLotteryPoolConfig::field('id,name')->order('id', 'asc');
|
||||
$requestDeptId = AdminScopeHelper::pickRequestDeptId(
|
||||
$request->input('dept_id'),
|
||||
$request->all()
|
||||
);
|
||||
AdminScopeHelper::applyConfigScope($query, $this->adminInfo ?? null, $requestDeptId);
|
||||
$list = $query->select();
|
||||
$data = $list->map(function ($item) {
|
||||
return ['id' => $item['id'], 'name' => $item['name'] ?? ''];
|
||||
return ['id' => (int) $item['id'], 'name' => (string) ($item['name'] ?? '')];
|
||||
})->toArray();
|
||||
return $this->success($data);
|
||||
}
|
||||
@@ -112,8 +118,7 @@ class DicePlayRecordController extends BaseController
|
||||
if (!$model) {
|
||||
return $this->fail('not found');
|
||||
}
|
||||
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
|
||||
if ($allowedIds !== null && !in_array((int) ($model->admin_id ?? 0), $allowedIds, true)) {
|
||||
if (!AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $model->dept_id ?? null)) {
|
||||
return $this->fail('no permission to view this record');
|
||||
}
|
||||
$data = is_array($model) ? $model : $model->toArray();
|
||||
@@ -130,6 +135,7 @@ class DicePlayRecordController extends BaseController
|
||||
{
|
||||
$data = $request->post();
|
||||
$this->validate('save', $data);
|
||||
AdminScopeHelper::prepareBusinessSaveData($data, $this->adminInfo ?? null, $request->input('dept_id'), $data);
|
||||
$result = $this->logic->add($data);
|
||||
if ($result) {
|
||||
return $this->success('add success');
|
||||
@@ -148,6 +154,13 @@ class DicePlayRecordController extends BaseController
|
||||
{
|
||||
$data = $request->post();
|
||||
$this->validate('update', $data);
|
||||
$model = $this->logic->read($data['id'] ?? 0);
|
||||
if ($model) {
|
||||
$recordDeptId = is_array($model) ? ($model['dept_id'] ?? null) : ($model->dept_id ?? null);
|
||||
if (! AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $recordDeptId, $request->input('dept_id'))) {
|
||||
return $this->fail('no permission to update this record');
|
||||
}
|
||||
}
|
||||
$result = $this->logic->edit($data['id'], $data);
|
||||
if ($result) {
|
||||
return $this->success('update success');
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\controller\play_record_test;
|
||||
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use plugin\saiadmin\basic\BaseController;
|
||||
use app\dice\logic\play_record_test\DicePlayRecordTestLogic;
|
||||
use app\dice\validate\play_record_test\DicePlayRecordTestValidate;
|
||||
@@ -50,6 +51,7 @@ class DicePlayRecordTestController extends BaseController
|
||||
['roll_number', ''],
|
||||
]);
|
||||
$query = $this->logic->search($where);
|
||||
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null, $request->input('dept_id'));
|
||||
$query->with(['diceLotteryPoolConfig']);
|
||||
|
||||
// 按当前筛选条件统计:平台总盈利 = 付费金额(paid_amount 求和) - 玩家总收益(win_coin 求和)
|
||||
@@ -92,6 +94,7 @@ class DicePlayRecordTestController extends BaseController
|
||||
{
|
||||
$data = $request->post();
|
||||
$this->validate('save', $data);
|
||||
AdminScopeHelper::prepareBusinessSaveData($data, $this->adminInfo ?? null, $request->input('dept_id'), $data);
|
||||
$result = $this->logic->add($data);
|
||||
if ($result) {
|
||||
return $this->success('add success');
|
||||
@@ -110,6 +113,13 @@ class DicePlayRecordTestController extends BaseController
|
||||
{
|
||||
$data = $request->post();
|
||||
$this->validate('update', $data);
|
||||
$model = $this->logic->read($data['id'] ?? 0);
|
||||
if ($model) {
|
||||
$recordDeptId = is_array($model) ? ($model['dept_id'] ?? null) : ($model->dept_id ?? null);
|
||||
if (! AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $recordDeptId, $request->input('dept_id'))) {
|
||||
return $this->fail('no permission to update this record');
|
||||
}
|
||||
}
|
||||
$result = $this->logic->edit($data['id'], $data);
|
||||
if ($result) {
|
||||
return $this->success('update success');
|
||||
|
||||
@@ -8,6 +8,7 @@ namespace app\dice\controller\player;
|
||||
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig;
|
||||
use plugin\saiadmin\app\model\system\SystemDept;
|
||||
use plugin\saiadmin\app\model\system\SystemUser;
|
||||
use plugin\saiadmin\basic\BaseController;
|
||||
use app\dice\logic\player\DicePlayerLogic;
|
||||
@@ -42,7 +43,13 @@ class DicePlayerController extends BaseController
|
||||
#[Permission('玩家列表', 'dice:player:index:index')]
|
||||
public function getLotteryConfigOptions(Request $request): Response
|
||||
{
|
||||
$list = DiceLotteryPoolConfig::field('id,name')->order('id', 'asc')->select();
|
||||
$query = DiceLotteryPoolConfig::field('id,name')->order('id', 'asc');
|
||||
$requestDeptId = AdminScopeHelper::pickRequestDeptId(
|
||||
$request->input('dept_id'),
|
||||
$request->all()
|
||||
);
|
||||
AdminScopeHelper::applyConfigScope($query, $this->adminInfo ?? null, $requestDeptId);
|
||||
$list = $query->select();
|
||||
$data = $list->map(function ($item) {
|
||||
return ['id' => (int) $item['id'], 'name' => (string) ($item['name'] ?? '')];
|
||||
})->toArray();
|
||||
@@ -57,12 +64,17 @@ class DicePlayerController extends BaseController
|
||||
#[Permission('玩家列表', 'dice:player:index:index')]
|
||||
public function getSystemUserOptions(Request $request): Response
|
||||
{
|
||||
$query = SystemUser::field('id,username,realname')->where('status', 1)->order('id', 'asc');
|
||||
if (isset($this->adminInfo['id']) && (int) $this->adminInfo['id'] > 1) {
|
||||
$deptList = $this->adminInfo['deptList'] ?? [];
|
||||
if (!empty($deptList)) {
|
||||
$query->auth($deptList);
|
||||
$query = SystemUser::field('id,username,realname,dept_id')->where('status', 1)->order('id', 'asc');
|
||||
$requestDeptId = AdminScopeHelper::pickRequestDeptId(
|
||||
$request->input('dept_id'),
|
||||
$request->all()
|
||||
);
|
||||
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
|
||||
if ($allowedIds !== null) {
|
||||
if ($allowedIds === []) {
|
||||
return $this->success([]);
|
||||
}
|
||||
$query->whereIn('id', $allowedIds);
|
||||
}
|
||||
$list = $query->select();
|
||||
$data = $list->map(function ($item) {
|
||||
@@ -71,12 +83,89 @@ class DicePlayerController extends BaseController
|
||||
'id' => (int) $item['id'],
|
||||
'username' => (string) ($item['username'] ?? ''),
|
||||
'realname' => (string) ($item['realname'] ?? ''),
|
||||
'dept_id' => isset($item['dept_id']) ? (int) $item['dept_id'] : null,
|
||||
'label' => $label ?: (string) $item['id'],
|
||||
];
|
||||
})->toArray();
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 超管:按渠道树状展示全部管理员;非超管:同 getSystemUserOptions 扁平列表
|
||||
*/
|
||||
#[Permission('玩家列表', 'dice:player:index:index')]
|
||||
public function getSystemUserTreeOptions(Request $request): Response
|
||||
{
|
||||
if (!AdminScopeHelper::isSuperAdmin($this->adminInfo ?? null)) {
|
||||
return $this->getSystemUserOptions($request);
|
||||
}
|
||||
|
||||
$users = SystemUser::field('id,username,realname,dept_id')
|
||||
->where('status', 1)
|
||||
->order('id', 'asc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
$depts = SystemDept::field('id,name')
|
||||
->where('status', 1)
|
||||
->order('id', 'asc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
$deptNameMap = [];
|
||||
foreach ($depts as $dept) {
|
||||
$deptNameMap[(int) $dept['id']] = (string) ($dept['name'] ?? $dept['id']);
|
||||
}
|
||||
|
||||
$grouped = [];
|
||||
$unassigned = [];
|
||||
foreach ($users as $user) {
|
||||
$item = [
|
||||
'id' => (int) $user['id'],
|
||||
'username' => (string) ($user['username'] ?? ''),
|
||||
'realname' => (string) ($user['realname'] ?? ''),
|
||||
'dept_id' => isset($user['dept_id']) ? (int) $user['dept_id'] : null,
|
||||
];
|
||||
$label = trim($item['realname']) ?: $item['username'];
|
||||
$item['label'] = $label ?: (string) $item['id'];
|
||||
$deptId = $item['dept_id'] ?? 0;
|
||||
if ($deptId > 0 && isset($deptNameMap[$deptId])) {
|
||||
if (!isset($grouped[$deptId])) {
|
||||
$grouped[$deptId] = [];
|
||||
}
|
||||
$grouped[$deptId][] = $item;
|
||||
} else {
|
||||
$unassigned[] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
$tree = [];
|
||||
foreach ($depts as $dept) {
|
||||
$deptId = (int) $dept['id'];
|
||||
$children = $grouped[$deptId] ?? [];
|
||||
if ($children === []) {
|
||||
continue;
|
||||
}
|
||||
$tree[] = [
|
||||
'id' => 'dept_' . $deptId,
|
||||
'label' => (string) ($dept['name'] ?? $deptId),
|
||||
'disabled' => true,
|
||||
'children' => $children,
|
||||
];
|
||||
}
|
||||
|
||||
if ($unassigned !== []) {
|
||||
$tree[] = [
|
||||
'id' => 'dept_unassigned',
|
||||
'label' => '__unassigned__',
|
||||
'disabled' => true,
|
||||
'children' => $unassigned,
|
||||
];
|
||||
}
|
||||
|
||||
return $this->success($tree);
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据列表
|
||||
* @param Request $request
|
||||
@@ -94,7 +183,7 @@ class DicePlayerController extends BaseController
|
||||
['lottery_config_id', ''],
|
||||
]);
|
||||
$query = $this->logic->search($where);
|
||||
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null);
|
||||
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null, $request->input('dept_id'));
|
||||
$query->with(['diceLotteryPoolConfig']);
|
||||
$data = $this->logic->getList($query);
|
||||
return $this->success($data);
|
||||
@@ -113,8 +202,7 @@ class DicePlayerController extends BaseController
|
||||
if (!$model) {
|
||||
return $this->fail('not found');
|
||||
}
|
||||
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
|
||||
if ($allowedIds !== null && !in_array((int) ($model->admin_id ?? 0), $allowedIds, true)) {
|
||||
if (!AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $model->dept_id ?? null)) {
|
||||
return $this->fail('no permission to view this record');
|
||||
}
|
||||
$data = is_array($model) ? $model : $model->toArray();
|
||||
@@ -130,20 +218,32 @@ class DicePlayerController extends BaseController
|
||||
public function save(Request $request): Response
|
||||
{
|
||||
$data = $request->post();
|
||||
$this->validate('save', $data);
|
||||
// 类型转化
|
||||
if (empty($data['admin_id']) && isset($this->adminInfo['id']) && (int) $this->adminInfo['id'] > 0) {
|
||||
$data['admin_id'] = (int) $this->adminInfo['id'];
|
||||
}
|
||||
$result = $this->logic->add($data);
|
||||
if ($result && isset($result['id'])) {
|
||||
// 出于安全:删除该玩家缓存,后续 API 按需重建
|
||||
UserCache::deleteUser($result['id']);
|
||||
$player = DicePlayer::find($result['id']);
|
||||
AdminScopeHelper::prepareBusinessSaveData(
|
||||
$data,
|
||||
$this->adminInfo ?? null,
|
||||
$request->input('dept_id'),
|
||||
$data
|
||||
);
|
||||
$this->validate('save', $data);
|
||||
try {
|
||||
$result = $this->logic->add($data);
|
||||
} catch (\Throwable $e) {
|
||||
if (self::isDeptUsernameDuplicateException($e)) {
|
||||
return $this->fail('PLAYER_USERNAME_DEPT_UNIQUE');
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
$playerId = is_array($result) ? ($result['id'] ?? null) : $result;
|
||||
if ($playerId) {
|
||||
UserCache::deleteUser($playerId);
|
||||
$player = DicePlayer::find($playerId);
|
||||
if ($player && $player->username !== '') {
|
||||
UserCache::deletePlayerByUsername($player->username);
|
||||
}
|
||||
return $this->success('add success');
|
||||
return $this->success('ADD_SUCCESS');
|
||||
}
|
||||
return $this->fail('add failed');
|
||||
}
|
||||
@@ -157,14 +257,16 @@ class DicePlayerController extends BaseController
|
||||
public function update(Request $request): Response
|
||||
{
|
||||
$data = $request->post();
|
||||
$this->validate('update', $data);
|
||||
$model = $this->logic->read($data['id'] ?? 0);
|
||||
if ($model) {
|
||||
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
|
||||
if ($allowedIds !== null && !in_array((int) ($model->admin_id ?? 0), $allowedIds, true)) {
|
||||
if (!AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $model->dept_id ?? null, $request->input('dept_id'))) {
|
||||
return $this->fail('no permission to update this record');
|
||||
}
|
||||
if (!isset($data['dept_id']) || $data['dept_id'] === '' || $data['dept_id'] === null) {
|
||||
$data['dept_id'] = $model->dept_id ?? null;
|
||||
}
|
||||
}
|
||||
$this->validate('update', $data);
|
||||
$result = $this->logic->edit($data['id'], $data);
|
||||
if ($result) {
|
||||
// 出于安全:删除该玩家缓存,后续 API 按需重建
|
||||
@@ -173,7 +275,7 @@ class DicePlayerController extends BaseController
|
||||
if ($player && $player->username !== '') {
|
||||
UserCache::deletePlayerByUsername($player->username);
|
||||
}
|
||||
return $this->success('update success');
|
||||
return $this->success('UPDATE_SUCCESS');
|
||||
}
|
||||
return $this->fail('update failed');
|
||||
}
|
||||
@@ -196,8 +298,7 @@ class DicePlayerController extends BaseController
|
||||
}
|
||||
$model = $this->logic->read($id);
|
||||
if ($model) {
|
||||
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
|
||||
if ($allowedIds !== null && !in_array((int) ($model->admin_id ?? 0), $allowedIds, true)) {
|
||||
if (!AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $model->dept_id ?? null, $request->input('dept_id'))) {
|
||||
return $this->fail('no permission to update this record');
|
||||
}
|
||||
}
|
||||
@@ -227,8 +328,7 @@ class DicePlayerController extends BaseController
|
||||
if (!$player) {
|
||||
return $this->fail('not found');
|
||||
}
|
||||
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
|
||||
if ($allowedIds !== null && !in_array((int) ($player->admin_id ?? 0), $allowedIds, true)) {
|
||||
if (!AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $player->dept_id ?? null)) {
|
||||
return $this->fail('no permission to view this record');
|
||||
}
|
||||
if ((int) ($player->status ?? 1) === 0) {
|
||||
@@ -281,13 +381,12 @@ class DicePlayerController extends BaseController
|
||||
return $this->fail('please select data to delete');
|
||||
}
|
||||
$ids = is_array($ids) ? $ids : explode(',', (string) $ids);
|
||||
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
|
||||
if ($allowedIds !== null) {
|
||||
$models = $this->logic->model->whereIn('id', $ids)->column('admin_id', 'id');
|
||||
$deptId = AdminScopeHelper::getDeptId($this->adminInfo ?? null);
|
||||
if ($deptId !== null) {
|
||||
$models = $this->logic->model->whereIn('id', $ids)->column('dept_id', 'id');
|
||||
$validIds = [];
|
||||
foreach ($ids as $id) {
|
||||
$adminId = (int) ($models[$id] ?? 0);
|
||||
if (in_array($adminId, $allowedIds, true)) {
|
||||
if (AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $models[$id] ?? null)) {
|
||||
$validIds[] = $id;
|
||||
}
|
||||
}
|
||||
@@ -311,4 +410,17 @@ class DicePlayerController extends BaseController
|
||||
return $this->fail('delete failed');
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否违反 dice_player (dept_id, username) 唯一索引
|
||||
*/
|
||||
private static function isDeptUsernameDuplicateException(\Throwable $e): bool
|
||||
{
|
||||
$message = $e->getMessage();
|
||||
if ($message === '') {
|
||||
return false;
|
||||
}
|
||||
return str_contains($message, 'uk_dice_player_dept_username')
|
||||
|| (str_contains($message, 'Duplicate entry') && str_contains($message, 'username'));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ class DicePlayerTicketRecordController extends BaseController
|
||||
['create_time_max', ''],
|
||||
]);
|
||||
$query = $this->logic->search($where);
|
||||
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null);
|
||||
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null, $request->input('dept_id'));
|
||||
$query->with([
|
||||
'dicePlayer',
|
||||
]);
|
||||
@@ -70,7 +70,7 @@ class DicePlayerTicketRecordController extends BaseController
|
||||
public function getPlayerOptions(Request $request): Response
|
||||
{
|
||||
$query = DicePlayer::field('id,username');
|
||||
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null);
|
||||
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null, $request->input('dept_id'));
|
||||
$list = $query->select();
|
||||
$data = $list->map(function ($item) {
|
||||
return ['id' => $item['id'], 'username' => $item['username'] ?? ''];
|
||||
@@ -91,8 +91,7 @@ class DicePlayerTicketRecordController extends BaseController
|
||||
if (!$model) {
|
||||
return $this->fail('not found');
|
||||
}
|
||||
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
|
||||
if ($allowedIds !== null && !in_array((int) ($model->admin_id ?? 0), $allowedIds, true)) {
|
||||
if (!AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $model->dept_id ?? null, $request->input('dept_id'))) {
|
||||
return $this->fail('no permission to view this record');
|
||||
}
|
||||
$data = is_array($model) ? $model : $model->toArray();
|
||||
@@ -109,6 +108,7 @@ class DicePlayerTicketRecordController extends BaseController
|
||||
{
|
||||
$data = $request->post();
|
||||
$this->validate('save', $data);
|
||||
AdminScopeHelper::prepareBusinessSaveData($data, $this->adminInfo ?? null, $request->input('dept_id'), $data);
|
||||
$result = $this->logic->add($data);
|
||||
if ($result) {
|
||||
return $this->success('add success');
|
||||
@@ -127,6 +127,13 @@ class DicePlayerTicketRecordController extends BaseController
|
||||
{
|
||||
$data = $request->post();
|
||||
$this->validate('update', $data);
|
||||
$model = $this->logic->read($data['id'] ?? 0);
|
||||
if ($model) {
|
||||
$recordDeptId = is_array($model) ? ($model['dept_id'] ?? null) : ($model->dept_id ?? null);
|
||||
if (! AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $recordDeptId, $request->input('dept_id'))) {
|
||||
return $this->fail('no permission to update this record');
|
||||
}
|
||||
}
|
||||
$result = $this->logic->edit($data['id'], $data);
|
||||
if ($result) {
|
||||
return $this->success('update success');
|
||||
|
||||
@@ -47,13 +47,18 @@ class DicePlayerWalletRecordController extends BaseController
|
||||
['create_time_min', ''],
|
||||
['create_time_max', ''],
|
||||
]);
|
||||
$deptId = $request->input('dept_id');
|
||||
$totalCoinChange = $this->logic->sumCoinBySearch($where, $this->adminInfo ?? null, $deptId);
|
||||
|
||||
$query = $this->logic->search($where);
|
||||
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null);
|
||||
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null, $deptId);
|
||||
$query->with([
|
||||
'dicePlayer',
|
||||
'operator',
|
||||
]);
|
||||
|
||||
$data = $this->logic->getList($query);
|
||||
$data['total_coin_change'] = $totalCoinChange;
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
@@ -66,7 +71,7 @@ class DicePlayerWalletRecordController extends BaseController
|
||||
public function getPlayerOptions(Request $request): Response
|
||||
{
|
||||
$query = DicePlayer::field('id,username');
|
||||
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null);
|
||||
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null, $request->input('dept_id'));
|
||||
$list = $query->select();
|
||||
$data = $list->map(function ($item) {
|
||||
return ['id' => $item['id'], 'username' => $item['username'] ?? ''];
|
||||
@@ -87,12 +92,11 @@ class DicePlayerWalletRecordController extends BaseController
|
||||
if ($playerId === null || $playerId === '') {
|
||||
return $this->fail('missing player_id');
|
||||
}
|
||||
$player = DicePlayer::field('coin,admin_id')->where('id', $playerId)->find();
|
||||
$player = DicePlayer::field('coin,dept_id')->where('id', $playerId)->find();
|
||||
if (!$player) {
|
||||
return $this->fail('Player not found');
|
||||
}
|
||||
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
|
||||
if ($allowedIds !== null && !in_array((int) ($player->admin_id ?? 0), $allowedIds, true)) {
|
||||
if (!AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $player->dept_id ?? null)) {
|
||||
return $this->fail('no permission to operate this player');
|
||||
}
|
||||
return $this->success(['wallet_before' => (float) $player['coin']]);
|
||||
@@ -111,8 +115,7 @@ class DicePlayerWalletRecordController extends BaseController
|
||||
if (!$model) {
|
||||
return $this->fail('not found');
|
||||
}
|
||||
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
|
||||
if ($allowedIds !== null && !in_array((int) ($model->admin_id ?? 0), $allowedIds, true)) {
|
||||
if (!AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $model->dept_id ?? null)) {
|
||||
return $this->fail('no permission to view this record');
|
||||
}
|
||||
$data = is_array($model) ? $model : $model->toArray();
|
||||
@@ -166,12 +169,9 @@ class DicePlayerWalletRecordController extends BaseController
|
||||
return $this->fail('please login first');
|
||||
}
|
||||
|
||||
$player = DicePlayer::field('admin_id')->where('id', $playerId)->find();
|
||||
if ($player) {
|
||||
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
|
||||
if ($allowedIds !== null && !in_array((int) ($player->admin_id ?? 0), $allowedIds, true)) {
|
||||
return $this->fail('no permission to operate this player');
|
||||
}
|
||||
$player = DicePlayer::field('dept_id')->where('id', $playerId)->find();
|
||||
if ($player && !AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $player->dept_id ?? null, $request->input('dept_id'))) {
|
||||
return $this->fail('no permission to operate this player');
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -182,6 +182,24 @@ class DicePlayerWalletRecordController extends BaseController
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存数据
|
||||
* @param Request $request
|
||||
* @return Response
|
||||
*/
|
||||
#[Permission('玩家钱包流水添加', 'dice:player_wallet_record:index:save')]
|
||||
public function save(Request $request): Response
|
||||
{
|
||||
$data = $request->post();
|
||||
$this->validate('save', $data);
|
||||
AdminScopeHelper::prepareBusinessSaveData($data, $this->adminInfo ?? null, $request->input('dept_id'), $data);
|
||||
$result = $this->logic->add($data);
|
||||
if ($result) {
|
||||
return $this->success('add success');
|
||||
}
|
||||
return $this->fail('add failed');
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新数据
|
||||
* @param Request $request
|
||||
@@ -192,6 +210,13 @@ class DicePlayerWalletRecordController extends BaseController
|
||||
{
|
||||
$data = $request->post();
|
||||
$this->validate('update', $data);
|
||||
$model = $this->logic->read($data['id'] ?? 0);
|
||||
if ($model) {
|
||||
$recordDeptId = is_array($model) ? ($model['dept_id'] ?? null) : ($model->dept_id ?? null);
|
||||
if (! AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $recordDeptId, $request->input('dept_id'))) {
|
||||
return $this->fail('no permission to update this record');
|
||||
}
|
||||
}
|
||||
$result = $this->logic->edit($data['id'], $data);
|
||||
if ($result) {
|
||||
return $this->success('update success');
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\controller\reward;
|
||||
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use app\dice\logic\reward\DiceRewardLogic;
|
||||
use app\dice\logic\reward_config_record\DiceRewardConfigRecordLogic;
|
||||
use app\dice\model\reward\DiceReward;
|
||||
@@ -42,11 +43,18 @@ class DiceRewardController extends BaseController
|
||||
$orderType = $request->input('orderType', 'asc');
|
||||
|
||||
$logic = new DiceRewardLogic();
|
||||
$data = $logic->getListWithConfig($direction, [
|
||||
'tier' => $tier,
|
||||
'orderField' => $orderField,
|
||||
'orderType' => $orderType,
|
||||
], $page, $limit);
|
||||
$data = $logic->getListWithConfig(
|
||||
$direction,
|
||||
[
|
||||
'tier' => $tier,
|
||||
'orderField' => $orderField,
|
||||
'orderType' => $orderType,
|
||||
],
|
||||
$page,
|
||||
$limit,
|
||||
$this->adminInfo ?? null,
|
||||
$request->input('dept_id')
|
||||
);
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
@@ -61,8 +69,10 @@ class DiceRewardController extends BaseController
|
||||
if (!in_array($direction, [DiceReward::DIRECTION_CLOCKWISE, DiceReward::DIRECTION_COUNTERCLOCKWISE], true)) {
|
||||
$direction = DiceReward::DIRECTION_CLOCKWISE;
|
||||
}
|
||||
$deptId = AdminScopeHelper::resolveConfigDeptId($this->adminInfo ?? null, $request->input('dept_id'));
|
||||
DiceReward::refreshCache($deptId);
|
||||
$logic = new DiceRewardLogic();
|
||||
$data = $logic->getListGroupedByTierForDirection($direction);
|
||||
$data = $logic->getListGroupedByTierForDirection($direction, $deptId);
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
@@ -74,9 +84,10 @@ class DiceRewardController extends BaseController
|
||||
#[Permission('奖励对照列表', 'dice:reward:index:index')]
|
||||
public function weightRatioListWithDirection(Request $request): Response
|
||||
{
|
||||
DiceReward::refreshCache();
|
||||
$deptId = AdminScopeHelper::resolveConfigDeptId($this->adminInfo ?? null, $request->input('dept_id'));
|
||||
DiceReward::refreshCache($deptId);
|
||||
$logic = new DiceRewardLogic();
|
||||
$data = $logic->getListGroupedByTierWithDirection();
|
||||
$data = $logic->getListGroupedByTierWithDirection($deptId);
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
@@ -103,11 +114,14 @@ class DiceRewardController extends BaseController
|
||||
'chain_free_mode' => $post['chain_free_mode'] ?? null,
|
||||
'kill_mode_enabled' => $post['kill_mode_enabled'] ?? null,
|
||||
'test_safety_line' => $post['test_safety_line'] ?? null,
|
||||
'dept_id' => $post['dept_id'] ?? null,
|
||||
'ante_config_id' => $post['ante_config_id'] ?? null,
|
||||
];
|
||||
$adminId = isset($this->adminInfo['id']) ? (int) $this->adminInfo['id'] : null;
|
||||
$requestDeptId = AdminScopeHelper::pickRequestDeptId($request->input('dept_id'), $post);
|
||||
try {
|
||||
$logic = new DiceRewardConfigRecordLogic();
|
||||
$recordId = $logic->createWeightTestRecord($params, $adminId);
|
||||
$recordId = $logic->createWeightTestRecord($params, $adminId, $this->adminInfo ?? null, $requestDeptId);
|
||||
return $this->success(['record_id' => $recordId]);
|
||||
} catch (\plugin\saiadmin\exception\ApiException $e) {
|
||||
return $this->fail($e->getMessage());
|
||||
@@ -167,8 +181,9 @@ class DiceRewardController extends BaseController
|
||||
return $this->fail('parameter items must be an array');
|
||||
}
|
||||
try {
|
||||
$deptId = AdminScopeHelper::resolveConfigDeptId($this->adminInfo ?? null, $request->input('dept_id'));
|
||||
$logic = new DiceRewardLogic();
|
||||
$logic->batchUpdateWeights($items);
|
||||
$logic->batchUpdateWeights($items, $deptId);
|
||||
return $this->success('save success');
|
||||
} catch (\plugin\saiadmin\exception\ApiException $e) {
|
||||
return $this->fail($e->getMessage());
|
||||
@@ -191,8 +206,9 @@ class DiceRewardController extends BaseController
|
||||
return $this->fail('parameter items must be an array');
|
||||
}
|
||||
try {
|
||||
$deptId = AdminScopeHelper::resolveConfigDeptId($this->adminInfo ?? null, $request->input('dept_id'));
|
||||
$logic = new DiceRewardLogic();
|
||||
$logic->batchUpdateWeightsByDirection($direction, $items);
|
||||
$logic->batchUpdateWeightsByDirection($direction, $items, $deptId);
|
||||
return $this->success('save success');
|
||||
} catch (\plugin\saiadmin\exception\ApiException $e) {
|
||||
return $this->fail($e->getMessage());
|
||||
|
||||
@@ -6,9 +6,11 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\controller\reward_config;
|
||||
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use plugin\saiadmin\basic\BaseController;
|
||||
use app\dice\logic\reward_config\DiceRewardConfigLogic;
|
||||
use app\dice\logic\reward\DiceRewardLogic;
|
||||
use app\dice\model\reward\DiceReward;
|
||||
use app\dice\validate\reward_config\DiceRewardConfigValidate;
|
||||
use plugin\saiadmin\service\Permission;
|
||||
use support\Request;
|
||||
@@ -46,7 +48,9 @@ class DiceRewardConfigController extends BaseController
|
||||
['tier', ''],
|
||||
]);
|
||||
$query = $this->logic->search($where);
|
||||
$data = $this->logic->getList($query);
|
||||
AdminScopeHelper::applyConfigScope($query, $this->adminInfo ?? null, $request->input('dept_id'));
|
||||
// 奖励索引 + 大奖权重共约 32 条,配置页需一次返回本渠道全部数据
|
||||
$data = $query->order('id', 'asc')->select()->toArray();
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
@@ -78,6 +82,7 @@ class DiceRewardConfigController extends BaseController
|
||||
{
|
||||
$data = $request->post();
|
||||
$this->validate('save', $data);
|
||||
AdminScopeHelper::prepareConfigSaveData($data, $this->adminInfo ?? null, $request->input('dept_id'), $data);
|
||||
$result = $this->logic->add($data);
|
||||
if ($result) {
|
||||
return $this->success('add success');
|
||||
@@ -95,8 +100,19 @@ class DiceRewardConfigController extends BaseController
|
||||
public function update(Request $request): Response
|
||||
{
|
||||
$data = $request->post();
|
||||
if ($data === [] || $data === null) {
|
||||
$data = $request->all();
|
||||
}
|
||||
$this->validate('update', $data);
|
||||
$result = $this->logic->edit($data['id'], $data);
|
||||
$requestDeptId = AdminScopeHelper::pickRequestDeptId($request->input('dept_id'), is_array($data) ? $data : []);
|
||||
$model = $this->logic->read($data['id'] ?? 0);
|
||||
if ($model) {
|
||||
$recordDeptId = is_array($model) ? ($model['dept_id'] ?? null) : ($model->dept_id ?? null);
|
||||
if (! AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $recordDeptId, $requestDeptId)) {
|
||||
return $this->fail('no permission to update this record');
|
||||
}
|
||||
}
|
||||
$result = $this->logic->edit($data['id'], $data, $this->adminInfo ?? null, $requestDeptId);
|
||||
if ($result) {
|
||||
return $this->success('update success');
|
||||
} else {
|
||||
@@ -123,7 +139,9 @@ class DiceRewardConfigController extends BaseController
|
||||
foreach ($items as $item) {
|
||||
$this->validate('batch_update', array_merge($item, ['id' => $item['id']]));
|
||||
}
|
||||
$this->logic->batchUpdate($items);
|
||||
$requestDeptId = AdminScopeHelper::pickRequestDeptId($request->input('dept_id'), $request->post());
|
||||
$deptId = AdminScopeHelper::resolveConfigDeptId($this->adminInfo ?? null, $requestDeptId);
|
||||
$this->logic->batchUpdate($items, $deptId);
|
||||
return $this->success('save success');
|
||||
}
|
||||
|
||||
@@ -139,7 +157,7 @@ class DiceRewardConfigController extends BaseController
|
||||
if (empty($ids)) {
|
||||
return $this->fail('please select data to delete');
|
||||
}
|
||||
$result = $this->logic->destroy($ids);
|
||||
$result = $this->logic->destroy($ids, $this->adminInfo ?? null, $request->input('dept_id'));
|
||||
if ($result) {
|
||||
return $this->success('delete success');
|
||||
} else {
|
||||
@@ -155,8 +173,10 @@ class DiceRewardConfigController extends BaseController
|
||||
#[Permission('奖励配置列表', 'dice:reward_config:index:index')]
|
||||
public function weightRatioList(Request $request): Response
|
||||
{
|
||||
$deptId = AdminScopeHelper::resolveConfigDeptId($this->adminInfo ?? null, $request->input('dept_id'));
|
||||
DiceReward::refreshCache($deptId);
|
||||
$rewardLogic = new DiceRewardLogic();
|
||||
$data = $rewardLogic->getListGroupedByTierWithDirection();
|
||||
$data = $rewardLogic->getListGroupedByTierWithDirection($deptId);
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
@@ -174,8 +194,9 @@ class DiceRewardConfigController extends BaseController
|
||||
return $this->fail('parameter items must be an array');
|
||||
}
|
||||
try {
|
||||
$deptId = AdminScopeHelper::resolveConfigDeptId($this->adminInfo ?? null, $request->input('dept_id'));
|
||||
$rewardLogic = new DiceRewardLogic();
|
||||
$rewardLogic->batchUpdateWeights($items);
|
||||
$rewardLogic->batchUpdateWeights($items, $deptId);
|
||||
return $this->success('save success');
|
||||
} catch (\plugin\saiadmin\exception\ApiException $e) {
|
||||
return $this->fail($e->getMessage());
|
||||
@@ -199,7 +220,8 @@ class DiceRewardConfigController extends BaseController
|
||||
if ($err !== null) {
|
||||
return $this->fail($err);
|
||||
}
|
||||
$this->logic->batchUpdateBigwinWeight($items);
|
||||
$deptId = AdminScopeHelper::resolveConfigDeptId($this->adminInfo ?? null, $request->input('dept_id'));
|
||||
$this->logic->batchUpdateBigwinWeight($items, $deptId);
|
||||
return $this->success('save success');
|
||||
}
|
||||
|
||||
@@ -214,7 +236,8 @@ class DiceRewardConfigController extends BaseController
|
||||
{
|
||||
try {
|
||||
$rewardLogic = new DiceRewardLogic();
|
||||
$result = $rewardLogic->createRewardReferenceFromConfig();
|
||||
$deptId = AdminScopeHelper::resolveConfigDeptId($this->adminInfo ?? null, $request->input('dept_id'));
|
||||
$result = $rewardLogic->createRewardReferenceFromConfig($deptId);
|
||||
return $this->success($result, 'create reward mapping success');
|
||||
} catch (\plugin\saiadmin\exception\ApiException $e) {
|
||||
return $this->fail($e->getMessage());
|
||||
|
||||
@@ -8,6 +8,7 @@ namespace app\dice\controller\reward_config_record;
|
||||
|
||||
use app\dice\logic\reward_config_record\DiceRewardConfigRecordLogic;
|
||||
use app\dice\validate\reward_config_record\DiceRewardConfigRecordValidate;
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use plugin\saiadmin\basic\BaseController;
|
||||
use plugin\saiadmin\app\model\system\SystemUser;
|
||||
use plugin\saiadmin\service\Permission;
|
||||
@@ -42,6 +43,7 @@ class DiceRewardConfigRecordController extends BaseController
|
||||
['ante', ''],
|
||||
]);
|
||||
$query = $this->logic->search($where);
|
||||
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null, $request->input('dept_id'));
|
||||
$data = $this->logic->getList($query);
|
||||
return $this->success($data);
|
||||
}
|
||||
@@ -96,6 +98,7 @@ class DiceRewardConfigRecordController extends BaseController
|
||||
{
|
||||
$data = $request->post();
|
||||
$this->validate('save', $data);
|
||||
AdminScopeHelper::prepareBusinessSaveData($data, $this->adminInfo ?? null, $request->input('dept_id'), $data);
|
||||
$result = $this->logic->add($data);
|
||||
if ($result) {
|
||||
return $this->success('add success');
|
||||
@@ -114,6 +117,13 @@ class DiceRewardConfigRecordController extends BaseController
|
||||
{
|
||||
$data = $request->post();
|
||||
$this->validate('update', $data);
|
||||
$model = $this->logic->read($data['id'] ?? 0);
|
||||
if ($model) {
|
||||
$recordDeptId = is_array($model) ? ($model['dept_id'] ?? null) : ($model->dept_id ?? null);
|
||||
if (! AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $recordDeptId, $request->input('dept_id'))) {
|
||||
return $this->fail('no permission to update this record');
|
||||
}
|
||||
}
|
||||
$result = $this->logic->edit($data['id'], $data);
|
||||
if ($result) {
|
||||
return $this->success('update success');
|
||||
|
||||
@@ -3,21 +3,75 @@ declare(strict_types=1);
|
||||
|
||||
namespace app\dice\helper;
|
||||
|
||||
use app\dice\model\player\DicePlayer;
|
||||
use plugin\saiadmin\app\model\system\SystemUser;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
|
||||
/**
|
||||
* 管理员数据范围辅助类
|
||||
* 用于获取当前管理员及其部门下属管理员可访问的数据范围
|
||||
* 大富翁数据范围:按渠道 dept_id 隔离(关联 sa_system_dept)
|
||||
*/
|
||||
class AdminScopeHelper
|
||||
{
|
||||
/** 超管查看默认配置模板 */
|
||||
public const DEFAULT_TEMPLATE_DEPT = 0;
|
||||
|
||||
/**
|
||||
* 获取当前管理员可访问的 admin_id 列表
|
||||
* 超级管理员(id=1) 返回 null 表示不限制
|
||||
* 普通管理员返回其本人及部门下属管理员的 id 列表
|
||||
* 当前管理员所属渠道 ID;超级管理员(id=1) 返回 null 表示不限制
|
||||
*/
|
||||
public static function getDeptId(?array $adminInfo): ?int
|
||||
{
|
||||
if (empty($adminInfo) || !isset($adminInfo['id'])) {
|
||||
return null;
|
||||
}
|
||||
$adminId = (int) $adminInfo['id'];
|
||||
if ($adminId <= 1) {
|
||||
return null;
|
||||
}
|
||||
$deptList = $adminInfo['deptList'] ?? [];
|
||||
if (!empty($deptList['id'])) {
|
||||
return (int) $deptList['id'];
|
||||
}
|
||||
if (!empty($adminInfo['dept_id'])) {
|
||||
return (int) $adminInfo['dept_id'];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static function isSuperAdmin(?array $adminInfo): bool
|
||||
{
|
||||
return !empty($adminInfo['id']) && (int) $adminInfo['id'] <= 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析配置类接口的渠道 ID(请求参数 dept_id)
|
||||
* 超管:0 或空=默认模板(null);>0=指定渠道
|
||||
* 普通管理员:固定本渠道
|
||||
*/
|
||||
public static function resolveConfigDeptId(?array $adminInfo, $requestDeptId): int
|
||||
{
|
||||
$scopeDeptId = self::getDeptId($adminInfo);
|
||||
if ($scopeDeptId !== null) {
|
||||
return $scopeDeptId > 0 ? $scopeDeptId : self::DEFAULT_TEMPLATE_DEPT;
|
||||
}
|
||||
if ($requestDeptId === null || $requestDeptId === '') {
|
||||
return self::DEFAULT_TEMPLATE_DEPT;
|
||||
}
|
||||
$id = (int) $requestDeptId;
|
||||
if ($id === self::DEFAULT_TEMPLATE_DEPT) {
|
||||
return self::DEFAULT_TEMPLATE_DEPT;
|
||||
}
|
||||
return $id > 0 ? $id : self::DEFAULT_TEMPLATE_DEPT;
|
||||
}
|
||||
|
||||
public static function isTemplateDeptId($deptId): bool
|
||||
{
|
||||
return $deptId === null || $deptId === '' || (int) $deptId === self::DEFAULT_TEMPLATE_DEPT;
|
||||
}
|
||||
|
||||
/**
|
||||
* 同渠道下可访问的管理员 ID
|
||||
*
|
||||
* @param array|null $adminInfo 当前登录管理员信息(含 id、deptList)
|
||||
* @return int[]|null null=不限制(超级管理员),否则为可访问的 admin_id 数组
|
||||
* @return int[]|null null=不限制
|
||||
*/
|
||||
public static function getAllowedAdminIds(?array $adminInfo): ?array
|
||||
{
|
||||
@@ -28,33 +82,285 @@ class AdminScopeHelper
|
||||
if ($adminId <= 1) {
|
||||
return null;
|
||||
}
|
||||
$deptList = $adminInfo['deptList'] ?? [];
|
||||
if (empty($deptList) || !isset($deptList['id'])) {
|
||||
$deptId = self::getDeptId($adminInfo);
|
||||
if ($deptId === null) {
|
||||
return null;
|
||||
}
|
||||
if ($deptId <= 0) {
|
||||
return [$adminId];
|
||||
}
|
||||
$query = SystemUser::field('id');
|
||||
$query->auth($deptList);
|
||||
$ids = $query->column('id');
|
||||
return array_map('intval', $ids ?: []);
|
||||
$ids = SystemUser::where('dept_id', $deptId)->column('id');
|
||||
return array_map('intval', $ids ?: [$adminId]);
|
||||
}
|
||||
|
||||
public static function fillDeptId(array &$data, ?array $adminInfo, $requestDeptId = null): void
|
||||
{
|
||||
if (isset($data['dept_id']) && $data['dept_id'] !== '' && $data['dept_id'] !== null) {
|
||||
return;
|
||||
}
|
||||
$deptId = self::resolveConfigDeptId($adminInfo, $requestDeptId ?? ($data['filter_dept_id'] ?? null));
|
||||
if ($deptId > 0) {
|
||||
$data['dept_id'] = $deptId;
|
||||
} elseif (!isset($data['dept_id']) || self::isTemplateDeptId($data['dept_id'])) {
|
||||
$data['dept_id'] = self::DEFAULT_TEMPLATE_DEPT;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 对查询应用 admin_id 范围过滤
|
||||
*
|
||||
* @param object $query ThinkORM 查询对象
|
||||
* @param array|null $adminInfo 当前登录管理员信息
|
||||
* @return void
|
||||
* 业务数据新增:按渠道写入 dept_id(玩家、记录等)
|
||||
* 优先级:已有 dept_id → 所属管理员 admin_id → 请求渠道 → 当前登录人渠道
|
||||
*/
|
||||
public static function applyAdminScope($query, ?array $adminInfo): void
|
||||
public static function fillBusinessDeptId(array &$data, ?array $adminInfo, $requestDeptId = null): void
|
||||
{
|
||||
$allowedIds = self::getAllowedAdminIds($adminInfo);
|
||||
if ($allowedIds === null) {
|
||||
if (isset($data['dept_id']) && $data['dept_id'] !== '' && $data['dept_id'] !== null) {
|
||||
$data['dept_id'] = (int) $data['dept_id'];
|
||||
if ($data['dept_id'] > 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($data['player_id'])) {
|
||||
$playerDeptId = self::resolveDeptIdByPlayerId($data['player_id']);
|
||||
if ($playerDeptId !== null && $playerDeptId > 0) {
|
||||
$data['dept_id'] = $playerDeptId;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($data['admin_id'])) {
|
||||
$ownerDeptId = self::resolveDeptIdByAdminId($data['admin_id']);
|
||||
if ($ownerDeptId !== null && $ownerDeptId > 0) {
|
||||
$data['dept_id'] = $ownerDeptId;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$deptId = self::resolveBusinessDeptId($adminInfo, $requestDeptId);
|
||||
if ($deptId !== null && $deptId > 0) {
|
||||
$data['dept_id'] = $deptId;
|
||||
return;
|
||||
}
|
||||
if (empty($allowedIds)) {
|
||||
|
||||
$scopeDeptId = self::getDeptId($adminInfo);
|
||||
if ($scopeDeptId !== null && $scopeDeptId > 0) {
|
||||
$data['dept_id'] = $scopeDeptId;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 业务新增:解析请求渠道并填充 dept_id,缺失时抛错
|
||||
*/
|
||||
public static function prepareBusinessSaveData(
|
||||
array &$data,
|
||||
?array $adminInfo,
|
||||
$inputDeptId = null,
|
||||
array $body = []
|
||||
): void {
|
||||
$requestDeptId = self::pickRequestDeptId($inputDeptId, $body);
|
||||
self::fillBusinessDeptId($data, $adminInfo, $requestDeptId);
|
||||
self::assertBusinessDeptId($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置新增:解析请求渠道并填充 dept_id
|
||||
*/
|
||||
public static function prepareConfigSaveData(
|
||||
array &$data,
|
||||
?array $adminInfo,
|
||||
$inputDeptId = null,
|
||||
array $body = []
|
||||
): void {
|
||||
$requestDeptId = self::pickRequestDeptId($inputDeptId, $body);
|
||||
self::fillDeptId($data, $adminInfo, $requestDeptId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 业务表 dept_id 必须 > 0(非默认模板)
|
||||
*/
|
||||
public static function assertBusinessDeptId(array $data): void
|
||||
{
|
||||
if (!isset($data['dept_id']) || $data['dept_id'] === '' || $data['dept_id'] === null) {
|
||||
throw new ApiException('CHANNEL_DEPT_ID_REQUIRED');
|
||||
}
|
||||
if ((int) $data['dept_id'] <= 0) {
|
||||
throw new ApiException('INVALID_CHANNEL_DEPT_ID');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据玩家 ID 解析所属渠道
|
||||
*/
|
||||
public static function resolveDeptIdByPlayerId($playerId): ?int
|
||||
{
|
||||
if ($playerId === null || $playerId === '') {
|
||||
return null;
|
||||
}
|
||||
$player = DicePlayer::field('dept_id,admin_id')->find($playerId);
|
||||
if (!$player || $player->isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
$deptId = $player->dept_id ?? null;
|
||||
if ($deptId !== null && $deptId !== '' && (int) $deptId > 0) {
|
||||
return (int) $deptId;
|
||||
}
|
||||
if (!empty($player->admin_id)) {
|
||||
return self::resolveDeptIdByAdminId($player->admin_id);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据后台管理员 ID 解析所属渠道
|
||||
*/
|
||||
public static function resolveDeptIdByAdminId($adminId): ?int
|
||||
{
|
||||
if ($adminId === null || $adminId === '') {
|
||||
return null;
|
||||
}
|
||||
$admin = SystemUser::find($adminId);
|
||||
if (!$admin || $admin->isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
$deptId = $admin->dept_id ?? null;
|
||||
if ($deptId !== null && $deptId !== '' && (int) $deptId > 0) {
|
||||
return (int) $deptId;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 规范化记录上的 dept_id(null 视为默认模板 0)
|
||||
*/
|
||||
public static function normalizeRecordDeptId($recordDeptId): int
|
||||
{
|
||||
if ($recordDeptId === null || $recordDeptId === '') {
|
||||
return self::DEFAULT_TEMPLATE_DEPT;
|
||||
}
|
||||
return (int) $recordDeptId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 玩家端读取游戏配置时使用的渠道 ID(玩家 dept_id 优先,否则按所属管理员)
|
||||
*/
|
||||
public static function resolvePlayerConfigDeptId($player): int
|
||||
{
|
||||
$deptId = null;
|
||||
$adminId = null;
|
||||
if (is_array($player)) {
|
||||
$deptId = $player['dept_id'] ?? null;
|
||||
$adminId = $player['admin_id'] ?? null;
|
||||
} elseif (is_object($player)) {
|
||||
$deptId = $player->dept_id ?? null;
|
||||
$adminId = $player->admin_id ?? null;
|
||||
}
|
||||
if ($deptId !== null && $deptId !== '' && (int) $deptId > 0) {
|
||||
return (int) $deptId;
|
||||
}
|
||||
if ($adminId !== null && $adminId !== '') {
|
||||
$fromAdmin = self::resolveDeptIdByAdminId($adminId);
|
||||
if ($fromAdmin !== null && $fromAdmin > 0) {
|
||||
return $fromAdmin;
|
||||
}
|
||||
}
|
||||
return self::DEFAULT_TEMPLATE_DEPT;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从请求参数或请求体中解析 dept_id(兼容 PUT JSON 仅出现在 body 的情况)
|
||||
*/
|
||||
public static function pickRequestDeptId($inputDeptId, array $body = [])
|
||||
{
|
||||
if ($inputDeptId !== null && $inputDeptId !== '') {
|
||||
return $inputDeptId;
|
||||
}
|
||||
if (isset($body['dept_id']) && $body['dept_id'] !== '' && $body['dept_id'] !== null) {
|
||||
return $body['dept_id'];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function canAccessDept(?array $adminInfo, $recordDeptId, $requestDeptId = null): bool
|
||||
{
|
||||
$recordDeptId = self::normalizeRecordDeptId($recordDeptId);
|
||||
$scopeDeptId = self::getDeptId($adminInfo);
|
||||
if ($scopeDeptId === null) {
|
||||
if ($requestDeptId === null || $requestDeptId === '') {
|
||||
return true;
|
||||
}
|
||||
$target = self::resolveConfigDeptId($adminInfo, $requestDeptId);
|
||||
if (self::isTemplateDeptId($target)) {
|
||||
return self::isTemplateDeptId($recordDeptId);
|
||||
}
|
||||
return $recordDeptId === $target;
|
||||
}
|
||||
if ($recordDeptId === self::DEFAULT_TEMPLATE_DEPT && $scopeDeptId > 0) {
|
||||
return false;
|
||||
}
|
||||
return $recordDeptId === $scopeDeptId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 业务页渠道 ID:超管通过请求 dept_id 筛选;未传或 <=0 时不限制
|
||||
*/
|
||||
public static function resolveBusinessDeptId(?array $adminInfo, $requestDeptId): ?int
|
||||
{
|
||||
$scopeDeptId = self::getDeptId($adminInfo);
|
||||
if ($scopeDeptId !== null) {
|
||||
return $scopeDeptId > 0 ? $scopeDeptId : null;
|
||||
}
|
||||
if ($requestDeptId === null || $requestDeptId === '') {
|
||||
return null;
|
||||
}
|
||||
$id = (int) $requestDeptId;
|
||||
return $id > 0 ? $id : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 业务数据列表(玩家、记录、工作台等)
|
||||
*/
|
||||
public static function applyAdminScope($query, ?array $adminInfo, $requestDeptId = null): void
|
||||
{
|
||||
if (self::getDeptId($adminInfo) === null) {
|
||||
$target = self::resolveBusinessDeptId($adminInfo, $requestDeptId);
|
||||
if ($target !== null && $target > 0) {
|
||||
$query->where('dept_id', $target);
|
||||
}
|
||||
return;
|
||||
}
|
||||
$deptId = self::getDeptId($adminInfo);
|
||||
if ($deptId <= 0) {
|
||||
$query->whereRaw('1=0');
|
||||
return;
|
||||
}
|
||||
$query->whereIn('admin_id', $allowedIds);
|
||||
$query->where('dept_id', $deptId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置类列表:超管按所选渠道/默认模板筛选
|
||||
*/
|
||||
public static function applyConfigScope($query, ?array $adminInfo, $requestDeptId = null, string $deptColumn = 'dept_id'): void
|
||||
{
|
||||
$targetDeptId = self::resolveConfigDeptId($adminInfo, $requestDeptId);
|
||||
$scopeDeptId = self::getDeptId($adminInfo);
|
||||
|
||||
if ($scopeDeptId !== null && !self::isSuperAdmin($adminInfo)) {
|
||||
if ($scopeDeptId <= 0) {
|
||||
$query->whereRaw('1=0');
|
||||
return;
|
||||
}
|
||||
$query->where($deptColumn, $scopeDeptId);
|
||||
return;
|
||||
}
|
||||
|
||||
if (self::isTemplateDeptId($targetDeptId)) {
|
||||
$templateId = self::DEFAULT_TEMPLATE_DEPT;
|
||||
$query->where(function ($q) use ($templateId, $deptColumn) {
|
||||
$q->where($deptColumn, $templateId)->whereOr($deptColumn, null);
|
||||
});
|
||||
return;
|
||||
}
|
||||
$query->where($deptColumn, $targetDeptId);
|
||||
}
|
||||
}
|
||||
|
||||
139
server/app/dice/helper/ConfigScopeEditHelper.php
Normal file
139
server/app/dice/helper/ConfigScopeEditHelper.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\dice\helper;
|
||||
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
|
||||
/**
|
||||
* 配置类数据按渠道隔离的更新(防止 find(id) 误更新其他渠道同业务主键行)
|
||||
*/
|
||||
class ConfigScopeEditHelper
|
||||
{
|
||||
/**
|
||||
* 在查询上附加渠道条件
|
||||
*/
|
||||
public static function applyDeptIdWhere($query, int $deptId, string $column = 'dept_id'): void
|
||||
{
|
||||
if (AdminScopeHelper::isTemplateDeptId($deptId)) {
|
||||
$query->where(function ($q) use ($deptId, $column) {
|
||||
$q->where($column, $deptId);
|
||||
if (method_exists($q, 'orWhereNull')) {
|
||||
$q->orWhereNull($column);
|
||||
} else {
|
||||
$q->whereOr($column, null);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
$query->where($column, $deptId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按主键 + 渠道更新配置行
|
||||
*
|
||||
* @param Model $model 模型实例(用于取表名、主键)
|
||||
* @param mixed $primaryKeyValue 列表/表单中的主键值
|
||||
* @param int $deptId 渠道 ID(0=默认模板)
|
||||
* @param array $data 更新字段
|
||||
* @param array $forbidden 禁止写入的字段名
|
||||
*/
|
||||
/**
|
||||
* 按主键更新(主键全局唯一表如 dice_lottery_pool_config)
|
||||
* 以库中记录的 dept_id 为准,避免请求未带 dept_id 时误按默认模板 0 查找失败
|
||||
*/
|
||||
public static function updateByPkAndDept(
|
||||
object $model,
|
||||
$primaryKeyValue,
|
||||
int $requestDeptId,
|
||||
array $data,
|
||||
array $forbidden = ['id', 'dept_id', 'create_time', 'update_time', 'delete_time', 'row_id'],
|
||||
?array $adminInfo = null,
|
||||
$rawRequestDeptId = null
|
||||
): bool {
|
||||
foreach ($forbidden as $field) {
|
||||
unset($data[$field]);
|
||||
}
|
||||
if ($data === []) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$pk = self::resolvePk($model);
|
||||
$record = $model->where($pk, $primaryKeyValue)->find();
|
||||
if ($record === null) {
|
||||
throw new ApiException('data not found');
|
||||
}
|
||||
|
||||
$recordDeptId = AdminScopeHelper::normalizeRecordDeptId(
|
||||
is_array($record) ? ($record['dept_id'] ?? null) : ($record->dept_id ?? null)
|
||||
);
|
||||
|
||||
if ($adminInfo !== null && ! AdminScopeHelper::canAccessDept($adminInfo, $recordDeptId, $rawRequestDeptId)) {
|
||||
throw new ApiException('no permission to update this record');
|
||||
}
|
||||
|
||||
if ($rawRequestDeptId !== null && $rawRequestDeptId !== '') {
|
||||
$targetDeptId = AdminScopeHelper::resolveConfigDeptId($adminInfo, $rawRequestDeptId);
|
||||
if ($targetDeptId !== $recordDeptId) {
|
||||
throw new ApiException('record does not belong to selected channel');
|
||||
}
|
||||
}
|
||||
|
||||
$query = $model->where($pk, $primaryKeyValue);
|
||||
self::applyDeptIdWhere($query, $recordDeptId);
|
||||
|
||||
$affected = $query->update($data);
|
||||
return $affected !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* dice_reward_config / dice_config:业务 id(0~25 等)+ 渠道更新
|
||||
*/
|
||||
public static function updateByBusinessIdAndDept(
|
||||
object $model,
|
||||
int $businessId,
|
||||
int $deptId,
|
||||
array $data,
|
||||
array $forbidden = ['id', 'dept_id', 'create_time', 'update_time', 'delete_time', 'row_id']
|
||||
): bool {
|
||||
foreach ($forbidden as $field) {
|
||||
unset($data[$field]);
|
||||
}
|
||||
if ($data === []) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$query = $model->where('id', $businessId);
|
||||
self::applyDeptIdWhere($query, $deptId);
|
||||
|
||||
$record = (clone $query)->find();
|
||||
if ($record === null) {
|
||||
throw new ApiException('config id=' . $businessId . ' not found for current channel');
|
||||
}
|
||||
|
||||
$affected = $query->update($data);
|
||||
return $affected !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表/读取:按主键 + 渠道取单条(避免 find(pk) 命中其他渠道)
|
||||
*/
|
||||
private static function resolvePk(object $model): string
|
||||
{
|
||||
if (method_exists($model, 'getPk')) {
|
||||
return (string) $model->getPk();
|
||||
}
|
||||
if (method_exists($model, 'getKeyName')) {
|
||||
return (string) $model->getKeyName();
|
||||
}
|
||||
return 'id';
|
||||
}
|
||||
|
||||
public static function findByPkAndDept(object $model, $primaryKeyValue, int $deptId)
|
||||
{
|
||||
$pk = self::resolvePk($model);
|
||||
$query = $model->where($pk, $primaryKeyValue);
|
||||
self::applyDeptIdWhere($query, $deptId);
|
||||
return $query->find();
|
||||
}
|
||||
}
|
||||
@@ -6,13 +6,15 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\logic\ante_config;
|
||||
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use app\dice\helper\ConfigScopeEditHelper;
|
||||
use app\dice\model\ante_config\DiceAnteConfig;
|
||||
use plugin\saiadmin\basic\think\BaseLogic;
|
||||
use app\dice\basic\DiceBaseLogic;
|
||||
|
||||
/**
|
||||
* 底注配置逻辑层
|
||||
*/
|
||||
class DiceAnteConfigLogic extends BaseLogic
|
||||
class DiceAnteConfigLogic extends DiceBaseLogic
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
@@ -23,21 +25,32 @@ class DiceAnteConfigLogic extends BaseLogic
|
||||
{
|
||||
return $this->transaction(function () use ($data) {
|
||||
$this->normalizeDefaultField($data);
|
||||
$deptId = AdminScopeHelper::resolveConfigDeptId(null, $data['dept_id'] ?? AdminScopeHelper::DEFAULT_TEMPLATE_DEPT);
|
||||
if ((int) ($data['is_default'] ?? 0) === 1) {
|
||||
$this->clearOtherDefaults();
|
||||
$this->clearOtherDefaults(null, $deptId);
|
||||
}
|
||||
return parent::add($data);
|
||||
});
|
||||
}
|
||||
|
||||
public function edit($id, array $data): mixed
|
||||
public function edit($id, array $data, ?array $adminInfo = null, $requestDeptId = null): mixed
|
||||
{
|
||||
return $this->transaction(function () use ($id, $data) {
|
||||
$pickedDeptId = AdminScopeHelper::pickRequestDeptId($requestDeptId, $data);
|
||||
$deptId = AdminScopeHelper::resolveConfigDeptId($adminInfo, $pickedDeptId);
|
||||
return $this->transaction(function () use ($id, $data, $deptId, $adminInfo, $pickedDeptId) {
|
||||
$this->normalizeDefaultField($data);
|
||||
if ((int) ($data['is_default'] ?? 0) === 1) {
|
||||
$this->clearOtherDefaults((int) $id);
|
||||
$this->clearOtherDefaults((int) $id, $deptId);
|
||||
}
|
||||
return parent::edit($id, $data);
|
||||
return ConfigScopeEditHelper::updateByPkAndDept(
|
||||
$this->model,
|
||||
$id,
|
||||
$deptId,
|
||||
$data,
|
||||
['id', 'dept_id', 'create_time', 'update_time', 'delete_time', 'row_id'],
|
||||
$adminInfo,
|
||||
$pickedDeptId
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -79,9 +92,10 @@ class DiceAnteConfigLogic extends BaseLogic
|
||||
$data['is_default'] = ((int) $data['is_default']) === 1 ? 1 : 0;
|
||||
}
|
||||
|
||||
private function clearOtherDefaults(?int $excludeId = null): void
|
||||
private function clearOtherDefaults(?int $excludeId = null, int $deptId = AdminScopeHelper::DEFAULT_TEMPLATE_DEPT): void
|
||||
{
|
||||
$query = $this->model->where('is_default', 1);
|
||||
ConfigScopeEditHelper::applyDeptIdWhere($query, $deptId);
|
||||
if ($excludeId !== null && $excludeId > 0) {
|
||||
$query->where('id', '<>', $excludeId);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\logic\config;
|
||||
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use app\dice\helper\ConfigScopeEditHelper;
|
||||
use plugin\saiadmin\basic\eloquent\BaseLogic;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
use plugin\saiadmin\utils\Helper;
|
||||
@@ -14,7 +16,7 @@ use app\dice\model\config\DiceConfig;
|
||||
/**
|
||||
* 摇色子配置逻辑层
|
||||
*/
|
||||
class DiceConfigLogic extends BaseLogic
|
||||
class DiceConfigLogic extends DiceBaseLogic
|
||||
{
|
||||
/**
|
||||
* 构造函数
|
||||
@@ -24,4 +26,13 @@ class DiceConfigLogic extends BaseLogic
|
||||
$this->model = new DiceConfig();
|
||||
}
|
||||
|
||||
public function edit($id, array $data, ?array $adminInfo = null, $requestDeptId = null): mixed
|
||||
{
|
||||
$deptId = AdminScopeHelper::resolveConfigDeptId(
|
||||
$adminInfo,
|
||||
AdminScopeHelper::pickRequestDeptId($requestDeptId, $data)
|
||||
);
|
||||
return ConfigScopeEditHelper::updateByBusinessIdAndDept($this->model, (int) $id, $deptId, $data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,16 +4,33 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\logic\game;
|
||||
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use app\dice\helper\ConfigScopeEditHelper;
|
||||
use plugin\saiadmin\basic\eloquent\BaseLogic;
|
||||
use app\dice\model\game\DiceGame;
|
||||
|
||||
/**
|
||||
* 游戏管理逻辑层
|
||||
*/
|
||||
class DiceGameLogic extends BaseLogic
|
||||
class DiceGameLogic extends DiceBaseLogic
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->model = new DiceGame();
|
||||
}
|
||||
|
||||
public function edit($id, array $data, ?array $adminInfo = null, $requestDeptId = null): mixed
|
||||
{
|
||||
$pickedDeptId = AdminScopeHelper::pickRequestDeptId($requestDeptId, $data);
|
||||
$deptId = AdminScopeHelper::resolveConfigDeptId($adminInfo, $pickedDeptId);
|
||||
return ConfigScopeEditHelper::updateByPkAndDept(
|
||||
$this->model,
|
||||
$id,
|
||||
$deptId,
|
||||
$data,
|
||||
['id', 'dept_id', 'create_time', 'update_time', 'delete_time', 'row_id'],
|
||||
$adminInfo,
|
||||
$pickedDeptId
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,10 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\logic\lottery_pool_config;
|
||||
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use app\dice\helper\ConfigScopeEditHelper;
|
||||
use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig;
|
||||
use plugin\saiadmin\basic\think\BaseLogic;
|
||||
use app\dice\basic\DiceBaseLogic;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
use plugin\saiadmin\utils\Helper;
|
||||
use support\think\Cache;
|
||||
@@ -15,7 +17,7 @@ use support\think\Cache;
|
||||
/**
|
||||
* 色子奖池配置逻辑层
|
||||
*/
|
||||
class DiceLotteryPoolConfigLogic extends BaseLogic
|
||||
class DiceLotteryPoolConfigLogic extends DiceBaseLogic
|
||||
{
|
||||
/** Redis 当前彩金池(type=0 实例)key,无则按 type=0 创建 */
|
||||
private const REDIS_KEY_CURRENT_POOL = 'api:game:lottery_pool:default';
|
||||
@@ -30,19 +32,55 @@ class DiceLotteryPoolConfigLogic extends BaseLogic
|
||||
$this->model = new DiceLotteryPoolConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* 按渠道隔离更新(主键 id 全局唯一,仍校验 dept_id 防止越权)
|
||||
*/
|
||||
public function edit($id, array $data, ?array $adminInfo = null, $requestDeptId = null): mixed
|
||||
{
|
||||
$pickedDeptId = AdminScopeHelper::pickRequestDeptId($requestDeptId, $data);
|
||||
$deptId = AdminScopeHelper::resolveConfigDeptId($adminInfo, $pickedDeptId);
|
||||
return ConfigScopeEditHelper::updateByPkAndDept(
|
||||
$this->model,
|
||||
$id,
|
||||
$deptId,
|
||||
$data,
|
||||
['id', 'dept_id', 'create_time', 'update_time', 'delete_time', 'row_id'],
|
||||
$adminInfo,
|
||||
$pickedDeptId
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前彩金池(type=0)+ 杀分权重为 type=1 的只读展示
|
||||
* profit_amount 每次从 DB 实时读取;t1_weight~t5_weight 来自 type=1(杀分权重,不可在弹窗内修改)
|
||||
*
|
||||
* @return array{id:int,name:string,safety_line:int,kill_enabled:int,t1_weight:int,...,t5_weight:int,profit_amount:float}
|
||||
*/
|
||||
public function getCurrentPool(): array
|
||||
public function getCurrentPool(int $deptId = AdminScopeHelper::DEFAULT_TEMPLATE_DEPT): array
|
||||
{
|
||||
$configType0 = DiceLotteryPoolConfig::where('name', 'default')->find();
|
||||
$query0 = DiceLotteryPoolConfig::where('name', 'default');
|
||||
if (AdminScopeHelper::isTemplateDeptId($deptId)) {
|
||||
$query0->where(function ($q) {
|
||||
$q->where('dept_id', AdminScopeHelper::DEFAULT_TEMPLATE_DEPT)
|
||||
->whereOr('dept_id', null);
|
||||
});
|
||||
} else {
|
||||
$query0->where('dept_id', $deptId);
|
||||
}
|
||||
$configType0 = $query0->find();
|
||||
if (!$configType0) {
|
||||
throw new ApiException('No name=default pool config found, please create one first');
|
||||
}
|
||||
$configType1 = DiceLotteryPoolConfig::where('name', 'killScore')->find();
|
||||
$query1 = DiceLotteryPoolConfig::where('name', 'killScore');
|
||||
if (AdminScopeHelper::isTemplateDeptId($deptId)) {
|
||||
$query1->where(function ($q) {
|
||||
$q->where('dept_id', AdminScopeHelper::DEFAULT_TEMPLATE_DEPT)
|
||||
->whereOr('dept_id', null);
|
||||
});
|
||||
} else {
|
||||
$query1->where('dept_id', $deptId);
|
||||
}
|
||||
$configType1 = $query1->find();
|
||||
$row0 = $configType0->toArray();
|
||||
$profitAmount = isset($row0['profit_amount']) ? (float) $row0['profit_amount'] : (isset($row0['ev']) ? (float) $row0['ev'] : 0.0);
|
||||
$pool = [
|
||||
@@ -66,9 +104,9 @@ class DiceLotteryPoolConfigLogic extends BaseLogic
|
||||
*
|
||||
* @param array{safety_line?:int,kill_enabled?:int} $data
|
||||
*/
|
||||
public function updateCurrentPool(array $data): void
|
||||
public function updateCurrentPool(array $data, int $deptId = AdminScopeHelper::DEFAULT_TEMPLATE_DEPT): void
|
||||
{
|
||||
$pool = $this->getCurrentPool();
|
||||
$pool = $this->getCurrentPool($deptId);
|
||||
$id = (int) $pool['id'];
|
||||
if (!array_key_exists('safety_line', $data) && !array_key_exists('kill_enabled', $data)) {
|
||||
return;
|
||||
@@ -83,17 +121,21 @@ class DiceLotteryPoolConfigLogic extends BaseLogic
|
||||
if ($update === []) {
|
||||
return;
|
||||
}
|
||||
DiceLotteryPoolConfig::where('id', $id)->update($update);
|
||||
$query = DiceLotteryPoolConfig::where('id', $id);
|
||||
ConfigScopeEditHelper::applyDeptIdWhere($query, $deptId);
|
||||
$query->update($update);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置当前彩金池的玩家累计盈利:将 profit_amount 置为 0,并刷新 Redis 缓存
|
||||
*/
|
||||
public function resetProfitAmount(): void
|
||||
public function resetProfitAmount(int $deptId = AdminScopeHelper::DEFAULT_TEMPLATE_DEPT): void
|
||||
{
|
||||
$pool = $this->getCurrentPool();
|
||||
$pool = $this->getCurrentPool($deptId);
|
||||
$id = (int) $pool['id'];
|
||||
DiceLotteryPoolConfig::where('id', $id)->update(['profit_amount' => 0]);
|
||||
$query = DiceLotteryPoolConfig::where('id', $id);
|
||||
ConfigScopeEditHelper::applyDeptIdWhere($query, $deptId);
|
||||
$query->update(['profit_amount' => 0]);
|
||||
$pool['profit_amount'] = 0.0;
|
||||
Cache::set(self::REDIS_KEY_CURRENT_POOL, json_encode($pool), self::EXPIRE);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\logic\play_record;
|
||||
|
||||
use plugin\saiadmin\basic\think\BaseLogic;
|
||||
use app\dice\basic\DiceBaseLogic;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
use plugin\saiadmin\utils\Helper;
|
||||
use app\dice\model\play_record\DicePlayRecord;
|
||||
@@ -14,7 +14,7 @@ use app\dice\model\play_record\DicePlayRecord;
|
||||
/**
|
||||
* 玩家抽奖记录逻辑层
|
||||
*/
|
||||
class DicePlayRecordLogic extends BaseLogic
|
||||
class DicePlayRecordLogic extends DiceBaseLogic
|
||||
{
|
||||
/**
|
||||
* 构造函数
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\logic\play_record_test;
|
||||
|
||||
use plugin\saiadmin\basic\think\BaseLogic;
|
||||
use app\dice\basic\DiceBaseLogic;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
use plugin\saiadmin\utils\Helper;
|
||||
use app\dice\model\play_record_test\DicePlayRecordTest;
|
||||
@@ -14,7 +14,7 @@ use app\dice\model\play_record_test\DicePlayRecordTest;
|
||||
/**
|
||||
* 玩家抽奖记录(测试数据)逻辑层
|
||||
*/
|
||||
class DicePlayRecordTestLogic extends BaseLogic
|
||||
class DicePlayRecordTestLogic extends DiceBaseLogic
|
||||
{
|
||||
/**
|
||||
* 构造函数
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\logic\player;
|
||||
|
||||
use plugin\saiadmin\basic\think\BaseLogic;
|
||||
use app\dice\basic\DiceBaseLogic;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
use plugin\saiadmin\utils\Helper;
|
||||
use app\dice\model\player\DicePlayer;
|
||||
@@ -14,7 +14,7 @@ use app\dice\model\player\DicePlayer;
|
||||
/**
|
||||
* 大富翁-玩家逻辑层
|
||||
*/
|
||||
class DicePlayerLogic extends BaseLogic
|
||||
class DicePlayerLogic extends DiceBaseLogic
|
||||
{
|
||||
/** 密码加密盐(可与 config 统一) */
|
||||
private const PASSWORD_SALT = 'dice_player_salt_2024';
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\logic\player_ticket_record;
|
||||
|
||||
use plugin\saiadmin\basic\think\BaseLogic;
|
||||
use app\dice\basic\DiceBaseLogic;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
use plugin\saiadmin\utils\Helper;
|
||||
use app\dice\model\player_ticket_record\DicePlayerTicketRecord;
|
||||
@@ -14,7 +14,7 @@ use app\dice\model\player_ticket_record\DicePlayerTicketRecord;
|
||||
/**
|
||||
* 抽奖券获取记录逻辑层
|
||||
*/
|
||||
class DicePlayerTicketRecordLogic extends BaseLogic
|
||||
class DicePlayerTicketRecordLogic extends DiceBaseLogic
|
||||
{
|
||||
/**
|
||||
* 构造函数
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\logic\player_wallet_record;
|
||||
|
||||
use plugin\saiadmin\basic\think\BaseLogic;
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use app\dice\basic\DiceBaseLogic;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
use app\dice\model\player_wallet_record\DicePlayerWalletRecord;
|
||||
use app\dice\model\player\DicePlayer;
|
||||
@@ -15,7 +16,7 @@ use app\api\cache\UserCache;
|
||||
/**
|
||||
* 玩家钱包流水逻辑层
|
||||
*/
|
||||
class DicePlayerWalletRecordLogic extends BaseLogic
|
||||
class DicePlayerWalletRecordLogic extends DiceBaseLogic
|
||||
{
|
||||
/**
|
||||
* 构造函数
|
||||
@@ -27,6 +28,18 @@ class DicePlayerWalletRecordLogic extends BaseLogic
|
||||
$this->setOrderType('DESC');
|
||||
}
|
||||
|
||||
/**
|
||||
* 按与列表相同的筛选条件汇总平台币变化(不含 with / 分页 / 排序)
|
||||
*/
|
||||
public function sumCoinBySearch(array $where, ?array $adminInfo, $requestDeptId = null): float
|
||||
{
|
||||
$query = $this->search($where);
|
||||
AdminScopeHelper::applyAdminScope($query, $adminInfo, $requestDeptId);
|
||||
$table = $this->model->getTable();
|
||||
$sum = $query->sum($table . '.coin');
|
||||
return round((float) $sum, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加数据(补全抽奖次数字段默认值)
|
||||
*/
|
||||
@@ -83,9 +96,11 @@ class DicePlayerWalletRecordLogic extends BaseLogic
|
||||
}
|
||||
|
||||
$playerAdminId = ($player->admin_id ?? null) ? (int) $player->admin_id : null;
|
||||
$playerDeptId = ($player->dept_id ?? null) ? (int) $player->dept_id : null;
|
||||
$record = [
|
||||
'player_id' => $playerId,
|
||||
'admin_id' => $playerAdminId,
|
||||
'dept_id' => $playerDeptId,
|
||||
'coin' => $type === 3 ? $coin : -$coin,
|
||||
'type' => $type,
|
||||
'wallet_before' => $walletBefore,
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\logic\reward;
|
||||
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use app\dice\helper\ConfigScopeEditHelper;
|
||||
use app\dice\model\reward\DiceReward;
|
||||
use app\dice\model\reward_config\DiceRewardConfig;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
@@ -29,8 +31,14 @@ class DiceRewardLogic
|
||||
* @param int $limit
|
||||
* @return array{total: int, per_page: int, current_page: int, data: array}
|
||||
*/
|
||||
public function getListWithConfig(int $direction, array $where, int $page = 1, int $limit = 10): array
|
||||
{
|
||||
public function getListWithConfig(
|
||||
int $direction,
|
||||
array $where,
|
||||
int $page = 1,
|
||||
int $limit = 10,
|
||||
?array $adminInfo = null,
|
||||
$requestDeptId = null
|
||||
): array {
|
||||
$tier = isset($where['tier']) ? trim((string) $where['tier']) : '';
|
||||
$orderField = isset($where['orderField']) && $where['orderField'] !== '' ? (string) $where['orderField'] : 'r.tier';
|
||||
$orderType = isset($where['orderType']) && strtoupper((string) $where['orderType']) === 'DESC' ? 'desc' : 'asc';
|
||||
@@ -41,6 +49,10 @@ class DiceRewardLogic
|
||||
->order($orderField, $orderType)
|
||||
->order('r.end_index', 'asc');
|
||||
|
||||
if ($adminInfo !== null) {
|
||||
AdminScopeHelper::applyConfigScope($query, $adminInfo, $requestDeptId, 'r.dept_id');
|
||||
}
|
||||
|
||||
if ($tier !== '') {
|
||||
$query->where('r.tier', $tier);
|
||||
}
|
||||
@@ -74,7 +86,7 @@ class DiceRewardLogic
|
||||
* @param int $direction 0=顺时针 1=逆时针
|
||||
* @param array<int, array{id: int, weight: int}> $items id 为 end_index(DiceRewardConfig.id)
|
||||
*/
|
||||
public function batchUpdateWeightsByDirection(int $direction, array $items): void
|
||||
public function batchUpdateWeightsByDirection(int $direction, array $items, ?int $deptId = null): void
|
||||
{
|
||||
if (empty($items)) {
|
||||
return;
|
||||
@@ -90,23 +102,36 @@ class DiceRewardLogic
|
||||
}
|
||||
$weight = max(self::WEIGHT_MIN, min(self::WEIGHT_MAX, $weight));
|
||||
|
||||
$tier = DiceRewardConfig::where('id', $id)->value('tier');
|
||||
$configQuery = DiceRewardConfig::where('id', $id);
|
||||
if ($deptId !== null) {
|
||||
ConfigScopeEditHelper::applyDeptIdWhere($configQuery, $deptId);
|
||||
}
|
||||
$tier = $configQuery->value('tier');
|
||||
if ($tier === null || $tier === '') {
|
||||
throw new ApiException(\app\api\util\ApiLang::translateParams('配置ID %s 不存在或档位为空', [$id]));
|
||||
}
|
||||
$tier = (string) $tier;
|
||||
|
||||
$affected = DiceReward::where('tier', $tier)->where('direction', $direction)->where('end_index', $id)->update(['weight' => $weight]);
|
||||
$rewardQuery = DiceReward::where('tier', $tier)
|
||||
->where('direction', $direction)
|
||||
->where('end_index', $id);
|
||||
if ($deptId !== null) {
|
||||
ConfigScopeEditHelper::applyDeptIdWhere($rewardQuery, $deptId);
|
||||
}
|
||||
$affected = $rewardQuery->update(['weight' => $weight]);
|
||||
if ($affected === 0) {
|
||||
$m = new DiceReward();
|
||||
$m->tier = $tier;
|
||||
$m->direction = $direction;
|
||||
$m->end_index = $id;
|
||||
$m->weight = $weight;
|
||||
if ($deptId !== null && $deptId > 0) {
|
||||
$m->dept_id = $deptId;
|
||||
}
|
||||
$m->save();
|
||||
}
|
||||
}
|
||||
DiceReward::refreshCache();
|
||||
DiceReward::refreshCache($deptId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,11 +139,11 @@ class DiceRewardLogic
|
||||
* @param int $direction 0=顺时针 1=逆时针
|
||||
* @return array<string, array> 键 T1|T2|...|BIGWIN,值为该档位下带 weight 的行数组
|
||||
*/
|
||||
public function getListGroupedByTierForDirection(int $direction): array
|
||||
public function getListGroupedByTierForDirection(int $direction, ?int $deptId = null): array
|
||||
{
|
||||
$configInstance = DiceRewardConfig::getCachedInstance();
|
||||
$configInstance = DiceRewardConfig::getCachedInstance($deptId);
|
||||
$byTier = $configInstance['by_tier'] ?? [];
|
||||
$rewardInstance = DiceReward::getCachedInstance();
|
||||
$rewardInstance = DiceReward::getCachedInstance($deptId);
|
||||
$byTierDirection = $rewardInstance['by_tier_direction'] ?? [];
|
||||
|
||||
$result = [];
|
||||
@@ -153,9 +178,9 @@ class DiceRewardLogic
|
||||
*
|
||||
* @return array<string, array{0: array, 1: array}>
|
||||
*/
|
||||
public function getListGroupedByTierWithDirection(): array
|
||||
public function getListGroupedByTierWithDirection(?int $deptId = null): array
|
||||
{
|
||||
$rewardInstance = DiceReward::getCachedInstance();
|
||||
$rewardInstance = DiceReward::getCachedInstance($deptId);
|
||||
$byTierDirection = $rewardInstance['by_tier_direction'] ?? [];
|
||||
|
||||
$result = [];
|
||||
@@ -185,7 +210,7 @@ class DiceRewardLogic
|
||||
* @param array<int, array{id: int, weight: int}> $items 每项 id 为 dice_reward 表主键,weight 为 1-10000
|
||||
* @throws ApiException
|
||||
*/
|
||||
public function batchUpdateWeights(array $items): void
|
||||
public function batchUpdateWeights(array $items, ?int $deptId = null): void
|
||||
{
|
||||
if (empty($items)) {
|
||||
return;
|
||||
@@ -203,13 +228,24 @@ class DiceRewardLogic
|
||||
}
|
||||
$weight = isset($item['weight']) ? (int) $item['weight'] : self::WEIGHT_MIN;
|
||||
$weight = max(self::WEIGHT_MIN, min(self::WEIGHT_MAX, $weight));
|
||||
$model = DiceReward::find($id);
|
||||
$query = DiceReward::where('id', $id);
|
||||
if ($deptId !== null) {
|
||||
if (AdminScopeHelper::isTemplateDeptId($deptId)) {
|
||||
$query->where(function ($q) {
|
||||
$q->where('dept_id', AdminScopeHelper::DEFAULT_TEMPLATE_DEPT)
|
||||
->whereOr('dept_id', null);
|
||||
});
|
||||
} else {
|
||||
$query->where('dept_id', $deptId);
|
||||
}
|
||||
}
|
||||
$model = $query->find();
|
||||
if ($model !== null) {
|
||||
$model->weight = $weight;
|
||||
$model->save();
|
||||
}
|
||||
}
|
||||
DiceReward::refreshCache();
|
||||
DiceReward::refreshCache($deptId);
|
||||
}
|
||||
|
||||
/** BIGWIN 权重范围:0=0% 中奖,10000=100% 中奖;grid_number=5/30 固定 100% 不可改 */
|
||||
@@ -219,9 +255,9 @@ class DiceRewardLogic
|
||||
* 按 grid_number 获取 BIGWIN 档位权重(取顺时针方向,用于编辑展示)
|
||||
* 若 DiceReward 无该点数则 5/30 返回 10000,其余返回 0
|
||||
*/
|
||||
public function getBigwinWeightByGridNumber(int $gridNumber): int
|
||||
public function getBigwinWeightByGridNumber(int $gridNumber, ?int $deptId = null): int
|
||||
{
|
||||
$inst = DiceReward::getCachedInstance();
|
||||
$inst = DiceReward::getCachedInstance($deptId);
|
||||
$rows = $inst['by_tier_direction']['BIGWIN'][DiceReward::DIRECTION_CLOCKWISE] ?? [];
|
||||
foreach ($rows as $row) {
|
||||
if ((int) ($row['grid_number'] ?? 0) === $gridNumber) {
|
||||
@@ -235,21 +271,24 @@ class DiceRewardLogic
|
||||
* 更新 BIGWIN 档位某点数的权重(顺/逆时针同时更新);0=0% 中奖,10000=100% 中奖
|
||||
* 表 dice_reward 唯一键为 (direction, grid_number),同一点数同一方向仅一条记录,故先按该键查找再更新,避免重复插入
|
||||
*/
|
||||
public function updateBigwinWeight(int $gridNumber, int $weight): void
|
||||
public function updateBigwinWeight(int $gridNumber, int $weight, ?int $deptId = null): void
|
||||
{
|
||||
$weight = min(self::BIGWIN_WEIGHT_MAX, max(0, $weight));
|
||||
$config = DiceRewardConfig::where('tier', 'BIGWIN')
|
||||
->where('grid_number', $gridNumber)
|
||||
->find();
|
||||
if ($deptId === null) {
|
||||
$deptId = AdminScopeHelper::DEFAULT_TEMPLATE_DEPT;
|
||||
}
|
||||
$configQuery = DiceRewardConfig::where('tier', 'BIGWIN')->where('grid_number', $gridNumber);
|
||||
ConfigScopeEditHelper::applyDeptIdWhere($configQuery, $deptId);
|
||||
$config = $configQuery->find();
|
||||
if (! $config) {
|
||||
return;
|
||||
}
|
||||
$configArr = $config->toArray();
|
||||
foreach ([DiceReward::DIRECTION_CLOCKWISE, DiceReward::DIRECTION_COUNTERCLOCKWISE] as $direction) {
|
||||
// 按唯一键 (direction, grid_number) 查找,存在则更新,不存在则插入
|
||||
$row = DiceReward::where('direction', $direction)
|
||||
->where('grid_number', $gridNumber)
|
||||
->find();
|
||||
$rowQuery = DiceReward::where('direction', $direction)->where('grid_number', $gridNumber);
|
||||
ConfigScopeEditHelper::applyDeptIdWhere($rowQuery, $deptId);
|
||||
$row = $rowQuery->find();
|
||||
if ($row) {
|
||||
$row->tier = 'BIGWIN';
|
||||
$row->weight = $weight > 0 ? $weight : self::WEIGHT_MIN;
|
||||
@@ -272,10 +311,13 @@ class DiceRewardLogic
|
||||
$m->remark = (string) ($configArr['remark'] ?? '');
|
||||
$m->type = $configArr['type'] ?? null;
|
||||
$m->weight = $weight > 0 ? $weight : self::WEIGHT_MIN;
|
||||
if (!AdminScopeHelper::isTemplateDeptId($deptId)) {
|
||||
$m->dept_id = $deptId;
|
||||
}
|
||||
$m->save();
|
||||
}
|
||||
}
|
||||
DiceReward::refreshCache();
|
||||
DiceReward::refreshCache($deptId);
|
||||
}
|
||||
|
||||
/** 盘面格数(用于顺时针/逆时针计算 end_index) */
|
||||
@@ -309,9 +351,19 @@ class DiceRewardLogic
|
||||
* @return array{created_clockwise: int, created_counterclockwise: int, updated_clockwise: int, updated_counterclockwise: int, skipped: int}
|
||||
* @throws ApiException
|
||||
*/
|
||||
public function createRewardReferenceFromConfig(): array
|
||||
public function createRewardReferenceFromConfig(?int $deptId = null): array
|
||||
{
|
||||
$list = DiceRewardConfig::order('id', 'asc')->select()->toArray();
|
||||
$configQuery = DiceRewardConfig::order('id', 'asc');
|
||||
if ($deptId === null || $deptId === \app\dice\helper\AdminScopeHelper::DEFAULT_TEMPLATE_DEPT) {
|
||||
$templateId = \app\dice\helper\AdminScopeHelper::DEFAULT_TEMPLATE_DEPT;
|
||||
$configQuery->where(function ($q) use ($templateId) {
|
||||
$q->where('dept_id', $templateId)->whereOr('dept_id', 'null');
|
||||
});
|
||||
$deptId = null;
|
||||
} else {
|
||||
$configQuery->where('dept_id', $deptId);
|
||||
}
|
||||
$list = $configQuery->select()->toArray();
|
||||
if (empty($list)) {
|
||||
throw new ApiException('Reward config is empty, please maintain dice_reward_config first');
|
||||
}
|
||||
@@ -326,8 +378,12 @@ class DiceRewardLogic
|
||||
}
|
||||
|
||||
$table = (new DiceReward())->getTable();
|
||||
Db::execute('DELETE FROM `' . $table . '`');
|
||||
DiceReward::refreshCache();
|
||||
if ($deptId === null) {
|
||||
Db::table($table)->whereNull('dept_id')->delete();
|
||||
} else {
|
||||
Db::table($table)->where('dept_id', $deptId)->delete();
|
||||
}
|
||||
DiceReward::refreshCache($deptId ?? AdminScopeHelper::DEFAULT_TEMPLATE_DEPT);
|
||||
|
||||
// 按 id 排序后,盘面位置 0..25 对应 $list[$pos],避免 config.id 非 0-25/1-26 时取模结果找不到
|
||||
$gridToPosition = [];
|
||||
@@ -379,7 +435,13 @@ class DiceRewardLogic
|
||||
'remark' => $configCw['remark'] ?? '',
|
||||
'type' => isset($configCw['type']) ? (int) $configCw['type'] : 0,
|
||||
];
|
||||
$existing = DiceReward::where('direction', DiceReward::DIRECTION_CLOCKWISE)->where('grid_number', $gridNumber)->find();
|
||||
$existingQuery = DiceReward::where('direction', DiceReward::DIRECTION_CLOCKWISE)->where('grid_number', $gridNumber);
|
||||
if ($deptId === null) {
|
||||
$existingQuery->whereNull('dept_id');
|
||||
} else {
|
||||
$existingQuery->where('dept_id', $deptId);
|
||||
}
|
||||
$existing = $existingQuery->find();
|
||||
if ($existing) {
|
||||
DiceReward::where('id', $existing->id)->update($payloadCw);
|
||||
$updatedCw++;
|
||||
@@ -395,6 +457,9 @@ class DiceRewardLogic
|
||||
$m->real_ev = $configCw['real_ev'] ?? null;
|
||||
$m->remark = $configCw['remark'] ?? '';
|
||||
$m->type = isset($configCw['type']) ? (int) $configCw['type'] : 0;
|
||||
if ($deptId !== null) {
|
||||
$m->dept_id = $deptId;
|
||||
}
|
||||
$m->save();
|
||||
$createdCw++;
|
||||
}
|
||||
@@ -419,7 +484,13 @@ class DiceRewardLogic
|
||||
'remark' => $configCcw['remark'] ?? '',
|
||||
'type' => isset($configCcw['type']) ? (int) $configCcw['type'] : 0,
|
||||
];
|
||||
$existing = DiceReward::where('direction', DiceReward::DIRECTION_COUNTERCLOCKWISE)->where('grid_number', $gridNumber)->find();
|
||||
$existingQuery = DiceReward::where('direction', DiceReward::DIRECTION_COUNTERCLOCKWISE)->where('grid_number', $gridNumber);
|
||||
if ($deptId === null) {
|
||||
$existingQuery->whereNull('dept_id');
|
||||
} else {
|
||||
$existingQuery->where('dept_id', $deptId);
|
||||
}
|
||||
$existing = $existingQuery->find();
|
||||
if ($existing) {
|
||||
DiceReward::where('id', $existing->id)->update($payloadCcw);
|
||||
$updatedCcw++;
|
||||
@@ -435,6 +506,9 @@ class DiceRewardLogic
|
||||
$m->real_ev = $configCcw['real_ev'] ?? null;
|
||||
$m->remark = $configCcw['remark'] ?? '';
|
||||
$m->type = isset($configCcw['type']) ? (int) $configCcw['type'] : 0;
|
||||
if ($deptId !== null) {
|
||||
$m->dept_id = $deptId;
|
||||
}
|
||||
$m->save();
|
||||
$createdCcw++;
|
||||
}
|
||||
@@ -442,7 +516,7 @@ class DiceRewardLogic
|
||||
}
|
||||
}
|
||||
|
||||
DiceReward::refreshCache();
|
||||
DiceReward::refreshCache($deptId ?? AdminScopeHelper::DEFAULT_TEMPLATE_DEPT);
|
||||
return [
|
||||
'created_clockwise' => $createdCw,
|
||||
'created_counterclockwise' => $createdCcw,
|
||||
|
||||
@@ -6,11 +6,13 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\logic\reward_config;
|
||||
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use app\dice\helper\ConfigScopeEditHelper;
|
||||
use app\dice\logic\reward\DiceRewardLogic;
|
||||
use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig;
|
||||
use app\dice\model\reward\DiceRewardConfig;
|
||||
use app\dice\model\reward_config_record\DiceRewardConfigRecord;
|
||||
use plugin\saiadmin\basic\think\BaseLogic;
|
||||
use app\dice\basic\DiceBaseLogic;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
use plugin\saiadmin\utils\Helper;
|
||||
use support\Log;
|
||||
@@ -19,7 +21,7 @@ use support\Log;
|
||||
* 奖励配置逻辑层(DiceRewardConfig)
|
||||
* weight 1-10000,各档位权重和不限制
|
||||
*/
|
||||
class DiceRewardConfigLogic extends BaseLogic
|
||||
class DiceRewardConfigLogic extends DiceBaseLogic
|
||||
{
|
||||
/** weight 取值范围 */
|
||||
private const WEIGHT_MIN = 1;
|
||||
@@ -36,18 +38,23 @@ class DiceRewardConfigLogic extends BaseLogic
|
||||
public function add(array $data): mixed
|
||||
{
|
||||
$result = parent::add($data);
|
||||
DiceRewardConfig::refreshCache();
|
||||
$deptId = AdminScopeHelper::normalizeRecordDeptId($data['dept_id'] ?? null);
|
||||
DiceRewardConfig::refreshCache($deptId);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改:保存后刷新缓存;BIGWIN 的 weight 直接写入 dice_reward_config 表,抽奖时从 Config 读取
|
||||
* 修改:按业务 id + 渠道更新;保存后刷新该渠道缓存
|
||||
*/
|
||||
public function edit($id, array $data): mixed
|
||||
public function edit($id, array $data, ?array $adminInfo = null, $requestDeptId = null): mixed
|
||||
{
|
||||
$result = parent::edit($id, $data);
|
||||
DiceRewardConfig::refreshCache();
|
||||
return $result;
|
||||
$deptId = AdminScopeHelper::resolveConfigDeptId(
|
||||
$adminInfo,
|
||||
AdminScopeHelper::pickRequestDeptId($requestDeptId, $data)
|
||||
);
|
||||
ConfigScopeEditHelper::updateByBusinessIdAndDept($this->model, (int) $id, $deptId, $data);
|
||||
DiceRewardConfig::refreshCache($deptId);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -152,8 +159,9 @@ class DiceRewardConfigLogic extends BaseLogic
|
||||
/**
|
||||
* 批量更新奖励索引配置:grid_number、ui_text、real_ev、tier、remark(不含 weight,BIGWIN 权重单独接口)
|
||||
* @param array $items 每项 [id, grid_number?, ui_text?, real_ev?, tier?, remark?]
|
||||
* @param int $deptId 渠道 ID(0=默认模板)
|
||||
*/
|
||||
public function batchUpdate(array $items): void
|
||||
public function batchUpdate(array $items, int $deptId = AdminScopeHelper::DEFAULT_TEMPLATE_DEPT): void
|
||||
{
|
||||
foreach ($items as $row) {
|
||||
if (! array_key_exists('id', $row) || $row['id'] === null || $row['id'] === '') {
|
||||
@@ -167,10 +175,18 @@ class DiceRewardConfigLogic extends BaseLogic
|
||||
}
|
||||
}
|
||||
if (! empty($data)) {
|
||||
parent::edit($id, $data);
|
||||
$this->updateByBusinessIdAndDept($id, $deptId, $data);
|
||||
}
|
||||
}
|
||||
DiceRewardConfig::refreshCache();
|
||||
DiceRewardConfig::refreshCache($deptId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按业务 id(0~25)与渠道更新单条配置
|
||||
*/
|
||||
private function updateByBusinessIdAndDept(int $businessId, int $deptId, array $data): void
|
||||
{
|
||||
ConfigScopeEditHelper::updateByBusinessIdAndDept($this->model, $businessId, $deptId, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -201,8 +217,9 @@ class DiceRewardConfigLogic extends BaseLogic
|
||||
/**
|
||||
* 批量更新 BIGWIN 档位权重(仅写 dice_reward_config 表,不操作 dice_reward)
|
||||
* @param array $items 每项 [grid_number => 5-30, weight => 0-10000]
|
||||
* @param int $deptId 渠道 ID(0=默认模板)
|
||||
*/
|
||||
public function batchUpdateBigwinWeight(array $items): void
|
||||
public function batchUpdateBigwinWeight(array $items, int $deptId = AdminScopeHelper::DEFAULT_TEMPLATE_DEPT): void
|
||||
{
|
||||
$weightMin = 0;
|
||||
$weightMax = 10000;
|
||||
@@ -213,21 +230,33 @@ class DiceRewardConfigLogic extends BaseLogic
|
||||
continue;
|
||||
}
|
||||
$weight = max($weightMin, min($weightMax, $weight));
|
||||
$this->model->where('tier', 'BIGWIN')
|
||||
->where('grid_number', $gridNumber)
|
||||
->update(['weight' => $weight]);
|
||||
$query = $this->model->where('tier', 'BIGWIN')->where('grid_number', $gridNumber);
|
||||
if (AdminScopeHelper::isTemplateDeptId($deptId)) {
|
||||
$query->where(function ($q) {
|
||||
$q->where('dept_id', AdminScopeHelper::DEFAULT_TEMPLATE_DEPT)
|
||||
->whereOr('dept_id', null);
|
||||
});
|
||||
} else {
|
||||
$query->where('dept_id', $deptId);
|
||||
}
|
||||
$exists = (clone $query)->find();
|
||||
if ($exists === null) {
|
||||
throw new ApiException('BIGWIN grid_number=' . $gridNumber . ' not found for current channel');
|
||||
}
|
||||
$query->update(['weight' => $weight]);
|
||||
}
|
||||
DiceRewardConfig::refreshCache();
|
||||
DiceRewardConfig::refreshCache($deptId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除后刷新缓存
|
||||
*/
|
||||
public function destroy($ids): bool
|
||||
public function destroy($ids, ?array $adminInfo = null, $requestDeptId = null): bool
|
||||
{
|
||||
$deptId = AdminScopeHelper::resolveConfigDeptId($adminInfo, $requestDeptId);
|
||||
$result = parent::destroy($ids);
|
||||
if ($result) {
|
||||
DiceRewardConfig::refreshCache();
|
||||
DiceRewardConfig::refreshCache($deptId);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
@@ -403,6 +432,12 @@ class DiceRewardConfigLogic extends BaseLogic
|
||||
$record->lottery_config_id = $config ? (int) $config->id : null;
|
||||
$record->result_counts = $counts;
|
||||
$record->admin_id = $adminId;
|
||||
if ($adminId > 0) {
|
||||
$admin = \plugin\saiadmin\app\model\system\SystemUser::find($adminId);
|
||||
if ($admin && !empty($admin->dept_id)) {
|
||||
$record->dept_id = $admin->dept_id;
|
||||
}
|
||||
}
|
||||
$record->create_time = date('Y-m-d H:i:s');
|
||||
$record->save();
|
||||
$recordId = (int) $record->id;
|
||||
|
||||
@@ -4,12 +4,15 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\logic\reward_config_record;
|
||||
|
||||
use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig;
|
||||
use app\api\util\ApiLang;
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use app\dice\helper\ConfigScopeEditHelper;
|
||||
use app\dice\model\ante_config\DiceAnteConfig;
|
||||
use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig;
|
||||
use app\dice\model\reward\DiceReward;
|
||||
use app\dice\model\reward\DiceRewardConfig;
|
||||
use app\dice\model\reward_config_record\DiceRewardConfigRecord;
|
||||
use plugin\saiadmin\basic\think\BaseLogic;
|
||||
use app\dice\basic\DiceBaseLogic;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
use plugin\saiadmin\app\model\system\SystemUser;
|
||||
|
||||
@@ -17,7 +20,7 @@ use plugin\saiadmin\app\model\system\SystemUser;
|
||||
* 奖励配置权重测试记录逻辑层
|
||||
*
|
||||
*/
|
||||
class DiceRewardConfigRecordLogic extends BaseLogic
|
||||
class DiceRewardConfigRecordLogic extends DiceBaseLogic
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
@@ -88,6 +91,7 @@ class DiceRewardConfigRecordLogic extends BaseLogic
|
||||
throw new ApiException('Test record not found');
|
||||
}
|
||||
$record = is_array($record) ? $record : $record->toArray();
|
||||
$configDeptId = AdminScopeHelper::normalizeRecordDeptId($record['dept_id'] ?? null);
|
||||
|
||||
$snapshot = $record['weight_config_snapshot'] ?? null;
|
||||
if (is_string($snapshot)) {
|
||||
@@ -112,9 +116,9 @@ class DiceRewardConfigRecordLogic extends BaseLogic
|
||||
$tier = $tierFromDb !== null ? (string) $tierFromDb : '';
|
||||
}
|
||||
// 仅按方向 + 点数更新 DiceReward(若存在则更新,不存在才插入,避免唯一键冲突)
|
||||
$reward = DiceReward::where('direction', $direction)
|
||||
->where('grid_number', $gridNumber)
|
||||
->find();
|
||||
$rewardQuery = DiceReward::where('direction', $direction)->where('grid_number', $gridNumber);
|
||||
ConfigScopeEditHelper::applyDeptIdWhere($rewardQuery, $configDeptId);
|
||||
$reward = $rewardQuery->find();
|
||||
if ($reward) {
|
||||
$reward->weight = $weight;
|
||||
// 若快照中有 tier,补齐 tier 信息
|
||||
@@ -130,10 +134,13 @@ class DiceRewardConfigRecordLogic extends BaseLogic
|
||||
$m->direction = $direction;
|
||||
$m->grid_number = $gridNumber;
|
||||
$m->weight = $weight;
|
||||
if (!AdminScopeHelper::isTemplateDeptId($configDeptId)) {
|
||||
$m->dept_id = $configDeptId;
|
||||
}
|
||||
$m->save();
|
||||
}
|
||||
}
|
||||
DiceReward::refreshCache();
|
||||
DiceReward::refreshCache($configDeptId);
|
||||
}
|
||||
|
||||
// 使用记录中的 bigwin_weight JSON 将 BIGWIN 概率导入到 DiceRewardConfig
|
||||
@@ -152,11 +159,11 @@ class DiceRewardConfigRecordLogic extends BaseLogic
|
||||
if ($weight < 0) {
|
||||
$weight = 0;
|
||||
}
|
||||
DiceRewardConfig::where('tier', 'BIGWIN')
|
||||
->where('grid_number', $gridNumber)
|
||||
->update(['weight' => $weight]);
|
||||
$bigwinQuery = DiceRewardConfig::where('tier', 'BIGWIN')->where('grid_number', $gridNumber);
|
||||
ConfigScopeEditHelper::applyDeptIdWhere($bigwinQuery, $configDeptId);
|
||||
$bigwinQuery->update(['weight' => $weight]);
|
||||
}
|
||||
DiceRewardConfig::refreshCache();
|
||||
DiceRewardConfig::refreshCache($configDeptId);
|
||||
}
|
||||
|
||||
$tiers = ['T1', 'T2', 'T3', 'T4', 'T5'];
|
||||
@@ -225,7 +232,8 @@ class DiceRewardConfigRecordLogic extends BaseLogic
|
||||
DiceLotteryPoolConfig::where('id', $freeTargetId)->update($update);
|
||||
}
|
||||
|
||||
DiceRewardConfig::refreshCache();
|
||||
DiceRewardConfig::refreshCache($configDeptId);
|
||||
DiceReward::refreshCache($configDeptId);
|
||||
DiceRewardConfig::clearRequestInstance();
|
||||
}
|
||||
|
||||
@@ -237,8 +245,12 @@ class DiceRewardConfigRecordLogic extends BaseLogic
|
||||
* @return int 记录 ID
|
||||
* @throws ApiException
|
||||
*/
|
||||
public function createWeightTestRecord(array|int $params, mixed $adminIdOrFreeS = null, mixed $freeSOrFreeN = null, mixed $freeN = null): int
|
||||
{
|
||||
public function createWeightTestRecord(
|
||||
array|int $params,
|
||||
mixed $adminIdOrFreeS = null,
|
||||
?array $adminInfo = null,
|
||||
$requestDeptId = null
|
||||
): int {
|
||||
$adminId = null;
|
||||
if (!is_array($params)) {
|
||||
// 兼容旧版调用:createWeightTestRecord(paid_s_count, paid_n_count)
|
||||
@@ -249,15 +261,14 @@ class DiceRewardConfigRecordLogic extends BaseLogic
|
||||
} else {
|
||||
$adminId = $adminIdOrFreeS !== null && $adminIdOrFreeS !== '' ? (int) $adminIdOrFreeS : null;
|
||||
}
|
||||
|
||||
$deptId = $this->resolveWeightTestDeptId(
|
||||
$adminInfo,
|
||||
AdminScopeHelper::pickRequestDeptId($requestDeptId, is_array($params) ? $params : []),
|
||||
is_array($params) ? $params : []
|
||||
);
|
||||
$allowed = [100, 500, 1000, 5000];
|
||||
$ante = isset($params['ante']) ? intval($params['ante']) : 1;
|
||||
if ($ante <= 0) {
|
||||
throw new ApiException('ante must be greater than 0');
|
||||
}
|
||||
$anteExists = DiceAnteConfig::where('mult', $ante)->count();
|
||||
if ($anteExists <= 0) {
|
||||
throw new ApiException('ante not allowed: ' . $ante);
|
||||
}
|
||||
$ante = $this->resolveWeightTestAnte($params, $deptId);
|
||||
|
||||
$lotteryConfigId = isset($params['lottery_config_id']) ? (int) $params['lottery_config_id'] : 0;
|
||||
$paidConfigId = isset($params['paid_lottery_config_id']) ? (int) $params['paid_lottery_config_id'] : 0;
|
||||
@@ -296,8 +307,8 @@ class DiceRewardConfigRecordLogic extends BaseLogic
|
||||
$paidTierWeights = null;
|
||||
$freeTierWeights = null;
|
||||
|
||||
// 来自 DiceReward 的当前权重快照(按方向+点数),用于权重测试模拟
|
||||
$instance = DiceReward::getCachedInstance();
|
||||
// 来自当前渠道的 DiceReward 权重快照(按方向+点数),用于权重测试模拟
|
||||
$instance = DiceReward::getCachedInstance($deptId);
|
||||
$byTierDirection = $instance['by_tier_direction'] ?? [];
|
||||
foreach ($byTierDirection as $tier => $byDir) {
|
||||
foreach ($byDir as $dir => $rows) {
|
||||
@@ -316,7 +327,7 @@ class DiceRewardConfigRecordLogic extends BaseLogic
|
||||
// BIGWIN 概率快照从 DiceRewardConfig 读取(例如豹子号配置)
|
||||
// JSON 结构 {"grid_number": weight, ...}
|
||||
$bigwinWeights = [];
|
||||
$bigwinConfigs = DiceRewardConfig::getCachedByTier('BIGWIN');
|
||||
$bigwinConfigs = DiceRewardConfig::getCachedByTier('BIGWIN', $deptId);
|
||||
foreach ($bigwinConfigs as $cfg) {
|
||||
$grid = isset($cfg['grid_number']) ? (int) $cfg['grid_number'] : 0;
|
||||
if ($grid <= 0) {
|
||||
@@ -327,10 +338,7 @@ class DiceRewardConfigRecordLogic extends BaseLogic
|
||||
}
|
||||
|
||||
if ($paidConfigId > 0) {
|
||||
$config = DiceLotteryPoolConfig::find($paidConfigId);
|
||||
if (!$config) {
|
||||
throw new ApiException('Paid pool config not found');
|
||||
}
|
||||
$config = $this->findPoolConfigInDept($paidConfigId, $deptId, 'Paid pool config not found');
|
||||
$tierWeightsSnapshot['paid'] = [
|
||||
'T1' => (int) ($config->t1_weight ?? 0),
|
||||
'T2' => (int) ($config->t2_weight ?? 0),
|
||||
@@ -359,10 +367,7 @@ class DiceRewardConfigRecordLogic extends BaseLogic
|
||||
}
|
||||
|
||||
if ($freeConfigId > 0) {
|
||||
$config = DiceLotteryPoolConfig::find($freeConfigId);
|
||||
if (!$config) {
|
||||
throw new ApiException('Free pool config not found');
|
||||
}
|
||||
$config = $this->findPoolConfigInDept($freeConfigId, $deptId, 'Free pool config not found');
|
||||
$tierWeightsSnapshot['free'] = [
|
||||
'T1' => (int) ($config->t1_weight ?? 0),
|
||||
'T2' => (int) ($config->t2_weight ?? 0),
|
||||
@@ -428,9 +433,94 @@ class DiceRewardConfigRecordLogic extends BaseLogic
|
||||
$record->bigwin_weight = $bigwinWeights ?: null;
|
||||
$record->ante = $ante;
|
||||
$record->admin_id = $adminId;
|
||||
$record->dept_id = $deptId;
|
||||
$record->create_time = date('Y-m-d H:i:s');
|
||||
$record->save();
|
||||
|
||||
return (int) $record->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析一键测试所属渠道:请求 dept_id > 奖池配置 dept_id > 渠道管理员本渠道
|
||||
*/
|
||||
private function resolveWeightTestDeptId(?array $adminInfo, $requestDeptId, array $params): int
|
||||
{
|
||||
$deptId = AdminScopeHelper::resolveConfigDeptId($adminInfo, $requestDeptId);
|
||||
if (! AdminScopeHelper::isTemplateDeptId($deptId)) {
|
||||
return $deptId;
|
||||
}
|
||||
|
||||
foreach (['paid_lottery_config_id', 'free_lottery_config_id', 'lottery_config_id'] as $key) {
|
||||
$poolId = isset($params[$key]) ? (int) $params[$key] : 0;
|
||||
if ($poolId <= 0) {
|
||||
continue;
|
||||
}
|
||||
$pool = DiceLotteryPoolConfig::find($poolId);
|
||||
if (! $pool) {
|
||||
continue;
|
||||
}
|
||||
$poolDeptId = AdminScopeHelper::normalizeRecordDeptId($pool->dept_id ?? null);
|
||||
if (! AdminScopeHelper::isTemplateDeptId($poolDeptId)) {
|
||||
return $poolDeptId;
|
||||
}
|
||||
}
|
||||
|
||||
$scopeDeptId = AdminScopeHelper::getDeptId($adminInfo);
|
||||
if ($scopeDeptId !== null && $scopeDeptId > 0) {
|
||||
return $scopeDeptId;
|
||||
}
|
||||
|
||||
return $deptId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验奖池配置属于当前渠道
|
||||
*/
|
||||
private function findPoolConfigInDept(int $poolId, int $deptId, string $notFoundMsg): DiceLotteryPoolConfig
|
||||
{
|
||||
$config = DiceLotteryPoolConfig::find($poolId);
|
||||
if (!$config) {
|
||||
throw new ApiException($notFoundMsg);
|
||||
}
|
||||
$poolDeptId = AdminScopeHelper::normalizeRecordDeptId($config->dept_id ?? null);
|
||||
if ($poolDeptId !== $deptId) {
|
||||
throw new ApiException('POOL_CONFIG_NOT_IN_CHANNEL');
|
||||
}
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析一键测试底注:优先 ante_config_id,否则按 mult + 渠道校验
|
||||
*/
|
||||
private function resolveWeightTestAnte(array $params, int $deptId): int
|
||||
{
|
||||
$anteConfigId = isset($params['ante_config_id']) ? (int) $params['ante_config_id'] : 0;
|
||||
if ($anteConfigId > 0) {
|
||||
$config = DiceAnteConfig::find($anteConfigId);
|
||||
if (! $config) {
|
||||
throw new ApiException('ANTE_CONFIG_NOT_FOUND');
|
||||
}
|
||||
$configDeptId = AdminScopeHelper::normalizeRecordDeptId($config->dept_id ?? null);
|
||||
if ($configDeptId !== $deptId) {
|
||||
throw new ApiException('ANTE_CONFIG_NOT_IN_CHANNEL');
|
||||
}
|
||||
$mult = (int) ($config->mult ?? 0);
|
||||
if ($mult <= 0) {
|
||||
throw new ApiException('ANTE_MUST_POSITIVE');
|
||||
}
|
||||
return $mult;
|
||||
}
|
||||
|
||||
$ante = isset($params['ante']) ? (int) $params['ante'] : 0;
|
||||
if ($ante <= 0) {
|
||||
throw new ApiException('ANTE_MUST_POSITIVE');
|
||||
}
|
||||
$anteQuery = DiceAnteConfig::where('mult', $ante);
|
||||
ConfigScopeEditHelper::applyDeptIdWhere($anteQuery, $deptId);
|
||||
if ($anteQuery->count() <= 0) {
|
||||
throw new ApiException(ApiLang::translateParams('ANTE_NOT_ALLOWED', [$ante]));
|
||||
}
|
||||
|
||||
return $ante;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
namespace app\dice\logic\reward_config_record;
|
||||
|
||||
use app\api\logic\PlayStartLogic;
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig;
|
||||
use app\dice\model\play_record_test\DicePlayRecordTest;
|
||||
use app\dice\model\reward_config_record\DiceRewardConfigRecord;
|
||||
@@ -19,9 +20,13 @@ use support\think\Db;
|
||||
class WeightTestRunner
|
||||
{
|
||||
private const BATCH_SIZE = 10;
|
||||
|
||||
/** 本次测试所属渠道(与 dice_reward_config_record.dept_id 一致) */
|
||||
private int $runDeptId = 0;
|
||||
/** 测试记录写库白名单字段 */
|
||||
private const PLAY_RECORD_TEST_COLUMNS = [
|
||||
'reward_config_record_id',
|
||||
'dept_id',
|
||||
'admin_id',
|
||||
'lottery_config_id',
|
||||
'lottery_type',
|
||||
@@ -61,10 +66,15 @@ class WeightTestRunner
|
||||
return;
|
||||
}
|
||||
|
||||
$configType0 = DiceLotteryPoolConfig::where('name', 'default')->find();
|
||||
$configType1 = DiceLotteryPoolConfig::where('name', 'killScore')->find();
|
||||
$this->runDeptId = $this->resolveRunDeptId($recordId, $record);
|
||||
$deptId = $this->runDeptId;
|
||||
DiceReward::setRequestDeptId($deptId);
|
||||
DiceRewardConfig::clearRequestInstance();
|
||||
|
||||
$configType0 = DiceLotteryPoolConfig::findByNameForDept('default', $deptId);
|
||||
$configType1 = DiceLotteryPoolConfig::findByNameForDept('killScore', $deptId);
|
||||
if (!$configType0) {
|
||||
$this->markFailed($recordId, '彩金池配置 name=default 不存在');
|
||||
$this->markFailed($recordId, '彩金池配置 name=default 不存在(当前渠道)');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -79,12 +89,12 @@ class WeightTestRunner
|
||||
$freePoolConfigId = (int) ($record->free_lottery_config_id ?? 0);
|
||||
|
||||
$paidPoolConfig = $paidPoolConfigId > 0 ? DiceLotteryPoolConfig::find($paidPoolConfigId) : $configType0;
|
||||
if (!$paidPoolConfig) {
|
||||
if (!$paidPoolConfig || AdminScopeHelper::normalizeRecordDeptId($paidPoolConfig->dept_id ?? null) !== $deptId) {
|
||||
$paidPoolConfig = $configType0;
|
||||
}
|
||||
$freePoolConfig = $freePoolConfigId > 0 ? DiceLotteryPoolConfig::find($freePoolConfigId) : $configType1;
|
||||
if (!$freePoolConfig) {
|
||||
$freePoolConfig = $configType0;
|
||||
if (!$freePoolConfig || AdminScopeHelper::normalizeRecordDeptId($freePoolConfig->dept_id ?? null) !== $deptId) {
|
||||
$freePoolConfig = $configType1 ?: $configType0;
|
||||
}
|
||||
|
||||
if ($paidTierWeightsCustom !== null && array_sum($paidTierWeightsCustom) <= 0) {
|
||||
@@ -118,6 +128,7 @@ class WeightTestRunner
|
||||
try {
|
||||
$this->runChainFreeMode(
|
||||
$recordId,
|
||||
$deptId,
|
||||
$playLogic,
|
||||
$paidS,
|
||||
$paidN,
|
||||
@@ -145,6 +156,9 @@ class WeightTestRunner
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('WeightTestRunner exception: ' . $e->getMessage(), ['record_id' => $recordId, 'trace' => $e->getTraceAsString()]);
|
||||
$this->markFailed($recordId, $e->getMessage());
|
||||
} finally {
|
||||
DiceReward::clearRequestInstance();
|
||||
DiceRewardConfig::clearRequestInstance();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,6 +167,7 @@ class WeightTestRunner
|
||||
*/
|
||||
private function runChainFreeMode(
|
||||
int $recordId,
|
||||
int $deptId,
|
||||
PlayStartLogic $playLogic,
|
||||
int $paidS,
|
||||
int $paidN,
|
||||
@@ -199,12 +214,12 @@ class WeightTestRunner
|
||||
$customWeights = $freeTierWeightsCustom;
|
||||
}
|
||||
|
||||
$row = $playLogic->simulateOnePlay($cfg, $dir, $lotteryType, $playAnte, $customWeights);
|
||||
$row = $playLogic->simulateOnePlay($cfg, $dir, $lotteryType, $playAnte, $customWeights, $deptId);
|
||||
$winCoin = (float) ($row['win_coin'] ?? 0);
|
||||
$paidAmount = (float) ($row['paid_amount'] ?? 0);
|
||||
$playerProfitTotal += $winCoin - $paidAmount;
|
||||
$this->aggregate($row, $resultCounts, $tierCounts);
|
||||
$buffer[] = $this->rowForInsert($row, $recordId);
|
||||
$buffer[] = $this->rowForInsert($row, $recordId, $deptId);
|
||||
$done++;
|
||||
|
||||
if (!empty($row['grants_free_ticket'])) {
|
||||
@@ -217,6 +232,58 @@ class WeightTestRunner
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析本次测试渠道:优先读库字段,避免 ORM 字段缓存未含 dept_id 时读不到
|
||||
*/
|
||||
private function resolveRunDeptId(int $recordId, DiceRewardConfigRecord $record): int
|
||||
{
|
||||
$recordTable = (new DiceRewardConfigRecord())->getTable();
|
||||
$fromDb = Db::table($recordTable)->where('id', $recordId)->value('dept_id');
|
||||
$deptId = AdminScopeHelper::normalizeRecordDeptId($fromDb);
|
||||
if (! AdminScopeHelper::isTemplateDeptId($deptId)) {
|
||||
return $deptId;
|
||||
}
|
||||
|
||||
$deptId = AdminScopeHelper::normalizeRecordDeptId($record->dept_id ?? null);
|
||||
if (! AdminScopeHelper::isTemplateDeptId($deptId)) {
|
||||
return $deptId;
|
||||
}
|
||||
|
||||
return $this->resolveDeptIdFromRecordPools($record, $deptId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 历史记录 dept_id=0 时,从关联奖池配置反推并回写
|
||||
*/
|
||||
private function resolveDeptIdFromRecordPools(DiceRewardConfigRecord $record, int $fallbackDeptId): int
|
||||
{
|
||||
foreach (
|
||||
[
|
||||
(int) ($record->paid_lottery_config_id ?? 0),
|
||||
(int) ($record->free_lottery_config_id ?? 0),
|
||||
(int) ($record->lottery_config_id ?? 0),
|
||||
] as $poolId
|
||||
) {
|
||||
if ($poolId <= 0) {
|
||||
continue;
|
||||
}
|
||||
$pool = DiceLotteryPoolConfig::find($poolId);
|
||||
if (! $pool) {
|
||||
continue;
|
||||
}
|
||||
$poolDeptId = AdminScopeHelper::normalizeRecordDeptId($pool->dept_id ?? null);
|
||||
if (! AdminScopeHelper::isTemplateDeptId($poolDeptId)) {
|
||||
if (AdminScopeHelper::normalizeRecordDeptId($record->dept_id ?? null) !== $poolDeptId) {
|
||||
$record->dept_id = $poolDeptId;
|
||||
$record->save();
|
||||
}
|
||||
return $poolDeptId;
|
||||
}
|
||||
}
|
||||
|
||||
return $fallbackDeptId;
|
||||
}
|
||||
|
||||
private function aggregate(array $row, array &$resultCounts, array &$tierCounts): void
|
||||
{
|
||||
$grid = (int) ($row['roll_number_for_count'] ?? $row['roll_number'] ?? 0);
|
||||
@@ -229,10 +296,14 @@ class WeightTestRunner
|
||||
}
|
||||
}
|
||||
|
||||
private function rowForInsert(array $row, int $rewardConfigRecordId): array
|
||||
private function rowForInsert(array $row, int $rewardConfigRecordId, int $deptId): array
|
||||
{
|
||||
$bindDeptId = ! AdminScopeHelper::isTemplateDeptId($this->runDeptId)
|
||||
? $this->runDeptId
|
||||
: $deptId;
|
||||
$out = [
|
||||
'reward_config_record_id' => $rewardConfigRecordId,
|
||||
'dept_id' => $bindDeptId,
|
||||
];
|
||||
$keys = [
|
||||
'admin_id', 'lottery_config_id', 'lottery_type', 'is_win', 'win_coin',
|
||||
@@ -254,7 +325,7 @@ class WeightTestRunner
|
||||
return;
|
||||
}
|
||||
$this->insertBuffer($buffer);
|
||||
$buffer = [];
|
||||
array_splice($buffer, 0, count($buffer));
|
||||
$this->updateProgress($recordId, $done, $resultCounts, $tierCounts, $recordTotalPlayCount);
|
||||
}
|
||||
|
||||
@@ -263,6 +334,7 @@ class WeightTestRunner
|
||||
if (empty($rows)) {
|
||||
return;
|
||||
}
|
||||
$table = (new DicePlayRecordTest())->getTable();
|
||||
foreach ($rows as $row) {
|
||||
if (!is_array($row)) {
|
||||
continue;
|
||||
@@ -273,6 +345,9 @@ class WeightTestRunner
|
||||
$payload[$column] = $row[$column];
|
||||
}
|
||||
}
|
||||
if (! AdminScopeHelper::isTemplateDeptId($this->runDeptId)) {
|
||||
$payload['dept_id'] = $this->runDeptId;
|
||||
}
|
||||
if (!array_key_exists('create_time', $payload) || $payload['create_time'] === null || $payload['create_time'] === '') {
|
||||
$payload['create_time'] = date('Y-m-d H:i:s');
|
||||
}
|
||||
@@ -282,10 +357,26 @@ class WeightTestRunner
|
||||
if ($payload === []) {
|
||||
continue;
|
||||
}
|
||||
Db::name((new DicePlayRecordTest())->getTable())->insert($payload);
|
||||
// strict(false):表结构新增 dept_id 后,避免连接层字段缓存未刷新导致插入被丢弃
|
||||
Db::table($table)->strict(false)->insert($payload);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将本批测试明细 dept_id 与主记录对齐(修复历史 worker 未写入 dept_id 的情况)
|
||||
*/
|
||||
private function syncPlayRecordTestDeptId(int $recordId, int $deptId): void
|
||||
{
|
||||
if (AdminScopeHelper::isTemplateDeptId($deptId)) {
|
||||
return;
|
||||
}
|
||||
DicePlayRecordTest::where('reward_config_record_id', $recordId)
|
||||
->where(function ($query) {
|
||||
$query->whereNull('dept_id')->whereOr('dept_id', AdminScopeHelper::DEFAULT_TEMPLATE_DEPT);
|
||||
})
|
||||
->update(['dept_id' => $deptId]);
|
||||
}
|
||||
|
||||
private function updateProgress(int $recordId, int $overPlayCount, array $resultCounts, array $tierCounts, ?int $totalPlayCount = null): void
|
||||
{
|
||||
$record = DiceRewardConfigRecord::find($recordId);
|
||||
@@ -308,6 +399,13 @@ class WeightTestRunner
|
||||
{
|
||||
$record = DiceRewardConfigRecord::find($recordId);
|
||||
if ($record) {
|
||||
$deptId = AdminScopeHelper::normalizeRecordDeptId($record->dept_id ?? null);
|
||||
if (AdminScopeHelper::isTemplateDeptId($deptId) && ! AdminScopeHelper::isTemplateDeptId($this->runDeptId)) {
|
||||
$deptId = $this->runDeptId;
|
||||
$record->dept_id = $deptId;
|
||||
}
|
||||
$this->syncPlayRecordTestDeptId($recordId, $deptId);
|
||||
|
||||
// 平台盈利通过关联测试记录统计
|
||||
$platformProfit = DiceRewardConfigRecord::computePlatformProfitFromRelated($recordId);
|
||||
// 落点统计也通过关联测试记录重新统计,避免模拟过程异常导致为空
|
||||
|
||||
25
server/app/dice/model/DiceModel.php
Normal file
25
server/app/dice/model/DiceModel.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\dice\model;
|
||||
|
||||
use plugin\saiadmin\basic\think\BaseModel as SaiBaseModel;
|
||||
|
||||
/**
|
||||
* 大富翁模块模型基类:删除均为硬删除(物理删除)
|
||||
*/
|
||||
abstract class DiceModel extends SaiBaseModel
|
||||
{
|
||||
/**
|
||||
* @param mixed $data
|
||||
*/
|
||||
public static function destroy($data, bool $force = true): bool
|
||||
{
|
||||
return parent::destroy($data, true);
|
||||
}
|
||||
|
||||
public function delete(): bool
|
||||
{
|
||||
return $this->force()->delete();
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\model\ante_config;
|
||||
|
||||
use plugin\saiadmin\basic\think\BaseModel;
|
||||
use app\dice\model\DiceModel;
|
||||
|
||||
/**
|
||||
* 底注配置模型
|
||||
@@ -19,7 +19,7 @@ use plugin\saiadmin\basic\think\BaseModel;
|
||||
* @property string $create_time 创建时间
|
||||
* @property string $update_time 更新时间
|
||||
*/
|
||||
class DiceAnteConfig extends BaseModel
|
||||
class DiceAnteConfig extends DiceModel
|
||||
{
|
||||
protected $pk = 'id';
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\model\config;
|
||||
|
||||
use plugin\saiadmin\basic\eloquent\BaseModel;
|
||||
use app\dice\model\DiceModel;
|
||||
|
||||
/**
|
||||
* 摇色子配置模型
|
||||
@@ -23,7 +23,7 @@ use plugin\saiadmin\basic\eloquent\BaseModel;
|
||||
* @property $create_time 创建时间
|
||||
* @property $update_time 修改时间
|
||||
*/
|
||||
class DiceConfig extends BaseModel
|
||||
class DiceConfig extends DiceModel
|
||||
{
|
||||
/**
|
||||
* 数据表主键
|
||||
|
||||
@@ -4,14 +4,14 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\model\game;
|
||||
|
||||
use plugin\saiadmin\basic\eloquent\BaseModel;
|
||||
use app\dice\model\DiceModel;
|
||||
|
||||
/**
|
||||
* 游戏管理模型
|
||||
*
|
||||
* dice_game 游戏配置表
|
||||
*/
|
||||
class DiceGame extends BaseModel
|
||||
class DiceGame extends DiceModel
|
||||
{
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
|
||||
@@ -6,7 +6,9 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\model\lottery_pool_config;
|
||||
|
||||
use plugin\saiadmin\basic\think\BaseModel;
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use app\dice\helper\ConfigScopeEditHelper;
|
||||
use app\dice\model\DiceModel;
|
||||
|
||||
/**
|
||||
* 色子奖池配置模型
|
||||
@@ -27,7 +29,7 @@ use plugin\saiadmin\basic\think\BaseModel;
|
||||
* @property $t5_weight T5池权重
|
||||
* @property $profit_amount 池子累计盈利(每局付费按 win_coin-paid_amount,免费按 win_coin 累加;仅展示不可编辑)
|
||||
*/
|
||||
class DiceLotteryPoolConfig extends BaseModel
|
||||
class DiceLotteryPoolConfig extends DiceModel
|
||||
{
|
||||
/**
|
||||
* 数据表主键
|
||||
@@ -41,6 +43,16 @@ class DiceLotteryPoolConfig extends BaseModel
|
||||
*/
|
||||
protected $table = 'dice_lottery_pool_config';
|
||||
|
||||
/**
|
||||
* 按名称与渠道查找奖池配置(一键测试等场景,避免命中其他渠道同名配置)
|
||||
*/
|
||||
public static function findByNameForDept(string $name, int $deptId): ?self
|
||||
{
|
||||
$query = (new self())->where('name', $name);
|
||||
ConfigScopeEditHelper::applyDeptIdWhere($query, AdminScopeHelper::normalizeRecordDeptId($deptId));
|
||||
return $query->find();
|
||||
}
|
||||
|
||||
/**
|
||||
* 名称 搜索
|
||||
*/
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace app\dice\model\play_record;
|
||||
use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig;
|
||||
use app\dice\model\player\DicePlayer;
|
||||
use app\dice\model\reward\DiceRewardConfig;
|
||||
use plugin\saiadmin\basic\think\BaseModel;
|
||||
use app\dice\model\DiceModel;
|
||||
use think\model\relation\BelongsTo;
|
||||
|
||||
/**
|
||||
@@ -41,7 +41,7 @@ use think\model\relation\BelongsTo;
|
||||
* @property $create_time 创建时间
|
||||
* @property $update_time 修改时间
|
||||
*/
|
||||
class DicePlayRecord extends BaseModel
|
||||
class DicePlayRecord extends DiceModel
|
||||
{
|
||||
/**
|
||||
* 数据表主键
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\model\play_record_test;
|
||||
|
||||
use plugin\saiadmin\basic\think\BaseModel;
|
||||
use app\dice\model\DiceModel;
|
||||
use app\dice\model\reward_config_record\DiceRewardConfigRecord;
|
||||
use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig;
|
||||
use think\model\relation\BelongsTo;
|
||||
@@ -37,7 +37,7 @@ use think\model\relation\BelongsTo;
|
||||
* @property $admin_id 所属管理员
|
||||
* @property int|null $reward_config_record_id 关联 DiceRewardConfigRecord.id(权重测试记录)
|
||||
*/
|
||||
class DicePlayRecordTest extends BaseModel
|
||||
class DicePlayRecordTest extends DiceModel
|
||||
{
|
||||
/**
|
||||
* 数据表主键
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\model\player;
|
||||
|
||||
use plugin\saiadmin\basic\think\BaseModel;
|
||||
use app\dice\model\DiceModel;
|
||||
use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig;
|
||||
|
||||
/**
|
||||
@@ -15,7 +15,8 @@ use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig;
|
||||
* dice_player 大富翁-玩家
|
||||
*
|
||||
* @property $id ID
|
||||
* @property $username 用户名
|
||||
* @property $dept_id 所属渠道ID
|
||||
* @property $username 用户名(同渠道内唯一)
|
||||
* @property $phone 手机
|
||||
* @property $uid uid
|
||||
* @property $name 昵称
|
||||
@@ -37,7 +38,7 @@ use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig;
|
||||
* @property $update_time 更新时间
|
||||
* @property $delete_time 删除时间
|
||||
*/
|
||||
class DicePlayer extends BaseModel
|
||||
class DicePlayer extends DiceModel
|
||||
{
|
||||
/**
|
||||
* 数据表主键
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
namespace app\dice\model\player_ticket_record;
|
||||
|
||||
use app\dice\model\player\DicePlayer;
|
||||
use plugin\saiadmin\basic\think\BaseModel;
|
||||
use app\dice\model\DiceModel;
|
||||
use think\model\relation\BelongsTo;
|
||||
|
||||
/**
|
||||
@@ -27,7 +27,7 @@ use think\model\relation\BelongsTo;
|
||||
* @property $create_time 创建时间
|
||||
* @property $update_time 修改时间
|
||||
*/
|
||||
class DicePlayerTicketRecord extends BaseModel
|
||||
class DicePlayerTicketRecord extends DiceModel
|
||||
{
|
||||
/**
|
||||
* 数据表主键
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
namespace app\dice\model\player_wallet_record;
|
||||
|
||||
use app\dice\model\player\DicePlayer;
|
||||
use plugin\saiadmin\basic\think\BaseModel;
|
||||
use app\dice\model\DiceModel;
|
||||
use plugin\saiadmin\app\model\system\SystemUser;
|
||||
use think\model\relation\BelongsTo;
|
||||
|
||||
@@ -31,7 +31,7 @@ use think\model\relation\BelongsTo;
|
||||
* @property $create_time 创建时间
|
||||
* @property $update_time 修改时间
|
||||
*/
|
||||
class DicePlayerWalletRecord extends BaseModel
|
||||
class DicePlayerWalletRecord extends DiceModel
|
||||
{
|
||||
/**
|
||||
* 数据表主键
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\model\reward;
|
||||
|
||||
use plugin\saiadmin\basic\think\BaseModel;
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use app\dice\helper\ConfigScopeEditHelper;
|
||||
use app\dice\model\DiceModel;
|
||||
use support\think\Cache;
|
||||
|
||||
/**
|
||||
@@ -25,42 +27,70 @@ use support\think\Cache;
|
||||
* @property $remark 备注(来自config)
|
||||
* @property $type 奖励类型(来自config)
|
||||
*/
|
||||
class DiceReward extends BaseModel
|
||||
class DiceReward extends DiceModel
|
||||
{
|
||||
/** 方向:顺时针 */
|
||||
public const DIRECTION_CLOCKWISE = 0;
|
||||
/** 方向:逆时针 */
|
||||
public const DIRECTION_COUNTERCLOCKWISE = 1;
|
||||
|
||||
/** 缓存键:奖励对照实例 */
|
||||
/** 缓存键前缀:奖励对照实例(按渠道分键) */
|
||||
private const CACHE_KEY_INSTANCE = 'dice:reward:instance';
|
||||
|
||||
private const CACHE_TTL = 86400 * 30;
|
||||
|
||||
private static ?array $instance = null;
|
||||
|
||||
private static ?int $requestDeptId = null;
|
||||
|
||||
protected $table = 'dice_reward';
|
||||
|
||||
/** 主键 id 自增,唯一约束 (direction, grid_number) */
|
||||
protected $pk = 'id';
|
||||
|
||||
private static function cacheKeyForDept(int $deptId): string
|
||||
{
|
||||
return self::CACHE_KEY_INSTANCE . ':' . $deptId;
|
||||
}
|
||||
|
||||
private static function resolveDeptId(?int $deptId): int
|
||||
{
|
||||
if ($deptId !== null) {
|
||||
return AdminScopeHelper::normalizeRecordDeptId($deptId);
|
||||
}
|
||||
if (self::$requestDeptId !== null) {
|
||||
return self::$requestDeptId;
|
||||
}
|
||||
return AdminScopeHelper::DEFAULT_TEMPLATE_DEPT;
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求级设置当前渠道(一键测试 worker 内调用 simulateOnePlay 前设置)
|
||||
*/
|
||||
public static function setRequestDeptId(?int $deptId): void
|
||||
{
|
||||
self::$requestDeptId = $deptId !== null
|
||||
? AdminScopeHelper::normalizeRecordDeptId($deptId)
|
||||
: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取奖励对照实例(按档位+方向索引,用于抽奖与权重配比)
|
||||
* 优先从共享缓存读取,保证多进程(如一键测试 worker)与数据库一致
|
||||
* @return array{list: array, by_tier_direction: array}
|
||||
*/
|
||||
public static function getCachedInstance(): array
|
||||
public static function getCachedInstance(?int $deptId = null): array
|
||||
{
|
||||
$instance = Cache::get(self::CACHE_KEY_INSTANCE);
|
||||
$deptId = self::resolveDeptId($deptId);
|
||||
$cacheKey = self::cacheKeyForDept($deptId);
|
||||
$instance = Cache::get($cacheKey);
|
||||
if ($instance !== null && is_array($instance)) {
|
||||
self::$instance = $instance;
|
||||
return $instance;
|
||||
}
|
||||
if (self::$instance !== null) {
|
||||
if (self::$instance !== null && self::$requestDeptId === $deptId) {
|
||||
return self::$instance;
|
||||
}
|
||||
self::refreshCache();
|
||||
$instance = Cache::get(self::CACHE_KEY_INSTANCE);
|
||||
self::refreshCache($deptId);
|
||||
$instance = Cache::get($cacheKey);
|
||||
self::$instance = is_array($instance) ? $instance : self::buildEmptyInstance();
|
||||
return self::$instance;
|
||||
}
|
||||
@@ -69,9 +99,9 @@ class DiceReward extends BaseModel
|
||||
* 按档位+方向取权重列表(用于抽奖:该档位该方向下 end_index => weight)
|
||||
* @return array<int, int> end_index => weight
|
||||
*/
|
||||
public static function getCachedByTierAndDirection(string $tier, int $direction): array
|
||||
public static function getCachedByTierAndDirection(string $tier, int $direction, ?int $deptId = null): array
|
||||
{
|
||||
$inst = self::getCachedInstance();
|
||||
$inst = self::getCachedInstance($deptId);
|
||||
$byTierDirection = $inst['by_tier_direction'] ?? [];
|
||||
$list = $byTierDirection[$tier][$direction] ?? [];
|
||||
$result = [];
|
||||
@@ -84,11 +114,14 @@ class DiceReward extends BaseModel
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新从数据库加载并写入缓存;修改/新增/删除后需调用以实例化
|
||||
* 按渠道从数据库加载并写入缓存
|
||||
*/
|
||||
public static function refreshCache(): void
|
||||
public static function refreshCache(?int $deptId = null): void
|
||||
{
|
||||
$list = (new self())->order('tier')->order('direction')->order('end_index')->select()->toArray();
|
||||
$deptId = self::resolveDeptId($deptId);
|
||||
$query = (new self())->order('tier')->order('direction')->order('end_index');
|
||||
ConfigScopeEditHelper::applyDeptIdWhere($query, $deptId);
|
||||
$list = $query->select()->toArray();
|
||||
$byTierDirection = [];
|
||||
foreach ($list as $row) {
|
||||
$tier = isset($row['tier']) ? (string) $row['tier'] : '';
|
||||
@@ -103,11 +136,12 @@ class DiceReward extends BaseModel
|
||||
$byTierDirection[$tier][$direction][] = $row;
|
||||
}
|
||||
}
|
||||
self::$instance = [
|
||||
$instance = [
|
||||
'list' => $list,
|
||||
'by_tier_direction' => $byTierDirection,
|
||||
];
|
||||
Cache::set(self::CACHE_KEY_INSTANCE, self::$instance, self::CACHE_TTL);
|
||||
self::$instance = $instance;
|
||||
Cache::set(self::cacheKeyForDept($deptId), $instance, self::CACHE_TTL);
|
||||
}
|
||||
|
||||
private static function buildEmptyInstance(): array
|
||||
@@ -121,20 +155,29 @@ class DiceReward extends BaseModel
|
||||
public static function clearRequestInstance(): void
|
||||
{
|
||||
self::$instance = null;
|
||||
self::$requestDeptId = null;
|
||||
}
|
||||
|
||||
private static function refreshCacheForModel($model): void
|
||||
{
|
||||
$deptId = AdminScopeHelper::normalizeRecordDeptId(
|
||||
is_array($model) ? ($model['dept_id'] ?? null) : ($model->dept_id ?? null)
|
||||
);
|
||||
self::refreshCache($deptId);
|
||||
}
|
||||
|
||||
public static function onAfterInsert($model): void
|
||||
{
|
||||
self::refreshCache();
|
||||
self::refreshCacheForModel($model);
|
||||
}
|
||||
|
||||
public static function onAfterUpdate($model): void
|
||||
{
|
||||
self::refreshCache();
|
||||
self::refreshCacheForModel($model);
|
||||
}
|
||||
|
||||
public static function onAfterDelete($model): void
|
||||
{
|
||||
self::refreshCache();
|
||||
self::refreshCacheForModel($model);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,10 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\model\reward_config;
|
||||
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use app\dice\helper\ConfigScopeEditHelper;
|
||||
use app\dice\model\reward\DiceReward;
|
||||
use plugin\saiadmin\basic\think\BaseModel;
|
||||
use app\dice\model\DiceModel;
|
||||
use support\think\Cache;
|
||||
|
||||
/**
|
||||
@@ -27,7 +29,7 @@ use support\think\Cache;
|
||||
* @property $create_time 创建时间
|
||||
* @property $update_time 修改时间
|
||||
*/
|
||||
class DiceRewardConfig extends BaseModel
|
||||
class DiceRewardConfig extends DiceModel
|
||||
{
|
||||
/** 缓存键:彩金池奖励列表实例 */
|
||||
private const CACHE_KEY_INSTANCE = 'dice:reward_config:instance';
|
||||
@@ -45,31 +47,35 @@ class DiceRewardConfig extends BaseModel
|
||||
* 优先从共享缓存读取,保证多进程(如一键测试 worker)能拿到最新配置,与数据库一致
|
||||
* @return array{list: array, by_tier: array, by_tier_grid: array, min_real_ev: float}
|
||||
*/
|
||||
public static function getCachedInstance(): array
|
||||
public static function getCachedInstance(?int $deptId = null): array
|
||||
{
|
||||
$instance = Cache::get(self::CACHE_KEY_INSTANCE);
|
||||
if ($deptId === null) {
|
||||
$deptId = AdminScopeHelper::DEFAULT_TEMPLATE_DEPT;
|
||||
}
|
||||
$cacheKey = self::cacheKeyForDept($deptId);
|
||||
$instance = Cache::get($cacheKey);
|
||||
if ($instance !== null && is_array($instance)) {
|
||||
self::$instance = $instance;
|
||||
return $instance;
|
||||
}
|
||||
if (self::$instance !== null) {
|
||||
return self::$instance;
|
||||
}
|
||||
self::refreshCache();
|
||||
$instance = Cache::get(self::CACHE_KEY_INSTANCE);
|
||||
self::$instance = is_array($instance) ? $instance : self::buildEmptyInstance();
|
||||
return self::$instance;
|
||||
self::refreshCache($deptId);
|
||||
$instance = Cache::get($cacheKey);
|
||||
return is_array($instance) ? $instance : self::buildEmptyInstance();
|
||||
}
|
||||
|
||||
public static function getCachedList(): array
|
||||
private static function cacheKeyForDept(int $deptId): string
|
||||
{
|
||||
$inst = self::getCachedInstance();
|
||||
return self::CACHE_KEY_INSTANCE . ':' . $deptId;
|
||||
}
|
||||
|
||||
public static function getCachedList(?int $deptId = null): array
|
||||
{
|
||||
$inst = self::getCachedInstance($deptId);
|
||||
return $inst['list'] ?? [];
|
||||
}
|
||||
|
||||
public static function getCachedById(int $id): ?array
|
||||
public static function getCachedById(int $id, ?int $deptId = null): ?array
|
||||
{
|
||||
$list = self::getCachedList();
|
||||
$list = self::getCachedList($deptId);
|
||||
foreach ($list as $row) {
|
||||
if (isset($row['id']) && (int) $row['id'] === $id) {
|
||||
return $row;
|
||||
@@ -79,11 +85,16 @@ class DiceRewardConfig extends BaseModel
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新从数据库加载并写入缓存(按档位+权重抽 grid_number,含 by_tier、by_tier_grid)
|
||||
* 按渠道从数据库加载并写入缓存(避免多渠道配置混在同一缓存键)
|
||||
*/
|
||||
public static function refreshCache(): void
|
||||
public static function refreshCache(?int $deptId = null): void
|
||||
{
|
||||
$list = (new self())->order('id', 'asc')->select()->toArray();
|
||||
if ($deptId === null) {
|
||||
$deptId = AdminScopeHelper::DEFAULT_TEMPLATE_DEPT;
|
||||
}
|
||||
$query = (new self())->order('id', 'asc');
|
||||
ConfigScopeEditHelper::applyDeptIdWhere($query, $deptId);
|
||||
$list = $query->select()->toArray();
|
||||
$byTier = [];
|
||||
$byTierGrid = [];
|
||||
foreach ($list as $row) {
|
||||
@@ -103,13 +114,16 @@ class DiceRewardConfig extends BaseModel
|
||||
}
|
||||
}
|
||||
$minRealEv = empty($list) ? 0.0 : (float) min(array_column($list, 'real_ev'));
|
||||
self::$instance = [
|
||||
$instance = [
|
||||
'list' => $list,
|
||||
'by_tier' => $byTier,
|
||||
'by_tier_grid' => $byTierGrid,
|
||||
'min_real_ev' => $minRealEv,
|
||||
];
|
||||
Cache::set(self::CACHE_KEY_INSTANCE, self::$instance, self::CACHE_TTL);
|
||||
Cache::set(self::cacheKeyForDept($deptId), $instance, self::CACHE_TTL);
|
||||
if ($deptId === AdminScopeHelper::DEFAULT_TEMPLATE_DEPT) {
|
||||
self::$instance = $instance;
|
||||
}
|
||||
}
|
||||
|
||||
private static function buildEmptyInstance(): array
|
||||
@@ -125,9 +139,9 @@ class DiceRewardConfig extends BaseModel
|
||||
/**
|
||||
* 按档位+色子点数取一条(用于 BIGWIN)
|
||||
*/
|
||||
public static function getCachedByTierAndGridNumber(string $tier, int $gridNumber): ?array
|
||||
public static function getCachedByTierAndGridNumber(string $tier, int $gridNumber, ?int $deptId = null): ?array
|
||||
{
|
||||
$inst = self::getCachedInstance();
|
||||
$inst = self::getCachedInstance($deptId);
|
||||
$byTierGrid = $inst['by_tier_grid'] ?? [];
|
||||
$tierData = $byTierGrid[$tier] ?? [];
|
||||
$row = $tierData[$gridNumber] ?? null;
|
||||
@@ -143,9 +157,9 @@ class DiceRewardConfig extends BaseModel
|
||||
/**
|
||||
* 从缓存按档位取奖励列表(不含权重,仅配置)
|
||||
*/
|
||||
public static function getCachedByTier(string $tier): array
|
||||
public static function getCachedByTier(string $tier, ?int $deptId = null): array
|
||||
{
|
||||
$inst = self::getCachedInstance();
|
||||
$inst = self::getCachedInstance($deptId);
|
||||
$byTier = $inst['by_tier'] ?? [];
|
||||
return $byTier[$tier] ?? [];
|
||||
}
|
||||
@@ -155,10 +169,10 @@ class DiceRewardConfig extends BaseModel
|
||||
* @param int $direction 0=顺时针, 1=逆时针
|
||||
* @return array 每行含 id, grid_number, real_ev, tier, weight 等
|
||||
*/
|
||||
public static function getCachedByTierForDirection(string $tier, int $direction): array
|
||||
public static function getCachedByTierForDirection(string $tier, int $direction, ?int $deptId = null): array
|
||||
{
|
||||
$list = self::getCachedByTier($tier);
|
||||
$weightMap = DiceReward::getCachedByTierAndDirection($tier, $direction);
|
||||
$list = self::getCachedByTier($tier, $deptId);
|
||||
$weightMap = DiceReward::getCachedByTierAndDirection($tier, $direction, $deptId);
|
||||
foreach ($list as $i => $row) {
|
||||
$id = isset($row['id']) ? (int) $row['id'] : 0;
|
||||
$list[$i]['weight'] = $weightMap[$id] ?? 1;
|
||||
@@ -171,19 +185,27 @@ class DiceRewardConfig extends BaseModel
|
||||
self::$instance = null;
|
||||
}
|
||||
|
||||
private static function refreshCacheForModel($model): void
|
||||
{
|
||||
$deptId = AdminScopeHelper::normalizeRecordDeptId(
|
||||
is_array($model) ? ($model['dept_id'] ?? null) : ($model->dept_id ?? null)
|
||||
);
|
||||
self::refreshCache($deptId);
|
||||
}
|
||||
|
||||
public static function onAfterInsert($model): void
|
||||
{
|
||||
self::refreshCache();
|
||||
self::refreshCacheForModel($model);
|
||||
}
|
||||
|
||||
public static function onAfterUpdate($model): void
|
||||
{
|
||||
self::refreshCache();
|
||||
self::refreshCacheForModel($model);
|
||||
}
|
||||
|
||||
public static function onAfterDelete($model): void
|
||||
{
|
||||
self::refreshCache();
|
||||
self::refreshCacheForModel($model);
|
||||
}
|
||||
|
||||
public function searchGridNumberMinAttr($query, $value)
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\model\reward_config;
|
||||
|
||||
use plugin\saiadmin\basic\think\BaseModel;
|
||||
use app\dice\model\DiceModel;
|
||||
|
||||
/**
|
||||
* 权重配比测试记录模型
|
||||
@@ -20,7 +20,7 @@ use plugin\saiadmin\basic\think\BaseModel;
|
||||
* @property int|null $admin_id 执行测试的管理员ID
|
||||
* @property string|null $create_time 创建时间
|
||||
*/
|
||||
class DiceRewardConfigRecord extends BaseModel
|
||||
class DiceRewardConfigRecord extends DiceModel
|
||||
{
|
||||
protected $pk = 'id';
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
namespace app\dice\model\reward_config_record;
|
||||
|
||||
use app\dice\model\play_record_test\DicePlayRecordTest;
|
||||
use plugin\saiadmin\basic\think\BaseModel;
|
||||
use app\dice\model\DiceModel;
|
||||
use think\model\relation\HasMany;
|
||||
|
||||
/**
|
||||
@@ -43,7 +43,7 @@ use think\model\relation\HasMany;
|
||||
* @property int|null $admin_id 执行测试的管理员ID
|
||||
* @property string|null $create_time 创建时间
|
||||
*/
|
||||
class DiceRewardConfigRecord extends BaseModel
|
||||
class DiceRewardConfigRecord extends DiceModel
|
||||
{
|
||||
/** 状态:失败 */
|
||||
public const STATUS_FAIL = -1;
|
||||
|
||||
497
server/app/dice/service/DiceChannelConfigService.php
Normal file
497
server/app/dice/service/DiceChannelConfigService.php
Normal file
@@ -0,0 +1,497 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\dice\service;
|
||||
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use app\dice\logic\reward\DiceRewardLogic;
|
||||
use app\dice\model\reward\DiceReward;
|
||||
use app\dice\model\reward_config\DiceRewardConfig;
|
||||
use plugin\saiadmin\app\model\system\SystemDept;
|
||||
use plugin\saiadmin\app\model\system\SystemUser;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
use support\think\Db;
|
||||
|
||||
/**
|
||||
* 渠道默认配置复制、补齐与关联删除
|
||||
* 默认配置:dept_id = 0(与超管「默认配置模板」一致)
|
||||
*/
|
||||
class DiceChannelConfigService
|
||||
{
|
||||
/** 需 (dept_id, id) 复合唯一的配置表 */
|
||||
private const COMPOSITE_KEY_TABLES = [
|
||||
'dice_config',
|
||||
'dice_reward_config',
|
||||
];
|
||||
/** 从默认模板复制的配置表 */
|
||||
private const CONFIG_TABLES = [
|
||||
'dice_config',
|
||||
'dice_ante_config',
|
||||
'dice_lottery_pool_config',
|
||||
'dice_reward_config',
|
||||
'dice_game',
|
||||
];
|
||||
|
||||
/** 复制时必须保留主键 id(非自增或固定 0-25) */
|
||||
private const TABLES_KEEP_ID = [
|
||||
'dice_config',
|
||||
'dice_reward_config',
|
||||
];
|
||||
|
||||
/** 可关联删除的业务表 */
|
||||
private const RELATION_TABLES = [
|
||||
'dice_config' => ['label' => '游戏键值配置', 'group' => 'configs'],
|
||||
'dice_ante_config' => ['label' => '底注配置', 'group' => 'configs'],
|
||||
'dice_lottery_pool_config' => ['label' => '彩金池配置', 'group' => 'configs'],
|
||||
'dice_reward_config' => ['label' => '奖励索引配置', 'group' => 'configs'],
|
||||
'dice_reward' => ['label' => '中奖概率(奖励对照)', 'group' => 'configs'],
|
||||
'dice_game' => ['label' => '游戏管理', 'group' => 'configs'],
|
||||
'dice_player' => ['label' => '玩家', 'group' => 'players'],
|
||||
'dice_play_record' => ['label' => '抽奖记录', 'group' => 'records'],
|
||||
'dice_play_record_test' => ['label' => '测试抽奖记录', 'group' => 'records'],
|
||||
'dice_player_wallet_record' => ['label' => '钱包流水', 'group' => 'records'],
|
||||
'dice_player_ticket_record' => ['label' => '票券记录', 'group' => 'records'],
|
||||
'dice_reward_config_record' => ['label' => '权重测试记录', 'group' => 'records'],
|
||||
];
|
||||
|
||||
/**
|
||||
* 默认模板 dept_id 统一为 0,并为固定 id 的配置表建立 (dept_id, id) 唯一约束
|
||||
*/
|
||||
public function ensureConfigCompositeKeys(): void
|
||||
{
|
||||
foreach (array_merge(self::CONFIG_TABLES, ['dice_reward']) as $table) {
|
||||
if ($this->tableHasColumn($table, 'dept_id')) {
|
||||
Db::table($table)->whereNull('dept_id')->update(['dept_id' => AdminScopeHelper::DEFAULT_TEMPLATE_DEPT]);
|
||||
}
|
||||
}
|
||||
foreach (self::COMPOSITE_KEY_TABLES as $table) {
|
||||
if (!$this->tableHasColumn($table, 'dept_id')) {
|
||||
continue;
|
||||
}
|
||||
if (!$this->tableHasColumn($table, 'row_id')) {
|
||||
if ($table === 'dice_reward_config') {
|
||||
Db::execute(
|
||||
'ALTER TABLE `dice_reward_config`'
|
||||
. ' MODIFY `id` int(11) NOT NULL COMMENT \'ID\','
|
||||
. ' ADD COLUMN `row_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT FIRST,'
|
||||
. ' DROP PRIMARY KEY,'
|
||||
. ' ADD PRIMARY KEY (`row_id`)'
|
||||
);
|
||||
} else {
|
||||
Db::execute(
|
||||
'ALTER TABLE `dice_config`'
|
||||
. ' ADD COLUMN `row_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT FIRST,'
|
||||
. ' DROP PRIMARY KEY,'
|
||||
. ' ADD PRIMARY KEY (`row_id`)'
|
||||
);
|
||||
}
|
||||
}
|
||||
$indexes = Db::query("SHOW INDEX FROM `{$table}` WHERE Key_name = 'uk_dept_config'");
|
||||
if (empty($indexes)) {
|
||||
Db::execute("ALTER TABLE `{$table}` ADD UNIQUE KEY `uk_dept_config` (`dept_id`, `id`)");
|
||||
}
|
||||
}
|
||||
$this->ensureDeptScopedUniqueIndexes();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将全局唯一键改为按渠道 (dept_id, 业务键) 唯一,便于复制默认模板
|
||||
*/
|
||||
private function ensureDeptScopedUniqueIndexes(): void
|
||||
{
|
||||
if ($this->tableHasColumn('dice_lottery_pool_config', 'dept_id')) {
|
||||
$old = Db::query("SHOW INDEX FROM `dice_lottery_pool_config` WHERE Key_name = 'dice_lottery_poll_config_unique'");
|
||||
if (!empty($old)) {
|
||||
Db::execute('ALTER TABLE `dice_lottery_pool_config` DROP INDEX `dice_lottery_poll_config_unique`');
|
||||
}
|
||||
$uk = Db::query("SHOW INDEX FROM `dice_lottery_pool_config` WHERE Key_name = 'uk_dept_name'");
|
||||
if (empty($uk)) {
|
||||
Db::execute('ALTER TABLE `dice_lottery_pool_config` ADD UNIQUE KEY `uk_dept_name` (`dept_id`, `name`)');
|
||||
}
|
||||
}
|
||||
if ($this->tableHasColumn('dice_game', 'dept_id')) {
|
||||
foreach (['uk_dice_game_code', 'uk_dice_game_key'] as $idx) {
|
||||
$exists = Db::query("SHOW INDEX FROM `dice_game` WHERE Key_name = '{$idx}'");
|
||||
if (!empty($exists)) {
|
||||
Db::execute("ALTER TABLE `dice_game` DROP INDEX `{$idx}`");
|
||||
}
|
||||
}
|
||||
$ukCode = Db::query("SHOW INDEX FROM `dice_game` WHERE Key_name = 'uk_dept_game_code'");
|
||||
if (empty($ukCode)) {
|
||||
Db::execute('ALTER TABLE `dice_game` ADD UNIQUE KEY `uk_dept_game_code` (`dept_id`, `game_code`)');
|
||||
}
|
||||
$ukKey = Db::query("SHOW INDEX FROM `dice_game` WHERE Key_name = 'uk_dept_game_key'");
|
||||
if (empty($ukKey)) {
|
||||
Db::execute('ALTER TABLE `dice_game` ADD UNIQUE KEY `uk_dept_game_key` (`dept_id`, `game_key`)');
|
||||
}
|
||||
}
|
||||
if ($this->tableHasColumn('dice_reward', 'dept_id')) {
|
||||
$old = Db::query("SHOW INDEX FROM `dice_reward` WHERE Key_name = 'uk_direction_grid_number'");
|
||||
if (!empty($old)) {
|
||||
Db::execute('ALTER TABLE `dice_reward` DROP INDEX `uk_direction_grid_number`');
|
||||
}
|
||||
$uk = Db::query("SHOW INDEX FROM `dice_reward` WHERE Key_name = 'uk_dept_direction_grid'");
|
||||
if (empty($uk)) {
|
||||
Db::execute('ALTER TABLE `dice_reward` ADD UNIQUE KEY `uk_dept_direction_grid` (`dept_id`, `direction`, `grid_number`)');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将当前无 dept_id 的配置标记为默认模板(仅执行一次迁移)
|
||||
*/
|
||||
public function markLegacyConfigAsDefault(): int
|
||||
{
|
||||
$this->ensureConfigCompositeKeys();
|
||||
$total = 0;
|
||||
foreach (self::CONFIG_TABLES as $table) {
|
||||
if (!$this->tableHasColumn($table, 'dept_id')) {
|
||||
continue;
|
||||
}
|
||||
$total += $this->countByDept($table, AdminScopeHelper::DEFAULT_TEMPLATE_DEPT);
|
||||
}
|
||||
return $total;
|
||||
}
|
||||
|
||||
/**
|
||||
* 为单个渠道从默认模板复制配置(已存在则跳过)
|
||||
*/
|
||||
public function copyDefaultConfigToDept(int $deptId): array
|
||||
{
|
||||
if ($deptId <= 0) {
|
||||
throw new ApiException('Invalid channel id');
|
||||
}
|
||||
$result = ['dept_id' => $deptId, 'copied' => [], 'skipped' => [], 'merged' => []];
|
||||
foreach (self::CONFIG_TABLES as $table) {
|
||||
if (!$this->tableHasColumn($table, 'dept_id')) {
|
||||
continue;
|
||||
}
|
||||
if (in_array($table, self::TABLES_KEEP_ID, true)) {
|
||||
$merged = $this->syncCompositeIdTableFromDefault($table, $deptId);
|
||||
if ($merged > 0) {
|
||||
$result['merged'][$table] = $merged;
|
||||
} elseif ($this->countByDept($table, $deptId) > 0) {
|
||||
$result['skipped'][] = $table;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if ($this->countByDept($table, $deptId) > 0) {
|
||||
$result['skipped'][] = $table;
|
||||
continue;
|
||||
}
|
||||
$rows = $this->defaultTemplateRows($table);
|
||||
if (empty($rows)) {
|
||||
continue;
|
||||
}
|
||||
foreach ($rows as $row) {
|
||||
$row = (array) $row;
|
||||
unset($row['id'], $row['row_id'], $row['create_time'], $row['update_time'], $row['delete_time']);
|
||||
$row['dept_id'] = $deptId;
|
||||
Db::table($table)->insert($row);
|
||||
}
|
||||
$result['copied'][] = $table;
|
||||
}
|
||||
$this->ensureRewardReferenceForDept($deptId);
|
||||
DiceRewardConfig::refreshCache($deptId);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按业务 id 从默认模板补齐配置(dice_config / dice_reward_config)
|
||||
*/
|
||||
private function syncCompositeIdTableFromDefault(string $table, int $deptId): int
|
||||
{
|
||||
$templateRows = $this->defaultTemplateRows($table);
|
||||
if (empty($templateRows)) {
|
||||
return 0;
|
||||
}
|
||||
$inserted = 0;
|
||||
foreach ($templateRows as $row) {
|
||||
$row = (array) $row;
|
||||
if (!isset($row['id'])) {
|
||||
continue;
|
||||
}
|
||||
$businessId = $row['id'];
|
||||
$exists = Db::table($table)->where('dept_id', $deptId)->where('id', $businessId)->count();
|
||||
if ($exists > 0) {
|
||||
continue;
|
||||
}
|
||||
unset($row['row_id'], $row['create_time'], $row['update_time'], $row['delete_time']);
|
||||
$row['dept_id'] = $deptId;
|
||||
Db::table($table)->insert($row);
|
||||
$inserted++;
|
||||
}
|
||||
return $inserted;
|
||||
}
|
||||
|
||||
/**
|
||||
* 渠道已有奖励索引时,自动生成 dice_reward 对照表
|
||||
*/
|
||||
public function ensureRewardReferenceForDept(int $deptId): void
|
||||
{
|
||||
if ($deptId <= 0 || !$this->tableHasColumn('dice_reward', 'dept_id')) {
|
||||
return;
|
||||
}
|
||||
if ($this->countByDept('dice_reward_config', $deptId) <= 0) {
|
||||
return;
|
||||
}
|
||||
if ($this->countByDept('dice_reward', $deptId) > 0) {
|
||||
return;
|
||||
}
|
||||
$logic = new DiceRewardLogic();
|
||||
$logic->createRewardReferenceFromConfig($deptId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制默认 dice_reward 到渠道
|
||||
*/
|
||||
public function copyDefaultRewardsToDept(int $deptId): void
|
||||
{
|
||||
$this->ensureRewardReferenceForDept($deptId);
|
||||
if (!$this->tableHasColumn('dice_reward', 'dept_id')) {
|
||||
return;
|
||||
}
|
||||
if ($this->countByDept('dice_reward', $deptId) > 0) {
|
||||
return;
|
||||
}
|
||||
if ($this->countByDept('dice_reward_config', $deptId) > 0) {
|
||||
return;
|
||||
}
|
||||
$rows = $this->defaultTemplateRows('dice_reward');
|
||||
foreach ($rows as $row) {
|
||||
$row = (array) $row;
|
||||
unset($row['id'], $row['row_id']);
|
||||
unset($row['create_time'], $row['update_time'], $row['delete_time']);
|
||||
$row['dept_id'] = $deptId;
|
||||
Db::table('dice_reward')->insert($row);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 为所有已有渠道补齐缺失配置
|
||||
*/
|
||||
public function syncAllChannelsFromDefault(): array
|
||||
{
|
||||
$deptIds = SystemDept::column('id');
|
||||
$summary = [];
|
||||
foreach ($deptIds as $deptId) {
|
||||
$deptId = (int) $deptId;
|
||||
if ($deptId <= 0) {
|
||||
continue;
|
||||
}
|
||||
$summary[$deptId] = $this->copyDefaultConfigToDept($deptId);
|
||||
}
|
||||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修复已删除渠道 ID、无管理员关联的遗留数据,归并到首个顶级渠道
|
||||
*/
|
||||
public function repairOrphanDeptReferences(): array
|
||||
{
|
||||
$validDeptIds = array_map('intval', SystemDept::column('id') ?: []);
|
||||
if (empty($validDeptIds)) {
|
||||
return [];
|
||||
}
|
||||
$rootDeptId = min($validDeptIds);
|
||||
$stats = [];
|
||||
$inList = implode(',', $validDeptIds);
|
||||
|
||||
$stats['sa_system_user'] = Db::execute(
|
||||
"UPDATE sa_system_user SET dept_id = {$rootDeptId}
|
||||
WHERE dept_id IS NOT NULL AND dept_id > 0 AND dept_id NOT IN ({$inList})"
|
||||
);
|
||||
|
||||
$bizTables = [
|
||||
'dice_player',
|
||||
'dice_play_record',
|
||||
'dice_play_record_test',
|
||||
'dice_player_wallet_record',
|
||||
'dice_player_ticket_record',
|
||||
'dice_reward_config_record',
|
||||
];
|
||||
foreach ($bizTables as $table) {
|
||||
if (!$this->tableHasColumn($table, 'dept_id')) {
|
||||
continue;
|
||||
}
|
||||
$stats[$table . '_invalid_dept'] = Db::execute(
|
||||
"UPDATE `{$table}` SET dept_id = {$rootDeptId}
|
||||
WHERE dept_id IS NOT NULL AND dept_id > 0 AND dept_id NOT IN ({$inList})"
|
||||
);
|
||||
}
|
||||
|
||||
return $stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据管理员/玩家回填 dept_id
|
||||
*/
|
||||
public function backfillDataDeptId(): array
|
||||
{
|
||||
$stats = $this->repairOrphanDeptReferences();
|
||||
if ($this->tableHasColumn('dice_player', 'dept_id') && $this->tableHasColumn('dice_player', 'admin_id')) {
|
||||
$stats['dice_player'] = Db::execute(
|
||||
'UPDATE dice_player p INNER JOIN sa_system_user u ON p.admin_id = u.id
|
||||
SET p.dept_id = u.dept_id WHERE (p.dept_id IS NULL OR p.dept_id = 0) AND u.dept_id IS NOT NULL AND u.dept_id > 0'
|
||||
);
|
||||
}
|
||||
$validDeptIds = SystemDept::column('id') ?: [];
|
||||
if (!empty($validDeptIds) && $this->tableHasColumn('dice_player', 'dept_id')) {
|
||||
$rootDeptId = (int) min($validDeptIds);
|
||||
$stats['dice_player_legacy'] = Db::table('dice_player')
|
||||
->where(function ($q) {
|
||||
$q->whereNull('dept_id')->whereOr('dept_id', 0);
|
||||
})
|
||||
->update(['dept_id' => $rootDeptId]);
|
||||
}
|
||||
$stats = array_merge($stats, $this->backfillRecordDeptIdByPlayer('dice_play_record'));
|
||||
$stats = array_merge($stats, $this->backfillRecordDeptIdByAdmin('dice_play_record'));
|
||||
$stats = array_merge($stats, $this->backfillRecordDeptIdByPlayer('dice_player_wallet_record'));
|
||||
$stats = array_merge($stats, $this->backfillRecordDeptIdByPlayer('dice_player_ticket_record'));
|
||||
$stats = array_merge($stats, $this->backfillRecordDeptIdByAdmin('dice_play_record_test'));
|
||||
if (!empty($validDeptIds) && $this->tableHasColumn('dice_play_record_test', 'dept_id')) {
|
||||
$rootDeptId = (int) min($validDeptIds);
|
||||
$stats['dice_play_record_test_legacy'] = Db::table('dice_play_record_test')
|
||||
->where(function ($q) {
|
||||
$q->whereNull('dept_id')->whereOr('dept_id', 0);
|
||||
})
|
||||
->update(['dept_id' => $rootDeptId]);
|
||||
}
|
||||
if ($this->tableHasColumn('dice_reward_config_record', 'dept_id')) {
|
||||
$stats['dice_reward_config_record'] = Db::execute(
|
||||
'UPDATE dice_reward_config_record r INNER JOIN sa_system_user u ON r.admin_id = u.id
|
||||
SET r.dept_id = u.dept_id WHERE (r.dept_id IS NULL OR r.dept_id = 0) AND u.dept_id IS NOT NULL AND u.dept_id > 0'
|
||||
);
|
||||
}
|
||||
return $stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除渠道前关联数据统计
|
||||
*/
|
||||
public function getDestroyPreview(array $deptIds): array
|
||||
{
|
||||
$items = [];
|
||||
foreach ($deptIds as $deptId) {
|
||||
$deptId = (int) $deptId;
|
||||
if ($deptId <= 0) {
|
||||
continue;
|
||||
}
|
||||
$dept = SystemDept::find($deptId);
|
||||
$row = [
|
||||
'dept_id' => $deptId,
|
||||
'dept_name' => $dept ? $dept->name : '',
|
||||
'user_count' => SystemUser::where('dept_id', $deptId)->count(),
|
||||
'relations' => [],
|
||||
];
|
||||
foreach (self::RELATION_TABLES as $table => $meta) {
|
||||
if (!$this->tableHasColumn($table, 'dept_id')) {
|
||||
continue;
|
||||
}
|
||||
$count = $this->countByDept($table, $deptId);
|
||||
if ($count > 0) {
|
||||
$row['relations'][] = [
|
||||
'table' => $table,
|
||||
'label' => $meta['label'],
|
||||
'group' => $meta['group'],
|
||||
'count' => $count,
|
||||
];
|
||||
}
|
||||
}
|
||||
$items[] = $row;
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除渠道及勾选的关联数据
|
||||
*
|
||||
* @param array $deleteTables 要删除的表名列表
|
||||
*/
|
||||
public function destroyDeptWithRelations(int $deptId, array $deleteTables): void
|
||||
{
|
||||
if ($deptId <= 0) {
|
||||
throw new ApiException('Invalid channel id');
|
||||
}
|
||||
$userCount = SystemUser::where('dept_id', $deptId)->count();
|
||||
if ($userCount > 0) {
|
||||
throw new ApiException('This channel has users, please delete or transfer them first');
|
||||
}
|
||||
$allowed = array_keys(self::RELATION_TABLES);
|
||||
foreach ($deleteTables as $table) {
|
||||
if (!in_array($table, $allowed, true)) {
|
||||
continue;
|
||||
}
|
||||
if (!$this->tableHasColumn($table, 'dept_id')) {
|
||||
continue;
|
||||
}
|
||||
Db::table($table)->where('dept_id', $deptId)->delete();
|
||||
}
|
||||
SystemDept::destroy($deptId, true);
|
||||
DiceRewardConfig::refreshCache($deptId);
|
||||
DiceReward::refreshCache($deptId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
private function defaultTemplateRows(string $table): array
|
||||
{
|
||||
$templateId = AdminScopeHelper::DEFAULT_TEMPLATE_DEPT;
|
||||
$rows = Db::table($table)->where('dept_id', $templateId)->select()->toArray();
|
||||
if (!empty($rows)) {
|
||||
return $rows;
|
||||
}
|
||||
return Db::table($table)->whereNull('dept_id')->select()->toArray();
|
||||
}
|
||||
|
||||
private function backfillRecordDeptIdByPlayer(string $table): array
|
||||
{
|
||||
if (!$this->tableHasColumn($table, 'dept_id') || !$this->tableHasColumn($table, 'player_id')) {
|
||||
return [];
|
||||
}
|
||||
return [
|
||||
$table => Db::execute(
|
||||
"UPDATE `{$table}` r INNER JOIN dice_player p ON r.player_id = p.id
|
||||
SET r.dept_id = p.dept_id WHERE (r.dept_id IS NULL OR r.dept_id = 0) AND p.dept_id IS NOT NULL AND p.dept_id > 0"
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
private function backfillRecordDeptIdByAdmin(string $table): array
|
||||
{
|
||||
if (!$this->tableHasColumn($table, 'dept_id') || !$this->tableHasColumn($table, 'admin_id')) {
|
||||
return [];
|
||||
}
|
||||
return [
|
||||
$table => Db::execute(
|
||||
"UPDATE `{$table}` r INNER JOIN sa_system_user u ON r.admin_id = u.id
|
||||
SET r.dept_id = u.dept_id WHERE (r.dept_id IS NULL OR r.dept_id = 0) AND u.dept_id IS NOT NULL AND u.dept_id > 0"
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
private function countByDept(string $table, ?int $deptId): int
|
||||
{
|
||||
$query = Db::table($table);
|
||||
if ($deptId === null || $deptId === AdminScopeHelper::DEFAULT_TEMPLATE_DEPT) {
|
||||
$templateId = AdminScopeHelper::DEFAULT_TEMPLATE_DEPT;
|
||||
$query->where(function ($q) use ($templateId) {
|
||||
$q->where('dept_id', $templateId)->whereOr('dept_id', 'null');
|
||||
});
|
||||
} else {
|
||||
$query->where('dept_id', $deptId);
|
||||
}
|
||||
return $query->count();
|
||||
}
|
||||
|
||||
private function tableHasColumn(string $table, string $column): bool
|
||||
{
|
||||
try {
|
||||
$fields = Db::getFields($table);
|
||||
return isset($fields[$column]);
|
||||
} catch (\Throwable $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\validate\player;
|
||||
|
||||
use app\dice\model\player\DicePlayer;
|
||||
use plugin\saiadmin\basic\BaseValidate;
|
||||
|
||||
/**
|
||||
@@ -17,7 +18,7 @@ class DicePlayerValidate extends BaseValidate
|
||||
* 定义验证规则
|
||||
*/
|
||||
protected $rule = [
|
||||
'username' => 'require',
|
||||
'username' => 'require|unique:' . DicePlayer::class . ',username^dept_id',
|
||||
'name' => 'require',
|
||||
'phone' => 'require',
|
||||
'password' => 'require',
|
||||
@@ -30,6 +31,7 @@ class DicePlayerValidate extends BaseValidate
|
||||
*/
|
||||
protected $message = [
|
||||
'username' => '用户名必须填写',
|
||||
'username.unique' => 'PLAYER_USERNAME_DEPT_UNIQUE',
|
||||
'name' => '昵称必须填写',
|
||||
'phone' => '手机号必须填写',
|
||||
'password' => '密码必须填写',
|
||||
|
||||
Reference in New Issue
Block a user