package jnpf.elnService.impl; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import jnpf.base.UserInfo; import jnpf.elnEntity.ElnDocumentFileVo; import jnpf.elnMapper.ElnTemplateMapper; import jnpf.elnService.ElnTemplateFileQueryService; import jnpf.exception.DataException; import jnpf.util.JsonUtil; import jnpf.util.UserProvider; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import java.util.List; import java.util.function.Supplier; @Service public class ElnTemplateFileQueryServiceImpl implements ElnTemplateFileQueryService { private final TemplateFileQuery templateFileQuery; private final TemplateFileQuery templateFileCodeQuery; private final Supplier tenantIdSupplier; @Autowired public ElnTemplateFileQueryServiceImpl(ElnTemplateMapper templateMapper) { this(templateMapper::selectLatestTemplateFiles, templateMapper::selectLatestTemplateFilesByTestDefinitionCode, ElnTemplateFileQueryServiceImpl::dangqianZuhuId); } ElnTemplateFileQueryServiceImpl(TemplateFileQuery templateFileQuery, TemplateFileQuery templateFileCodeQuery, Supplier tenantIdSupplier) { this.templateFileQuery = templateFileQuery; this.templateFileCodeQuery = templateFileCodeQuery; this.tenantIdSupplier = tenantIdSupplier; } @Override public ElnDocumentFileVo getFile(String jianyanXiangmu, String jianyanXiangmuBianma) { boolean hasId = StringUtils.hasText(jianyanXiangmu); boolean hasCode = StringUtils.hasText(jianyanXiangmuBianma); if (!hasId && !hasCode) { throw new DataException("检验项目 ID 或编码不能为空"); } if (hasId && hasCode) { throw new DataException("检验项目 ID 与编码只能传一个"); } TemplateFileQuery query = hasCode ? templateFileCodeQuery : templateFileQuery; String projectReference = hasCode ? jianyanXiangmuBianma.trim() : jianyanXiangmu.trim(); IPage page = query.selectLatestTemplateFiles( new Page<>(1, 1, false), tenantIdSupplier.get(), projectReference); String mubanFile = page.getRecords().isEmpty() ? null : page.getRecords().get(0); if (!StringUtils.hasText(mubanFile)) { throw new DataException("未配置当前模板"); } List files; try { files = JsonUtil.getJsonToList(mubanFile, ElnDocumentFileVo.class); } catch (RuntimeException e) { throw new DataException("电子记录模板文件数据格式错误"); } if (files == null) { throw new DataException("电子记录模板文件数据格式错误"); } if (files.isEmpty()) { return null; } ElnDocumentFileVo file = files.get(0); if (file == null) { throw new DataException("电子记录模板文件数据格式错误"); } return file; } private static String dangqianZuhuId() { UserInfo user = UserProvider.getUser(); return user == null || !StringUtils.hasText(user.getTenantId()) ? "0" : user.getTenantId().trim(); } @FunctionalInterface interface TemplateFileQuery { IPage selectLatestTemplateFiles(IPage page, String tenantId, String jianyanXiangmu); } }