package com.ruoyi.staff.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; 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.utils.poi.ExcelUtil; import com.ruoyi.dto.WordDateDto; import com.ruoyi.project.system.domain.SysPost; import com.ruoyi.project.system.mapper.SysPostMapper; import com.ruoyi.staff.dto.StaffOnJobDto; import com.ruoyi.staff.mapper.StaffContractMapper; import com.ruoyi.staff.mapper.StaffJoinLeaveRecordMapper; import com.ruoyi.staff.mapper.StaffLeaveMapper; import com.ruoyi.staff.mapper.StaffOnJobMapper; import com.ruoyi.staff.pojo.StaffContract; import com.ruoyi.staff.pojo.StaffJoinLeaveRecord; import com.ruoyi.staff.pojo.StaffLeave; import com.ruoyi.staff.pojo.StaffOnJob; import com.ruoyi.staff.service.IStaffOnJobService; import freemarker.template.Configuration; import freemarker.template.Template; import lombok.AllArgsConstructor; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.nio.charset.StandardCharsets; import java.time.Instant; import java.time.LocalDate; import java.time.ZoneId; import java.util.*; import static com.ruoyi.common.enums.StaffJoinLeaveRecordDimissionReason.StaffJoinLeaveRecordDimissionReasonOther; @AllArgsConstructor @Service public class StaffOnJobServiceImpl extends ServiceImpl implements IStaffOnJobService { private StaffOnJobMapper staffOnJobMapper; private SysPostMapper sysPostMapper; private StaffJoinLeaveRecordMapper staffJoinLeaveRecordMapper; private StaffContractMapper staffContractMapper; private StaffLeaveMapper staffLeaveMapper; //在职员工台账分页查询 @Override public IPage 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 staffOnJobs = staffOnJobMapper.selectList(Wrappers.lambdaQuery().eq(StaffOnJob::getStaffNo, staffOnJobPrams.getStaffNo())); if (staffOnJobs.size()>0){ throw new BaseException("编号为"+staffOnJobPrams.getStaffNo()+"的员工已经存在,无法新增!!!"); } // 创建入职数据 staffOnJobMapper.insert(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"};//排除id属性 BeanUtils.copyProperties(staffOnJobParams,job,ignoreProperties); staffOnJobMapper.updateById(job); // 获取最新合同数据,并且更新 StaffContract contract = staffContractMapper.selectOne(Wrappers.lambdaQuery() .eq(StaffContract::getStaffOnJobId, id) .orderByDesc(StaffContract::getId)); if (contract != null){ BeanUtils.copyProperties(staffOnJobParams,contract,ignoreProperties); return staffContractMapper.updateById(contract); } return 0; } //删除入职 @Override @Transactional(rollbackFor = Exception.class) public int delStaffOnJobs(List ids) { // 删除入职数据 staffOnJobMapper.deleteBatchIds(ids); // 删除离职数据 staffLeaveMapper.delete(Wrappers.lambdaQuery().in(StaffLeave::getStaffOnJobId, ids)); // 删除合同数据 return staffContractMapper.delete(Wrappers.lambdaQuery().in(StaffContract::getStaffOnJobId, ids)); } //在职员工详情 @Override public StaffOnJobDto staffOnJobDetail(Long id) { StaffOnJob staffOnJob = staffOnJobMapper.selectById(id); StaffOnJobDto staffOnJobDto = new StaffOnJobDto(); BeanUtils.copyProperties(staffOnJob, staffOnJobDto); // 查询岗位名称 SysPost post = sysPostMapper.selectPostById((long) staffOnJob.getSysPostId()); staffOnJobDto.setPostName(post.getPostName()); // 查询合同信息 StaffContract contract = staffContractMapper.selectOne(Wrappers.lambdaQuery() .eq(StaffContract::getStaffOnJobId, staffOnJob.getId()) .orderByDesc(StaffContract::getId)); if (contract != null){ staffOnJobDto.setContractTerm(contract.getContractTerm()); staffOnJobDto.setContractStartTime(contract.getContractStartTime()); staffOnJobDto.setContractEndTime(contract.getContractEndTime()); } return staffOnJobDto; } //在职员工导出 @Override public void staffOnJobExport(HttpServletResponse response, StaffOnJob staffOnJob) { List staffOnJobs = staffOnJobMapper.staffOnJobList(staffOnJob); ExcelUtil util = new ExcelUtil(StaffOnJobDto.class); util.exportExcel(response, staffOnJobs, "在职员工台账导出"); } @Override public List staffOnJobList() { return staffJoinLeaveRecordMapper.staffOnJobList(); } @Override public Boolean importData(MultipartFile file) { try { ExcelUtil util = new ExcelUtil<>(StaffOnJob.class); List staffOnJobs = util.importExcel(file.getInputStream()); return saveOrUpdateBatch(staffOnJobs); } catch (Exception e) { e.printStackTrace(); return false; } } @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 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; } }