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> list(ElnFieldForms.Query query) { if (query == null || query.getCurrentPage() < 1 || query.getPageSize() < 1) { return ActionResult.fail("分页参数必须大于 0"); } trimQuery(query); IPage page = fieldCatalogService.page(query); PageListVO 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 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 detail(@PathVariable("id") String id) { if (!StringUtils.hasText(id)) { return ActionResult.fail("字段 id 不能为空"); } return ActionResult.success(fieldCatalogService.detail(id.trim())); } @GetMapping("/providerProperties") public ActionResult> 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 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 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 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 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 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 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 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 promoteGlobal( @PathVariable("id") String id, @RequestBody(required = false) ElnFieldForms.Reason form) { return changeField(id, form, FieldChange.PROMOTE_GLOBAL); } @PostMapping("/{id}/enable") public ActionResult enable( @PathVariable("id") String id, @RequestBody(required = false) ElnFieldForms.Reason form) { return changeField(id, form, FieldChange.ENABLE); } @PostMapping("/{id}/disable") public ActionResult disable( @PathVariable("id") String id, @RequestBody(required = false) ElnFieldForms.Reason form) { return changeField(id, form, FieldChange.DISABLE); } private ActionResult 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 } }