liu
2026-10-24 3990ae574c221e3764ce9cbc2e4e3eb71b7b1b52
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/warehouse/MesWmWarehouseServiceImpl.java
@@ -2,8 +2,11 @@
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mes.controller.admin.wm.warehouse.vo.MesWmWarehouseImportExcelVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.warehouse.vo.MesWmWarehouseImportRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.warehouse.vo.MesWmWarehousePageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.warehouse.vo.MesWmWarehouseSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.wm.warehouse.MesWmWarehouseDO;
@@ -12,14 +15,20 @@
import cn.iocoder.yudao.module.mes.service.md.workstation.MesMdWorkstationService;
import cn.iocoder.yudao.module.mes.service.wm.barcode.MesWmBarcodeService;
import cn.iocoder.yudao.module.mes.service.wm.materialstock.MesWmMaterialStockService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
@@ -33,6 +42,9 @@
    @Resource
    private MesWmWarehouseMapper warehouseMapper;
    @Resource
    private AdminUserApi adminUserApi;
    @Resource
    private MesWmWarehouseLocationService locationService;
@@ -183,4 +195,103 @@
        return null;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MesWmWarehouseImportRespVO importWarehouseList(List<MesWmWarehouseImportExcelVO> importList, boolean updateSupport) {
        // 校验导入数据不能为空
        if (CollUtil.isEmpty(importList)) {
            throw exception(WM_WAREHOUSE_IMPORT_LIST_IS_EMPTY);
        }
        MesWmWarehouseImportRespVO respVO = MesWmWarehouseImportRespVO.builder()
                .createCodes(new ArrayList<>()).updateCodes(new ArrayList<>())
                .failureCodes(new LinkedHashMap<>()).build();
        // 索引,用于错误提示
        AtomicInteger index = new AtomicInteger(1);
        importList.forEach(importItem -> {
            int currentIndex = index.getAndIncrement();
            String code = importItem.getCode();
            String key = StrUtil.blankToDefault(code, "第 " + currentIndex + " 行");
            // 校验编码必填
            if (StrUtil.isBlank(code)) {
                respVO.getFailureCodes().put(key, "仓库编码不能为空");
                return;
            }
            // 校验名称必填
            if (StrUtil.isBlank(importItem.getName())) {
                respVO.getFailureCodes().put(key, "仓库名称不能为空");
                return;
            }
            // 校验虚拟仓库不允许导入
            if (MesWmWarehouseDO.WIP_VIRTUAL_WAREHOUSE.equals(code)) {
                respVO.getFailureCodes().put(key, "虚拟仓库不允许导入");
                return;
            }
            // 按名称解析负责人
            Long chargeUserId = null;
            if (StrUtil.isNotBlank(importItem.getChargeUserName())) {
                chargeUserId = resolveChargeUserId(importItem.getChargeUserName());
                if (chargeUserId == null) {
                    respVO.getFailureCodes().put(key, "负责人不存在:" + importItem.getChargeUserName());
                    return;
                }
            }
            MesWmWarehouseDO existingByCode = warehouseMapper.selectByCode(code);
            MesWmWarehouseDO existingByName = warehouseMapper.selectByName(importItem.getName());
            if (existingByCode != null && updateSupport) {
                // 名称唯一校验(排除自己)
                if (existingByName != null && !existingByCode.getId().equals(existingByName.getId())) {
                    respVO.getFailureCodes().put(key, "仓库名称已存在:" + importItem.getName());
                    return;
                }
                // 更新
                MesWmWarehouseDO updateObj = new MesWmWarehouseDO();
                updateObj.setId(existingByCode.getId());
                updateObj.setName(importItem.getName());
                updateObj.setType(importItem.getType());
                updateObj.setAddress(importItem.getAddress());
                updateObj.setArea(importItem.getArea());
                updateObj.setChargeUserId(chargeUserId);
                updateObj.setFrozen(importItem.getFrozen());
                updateObj.setRemark(importItem.getRemark());
                warehouseMapper.updateById(updateObj);
                respVO.getUpdateCodes().add(code);
            } else if (existingByCode != null) {
                respVO.getFailureCodes().put(key, "仓库编码已存在:" + code);
            } else {
                // 名称唯一校验
                if (existingByName != null) {
                    respVO.getFailureCodes().put(key, "仓库名称已存在:" + importItem.getName());
                    return;
                }
                // 创建
                MesWmWarehouseDO createObj = new MesWmWarehouseDO();
                createObj.setId(null);
                createObj.setCode(code);
                createObj.setName(importItem.getName());
                createObj.setType(importItem.getType());
                createObj.setAddress(importItem.getAddress());
                createObj.setArea(importItem.getArea());
                createObj.setChargeUserId(chargeUserId);
                createObj.setFrozen(ObjUtil.defaultIfNull(importItem.getFrozen(), Boolean.FALSE));
                createObj.setRemark(importItem.getRemark());
                warehouseMapper.insert(createObj);
                // 自动生成条码
                barcodeService.autoGenerateBarcode(BarcodeBizTypeEnum.WAREHOUSE.getValue(),
                        createObj.getId(), createObj.getCode(), createObj.getName());
                respVO.getCreateCodes().add(code);
            }
        });
        return respVO;
    }
    private Long resolveChargeUserId(String chargeUserName) {
        List<AdminUserRespDTO> users = adminUserApi.getUserListByNickname(chargeUserName);
        for (AdminUserRespDTO user : users) {
            if (chargeUserName.equals(user.getNickname())) {
                return user.getId();
            }
        }
        return null;
    }
}