1.期号记录修改为纯数字
This commit is contained in:
55
app/common/library/game/GamePeriodNo.php
Normal file
55
app/common/library/game/GamePeriodNo.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
use app\common\library\game\GamePeriodNo;
|
||||
use support\think\Db;
|
||||
use Throwable;
|
||||
|
||||
@@ -192,7 +193,7 @@ final class GameRecordService
|
||||
|
||||
private static function generatePeriodNo(): string
|
||||
{
|
||||
return date('Ymd-His') . '-' . substr(bin2hex(random_bytes(4)), 0, 8);
|
||||
return GamePeriodNo::generateNext();
|
||||
}
|
||||
|
||||
private static function truthyConfigInput(mixed $v): bool
|
||||
|
||||
Reference in New Issue
Block a user