From 89071e0c09188d75c3d537f82204ae19713ac7fe Mon Sep 17 00:00:00 2001
From: tuqin <tuqin@tuqin.cn>
Date: 星期一, 12 十月 2026 10:09:33 +0800
Subject: [PATCH] feat(hrm): 合同生成与模板下载、员工转正、导入导出字段对齐

---
 yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/controller/admin/employee/HrmEmployeeContractController.java |   84 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 84 insertions(+), 0 deletions(-)

diff --git a/yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/controller/admin/employee/HrmEmployeeContractController.java b/yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/controller/admin/employee/HrmEmployeeContractController.java
index 0530349..ebc01fe 100644
--- a/yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/controller/admin/employee/HrmEmployeeContractController.java
+++ b/yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/controller/admin/employee/HrmEmployeeContractController.java
@@ -11,6 +11,11 @@
 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;
@@ -22,12 +27,19 @@
 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
@@ -37,6 +49,12 @@
 
     @Resource
     private HrmEmployeeContractService contractService;
+    @Resource
+    private ContractDocumentService contractDocumentService;
+    @Resource
+    private SystemStorageBlobService storageBlobService;
+    @Resource
+    private StorageAttachmentApi storageAttachmentApi;
 
     @PostMapping("/create")
     @Operation(summary = "鍒涘缓鍛樺伐鍚堝悓")
@@ -92,6 +110,72 @@
         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 = "鐢熸垚骞朵笅杞藉悎鍚屾枃妗o紙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')")

--
Gitblit v1.9.3