package com.ruoyi.staff.service.impl;
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
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.exception.base.BaseException;
|
import com.ruoyi.common.exception.ServiceException;
|
import com.ruoyi.common.utils.SecurityUtils;
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
import com.ruoyi.common.utils.poi.ExcelUtil;
|
import com.ruoyi.dto.WordDateDto;
|
import com.ruoyi.project.system.domain.SysDept;
|
import com.ruoyi.project.system.domain.SysPost;
|
import com.ruoyi.project.system.domain.SysRole;
|
import com.ruoyi.project.system.domain.SysUser;
|
import com.ruoyi.project.system.mapper.SysDeptMapper;
|
import com.ruoyi.project.system.mapper.SysPostMapper;
|
import com.ruoyi.project.system.mapper.SysRoleMapper;
|
import com.ruoyi.project.system.mapper.SysUserMapper;
|
import com.ruoyi.project.system.service.impl.SysUserServiceImpl;
|
import com.ruoyi.staff.dto.StaffOnJobDto;
|
import com.ruoyi.staff.dto.StaffOnJobExcelDto;
|
import com.ruoyi.staff.mapper.*;
|
import com.ruoyi.staff.pojo.*;
|
import com.ruoyi.staff.service.IStaffOnJobService;
|
import freemarker.template.Configuration;
|
import freemarker.template.Template;
|
import jakarta.servlet.http.HttpServletResponse;
|
import lombok.RequiredArgsConstructor;
|
import org.apache.commons.collections4.CollectionUtils;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.io.File;
|
import java.io.FileOutputStream;
|
import java.io.OutputStreamWriter;
|
import java.io.StringWriter;
|
import java.nio.charset.StandardCharsets;
|
import java.time.Instant;
|
import java.time.LocalDate;
|
import java.time.ZoneId;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
@RequiredArgsConstructor
|
@Service
|
public class StaffOnJobServiceImpl extends ServiceImpl<StaffOnJobMapper, StaffOnJob> implements IStaffOnJobService {
|
|
private final StaffOnJobMapper staffOnJobMapper;
|
private final SysDeptMapper sysDeptMapper;
|
private final SysRoleMapper sysRoleMapper;
|
private final SysPostMapper sysPostMapper;
|
private final StaffContractMapper staffContractMapper;
|
private final StaffLeaveMapper staffLeaveMapper;
|
private final PersonalAttendanceRecordsMapper personalAttendanceRecordsMapper;
|
private final SysUserServiceImpl sysUserService;
|
private final SysUserMapper sysUserMapper;
|
private final StaffEducationServiceImpl staffEducationService;
|
private final StaffEducationMapper staffEducationMapper;
|
private final StaffWorkExperienceMapper staffWorkExperienceMapper;
|
private final StaffWorkExperienceServiceImpl staffWorkExperienceServiceImpl;
|
private final StaffEmergencyContactMapper staffEmergencyContactMapper;
|
private final StaffEmergencyContactServiceImpl staffEmergencyContactServiceImpl;
|
|
// 在职员工台账分页查询
|
@Override
|
public IPage<StaffOnJobDto> staffOnJobListPage(Page page, StaffOnJob staffOnJob) {
|
return staffOnJobMapper.staffOnJobListPage(page, staffOnJob);
|
}
|
|
// 新增入职
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public int add(StaffOnJobDto staffOnJobPrams) {
|
String[] ignoreProperties = { "id" };// 排除id属性
|
// 判断编号是否存在
|
List<StaffOnJob> staffOnJobs = staffOnJobMapper.selectList(
|
Wrappers.<StaffOnJob>lambdaQuery().eq(StaffOnJob::getStaffNo, staffOnJobPrams.getStaffNo()));
|
if (staffOnJobs != null && !staffOnJobs.isEmpty()) {
|
throw new BaseException("编号为" + staffOnJobPrams.getStaffNo() + "的员工已经存在,无法新增!!!");
|
}
|
|
// 创建入职数据
|
staffOnJobPrams.setContractExpireTime(staffOnJobPrams.getContractEndTime());
|
staffOnJobPrams.setStaffState(1);
|
staffOnJobMapper.insert(staffOnJobPrams);
|
// 查询用户是否已经新增
|
SysUser sysUser = sysUserService.selectUserById(staffOnJobPrams.getId());
|
if (sysUser == null) {
|
SysUser sysUser1 = new SysUser();
|
sysUser1.setUserName(staffOnJobPrams.getStaffNo());
|
sysUser1.setNickName(staffOnJobPrams.getStaffName());
|
String s = SecurityUtils.encryptPassword("123456");
|
sysUser1.setPassword(s);
|
if (staffOnJobPrams.getSysPostId() != null) {
|
Long[] posts = new Long[] { staffOnJobPrams.getSysPostId().longValue() };
|
sysUser1.setPostIds(posts);
|
}
|
sysUser1.setRoleIds(new Long[] { staffOnJobPrams.getRoleId() });
|
sysUser1.setDeptIds(new Long[] { staffOnJobPrams.getSysDeptId() });
|
sysUser1.setStatus("0");
|
sysUserService.insertUser(sysUser1);
|
}
|
// 绑定子表数据
|
bingingStaffOnJobExtra(staffOnJobPrams.getId(), staffOnJobPrams);
|
// 创建合同记录
|
StaffContract staffContract = new StaffContract();
|
staffContract.setStaffOnJobId(staffOnJobPrams.getId());
|
staffContract.setContractTerm(staffOnJobPrams.getContractTerm());
|
staffContract.setContractStartTime(staffOnJobPrams.getContractStartTime());
|
staffContract.setContractEndTime(staffOnJobPrams.getContractEndTime());
|
return staffContractMapper.insert(staffContract);
|
}
|
|
// 更新入职信息
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public int update(Long id, StaffOnJobDto staffOnJobParams) {
|
// 判断对象是否存在
|
StaffOnJob job = staffOnJobMapper.selectById(id);
|
if (job == null) {
|
throw new BaseException("编号为" + staffOnJobParams.getStaffNo() + "的员工不存在,无法更新!!!");
|
}
|
|
String[] ignoreProperties = { "id" };// 排除更新属性
|
|
// 获取最新合同数据,并且更新
|
StaffContract contract = staffContractMapper.selectOne(Wrappers.<StaffContract>lambdaQuery()
|
.eq(StaffContract::getStaffOnJobId, id)
|
.last("limit 1")
|
.orderByDesc(StaffContract::getId));
|
if (contract != null) {
|
BeanUtils.copyProperties(staffOnJobParams, contract, ignoreProperties);
|
staffContractMapper.updateById(contract);
|
}
|
|
// 删除所有子表数据
|
delStaffOnJobExtra(Arrays.asList(id));
|
// 绑定子表数据
|
bingingStaffOnJobExtra(id, staffOnJobParams);
|
// 更新员工数据
|
staffOnJobParams.setContractExpireTime(staffOnJobParams.getContractEndTime());
|
return staffOnJobMapper.updateById(staffOnJobParams);
|
}
|
|
/**
|
* 绑定员工子表数据
|
*
|
* @param staffOnJobPrams
|
* @param id
|
*/
|
public void bingingStaffOnJobExtra(Long id, StaffOnJob staffOnJobPrams) {
|
// 新增教育经历
|
if (CollectionUtils.isNotEmpty(staffOnJobPrams.getStaffEducationList())) {
|
staffOnJobPrams.getStaffEducationList().stream()
|
.filter(Objects::nonNull) // 过滤null对象,避免空指针
|
.forEach(staff -> staff.setStaffOnJobId(id)); // 赋值
|
staffEducationService.saveBatch(staffOnJobPrams.getStaffEducationList());
|
}
|
// 新增工作经历
|
if (CollectionUtils.isNotEmpty(staffOnJobPrams.getStaffWorkExperienceList())) {
|
staffOnJobPrams.getStaffWorkExperienceList().stream()
|
.filter(Objects::nonNull) // 过滤null对象,避免空指针
|
.forEach(staff -> staff.setStaffOnJobId(id)); // 赋值
|
staffWorkExperienceServiceImpl.saveBatch(staffOnJobPrams.getStaffWorkExperienceList());
|
}
|
// 新增紧急联系人
|
if (CollectionUtils.isNotEmpty(staffOnJobPrams.getStaffEmergencyContactList())) {
|
staffOnJobPrams.getStaffEmergencyContactList().stream()
|
.filter(Objects::nonNull) // 过滤null对象,避免空指针
|
.forEach(staff -> staff.setStaffOnJobId(id)); // 赋值
|
staffEmergencyContactServiceImpl.saveBatch(staffOnJobPrams.getStaffEmergencyContactList());
|
}
|
}
|
|
/**
|
* 通过员工id删除教育经历,工作经历,紧急联系人
|
*
|
* @param ids
|
* @return
|
*/
|
public void delStaffOnJobExtra(List<Long> ids) {
|
// 删除教育经历
|
staffEducationService.remove(Wrappers.<StaffEducation>lambdaQuery().in(StaffEducation::getStaffOnJobId, ids));
|
// 删除工作经历
|
staffWorkExperienceServiceImpl
|
.remove(Wrappers.<StaffWorkExperience>lambdaQuery().in(StaffWorkExperience::getStaffOnJobId, ids));
|
// 删除紧急联系人
|
staffEmergencyContactServiceImpl
|
.remove(Wrappers.<StaffEmergencyContact>lambdaQuery().in(StaffEmergencyContact::getStaffOnJobId, ids));
|
}
|
|
// 删除入职
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public int delStaffOnJobs(List<Integer> ids) {
|
List<StaffOnJob> staffOnJobs = staffOnJobMapper
|
.selectList(Wrappers.<StaffOnJob>lambdaQuery().in(StaffOnJob::getId, ids));
|
if (CollectionUtils.isEmpty(staffOnJobs)) {
|
throw new BaseException("该员工不存在,无法删除!!!");
|
}
|
// 删除入职数据
|
staffOnJobMapper.deleteBatchIds(ids);
|
// 删除离职数据
|
staffLeaveMapper.delete(Wrappers.<StaffLeave>lambdaQuery().in(StaffLeave::getStaffOnJobId, ids));
|
// 删除打卡记录
|
personalAttendanceRecordsMapper.delete(
|
Wrappers.<PersonalAttendanceRecords>lambdaQuery().in(PersonalAttendanceRecords::getStaffOnJobId, ids));
|
// 删除用户数据
|
List<SysUser> sysUsers = sysUserMapper.selectList(Wrappers.<SysUser>lambdaQuery()
|
.in(SysUser::getUserName,
|
staffOnJobs.stream().map(StaffOnJob::getStaffNo).collect(Collectors.toList())));
|
if (CollectionUtils.isNotEmpty(sysUsers)) {
|
Long[] longs = sysUsers.stream().map(SysUser::getUserId).toArray(Long[]::new);
|
sysUserService.deleteUserByIds(longs);
|
}
|
// 删除子表数据
|
delStaffOnJobExtra(ids.stream().map(Integer::longValue).collect(Collectors.toList()));
|
|
// 删除合同数据
|
return staffContractMapper
|
.delete(Wrappers.<StaffContract>lambdaQuery().in(StaffContract::getStaffOnJobId, ids));
|
}
|
|
// 续签合同
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public int renewContract(Long id, StaffContract staffContract) {
|
// 判断对象是否存在
|
StaffOnJob job = staffOnJobMapper.selectById(id);
|
if (job == null) {
|
throw new BaseException("该员工不存在,无法更新!!!");
|
}
|
|
// 增加合同
|
StaffContract newStaffContract = new StaffContract();
|
newStaffContract.setStaffOnJobId(id);
|
newStaffContract.setContractTerm(staffContract.getContractTerm());
|
newStaffContract.setContractStartTime(staffContract.getContractStartTime());
|
newStaffContract.setContractEndTime(staffContract.getContractEndTime());
|
staffContractMapper.insert(newStaffContract);
|
|
// 更新员工合同过期时间
|
job.setContractExpireTime(staffContract.getContractEndTime());
|
staffOnJobMapper.updateById(job);
|
return 0;
|
}
|
|
// 在职员工详情
|
@Override
|
public StaffOnJobDto staffOnJobDetail(Long id) {
|
StaffOnJob staffOnJob = staffOnJobMapper.selectById(id);
|
if (staffOnJob == null) {
|
throw new IllegalArgumentException("该员工不存在");
|
}
|
|
StaffOnJobDto staffOnJobDto = new StaffOnJobDto();
|
BeanUtils.copyProperties(staffOnJob, staffOnJobDto);
|
// 查询岗位名称
|
if (staffOnJob.getSysPostId() != null) {
|
SysPost post = sysPostMapper.selectPostById(staffOnJob.getSysPostId().longValue());
|
if (post != null) {
|
staffOnJobDto.setPostName(post.getPostName());
|
}
|
}
|
|
// 查询合同信息
|
StaffContract contract = staffContractMapper.selectOne(Wrappers.<StaffContract>lambdaQuery()
|
.eq(StaffContract::getStaffOnJobId, staffOnJob.getId())
|
.last("limit 1")
|
.orderByDesc(StaffContract::getId));
|
if (contract != null) {
|
staffOnJobDto.setContractTerm(contract.getContractTerm());
|
staffOnJobDto.setContractStartTime(contract.getContractStartTime());
|
staffOnJobDto.setContractEndTime(contract.getContractEndTime());
|
}
|
// 获取子表数据
|
staffOnJobDto.setStaffEducationList(staffEducationMapper.selectList(Wrappers.<StaffEducation>lambdaQuery()
|
.eq(StaffEducation::getStaffOnJobId, staffOnJob.getId())));
|
staffOnJobDto.setStaffWorkExperienceList(
|
staffWorkExperienceMapper.selectList(Wrappers.<StaffWorkExperience>lambdaQuery()
|
.eq(StaffWorkExperience::getStaffOnJobId, staffOnJob.getId())));
|
staffOnJobDto.setStaffEmergencyContactList(
|
staffEmergencyContactMapper.selectList(Wrappers.<StaffEmergencyContact>lambdaQuery()
|
.eq(StaffEmergencyContact::getStaffOnJobId, staffOnJob.getId())));
|
return staffOnJobDto;
|
}
|
|
// 在职员工导出
|
@Override
|
public void staffOnJobExport(HttpServletResponse response, StaffOnJob staffOnJob) {
|
List<StaffOnJobDto> staffOnJobs = staffOnJobMapper.staffOnJobList(staffOnJob);
|
ExcelUtil<StaffOnJobDto> util = new ExcelUtil<StaffOnJobDto>(StaffOnJobDto.class);
|
util.exportExcel(response, staffOnJobs, "员工台账导出");
|
}
|
|
@Override
|
public List<StaffOnJobDto> staffOnJobList(StaffOnJob staffOnJob) {
|
return staffOnJobMapper.staffOnJobList(staffOnJob);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public Boolean importData(MultipartFile file) {
|
try {
|
ExcelUtil<StaffOnJobExcelDto> util = new ExcelUtil<>(StaffOnJobExcelDto.class);
|
List<StaffOnJobExcelDto> staffOnJobs = util.importExcel(file.getInputStream());
|
if (CollectionUtils.isEmpty(staffOnJobs)) {
|
return false;
|
}
|
// 获取所有部门数据
|
List<SysDept> sysDepts = sysDeptMapper
|
.selectList(Wrappers.<SysDept>lambdaQuery().eq(SysDept::getDelFlag, 0));
|
// 获取所有角色数据
|
List<SysRole> sysRoles = sysRoleMapper.selectRoleAll();
|
staffOnJobs.forEach(staffOnJob -> {
|
// 处理合同期限数据格式
|
if (staffOnJob.getContractTerm() != null && !staffOnJob.getContractTerm().trim().isEmpty()) {
|
String term = staffOnJob.getContractTerm().trim();
|
try {
|
Integer.parseInt(term);
|
} catch (NumberFormatException e) {
|
throw new ServiceException("员工[" + staffOnJob.getStaffName() + "]的合同期限["
|
+ staffOnJob.getContractTerm() + "]格式不正确,必须为纯数字(如: 1, 2, 3)");
|
}
|
}
|
StaffOnJobDto staffOnJobDto = new StaffOnJobDto();
|
BeanUtils.copyProperties(staffOnJob, staffOnJobDto);
|
// 通过名称获取部门id
|
Long deptId = sysDepts.stream()
|
.filter(dept -> dept.getDeptName() != null
|
&& dept.getDeptName().equals(staffOnJob.getSysDeptName()))
|
.findFirst()
|
.map(SysDept::getDeptId)
|
.orElse(null);
|
if (deptId == null) {
|
throw new ServiceException(
|
"员工[" + staffOnJob.getStaffName() + "]的部门[" + staffOnJob.getSysDeptName() + "]不存在,请检查数据");
|
}
|
staffOnJobDto.setSysDeptId(deptId);
|
|
// 通过名称获取角色id
|
Long roleId = sysRoles.stream()
|
.filter(role -> role.getRoleName() != null
|
&& role.getRoleName().equals(staffOnJob.getRoleName()))
|
.findFirst()
|
.map(SysRole::getRoleId)
|
.orElse(null);
|
if (roleId == null) {
|
throw new ServiceException(
|
"员工[" + staffOnJob.getStaffName() + "]的角色[" + staffOnJob.getRoleName() + "]不存在,请检查数据");
|
}
|
staffOnJobDto.setRoleId(roleId);
|
SpringUtils.getAopProxy(this).add(staffOnJobDto);
|
});
|
return true;
|
} catch (ServiceException | BaseException e) {
|
throw e;
|
} catch (Exception e) {
|
log.error("员工台账导入失败 : " + e.getMessage());
|
throw new ServiceException("导入失败: " + e.getMessage());
|
}
|
}
|
|
@Override
|
public String exportCopy(HttpServletResponse response, StaffOnJob staffOnJob) throws Exception {
|
String url = "/javaWork/product-inventory-management/file/prod/" + staffOnJob.getStaffName() + "-劳动合同书.docx";
|
Configuration cfg = new Configuration(Configuration.VERSION_2_3_32);
|
// 设置模板文件所在目录(绝对路径,例如:/templates/)
|
cfg.setClassForTemplateLoading(StaffOnJobServiceImpl.class, "/static");
|
cfg.setDefaultEncoding("UTF-8");
|
// 2.定义需要填充的变里
|
// ① 构造员工信息(实际项目中可从数据库/Excel读取)
|
WordDateDto staff = new WordDateDto();
|
BeanUtils.copyProperties(staffOnJob, staff);
|
// 通过合同年限,合同到期日期计算合同开始日期,在获取开始日期,结束日期的年月日数字
|
// 合同到期日期 - 合同年限(Date类型)
|
// 1. 将Date转换为Instant(时间戳)
|
Instant instant = staff.getContractExpireTime().toInstant();
|
|
// 也可以指定具体时区,例如Asia/Shanghai:
|
LocalDate localDate = instant.atZone(ZoneId.of("Asia/Shanghai")).toLocalDate(); // 合同结束时间
|
LocalDate localDate1 = localDate.minusYears(Integer.parseInt(staff.getContractTerm()));// 合同开始时间
|
|
// 签订日期转换lcoaldate
|
LocalDate localDate2 = staff.getSignDate().toInstant().atZone(ZoneId.of("Asia/Shanghai")).toLocalDate();
|
|
// 试用日期转换lcoaldate
|
LocalDate localDate3 = staff.getTrialStartDate().toInstant().atZone(ZoneId.of("Asia/Shanghai")).toLocalDate();
|
LocalDate localDate4 = staff.getTrialEndDate().toInstant().atZone(ZoneId.of("Asia/Shanghai")).toLocalDate();
|
|
staff.setQyear(localDate2.getYear() + "");
|
staff.setQmoth(localDate2.getMonthValue() + "");
|
staff.setQday(localDate2.getDayOfMonth() + "");
|
if (staff.getDateSelect().equals("A")) {
|
staff.setSyear(localDate1.getYear() + "");
|
staff.setSmoth(localDate1.getMonthValue() + "");
|
staff.setSday(localDate1.getDayOfMonth() + "");
|
staff.setEyear(localDate.getDayOfMonth() + "");
|
staff.setEmoth(localDate.getDayOfMonth() + "");
|
staff.setEday(localDate.getDayOfMonth() + "");
|
|
staff.setStyear(localDate3.getYear() + "");
|
staff.setStmoth(localDate3.getMonthValue() + "");
|
staff.setStday(localDate3.getDayOfMonth() + "");
|
staff.setSeyear(localDate4.getYear() + "");
|
staff.setSemoth(localDate4.getMonthValue() + "");
|
staff.setSeday(localDate4.getDayOfMonth() + "");
|
} else if (staff.getDateSelect().equals("B")) {
|
|
staff.setBsyear(localDate1.getYear() + "");
|
staff.setBsmoth(localDate1.getMonthValue() + "");
|
staff.setBsday(localDate1.getDayOfMonth() + "");
|
|
staff.setBstyear(localDate3.getYear() + "");
|
staff.setBstmoth(localDate3.getMonthValue() + "");
|
staff.setBstday(localDate3.getDayOfMonth() + "");
|
staff.setBseyear(localDate4.getYear() + "");
|
staff.setBsemoth(localDate4.getMonthValue() + "");
|
staff.setBseday(localDate4.getDayOfMonth() + "");
|
} else if (staff.getDateSelect().equals("C")) {
|
staff.setCsyear(localDate1.getYear() + "");
|
staff.setCsmoth(localDate1.getMonthValue() + "");
|
staff.setCsday(localDate1.getDayOfMonth() + "");
|
}
|
|
Map<String, Object> data = new HashMap<>();
|
data.put("item", staff);
|
// 3.加载XML 模板
|
Template template = cfg.getTemplate("劳动合同书.xml");
|
// 4.生成填充后的 XML 内容
|
StringWriter out = new StringWriter();
|
template.process(data, out);
|
String filledXml = out.toString();
|
// 5.将XML内容写入交件并改为.docx 格式
|
File outputFile = new File(url);
|
try (FileOutputStream fos = new FileOutputStream(outputFile);
|
OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8)) {
|
osw.write(filledXml);
|
}
|
return url;
|
}
|
|
}
|