yaowanxin
9 天以前 7d188cc913d5ea547c5e99ae434ea3b1cf4e818a
src/main/java/com/ruoyi/warehouse/service/impl/WarehouseGoodsShelvesServiceImpl.java
@@ -9,9 +9,12 @@
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.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;
@@ -41,6 +44,10 @@
    private WarehouseGoodsShelvesRowcolMapper warehouseGoodsShelvesRowcolMapper;
    @Autowired
    private WarehouseGoodsShelvesMapper warehouseGoodsShelvesMapper;
    @Autowired
    private DocumentationService documentationService;
    @Autowired
    private DocumentationMapper documentationMapper;
    @Override
    public IPage<WarehouseGoodsShelves> listPage(Page page, WarehouseGoodsShelves warehouseGoodsShelves) {
        return warehouseGoodsShelvesMapper.listPage(page,warehouseGoodsShelves);
@@ -49,11 +56,17 @@
    @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) {
            log.error("货架ID不存在");
            int insert = warehouseGoodsShelvesMapper.insert(warehouseGoodsShelves);
            if (insert <= 0) {
                log.error("货架主记录添加失败");
                return false;
            }
        }
        Long shelvesId = warehouseGoodsShelves.getId();
        Long warehouseId = warehouseGoodsShelves.getWarehouseId();
        // 3. 批量创建行列记录
@@ -91,52 +104,38 @@
     * @return 是否更新成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateRowcolById(WarehouseGoodsShelves warehouseGoodsShelves) {
        // 提取关键ID,减少重复调用
        Long shelvesId = warehouseGoodsShelves.getId();
        Long warehouseId = warehouseGoodsShelves.getWarehouseId();
        // 构建查询条件:查询该货架下的所有行列记录
        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()) {
            log.error("货架[ID:{}]上存在商品,禁止更新", shelvesId);
            return false;
        }
        // 先更新货架主信息
        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()
                .map(WarehouseGoodsShelvesRowcol::getId)
                .collect(Collectors.toList());
        // 删除旧的行列记录
        boolean isOldRowcolRemoved = warehouseGoodsShelvesRowcolService.removeByIds(oldRowcolIds);
        boolean isOldRowcolRemoved = warehouseGoodsShelvesRowcolService.removeByIds(rowcolIds);
        if (!isOldRowcolRemoved) {
            log.error("货架[{}]的旧行列记录删除失败", shelvesId);
            return false;
        }
        // 添加新的行列记录
        add(warehouseGoodsShelves);
        log.info("货架[{}]的行列信息更新成功", shelvesId);
@@ -148,6 +147,7 @@
     * @return 是否删除成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIds(List<Long> ids) {
        // 1. 先查询所有要删除的货架信息
        List<WarehouseGoodsShelves> shelvesList = warehouseGoodsShelvesMapper.selectBatchIds(ids);
@@ -159,21 +159,23 @@
        // 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) {
            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()) {
                log.error("货架[ID:{}]上存在商品,禁止删除", shelvesId);
                return false; // 任一货架有商品则终止删除
            }