yaowanxin
6 天以前 09e1c1155f4db6dc7a829e515337aa88ac4db81c
src/main/java/com/ruoyi/warehouse/service/impl/WarehouseGoodsShelvesServiceImpl.java
@@ -9,9 +9,13 @@
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.warehouse.dto.WarehouseGoodsShelvesDto;
import com.ruoyi.warehouse.mapper.DocumentationMapper;
import com.ruoyi.warehouse.mapper.WarehouseGoodsShelvesMapper;
import com.ruoyi.warehouse.mapper.WarehouseGoodsShelvesRowcolMapper;
import com.ruoyi.warehouse.pojo.Documentation;
import com.ruoyi.warehouse.pojo.WarehouseGoodsShelvesRowcol;
import com.ruoyi.warehouse.service.DocumentationService;
import com.ruoyi.warehouse.service.WarehouseGoodsShelvesRowcolService;
import com.ruoyi.warehouse.service.WarehouseGoodsShelvesService;
import lombok.extern.slf4j.Slf4j;
@@ -33,6 +37,7 @@
*/
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class WarehouseGoodsShelvesServiceImpl extends ServiceImpl<WarehouseGoodsShelvesMapper, WarehouseGoodsShelves>
    implements WarehouseGoodsShelvesService {
    @Autowired
@@ -41,19 +46,20 @@
    private WarehouseGoodsShelvesRowcolMapper warehouseGoodsShelvesRowcolMapper;
    @Autowired
    private WarehouseGoodsShelvesMapper warehouseGoodsShelvesMapper;
    @Override
    public IPage<WarehouseGoodsShelves> listPage(Page page, WarehouseGoodsShelves warehouseGoodsShelves) {
        return warehouseGoodsShelvesMapper.listPage(page,warehouseGoodsShelves);
    }
    @Autowired
    private DocumentationService documentationService;
    @Autowired
    private DocumentationMapper documentationMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(WarehouseGoodsShelves warehouseGoodsShelves) {
        int insert = warehouseGoodsShelvesMapper.insert(warehouseGoodsShelves);
        if (insert <= 0) {
            log.error("货架主记录添加失败");
            return false;
        WarehouseGoodsShelves one = warehouseGoodsShelvesMapper.selectOne(new LambdaQueryWrapper<WarehouseGoodsShelves>().eq(WarehouseGoodsShelves::getId, warehouseGoodsShelves.getId()));
        // 1. 检查货架名称是否已存在
        if (one == null) {
            int insert = warehouseGoodsShelvesMapper.insert(warehouseGoodsShelves);
            if (insert <= 0) return false;
        }
        Long shelvesId = warehouseGoodsShelves.getId();
        Long warehouseId = warehouseGoodsShelves.getWarehouseId();
        // 3. 批量创建行列记录
@@ -73,16 +79,10 @@
                // 使用批量插入方法替代循环单条插入
                warehouseGoodsShelvesRowcolService.saveBatch(rowcolList);
            } catch (Exception e) {
                log.error("货架[{}]的行列记录批量添加失败", shelvesId, e);
                // 抛出异常触发事务回滚
                throw e;
            }
        }
        log.info("货架[{}]添加成功,层数:{},排数:{}",
                warehouseGoodsShelves.getGoodsShelvesName(),
                warehouseGoodsShelves.getStorey(),
                warehouseGoodsShelves.getArrange());
        return true;
    }
    /**
@@ -92,54 +92,28 @@
     */
    @Override
    public boolean updateRowcolById(WarehouseGoodsShelves warehouseGoodsShelves) {
        // 提取关键ID,减少重复调用
        Long shelvesId = warehouseGoodsShelves.getId();
        Long warehouseId = warehouseGoodsShelves.getWarehouseId();
        // 先更新货架主信息
        boolean isShelvesUpdated = updateById(warehouseGoodsShelves);
        if (!isShelvesUpdated) {
            log.warn("货架[{}]主信息更新失败", shelvesId);
            return false;
        }
        // 构建查询条件:查询该货架下的所有行列记录
        WarehouseGoodsShelvesRowcol queryRowcol = new WarehouseGoodsShelvesRowcol();
        queryRowcol.setWarehouseGoodsShelvesId(shelvesId);
        queryRowcol.setWarehouseId(warehouseId);
        IPage<WarehouseGoodsShelvesRowcol> rowcolPage = warehouseGoodsShelvesRowcolService.listPage(new Page<>(), queryRowcol);
        List<WarehouseGoodsShelvesRowcol> oldRowcolList = rowcolPage.getRecords();
        // 如果没有旧的行列记录,直接添加新记录
        if (oldRowcolList.isEmpty()) {
            add(warehouseGoodsShelves);
            return true;
        }
        // 检查货架上是否有商品(有商品则不允许更新)
        boolean hasGoodsOnShelves = oldRowcolList.stream()
                .anyMatch(rowcol -> rowcol.getDocumentationId() != null);
        if (hasGoodsOnShelves) {
            log.error("货架[{}]上存在商品,不允许更新行列信息", shelvesId);
            return false;
        }
        // 提取旧行列记录的ID集合,用于删除操作
        List<Long> oldRowcolIds = oldRowcolList.stream()
        Wrapper<WarehouseGoodsShelvesRowcol> queryWrapper = new LambdaQueryWrapper<WarehouseGoodsShelvesRowcol>()
                .eq(WarehouseGoodsShelvesRowcol::getWarehouseGoodsShelvesId, shelvesId);
        List<WarehouseGoodsShelvesRowcol> rowcolList = warehouseGoodsShelvesRowcolService.list(queryWrapper);
        //获得rowcolList中所有的Id
        List<Long> rowcolIds = rowcolList.stream()
                .map(WarehouseGoodsShelvesRowcol::getId)
                .collect(Collectors.toList());
        // 3. 检查是否有商品
        Wrapper<Documentation> queryWrapper1 = new LambdaQueryWrapper<Documentation>()
                .in(Documentation::getWarehouseGoodsShelvesRowcolId, rowcolIds);
        List<Documentation> documentations = documentationMapper.selectList(queryWrapper1);
        if (!documentations.isEmpty()) throw new RuntimeException("货架下有商品,不能删除");
        // 先更新货架主信息
        updateById(warehouseGoodsShelves);
        // 删除旧的行列记录
        boolean isOldRowcolRemoved = warehouseGoodsShelvesRowcolService.removeByIds(oldRowcolIds);
        if (!isOldRowcolRemoved) {
            log.error("货架[{}]的旧行列记录删除失败", shelvesId);
            return false;
        }
        warehouseGoodsShelvesRowcolService.removeByIds(rowcolIds);
        // 添加新的行列记录
        add(warehouseGoodsShelves);
        log.info("货架[{}]的行列信息更新成功", shelvesId);
        return true;
    }
    /**
@@ -151,42 +125,33 @@
    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) {
            Long shelvesId = shelves.getId();
            Long warehouseId = shelves.getWarehouseId();
            // 构建查询条件:查询当前货架的所有行列记录
            Wrapper<WarehouseGoodsShelvesRowcol> queryWrapper = new LambdaQueryWrapper<WarehouseGoodsShelvesRowcol>()
                    .eq(WarehouseGoodsShelvesRowcol::getWarehouseGoodsShelvesId, shelvesId)
                    .eq(WarehouseGoodsShelvesRowcol::getWarehouseId, warehouseId);
                    .eq(WarehouseGoodsShelvesRowcol::getWarehouseGoodsShelvesId, shelvesId);
            // 查询行列记录(仅需判断是否有商品,无需分页)
            List<WarehouseGoodsShelvesRowcol> rowcolList = warehouseGoodsShelvesRowcolService.list(queryWrapper);
            // 检查是否有商品
            boolean hasGoods = rowcolList.stream()
                    .anyMatch(rowcol -> rowcol.getDocumentationId() != null && rowcol.getDocumentationId()!=0);
            if (hasGoods) {
                log.error("货架[ID:{}]上存在商品,禁止删除", shelvesId);
                return false; // 任一货架有商品则终止删除
            if (CollectionUtils.isEmpty(rowcolList)) {
                continue; // 无关联记录,跳过检查
            }
            //获得rowcolList中所有的Id
            List<Long> rowcolIds = rowcolList.stream()
                    .map(WarehouseGoodsShelvesRowcol::getId)
                    .collect(Collectors.toList());
            // 3. 检查是否有商品
            Wrapper<Documentation> queryWrapper1 = new LambdaQueryWrapper<Documentation>()
                    .in(Documentation::getWarehouseGoodsShelvesRowcolId, rowcolIds);
            List<Documentation> documentations = documentationMapper.selectList(queryWrapper1);
            if (!documentations.isEmpty()) throw new RuntimeException("货架下有商品,不能删除"); // 任一货架有商品则终止删除
        }
        // 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());
@@ -194,17 +159,16 @@
        // 构建批量删除条件
        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<WarehouseGoodsShelvesDto> findList(WarehouseGoodsShelves warehouseGoodsShelves) {
        return warehouseGoodsShelvesMapper.listAll(warehouseGoodsShelves);
    }
}