package com.ruoyi.personnel.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ruoyi.common.core.domain.entity.DepartmentLims;
|
import com.ruoyi.common.core.domain.entity.User;
|
import com.ruoyi.common.numgen.NumberGenerator;
|
import com.ruoyi.common.utils.SecurityUtils;
|
import com.ruoyi.framework.exception.ErrorException;
|
import com.ruoyi.personnel.dto.PersonTrainingDetailedDto;
|
import com.ruoyi.personnel.enumeration.AttachmentType;
|
import com.ruoyi.personnel.excel.PersonTrainingDetailedUpload;
|
import com.ruoyi.personnel.mapper.*;
|
import com.ruoyi.personnel.pojo.*;
|
import com.ruoyi.personnel.service.PersonTrainingDetailedService;
|
import com.ruoyi.system.mapper.DepartmentLimsMapper;
|
import com.ruoyi.system.mapper.UserMapper;
|
import com.ruoyi.system.service.DepartmentLimsService;
|
import lombok.AllArgsConstructor;
|
import org.apache.commons.math3.analysis.function.Power;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.annotation.Resource;
|
import javax.servlet.ServletOutputStream;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.IOException;
|
import java.net.URLEncoder;
|
import java.text.SimpleDateFormat;
|
import java.time.LocalDateTime;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* <p>
|
* 培训计划详情 服务实现类
|
* </p>
|
*
|
* @author 芯导软件(江苏)有限公司
|
* @since 2024-10-11 01:46:27
|
*/
|
@Service
|
@Transactional(rollbackFor = Exception.class)
|
public class PersonTrainingDetailedServiceImpl extends ServiceImpl<PersonTrainingDetailedMapper, PersonTrainingDetailed> implements PersonTrainingDetailedService {
|
|
@Resource
|
private DepartmentLimsService departmentLimsService;
|
|
@Resource
|
private UserMapper userMapper;
|
|
@Resource
|
private PersonTrainingRecordMapper personTrainingRecordMapper;
|
|
@Resource
|
private PersonTrainingMapper personTrainingMapper;
|
|
// private PowerMapper powerMapper;
|
|
@Resource
|
private PersonTrainingFileMapper personTrainingFileMapper;
|
|
@Resource
|
private NumberGenerator<PersonTrainingDetailed> numberGenerator;
|
|
@Resource
|
private DepartmentLimsMapper departmentLimsMapper;
|
|
@Value("${file.path}")
|
private String imgUrl;
|
|
@Value("${excelUrl}")
|
private String excelUrl;
|
|
@Value("${wordUrl}")
|
private String wordUrl;
|
|
/**
|
*
|
* @param file
|
* @param id 年度计划明细表id
|
*/
|
@Override
|
public Map<String,Object> fileUpload(MultipartFile file, Integer id) {
|
HashMap<String, Object> map = new HashMap<>();
|
String fileName = file.getOriginalFilename();
|
|
try {
|
UUID uuid = UUID.randomUUID();
|
String uuidString = uuid.toString();
|
int i = fileName.indexOf(".");
|
String suffix = fileName.substring(i); //文件后缀
|
String url = uuidString + suffix;
|
String OriginalFileName = fileName.substring(0,i); // 文件原名称
|
// 存放路径
|
String path = "";
|
// 根据不同的后缀存放在不同的路径下
|
if(suffix.toLowerCase().contains("xls")) {
|
path = excelUrl;
|
} else if (suffix.toLowerCase().contains("doc") || suffix.toLowerCase().contains("pdf")) {
|
path = wordUrl;
|
}else {
|
path = imgUrl;
|
}
|
File uploadDir = new File(path);
|
if(!uploadDir.exists()){
|
uploadDir.mkdir();
|
}
|
File destFile = new File(uploadDir , url);
|
file.transferTo(destFile);
|
// 将文件路径存储到数据库
|
PersonTrainingFile personTrainingFile = new PersonTrainingFile();
|
personTrainingFile.setDetailId(id);
|
personTrainingFile.setFileName(OriginalFileName + suffix);
|
personTrainingFile.setFileUrl("/"+url);
|
personTrainingFile.setCreateUser(SecurityUtils.getUserId().intValue());
|
personTrainingFile.setCreateTime(LocalDateTime.now());
|
personTrainingFileMapper.insert(personTrainingFile);
|
|
|
map.put("fileName",OriginalFileName + suffix);
|
map.put("url","/"+url);
|
} catch (IOException e) {
|
throw new ErrorException("上传失败");
|
}
|
return map;
|
}
|
|
@Override
|
public List<DepartmentLims> selectDepartLims() {
|
List<DepartmentLims> departmentLims = departmentLimsMapper.selectList(null);
|
return departmentLims;
|
|
|
}
|
|
@Override
|
public void importExcel(List<PersonTrainingDetailedUpload> list, Integer planId) {
|
List<PersonTrainingDetailed> personTrainingDetailedList = new ArrayList<>();
|
list.forEach(i -> {
|
PersonTrainingDetailed personTrainingDetailed = new PersonTrainingDetailed();
|
BeanUtils.copyProperties(i, personTrainingDetailed);
|
personTrainingDetailed.setClassHour(Double.parseDouble( i.getClassHour()));
|
// 匹配举办部门
|
DepartmentLims departmentLims = departmentLimsService.getOne(Wrappers.<DepartmentLims>lambdaQuery()
|
.eq(DepartmentLims::getName, i.getHoldingDepartment()));
|
if (ObjectUtils.isEmpty(departmentLims)) {
|
throw new ErrorException("未匹配到举办部门:" + i.getHoldingDepartment());
|
}
|
personTrainingDetailed.setHoldingDepartment(departmentLims.getId());
|
|
// // 匹配讲师
|
// String[] names = i.getTrainingLecturerName().split("、|,|,|\\s+");
|
// ArrayList<Integer> ids = new ArrayList<>();
|
// for(String name : names) {
|
// if(StringUtils.isNotEmpty(name)) {
|
// User user = userMapper.selectOne(Wrappers.<User>lambdaQuery()
|
// .eq(User::getName, name));
|
// if(Objects.isNull(user)) {
|
// throw new ErrorException("未找到该讲师:" + name);
|
// }
|
// ids.add(user.getId());
|
// }
|
// }
|
// String collect = ids.stream().map(item -> item.toString()).collect(Collectors.joining(","));
|
personTrainingDetailed.setTrainingLecturerId(i.getTrainingLecturerName());
|
personTrainingDetailed.setPlanId(planId);
|
personTrainingDetailed.setState(3);
|
|
String year = new SimpleDateFormat("yy", Locale.CHINESE).format(new Date());
|
String month = new SimpleDateFormat("MM", Locale.CHINESE).format(new Date());
|
String processNumber = numberGenerator.generateNumberWithPrefix(4, "KC" + month + "-" + year + month, PersonTrainingDetailed::getCourseCode);
|
personTrainingDetailed.setCourseCode(processNumber);
|
personTrainingDetailedList.add(personTrainingDetailed);
|
});
|
// 批量新增
|
if (CollectionUtils.isNotEmpty(personTrainingDetailedList)) {
|
baseMapper.insertBatchSomeColumn(personTrainingDetailedList);
|
}
|
}
|
|
@Override
|
public void deleteAnnualPlanDetailTable(String ids) {
|
String[] split = ids.split(",");
|
if (split.length > 0) {
|
for (String s : split) {
|
baseMapper.deleteById(s);
|
}
|
}
|
}
|
|
@Override
|
public IPage<PersonTrainingDetailedDto> queryTheAnnualPlanDetailsTable(Page page, String trainingLecturerName, String courseCode, String trainingDate,
|
Integer id, Integer userId,Integer departId,Integer state) {
|
// 判断当前人是否有权限查看年度计划主表
|
Integer userId1 = SecurityUtils.getUserId().intValue();
|
User user = userMapper.selectById(userId1);
|
String name1 = user.getName();
|
Integer currentUserId = user.getId();
|
//todo 权限待完成
|
// List<Power> powers = powerMapper.selectList(new LambdaQueryWrapper<Power>()
|
// .eq(Power::getRoleId, user.getRoleId())
|
// .eq(Power::getMenuMethod,"personTrainingSelect"));
|
// if(CollectionUtils.isEmpty(powers)) {
|
// if(Objects.isNull(departId) || departId == 1) {
|
// // 没有权限 就默认查看自己部门最新的年度计划 18 通信产品实验室 19 电力产品实验室
|
// if(user.getDepartLimsId().contains("18")) {
|
// List<PersonTraining> personTrainings = personTrainingMapper.selectList(new LambdaQueryWrapper<PersonTraining>()
|
// .eq(PersonTraining::getDepartId, 18)
|
// .orderByDesc(PersonTraining::getId));
|
// if(CollectionUtils.isNotEmpty(personTrainings)) {
|
// id = personTrainings.get(0).getId();
|
// }
|
// }else if(user.getDepartLimsId().contains("19")) {
|
// List<PersonTraining> personTrainings = personTrainingMapper.selectList(new LambdaQueryWrapper<PersonTraining>()
|
// .eq(PersonTraining::getDepartId, 19)
|
// .orderByDesc(PersonTraining::getId));
|
// if(CollectionUtils.isNotEmpty(personTrainings)) {
|
// id = personTrainings.get(0).getId();
|
// }
|
// }
|
// }else {
|
// List<PersonTraining> personTrainings = personTrainingMapper.selectList(new LambdaQueryWrapper<PersonTraining>()
|
// .eq(PersonTraining::getDepartId, departId)
|
// .orderByDesc(PersonTraining::getId));
|
// if(CollectionUtils.isNotEmpty(personTrainings)) {
|
// id = personTrainings.get(0).getId();
|
// }
|
// }
|
// }
|
if(Objects.nonNull(state)) {
|
if(state == -1) {
|
state = null;
|
}
|
}
|
IPage<PersonTrainingDetailedDto> list = baseMapper.queryTheAnnualPlanDetailsTable(page, trainingLecturerName, courseCode, trainingDate, id, userId, userId1,state);
|
// 报名人数
|
List<PersonTrainingDetailedDto> records = list.getRecords();
|
for(PersonTrainingDetailedDto a : records) {
|
// 报名的人数
|
List<PersonTrainingRecord> personTrainingRecords = personTrainingRecordMapper.selectList(new LambdaQueryWrapper<PersonTrainingRecord>()
|
.eq(PersonTrainingRecord::getCourseId, a.getId()));
|
a.setEnrollment(CollectionUtils.isNotEmpty(personTrainingRecords) ? personTrainingRecords.size() : 0);
|
List<Integer> collect = personTrainingRecords.stream().map(PersonTrainingRecord::getUserId).collect(Collectors.toList());
|
if(collect.contains(SecurityUtils.getUserId().intValue())) {
|
a.setWhetherClaim(true);
|
}else {
|
a.setWhetherClaim(false);
|
}
|
//前端权限需要 是否可以操作按钮
|
// if(a.getTrainingLecturerId().contains(name1) || CollectionUtils.isNotEmpty(powers) ||
|
// (Objects.nonNull(a.getAssessmentUserId()) && a.getAssessmentUserId().equals(currentUserId)) || Arrays.asList(16,36,35).contains(currentUserId)) {
|
// a.setIsDisabled(1); // 1不禁用
|
// }else{
|
// a.setIsDisabled(0);
|
// }
|
}
|
return list;
|
}
|
|
@Override
|
public List<PersonTrainingFile> getFileData(Integer detailedId) {
|
List<PersonTrainingFile> personTrainingFiles = personTrainingFileMapper.selectList(new LambdaQueryWrapper<PersonTrainingFile>()
|
.eq(PersonTrainingFile::getDetailId, detailedId)
|
.eq(PersonTrainingFile::getEnumAttachmentType, AttachmentType.getTypeValue(2)));
|
if(CollectionUtils.isNotEmpty(personTrainingFiles)) {
|
for(PersonTrainingFile a : personTrainingFiles) {
|
// 设置mime
|
int i = a.getFileUrl().indexOf(".");
|
String contentType = getContentType(a.getFileUrl().substring(i + 1));
|
a.setMime(contentType);
|
}
|
}
|
return personTrainingFiles;
|
}
|
|
@Override
|
public void deleteFile(Integer id) {
|
personTrainingFileMapper.deleteById(id);
|
}
|
|
@Override
|
public void fileDownLoad(Integer id, HttpServletResponse response) {
|
// 根据id查询下载的文件
|
PersonTrainingFile personTrainingFile = personTrainingFileMapper.selectById(id);
|
if(Objects.nonNull(personTrainingFile)) {
|
// 拿到文件路径
|
String fileUrl = personTrainingFile.getFileUrl();
|
int i = fileUrl.indexOf(".");
|
String suffix = fileUrl.substring(i); // 文件后缀
|
String url = "";
|
if(suffix.toLowerCase().contains("xls")) {
|
url = excelUrl + File.separator + fileUrl;
|
} else if (suffix.toLowerCase().contains("doc") || suffix.toLowerCase().contains("pdf")) {
|
url = wordUrl + File.separator + fileUrl;
|
}else {
|
url = imgUrl + File.separator + fileUrl;
|
}
|
File file = new File(url);
|
if(!file.exists()) {
|
throw new ErrorException("文件不存在或损坏");
|
}
|
// 获取文件输入流
|
try(FileInputStream fileInputStream = new FileInputStream(file); ServletOutputStream outputStream = response.getOutputStream()) {
|
String contentType = getContentType(fileUrl.substring(i + 1));
|
// 设置响应头
|
response.setContentType(contentType);
|
int i1 = personTrainingFile.getFileName().indexOf(".");
|
String fileName = URLEncoder.encode(personTrainingFile.getFileName().substring(0, i1), "UTF-8");
|
response.setHeader("Content-disposition", "attachment;filename=" + fileName + suffix);
|
byte[] bytes = new byte[1024];
|
int bytesRead;
|
while ((bytesRead = fileInputStream.read(bytes)) != -1 ){
|
outputStream.write(bytes,0,bytesRead);
|
}
|
outputStream.flush();
|
} catch (Exception e) {
|
throw new RuntimeException(e);
|
}
|
}
|
}
|
// 根据文件扩展名获取 MIME 类型
|
private String getContentType(String fileExtension) {
|
switch (fileExtension) {
|
case "jpg":
|
case "jpeg":
|
return "image/jpeg";
|
case "png":
|
return "image/png";
|
case "gif":
|
return "image/gif";
|
case "pdf":
|
return "application/pdf";
|
case "doc":
|
return "application/msword";
|
case "docx":
|
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
case "xls":
|
return "application/vnd.ms-excel";
|
case "xlsx":
|
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
case "ppt":
|
case "pptx":
|
return "application/vnd.ms-powerpoint";
|
default:
|
return "application/octet-stream"; // 默认二进制流
|
}
|
}
|
|
|
|
}
|