package com.ruoyi.warehouse.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.metadata.IPage; 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.WarehouseGoodsShelvesMapper; import com.ruoyi.warehouse.mapper.WarehouseGoodsShelvesRowcolMapper; import com.ruoyi.warehouse.pojo.WarehouseGoodsShelvesRowcol; import com.ruoyi.warehouse.service.WarehouseGoodsShelvesRowcolService; import com.ruoyi.warehouse.service.WarehouseGoodsShelvesService; import lombok.extern.slf4j.Slf4j; import com.ruoyi.warehouse.pojo.WarehouseGoodsShelves; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; /** * @author 86151 * @description 针对表【warehouse_goods_shelves(仓库货架表)】的数据库操作Service实现 * @createDate 2025-08-13 11:49:02 */ @Service @Slf4j public class WarehouseGoodsShelvesServiceImpl extends ServiceImpl implements WarehouseGoodsShelvesService { @Autowired private WarehouseGoodsShelvesRowcolService warehouseGoodsShelvesRowcolService; @Autowired private WarehouseGoodsShelvesRowcolMapper warehouseGoodsShelvesRowcolMapper; @Autowired private WarehouseGoodsShelvesMapper warehouseGoodsShelvesMapper; @Override public IPage listPage(Page page, WarehouseGoodsShelves warehouseGoodsShelves) { return warehouseGoodsShelvesMapper.listPage(page,warehouseGoodsShelves); } @Override public boolean add(WarehouseGoodsShelves warehouseGoodsShelves) { int insert = warehouseGoodsShelvesMapper.insert(warehouseGoodsShelves); if (insert <= 0) { log.error("货架主记录添加失败"); return false; } Long shelvesId = warehouseGoodsShelves.getId(); Long warehouseId = warehouseGoodsShelves.getWarehouseId(); Long createUser = warehouseGoodsShelves.getCreateUser(); Long updateUser = warehouseGoodsShelves.getUpdateUser(); // 3. 批量创建行列记录 List rowcolList = new ArrayList<>(); for (long i = 1; i <= warehouseGoodsShelves.getStorey(); i++) { for (long j = 1; j <= warehouseGoodsShelves.getArrange(); j++) { WarehouseGoodsShelvesRowcol rowcol = new WarehouseGoodsShelvesRowcol(); rowcol.setStorey(i); rowcol.setArrange(j); rowcol.setWarehouseGoodsShelvesId(shelvesId); rowcol.setWarehouseId(warehouseId); rowcol.setCreateUser(createUser); rowcol.setUpdateUser(updateUser); rowcolList.add(rowcol); } } // 4. 批量插入行列记录 if (!rowcolList.isEmpty()) { try { // 使用批量插入方法替代循环单条插入 boolean saveBatch = warehouseGoodsShelvesRowcolService.saveBatch(rowcolList); if (!saveBatch) { log.warn("货架[{}]的行列记录部分添加失败", shelvesId); } } catch (Exception e) { log.error("货架[{}]的行列记录批量添加失败", shelvesId, e); // 抛出异常触发事务回滚 throw e; } } log.info("货架[{}]添加成功,层数:{},排数:{}", shelvesId, warehouseGoodsShelves.getStorey(), warehouseGoodsShelves.getArrange()); return true; } /** * 根据ID更新货架及其行列信息 * @param warehouseGoodsShelves 货架信息 * @return 是否更新成功 */ @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 rowcolPage = warehouseGoodsShelvesRowcolService.listPage(new Page<>(), queryRowcol); List 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 oldRowcolIds = oldRowcolList.stream() .map(WarehouseGoodsShelvesRowcol::getId) .collect(Collectors.toList()); // 删除旧的行列记录 boolean isOldRowcolRemoved = warehouseGoodsShelvesRowcolService.removeByIds(oldRowcolIds); if (!isOldRowcolRemoved) { log.error("货架[{}]的旧行列记录删除失败", shelvesId); return false; } // 添加新的行列记录 add(warehouseGoodsShelves); log.info("货架[{}]的行列信息更新成功", shelvesId); return true; } /** * 批量删除货架及其关联的行列记录 * @param ids 货架ID列表 * @return 是否删除成功 */ @Override public boolean deleteByIds(List ids) { // 1. 先查询所有要删除的货架信息 List 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 queryWrapper = new LambdaQueryWrapper() .eq(WarehouseGoodsShelvesRowcol::getWarehouseGoodsShelvesId, shelvesId) .eq(WarehouseGoodsShelvesRowcol::getWarehouseId, warehouseId); // 查询行列记录(仅需判断是否有商品,无需分页) List rowcolList = warehouseGoodsShelvesRowcolService.list(queryWrapper); // 检查是否有商品 boolean hasGoods = rowcolList.stream() .anyMatch(rowcol -> rowcol.getDocumentationId() != null && rowcol.getDocumentationId()!=0); if (hasGoods) { log.error("货架[ID:{}]上存在商品,禁止删除", shelvesId); return false; // 任一货架有商品则终止删除 } } // 3. 先删除货架主记录 boolean isShelvesDeleted = removeByIds(ids); if (!isShelvesDeleted) { log.error("货架主记录删除失败,ID列表:{}", ids); return false; } // 4. 批量删除所有关联的行列记录 List allShelvesIds = shelvesList.stream() .map(WarehouseGoodsShelves::getId) .collect(Collectors.toList()); // 构建批量删除条件 Wrapper deleteWrapper = new LambdaQueryWrapper() .in(WarehouseGoodsShelvesRowcol::getWarehouseGoodsShelvesId, allShelvesIds); boolean isRowcolDeleted = warehouseGoodsShelvesRowcolService.remove(deleteWrapper); if (!isRowcolDeleted) { log.warn("货架关联的行列记录删除失败,货架ID列表:{}", allShelvesIds); // 此处可根据业务需求决定是否回滚货架删除操作 } log.info("货架批量删除成功,ID列表:{}", ids); return true; } }