refactor(intake): extract non-integrated style file helpers

This commit is contained in:
WuYongJie 2026-06-08 16:38:59 +08:00
parent c37e80fb02
commit 6cd4ab5a60
2 changed files with 245 additions and 221 deletions

View File

@ -368,6 +368,24 @@ import {
readFileAsDataUrl,
type UploadFilePreviewType,
} from "./uploadFilePreview";
import {
getDefaultProductPhotoStyleKey,
getFileNameWithoutExtension,
getInstallationStyleLabel,
getPackagingStyleLabel,
getProductPhotoStyleLabel,
getSkuBackedStyleLabel,
getStyleKeyFromModel,
resolveFactoryQualityAssetType,
resolveFactoryQualityFileStyleKey,
resolveInstallationAssetType,
resolveInstallationFileStyleKey,
resolvePackagingFileStyleKey,
sortStyleKeys,
toChineseStyleLabel,
type FactoryQualityAssetType,
type InstallationAssetType,
} from "./styleFileHelpers";
import {
SUPPLIER_CODE_BY_NAME,
SUPPLIER_DIRECTORY,
@ -757,10 +775,8 @@ function areProductPhotoSelectionsEqual(
return false;
}
type InstallationAssetType = "drawing" | "video";
type PackagingAssetType = "inner-drawing";
type DetectionWorkspaceAssetType = NonIntegratedDetectionAssetType;
type FactoryQualityAssetType = "drop-image" | "drop-video";
type InstallationStyleEntry = {
key: string;
@ -4567,110 +4583,6 @@ function normalizeNationalStandardCompliance(value: string | undefined) {
: "";
}
function getStyleKeyFromModel(model: string) {
const match = model
.trim()
.toUpperCase()
.match(/K\d{2}$/);
return match?.[0] ?? "";
}
function toChineseStyleLabel(model: string) {
const match = model
.trim()
.toUpperCase()
.match(/K(\d{2})$/);
if (!match) return "";
const styleIndex = Number(match[1]);
if (!Number.isFinite(styleIndex) || styleIndex <= 0) return "";
if (styleIndex <= 10) {
const numerals = [
"一",
"二",
"三",
"四",
"五",
"六",
"七",
"八",
"九",
"十",
];
return `款式${numerals[styleIndex - 1]}`;
}
if (styleIndex < 20) {
const numerals = ["一", "二", "三", "四", "五", "六", "七", "八", "九"];
return `款式十${numerals[styleIndex - 11]}`;
}
if (styleIndex === 20) {
return "款式二十";
}
return `款式${styleIndex}`;
}
function sortStyleKeys(styleKeys: string[]) {
return [...styleKeys].sort((left, right) => {
const leftMatch = left.match(/^K(\d{2})$/);
const rightMatch = right.match(/^K(\d{2})$/);
if (leftMatch && rightMatch) {
return Number(leftMatch[1]) - Number(rightMatch[1]);
}
if (leftMatch) return -1;
if (rightMatch) return 1;
return left.localeCompare(right, "zh-CN");
});
}
function getInstallationStyleLabel(styleKey: string) {
return toChineseStyleLabel(styleKey) || styleKey || "安装资料";
}
function getSkuBackedStyleLabel(
styleKey: string,
skuItems: NonIntegratedSkuItem[],
fallbackLabel: string,
) {
const normalizedStyleKey = styleKey.trim().toUpperCase();
const matchedSku = skuItems.find(
(item) => getStyleKeyFromModel(item.model) === normalizedStyleKey,
);
const modelLabel = matchedSku?.model.trim();
return modelLabel || getInstallationStyleLabel(normalizedStyleKey) || fallbackLabel;
}
function getProductPhotoStyleLabel(
styleKey: string,
skuItems: NonIntegratedSkuItem[],
) {
return getSkuBackedStyleLabel(styleKey, skuItems, "产品实拍");
}
function getPackagingStyleLabel(
styleKey: string,
skuItems: NonIntegratedSkuItem[],
) {
return getSkuBackedStyleLabel(styleKey, skuItems, "包装资料");
}
function getDefaultProductPhotoStyleKey(knownStyleKeys: string[]) {
const normalizedStyleKeys = sortStyleKeys(
Array.from(
new Set(
knownStyleKeys
.map((styleKey) => styleKey.trim().toUpperCase())
.filter(Boolean),
),
),
);
return normalizedStyleKeys.find((styleKey) => styleKey === "K01") ?? "K01";
}
const INSTALL_METHOD_OPTIONS = ["整装", "拆装"] as const;
const PRODUCT_FACTORY_SHIPPING_METHOD_OPTIONS = ["入仓", "代发"] as const;
const PRODUCT_FACTORY_PRODUCTION_TYPE_OPTIONS = ["OEM", "ODM"] as const;
@ -4754,105 +4666,6 @@ function buildNextInstallationVersionLabel(files: NonIntegratedUploadFile[]) {
return formatInstallationVersionLabel(`V${maxVersion + 1}`);
}
function resolveInstallationFileStyleKey(
file: NonIntegratedUploadFile | null,
knownStyleKeys: string[],
) {
if (!file) return "";
const explicitStyleKey = file.installStyleKey?.trim().toUpperCase() ?? "";
if (explicitStyleKey && knownStyleKeys.includes(explicitStyleKey)) {
return explicitStyleKey;
}
const normalizedFileName = getFileNameWithoutExtension(file.name);
const matchedStyleKey = knownStyleKeys.find((styleKey) =>
normalizedFileName.includes(styleKey),
);
if (matchedStyleKey) {
return matchedStyleKey;
}
const matchedStyleLabel = knownStyleKeys.find((styleKey) => {
const label = getInstallationStyleLabel(styleKey);
return label ? file.name.includes(label) : false;
});
if (matchedStyleLabel) {
return matchedStyleLabel;
}
return knownStyleKeys.length === 1 ? (knownStyleKeys[0] ?? "") : "";
}
function resolveFactoryQualityAssetType(
file: NonIntegratedUploadFile | null,
): FactoryQualityAssetType {
if (file?.factoryQualityAssetType === "drop-video") return "drop-video";
if (file?.factoryQualityAssetType === "drop-image") return "drop-image";
return isVideoUploadFile(file) ? "drop-video" : "drop-image";
}
function resolveFactoryQualityFileStyleKey(
file: NonIntegratedUploadFile | null,
knownStyleKeys: string[],
) {
if (!file) return "";
const explicitStyleKey =
file.factoryQualityStyleKey?.trim().toUpperCase() ?? "";
if (explicitStyleKey && knownStyleKeys.includes(explicitStyleKey)) {
return explicitStyleKey;
}
const normalizedFileName = getFileNameWithoutExtension(file.name);
const matchedStyleKey = knownStyleKeys.find((styleKey) =>
normalizedFileName.includes(styleKey),
);
if (matchedStyleKey) {
return matchedStyleKey;
}
const matchedStyleLabel = knownStyleKeys.find((styleKey) => {
const label = getInstallationStyleLabel(styleKey);
return label ? file.name.includes(label) : false;
});
if (matchedStyleLabel) {
return matchedStyleLabel;
}
return knownStyleKeys.length === 1 ? (knownStyleKeys[0] ?? "") : "";
}
function resolvePackagingFileStyleKey(
file: NonIntegratedUploadFile | null,
knownStyleKeys: string[],
) {
if (!file) return "";
const explicitStyleKey = file.packagingStyleKey?.trim().toUpperCase() ?? "";
if (explicitStyleKey && knownStyleKeys.includes(explicitStyleKey)) {
return explicitStyleKey;
}
const normalizedFileName = getFileNameWithoutExtension(file.name);
const matchedStyleKey = knownStyleKeys.find((styleKey) =>
normalizedFileName.includes(styleKey),
);
if (matchedStyleKey) {
return matchedStyleKey;
}
const matchedStyleLabel = knownStyleKeys.find((styleKey) => {
const label = getInstallationStyleLabel(styleKey);
return label ? file.name.includes(label) : false;
});
if (matchedStyleLabel) {
return matchedStyleLabel;
}
return knownStyleKeys.length === 1 ? (knownStyleKeys[0] ?? "") : "";
}
function buildAutoSpecName(item: NonIntegratedSkuItem) {
const brandPrefixMatch = item.model
.trim()
@ -4999,14 +4812,6 @@ function shouldKeepCustomDrawingId(
).test(normalizedDrawingId);
}
function getFileNameWithoutExtension(fileName: string) {
const trimmedName = fileName.trim();
const extensionIndex = trimmedName.lastIndexOf(".");
if (extensionIndex <= 0) return trimmedName.toUpperCase();
return trimmedName.slice(0, extensionIndex).toUpperCase();
}
function clampPercent(value: number) {
if (!Number.isFinite(value)) return 0;
return Math.min(100, Math.max(0, Math.round(value)));
@ -8652,14 +8457,6 @@ function resolveProductDataGroup(
return inferProductDataGroup(file.name);
}
function resolveInstallationAssetType(
file: NonIntegratedUploadFile | null,
): InstallationAssetType {
if (file?.installAssetType === "video") return "video";
if (file?.installAssetType === "drawing") return "drawing";
return isVideoUploadFile(file) ? "video" : "drawing";
}
function resolveSupplierEntryByName(
value: string,
supplierCodeByName: ReadonlyMap<string, string> = SUPPLIER_CODE_BY_NAME,

View File

@ -0,0 +1,227 @@
import type {
NonIntegratedSkuItem,
NonIntegratedUploadFile,
} from "./nonIntegratedStorage";
import { isVideoUploadFile } from "./uploadFilePreview";
export type InstallationAssetType = "drawing" | "video";
export type FactoryQualityAssetType = "drop-image" | "drop-video";
export function getStyleKeyFromModel(model: string) {
const match = model
.trim()
.toUpperCase()
.match(/K\d{2}$/);
return match?.[0] ?? "";
}
export function toChineseStyleLabel(model: string) {
const match = model
.trim()
.toUpperCase()
.match(/K(\d{2})$/);
if (!match) return "";
const styleIndex = Number(match[1]);
if (!Number.isFinite(styleIndex) || styleIndex <= 0) return "";
if (styleIndex <= 10) {
const numerals = [
"一",
"二",
"三",
"四",
"五",
"六",
"七",
"八",
"九",
"十",
];
return `款式${numerals[styleIndex - 1]}`;
}
if (styleIndex < 20) {
const numerals = ["一", "二", "三", "四", "五", "六", "七", "八", "九"];
return `款式十${numerals[styleIndex - 11]}`;
}
if (styleIndex === 20) {
return "款式二十";
}
return `款式${styleIndex}`;
}
export function sortStyleKeys(styleKeys: string[]) {
return [...styleKeys].sort((left, right) => {
const leftMatch = left.match(/^K(\d{2})$/);
const rightMatch = right.match(/^K(\d{2})$/);
if (leftMatch && rightMatch) {
return Number(leftMatch[1]) - Number(rightMatch[1]);
}
if (leftMatch) return -1;
if (rightMatch) return 1;
return left.localeCompare(right, "zh-CN");
});
}
export function getInstallationStyleLabel(styleKey: string) {
return toChineseStyleLabel(styleKey) || styleKey || "安装资料";
}
export function getSkuBackedStyleLabel(
styleKey: string,
skuItems: NonIntegratedSkuItem[],
fallbackLabel: string,
) {
const normalizedStyleKey = styleKey.trim().toUpperCase();
const matchedSku = skuItems.find(
(item) => getStyleKeyFromModel(item.model) === normalizedStyleKey,
);
const modelLabel = matchedSku?.model.trim();
return modelLabel || getInstallationStyleLabel(normalizedStyleKey) || fallbackLabel;
}
export function getProductPhotoStyleLabel(
styleKey: string,
skuItems: NonIntegratedSkuItem[],
) {
return getSkuBackedStyleLabel(styleKey, skuItems, "产品实拍");
}
export function getPackagingStyleLabel(
styleKey: string,
skuItems: NonIntegratedSkuItem[],
) {
return getSkuBackedStyleLabel(styleKey, skuItems, "包装资料");
}
export function getDefaultProductPhotoStyleKey(knownStyleKeys: string[]) {
const normalizedStyleKeys = sortStyleKeys(
Array.from(
new Set(
knownStyleKeys
.map((styleKey) => styleKey.trim().toUpperCase())
.filter(Boolean),
),
),
);
return normalizedStyleKeys.find((styleKey) => styleKey === "K01") ?? "K01";
}
export function getFileNameWithoutExtension(fileName: string) {
const trimmedName = fileName.trim();
const extensionIndex = trimmedName.lastIndexOf(".");
if (extensionIndex <= 0) return trimmedName.toUpperCase();
return trimmedName.slice(0, extensionIndex).toUpperCase();
}
export function resolveInstallationFileStyleKey(
file: NonIntegratedUploadFile | null,
knownStyleKeys: string[],
) {
if (!file) return "";
const explicitStyleKey = file.installStyleKey?.trim().toUpperCase() ?? "";
if (explicitStyleKey && knownStyleKeys.includes(explicitStyleKey)) {
return explicitStyleKey;
}
const normalizedFileName = getFileNameWithoutExtension(file.name);
const matchedStyleKey = knownStyleKeys.find((styleKey) =>
normalizedFileName.includes(styleKey),
);
if (matchedStyleKey) {
return matchedStyleKey;
}
const matchedStyleLabel = knownStyleKeys.find((styleKey) => {
const label = getInstallationStyleLabel(styleKey);
return label ? file.name.includes(label) : false;
});
if (matchedStyleLabel) {
return matchedStyleLabel;
}
return knownStyleKeys.length === 1 ? (knownStyleKeys[0] ?? "") : "";
}
export function resolveFactoryQualityAssetType(
file: NonIntegratedUploadFile | null,
): FactoryQualityAssetType {
if (file?.factoryQualityAssetType === "drop-video") return "drop-video";
if (file?.factoryQualityAssetType === "drop-image") return "drop-image";
return isVideoUploadFile(file) ? "drop-video" : "drop-image";
}
export function resolveFactoryQualityFileStyleKey(
file: NonIntegratedUploadFile | null,
knownStyleKeys: string[],
) {
if (!file) return "";
const explicitStyleKey =
file.factoryQualityStyleKey?.trim().toUpperCase() ?? "";
if (explicitStyleKey && knownStyleKeys.includes(explicitStyleKey)) {
return explicitStyleKey;
}
const normalizedFileName = getFileNameWithoutExtension(file.name);
const matchedStyleKey = knownStyleKeys.find((styleKey) =>
normalizedFileName.includes(styleKey),
);
if (matchedStyleKey) {
return matchedStyleKey;
}
const matchedStyleLabel = knownStyleKeys.find((styleKey) => {
const label = getInstallationStyleLabel(styleKey);
return label ? file.name.includes(label) : false;
});
if (matchedStyleLabel) {
return matchedStyleLabel;
}
return knownStyleKeys.length === 1 ? (knownStyleKeys[0] ?? "") : "";
}
export function resolvePackagingFileStyleKey(
file: NonIntegratedUploadFile | null,
knownStyleKeys: string[],
) {
if (!file) return "";
const explicitStyleKey = file.packagingStyleKey?.trim().toUpperCase() ?? "";
if (explicitStyleKey && knownStyleKeys.includes(explicitStyleKey)) {
return explicitStyleKey;
}
const normalizedFileName = getFileNameWithoutExtension(file.name);
const matchedStyleKey = knownStyleKeys.find((styleKey) =>
normalizedFileName.includes(styleKey),
);
if (matchedStyleKey) {
return matchedStyleKey;
}
const matchedStyleLabel = knownStyleKeys.find((styleKey) => {
const label = getInstallationStyleLabel(styleKey);
return label ? file.name.includes(label) : false;
});
if (matchedStyleLabel) {
return matchedStyleLabel;
}
return knownStyleKeys.length === 1 ? (knownStyleKeys[0] ?? "") : "";
}
export function resolveInstallationAssetType(
file: NonIntegratedUploadFile | null,
): InstallationAssetType {
if (file?.installAssetType === "video") return "video";
if (file?.installAssetType === "drawing") return "drawing";
return isVideoUploadFile(file) ? "video" : "drawing";
}