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.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;
|
import com.ruoyi.warehouse.pojo.WarehouseGoodsShelves;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
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
|
@Transactional(rollbackFor = Exception.class)
|
public class WarehouseGoodsShelvesServiceImpl extends ServiceImpl<WarehouseGoodsShelvesMapper, WarehouseGoodsShelves>
|
implements WarehouseGoodsShelvesService {
|
@Autowired
|
private WarehouseGoodsShelvesRowcolService warehouseGoodsShelvesRowcolService;
|
@Autowired
|
private WarehouseGoodsShelvesRowcolMapper warehouseGoodsShelvesRowcolMapper;
|
@Autowired
|
private WarehouseGoodsShelvesMapper warehouseGoodsShelvesMapper;
|
@Autowired
|
private DocumentationService documentationService;
|
@Autowired
|
private DocumentationMapper documentationMapper;
|
|
@Override
|
public boolean add(WarehouseGoodsShelves warehouseGoodsShelves) {
|
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. 批量创建行列记录
|
List<WarehouseGoodsShelvesRowcol> 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);
|
rowcolList.add(rowcol);
|
}
|
}
|
// 4. 批量插入行列记录
|
if (!rowcolList.isEmpty()) {
|
try {
|
// 使用批量插入方法替代循环单条插入
|
warehouseGoodsShelvesRowcolService.saveBatch(rowcolList);
|
} catch (Exception e) {
|
// 抛出异常触发事务回滚
|
throw e;
|
}
|
}
|
return true;
|
}
|
/**
|
* 根据ID更新货架及其行列信息
|
* @param warehouseGoodsShelves 货架信息
|
* @return 是否更新成功
|
*/
|
@Override
|
public boolean updateRowcolById(WarehouseGoodsShelves warehouseGoodsShelves) {
|
Long shelvesId = warehouseGoodsShelves.getId();
|
|
// 构建查询条件:查询该货架下的所有行列记录
|
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);
|
// 删除旧的行列记录
|
warehouseGoodsShelvesRowcolService.removeByIds(rowcolIds);
|
// 添加新的行列记录
|
add(warehouseGoodsShelves);
|
return true;
|
}
|
/**
|
* 批量删除货架及其关联的行列记录
|
* @param ids 货架ID列表
|
* @return 是否删除成功
|
*/
|
@Override
|
public boolean deleteByIds(List<Long> ids) {
|
// 1. 先查询所有要删除的货架信息
|
List<WarehouseGoodsShelves> shelvesList = warehouseGoodsShelvesMapper.selectBatchIds(ids);
|
|
// 2. 检查所有货架是否存在商品(有商品则不允许删除)
|
for (WarehouseGoodsShelves shelves : shelvesList) {
|
Long shelvesId = shelves.getId();
|
// 构建查询条件:查询当前货架的所有行列记录
|
Wrapper<WarehouseGoodsShelvesRowcol> queryWrapper = new LambdaQueryWrapper<WarehouseGoodsShelvesRowcol>()
|
.eq(WarehouseGoodsShelvesRowcol::getWarehouseGoodsShelvesId, shelvesId);
|
|
List<WarehouseGoodsShelvesRowcol> rowcolList = warehouseGoodsShelvesRowcolService.list(queryWrapper);
|
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. 先删除货架主记录
|
warehouseGoodsShelvesMapper.deleteBatchIds(ids);
|
|
// 4. 批量删除所有行列记录
|
List<Long> allShelvesIds = shelvesList.stream()
|
.map(WarehouseGoodsShelves::getId)
|
.collect(Collectors.toList());
|
|
// 构建批量删除条件
|
Wrapper<WarehouseGoodsShelvesRowcol> deleteWrapper = new LambdaQueryWrapper<WarehouseGoodsShelvesRowcol>()
|
.in(WarehouseGoodsShelvesRowcol::getWarehouseGoodsShelvesId, allShelvesIds);
|
warehouseGoodsShelvesRowcolService.remove(deleteWrapper);
|
return true;
|
}
|
|
|
@Override
|
public List<WarehouseGoodsShelvesDto> findList(WarehouseGoodsShelves warehouseGoodsShelves) {
|
return warehouseGoodsShelvesMapper.listAll(warehouseGoodsShelves);
|
}
|
|
}
|