yaowanxin
9 天以前 b0c345d2a74f47e5c34617051bcd61becde11b96
src/main/java/com/ruoyi/warehouse/service/impl/WarehouseGoodsShelvesServiceImpl.java
@@ -36,6 +36,7 @@
*/
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class WarehouseGoodsShelvesServiceImpl extends ServiceImpl<WarehouseGoodsShelvesMapper, WarehouseGoodsShelves>
    implements WarehouseGoodsShelvesService {
    @Autowired
@@ -54,17 +55,12 @@
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(WarehouseGoodsShelves warehouseGoodsShelves) {
        WarehouseGoodsShelves one = warehouseGoodsShelvesMapper.selectOne(new LambdaQueryWrapper<WarehouseGoodsShelves>().eq(WarehouseGoodsShelves::getId, warehouseGoodsShelves.getId()));
        // 1. 检查货架名称是否已存在
        if (one == null) {
            log.error("货架ID不存在");
            int insert = warehouseGoodsShelvesMapper.insert(warehouseGoodsShelves);
            if (insert <= 0) {
                log.error("货架主记录添加失败");
                return false;
            }
            if (insert <= 0) return false;
        }
        Long shelvesId = warehouseGoodsShelves.getId();
@@ -86,16 +82,10 @@
                // 使用批量插入方法替代循环单条插入
                warehouseGoodsShelvesRowcolService.saveBatch(rowcolList);
            } catch (Exception e) {
                log.error("货架[{}]的行列记录批量添加失败", shelvesId, e);
                // 抛出异常触发事务回滚
                throw e;
            }
        }
        log.info("货架[{}]添加成功,层数:{},排数:{}",
                warehouseGoodsShelves.getGoodsShelvesName(),
                warehouseGoodsShelves.getStorey(),
                warehouseGoodsShelves.getArrange());
        return true;
    }
    /**
@@ -104,7 +94,6 @@
     * @return 是否更新成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateRowcolById(WarehouseGoodsShelves warehouseGoodsShelves) {
        Long shelvesId = warehouseGoodsShelves.getId();
@@ -120,25 +109,14 @@
        Wrapper<Documentation> queryWrapper1 = new LambdaQueryWrapper<Documentation>()
                .in(Documentation::getWarehouseGoodsShelvesRowcolId, rowcolIds);
        List<Documentation> documentations = documentationMapper.selectList(queryWrapper1);
        if (!documentations.isEmpty()) {
            log.error("货架[ID:{}]上存在商品,禁止更新", shelvesId);
            return false;
        }
        if (!documentations.isEmpty()) return false;
        // 先更新货架主信息
        boolean isShelvesUpdated = updateById(warehouseGoodsShelves);
        if (!isShelvesUpdated) {
            log.warn("货架[{}]主信息更新失败", shelvesId);
            return false;
        }
        updateById(warehouseGoodsShelves);
        // 删除旧的行列记录
        boolean isOldRowcolRemoved = warehouseGoodsShelvesRowcolService.removeByIds(rowcolIds);
        if (!isOldRowcolRemoved) {
            log.error("货架[{}]的旧行列记录删除失败", shelvesId);
            return false;
        }
        warehouseGoodsShelvesRowcolService.removeByIds(rowcolIds);
        // 添加新的行列记录
        add(warehouseGoodsShelves);
        log.info("货架[{}]的行列信息更新成功", shelvesId);
        return true;
    }
    /**
@@ -147,14 +125,9 @@
     * @return 是否删除成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIds(List<Long> ids) {
        // 1. 先查询所有要删除的货架信息
        List<WarehouseGoodsShelves> shelvesList = warehouseGoodsShelvesMapper.selectBatchIds(ids);
        if (CollectionUtils.isEmpty(shelvesList)) {
            log.info("未查询到需删除的货架,ID列表:{}", ids);
            return true; // 无数据可删,视为成功
        }
        // 2. 检查所有货架是否存在商品(有商品则不允许删除)
        for (WarehouseGoodsShelves shelves : shelvesList) {
@@ -175,20 +148,13 @@
            Wrapper<Documentation> queryWrapper1 = new LambdaQueryWrapper<Documentation>()
                    .in(Documentation::getWarehouseGoodsShelvesRowcolId, rowcolIds);
            List<Documentation> documentations = documentationMapper.selectList(queryWrapper1);
            if (!documentations.isEmpty()) {
                log.error("货架[ID:{}]上存在商品,禁止删除", shelvesId);
                return false; // 任一货架有商品则终止删除
            }
            if (!documentations.isEmpty()) return false; // 任一货架有商品则终止删除
        }
        // 3. 先删除货架主记录
        boolean isShelvesDeleted = removeByIds(ids);
        if (!isShelvesDeleted) {
            log.error("货架主记录删除失败,ID列表:{}", ids);
            return false;
        }
        warehouseGoodsShelvesMapper.deleteBatchIds(ids);
        // 4. 批量删除所有关联的行列记录
        // 4. 批量删除所有行列记录
        List<Long> allShelvesIds = shelvesList.stream()
                .map(WarehouseGoodsShelves::getId)
                .collect(Collectors.toList());
@@ -196,17 +162,15 @@
        // 构建批量删除条件
        Wrapper<WarehouseGoodsShelvesRowcol> deleteWrapper = new LambdaQueryWrapper<WarehouseGoodsShelvesRowcol>()
                .in(WarehouseGoodsShelvesRowcol::getWarehouseGoodsShelvesId, allShelvesIds);
        boolean isRowcolDeleted = warehouseGoodsShelvesRowcolService.remove(deleteWrapper);
        if (!isRowcolDeleted) {
            log.warn("货架关联的行列记录删除失败,货架ID列表:{}", allShelvesIds);
            // 此处可根据业务需求决定是否回滚货架删除操作
        }
        log.info("货架批量删除成功,ID列表:{}", ids);
        warehouseGoodsShelvesRowcolService.remove(deleteWrapper);
        return true;
    }
    @Override
    public List<WarehouseGoodsShelves> list(WarehouseGoodsShelves warehouseGoodsShelves) {
        return warehouseGoodsShelvesMapper.list(warehouseGoodsShelves);
    }
}