liu
2026-10-24 3990ae574c221e3764ce9cbc2e4e3eb71b7b1b52
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/warehouse/MesWmWarehouseAreaServiceImpl.java
@@ -3,14 +3,20 @@
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
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.area.MesWmWarehouseAreaImportExcelVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.warehouse.vo.area.MesWmWarehouseAreaImportRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.warehouse.vo.area.MesWmWarehouseAreaPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.warehouse.vo.area.MesWmWarehouseAreaSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.wm.warehouse.MesWmWarehouseAreaDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.wm.warehouse.MesWmWarehouseLocationDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.wm.warehouse.MesWmWarehouseDO;
import cn.iocoder.yudao.module.mes.dal.mysql.wm.warehouse.MesWmWarehouseAreaMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.wm.warehouse.MesWmWarehouseLocationMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.wm.warehouse.MesWmWarehouseMapper;
import cn.iocoder.yudao.module.mes.enums.wm.BarcodeBizTypeEnum;
import cn.iocoder.yudao.module.mes.service.md.workstation.MesMdWorkstationService;
import cn.iocoder.yudao.module.mes.service.wm.barcode.MesWmBarcodeService;
@@ -18,11 +24,15 @@
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.*;
@@ -36,6 +46,12 @@
    @Resource
    private MesWmWarehouseAreaMapper areaMapper;
    @Resource
    private MesWmWarehouseMapper warehouseMapper;
    @Resource
    private MesWmWarehouseLocationMapper locationMapper;
    @Resource
    @Lazy
@@ -238,4 +254,117 @@
        return null;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MesWmWarehouseAreaImportRespVO importWarehouseAreaList(List<MesWmWarehouseAreaImportExcelVO> importList, boolean updateSupport) {
        // 校验导入数据不能为空
        if (CollUtil.isEmpty(importList)) {
            throw exception(WM_WAREHOUSE_AREA_IMPORT_LIST_IS_EMPTY);
        }
        MesWmWarehouseAreaImportRespVO respVO = MesWmWarehouseAreaImportRespVO.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 (MesWmWarehouseAreaDO.WIP_VIRTUAL_AREA.equals(code)) {
                respVO.getFailureCodes().put(key, "虚拟库位不允许导入");
                return;
            }
            // 按名称解析所属仓库
            if (StrUtil.isBlank(importItem.getWarehouseName())) {
                respVO.getFailureCodes().put(key, "所属仓库不能为空");
                return;
            }
            MesWmWarehouseDO warehouse = warehouseMapper.selectByName(importItem.getWarehouseName());
            if (warehouse == null) {
                respVO.getFailureCodes().put(key, "仓库不存在:" + importItem.getWarehouseName());
                return;
            }
            // 按名称解析所属库区
            if (StrUtil.isBlank(importItem.getLocationName())) {
                respVO.getFailureCodes().put(key, "所属库区不能为空");
                return;
            }
            MesWmWarehouseLocationDO location = locationMapper.selectByName(warehouse.getId(), importItem.getLocationName());
            if (location == null) {
                respVO.getFailureCodes().put(key, "库区不存在:" + importItem.getLocationName());
                return;
            }
            Long locationId = location.getId();
            MesWmWarehouseAreaDO existingByCode = areaMapper.selectByCode(locationId, code);
            MesWmWarehouseAreaDO existingByName = areaMapper.selectByName(locationId, importItem.getName());
            if (existingByCode != null && updateSupport) {
                // 名称唯一校验(排除自己)
                if (existingByName != null && !existingByCode.getId().equals(existingByName.getId())) {
                    respVO.getFailureCodes().put(key, "库位名称已存在:" + importItem.getName());
                    return;
                }
                // 更新
                MesWmWarehouseAreaDO updateObj = new MesWmWarehouseAreaDO();
                updateObj.setId(existingByCode.getId());
                updateObj.setName(importItem.getName());
                updateObj.setLocationId(locationId);
                updateObj.setType(importItem.getType());
                updateObj.setArea(importItem.getArea());
                updateObj.setMaxLoad(importItem.getMaxLoad());
                updateObj.setPositionX(importItem.getPositionX());
                updateObj.setPositionY(importItem.getPositionY());
                updateObj.setPositionZ(importItem.getPositionZ());
                updateObj.setStatus(importItem.getStatus());
                updateObj.setFrozen(importItem.getFrozen());
                updateObj.setAllowItemMixing(importItem.getAllowItemMixing());
                updateObj.setAllowBatchMixing(importItem.getAllowBatchMixing());
                updateObj.setRemark(importItem.getRemark());
                areaMapper.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;
                }
                // 创建
                MesWmWarehouseAreaDO createObj = new MesWmWarehouseAreaDO();
                createObj.setId(null);
                createObj.setCode(code);
                createObj.setName(importItem.getName());
                createObj.setLocationId(locationId);
                createObj.setType(importItem.getType());
                createObj.setArea(importItem.getArea());
                createObj.setMaxLoad(importItem.getMaxLoad());
                createObj.setPositionX(importItem.getPositionX());
                createObj.setPositionY(importItem.getPositionY());
                createObj.setPositionZ(importItem.getPositionZ());
                createObj.setStatus(ObjUtil.defaultIfNull(importItem.getStatus(), CommonStatusEnum.ENABLE.getStatus()));
                createObj.setFrozen(ObjUtil.defaultIfNull(importItem.getFrozen(), Boolean.FALSE));
                createObj.setAllowItemMixing(ObjUtil.defaultIfNull(importItem.getAllowItemMixing(), Boolean.TRUE));
                createObj.setAllowBatchMixing(ObjUtil.defaultIfNull(importItem.getAllowBatchMixing(), Boolean.TRUE));
                createObj.setRemark(importItem.getRemark());
                areaMapper.insert(createObj);
                // 自动生成条码
                barcodeService.autoGenerateBarcode(BarcodeBizTypeEnum.AREA.getValue(),
                        createObj.getId(), createObj.getCode(), createObj.getName());
                respVO.getCreateCodes().add(code);
            }
        });
        return respVO;
    }
}