tuqin
4 小时以前 89071e0c09188d75c3d537f82204ae19713ac7fe
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package cn.iocoder.yudao.module.hrm.controller.admin.employee;
 
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.hrm.controller.admin.employee.vo.HrmEmployeeContractImportExcelVO;
import cn.iocoder.yudao.module.hrm.controller.admin.employee.vo.HrmEmployeeContractImportReqVO;
import cn.iocoder.yudao.module.hrm.controller.admin.employee.vo.HrmEmployeeContractImportRespVO;
import cn.iocoder.yudao.module.hrm.controller.admin.employee.vo.HrmEmployeeContractPageReqVO;
import cn.iocoder.yudao.module.hrm.controller.admin.employee.vo.HrmEmployeeContractRespVO;
import cn.iocoder.yudao.module.hrm.controller.admin.employee.vo.HrmEmployeeContractSaveReqVO;
import cn.iocoder.yudao.module.hrm.controller.admin.employee.vo.HrmEmployeeContractTerminateReqVO;
import cn.iocoder.yudao.module.hrm.service.employee.HrmEmployeeContractService;
import cn.iocoder.yudao.module.hrm.service.employee.contract.ContractDocumentService;
import cn.iocoder.yudao.module.hrm.service.employee.contract.ByteArrayMultipartFile;
import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi;
import cn.iocoder.yudao.module.system.service.storage.SystemStorageBlobService;
import org.springframework.web.multipart.MultipartFile;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.hrm.enums.ErrorCodeConstants.CONTRACT_TEMPLATE_LOAD_FAILED;
import static cn.iocoder.yudao.module.hrm.enums.ErrorCodeConstants.CONTRACT_TEMPLATE_TYPE_NOT_SUPPORT;
 
@Tag(name = "管理后台 - HRM 员工合同")
@RestController
@RequestMapping("/hrm/employee-contract")
@Validated
public class HrmEmployeeContractController {
 
    @Resource
    private HrmEmployeeContractService contractService;
    @Resource
    private ContractDocumentService contractDocumentService;
    @Resource
    private SystemStorageBlobService storageBlobService;
    @Resource
    private StorageAttachmentApi storageAttachmentApi;
 
    @PostMapping("/create")
    @Operation(summary = "创建员工合同")
    @PreAuthorize("@ss.hasPermission('hrm:employee-contract:create')")
    public CommonResult<Long> createContract(@Valid @RequestBody HrmEmployeeContractSaveReqVO createReqVO) {
        return success(contractService.createContract(createReqVO));
    }
 
    @PutMapping("/update")
    @Operation(summary = "更新员工合同")
    @PreAuthorize("@ss.hasPermission('hrm:employee-contract:update')")
    public CommonResult<Boolean> updateContract(@Valid @RequestBody HrmEmployeeContractSaveReqVO updateReqVO) {
        contractService.updateContract(updateReqVO);
        return success(true);
    }
 
    @PutMapping("/terminate")
    @Operation(summary = "解除/终止员工合同")
    @PreAuthorize("@ss.hasPermission('hrm:employee-contract:update')")
    public CommonResult<Boolean> terminateContract(@Valid @RequestBody HrmEmployeeContractTerminateReqVO reqVO) {
        contractService.terminateContract(reqVO);
        return success(true);
    }
 
    @DeleteMapping("/delete")
    @Operation(summary = "删除员工合同")
    @Parameter(name = "id", description = "合同ID", required = true)
    @PreAuthorize("@ss.hasPermission('hrm:employee-contract:delete')")
    public CommonResult<Boolean> deleteContract(@RequestParam("id") Long id) {
        contractService.deleteContract(id);
        return success(true);
    }
 
    @GetMapping("/get")
    @Operation(summary = "获得员工合同详情")
    @Parameter(name = "id", description = "合同ID", required = true)
    @PreAuthorize("@ss.hasPermission('hrm:employee-contract:query')")
    public CommonResult<HrmEmployeeContractRespVO> getContract(@RequestParam("id") Long id) {
        return success(contractService.getContract(id));
    }
 
    @GetMapping("/page")
    @Operation(summary = "获得员工合同分页")
    @PreAuthorize("@ss.hasPermission('hrm:employee-contract:query')")
    public CommonResult<PageResult<HrmEmployeeContractRespVO>> getContractPage(@Valid HrmEmployeeContractPageReqVO pageReqVO) {
        return success(contractService.getContractPage(pageReqVO));
    }
 
    @GetMapping("/expiring-count")
    @Operation(summary = "获得即将到期合同数量", description = "供 HRM 待办/首页提醒使用")
    @PreAuthorize("@ss.hasPermission('hrm:employee-contract:query')")
    public CommonResult<Long> getExpiringCount() {
        return success(contractService.getExpiringCount());
    }
 
    @PostMapping("/generate")
    @Operation(summary = "生成合同并回存为合同附件")
    @Parameter(name = "id", description = "合同ID", required = true)
    @Parameter(name = "templateType", description = "模板类型:1-劳动合同(简版) 2-劳动合同(标准) 3-劳务合同", required = true)
    @PreAuthorize("@ss.hasPermission('hrm:employee-contract:query')")
    public CommonResult<Long> generateContract(@RequestParam("id") Long id,
                                               @RequestParam("templateType") Integer templateType) {
        byte[] bytes = contractDocumentService.generateDocx(id, templateType);
        String fileName = contractDocumentService.buildFileName(id, templateType);
        MultipartFile file = new ByteArrayMultipartFile("file", fileName,
                "application/vnd.openxmlformats-officedocument.wordprocessingml.document", bytes);
        Long blobId = storageBlobService.upload(Collections.singletonList(file), false).get(0).getId();
        storageAttachmentApi.bindAttachments("file", "hrm_employee_contract", id, Collections.singletonList(blobId));
        return success(blobId);
    }
 
    @GetMapping("/generate-docx")
    @Operation(summary = "生成并下载合同文档(docx)")
    @Parameter(name = "id", description = "合同ID", required = true)
    @Parameter(name = "templateType", description = "模板类型:1-劳动合同(简版) 2-劳动合同(标准) 3-劳务合同", required = true)
    @PreAuthorize("@ss.hasPermission('hrm:employee-contract:query')")
    public void generateDocx(@RequestParam("id") Long id,
                             @RequestParam("templateType") Integer templateType,
                             HttpServletResponse response) throws IOException {
        byte[] bytes = contractDocumentService.generateDocx(id, templateType);
        String fileName = URLEncoder.encode(contractDocumentService.buildFileName(id, templateType), StandardCharsets.UTF_8);
        response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName + "; filename*=UTF-8''" + fileName);
        response.getOutputStream().write(bytes);
    }
 
    @GetMapping("/download-template")
    @Operation(summary = "下载合同空白模板", description = "1-劳动合同(简版) 2-劳动合同(标准) 3-劳务合同")
    @Parameter(name = "type", description = "模板类型:1-劳动合同(简版) 2-劳动合同(标准) 3-劳务合同", required = true)
    @PreAuthorize("@ss.hasPermission('hrm:employee-contract:query')")
    public void downloadTemplate(@RequestParam("type") Integer type, HttpServletResponse response) throws IOException {
        String path;
        String fileName;
        String contentType;
        if (type == 1) {
            path = "template/contract/original/labor_simple.docx";
            fileName = "劳动合同(简版).docx";
            contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
        } else if (type == 2) {
            path = "template/contract/original/labor_standard.docx";
            fileName = "劳动合同(标准).docx";
            contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
        } else if (type == 3) {
            path = "template/contract/original/service.doc";
            fileName = "劳务合同.doc";
            contentType = "application/msword";
        } else {
            throw exception(CONTRACT_TEMPLATE_TYPE_NOT_SUPPORT);
        }
        try (InputStream in = getClass().getClassLoader().getResourceAsStream(path)) {
            if (in == null) {
                throw exception(CONTRACT_TEMPLATE_LOAD_FAILED);
            }
            byte[] bytes = in.readAllBytes();
            String enc = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
            response.setContentType(contentType);
            response.setHeader("Content-Disposition", "attachment; filename=" + enc + "; filename*=UTF-8''" + enc);
            response.getOutputStream().write(bytes);
        }
    }
 
    @GetMapping("/import-template")
    @Operation(summary = "下载员工合同导入模板")
    @PreAuthorize("@ss.hasPermission('hrm:employee-contract:import')")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 输出示例数据,方便使用者参考。合同类型/期限类型按字典中文填写
        List<HrmEmployeeContractImportExcelVO> list = Arrays.asList(
                HrmEmployeeContractImportExcelVO.builder().name("张三").contractType(1).contractTermType(1)
                        .signCompany("南通XX科技有限公司").signDate(LocalDate.of(2026, 1, 1))
                        .startDate(LocalDate.of(2026, 1, 1)).endDate(LocalDate.of(2028, 12, 31))
                        .probationStartDate(LocalDate.of(2026, 1, 1)).probationEndDate(LocalDate.of(2026, 6, 30))
                        .probationSalary(new BigDecimal("8000.00")).regularSalary(new BigDecimal("10000.00"))
                        .remark("示例:请按实际修改后导入").build()
        );
        ExcelUtils.write(response, "员工合同导入模板.xls", "员工合同", HrmEmployeeContractImportExcelVO.class, list);
    }
 
    @PostMapping("/import")
    @Operation(summary = "导入员工合同", description = "按员工姓名 + 合同类型 + 合同开始日期判定;已存在且开启“已存在时更新”则覆盖更新该行")
    @PreAuthorize("@ss.hasPermission('hrm:employee-contract:import')")
    public CommonResult<HrmEmployeeContractImportRespVO> importExcel(@Valid HrmEmployeeContractImportReqVO importReqVO)
            throws Exception {
        List<HrmEmployeeContractImportExcelVO> list = ExcelUtils.read(importReqVO.getFile(),
                HrmEmployeeContractImportExcelVO.class);
        return success(contractService.importContractList(list, importReqVO));
    }
 
}