云
2026-10-25 d54fbc9d4f8981fa0f7f613b33a28a8cd4f764ea
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/warehouse/MesWmWarehouseLocationServiceImpl.java
@@ -2,13 +2,17 @@
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.location.MesWmWarehouseLocationImportExcelVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.warehouse.vo.location.MesWmWarehouseLocationImportRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.warehouse.vo.location.MesWmWarehouseLocationPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.warehouse.vo.location.MesWmWarehouseLocationSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.wm.warehouse.MesWmWarehouseDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.wm.warehouse.MesWmWarehouseLocationDO;
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;
@@ -16,11 +20,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.*;
@@ -34,6 +42,9 @@
    @Resource
    private MesWmWarehouseLocationMapper locationMapper;
    @Resource
    private MesWmWarehouseMapper warehouseMapper;
    @Resource
    private MesWmWarehouseAreaService areaService;
@@ -198,4 +209,91 @@
        return null;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MesWmWarehouseLocationImportRespVO importWarehouseLocationList(List<MesWmWarehouseLocationImportExcelVO> importList, boolean updateSupport) {
        // 校验导入数据不能为空
        if (CollUtil.isEmpty(importList)) {
            throw exception(WM_WAREHOUSE_LOCATION_IMPORT_LIST_IS_EMPTY);
        }
        MesWmWarehouseLocationImportRespVO respVO = MesWmWarehouseLocationImportRespVO.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 (MesWmWarehouseLocationDO.WIP_VIRTUAL_LOCATION.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;
            }
            Long warehouseId = warehouse.getId();
            MesWmWarehouseLocationDO existingByCode = locationMapper.selectByCode(warehouseId, code);
            MesWmWarehouseLocationDO existingByName = locationMapper.selectByName(warehouseId, importItem.getName());
            if (existingByCode != null && updateSupport) {
                // 名称唯一校验(排除自己)
                if (existingByName != null && !existingByCode.getId().equals(existingByName.getId())) {
                    respVO.getFailureCodes().put(key, "库区名称已存在:" + importItem.getName());
                    return;
                }
                // 更新
                MesWmWarehouseLocationDO updateObj = new MesWmWarehouseLocationDO();
                updateObj.setId(existingByCode.getId());
                updateObj.setName(importItem.getName());
                updateObj.setWarehouseId(warehouseId);
                updateObj.setArea(importItem.getArea());
                updateObj.setFrozen(importItem.getFrozen());
                updateObj.setRemark(importItem.getRemark());
                locationMapper.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;
                }
                // 创建
                MesWmWarehouseLocationDO createObj = new MesWmWarehouseLocationDO();
                createObj.setId(null);
                createObj.setCode(code);
                createObj.setName(importItem.getName());
                createObj.setWarehouseId(warehouseId);
                createObj.setArea(importItem.getArea());
                createObj.setFrozen(ObjUtil.defaultIfNull(importItem.getFrozen(), Boolean.FALSE));
                createObj.setRemark(importItem.getRemark());
                locationMapper.insert(createObj);
                // 自动生成条码
                barcodeService.autoGenerateBarcode(BarcodeBizTypeEnum.LOCATION.getValue(),
                        createObj.getId(), createObj.getCode(), createObj.getName());
                respVO.getCreateCodes().add(code);
            }
        });
        return respVO;
    }
}