package cn.iocoder.yudao.module.srm.controller.admin.supplier; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.PageParam; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; 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; 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 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; @Tag(name = "管理后台 - SRM 供应商") @RestController @RequestMapping("/srm/supplier") @Validated public class SrmSupplierController { @Resource private SrmSupplierService supplierService; @PostMapping("/create") @Operation(summary = "创建供应商") @PreAuthorize("@ss.hasPermission('srm:supplier:create')") public CommonResult createSupplier(@Valid @RequestBody SrmSupplierSaveReqVO createReqVO) { return success(supplierService.createSupplier(createReqVO)); } @PutMapping("/update") @Operation(summary = "更新供应商") @PreAuthorize("@ss.hasPermission('srm:supplier:update')") public CommonResult updateSupplier(@Valid @RequestBody SrmSupplierSaveReqVO updateReqVO) { supplierService.updateSupplier(updateReqVO); return success(true); } @DeleteMapping("/delete") @Operation(summary = "删除供应商") @Parameter(name = "id", description = "编号", required = true) @PreAuthorize("@ss.hasPermission('srm:supplier:delete')") public CommonResult deleteSupplier(@RequestParam("id") Long id) { supplierService.deleteSupplier(id); return success(true); } @GetMapping("/get") @Operation(summary = "获取供应商详情") @Parameter(name = "id", description = "编号", required = true) @PreAuthorize("@ss.hasPermission('srm:supplier:query')") public CommonResult getSupplier(@RequestParam("id") Long id) { SrmSupplierDO supplier = supplierService.getSupplier(id); return success(BeanUtils.toBean(supplier, SrmSupplierRespVO.class)); } @GetMapping("/page") @Operation(summary = "供应商分页查询") @PreAuthorize("@ss.hasPermission('srm:supplier:query')") public CommonResult> getSupplierPage(@Valid SrmSupplierPageReqVO pageReqVO) { PageResult pageResult = supplierService.getSupplierPage(pageReqVO); return success(BeanUtils.toBean(pageResult, SrmSupplierRespVO.class)); } @GetMapping("/simple-list") @Operation(summary = "获取供应商精简列表(仅返回已通过准入审批的供应商)") public CommonResult> getSupplierSimpleList() { List list = supplierService.getSupplierSimpleList(); return success(BeanUtils.toBean(list, SrmSupplierRespVO.class)); } @GetMapping("/list") @Operation(summary = "获取供应商档案列表(不过滤准入状态)") public CommonResult> getSupplierAllList() { List list = supplierService.getSupplierAllList(); return success(BeanUtils.toBean(list, SrmSupplierRespVO.class)); } @GetMapping("/export-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 list = supplierService.getSupplierPage(pageReqVO).getList(); List 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 importSupplierExcel(@RequestParam("file") MultipartFile file, @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception { List 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 ? "启用" : "禁用"; } }