yaowanxin
6 天以前 9d5128803ebebb5788f13924a5775ac16494dfef
src/main/java/com/ruoyi/warehouse/service/impl/WarehouseGoodsShelvesServiceImpl.java
@@ -9,6 +9,7 @@
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;
@@ -36,6 +37,7 @@
*/
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class WarehouseGoodsShelvesServiceImpl extends ServiceImpl<WarehouseGoodsShelvesMapper, WarehouseGoodsShelves>
    implements WarehouseGoodsShelvesService {
    @Autowired
@@ -48,23 +50,14 @@
    private DocumentationService documentationService;
    @Autowired
    private DocumentationMapper documentationMapper;
    @Override
    public IPage<WarehouseGoodsShelves> listPage(Page page, WarehouseGoodsShelves warehouseGoodsShelves) {
        return warehouseGoodsShelvesMapper.listPage(page,warehouseGoodsShelves);
    }
    @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 +79,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 +91,6 @@
     * @return 是否更新成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateRowcolById(WarehouseGoodsShelves warehouseGoodsShelves) {
        Long shelvesId = warehouseGoodsShelves.getId();
@@ -120,25 +106,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()) throw new RuntimeException("货架下有商品,不能删除");
        // 先更新货架主信息
        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 +122,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 +145,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()) 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());
@@ -196,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<WarehouseGoodsShelves> findList(WarehouseGoodsShelves warehouseGoodsShelves) {
        return warehouseGoodsShelvesMapper.listAll(warehouseGoodsShelves);
    }
}