Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/flow-core/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export const FlowMessageKey = {
DESIGN_SCRIPT_COMPILE_ERROR: 'design.script_compile_error',
DESIGN_VIEW_CODE_SAVE_SUCCESS: 'design.view_code_save_success',
DESIGN_GROOVY_META_FAILED: 'design.groovy_meta_failed',
/** 运行中实例强制同步成功 */
DESIGN_FORCE_SYNC_SUCCESS: 'design.force_sync_success',
/** 运行中实例强制同步未选择实例 */
DESIGN_FORCE_SYNC_NO_SELECTED: 'design.force_sync_no_selected',

// ---- App 层 ----
APP_LOGIN_SUCCESS: 'app.login_success',
Expand Down Expand Up @@ -167,6 +171,9 @@ function getDefaultMessages(): Record<string, MessageTemplate> {
[FlowMessageKey.DESIGN_SCRIPT_COMPILE_ERROR]: '脚本编译请求失败',
[FlowMessageKey.DESIGN_VIEW_CODE_SAVE_SUCCESS]: '保存成功',
[FlowMessageKey.DESIGN_GROOVY_META_FAILED]: '获取脚本元数据失败',
[FlowMessageKey.DESIGN_FORCE_SYNC_SUCCESS]: (data: any) =>
`已同步 ${data?.count ?? 0} 条流程实例,跳过 ${data?.skipped ?? 0} 条已是最新配置的实例`,
[FlowMessageKey.DESIGN_FORCE_SYNC_NO_SELECTED]: '请先选择需要同步的流程实例',

// App 层
[FlowMessageKey.APP_LOGIN_SUCCESS]: 'login success',
Expand Down
46 changes: 46 additions & 0 deletions packages/flow-design/src/api/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,49 @@ export const importWorkflow = (request: WorkflowImportRequest) => {
export const workflowCodeExists = (code: string) => {
return httpClient.get('/api/cmd/workflow/exists', {code});
}

/**
* 运行中的流程实例(以开始节点记录代表一个实例),用于强制同步时选择范围
*/
export interface RunningInstanceQuery {
workCode: string;
current: number;
pageSize: number;
}

export interface RunningInstanceItem {
processId: string;
recordId: number;
title?: string;
workTitle?: string;
workCode?: string;
nodeName?: string;
createOperatorId?: number;
createOperatorName?: string;
createTime?: number;
workflowRuntimeId?: number;
}

export const runningInstances = (request: RunningInstanceQuery) => {
return httpClient.page('/api/query/record/running', request, {}, {}, []);
}

export interface ForceSyncRequest {
workCode: string;
/** 选中同步的流程实例,未选中的实例不会被修改 */
processIds: string[];
}

export interface ForceSyncResult {
workflowRuntimeId: number;
workVersion: number;
syncedProcessIds: string[];
skippedProcessIds: string[];
}

/**
* 运行中流程实例强制同步最新流程设计
*/
export const forceSync = (request: ForceSyncRequest) => {
return httpClient.post('/api/cmd/record/forceSync', request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import React from "react";
import {Button, Divider, Form, Input, message, Popover, Space, Tabs} from "antd";
import {LayoutHeaderHeight, TabPanelType} from "../types";
import {useDesignContext} from "../hooks/use-design-context";
import {CloseOutlined, ExportOutlined, ImportOutlined, SaveOutlined} from "@ant-design/icons";
import {CloseOutlined, ExportOutlined, ImportOutlined, SaveOutlined, SyncOutlined} from "@ant-design/icons";
import {EventBus, FlowMessageKey, FlowMessageRegistry} from "@coding-flow/flow-core";
import {exportWorkflow} from "@/api/workflow";
import {DesignImport} from "@/components/design-import";
import {DesignSyncInstance} from "@/components/design-sync-instance";

const Left = () => {
return (
Expand Down Expand Up @@ -103,6 +104,7 @@ const SaveAsButton = () => {
const Right = () => {
const {state,context} = useDesignContext();
const [resetImportVisible, setResetImportVisible] = React.useState(false);
const [syncInstanceVisible, setSyncInstanceVisible] = React.useState(false);

return (
<>
Expand All @@ -122,6 +124,12 @@ const Right = () => {
setResetImportVisible(true);
}}
>重置流程</Button>
<Button
icon={<SyncOutlined />}
onClick={() => {
setSyncInstanceVisible(true);
}}
>强制同步</Button>
<Divider type="vertical" />
<Button
icon={<SaveOutlined/>}
Expand Down Expand Up @@ -154,6 +162,13 @@ const Right = () => {
setResetImportVisible(false);
}}
/>
<DesignSyncInstance
open={syncInstanceVisible}
workCode={state.workflow.code}
onClose={() => {
setSyncInstanceVisible(false);
}}
/>
</>
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.notice {
margin-bottom: 12px;
}
128 changes: 128 additions & 0 deletions packages/flow-design/src/components/design-sync-instance/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import React from "react";
import {Alert, message, Modal, type TableColumnsType} from "antd";
import {Table} from "@coding-flow/flow-pc-ui";
import {FlowMessageKey, FlowMessageRegistry} from "@coding-flow/flow-core";
import {forceSync, runningInstances, type ForceSyncResult, type RunningInstanceItem} from "@/api/workflow";
import {toProcessIds} from "./selection";
import styles from "./design_sync_instance.module.scss";

export interface DesignSyncInstanceProps {
/** 是否展示 */
open: boolean;
/** 当前设计的流程编码 */
workCode: string;
/** 关闭 */
onClose: () => void;
}

const columns: TableColumnsType<RunningInstanceItem> = [
{
title: '流程标题',
dataIndex: 'title',
ellipsis: true,
},
{
title: '发起人',
dataIndex: 'createOperatorName',
width: 140,
render: (value: string | undefined) => value || '-',
},
{
title: '创建时间',
dataIndex: 'createTime',
width: 200,
render: (value: number | undefined) => (value ? new Date(value).toLocaleString() : '-'),
},
];

/**
* 运行中实例强制同步最新流程设计。
*
* <p>只同步勾选的流程实例,未勾选的实例继续使用原运行时快照;
* 后端会校验流程结构与运行实例一致,新增/删除节点、调整节点顺序或删除已有按钮时不允许同步。</p>
*/
export const DesignSyncInstance: React.FC<DesignSyncInstanceProps> = (props) => {

const [selectedRows, setSelectedRows] = React.useState<RunningInstanceItem[]>([]);
const [submitting, setSubmitting] = React.useState(false);

React.useEffect(() => {
if (!props.open) {
setSelectedRows([]);
setSubmitting(false);
}
}, [props.open]);

const submit = async () => {
const processIds = toProcessIds(selectedRows);
if (processIds.length === 0) {
message.warning(
FlowMessageRegistry.getInstance().get(FlowMessageKey.DESIGN_FORCE_SYNC_NO_SELECTED)
);
return;
}
setSubmitting(true);
try {
const response = await forceSync({
workCode: props.workCode,
processIds,
});
if (response.success) {
const result = response.data as ForceSyncResult | undefined;
message.success(
FlowMessageRegistry.getInstance().get(FlowMessageKey.DESIGN_FORCE_SYNC_SUCCESS, {
count: result?.syncedProcessIds?.length ?? 0,
skipped: result?.skippedProcessIds?.length ?? 0,
})
);
props.onClose();
}
} finally {
setSubmitting(false);
}
};

return (
<Modal
title={"强制同步运行中实例"}
open={props.open}
width={860}
destroyOnHidden={true}
okText={"同步"}
cancelText={"取消"}
confirmLoading={submitting}
onOk={submit}
onCancel={props.onClose}
>
<Alert
className={styles.notice}
type={"warning"}
showIcon={true}
message={"同步说明"}
description={"同步使用已保存的最新流程设计,修改设计后请先保存。仅勾选的运行中实例会改用最新配置,未勾选的实例保持原配置不变;新增或删除节点、调整节点顺序、删除已有按钮时不允许同步。"}
/>
<Table<RunningInstanceItem>
rowKey={"processId"}
columns={columns}
request={async (params) => {
const response = await runningInstances({
workCode: props.workCode,
current: params.current,
pageSize: params.pageSize,
});
return {
data: response.data ?? [],
success: response.success,
total: response.total ?? 0,
};
}}
rowSelection={{
selectedRowKeys: toProcessIds(selectedRows),
onChange: (_keys, rows) => {
setSelectedRows(rows);
},
}}
/>
</Modal>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type {RunningInstanceItem} from "@/api/workflow";

/**
* 汇总选中实例的流程id。
*
* <p>跨页勾选时 antd Table 会返回多行数据(同一实例以开始节点记录代表一行),
* 这里按 processId 去重并过滤空值,避免重复提交同一实例。</p>
*/
export const toProcessIds = (rows: RunningInstanceItem[]): string[] => {
const processIds = rows
.map((row) => row.processId)
.filter((processId): processId is string => !!processId);
return Array.from(new Set(processIds));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {describe, expect, it} from "@rstest/core";
import {toProcessIds} from "@/components/design-sync-instance/selection";
import type {RunningInstanceItem} from "@/api/workflow";

/**
* 运行中实例强制同步(后端 issue #281)
*
* 选择弹窗按「开始节点记录」代表一个流程实例,提交时只传流程id;
* 跨页勾选可能重复返回同一实例,需要去重并过滤空值。
*/
describe('强制同步实例选择', () => {

const row = (processId: string): RunningInstanceItem => ({
processId,
recordId: 1,
});

it('应返回选中实例的流程id', () => {
// when
const processIds = toProcessIds([row('p1'), row('p2')]);

// then
expect(processIds).toEqual(['p1', 'p2']);
});

it('应对重复的流程id去重', () => {
// when
const processIds = toProcessIds([row('p1'), row('p1'), row('p2')]);

// then
expect(processIds).toEqual(['p1', 'p2']);
});

it('应过滤空流程id', () => {
// when
const processIds = toProcessIds([row(''), row('p1')]);

// then
expect(processIds).toEqual(['p1']);
});

it('未选择时应返回空数组', () => {
// when
const processIds = toProcessIds([]);

// then
expect(processIds).toEqual([]);
});
});
Loading