车系编码

This commit is contained in:
2025-09-16 12:04:12 +08:00
parent 62c32d25ff
commit afa84c9823
+46
View File
@@ -3,6 +3,7 @@
namespace app\admin\service;
use app\admin\validate\BrandSeriesValidate;
use app\common\model\Brand;
use app\common\traits\crud\CrudServiceTrait;
use app\common\model\BrandSeries;
use support\Request;
@@ -77,4 +78,49 @@ class BrandSeriesService
return $config;
}
/**
* 生成唯一的7位颜色编码
* 格式:C + (O/I) + 5位字母数字组合
*
* @param int $type 类型
* @return string 7位唯一编码
*/
public function generateUniqueModificationCode($brand_id)
{
$chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$maxAttempts = 10; // 防止无限循环
$brand_code = Brand::where('id', $brand_id)->value('brand_code');
$brand_code = $brand_code??'BS';
for ($attempt = 0; $attempt < $maxAttempts; $attempt++) {
// 生成后5位随机字符
$suffix = '';
for ($i = 0; $i < 6; $i++) {
$suffix .= $chars[rand(0, strlen($chars) - 1)];
}
$code = $brand_code . $suffix;
// 检查是否已存在
$existing = BrandSeries::where('series_code', $code)->first();
if (!$existing) {
return $code;
}
}
// 如果1000次尝试后仍然重复,使用时间戳确保唯一性
$timestamp = substr(time(), -6);
return $brand_code . $timestamp;
}
/**
* 添加前置处理
*/
public function beforeServiceStore($data, Request $request)
{
$brand_id = $data['brand_id'] ?? 0;
$data['series_code'] = $this->generateUniqueModificationCode($brand_id);
return $data;
}
}