刘光辉
3 小时以前 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
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<String> tenantIdSupplier;
 
    @Autowired
    public ElnTemplateFileQueryServiceImpl(ElnTemplateMapper templateMapper) {
        this(templateMapper::selectLatestTemplateFiles,
                templateMapper::selectLatestTemplateFilesByTestDefinitionCode,
                ElnTemplateFileQueryServiceImpl::dangqianZuhuId);
    }
 
    ElnTemplateFileQueryServiceImpl(TemplateFileQuery templateFileQuery,
                                    TemplateFileQuery templateFileCodeQuery,
                                    Supplier<String> 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<String> 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<ElnDocumentFileVo> 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<String> selectLatestTemplateFiles(IPage<String> page, String tenantId,
                                                String jianyanXiangmu);
    }
}