[色子游戏]底注配置
This commit is contained in:
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]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user