package com.ruoyi.business.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ruoyi.basic.entity.CoalInfo;
|
import com.ruoyi.basic.entity.Supply;
|
import com.ruoyi.basic.mapper.CoalInfoMapper;
|
import com.ruoyi.basic.mapper.SupplyMapper;
|
import com.ruoyi.business.dto.PurchaseRegistrationDto;
|
import com.ruoyi.business.entity.PendingInventory;
|
import com.ruoyi.business.entity.PurchaseRegistration;
|
import com.ruoyi.business.mapper.PendingInventoryMapper;
|
import com.ruoyi.business.mapper.PurchaseRegistrationMapper;
|
import com.ruoyi.business.service.PurchaseRegistrationService;
|
import com.ruoyi.common.exception.base.BaseException;
|
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.common.utils.bean.BeanUtils;
|
import lombok.RequiredArgsConstructor;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.util.Assert;
|
|
import java.time.LocalDate;
|
import java.util.Arrays;
|
import java.util.List;
|
import java.util.Objects;
|
import java.util.stream.Collectors;
|
|
/**
|
* <p>
|
* 采购登记表 服务实现类
|
* </p>
|
*
|
* @author ruoyi
|
* @since 2025-06-03
|
*/
|
@Service
|
@RequiredArgsConstructor
|
public class PurchaseRegistrationServiceImpl extends ServiceImpl<PurchaseRegistrationMapper, PurchaseRegistration> implements PurchaseRegistrationService {
|
|
|
private final PurchaseRegistrationMapper purchaseRegistrationMapper;
|
|
private final PendingInventoryMapper pendingInventoryMapper;
|
|
private final CoalInfoMapper coalInfoMapper;
|
|
private final SupplyMapper supplyMapper;
|
|
@Override
|
public IPage<PurchaseRegistration> selectPurchaseRegistrationList(Page<PurchaseRegistration> page, PurchaseRegistrationDto dto) {
|
LambdaQueryWrapper<PurchaseRegistration> queryWrapper = new LambdaQueryWrapper<>();
|
|
String keyword = dto.getSearchAll();
|
if (StringUtils.isNotBlank(keyword)) {
|
// 查询煤种名称中模糊匹配的coalId列表
|
List<Long> matchedCoalIds = coalInfoMapper.selectList(
|
new LambdaQueryWrapper<CoalInfo>().like(CoalInfo::getCoal, keyword)
|
).stream()
|
.map(CoalInfo::getId)
|
.collect(Collectors.toList());
|
|
// 组装查询条件:煤种ID在匹配的列表中 或 供应商名称匹配
|
queryWrapper.and(w -> {
|
if (!matchedCoalIds.isEmpty()) {
|
w.in(PurchaseRegistration::getCoalId, matchedCoalIds).or();
|
}
|
w.like(PurchaseRegistration::getSupplierName, keyword);
|
});
|
}
|
|
queryWrapper.orderByDesc(PurchaseRegistration::getCreateTime);
|
|
return purchaseRegistrationMapper.selectPage(page, queryWrapper);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public int addOrEditPR(PurchaseRegistrationDto purchaseRegistrationDto) {
|
// 参数校验
|
Assert.notNull(purchaseRegistrationDto, "采购登记信息不能为空");
|
|
// 创建采购登记实体并复制属性
|
PurchaseRegistration purchaseRegistration = new PurchaseRegistration();
|
BeanUtils.copyProperties(purchaseRegistrationDto, purchaseRegistration);
|
CoalInfo coalInfo = coalInfoMapper.selectById(purchaseRegistrationDto.getCoalId());
|
if (coalInfo == null) {
|
throw new BaseException("煤种信息不存在");
|
}
|
Supply supply = supplyMapper.selectById(purchaseRegistrationDto.getSupplierId());
|
if (supply == null) {
|
throw new BaseException("供应商信息不存在");
|
}
|
purchaseRegistration.setSupplierName(supply.getSupplierName());
|
if (Objects.isNull(purchaseRegistrationDto.getId())) {
|
// 新增采购登记
|
purchaseRegistration.setRegistrationDate(LocalDate.now());
|
int insertCount = purchaseRegistrationMapper.insert(purchaseRegistration);
|
if (insertCount > 0) {
|
// 采购登记成功,同步创建待入库记录
|
PendingInventory pendingInventory = createPendingInventory(purchaseRegistration);
|
pendingInventory.setSupplierName(supply.getSupplierName());
|
return pendingInventoryMapper.insert(pendingInventory);
|
}
|
return insertCount;
|
} else {
|
// 更新采购登记
|
return purchaseRegistrationMapper.updateById(purchaseRegistration);
|
}
|
}
|
|
/**
|
* 根据采购登记信息创建待入库记录
|
*
|
* @param purchaseRegistration 采购登记实体
|
* @return 待入库实体
|
*/
|
private PendingInventory createPendingInventory(PurchaseRegistration purchaseRegistration) {
|
PendingInventory pendingInventory = new PendingInventory();
|
// 复制基本属性
|
BeanUtils.copyProperties(purchaseRegistration, pendingInventory);
|
|
// 设置待入库记录特有的属性(如果有)
|
pendingInventory.setId(null);
|
pendingInventory.setPurchaseId(purchaseRegistration.getId());
|
pendingInventory.setCoalId(purchaseRegistration.getCoalId());
|
pendingInventory.setInventoryQuantity(purchaseRegistration.getPurchaseQuantity());
|
return pendingInventory;
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public int delByIds(Long[] ids) {
|
if (ids == null || ids.length == 0) {
|
return 0;
|
}
|
|
// 1. 删除关联的PendingInventory记录
|
LambdaQueryWrapper<PendingInventory> wrapper = new LambdaQueryWrapper<>();
|
wrapper.in(PendingInventory::getPurchaseId, Arrays.asList(ids));
|
pendingInventoryMapper.delete(wrapper); // 改为delete操作
|
|
// 2. 批量删除采购注册记录
|
return purchaseRegistrationMapper.deleteByIds(Arrays.asList(ids));
|
}
|
|
}
|