刘光辉
5 小时以前 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
package jnpf.dmsService.center;
 
import jnpf.dmsIntegration.audit.DmsBusinessChangeAudit;
import static jnpf.dmsIntegration.audit.DmsBusinessChangeAudit.values;
 
import jnpf.dmsEntity.permission.entity.DmsDocumentEntity;
import jnpf.dmsMapper.DmsDocumentMapper;
import jnpf.exception.DataException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
 
/** Dedicated logical directory ownership. Historical records retain their membership. */
@Service
@RequiredArgsConstructor
public class DmsCenterDirectoryService {
    private final DmsBusinessChangeAudit businessAudit;
    private final DmsDocumentMapper documents;
 
    @Transactional(rollbackFor = Exception.class)
    public void bind(String tenantId, UUID mainDocumentId, Collection<UUID> recordDocumentIds) {
        if (tenantId == null || tenantId.isBlank() || mainDocumentId == null || recordDocumentIds == null
                || recordDocumentIds.stream().anyMatch(Objects::isNull) || recordDocumentIds.contains(mainDocumentId))
            throw new DataException("文件目录归属请求无效");
        Set<UUID> ids = new TreeSet<>(); ids.add(mainDocumentId); ids.addAll(recordDocumentIds);
        Map<UUID, DmsDocumentEntity> locked = new HashMap<>();
        for (UUID id : ids) {
            DmsDocumentEntity document = documents.selectByTenantAndIdForUpdate(tenantId, id);
            if (document == null) throw new DataException("文件目录成员不存在");
            locked.put(id, document);
        }
        DmsDocumentEntity main = locked.get(mainDocumentId);
        if (main.getMainDocumentId() != null) throw new DataException("记录文件不能作为主文件目录");
        for (UUID id : recordDocumentIds) {
            DmsDocumentEntity record = locked.get(id);
            if (!Objects.equals(main.getFolderId(), record.getFolderId()))
                throw new DataException("记录与主文件的上级目录不一致");
            if (record.getMainDocumentId() != null) {
                if (!mainDocumentId.equals(record.getMainDocumentId()))
                    throw new DataException("记录已属于其他主文件目录");
                continue;
            }
            if (documents.countRecords(tenantId, id) != 0)
                throw new DataException("已有记录的主文件不能转为记录");
            if (documents.bindMainDocument(tenantId, id, mainDocumentId) != 1)
                throw new DataException("记录目录归属发生变化");
            businessAudit.record(tenantId, "DMS_CENTER_BIND_DIRECTORY", "记录文件归属绑定", "dms_documents", id.toString(),
                    values("main_document_id", record.getMainDocumentId()), values("main_document_id", mainDocumentId),
                    null, values("document_id", id, "main_document_id", mainDocumentId));
        }
    }
}