liu
2026-09-03 34e14f4d8f4756f12ad90d0d478c4c2d575203b8
yudao-module-srm/src/main/java/cn/iocoder/yudao/module/srm/controller/admin/supplier/SrmSupplierController.java
@@ -7,6 +7,7 @@
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.srm.controller.admin.supplier.vo.*;
import cn.iocoder.yudao.module.srm.dal.dataobject.SrmSupplierDO;
import cn.iocoder.yudao.module.srm.enums.SupplierTypeEnum;
import cn.iocoder.yudao.module.srm.service.supplier.SrmSupplierService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
@@ -17,8 +18,11 @@
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@@ -88,12 +92,82 @@
    }
    @GetMapping("/export-excel")
    @Operation(summary = "导出供应商 Excel")
    @Operation(summary = "导出供应商档案 Excel")
    @PreAuthorize("@ss.hasPermission('srm:supplier:export')")
    public void exportSupplierExcel(@Valid SrmSupplierPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        // 复用分页过滤条件,导出内容与列表一致
        List<SrmSupplierDO> list = supplierService.getSupplierPage(pageReqVO).getList();
        ExcelUtils.write(response, "供应商数据.xlsx", "供应商", SrmSupplierRespVO.class, BeanUtils.toBean(list, SrmSupplierRespVO.class));
        List<SrmSupplierExcelVO> excelList = new ArrayList<>();
        for (SrmSupplierDO supplier : list) {
            SrmSupplierExcelVO excelVO = BeanUtils.toBean(supplier, SrmSupplierExcelVO.class);
            excelVO.setType(toTypeLabel(supplier.getType()));
            excelVO.setStatus(toStatusLabel(supplier.getStatus()));
            excelList.add(excelVO);
        }
        ExcelUtils.write(response, "供应商数据.xlsx", "供应商", SrmSupplierExcelVO.class, excelList);
    }
    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入供应商档案模板")
    @PreAuthorize("@ss.hasPermission('srm:supplier:import')")
    public void getImportTemplate(HttpServletResponse response) throws IOException {
        SrmSupplierImportExcelVO sample = new SrmSupplierImportExcelVO();
        sample.setCode("GYS0001");
        sample.setName("示例供应商");
        sample.setShortName("示例");
        sample.setType("生产型");
        sample.setCompanyNature("民营");
        sample.setRegisteredCapital(new java.math.BigDecimal("1000"));
        sample.setBusinessLicenseNo("91320000XXXXXXXXXX");
        sample.setTaxNo("91320000XXXXXXXXXX");
        sample.setLegalPerson("张三");
        sample.setAddress("示例市示例区示例路1号");
        sample.setWebsite("http://www.example.com");
        sample.setContactName("李四");
        sample.setContactMobile("13800000000");
        sample.setContactEmail("contact@example.com");
        sample.setBankName("示例银行示例支行");
        sample.setBankAccount("6222020000000000000");
        sample.setStatus(0);
        sample.setRemark("导入模板示例行,可删除");
        ExcelUtils.write(response, "供应商导入模板.xlsx", "供应商", SrmSupplierImportExcelVO.class, Arrays.asList(sample));
    }
    @PostMapping("/import-excel")
    @Operation(summary = "导入供应商档案 Excel")
    @PreAuthorize("@ss.hasPermission('srm:supplier:import')")
    public CommonResult<SrmSupplierImportRespVO> importSupplierExcel(@RequestParam("file") MultipartFile file,
                                                                     @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<SrmSupplierImportExcelVO> list = ExcelUtils.read(file, SrmSupplierImportExcelVO.class);
        return success(supplierService.importSupplierList(list, updateSupport));
    }
    // ========== Excel 展示辅助 ==========
    /**
     * 供应商类型编码 -> 中文名称(如 1 -> 生产型);无法识别时原样返回
     */
    private static String toTypeLabel(String typeCode) {
        if (typeCode == null) {
            return null;
        }
        for (SupplierTypeEnum e : SupplierTypeEnum.values()) {
            if (typeCode.equals(String.valueOf(e.getCode()))) {
                return e.getName();
            }
        }
        return typeCode;
    }
    /**
     * 状态 0 启用 / 1 禁用 -> 中文文案;空值返回空
     */
    private static String toStatusLabel(Integer status) {
        if (status == null) {
            return null;
        }
        return status == 0 ? "启用" : "禁用";
    }
}