| | |
| | | import cn.iocoder.yudao.module.mdm.dal.dataobject.category.MdmItemCategoryDO; |
| | | import cn.iocoder.yudao.module.mdm.dal.dataobject.item.MdmItemBatchConfigDO; |
| | | import cn.iocoder.yudao.module.mdm.dal.dataobject.item.MdmItemDO; |
| | | import cn.iocoder.yudao.module.mdm.controller.admin.unit.vo.MdmUnitMeasurePageReqVO; |
| | | import cn.iocoder.yudao.module.mdm.dal.dataobject.unit.MdmUnitMeasureDO; |
| | | import cn.iocoder.yudao.module.mdm.enums.MdmItemTypeEnum; |
| | | import cn.iocoder.yudao.module.mdm.service.brand.MdmBrandService; |
| | |
| | | @Operation(summary = "获得导入物料模板") |
| | | @PreAuthorize("@ss.hasPermission('mdm:item:import')") |
| | | public void importTemplate(HttpServletResponse response) throws IOException { |
| | | // 分类编码、计量单位编码为必填项,示例取当前系统真实编码,避免照模板导入直接失败 |
| | | String sampleCategoryCode = ""; |
| | | try { |
| | | List<MdmItemCategoryDO> cats = itemCategoryService.getSimpleItemList(); |
| | | if (cats != null && !cats.isEmpty()) { |
| | | sampleCategoryCode = cats.get(0).getCode(); |
| | | } |
| | | } catch (Exception ignored) { |
| | | // 取不到留空,不影响模板下载 |
| | | } |
| | | String sampleUnitCode = ""; |
| | | try { |
| | | MdmUnitMeasurePageReqVO unitReq = new MdmUnitMeasurePageReqVO(); |
| | | unitReq.setPageNo(1); |
| | | unitReq.setPageSize(1); |
| | | List<MdmUnitMeasureDO> units = unitMeasureService.getUnitMeasurePage(unitReq).getList(); |
| | | if (units != null && !units.isEmpty()) { |
| | | sampleUnitCode = units.get(0).getCode(); |
| | | } |
| | | } catch (Exception ignored) { |
| | | // 同上 |
| | | } |
| | | |
| | | List<MdmItemImportExcelVO> list = Arrays.asList( |
| | | MdmItemImportExcelVO.builder().code("ITEM001").name("示例物料").specification("规格").status(0).isBatchManaged(false).build() |
| | | MdmItemImportExcelVO.builder() |
| | | .code("ITEM-EXAMPLE").name("示例物料(请替换)").specification("规格") |
| | | .categoryCode(sampleCategoryCode).unitMeasureCode(sampleUnitCode) |
| | | .status(0).isBatchManaged(false) |
| | | .remark("示例数据,导入前请替换;分类编码/单位编码请填写系统中已存在的编码") |
| | | .build() |
| | | ); |
| | | ExcelUtils.write(response, "物料导入模板.xls", "物料", MdmItemImportExcelVO.class, list); |
| | | ExcelUtils.writeTemplate(response, "物料导入模板.xls", "物料", MdmItemImportExcelVO.class, list); |
| | | } |
| | | |
| | | @PostMapping("/import") |
| | |
| | | @PreAuthorize("@ss.hasPermission('mdm:item:import')") |
| | | public CommonResult<MdmItemImportRespVO> importExcel(@RequestParam("file") MultipartFile file, |
| | | @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception { |
| | | List<MdmItemImportExcelVO> list = ExcelUtils.read(file, MdmItemImportExcelVO.class); |
| | | List<MdmItemImportExcelVO> list; |
| | | try { |
| | | list = ExcelUtils.read(file, MdmItemImportExcelVO.class); |
| | | } catch (cn.idev.excel.exception.ExcelDataConvertException e) { |
| | | int row = e.getRowIndex() == null ? 0 : e.getRowIndex() + 1; |
| | | int col = e.getColumnIndex() == null ? 0 : e.getColumnIndex() + 1; |
| | | String cellVal = e.getCellData() == null ? "" : String.valueOf(e.getCellData().getStringValue()); |
| | | throw new cn.iocoder.yudao.framework.common.exception.ServiceException(400, |
| | | "Excel 第 " + row + " 行第 " + col + " 列数据『" + cellVal + "』格式不正确,请检查该列内容(例如“状态”请填 0/1 或 启用/停用)"); |
| | | } catch (Exception e) { |
| | | throw new cn.iocoder.yudao.framework.common.exception.ServiceException(400, |
| | | "Excel 解析失败:" + e.getMessage() + ",请使用系统“下载导入模板”并按模板填写。"); |
| | | } |
| | | return success(itemService.importItemList(list, updateSupport)); |
| | | } |
| | | |