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 public class TReportTemplateServiceImpl implements ITReportTemplateService { @Autowired private TReportTemplateMapper tReportTemplateMapper; @Autowired private ISysUserExtendService sysUserExtendService; @Autowired private ISysUserPaperService userPaperService; @Autowired private ITExamPaperService paperService; @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 ""; } @Override public List selectTReportTemplateList(TReportTemplate tReportTemplate) { return tReportTemplateMapper.selectTReportTemplateList(tReportTemplate); } @Override public List getReportTemplates(User user) { return getReportTemplates(user, null); } @Override public List getReportTemplates(User user, Long prodId) { if (Objects.nonNull(prodId)) { TExamPaper tExamPaper = paperService.selectTExamPaperById(prodId); String reportTemplateId = tExamPaper.getReportTemplateId(); List reportTemplates = selectTReportTemplates(reportTemplateId); List 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 sysUserPapers = userPaperService.selectSysUserPaperList(sysUserPaper); List templates = new ArrayList<>(); for (SysUserPaper paper : sysUserPapers) { String rid = paper.getReportTemplateId(); if (StringUtils.isBlank(rid)) { continue; } templates.addAll(selectTReportTemplates(rid)); } List collect = ret.stream().map(pp -> pp.getId()).collect(Collectors.toList()); List resultTemplateList = new ArrayList<>(); for (TReportTemplate template : templates) { if (collect.contains(template.getId())) { resultTemplateList.add(template); } } List 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 langTypes = Arrays.asList(sysUserExtend.getLangType().split(",")); tReportTemplates = tReportTemplates.stream().filter(f -> langTypes.contains(f.getLangType())).collect(Collectors.toList()); } List 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 tReportTemplates = tReportTemplateMapper.selectTReportTemplateList(template); List 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 sysUserPapers = userPaperService.selectSysUserPaperList(sysUserPaper); List templates = new ArrayList<>(); for (SysUserPaper paper : sysUserPapers) { String reportTemplateId = paper.getReportTemplateId(); if (StringUtils.isBlank(reportTemplateId)) { continue; } templates.addAll(selectTReportTemplates(reportTemplateId)); } List 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 langTypes = Arrays.asList(sysUserExtend.getLangType().split(",")); tReportTemplates = tReportTemplates.stream().filter(f -> langTypes.contains(f.getLangType())).collect(Collectors.toList()); } } List 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 selectTReportTemplates(String ids) { if (Objects.isNull(ids)) { return new ArrayList<>(); } return tReportTemplateMapper.selectTReportTemplates(Convert.toStrArray(ids)); } @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); } @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); } @Override public int deleteTReportTemplateByIds(String ids) { return tReportTemplateMapper.deleteTReportTemplateByIds(Convert.toStrArray(ids)); } public int deleteTReportTemplateById(Long id) { return tReportTemplateMapper.deleteTReportTemplateById(id); } }