package com.ots.project.exam.service.impl;
|
|
import com.ots.common.enums.UserTypeEnum;
|
import com.ots.common.utils.DateUtils;
|
import com.ots.common.utils.StringUtils;
|
import com.ots.common.utils.file.FileUploadUtils;
|
import com.ots.common.utils.text.Convert;
|
import com.ots.framework.config.EssConfig;
|
import com.ots.project.exam.domain.SysUserExtend;
|
import com.ots.project.exam.domain.SysUserPaper;
|
import com.ots.project.exam.domain.TExamPaper;
|
import com.ots.project.exam.domain.TReportTemplate;
|
import com.ots.project.exam.mapper.TReportTemplateMapper;
|
import com.ots.project.exam.service.ISysUserExtendService;
|
import com.ots.project.exam.service.ISysUserPaperService;
|
import com.ots.project.exam.service.ITExamPaperService;
|
import com.ots.project.exam.service.ITReportTemplateService;
|
import com.ots.project.system.user.domain.User;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.io.IOException;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* 报告模板配置Service业务层处理
|
*
|
* @author ots
|
* @date 2020-03-24
|
*/
|
@Service
|
public class TReportTemplateServiceImpl implements ITReportTemplateService {
|
@Autowired
|
private TReportTemplateMapper tReportTemplateMapper;
|
|
@Autowired
|
private ISysUserExtendService sysUserExtendService;
|
|
@Autowired
|
private ISysUserPaperService userPaperService;
|
|
@Autowired
|
private ITExamPaperService paperService;
|
|
/**
|
* 查询报告模板配置
|
*
|
* @param id 报告模板配置ID
|
* @return 报告模板配置
|
*/
|
@Override
|
public TReportTemplate selectTReportTemplateById(Long id) {
|
return tReportTemplateMapper.selectTReportTemplateById(id);
|
}
|
|
@Override
|
public String selectTReportTemplateFilePath(TReportTemplate tReportTemplate) {
|
TReportTemplate tReportTemplateResult = tReportTemplateMapper.selectTReportTemplate(tReportTemplate);
|
if (Objects.nonNull(tReportTemplateResult)) {
|
return tReportTemplateResult.getLocation();
|
}
|
return "";
|
}
|
|
/**
|
* 查询报告模板配置列表
|
*
|
* @param tReportTemplate 报告模板配置
|
* @return 报告模板配置
|
*/
|
@Override
|
public List<TReportTemplate> selectTReportTemplateList(TReportTemplate tReportTemplate) {
|
return tReportTemplateMapper.selectTReportTemplateList(tReportTemplate);
|
}
|
|
@Override
|
public List<TReportTemplate> getReportTemplates(User user) {
|
return getReportTemplates(user, null);
|
}
|
|
@Override
|
public List<TReportTemplate> getReportTemplates(User user, Long prodId) {
|
|
if (Objects.nonNull(prodId)) {
|
TExamPaper tExamPaper = paperService.selectTExamPaperById(prodId);
|
String reportTemplateId = tExamPaper.getReportTemplateId();
|
List<TReportTemplate> reportTemplates = selectTReportTemplates(reportTemplateId);
|
List<TReportTemplate> ret = reportTemplates.stream().map(item -> {
|
String value = item.getReportType() + "-" + item.getTemplateType() + "-" + item.getLangType();
|
item.setReportType(value);
|
return item;
|
}).collect(Collectors.toList());
|
|
//取用户授权和产品包配置的交集
|
if (!Objects.equals(UserTypeEnum.SYS_USER.getUserType(), user.getUserType())) {
|
SysUserExtend sysUserExtend = sysUserExtendService.selectSysUserExtendById(user.getUserId());
|
//查询产品包授权分配的报告模板
|
SysUserPaper sysUserPaper = new SysUserPaper();
|
sysUserPaper.setUserId(user.getUserId());
|
sysUserPaper.setProdId(prodId);
|
|
List<SysUserPaper> sysUserPapers = userPaperService.selectSysUserPaperList(sysUserPaper);
|
|
List<TReportTemplate> templates = new ArrayList<>();
|
for (SysUserPaper paper : sysUserPapers) {
|
String rid = paper.getReportTemplateId();
|
if (StringUtils.isBlank(rid)) {
|
continue;
|
}
|
templates.addAll(selectTReportTemplates(rid));
|
}
|
|
List<Long> collect = ret.stream().map(pp -> pp.getId()).collect(Collectors.toList());
|
List<TReportTemplate> resultTemplateList = new ArrayList<>();
|
for (TReportTemplate template : templates) {
|
if (collect.contains(template.getId())) {
|
resultTemplateList.add(template);
|
}
|
}
|
List<TReportTemplate> tReportTemplates = resultTemplateList.stream().collect(Collectors.collectingAndThen
|
(Collectors.toCollection(() ->
|
new TreeSet<>(Comparator.comparing(t -> t.getLocation()))),
|
ArrayList::new
|
)
|
);
|
//根据企业用户配置的支持语言类型过滤
|
if (Objects.nonNull(sysUserExtend) && Objects.nonNull(sysUserExtend.getLangType())) {
|
List<String> langTypes = Arrays.asList(sysUserExtend.getLangType().split(","));
|
tReportTemplates = tReportTemplates.stream().filter(f -> langTypes.contains(f.getLangType())).collect(Collectors.toList());
|
}
|
List<TReportTemplate> result = tReportTemplates.stream().map(item -> {
|
String value = item.getReportType() + "-" + item.getTemplateType() + "-" + item.getLangType();
|
item.setReportType(value);
|
return item;
|
}).collect(Collectors.toList());
|
return result;
|
}
|
return ret;
|
}
|
|
if (Objects.equals(UserTypeEnum.SYS_USER.getUserType(), user.getUserType())) {
|
TReportTemplate template = new TReportTemplate();
|
List<TReportTemplate> tReportTemplates = tReportTemplateMapper.selectTReportTemplateList(template);
|
List<TReportTemplate> ret = tReportTemplates.stream().map(item -> {
|
String value = item.getReportType() + "-" + item.getTemplateType() + "-" + item.getLangType();
|
item.setReportType(value);
|
return item;
|
}).collect(Collectors.toList());
|
return ret;
|
}
|
|
//查询产品包授权分配的报告模板
|
SysUserPaper sysUserPaper = new SysUserPaper();
|
if (!Objects.equals(UserTypeEnum.SYS_USER.getUserType(), user.getUserType())) {
|
sysUserPaper.setUserId(user.getUserId());
|
}
|
List<SysUserPaper> sysUserPapers = userPaperService.selectSysUserPaperList(sysUserPaper);
|
|
List<TReportTemplate> templates = new ArrayList<>();
|
for (SysUserPaper paper : sysUserPapers) {
|
String reportTemplateId = paper.getReportTemplateId();
|
if (StringUtils.isBlank(reportTemplateId)) {
|
continue;
|
}
|
templates.addAll(selectTReportTemplates(reportTemplateId));
|
}
|
|
//对象去重
|
// List<TReportTemplate> tReportTemplates = templates.stream().distinct().collect(Collectors.toList());
|
List<TReportTemplate> tReportTemplates = templates.stream().collect(Collectors.collectingAndThen
|
(Collectors.toCollection(() ->
|
new TreeSet<>(Comparator.comparing(t -> t.getLocation()))),
|
ArrayList::new
|
)
|
);
|
//根据企业用户配置的支持语言类型过滤
|
if (Objects.equals(UserTypeEnum.ENT_USER.getUserType(), user.getUserType())) {
|
SysUserExtend sysUserExtend = sysUserExtendService.selectSysUserExtendById(user.getUserId());
|
if (Objects.nonNull(sysUserExtend) && Objects.nonNull(sysUserExtend.getLangType())) {
|
List<String> langTypes = Arrays.asList(sysUserExtend.getLangType().split(","));
|
tReportTemplates = tReportTemplates.stream().filter(f -> langTypes.contains(f.getLangType())).collect(Collectors.toList());
|
}
|
}
|
List<TReportTemplate> result = tReportTemplates.stream().map(item -> {
|
String value = item.getReportType() + "-" + item.getTemplateType() + "-" + item.getLangType();
|
item.setReportType(value);
|
return item;
|
}).collect(Collectors.toList());
|
|
return result;
|
}
|
|
@Override
|
public List<TReportTemplate> selectTReportTemplates(String ids) {
|
if (Objects.isNull(ids)) {
|
return new ArrayList<>();
|
}
|
return tReportTemplateMapper.selectTReportTemplates(Convert.toStrArray(ids));
|
}
|
|
|
/**
|
* 新增报告模板配置
|
*
|
* @param tReportTemplate 报告模板配置
|
* @return 结果
|
*/
|
@Override
|
public int insertTReportTemplate(TReportTemplate tReportTemplate) {
|
tReportTemplate.setCreateTime(DateUtils.getNowDate());
|
MultipartFile locationFile = tReportTemplate.getLocationFile();
|
if (locationFile != null && !locationFile.isEmpty()) {
|
try {
|
String ufi = FileUploadUtils.upload(EssConfig.getUploadPath(), locationFile);
|
tReportTemplate.setLocation(ufi.split("profile/upload")[1]);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return tReportTemplateMapper.insertTReportTemplate(tReportTemplate);
|
}
|
|
/**
|
* 修改报告模板配置
|
*
|
* @param tReportTemplate 报告模板配置
|
* @return 结果
|
*/
|
@Override
|
public int updateTReportTemplate(TReportTemplate tReportTemplate) {
|
tReportTemplate.setUpdateTime(DateUtils.getNowDate());
|
MultipartFile locationFile = tReportTemplate.getLocationFile();
|
if (locationFile != null && !locationFile.isEmpty()) {
|
try {
|
String uploadPath = EssConfig.getUploadPath();
|
String ufi = FileUploadUtils.upload(uploadPath, locationFile);
|
tReportTemplate.setLocation(ufi.split("profile/upload")[1]);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return tReportTemplateMapper.updateTReportTemplate(tReportTemplate);
|
}
|
|
/**
|
* 删除报告模板配置对象
|
*
|
* @param ids 需要删除的数据ID
|
* @return 结果
|
*/
|
@Override
|
public int deleteTReportTemplateByIds(String ids) {
|
return tReportTemplateMapper.deleteTReportTemplateByIds(Convert.toStrArray(ids));
|
}
|
|
/**
|
* 删除报告模板配置信息
|
*
|
* @param id 报告模板配置ID
|
* @return 结果
|
*/
|
public int deleteTReportTemplateById(Long id) {
|
return tReportTemplateMapper.deleteTReportTemplateById(id);
|
}
|
}
|