刘光辉
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package jnpf.dmsService;
 
import jnpf.dmsEntity.DmsWenjianBanbenEntity;
import jnpf.dmsEntity.DmsWenjianZiyuanEntity;
import jnpf.dmsEntity.permission.entity.DmsFileVersionEntity;
import jnpf.dmsEntity.storage.entity.DmsFileObjectEntity;
import jnpf.dmsMapper.DmsFileObjectMapper;
import jnpf.dmsMapper.DmsFileVersionMapper;
import jnpf.dmsMapper.DmsWenjianBanbenMapper;
import jnpf.dmsMapper.DmsWenjianZiyuanMapper;
import jnpf.exception.DataException;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.util.UUID;
 
@Service
@RequiredArgsConstructor
public class DmsWorkflowFileResolutionService {
    private final DmsWenjianZiyuanMapper businessFileMapper;
    private final DmsWenjianBanbenMapper businessVersionMapper;
    private final DmsFileVersionMapper fileVersionMapper;
    private final DmsFileObjectMapper fileObjectMapper;
 
    public ResolvedFile resolve(String tenantId, String detailId, String fileReferenceId,
                                String fileNumber, String fileName, String versionLabel) {
        DmsWenjianZiyuanEntity businessFile = resolveBusinessFile(
                tenantId, detailId, fileReferenceId, fileNumber);
        String requestedNumber = trimToNull(fileNumber);
        if (requestedNumber != null && !requestedNumber.equals(businessFile.getWenjianBianhao())) {
            throw new DataException("申请明细的文件编号与文件资产不一致:" + detailId);
        }
        String requestedName = trimToNull(fileName);
        if (requestedName != null && !requestedName.equals(businessFile.getWenjianMingcheng())) {
            throw new DataException("申请明细的文件名称与文件资产不一致:" + detailId);
        }
 
        String requestedVersion = trimToNull(versionLabel);
        DmsWenjianBanbenEntity businessVersion = requestedVersion == null
                ? businessVersionMapper.selectById(tenantId,
                        requiredText(businessFile.getDangqianBanbenId(), "文件资产缺少当前版本"))
                : businessVersionMapper.selectByFileAndVersionLabel(
                        tenantId, businessFile.getId(), requestedVersion);
        if (businessVersion == null) {
            throw new DataException("申请对应的业务文件版本不存在:" + detailId);
        }
        UUID fileVersionId;
        try {
            fileVersionId = UUID.fromString(requiredText(businessVersion.getWenjianZhongxinBanbenId(),
                    "业务文件版本尚未关联文件中心版本"));
        } catch (IllegalArgumentException exception) {
            throw new DataException("业务文件版本关联的文件中心版本ID无效:" + detailId);
        }
        DmsFileVersionEntity fileVersion = fileVersionMapper.selectReadableByTenantAndId(
                tenantId, fileVersionId);
        DmsFileObjectEntity fileObject = fileObjectMapper.selectByTenantAndFileVersionId(
                tenantId, fileVersionId);
        if (fileVersion == null || fileObject == null) {
            throw new DataException("申请对应的文件中心版本或存储对象不存在:" + detailId);
        }
        String documentId = trimToNull(businessFile.getWenjianZhongxinWenjianId());
        if (documentId != null && !fileVersion.getDocumentId().toString().equals(documentId)) {
            throw new DataException("业务文件与文件中心版本不匹配:" + detailId);
        }
        return new ResolvedFile(businessFile, businessVersion, fileVersion);
    }
 
    private DmsWenjianZiyuanEntity resolveBusinessFile(
            String tenantId, String detailId, String fileReferenceId, String fileNumber) {
        String referenceId = trimToNull(fileReferenceId);
        DmsWenjianZiyuanEntity result = null;
        if (referenceId != null) {
            result = businessFileMapper.selectByIdForUpdate(tenantId, referenceId);
            if (result == null) {
                result = businessFileMapper.selectActiveByNumberDetailForUpdate(tenantId, referenceId);
            }
        }
        if (result == null) {
            String requestedNumber = requiredText(fileNumber,
                    "申请明细无法定位文件资产:" + detailId);
            result = businessFileMapper.selectActiveByNumberForUpdate(tenantId, requestedNumber);
        }
        if (result == null) {
            throw new DataException("申请明细关联的文件资产不存在:" + detailId);
        }
        return result;
    }
 
    private String requiredText(String value, String message) {
        String normalized = trimToNull(value);
        if (normalized == null) {
            throw new DataException(message);
        }
        return normalized;
    }
 
    private String trimToNull(String value) {
        return value == null || value.trim().isEmpty() ? null : value.trim();
    }
 
    @Getter
    @RequiredArgsConstructor
    public static final class ResolvedFile {
        private final DmsWenjianZiyuanEntity businessFile;
        private final DmsWenjianBanbenEntity businessVersion;
        private final DmsFileVersionEntity fileVersion;
    }
}