刘光辉
4 小时以前 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
package jnpf.elnService.template.impl;
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.database.util.TenantDataSourceUtil;
import jnpf.elnEntity.ElnDocumentFileVo;
import jnpf.elnEntity.template.ElnTemplateVersionEntity;
import jnpf.elnMapper.ElnTemplateMapper;
import jnpf.elnService.template.ElnTemplateEditabilityService;
import jnpf.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
@Service
public class ElnTemplateEditabilityServiceImpl implements ElnTemplateEditabilityService {
 
    private static final String DRAFT = "DRAFT";
    private static final String ENABLED = "enabled";
 
    private final TemplateLookup templateLookup;
    private final TenantRoute tenantRoute;
 
    @Autowired
    public ElnTemplateEditabilityServiceImpl(ElnTemplateMapper templateMapper) {
        this((tenantId, templateId) -> templateMapper.selectOne(
                Wrappers.<ElnTemplateVersionEntity>lambdaQuery()
                        .eq(ElnTemplateVersionEntity::getFTenantId, tenantId)
                        .eq(ElnTemplateVersionEntity::getId, templateId)
                        .last("limit 1")), new TenantRoute() {
            @Override
            public void switchTo(String tenantId) {
                TenantDataSourceUtil.switchTenant(tenantId);
            }
 
            @Override
            public void clear() {
                TenantDataSourceUtil.clearLocalTenantInfo();
            }
        });
    }
 
    ElnTemplateEditabilityServiceImpl(TemplateLookup templateLookup, TenantRoute tenantRoute) {
        this.templateLookup = templateLookup;
        this.tenantRoute = tenantRoute;
    }
 
    @Override
    public boolean isEditable(String tenantId, String templateId, String fileId) {
        boolean editable = false;
        try {
            if (!StringUtils.hasText(tenantId)
                    || !StringUtils.hasText(templateId)
                    || !StringUtils.hasText(fileId)) {
                return false;
            }
            tenantRoute.switchTo(tenantId);
            ElnTemplateVersionEntity template = templateLookup.find(tenantId, templateId);
            editable = matchesTemplate(template, tenantId, templateId, fileId);
        } catch (RuntimeException ignored) {
            editable = false;
        } finally {
            try {
                tenantRoute.clear();
            } catch (RuntimeException ignored) {
                editable = false;
            }
        }
        return editable;
    }
 
    private boolean matchesTemplate(ElnTemplateVersionEntity template, String tenantId,
                                    String templateId, String fileId) {
        if (template == null
                || !Objects.equals(tenantId, template.getFTenantId())
                || !Objects.equals(templateId, template.getId())
                || (template.getFDeleteMark() != null && template.getFDeleteMark() != 0)
                || !DRAFT.equals(template.getBizStatus())
                || !ENABLED.equals(template.getBizEnabled())) {
            return false;
        }
        List<ElnDocumentFileVo> files = JsonUtil.getJsonToList(template.getMubanFile(), ElnDocumentFileVo.class);
        if (files == null) {
            return false;
        }
        List<ElnDocumentFileVo> docxFiles = files.stream()
                .filter(Objects::nonNull)
                .filter(file -> "docx".equalsIgnoreCase(file.getFileExtension()))
                .collect(Collectors.toList());
        return docxFiles.size() == 1 && Objects.equals(fileId, docxFiles.get(0).getFileId());
    }
 
    @FunctionalInterface
    interface TemplateLookup {
        ElnTemplateVersionEntity find(String tenantId, String templateId);
    }
 
    interface TenantRoute {
        void switchTo(String tenantId);
 
        void clear();
    }
}