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();
|
}
|
}
|