liu
2026-09-02 1fb5fc73e8df33b25337ab34f669ab03e0d9ce06
yudao-module-mdm/src/main/java/cn/iocoder/yudao/module/mdm/controller/admin/item/MdmItemController.java
@@ -1,9 +1,12 @@
package cn.iocoder.yudao.module.mdm.controller.admin.item;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
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.mdm.controller.admin.item.vo.MdmItemExcelVO;
import cn.iocoder.yudao.module.mdm.controller.admin.item.vo.MdmItemImportExcelVO;
import cn.iocoder.yudao.module.mdm.controller.admin.item.vo.MdmItemImportRespVO;
import cn.iocoder.yudao.module.mdm.controller.admin.item.vo.MdmItemPageReqVO;
@@ -15,6 +18,7 @@
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.dal.dataobject.unit.MdmUnitMeasureDO;
import cn.iocoder.yudao.module.mdm.enums.MdmItemTypeEnum;
import cn.iocoder.yudao.module.mdm.service.brand.MdmBrandService;
import cn.iocoder.yudao.module.mdm.service.category.MdmItemCategoryService;
import cn.iocoder.yudao.module.mdm.service.item.MdmItemBatchConfigService;
@@ -39,6 +43,7 @@
import java.util.*;
import java.util.stream.Collectors;
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.convertSet;
@@ -136,14 +141,48 @@
    @PreAuthorize("@ss.hasPermission('mdm:item:query')")
    public CommonResult<PageResult<MdmItemRespVO>> getItemPage(@Valid MdmItemPageReqVO pageReqVO) {
        PageResult<MdmItemDO> pageResult = itemService.getItemPage(pageReqVO);
        // 转换并补齐关联名称
        List<MdmItemRespVO> respList = convertAndEnrichItemList(pageResult.getList());
        return success(new PageResult<>(respList, pageResult.getTotal()));
    }
    @GetMapping("/export-excel")
    @Operation(summary = "导出物料 Excel")
    @PreAuthorize("@ss.hasPermission('mdm:item:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportItemExcel(@Valid MdmItemPageReqVO pageReqVO,
                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        // 复用分页查询的过滤与关联填充逻辑,保证导出数据与列表一致
        List<MdmItemRespVO> respList = convertAndEnrichItemList(itemService.getItemPage(pageReqVO).getList());
        List<MdmItemExcelVO> excelList = new ArrayList<>();
        for (MdmItemRespVO resp : respList) {
            MdmItemExcelVO excelVO = BeanUtils.toBean(resp, MdmItemExcelVO.class);
            MdmItemTypeEnum itemType = MdmItemTypeEnum.fromType(resp.getItemType());
            excelVO.setItemTypeName(itemType != null ? itemType.getName() : null);
            excelList.add(excelVO);
        }
        // 导出 Excel
        ExcelUtils.write(response, "物料数据.xls", "物料", MdmItemExcelVO.class, excelList);
    }
    /**
     * 将物料 DO 列表转换为已补齐关联名称(分类、主/辅计量单位、默认仓库)及同步状态的 RespVO 列表
     *
     * 分页查询与 Excel 导出共用,保证导出内容与列表展示一致
     */
    private List<MdmItemRespVO> convertAndEnrichItemList(List<MdmItemDO> itemList) {
        if (itemList == null || itemList.isEmpty()) {
            return Collections.emptyList();
        }
        // 关联查询分类名称
        Map<Long, MdmItemCategoryDO> categoryMap = itemCategoryService.getItemCategoryMap(
                convertSet(pageResult.getList(), MdmItemDO::getCategoryId));
                convertSet(itemList, MdmItemDO::getCategoryId));
        // 关联查询计量单位名称
        Map<Long, MdmUnitMeasureDO> unitMap = unitMeasureService.getUnitMeasureMap(
                convertSet(pageResult.getList(), MdmItemDO::getUnitMeasureId));
                convertSet(itemList, MdmItemDO::getUnitMeasureId));
        // 转换
        List<MdmItemRespVO> respList = BeanUtils.toBean(pageResult.getList(), MdmItemRespVO.class);
        List<MdmItemRespVO> respList = BeanUtils.toBean(itemList, MdmItemRespVO.class);
        // 批量查询同步状态
        Set<Long> allIds = convertSet(respList, MdmItemRespVO::getId);
        Set<Long> syncedIds = mesMdItemApi.getSyncedMdmItemIds(allIds);
@@ -189,7 +228,7 @@
            // 同步状态
            resp.setSyncedMes(syncedIds.contains(resp.getId()));
        });
        return success(new PageResult<>(respList, pageResult.getTotal()));
        return respList;
    }
    @GetMapping("/list-by-ids")
@@ -277,6 +316,7 @@
    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入物料模板")
    @PreAuthorize("@ss.hasPermission('mdm:item:import')")
    public void importTemplate(HttpServletResponse response) throws IOException {
        List<MdmItemImportExcelVO> list = Arrays.asList(
                MdmItemImportExcelVO.builder().code("ITEM001").name("示例物料").specification("规格").status(0).isBatchManaged(false).build()