package jnpf.elnService; import lombok.Value; import jnpf.base.ActionResult; import jnpf.base.ActionResultCode; import jnpf.exception.DataException; import jnpf.lims.api.ElnSourceCatalogApi; import jnpf.lims.api.model.ElnSourceReference; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public interface ElnFieldProviderCatalog { List list(String source); boolean contains(String source, String property); default Property reference(String source, String property, Integer revision) { return list(source).stream().filter(item -> java.util.Objects.equals(property, item.getProperty()) && java.util.Objects.equals(revision, item.getPropertyRevision())).findFirst().orElse(null); } @Value @lombok.AllArgsConstructor class Property { String source; String property; String label; String dataType; boolean readOnly; Integer propertyRevision; boolean enabled; public Property(String source, String property, String label, String dataType, boolean readOnly) { this(source, property, label, dataType, readOnly, null, true); } } } @Component class DefaultElnFieldProviderCatalog implements ElnFieldProviderCatalog { private final ElnSourceCatalogApi remote; @Autowired public DefaultElnFieldProviderCatalog(ElnSourceCatalogApi remote) { this.remote = remote; } DefaultElnFieldProviderCatalog() { this(null); } private static final List PROPERTIES = Collections.unmodifiableList(Arrays.asList( property("LIMS_TASK", "test.basis", "检验依据", "TEXT"), property("LIMS_TASK", "sample.name", "供试品名称", "TEXT"), property("LIMS_TASK", "sample.batch_no", "批号", "TEXT"), property("LIMS_TASK", "sample.production_date", "生产日期", "DATE"), property("LIMS_TASK", "quality_standard.source", "质量标准来源", "TEXT"), property("LIMS_TASK", "sample.specification", "规格/浓度", "TEXT"), property("LIMS_TASK", "test.type", "检验类型", "TEXT"), property("SYSTEM", "current_user.id", "当前用户 ID", "TEXT"), property("SYSTEM", "current_user.real_name", "当前用户姓名", "TEXT"), property("SYSTEM", "current_date", "当前日期", "DATE"), property("SYSTEM", "current_datetime", "当前日期时间", "DATETIME") )); @Override public List list(String source) { if ("LIMS_TASK".equals(source) && remote != null) { ActionResult> response = remote.references(); if (response == null || !ActionResultCode.SUCCESS.getCode().equals(response.getCode()) || response.getData() == null) throw new DataException("LIMS 来源目录不可用"); List result = new ArrayList<>(); for (ElnSourceReference item : response.getData()) { result.add(new Property("LIMS_TASK", item.getPropertyCode(), item.getDisplayName(), item.getDataType(), true, item.getRevision(), item.isEnabled())); } return result; } if (source == null) { return Collections.emptyList(); } List result = new ArrayList<>(); for (Property property : PROPERTIES) { if (source.equals(property.getSource())) { result.add(property); } } return result; } @Override public Property reference(String source, String property, Integer revision) { if (revision == null) { // Legacy unversioned references are explicit compatibility, not a failure fallback. return PROPERTIES.stream().filter(item -> java.util.Objects.equals(source, item.getSource()) && java.util.Objects.equals(property, item.getProperty())).findFirst().orElse(null); } if (!"LIMS_TASK".equals(source) || revision < 1 || remote == null) return null; ActionResult response = remote.reference(property, revision); if (response == null || !ActionResultCode.SUCCESS.getCode().equals(response.getCode()) || response.getData() == null) throw new DataException("LIMS 固定来源版本不可用"); ElnSourceReference item = response.getData(); if (!java.util.Objects.equals(property, item.getPropertyCode()) || !revision.equals(item.getRevision())) { throw new DataException("LIMS 返回的来源版本不一致"); } return new Property(source, item.getPropertyCode(), item.getDisplayName(), item.getDataType(), true, item.getRevision(), item.isEnabled()); } @Override public boolean contains(String source, String property) { if (source == null || property == null) { return false; } for (Property item : list(source)) { if (source.equals(item.getSource()) && property.equals(item.getProperty())) { return true; } } return false; } private static Property property(String source, String property, String label, String dataType) { return new Property(source, property, label, dataType, true); } }