maven
20 小时以前 ce80b4856af879484f039c3176843e29978b436f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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.utils.poi.ExcelUtil;
import com.ruoyi.dto.WordDateDto;
import com.ruoyi.staff.mapper.StaffJoinLeaveRecordMapper;
import com.ruoyi.staff.mapper.StaffOnJobMapper;
import com.ruoyi.staff.pojo.StaffJoinLeaveRecord;
import com.ruoyi.staff.pojo.StaffOnJob;
import com.ruoyi.staff.service.IStaffJoinLeaveRecordService;
import com.ruoyi.staff.service.IStaffOnJobService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.AllArgsConstructor;
 
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@AllArgsConstructor
@Service
public class StaffOnJobServiceImpl extends ServiceImpl<StaffOnJobMapper, StaffOnJob>  implements IStaffOnJobService {
 
 
    private StaffOnJobMapper staffOnJobMapper;
 
    private StaffJoinLeaveRecordMapper staffJoinLeaveRecordMapper;
 
    //在职员工台账分页查询
    @Override
    public IPage<StaffOnJob> staffOnJobListPage(Page page, StaffOnJob staffOnJob) {
        return staffOnJobMapper.staffOnJobListPage(page,staffOnJob);
    }
 
    //在职员工详情
    @Override
    public List<StaffJoinLeaveRecord> staffOnJobDetail(String staffNo) {
        return staffJoinLeaveRecordMapper.selectList(Wrappers.<StaffJoinLeaveRecord>lambdaQuery()
                .eq(StaffJoinLeaveRecord::getStaffState,1)
                .eq(StaffJoinLeaveRecord::getStaffNo,staffNo));
    }
 
    //在职员工导出
    @Override
    public void staffOnJobExport(HttpServletResponse response, StaffOnJob staffOnJob) {
        List<StaffOnJob> staffOnJobs = staffOnJobMapper.staffOnJobList(staffOnJob);
        ExcelUtil<StaffOnJob> util = new ExcelUtil<StaffOnJob>(StaffOnJob.class);
        util.exportExcel(response, staffOnJobs, "在职员工台账导出");
    }
 
    @Override
    public List<StaffJoinLeaveRecord> staffOnJobList() {
        return staffJoinLeaveRecordMapper.staffOnJobList();
    }
 
    @Override
    public Boolean importData(MultipartFile file) {
        try {
            ExcelUtil<StaffOnJob> util = new ExcelUtil<>(StaffOnJob.class);
            List<StaffOnJob> 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/uploads/" + 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() + "");
 
        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() + "");
 
        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;
    }
 
 
 
}