package com.ruoyi.procurementrecord.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.common.utils.SecurityUtils;
|
import com.ruoyi.common.utils.poi.ExcelUtil;
|
import com.ruoyi.framework.security.LoginUser;
|
import com.ruoyi.procurementrecord.dto.*;
|
import com.ruoyi.procurementrecord.mapper.ProcurementRecordMapper;
|
import com.ruoyi.procurementrecord.pojo.ProcurementRecord;
|
import com.ruoyi.procurementrecord.service.ProcurementRecordService;
|
import com.ruoyi.quality.pojo.QualityInspect;
|
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
|
import com.ruoyi.sales.pojo.SalesLedgerProduct;
|
import lombok.RequiredArgsConstructor;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Service;
|
import org.springframework.util.CollectionUtils;
|
|
import javax.servlet.http.HttpServletResponse;
|
import java.math.BigDecimal;
|
import java.time.LocalDateTime;
|
import java.util.List;
|
import java.util.stream.Collectors;
|
|
/**
|
* @author :yys
|
* @date : 2025/7/7 14:38
|
*/
|
@Service
|
@RequiredArgsConstructor
|
@Slf4j
|
public class ProcurementRecordServiceImpl extends ServiceImpl<ProcurementRecordMapper, ProcurementRecord> implements ProcurementRecordService {
|
|
private final ProcurementRecordMapper procurementRecordMapper;
|
|
private final SalesLedgerProductMapper salesLedgerProductMapper;
|
|
@Override
|
public List<ProcurementDto> listProcurementBySalesLedgerId(ProcurementDto procurementDto) {
|
List<ProcurementDto> procurementDtos = procurementRecordMapper.listProcurementBySalesLedgerId(procurementDto);
|
// 计算待入库数量
|
// 查询采购记录已入库数量
|
List<Integer> collect = procurementDtos.stream().map(ProcurementDto::getId).collect(Collectors.toList());
|
if(CollectionUtils.isEmpty( collect)){
|
return procurementDtos;
|
}
|
LambdaQueryWrapper<ProcurementRecord> procurementRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
procurementRecordLambdaQueryWrapper.in(ProcurementRecord::getSalesLedgerProductId, collect);
|
List<ProcurementRecord> procurementRecords = procurementRecordMapper.selectList(procurementRecordLambdaQueryWrapper);
|
if(CollectionUtils.isEmpty( procurementRecords)){
|
return procurementDtos;
|
}
|
for (ProcurementDto dto : procurementDtos) {
|
// 根据采购台账ID筛选对应的入库记录
|
List<ProcurementRecord> collect1 = procurementRecords.stream()
|
.filter(procurementRecord -> procurementRecord.getSalesLedgerProductId().equals(dto.getId()))
|
.collect(Collectors.toList());
|
|
// 如果没有相关的入库记录,跳过该条数据
|
if(CollectionUtils.isEmpty(collect1)){
|
dto.setQuantity0(dto.getQuantity());
|
continue;
|
}
|
|
// 计算已入库数量总和,并设置待入库数量
|
BigDecimal totalInboundNum = collect1.stream()
|
.map(ProcurementRecord::getInboundNum)
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
// 待入库数量 = 总数量 - 已入库数量
|
dto.setQuantity0(dto.getQuantity().subtract(totalInboundNum));
|
}
|
return procurementDtos;
|
}
|
|
public ProcurementRecord getProcurementRecordById(Integer id){
|
ProcurementRecord procurementRecord = procurementRecordMapper.selectById(id);
|
if(procurementRecord == null) {
|
throw new RuntimeException("未找到该采购入库记录");
|
}
|
return procurementRecord;
|
}
|
|
public List<ProcurementRecord> getProcurementRecordByIds(List<Integer> id){
|
List<ProcurementRecord> procurementRecord = procurementRecordMapper.selectBatchIds(id);
|
if(procurementRecord == null) {
|
throw new RuntimeException("未找到该采购入库记录");
|
}
|
return procurementRecord;
|
}
|
|
@Override
|
public int updatePro(ProcurementUpdateDto procurementDto) {
|
ProcurementRecord procurementRecordById = getProcurementRecordById(procurementDto.getId());
|
procurementRecordById.setInboundNum(procurementDto.getQuantityStock());
|
return procurementRecordMapper.updateById(procurementRecordById);
|
}
|
|
@Override
|
public int deletePro(ProcurementUpdateDto procurementDto) {
|
List<ProcurementRecord> procurementRecordById = getProcurementRecordByIds(procurementDto.getIds());
|
procurementRecordMapper.deleteBatchIds(procurementRecordById.stream().map(ProcurementRecord::getId).collect(Collectors.toList()));
|
return 0;
|
}
|
|
@Override
|
public void export(HttpServletResponse response) {
|
List<ProcurementPageDto> list =procurementRecordMapper.list();
|
ExcelUtil<ProcurementPageDto> util = new ExcelUtil<ProcurementPageDto>(ProcurementPageDto.class);
|
util.exportExcel(response, list, "入库台账");
|
}
|
|
@Override
|
public int add(ProcurementAddDto procurementDto) {
|
LoginUser loginUser = SecurityUtils.getLoginUser();
|
// 批量新增
|
for (Details detail : procurementDto.getDetails()) {
|
// 查询采购入库数量
|
LambdaQueryWrapper<ProcurementRecord> procurementRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
procurementRecordLambdaQueryWrapper.eq(ProcurementRecord::getSalesLedgerProductId, detail.getId());
|
Long aLong = procurementRecordMapper.selectCount(procurementRecordLambdaQueryWrapper);
|
|
ProcurementRecord.ProcurementRecordBuilder procurementRecordBuilder = ProcurementRecord.builder()
|
.salesLedgerProductId(detail.getId())
|
.inboundBatches(aLong.equals(0L) ? "第1批次" : "第"+ (aLong + 1) + "批次")
|
.inboundNum(detail.getInboundQuantity())
|
.createDate(LocalDateTime.now())
|
.tenantId(loginUser.getTenantId())
|
.createBy(procurementDto.getNickName());
|
this.save(procurementRecordBuilder.build());
|
// 入库成功减掉采购数量
|
// LambdaQueryWrapper<SalesLedgerProduct> salesLedgerProductLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
// salesLedgerProductLambdaQueryWrapper.eq(SalesLedgerProduct::getId, detail.getId());
|
// SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectOne(salesLedgerProductLambdaQueryWrapper);
|
// if(salesLedgerProduct == null){
|
// throw new RuntimeException("未找到该商品");
|
// }
|
// salesLedgerProduct.setQuantity(salesLedgerProduct.getQuantity().subtract(detail.getInboundQuantity()));
|
// salesLedgerProductMapper.updateById(salesLedgerProduct);
|
}
|
return 1;
|
}
|
|
@Override
|
public IPage<ProcurementPageDto> listPage(Page page, ProcurementPageDto procurementDto) {
|
return procurementRecordMapper.listPage(page,procurementDto);
|
}
|
|
}
|