Files
webman-buildadmin/app/common/library/game/GamePeriodNo.php

56 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace app\common\library\game;
use support\think\Db;
/**
* 游戏期号:纯数字生成与对外展示格式化。
*/
final class GamePeriodNo
{
/**
* 生成下一期业务期号(纯数字字符串,递增)。
*/
public static function generateNext(): string
{
$maxIdRaw = Db::name('game_record')->max('id');
$maxId = filter_var($maxIdRaw, FILTER_VALIDATE_INT);
if ($maxId === false) {
$maxId = 0;
}
$maxNumericRow = Db::name('game_record')
->whereRaw("period_no REGEXP '^[0-9]+$'")
->fieldRaw('MAX(CAST(period_no AS UNSIGNED)) AS max_no')
->find();
$maxNumericRaw = is_array($maxNumericRow) ? ($maxNumericRow['max_no'] ?? null) : null;
$maxNumeric = filter_var($maxNumericRaw, FILTER_VALIDATE_INT);
if ($maxNumeric === false) {
$maxNumeric = 0;
}
$next = max($maxId, $maxNumeric) + 1;
return (string) $next;
}
/**
* 对外 API / 列表展示的纯数字期号;历史非数字期号回退为 period_id。
*/
public static function toDisplay(string $periodNo, int $periodId = 0): string
{
$trimmed = trim($periodNo);
if ($trimmed !== '' && ctype_digit($trimmed)) {
return $trimmed;
}
if ($periodId > 0) {
return (string) $periodId;
}
return $trimmed;
}
}