| | |
| | | package cn.iocoder.yudao.module.mes.controller.admin.md.item; |
| | | |
| | | import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog; |
| | | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| | | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.type.MesMdItemTypeExcelVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.type.MesMdItemTypeImportExcelVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.type.MesMdItemTypeImportRespVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.type.MesMdItemTypeListReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.type.MesMdItemTypeRespVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.type.MesMdItemTypeSaveReqVO; |
| | |
| | | import cn.iocoder.yudao.module.mes.service.md.item.MesMdItemTypeService; |
| | | import io.swagger.v3.oas.annotations.Operation; |
| | | import io.swagger.v3.oas.annotations.Parameter; |
| | | import io.swagger.v3.oas.annotations.Parameters; |
| | | 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.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT; |
| | | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| | | |
| | |
| | | .setItemOrProduct(itemType.getItemOrProduct()))); |
| | | } |
| | | |
| | | @GetMapping("/export-excel") |
| | | @Operation(summary = "导出物料产品分类 Excel") |
| | | @PreAuthorize("@ss.hasPermission('mes:md-item-type:export')") |
| | | @ApiAccessLog(operateType = EXPORT) |
| | | public void exportItemTypeExcel(@Valid MesMdItemTypeListReqVO listReqVO, |
| | | HttpServletResponse response) throws IOException { |
| | | List<MesMdItemTypeDO> list = itemTypeService.getItemTypeList(listReqVO); |
| | | // 全量分类,用于解析上级分类(父分类可能不在筛选结果内) |
| | | Map<Long, String> parentCodeMap = new HashMap<>(); |
| | | Map<Long, String> parentNameMap = new HashMap<>(); |
| | | for (MesMdItemTypeDO type : itemTypeService.getItemTypeList(new MesMdItemTypeListReqVO())) { |
| | | parentCodeMap.put(type.getId(), type.getCode()); |
| | | parentNameMap.put(type.getId(), type.getName()); |
| | | } |
| | | // 组装导出 VO(补齐上级分类编码/名称、物料产品中文标识) |
| | | List<MesMdItemTypeExcelVO> excelList = new ArrayList<>(); |
| | | for (MesMdItemTypeDO type : list) { |
| | | MesMdItemTypeExcelVO excelVO = BeanUtils.toBean(type, MesMdItemTypeExcelVO.class); |
| | | if (type.getParentId() != null && !MesMdItemTypeDO.PARENT_ID_ROOT.equals(type.getParentId())) { |
| | | excelVO.setParentCode(parentCodeMap.get(type.getParentId())); |
| | | excelVO.setParentName(parentNameMap.get(type.getParentId())); |
| | | } |
| | | excelVO.setItemOrProduct(convertItemOrProductToText(type.getItemOrProduct())); |
| | | excelList.add(excelVO); |
| | | } |
| | | // 导出 Excel |
| | | ExcelUtils.write(response, "物料分类数据.xls", "物料分类", MesMdItemTypeExcelVO.class, excelList); |
| | | } |
| | | |
| | | /** |
| | | * 将物料/产品标识转换为中文名称 |
| | | * |
| | | * @param itemOrProduct 字典值:ITEM=物料,PRODUCT=产品 |
| | | * @return 中文名称 |
| | | */ |
| | | private String convertItemOrProductToText(String itemOrProduct) { |
| | | if (itemOrProduct == null) { |
| | | return null; |
| | | } |
| | | if ("ITEM".equals(itemOrProduct)) { |
| | | return "物料"; |
| | | } |
| | | if ("PRODUCT".equals(itemOrProduct)) { |
| | | return "产品"; |
| | | } |
| | | return itemOrProduct; |
| | | } |
| | | |
| | | @GetMapping("/get-import-template") |
| | | @Operation(summary = "获得导入物料产品分类模板") |
| | | @PreAuthorize("@ss.hasPermission('mes:md-item-type:import')") |
| | | public void importTemplate(HttpServletResponse response) throws IOException { |
| | | List<MesMdItemTypeImportExcelVO> list = Arrays.asList( |
| | | MesMdItemTypeImportExcelVO.builder().code("RAW").name("原材料").itemOrProduct("ITEM").sort(1).status(0).remark("示例:顶级分类").build(), |
| | | MesMdItemTypeImportExcelVO.builder().code("METAL").name("金属材料").parentCode("RAW").itemOrProduct("ITEM").sort(1).status(0).remark("示例:父分类编码为 RAW").build() |
| | | ); |
| | | ExcelUtils.write(response, "物料分类导入模板.xls", "物料分类", MesMdItemTypeImportExcelVO.class, list); |
| | | } |
| | | |
| | | @PostMapping("/import") |
| | | @Operation(summary = "导入物料产品分类") |
| | | @Parameters({ |
| | | @Parameter(name = "file", description = "Excel 文件", required = true), |
| | | @Parameter(name = "updateSupport", description = "是否支持更新,默认为 false", example = "true") |
| | | }) |
| | | @PreAuthorize("@ss.hasPermission('mes:md-item-type:import')") |
| | | public CommonResult<MesMdItemTypeImportRespVO> importExcel(@RequestParam("file") MultipartFile file, |
| | | @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception { |
| | | List<MesMdItemTypeImportExcelVO> list = ExcelUtils.read(file, MesMdItemTypeImportExcelVO.class); |
| | | return success(itemTypeService.importItemTypeList(list, updateSupport)); |
| | | } |
| | | |
| | | } |