From afa84c9823ac932e2ba4f90865fcac50f9b85e8a Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Tue, 16 Sep 2025 12:04:12 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BD=A6=E7=B3=BB=E7=BC=96=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/service/BrandSeriesService.php | 46 ++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/app/admin/service/BrandSeriesService.php b/app/admin/service/BrandSeriesService.php index 21760c3..bc9d11b 100644 --- a/app/admin/service/BrandSeriesService.php +++ b/app/admin/service/BrandSeriesService.php @@ -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; + } } \ No newline at end of file