刘光辉
5 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package jnpf.elnController;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.tags.Tag;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.elnEntity.field.ElnFieldForms;
import jnpf.elnEntity.field.ElnFieldVos;
import jnpf.elnEntity.template.ElnTemplateForms;
import jnpf.elnEntity.template.ElnTemplateVos;
import jnpf.elnService.ElnFieldCatalogService;
import jnpf.elnService.ElnFieldProviderCatalog;
import jnpf.elnService.template.ElnTemplateCodeAssistService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.List;
 
@Tag(name = "ELN字段目录")
@RestController
@RequestMapping("/fieldCatalog")
@RequiredArgsConstructor
public class ElnFieldCatalogController {
 
    private final ElnFieldCatalogService fieldCatalogService;
    private final ElnFieldProviderCatalog providerCatalog;
    private final ElnTemplateCodeAssistService codeAssistService;
 
    @GetMapping
    public ActionResult<PageListVO<ElnFieldVos.ListItem>> list(ElnFieldForms.Query query) {
        if (query == null || query.getCurrentPage() < 1 || query.getPageSize() < 1) {
            return ActionResult.fail("分页参数必须大于 0");
        }
        trimQuery(query);
        IPage<ElnFieldVos.ListItem> page = fieldCatalogService.page(query);
        PageListVO<ElnFieldVos.ListItem> result = new PageListVO<>();
        result.setList(page.getRecords());
        PaginationVO pagination = new PaginationVO();
        pagination.setCurrentPage(page.getCurrent());
        pagination.setPageSize(page.getSize());
        pagination.setTotal(Math.toIntExact(page.getTotal()));
        result.setPagination(pagination);
        return ActionResult.success(result);
    }
 
    @PostMapping
    public ActionResult<ElnFieldVos.Detail> create(@RequestBody(required = false) ElnFieldForms.Create form) {
        if (form == null) {
            return ActionResult.fail("字段信息不能为空");
        }
        trimCreate(form);
        return ActionResult.success(fieldCatalogService.create(form));
    }
 
    @GetMapping("/{id}")
    public ActionResult<ElnFieldVos.Detail> detail(@PathVariable("id") String id) {
        if (!StringUtils.hasText(id)) {
            return ActionResult.fail("字段 id 不能为空");
        }
        return ActionResult.success(fieldCatalogService.detail(id.trim()));
    }
 
    @GetMapping("/providerProperties")
    public ActionResult<List<ElnFieldProviderCatalog.Property>> providerProperties(
            @RequestParam(value = "source", required = false) String source) {
        String normalizedSource = trim(source);
        if (!"LIMS_TASK".equals(normalizedSource) && !"SYSTEM".equals(normalizedSource)) {
            return ActionResult.fail("provider source 只允许 LIMS_TASK 或 SYSTEM");
        }
        return ActionResult.success(providerCatalog.list(normalizedSource));
    }
 
    @PostMapping("/codeSuggestions")
    public ActionResult<ElnTemplateVos.CodeSuggestion> suggestCode(
            @RequestBody(required = false) ElnTemplateForms.CodeSuggestion form) {
        if (form == null) {
            return ActionResult.fail("中文名称不能为空");
        }
        ElnTemplateVos.CodeSuggestion result = new ElnTemplateVos.CodeSuggestion();
        result.setCode(codeAssistService.suggest(form.getText()));
        return ActionResult.success(result);
    }
 
    @PostMapping("/{id}/versions")
    public ActionResult<ElnFieldVos.Version> createVersion(
            @PathVariable("id") String id,
            @RequestBody(required = false) ElnFieldForms.VersionCreate form) {
        if (!StringUtils.hasText(id) || form == null) {
            return ActionResult.fail("字段 id 和版本信息不能为空");
        }
        form.setSourceVersionId(trim(form.getSourceVersionId()));
        trimVersionRule(form);
        return ActionResult.success(fieldCatalogService.createVersion(id.trim(), form));
    }
 
    @PutMapping("/versions/{versionId}")
    public ActionResult<ElnFieldVos.Version> updateVersion(
            @PathVariable("versionId") String versionId,
            @RequestBody(required = false) ElnFieldForms.VersionUpdate form) {
        if (!StringUtils.hasText(versionId) || form == null) {
            return ActionResult.fail("字段版本 id 和版本信息不能为空");
        }
        trimVersionRule(form);
        return ActionResult.success(fieldCatalogService.updateDraft(versionId.trim(), form));
    }
 
    @PostMapping("/versions/{versionId}/preRelease")
    public ActionResult<Void> preRelease(@PathVariable("versionId") String versionId) {
        if (!StringUtils.hasText(versionId)) {
            return ActionResult.fail("字段版本 id 不能为空");
        }
        fieldCatalogService.preRelease(versionId.trim());
        return ActionResult.success();
    }
 
    @PostMapping("/versions/{versionId}/publish")
    public ActionResult<Void> publish(@PathVariable("versionId") String versionId) {
        if (!StringUtils.hasText(versionId)) {
            return ActionResult.fail("字段版本 id 不能为空");
        }
        fieldCatalogService.publish(versionId.trim());
        return ActionResult.success();
    }
 
    @PostMapping("/versions/{versionId}/cancel")
    public ActionResult<Void> cancel(
            @PathVariable("versionId") String versionId,
            @RequestBody(required = false) ElnFieldForms.Reason form) {
        if (!StringUtils.hasText(versionId) || !hasReason(form)) {
            return ActionResult.fail("字段版本 id 和操作原因不能为空");
        }
        fieldCatalogService.cancel(versionId.trim(), form.getReason().trim());
        return ActionResult.success();
    }
 
    @PostMapping("/{id}/currentVersion")
    public ActionResult<Void> switchCurrent(
            @PathVariable("id") String id,
            @RequestBody(required = false) ElnFieldForms.SwitchCurrent form) {
        if (!StringUtils.hasText(id) || form == null || !StringUtils.hasText(form.getVersionId())
                || !StringUtils.hasText(form.getReason())) {
            return ActionResult.fail("字段 id、版本 id 和操作原因不能为空");
        }
        fieldCatalogService.switchCurrent(id.trim(), form.getVersionId().trim(), form.getReason().trim());
        return ActionResult.success();
    }
 
    @PostMapping("/{id}/promoteGlobal")
    public ActionResult<Void> promoteGlobal(
            @PathVariable("id") String id,
            @RequestBody(required = false) ElnFieldForms.Reason form) {
        return changeField(id, form, FieldChange.PROMOTE_GLOBAL);
    }
 
    @PostMapping("/{id}/enable")
    public ActionResult<Void> enable(
            @PathVariable("id") String id,
            @RequestBody(required = false) ElnFieldForms.Reason form) {
        return changeField(id, form, FieldChange.ENABLE);
    }
 
    @PostMapping("/{id}/disable")
    public ActionResult<Void> disable(
            @PathVariable("id") String id,
            @RequestBody(required = false) ElnFieldForms.Reason form) {
        return changeField(id, form, FieldChange.DISABLE);
    }
 
    private ActionResult<Void> changeField(String id, ElnFieldForms.Reason form, FieldChange change) {
        if (!StringUtils.hasText(id) || !hasReason(form)) {
            return ActionResult.fail("字段 id 和操作原因不能为空");
        }
        String fieldId = id.trim();
        String reason = form.getReason().trim();
        if (change == FieldChange.PROMOTE_GLOBAL) {
            fieldCatalogService.promoteToGlobal(fieldId, reason);
        } else if (change == FieldChange.ENABLE) {
            fieldCatalogService.enable(fieldId, reason);
        } else {
            fieldCatalogService.disable(fieldId, reason);
        }
        return ActionResult.success();
    }
 
    private static boolean hasReason(ElnFieldForms.Reason form) {
        return form != null && StringUtils.hasText(form.getReason());
    }
 
    private static void trimQuery(ElnFieldForms.Query query) {
        query.setKeyword(trim(query.getKeyword()));
        query.setZuoyongFanwei(trim(query.getZuoyongFanwei()));
        query.setBizEnabled(trim(query.getBizEnabled()));
        query.setMubanBianhao(trim(query.getMubanBianhao()));
    }
 
    private static void trimCreate(ElnFieldForms.Create form) {
        form.setZuoyongFanwei(trim(form.getZuoyongFanwei()));
        form.setBeizhu(trim(form.getBeizhu()));
        trimVersionRule(form);
    }
 
    private static void trimVersionRule(ElnFieldForms.VersionRule form) {
        form.setZiduanMingcheng(trim(form.getZiduanMingcheng()));
        form.setShujuLeixing(trim(form.getShujuLeixing()));
        form.setDanwei(trim(form.getDanwei()));
        form.setPiciMoshi(trim(form.getPiciMoshi()));
        if (form.getBinding() != null && form.getBinding().getRead() != null) {
            ElnFieldForms.Read read = form.getBinding().getRead();
            read.setSource(trim(read.getSource()));
            read.setProperty(trim(read.getProperty()));
        }
        if (form.getBinding() != null && form.getBinding().getWrite() != null) {
            ElnFieldForms.Write write = form.getBinding().getWrite();
            write.setTarget(trim(write.getTarget()));
        }
    }
 
    private static String trim(String value) {
        return value == null ? null : value.trim();
    }
 
    private enum FieldChange {
        PROMOTE_GLOBAL,
        ENABLE,
        DISABLE
    }
}