[色子游戏]底注配置
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | saiadmin [ saiadmin快速开发框架 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: your name
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\controller\ante_config;
|
||||
|
||||
use app\dice\logic\ante_config\DiceAnteConfigLogic;
|
||||
use app\dice\validate\ante_config\DiceAnteConfigValidate;
|
||||
use plugin\saiadmin\basic\BaseController;
|
||||
use plugin\saiadmin\service\Permission;
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
|
||||
/**
|
||||
* 底注配置控制器
|
||||
*/
|
||||
class DiceAnteConfigController extends BaseController
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->logic = new DiceAnteConfigLogic();
|
||||
$this->validate = new DiceAnteConfigValidate();
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
#[Permission('底注配置列表', 'dice:ante_config:index:index')]
|
||||
public function index(Request $request): Response
|
||||
{
|
||||
$where = $request->more([
|
||||
['name', ''],
|
||||
['title', ''],
|
||||
['is_default', ''],
|
||||
]);
|
||||
$query = $this->logic->search($where);
|
||||
$data = $this->logic->getList($query);
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
#[Permission('底注配置读取', 'dice:ante_config:index:read')]
|
||||
public function read(Request $request): Response
|
||||
{
|
||||
$id = $request->input('id', '');
|
||||
$model = $this->logic->read($id);
|
||||
$data = is_array($model) ? $model : $model->toArray();
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
#[Permission('底注配置添加', 'dice:ante_config:index:save')]
|
||||
public function save(Request $request): Response
|
||||
{
|
||||
$data = $request->post();
|
||||
$this->validate('save', $data);
|
||||
$result = $this->logic->add($data);
|
||||
return $result ? $this->success('add success') : $this->fail('add failed');
|
||||
}
|
||||
|
||||
#[Permission('底注配置修改', 'dice:ante_config:index:update')]
|
||||
public function update(Request $request): Response
|
||||
{
|
||||
$data = $request->post();
|
||||
$this->validate('update', $data);
|
||||
$result = $this->logic->edit($data['id'], $data);
|
||||
return $result ? $this->success('update success') : $this->fail('update failed');
|
||||
}
|
||||
|
||||
#[Permission('底注配置删除', 'dice:ante_config:index:destroy')]
|
||||
public function destroy(Request $request): Response
|
||||
{
|
||||
$ids = $request->post('ids', '');
|
||||
if (empty($ids)) {
|
||||
return $this->fail('please select data to delete');
|
||||
}
|
||||
$result = $this->logic->destroy($ids);
|
||||
return $result ? $this->success('delete success') : $this->fail('delete failed');
|
||||
}
|
||||
}
|
||||
90
server/app/dice/logic/ante_config/DiceAnteConfigLogic.php
Normal file
90
server/app/dice/logic/ante_config/DiceAnteConfigLogic.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | saiadmin [ saiadmin快速开发框架 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: your name
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\logic\ante_config;
|
||||
|
||||
use app\dice\model\ante_config\DiceAnteConfig;
|
||||
use plugin\saiadmin\basic\think\BaseLogic;
|
||||
|
||||
/**
|
||||
* 底注配置逻辑层
|
||||
*/
|
||||
class DiceAnteConfigLogic extends BaseLogic
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->model = new DiceAnteConfig();
|
||||
}
|
||||
|
||||
public function add(array $data): mixed
|
||||
{
|
||||
return $this->transaction(function () use ($data) {
|
||||
$this->normalizeDefaultField($data);
|
||||
if ((int) ($data['is_default'] ?? 0) === 1) {
|
||||
$this->clearOtherDefaults();
|
||||
}
|
||||
return parent::add($data);
|
||||
});
|
||||
}
|
||||
|
||||
public function edit($id, array $data): mixed
|
||||
{
|
||||
return $this->transaction(function () use ($id, $data) {
|
||||
$this->normalizeDefaultField($data);
|
||||
if ((int) ($data['is_default'] ?? 0) === 1) {
|
||||
$this->clearOtherDefaults((int) $id);
|
||||
}
|
||||
return parent::edit($id, $data);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 防止删除后全表无默认:若删除了默认项,自动把最小 id 设为默认。
|
||||
*/
|
||||
public function destroy($ids): bool
|
||||
{
|
||||
return $this->transaction(function () use ($ids) {
|
||||
$idList = is_array($ids) ? $ids : explode(',', (string) $ids);
|
||||
$intIds = [];
|
||||
foreach ($idList as $v) {
|
||||
$iv = (int) $v;
|
||||
if ($iv > 0) {
|
||||
$intIds[] = $iv;
|
||||
}
|
||||
}
|
||||
if ($intIds === []) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$deletedDefaultCount = $this->model->whereIn('id', $intIds)->where('is_default', 1)->count();
|
||||
$result = $this->model->destroy($intIds);
|
||||
if ($result && $deletedDefaultCount > 0) {
|
||||
$first = $this->model->order('id', 'asc')->find();
|
||||
if ($first) {
|
||||
$this->model->where('id', (int) $first['id'])->update(['is_default' => 1]);
|
||||
}
|
||||
}
|
||||
return (bool) $result;
|
||||
});
|
||||
}
|
||||
|
||||
private function normalizeDefaultField(array &$data): void
|
||||
{
|
||||
if (!array_key_exists('is_default', $data)) {
|
||||
return;
|
||||
}
|
||||
$data['is_default'] = ((int) $data['is_default']) === 1 ? 1 : 0;
|
||||
}
|
||||
|
||||
private function clearOtherDefaults(?int $excludeId = null): void
|
||||
{
|
||||
$query = $this->model->where('is_default', 1);
|
||||
if ($excludeId !== null && $excludeId > 0) {
|
||||
$query->where('id', '<>', $excludeId);
|
||||
}
|
||||
$query->update(['is_default' => 0]);
|
||||
}
|
||||
}
|
||||
48
server/app/dice/model/ante_config/DiceAnteConfig.php
Normal file
48
server/app/dice/model/ante_config/DiceAnteConfig.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | saiadmin [ saiadmin快速开发框架 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: your name
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\model\ante_config;
|
||||
|
||||
use plugin\saiadmin\basic\think\BaseModel;
|
||||
|
||||
/**
|
||||
* 底注配置模型
|
||||
*
|
||||
* @property int $id ID
|
||||
* @property string $name 名称
|
||||
* @property string $title 标题
|
||||
* @property int $is_default 是否默认底注:0否 1是(全表仅允许一条为1)
|
||||
* @property int $mult 底注倍率
|
||||
* @property string $create_time 创建时间
|
||||
* @property string $update_time 更新时间
|
||||
*/
|
||||
class DiceAnteConfig extends BaseModel
|
||||
{
|
||||
protected $pk = 'id';
|
||||
|
||||
protected $table = 'dice_ante_config';
|
||||
|
||||
public function searchNameAttr($query, $value): void
|
||||
{
|
||||
if ($value !== '' && $value !== null) {
|
||||
$query->where('name', 'like', '%' . $value . '%');
|
||||
}
|
||||
}
|
||||
|
||||
public function searchTitleAttr($query, $value): void
|
||||
{
|
||||
if ($value !== '' && $value !== null) {
|
||||
$query->where('title', 'like', '%' . $value . '%');
|
||||
}
|
||||
}
|
||||
|
||||
public function searchIsDefaultAttr($query, $value): void
|
||||
{
|
||||
if ($value !== '' && $value !== null) {
|
||||
$query->where('is_default', (int) $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | saiadmin [ saiadmin快速开发框架 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: your name
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\validate\ante_config;
|
||||
|
||||
use plugin\saiadmin\basic\BaseValidate;
|
||||
|
||||
/**
|
||||
* 底注配置验证器
|
||||
*/
|
||||
class DiceAnteConfigValidate extends BaseValidate
|
||||
{
|
||||
protected $rule = [
|
||||
'name' => 'require|max:64',
|
||||
'title' => 'require|max:255',
|
||||
'is_default' => 'require|in:0,1',
|
||||
'mult' => 'require|integer|gt:0',
|
||||
];
|
||||
|
||||
protected $message = [
|
||||
'name' => '名称必须填写',
|
||||
'title' => '标题必须填写',
|
||||
'is_default' => '默认底注标记必须为 0 或 1',
|
||||
'mult' => '底注倍率必须为大于 0 的整数',
|
||||
];
|
||||
|
||||
protected $scene = [
|
||||
'save' => ['name', 'title', 'is_default', 'mult'],
|
||||
'update' => ['name', 'title', 'is_default', 'mult'],
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user