You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

43 lines
1.2 KiB

<?php
namespace app\service;
use app\model\Order;
class CardService
{
// 生成 [A-Z][0-9]{3} 格式号码牌,最多重试10次避免冲突
public function generate(): string
{
$maxRetry = 10;
for ($i = 0; $i < $maxRetry; $i++) {
$letter = chr(rand(65, 90)); // A-Z
$num = str_pad(rand(0, 999), 3, '0', STR_PAD_LEFT);
$cardNo = $letter . $num;
if ($this->isAvailable($cardNo)) {
return $cardNo;
}
}
// 冲突过多时追加随机码
return chr(rand(65, 90)) . str_pad(rand(0, 999), 3, '0', STR_PAD_LEFT) . rand(0, 9);
}
// 检查号码牌是否可用(不存在进行中/新单订单)
public function isAvailable(string $cardNo): bool
{
$count = Order::where('card_no', $cardNo)
->whereIn('status', [0, 1])
->count();
return $count === 0;
}
// 释放号码牌:检查该cardNo是否还有未完成订单
public function release(string $cardNo): bool
{
$count = Order::where('card_no', $cardNo)
->whereIn('status', [0, 1])
->count();
return $count === 0; // true=已释放,false=仍占用
}
}