Skip to content

Commit d343994

Browse files
committed
#72 change version
1 parent 458eb01 commit d343994

15 files changed

Lines changed: 358 additions & 54 deletions

File tree

flow-engine-framework/src/main/java/com/codingapi/flow/service/WorkflowService.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,32 @@ public class WorkflowService {
1919
private final WorkflowRuntimeRepository workflowRuntimeRepository;
2020

2121

22-
public void saveWorkflowVersion(WorkflowVersion workflowVersion) {
22+
public void saveWorkflowVersion(WorkflowVersion currentVersion, boolean creatable) {
2323
List<WorkflowVersion> updateList = new ArrayList<>();
2424

25-
workflowVersion.enableVersion();
26-
updateList.add(workflowVersion);
25+
currentVersion.enableVersion();
26+
updateList.add(currentVersion);
2727

28-
List<WorkflowVersion> versionList = workflowVersionRepository.findVersion(workflowVersion.getWorkId());
28+
List<WorkflowVersion> versionList = workflowVersionRepository.findVersion(currentVersion.getWorkId());
2929
if (versionList != null) {
30-
versionList.stream().filter(WorkflowVersion::isCurrent).findFirst().ifPresent(current -> workflowVersion.setId(current.getId()));
30+
31+
if (!creatable) {
32+
versionList.stream().filter(WorkflowVersion::isCurrent).findFirst().ifPresent(current ->{
33+
currentVersion.setId(current.getId());
34+
currentVersion.setVersionName(current.getVersionName());
35+
});
36+
}
3137

3238
for (WorkflowVersion version : versionList) {
33-
if (version.getId() != workflowVersion.getId()) {
39+
if (version.getId() != currentVersion.getId()) {
3440
version.disableVersion();
3541
updateList.add(version);
3642
}
3743
}
3844
}
3945

4046
workflowVersionRepository.saveAll(updateList);
41-
Workflow workflow = workflowVersion.toWorkflow();
47+
Workflow workflow = currentVersion.toWorkflow();
4248
workflowRepository.save(workflow);
4349
}
4450

@@ -53,10 +59,20 @@ public Workflow getWorkflow(String workId) {
5359

5460

5561
public void changeVersion(long versionId) {
56-
WorkflowVersion workflowVersion = workflowVersionRepository.get(versionId);
57-
if (workflowVersion != null) {
58-
this.saveWorkflowVersion(workflowVersion);
62+
WorkflowVersion currentVersion = workflowVersionRepository.get(versionId);
63+
List<WorkflowVersion> versionList = workflowVersionRepository.findVersion(currentVersion.getWorkId());
64+
if (versionList != null) {
65+
for (WorkflowVersion version:versionList){
66+
if(currentVersion.getId() == version.getId()){
67+
version.enableVersion();
68+
}else {
69+
version.disableVersion();
70+
}
71+
}
5972
}
73+
workflowVersionRepository.saveAll(versionList);
74+
workflowRepository.save(currentVersion.toWorkflow());
75+
6076
}
6177

6278
public void updateVersionName(long versionId, String versionName) {
@@ -74,7 +90,7 @@ public void delete(String workId) {
7490

7591
public void saveWorkflow(Workflow workflow) {
7692
WorkflowVersion workflowVersion = new WorkflowVersion(workflow);
77-
this.saveWorkflowVersion(workflowVersion);
93+
this.saveWorkflowVersion(workflowVersion,false);
7894
}
7995

8096
public void saveWorkflowRuntime(WorkflowRuntime workflowRuntime) {

flow-engine-starter-api/src/main/java/com/codingapi/flow/api/controller/WorkflowController.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
import com.codingapi.flow.service.WorkflowService;
1212
import com.codingapi.flow.workflow.Workflow;
1313
import com.codingapi.flow.workflow.WorkflowBuilder;
14+
import com.codingapi.flow.workflow.WorkflowVersion;
1415
import com.codingapi.springboot.framework.dto.request.IdRequest;
1516
import com.codingapi.springboot.framework.dto.response.Response;
1617
import com.codingapi.springboot.framework.dto.response.SingleResponse;
1718
import com.codingapi.springboot.framework.user.UserContext;
1819
import lombok.AllArgsConstructor;
20+
import org.springframework.util.StringUtils;
1921
import org.springframework.web.bind.annotation.*;
2022

2123
import java.util.Map;
@@ -36,7 +38,7 @@ public Response remove(@RequestBody IdRequest request) {
3638

3739
@PostMapping("/updateVersionName")
3840
public Response updateVersionName(@RequestBody WorkflowUpdateVersionNameRequest request) {
39-
workflowService.updateVersionName(request.getId(),request.getName());
41+
workflowService.updateVersionName(request.getId(), request.getVersionName());
4042
return Response.buildSuccess();
4143
}
4244

@@ -88,7 +90,14 @@ public Response save(@RequestBody JSONObject request) {
8890
}
8991
Workflow workflow = Workflow.formJson(request.toJSONString());
9092
workflow.updateTime();
91-
workflowService.saveWorkflow(workflow);
93+
String versionName = request.getString("versionName");
94+
if (StringUtils.hasText(versionName)) {
95+
WorkflowVersion workflowVersion = new WorkflowVersion(workflow);
96+
workflowVersion.setVersionName(versionName);
97+
workflowService.saveWorkflowVersion(workflowVersion,true);
98+
} else {
99+
workflowService.saveWorkflow(workflow);
100+
}
92101
return Response.buildSuccess();
93102
}
94103

flow-engine-starter-api/src/main/java/com/codingapi/flow/api/pojo/WorkflowUpdateVersionNameRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
public class WorkflowUpdateVersionNameRequest {
77

88
private long id;
9-
private String name;
9+
private String versionName;
1010
}

flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/jpa/WorkflowVersionEntityRepository.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.codingapi.flow.infra.jpa;
22

33
import com.codingapi.flow.infra.entity.WorkflowVersionEntity;
4-
import com.codingapi.flow.infra.pojo.WorkflowOption;
54
import com.codingapi.flow.infra.pojo.WorkflowVersionOption;
65
import com.codingapi.springboot.fast.jpa.repository.FastRepository;
76
import org.springframework.data.jpa.repository.Query;
@@ -16,7 +15,7 @@ public interface WorkflowVersionEntityRepository extends FastRepository<Workflo
1615

1716
List<WorkflowVersionEntity> findByWorkId(String workId);
1817

19-
@Query("select new com.codingapi.flow.infra.pojo.WorkflowVersionOption(w.id,w.versionName,w.current) from WorkflowVersionEntity w where w.workId = ?1")
18+
@Query("select new com.codingapi.flow.infra.pojo.WorkflowVersionOption(w.id,w.versionName,w.current,w.updatedTime) from WorkflowVersionEntity w where w.workId = ?1 order by w.updatedTime desc ")
2019
List<WorkflowVersionOption> versions(String workId);
2120

2221
}

flow-engine-starter-infra/src/main/java/com/codingapi/flow/infra/pojo/WorkflowVersionOption.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
@NoArgsConstructor
88
public class WorkflowVersionOption {
99

10-
private String label;
11-
private String value;
10+
private long id;
11+
private String versionName;
1212
private boolean current;
13+
private long updatedTime;
1314

14-
public WorkflowVersionOption(long value, String label, boolean current) {
15-
this.value = String.valueOf(value);
16-
this.label = label;
15+
public WorkflowVersionOption(long id, String versionName, boolean current,long updatedTime) {
16+
this.id = id;
17+
this.versionName = versionName;
1718
this.current = current;
19+
this.updatedTime = updatedTime;
1820
}
1921

2022

frontend/packages/flow-pc/flow-pc-design/src/api/workflow.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export const updateVersionName = (body:any) => {
2020
return httpClient.post('/api/cmd/workflow/updateVersionName',body);
2121
}
2222

23-
export const changeVersion = (versionId:string) => {
24-
return httpClient.post('/api/cmd/workflow/changeVersion',versionId);
23+
export const changeVersion = (id:any) => {
24+
return httpClient.post('/api/cmd/workflow/changeVersion',{id});
2525
}
2626

2727
export const changeState = (id:string) => {

frontend/packages/flow-pc/flow-pc-design/src/components/design-editor/version/edtior-version.tsx

Lines changed: 124 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,139 @@
11
import React from "react";
22
import {VersionContainer, VersionSection} from "@/components/design-editor/styles";
3-
import {useDesignContext} from "@/components/design-panel/hooks/use-design-context";
4-
import {Badge, Button} from "antd";
5-
import {versions as versionList} from "@/api/workflow";
3+
import {Badge, Button, Input, message, Popconfirm, Popover, Space, Tag, Typography} from "antd";
64
import {HistoryOutlined} from "@ant-design/icons";
5+
import {useVersionPresenter} from "@/components/design-editor/version/hooks/use-version-presenter";
6+
import {WorkflowVersion} from "@/components/design-editor/version/types";
7+
import {VersionPresenter} from "@/components/design-editor/version/presenter";
8+
import dayjs from "dayjs";
79

8-
export const EditorVersion = () => {
10+
const {Text} = Typography;
11+
12+
13+
interface VersionItemProps {
14+
version: WorkflowVersion;
15+
onUpdateVersionName: (id: number, name: string) => Promise<void>;
16+
onVersionChange: (id: number) => void;
17+
}
18+
19+
const VersionItem: React.FC<VersionItemProps> = (props) => {
20+
const {version} = props;
21+
22+
const [editeVisible, setEditeVisible] = React.useState(false);
23+
const versionName = version.versionName;
24+
const versionDate = dayjs(version.updatedTime).format('YYYY-MM-DD');
925

10-
const {state} = useDesignContext();
26+
const [title, setTitle] = React.useState(versionName);
27+
28+
return (
29+
<div style={{
30+
padding: 3,
31+
borderBottom: "1px solid #808080",
32+
}}>
33+
<Space>
34+
{editeVisible && (
35+
<Space>
36+
<Input
37+
value={title}
38+
onChange={(e) => {
39+
setTitle(e.target.value);
40+
}}
41+
placeholder={"请输入版本名称"}
42+
/>
43+
<a onClick={() => {
44+
props.onUpdateVersionName(version.id, title).then(() => {
45+
setEditeVisible(false);
46+
message.success('保存成功')
47+
});
48+
}}>确定</a>
49+
<a onClick={() => {
50+
setEditeVisible(false)
51+
}}>取消</a>
52+
</Space>
53+
)}
54+
{!editeVisible && (
55+
<Space>
56+
<Text>{versionName}</Text>
57+
<Text>({versionDate})</Text>
58+
<a
59+
onClick={() => {
60+
setTitle(versionName);
61+
setEditeVisible(true)
62+
}}
63+
>编辑</a>
64+
{!version.current && (
65+
<Popconfirm
66+
title={"确认要切换到该版本吗?"}
67+
onConfirm={() => {
68+
props.onVersionChange(version.id);
69+
}}
70+
>
71+
<a>切换</a>
72+
</Popconfirm>
73+
74+
)}
75+
76+
{version.current && (
77+
<Tag color={'success'}>当前版本</Tag>
78+
)}
79+
</Space>
80+
)}
81+
</Space>
82+
</div>
83+
)
84+
}
1185

12-
const [versions, setVersions] = React.useState<any[]>([]);
1386

14-
React.useEffect(() => {
15-
versionList(state.workflow.id).then(res => {
16-
setVersions(res.data.list);
17-
})
18-
}, [state.workflow.id])
87+
interface VersionContentProps {
88+
versions: WorkflowVersion[];
89+
presenter: VersionPresenter;
90+
}
91+
92+
const VersionContent: React.FC<VersionContentProps> = (props) => {
93+
return (
94+
<div style={{
95+
padding: "10px",
96+
}}>
97+
{props.versions.map((version: WorkflowVersion) => {
98+
return (
99+
<VersionItem
100+
version={version}
101+
onUpdateVersionName={async (id, name) => {
102+
await props.presenter.updateVersionName(id, name);
103+
}}
104+
onVersionChange={async (versionId) => {
105+
await props.presenter.changeVersion(versionId);
106+
}}
107+
/>
108+
)
109+
})}
110+
</div>
111+
)
112+
}
113+
114+
export const EditorVersion = () => {
115+
116+
const {state, presenter} = useVersionPresenter();
19117

20118
return (
21119
<VersionContainer>
22120
<VersionSection>
23-
<Badge count={versions.length}>
24-
<Button
25-
type={"text"}
26-
icon={<HistoryOutlined/>}
27-
onClick={() => {
28-
console.log(versions);
29-
}}
121+
<Badge count={state.length}>
122+
<Popover
123+
content={<VersionContent versions={state} presenter={presenter}/>}
124+
trigger="click"
125+
placement="bottom"
30126
>
31-
版本
32-
</Button>
127+
<Button
128+
type={"text"}
129+
icon={<HistoryOutlined/>}
130+
onClick={() => {
131+
console.log(presenter);
132+
}}
133+
>
134+
版本
135+
</Button>
136+
</Popover>
33137
</Badge>
34138
</VersionSection>
35139
</VersionContainer>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from "react";
2+
import {VersionPresenter} from "@/components/design-editor/version/presenter";
3+
import {VersionApiImpl} from "@/components/design-editor/version/model";
4+
import {useDesignContext} from "@/components/design-panel/hooks/use-design-context";
5+
6+
7+
export const useVersionPresenter = () => {
8+
9+
const {state} = useDesignContext();
10+
const workId = state.workflow.id;
11+
const ref = React.useRef<VersionPresenter>(undefined);
12+
13+
const [versions, setVersions] = React.useState<any[]>([]);
14+
15+
if (!ref.current) {
16+
ref.current = new VersionPresenter(workId,versions, setVersions, new VersionApiImpl());
17+
}
18+
19+
React.useEffect(() => {
20+
ref.current?.syncState(versions);
21+
}, [versions]);
22+
23+
React.useEffect(() => {
24+
ref.current?.initState();
25+
}, []);
26+
27+
return {
28+
state: versions,
29+
presenter: ref.current,
30+
};
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {VersionApi, WorkflowVersion} from "./types";
2+
import {updateVersionName as updateVersionNameApi, versions, changeVersion as changeVersionApi} from "@/api/workflow";
3+
4+
export class VersionApiImpl implements VersionApi {
5+
6+
loadVersions = async (workId: string): Promise<WorkflowVersion[]> => {
7+
const result = await versions(workId);
8+
if (result.success) {
9+
return result.data.list;
10+
}
11+
return []
12+
}
13+
14+
updateVersionName = async (id: number, versionName: string) => {
15+
await updateVersionNameApi({
16+
id,
17+
versionName
18+
})
19+
}
20+
21+
changeVersion = async (id: number) => {
22+
await changeVersionApi(id);
23+
}
24+
25+
}

0 commit comments

Comments
 (0)