fix(category-config): 规模行导入同步一级类目与分类到类目配置;档位导入表头兼容带单位列名
This commit is contained in:
parent
eb6a2e6571
commit
cb3efaa50c
@ -0,0 +1,49 @@
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
import { AppError, ErrorCode } from "@/server/core/errors";
|
||||
import { handleRequest, jsonError, jsonSuccess } from "@/server/core/response";
|
||||
import { intakeNonIntegratedProductService } from "@/server/services/intake-non-integrated-product.service";
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
return handleRequest(
|
||||
request,
|
||||
async ({ requestId, user }) => {
|
||||
const body = (await request.json().catch(() => null)) as Record<
|
||||
string,
|
||||
unknown
|
||||
> | null;
|
||||
const productId =
|
||||
typeof body?.productId === "string" ? body.productId.trim() : "";
|
||||
const fileIds = Array.isArray(body?.fileIds)
|
||||
? body.fileIds.filter(
|
||||
(fileId): fileId is string =>
|
||||
typeof fileId === "string" && fileId.trim().length > 0,
|
||||
)
|
||||
: [];
|
||||
|
||||
if (!productId || fileIds.length === 0) {
|
||||
return jsonError(
|
||||
ErrorCode.VALIDATION_INVALID_FORMAT,
|
||||
"产品或安装资料排序内容不正确",
|
||||
400,
|
||||
requestId,
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const product = await intakeNonIntegratedProductService.reorderInstallationFiles(
|
||||
productId,
|
||||
fileIds,
|
||||
user,
|
||||
);
|
||||
return jsonSuccess({ product }, 200, requestId);
|
||||
} catch (error) {
|
||||
if (error instanceof AppError) {
|
||||
return jsonError(error.code, error.message, error.statusCode, requestId);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
{ requireAuth: true },
|
||||
);
|
||||
}
|
||||
@ -6,10 +6,14 @@ import { handleRequest, jsonError } from '@/server/core/response'
|
||||
import { supplyChainProductArchiveCategoryGroupTierService } from '@/server/services/supply-chain-product-archive-category-group-tier.service'
|
||||
|
||||
const HEADER_MAP: Record<string, string> = {
|
||||
一级类目: 'categoryL1',
|
||||
二级类目: 'categoryL2',
|
||||
分类: 'categoryGroup',
|
||||
规模标签: 'scaleLabel',
|
||||
'业绩起点(元)': 'performanceStart',
|
||||
业绩起点: 'performanceStart',
|
||||
业绩终点: 'performanceEnd',
|
||||
'日销起点(件/天)': 'dailySalesStart',
|
||||
日销起点: 'dailySalesStart',
|
||||
日销终点: 'dailySalesEnd',
|
||||
categoryGroup: 'categoryGroup',
|
||||
|
||||
@ -231,6 +231,7 @@ export function buildNonIntegratedSecondaryWorkspaceRenderers({
|
||||
isReadOnly: editability.isReadOnly,
|
||||
onPushDrawingSystemVersion:
|
||||
workspaceUploadActions.handlePushInstallationDrawingSystemVersion,
|
||||
onReorderFiles: workspaceUploadActions.handleReorderInstallationFiles,
|
||||
selectedFileId:
|
||||
documentPresentation.effectiveSelectedWorkspaceFile?.id ?? "",
|
||||
archivePreviewPanel: renderArchivePreviewPanel(),
|
||||
|
||||
@ -836,6 +836,21 @@ export async function pushInstallationFileToDrawingSystem(input: {
|
||||
}>(response);
|
||||
}
|
||||
|
||||
export async function reorderInstallationFiles(
|
||||
productId: string,
|
||||
fileIds: string[],
|
||||
) {
|
||||
const response = await fetchWithTimeout(
|
||||
"/api/intake/non-integrated/installation-order",
|
||||
{
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ productId, fileIds }),
|
||||
},
|
||||
);
|
||||
return readApiData<{ product: NonIntegratedProductRecord }>(response);
|
||||
}
|
||||
|
||||
export async function uploadDetachedNonIntegratedFile(
|
||||
productId: string | undefined,
|
||||
file: File,
|
||||
|
||||
@ -660,6 +660,7 @@ export type NonIntegratedUploadFile = {
|
||||
installDrawingId?: string;
|
||||
installAssetType?: string;
|
||||
installVersionLabel?: string;
|
||||
installOrder?: number;
|
||||
installIsCurrentVersion?: boolean;
|
||||
installDrawingSyncStatus?:
|
||||
"not_applicable" | "pending" | "processing" | "succeeded" | "failed";
|
||||
@ -2027,6 +2028,10 @@ function normalizeUploadFile(
|
||||
installDrawingId: normalizeString(item.installDrawingId).toUpperCase(),
|
||||
installAssetType: normalizeString(item.installAssetType),
|
||||
installVersionLabel: normalizeString(item.installVersionLabel),
|
||||
installOrder:
|
||||
typeof item.installOrder === "number" && Number.isFinite(item.installOrder)
|
||||
? item.installOrder
|
||||
: undefined,
|
||||
installIsCurrentVersion:
|
||||
typeof item.installIsCurrentVersion === "boolean"
|
||||
? item.installIsCurrentVersion
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
"use client";
|
||||
|
||||
import { useSortable } from "@dnd-kit/sortable";
|
||||
import { CSS } from "@dnd-kit/utilities";
|
||||
import { GripVertical } from "lucide-react";
|
||||
import type { NonIntegratedUploadFile } from "../nonIntegratedStorage";
|
||||
import { getUploadFileTypeLabel } from "./documents/uploadFilePreview";
|
||||
|
||||
@ -10,6 +13,7 @@ type InstallationFileCardProps = {
|
||||
assetLabel: string;
|
||||
versionLabel: string;
|
||||
isActive: boolean;
|
||||
isDragDisabled: boolean;
|
||||
onSelect: (fileId: string) => void;
|
||||
};
|
||||
|
||||
@ -20,37 +24,64 @@ export function InstallationFileCard({
|
||||
assetLabel,
|
||||
versionLabel,
|
||||
isActive,
|
||||
isDragDisabled,
|
||||
onSelect,
|
||||
}: InstallationFileCardProps) {
|
||||
const {
|
||||
attributes,
|
||||
listeners,
|
||||
setNodeRef,
|
||||
transform,
|
||||
transition,
|
||||
isDragging,
|
||||
} = useSortable({ id: file.id, disabled: isDragDisabled });
|
||||
const syncStatus = file.installDrawingSyncStatus;
|
||||
const fileDrawingId = file.installDrawingId || drawingId;
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={setNodeRef}
|
||||
style={{
|
||||
transform: CSS.Transform.toString(transform),
|
||||
transition,
|
||||
}}
|
||||
className={`min-w-0 rounded-apple-xl border p-3 transition ${
|
||||
isActive
|
||||
? "border-apple-label-primary bg-white shadow-sm"
|
||||
: "border-apple-separator bg-white/80"
|
||||
}`}
|
||||
} ${isDragging ? "z-10 opacity-70 shadow-lg" : ""}`}
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
data-testid={`installation-workspace-file-${file.id}`}
|
||||
onClick={() => onSelect(file.id)}
|
||||
className="flex w-full min-w-0 items-start gap-2 text-left"
|
||||
>
|
||||
<span className="shrink-0 rounded-md bg-apple-bg-secondary px-2 py-1 text-[10px] font-apple-semibold text-apple-label-secondary">
|
||||
{getUploadFileTypeLabel(file)}
|
||||
</span>
|
||||
<span className="min-w-0 flex-1">
|
||||
<span className="block truncate text-sm font-apple-semibold text-apple-label-primary">
|
||||
{file.name}
|
||||
<div className="flex min-w-0 items-start gap-2">
|
||||
<button
|
||||
type="button"
|
||||
{...attributes}
|
||||
{...listeners}
|
||||
disabled={isDragDisabled}
|
||||
aria-label="拖动调整顺序"
|
||||
title="拖动调整顺序"
|
||||
className="mt-0.5 shrink-0 cursor-grab rounded-md p-1 text-apple-label-tertiary hover:bg-apple-bg-secondary hover:text-apple-label-primary active:cursor-grabbing disabled:cursor-default disabled:opacity-40"
|
||||
>
|
||||
<GripVertical className="size-4" />
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
data-testid={`installation-workspace-file-${file.id}`}
|
||||
onClick={() => onSelect(file.id)}
|
||||
className="flex min-w-0 flex-1 items-start gap-2 text-left"
|
||||
>
|
||||
<span className="shrink-0 rounded-md bg-apple-bg-secondary px-2 py-1 text-[10px] font-apple-semibold text-apple-label-secondary">
|
||||
{getUploadFileTypeLabel(file)}
|
||||
</span>
|
||||
<span className="mt-1 block truncate text-[11px] text-apple-label-secondary">
|
||||
{styleLabel} · {assetLabel} · {versionLabel}
|
||||
<span className="min-w-0 flex-1">
|
||||
<span className="block truncate text-sm font-apple-semibold text-apple-label-primary">
|
||||
{file.name}
|
||||
</span>
|
||||
<span className="mt-1 block truncate text-[11px] text-apple-label-secondary">
|
||||
{styleLabel} · {assetLabel} · {versionLabel}
|
||||
</span>
|
||||
</span>
|
||||
</span>
|
||||
</button>
|
||||
</button>
|
||||
</div>
|
||||
{fileDrawingId && syncStatus !== "not_applicable" ? (
|
||||
<div className="pt-2">
|
||||
<p
|
||||
|
||||
@ -1,6 +1,19 @@
|
||||
"use client";
|
||||
|
||||
import type { ReactNode } from "react";
|
||||
import {
|
||||
closestCenter,
|
||||
DndContext,
|
||||
PointerSensor,
|
||||
useSensor,
|
||||
useSensors,
|
||||
type DragEndEvent,
|
||||
} from "@dnd-kit/core";
|
||||
import {
|
||||
arrayMove,
|
||||
rectSortingStrategy,
|
||||
SortableContext,
|
||||
} from "@dnd-kit/sortable";
|
||||
import type { NonIntegratedUploadFile } from "../nonIntegratedStorage";
|
||||
import { FieldLabel } from "../components/FormControls";
|
||||
import { InstallationFileCard } from "./InstallationFileCard";
|
||||
@ -53,6 +66,7 @@ type InstallationWorkspaceProps = {
|
||||
drawingId: string,
|
||||
versionLabel: string,
|
||||
) => void;
|
||||
onReorderFiles: (fileIds: string[]) => Promise<void>;
|
||||
onSelectStyle: (styleKey: string) => void;
|
||||
onSelectAssetType: (assetType: InstallationAssetType) => void;
|
||||
onNoteChange: (value: string) => void;
|
||||
@ -64,7 +78,6 @@ export function InstallationWorkspace({
|
||||
selectedInstallationStyleEntry,
|
||||
activeInstallationStyleFiles,
|
||||
activeInstallationAssetType,
|
||||
activeInstallationAssetFiles,
|
||||
activeInstallationVersionGroups,
|
||||
activeInstallationAssetLabel,
|
||||
noteLabel,
|
||||
@ -74,11 +87,15 @@ export function InstallationWorkspace({
|
||||
selectedFileId,
|
||||
archivePreviewPanel,
|
||||
onPushDrawingSystemVersion,
|
||||
onReorderFiles,
|
||||
onSelectStyle,
|
||||
onSelectAssetType,
|
||||
onNoteChange,
|
||||
onSelectFile,
|
||||
}: InstallationWorkspaceProps) {
|
||||
const sensors = useSensors(
|
||||
useSensor(PointerSensor, { activationConstraint: { distance: 6 } }),
|
||||
);
|
||||
const installationAssetTypeSummaries = INSTALLATION_ASSET_TYPE_OPTIONS.map(
|
||||
(option) => {
|
||||
const files = activeInstallationStyleFiles.filter(
|
||||
@ -100,10 +117,33 @@ export function InstallationWorkspace({
|
||||
const isAssembledOnly =
|
||||
selectedInstallationStyleEntry?.installModeLabel === "整装";
|
||||
|
||||
const handleDragEnd = (event: DragEndEvent) => {
|
||||
if (isReadOnly || !event.over || event.active.id === event.over.id) return;
|
||||
|
||||
const group = activeInstallationVersionGroups.find((candidate) =>
|
||||
candidate.files.some((file) => file.id === event.active.id),
|
||||
);
|
||||
if (!group || !group.files.some((file) => file.id === event.over?.id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const oldIndex = group.files.findIndex(
|
||||
(file) => file.id === event.active.id,
|
||||
);
|
||||
const newIndex = group.files.findIndex(
|
||||
(file) => file.id === event.over?.id,
|
||||
);
|
||||
if (oldIndex < 0 || newIndex < 0) return;
|
||||
|
||||
void onReorderFiles(
|
||||
arrayMove(group.files, oldIndex, newIndex).map((file) => file.id),
|
||||
).catch(() => undefined);
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
data-testid="installation-workspace"
|
||||
className="grid min-h-0 flex-1 overflow-hidden rounded-apple-xl border border-apple-separator bg-apple-bg-primary xl:grid-cols-[220px_minmax(260px,0.85fr)_minmax(420px,1.15fr)]"
|
||||
className="grid min-h-0 flex-1 overflow-hidden rounded-apple-xl border border-apple-separator bg-apple-bg-primary xl:grid-cols-[260px_minmax(260px,0.7fr)_minmax(420px,1.3fr)]"
|
||||
>
|
||||
<div className="flex min-h-0 flex-col border-r border-apple-separator bg-white">
|
||||
<div className="border-b border-apple-separator px-4 py-3">
|
||||
@ -175,10 +215,6 @@ export function InstallationWorkspace({
|
||||
<div className="flex min-h-0 min-w-0 flex-col bg-apple-bg-secondary/40">
|
||||
<div className="border-b border-apple-separator bg-apple-bg-primary/80 px-4 py-3">
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<div className="min-w-[190px] flex-1 rounded-full bg-white px-4 py-2 text-xs text-apple-label-secondary">
|
||||
{selectedInstallationStyleEntry?.label ?? "安装资料"} · 当前{" "}
|
||||
{activeInstallationAssetFiles.length} 份
|
||||
</div>
|
||||
{installationAssetTypeSummaries.map((option) => {
|
||||
const isActive = activeInstallationAssetType === option.key;
|
||||
|
||||
@ -214,41 +250,53 @@ export function InstallationWorkspace({
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex-1 space-y-4">
|
||||
{activeInstallationVersionGroups.map((group) => {
|
||||
const styleKey = selectedInstallationStyleEntry?.key ?? "";
|
||||
const drawingId =
|
||||
styleKey === INSTALLATION_SHARED_STYLE_KEY ? "" : styleKey;
|
||||
<DndContext
|
||||
sensors={isReadOnly ? undefined : sensors}
|
||||
collisionDetection={closestCenter}
|
||||
onDragEnd={handleDragEnd}
|
||||
>
|
||||
<div className="flex-1 space-y-4">
|
||||
{activeInstallationVersionGroups.map((group) => {
|
||||
const styleKey = selectedInstallationStyleEntry?.key ?? "";
|
||||
const drawingId =
|
||||
styleKey === INSTALLATION_SHARED_STYLE_KEY ? "" : styleKey;
|
||||
|
||||
return (
|
||||
<div key={group.label} className="space-y-2">
|
||||
<InstallationVersionGroupHeader
|
||||
label={group.label}
|
||||
isCurrent={group.isCurrent}
|
||||
files={group.files}
|
||||
drawingId={drawingId}
|
||||
isReadOnly={isReadOnly}
|
||||
onPush={onPushDrawingSystemVersion}
|
||||
/>
|
||||
return (
|
||||
<div key={group.label} className="space-y-2">
|
||||
<InstallationVersionGroupHeader
|
||||
label={group.label}
|
||||
isCurrent={group.isCurrent}
|
||||
files={group.files}
|
||||
drawingId={drawingId}
|
||||
isReadOnly={isReadOnly}
|
||||
onPush={onPushDrawingSystemVersion}
|
||||
/>
|
||||
|
||||
<div className="grid grid-cols-[repeat(auto-fill,minmax(220px,1fr))] gap-2">
|
||||
{group.files.map((file) => (
|
||||
<InstallationFileCard
|
||||
key={file.id}
|
||||
file={file}
|
||||
drawingId={drawingId}
|
||||
styleLabel={selectedInstallationStyleEntry?.label ?? "当前款式"}
|
||||
assetLabel={activeInstallationAssetLabel}
|
||||
versionLabel={group.label}
|
||||
isActive={selectedFileId === file.id}
|
||||
onSelect={onSelectFile}
|
||||
/>
|
||||
))}
|
||||
<SortableContext
|
||||
items={group.files.map((file) => file.id)}
|
||||
strategy={rectSortingStrategy}
|
||||
>
|
||||
<div className="grid grid-cols-[repeat(auto-fill,minmax(220px,1fr))] gap-2">
|
||||
{group.files.map((file) => (
|
||||
<InstallationFileCard
|
||||
key={file.id}
|
||||
file={file}
|
||||
drawingId={drawingId}
|
||||
styleLabel={selectedInstallationStyleEntry?.label ?? "当前款式"}
|
||||
assetLabel={activeInstallationAssetLabel}
|
||||
versionLabel={group.label}
|
||||
isActive={selectedFileId === file.id}
|
||||
isDragDisabled={isReadOnly}
|
||||
onSelect={onSelectFile}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</SortableContext>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</DndContext>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -39,6 +39,7 @@ export type CreateUploadFileRecordOptions = {
|
||||
installDrawingId?: string;
|
||||
installAssetType?: InstallationAssetType;
|
||||
installVersionLabel?: string;
|
||||
installOrder?: number;
|
||||
installIsCurrentVersion?: boolean;
|
||||
installDrawingSyncStatus?: "pending" | "processing" | "succeeded" | "failed";
|
||||
packagingStyleKey?: string;
|
||||
@ -84,6 +85,9 @@ export function buildUploadMetadataPayload(
|
||||
if (options.installVersionLabel) {
|
||||
metadata.installVersionLabel = options.installVersionLabel;
|
||||
}
|
||||
if (typeof options.installOrder === "number" && Number.isFinite(options.installOrder)) {
|
||||
metadata.installOrder = options.installOrder;
|
||||
}
|
||||
if (typeof options.installIsCurrentVersion === "boolean") {
|
||||
metadata.installIsCurrentVersion = options.installIsCurrentVersion;
|
||||
}
|
||||
@ -133,6 +137,7 @@ export function applyUploadFileCategoryMetadata(
|
||||
installDrawingSyncedAt: undefined,
|
||||
installAssetType: undefined,
|
||||
installVersionLabel: undefined,
|
||||
installOrder: undefined,
|
||||
installIsCurrentVersion: undefined,
|
||||
packagingStyleKey: undefined,
|
||||
packagingAssetType: undefined,
|
||||
@ -164,6 +169,7 @@ export function applyUploadFileCategoryMetadata(
|
||||
installDrawingSyncedAt: undefined,
|
||||
installAssetType: undefined,
|
||||
installVersionLabel: undefined,
|
||||
installOrder: undefined,
|
||||
installIsCurrentVersion: undefined,
|
||||
packagingStyleKey: undefined,
|
||||
packagingAssetType: undefined,
|
||||
@ -190,6 +196,7 @@ export function applyUploadFileCategoryMetadata(
|
||||
installAssetType: options?.installAssetType ?? file.installAssetType,
|
||||
installVersionLabel:
|
||||
options?.installVersionLabel ?? file.installVersionLabel,
|
||||
installOrder: options?.installOrder ?? file.installOrder,
|
||||
installIsCurrentVersion:
|
||||
options?.installIsCurrentVersion ?? file.installIsCurrentVersion,
|
||||
installDrawingSyncStatus:
|
||||
@ -220,6 +227,7 @@ export function applyUploadFileCategoryMetadata(
|
||||
installDrawingSyncedAt: undefined,
|
||||
installAssetType: undefined,
|
||||
installVersionLabel: undefined,
|
||||
installOrder: undefined,
|
||||
installIsCurrentVersion: undefined,
|
||||
packagingStyleKey: options?.packagingStyleKey ?? file.packagingStyleKey,
|
||||
packagingAssetType:
|
||||
@ -248,6 +256,7 @@ export function applyUploadFileCategoryMetadata(
|
||||
installDrawingSyncedAt: undefined,
|
||||
installAssetType: undefined,
|
||||
installVersionLabel: undefined,
|
||||
installOrder: undefined,
|
||||
installIsCurrentVersion: undefined,
|
||||
packagingStyleKey: undefined,
|
||||
packagingAssetType: undefined,
|
||||
@ -276,6 +285,7 @@ export function applyUploadFileCategoryMetadata(
|
||||
installDrawingSyncedAt: undefined,
|
||||
installAssetType: undefined,
|
||||
installVersionLabel: undefined,
|
||||
installOrder: undefined,
|
||||
installIsCurrentVersion: undefined,
|
||||
packagingStyleKey: undefined,
|
||||
packagingAssetType: undefined,
|
||||
@ -300,6 +310,7 @@ export function applyUploadFileCategoryMetadata(
|
||||
installStyleKey: undefined,
|
||||
installAssetType: undefined,
|
||||
installVersionLabel: undefined,
|
||||
installOrder: undefined,
|
||||
installIsCurrentVersion: undefined,
|
||||
packagingStyleKey: undefined,
|
||||
packagingAssetType: undefined,
|
||||
|
||||
@ -5,7 +5,10 @@ import {
|
||||
} from "react";
|
||||
|
||||
import type { NonIntegratedUploadFile } from "../../nonIntegratedStorage";
|
||||
import type { NonIntegratedUploadProgressHandler } from "../../nonIntegratedApi";
|
||||
import {
|
||||
reorderInstallationFiles as persistInstallationFileOrder,
|
||||
type NonIntegratedUploadProgressHandler,
|
||||
} from "../../nonIntegratedApi";
|
||||
import type { DocumentWorkspaceView } from "../../nonIntegratedDetailTypes";
|
||||
import {
|
||||
DETECTION_WORKSPACE_CATEGORY,
|
||||
@ -31,6 +34,7 @@ import {
|
||||
type InstallationAssetType,
|
||||
} from "../style-assets/styleFileHelpers";
|
||||
import { useInstallationDrawingSystemPushController } from "./useInstallationDrawingSystemPushController";
|
||||
import { reorderInstallationFiles as applyInstallationFileOrder } from "../style-assets/installationFileOrdering";
|
||||
import type { InstallationStyleEntry } from "../style-assets/styleWorkspaceEntries";
|
||||
import {
|
||||
INSTALLATION_ASSET_TYPE_LABELS,
|
||||
@ -226,6 +230,7 @@ export function useWorkspaceFileUploadController({
|
||||
installDrawingId: targetDrawingId || undefined,
|
||||
installAssetType: targetAssetType,
|
||||
installVersionLabel: nextVersionLabel,
|
||||
installOrder: index + 1,
|
||||
installIsCurrentVersion: true,
|
||||
installDrawingSyncStatus: targetDrawingId ? "pending" : undefined,
|
||||
},
|
||||
@ -328,11 +333,39 @@ export function useWorkspaceFileUploadController({
|
||||
event.target.value = "";
|
||||
}
|
||||
|
||||
async function handleReorderInstallationFiles(fileIds: string[]) {
|
||||
if (isReadOnly || !sharedUploadProductId || fileIds.length === 0) return;
|
||||
|
||||
let previousFiles: NonIntegratedUploadFile[] | null = null;
|
||||
setUploadFiles((current) => {
|
||||
previousFiles = current;
|
||||
return applyInstallationFileOrder(current, fileIds);
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await persistInstallationFileOrder(
|
||||
sharedUploadProductId,
|
||||
fileIds,
|
||||
);
|
||||
setUploadFiles(result.product.uploadFiles);
|
||||
setSaveMessage("安装资料顺序已保存。");
|
||||
setSaveMessageType("success");
|
||||
} catch (error) {
|
||||
if (previousFiles) setUploadFiles(previousFiles);
|
||||
setSaveMessage(
|
||||
error instanceof Error ? error.message : "安装资料顺序保存失败",
|
||||
);
|
||||
setSaveMessageType("error");
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
handleUploadDetectionQualityStandardFiles,
|
||||
handleUploadFilesChange,
|
||||
handleUploadFirstQualityFiles,
|
||||
handleUploadInstallationFiles,
|
||||
handleReorderInstallationFiles,
|
||||
handlePushInstallationDrawingSystemVersion,
|
||||
};
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import {
|
||||
pushInstallationFilesInOrder,
|
||||
reorderInstallationFiles,
|
||||
sortInstallationFiles,
|
||||
} from "./installationFileOrdering";
|
||||
|
||||
@ -47,4 +48,40 @@ describe("installation file ordering", () => {
|
||||
remainingCount: 2,
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps the manually saved order ahead of filename order", () => {
|
||||
const files = [
|
||||
{ id: "a", name: "1.jpg" },
|
||||
{ id: "b", name: "2.jpg" },
|
||||
{ id: "c", name: "3.jpg" },
|
||||
];
|
||||
const reordered = reorderInstallationFiles(files, ["c", "a", "b"]);
|
||||
|
||||
expect(sortInstallationFiles(reordered).map((file) => file.id)).toEqual([
|
||||
"c",
|
||||
"a",
|
||||
"b",
|
||||
]);
|
||||
});
|
||||
|
||||
it("pushes files in the saved manual order", async () => {
|
||||
const files = [
|
||||
{ id: "a", name: "1.jpg", installOrder: 2 },
|
||||
{ id: "b", name: "2.jpg", installOrder: 3 },
|
||||
{ id: "c", name: "3.jpg", installOrder: 1 },
|
||||
];
|
||||
const pushed: string[] = [];
|
||||
|
||||
await pushInstallationFilesInOrder(files, {
|
||||
onProcessing: () => undefined,
|
||||
push: async (file) => {
|
||||
pushed.push(file.id);
|
||||
return file.id;
|
||||
},
|
||||
onSucceeded: () => undefined,
|
||||
onFailed: () => undefined,
|
||||
});
|
||||
|
||||
expect(pushed).toEqual(["c", "a", "b"]);
|
||||
});
|
||||
});
|
||||
|
||||
@ -5,12 +5,51 @@ const installationFileNameCollator = new Intl.Collator("zh-CN", {
|
||||
sensitivity: "base",
|
||||
});
|
||||
|
||||
export function sortInstallationFiles<T extends Pick<NonIntegratedUploadFile, "name">>(
|
||||
export function sortInstallationFiles<
|
||||
T extends Pick<NonIntegratedUploadFile, "name"> & {
|
||||
installOrder?: number;
|
||||
},
|
||||
>(
|
||||
files: readonly T[],
|
||||
) {
|
||||
return [...files].sort((left, right) =>
|
||||
installationFileNameCollator.compare(left.name, right.name),
|
||||
const hasCompleteManualOrder =
|
||||
files.length > 0 &&
|
||||
files.every(
|
||||
(file) =>
|
||||
typeof file.installOrder === "number" &&
|
||||
Number.isFinite(file.installOrder),
|
||||
);
|
||||
|
||||
return [...files]
|
||||
.map((file, index) => ({ file, index }))
|
||||
.sort((left, right) => {
|
||||
if (hasCompleteManualOrder) {
|
||||
const orderDifference =
|
||||
(left.file.installOrder ?? 0) - (right.file.installOrder ?? 0);
|
||||
if (orderDifference !== 0) return orderDifference;
|
||||
return left.index - right.index;
|
||||
}
|
||||
|
||||
return installationFileNameCollator.compare(
|
||||
left.file.name,
|
||||
right.file.name,
|
||||
);
|
||||
})
|
||||
.map(({ file }) => file);
|
||||
}
|
||||
|
||||
export function reorderInstallationFiles<T extends { id: string }>(
|
||||
files: readonly T[],
|
||||
orderedIds: readonly string[],
|
||||
) {
|
||||
const orderById = new Map(
|
||||
orderedIds.map((fileId, index) => [fileId, index + 1]),
|
||||
);
|
||||
|
||||
return files.map((file) => {
|
||||
const order = orderById.get(file.id);
|
||||
return order === undefined ? file : { ...file, installOrder: order };
|
||||
});
|
||||
}
|
||||
|
||||
type InstallationSyncStatus = NonIntegratedUploadFile["installDrawingSyncStatus"];
|
||||
|
||||
@ -267,6 +267,7 @@ type AttachUploadFileMetadata = Partial<
|
||||
| 'installDrawingId'
|
||||
| 'installAssetType'
|
||||
| 'installVersionLabel'
|
||||
| 'installOrder'
|
||||
| 'installIsCurrentVersion'
|
||||
| 'installDrawingSyncStatus'
|
||||
| 'installDrawingSyncTaskId'
|
||||
@ -4356,6 +4357,9 @@ function pickUploadFileMetadata(input: AttachUploadFileInput): AttachUploadFileM
|
||||
...(installDrawingSyncedAt ? { installDrawingSyncedAt } : {}),
|
||||
...(installAssetType ? { installAssetType } : {}),
|
||||
...(installVersionLabel ? { installVersionLabel } : {}),
|
||||
...(typeof input.installOrder === 'number' && Number.isFinite(input.installOrder)
|
||||
? { installOrder: input.installOrder }
|
||||
: {}),
|
||||
...(typeof input.installIsCurrentVersion === 'boolean'
|
||||
? { installIsCurrentVersion: input.installIsCurrentVersion }
|
||||
: {}),
|
||||
@ -8019,6 +8023,114 @@ class IntakeNonIntegratedProductService {
|
||||
}
|
||||
}
|
||||
|
||||
async reorderInstallationFiles(
|
||||
productId: string,
|
||||
orderedFileIds: string[],
|
||||
user: AuthUser,
|
||||
) {
|
||||
const existingRecord = await this.repository.findActiveById(productId)
|
||||
if (!existingRecord) {
|
||||
throw new AppError(ErrorCode.PRODUCT_NOT_FOUND, '非集成产品不存在')
|
||||
}
|
||||
|
||||
assertStockWorkspaceWriteCategory(
|
||||
existingRecord,
|
||||
INSTALLATION_WORKSPACE_CATEGORY,
|
||||
user,
|
||||
)
|
||||
await intakeNonIntegratedScopeService.assertCanWriteRecord(
|
||||
existingRecord,
|
||||
user,
|
||||
INSTALLATION_WORKSPACE_CATEGORY,
|
||||
)
|
||||
|
||||
const normalizedFileIds = Array.from(
|
||||
new Set(
|
||||
orderedFileIds
|
||||
.map((fileId) => fileId.trim())
|
||||
.filter(Boolean),
|
||||
),
|
||||
)
|
||||
if (normalizedFileIds.length === 0) {
|
||||
throw new AppError(
|
||||
ErrorCode.VALIDATION_REQUIRED_FIELD,
|
||||
'缺少安装资料排序内容',
|
||||
)
|
||||
}
|
||||
|
||||
const saved = await this.repository.saveActiveWithLock(
|
||||
productId,
|
||||
(record) => {
|
||||
const payload = isRecord(record.payload) ? record.payload : null
|
||||
const uploadFiles =
|
||||
payload && Array.isArray(payload.uploadFiles)
|
||||
? payload.uploadFiles
|
||||
: null
|
||||
if (!payload || !uploadFiles) {
|
||||
throw new AppError(
|
||||
ErrorCode.SYSTEM_INTERNAL,
|
||||
'产品资料中的上传文件数据异常',
|
||||
)
|
||||
}
|
||||
|
||||
const uploadFileById = new Map(
|
||||
uploadFiles.flatMap((candidate) => {
|
||||
if (!isRecord(candidate) || typeof candidate.id !== 'string') {
|
||||
return []
|
||||
}
|
||||
return [[candidate.id, candidate] as const]
|
||||
}),
|
||||
)
|
||||
const targetFiles = normalizedFileIds.map((fileId) => {
|
||||
const file = uploadFileById.get(fileId)
|
||||
if (!file || file.category !== INSTALLATION_WORKSPACE_CATEGORY) {
|
||||
throw new AppError(
|
||||
ErrorCode.FILE_NOT_FOUND,
|
||||
'排序的安装资料不存在',
|
||||
)
|
||||
}
|
||||
return file
|
||||
})
|
||||
const targetIds = new Set(normalizedFileIds)
|
||||
let targetIndex = 0
|
||||
const nextUploadFiles = uploadFiles.map((candidate) => {
|
||||
if (!isRecord(candidate) || typeof candidate.id !== 'string') {
|
||||
return candidate
|
||||
}
|
||||
if (!targetIds.has(candidate.id)) return candidate
|
||||
const nextFile = targetFiles[targetIndex]
|
||||
targetIndex += 1
|
||||
return {
|
||||
...nextFile,
|
||||
installOrder: targetIndex,
|
||||
}
|
||||
})
|
||||
const nextPayload = {
|
||||
...payload,
|
||||
uploadFiles: nextUploadFiles,
|
||||
updatedAt: new Date().toISOString(),
|
||||
}
|
||||
|
||||
return {
|
||||
data: {
|
||||
payload: toJsonValue(nextPayload),
|
||||
updatedBy: user.id,
|
||||
},
|
||||
result: nextUploadFiles,
|
||||
}
|
||||
},
|
||||
)
|
||||
if (saved.status !== 'saved') {
|
||||
throw new AppError(ErrorCode.PRODUCT_NOT_FOUND, '产品开发明细不存在')
|
||||
}
|
||||
|
||||
const product = await this.getById(productId, user)
|
||||
if (!product) {
|
||||
throw new AppError(ErrorCode.PRODUCT_NOT_FOUND, '产品开发明细不存在')
|
||||
}
|
||||
return product
|
||||
}
|
||||
|
||||
async resolveUploadFileForPreview(
|
||||
productId: string,
|
||||
selector: SharedUploadFileSelector,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user