关于生成订单号的问题
/**
* 生成分期流水号
*
* @return bool|string
* @throws \Exception
*/
public static function findAvailableNo()
{
// 分期流水号前缀
$prefix = date('YmdHis');
for ($i = 0; $i < 10; $i++) {
// 随机生成 6 位的数字
$no = $prefix . str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
// 判断是否存在
if (!static::query()->where('no', $no)->exists()) {
return $no;
}
}
\Log::warning(sprintf('find installment no failed'));
return false;
}
疑问一:循环为何定义为 10 次?
疑问二:每次生成都要做数据库查询,是否对性能影响太大了,比如抢购之类的业务场景,并发量大的时候如何快速的确保订单号的唯一?
推荐文章: