@@ -39,6 +39,11 @@ function normalizePhones(phones?: string[]): string[] {
|
||||
|
||||
const SKU_AUTO_PREFIX = 'DK';
|
||||
const SKU_AUTO_PAD = 6;
|
||||
const SKU_AUTO_RE = /^DK(\d+)$/;
|
||||
|
||||
function isDkSkuCode(code: string | null | undefined): boolean {
|
||||
return !!code && SKU_AUTO_RE.test(code);
|
||||
}
|
||||
const MAX_SPEC_ATTRS = 3;
|
||||
const MAX_SPEC_VALUES = 10;
|
||||
|
||||
@@ -465,14 +470,31 @@ export class AdminProductsService {
|
||||
throw new BadRequestException('请且仅指定一个默认 SKU');
|
||||
}
|
||||
|
||||
const barcodes = rows.map((r) => r.barcode69.trim());
|
||||
const barcodes = rows.map((r) => r.barcode69.trim()).filter(Boolean);
|
||||
if (barcodes.length !== rows.length) {
|
||||
throw new BadRequestException('每个规格须填写 69 码');
|
||||
}
|
||||
if (new Set(barcodes).size !== barcodes.length) {
|
||||
throw new BadRequestException('69 码不可重复');
|
||||
throw new BadRequestException('同一商品内 69 码不可重复,每个规格须使用不同 69 码');
|
||||
}
|
||||
const barcodeTaken = await this.prisma.commonProductSku.findFirst({
|
||||
where: { barcode69: { in: barcodes }, productId: { not: productId } },
|
||||
select: { barcode69: true },
|
||||
});
|
||||
if (barcodeTaken) {
|
||||
throw new BadRequestException(`69 码已存在:${barcodeTaken.barcode69}`);
|
||||
}
|
||||
|
||||
await this.prisma.$transaction(async (tx) => {
|
||||
const existing = await tx.commonProductSku.findMany({ where: { productId } });
|
||||
const keepIds = new Set(rows.map((r) => r.id).filter(Boolean) as string[]);
|
||||
const needGenerate = rows.filter((row) => {
|
||||
if (!row.id) return true;
|
||||
const old = existing.find((s) => s.id.toString() === row.id);
|
||||
return !old || !isDkSkuCode(old.skuCode);
|
||||
}).length;
|
||||
const generatedCodes = await this.allocateAutoSkuCodesTx(tx, needGenerate);
|
||||
let genIdx = 0;
|
||||
|
||||
for (const old of existing) {
|
||||
if (keepIds.has(old.id.toString())) continue;
|
||||
@@ -508,7 +530,12 @@ export class AdminProductsService {
|
||||
? BOTTLES_PER_BOX
|
||||
: 1;
|
||||
const status = (row.status ?? 'DRAFT') as 'DRAFT' | 'ON_SALE' | 'OFF_SALE';
|
||||
const skuCode = row.skuCode?.trim() || (await this.nextAutoSkuCodeTx(tx));
|
||||
const old = row.id ? existing.find((s) => s.id.toString() === row.id) : undefined;
|
||||
const skuCode =
|
||||
old && isDkSkuCode(old.skuCode) ? old.skuCode : generatedCodes[genIdx++];
|
||||
if (!skuCode) {
|
||||
throw new BadRequestException('SKU 码生成失败,请重试');
|
||||
}
|
||||
|
||||
let skuId: bigint;
|
||||
if (row.id) {
|
||||
@@ -609,7 +636,7 @@ export class AdminProductsService {
|
||||
await this.prisma.commonProductSku.create({
|
||||
data: {
|
||||
productId,
|
||||
skuCode: product.skuCode,
|
||||
skuCode: await this.nextAutoSkuCode(),
|
||||
barcode69: product.barcode69,
|
||||
specKey: '',
|
||||
specText: product.spec,
|
||||
@@ -635,7 +662,6 @@ export class AdminProductsService {
|
||||
await this.prisma.commonProductSku.update({
|
||||
where: { id: defaultSku.id },
|
||||
data: {
|
||||
skuCode: product.skuCode,
|
||||
barcode69: product.barcode69,
|
||||
specText: product.spec,
|
||||
price: product.price,
|
||||
@@ -651,12 +677,22 @@ export class AdminProductsService {
|
||||
|
||||
/** 生成 DK + 6 位自增 SKU,冲突重试 */
|
||||
private async nextAutoSkuCode(): Promise<string> {
|
||||
return this.nextAutoSkuCodeTx(this.prisma);
|
||||
const [code] = await this.allocateAutoSkuCodesTx(this.prisma, 1);
|
||||
return code;
|
||||
}
|
||||
|
||||
private async nextAutoSkuCodeTx(
|
||||
db: Prisma.TransactionClient | PrismaService,
|
||||
): Promise<string> {
|
||||
const [code] = await this.allocateAutoSkuCodesTx(db, 1);
|
||||
return code;
|
||||
}
|
||||
|
||||
private async allocateAutoSkuCodesTx(
|
||||
db: Prisma.TransactionClient | PrismaService,
|
||||
count: number,
|
||||
): Promise<string[]> {
|
||||
if (count <= 0) return [];
|
||||
const [fromItem, fromSku] = await Promise.all([
|
||||
db.commonProductItem.findMany({
|
||||
where: { skuCode: { startsWith: SKU_AUTO_PREFIX } },
|
||||
@@ -668,14 +704,15 @@ export class AdminProductsService {
|
||||
}),
|
||||
]);
|
||||
let maxSeq = 0;
|
||||
const re = new RegExp(`^${SKU_AUTO_PREFIX}(\\d+)$`);
|
||||
for (const row of [...fromItem, ...fromSku]) {
|
||||
const m = re.exec(row.skuCode);
|
||||
const m = SKU_AUTO_RE.exec(row.skuCode);
|
||||
if (!m) continue;
|
||||
const n = Number(m[1]);
|
||||
if (Number.isFinite(n) && n > maxSeq) maxSeq = n;
|
||||
}
|
||||
return `${SKU_AUTO_PREFIX}${String(maxSeq + 1).padStart(SKU_AUTO_PAD, '0')}`;
|
||||
return Array.from({ length: count }, (_, i) => {
|
||||
return `${SKU_AUTO_PREFIX}${String(maxSeq + 1 + i).padStart(SKU_AUTO_PAD, '0')}`;
|
||||
});
|
||||
}
|
||||
|
||||
private async createWithGeneratedSku(
|
||||
|
||||
@@ -1353,6 +1353,7 @@ class AdminSkuRowDto {
|
||||
@IsString({ each: true })
|
||||
specValueIds?: string[];
|
||||
|
||||
/** 忽略;服务端自动生成 DK 码 */
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
skuCode?: string;
|
||||
|
||||
Reference in New Issue
Block a user