package com.ruoyi.basic.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
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.dto.SupplierManageDto;
|
import com.ruoyi.basic.excel.SupplierManageExcelDto;
|
import com.ruoyi.basic.mapper.SupplierManageMapper;
|
import com.ruoyi.basic.pojo.SupplierManage;
|
import com.ruoyi.basic.service.ISupplierService;
|
import com.ruoyi.common.utils.SecurityUtils;
|
import com.ruoyi.common.utils.poi.ExcelUtil;
|
import com.ruoyi.purchase.mapper.PurchaseLedgerMapper;
|
import com.ruoyi.purchase.pojo.PurchaseLedger;
|
import com.ruoyi.purchase.vo.SupplierTransactionsDetailsVo;
|
import com.ruoyi.purchase.vo.SupplierTransactionsVo;
|
import com.ruoyi.quality.mapper.QualityInspectMapper;
|
import com.ruoyi.quality.mapper.QualityUnqualifiedMapper;
|
import com.ruoyi.quality.pojo.QualityInspect;
|
import com.ruoyi.quality.pojo.QualityUnqualified;
|
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
|
import com.ruoyi.sales.pojo.SalesLedgerProduct;
|
import com.ruoyi.stock.mapper.StockInRecordMapper;
|
import com.ruoyi.stock.pojo.StockInRecord;
|
import jakarta.servlet.http.HttpServletResponse;
|
import lombok.RequiredArgsConstructor;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.stereotype.Service;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.math.BigDecimal;
|
import java.time.LocalDate;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.stream.Collectors;
|
|
@Service
|
@RequiredArgsConstructor
|
public class SupplierServiceImpl extends ServiceImpl<SupplierManageMapper, SupplierManage> implements ISupplierService {
|
|
private final SupplierManageMapper supplierMapper;
|
private final PurchaseLedgerMapper purchaseLedgerMapper;
|
private final SalesLedgerProductMapper salesLedgerProductMapper;
|
private final StockInRecordMapper stockInRecordMapper;
|
private final QualityInspectMapper qualityInspectMapper;
|
private final QualityUnqualifiedMapper qualityUnqualifiedMapper;
|
|
/**
|
* 供应商新增
|
*
|
* @param supplierManage
|
* @return
|
*/
|
@Override
|
public void saveSupplier(SupplierManage supplierManage) {
|
LambdaQueryWrapper<SupplierManage> supplierManageLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
supplierManageLambdaQueryWrapper.eq(SupplierManage::getSupplierName, supplierManage.getSupplierName());
|
if (supplierMapper.selectCount(supplierManageLambdaQueryWrapper) > 0) {
|
throw new RuntimeException("供应商已存在");
|
}
|
supplierMapper.insert(supplierManage);
|
}
|
|
/**
|
* 供应商删除
|
*
|
* @param ids
|
* @return
|
*/
|
@Override
|
public int delSupplier(List<Integer> ids) {
|
List<PurchaseLedger> purchaseLedgers = purchaseLedgerMapper.selectList(new QueryWrapper<PurchaseLedger>().lambda().in(PurchaseLedger::getSupplierId, ids));
|
if (purchaseLedgers.size() > 0) {
|
throw new RuntimeException("供应商已存在采购合同");
|
}
|
LambdaQueryWrapper<SupplierManage> delWrapper = new LambdaQueryWrapper<>();
|
delWrapper.in(SupplierManage::getId, ids);
|
|
return supplierMapper.delete(delWrapper);
|
}
|
|
/**
|
* 供应商详情
|
*
|
* @param id
|
* @return
|
*/
|
@Override
|
public SupplierManage supplierDetail(Integer id) {
|
return supplierMapper.selectById(id);
|
}
|
|
/**
|
* 供应商修改
|
*
|
* @param supplierManage
|
* @return
|
*/
|
@Override
|
public int supplierUpdate(SupplierManage supplierManage) {
|
return supplierMapper.updateById(supplierManage);
|
}
|
|
/**
|
* 供应商分页查询
|
*
|
* @param page
|
* @param supplierManageDto
|
* @return
|
*/
|
@Override
|
public IPage<SupplierManage> supplierListPage(Page page, SupplierManageDto supplierManageDto) {
|
return supplierMapper.supplierListPage(page, supplierManageDto);
|
}
|
|
/**
|
* 供应商导出
|
*
|
* @param response
|
* @param supplierManageDto
|
*/
|
@Override
|
public void supplierExport(HttpServletResponse response, SupplierManageDto supplierManageDto) {
|
List<SupplierManageExcelDto> supplierManageList = supplierMapper.supplierExportList(supplierManageDto);
|
ExcelUtil<SupplierManageExcelDto> util = new ExcelUtil<SupplierManageExcelDto>(SupplierManageExcelDto.class);
|
util.exportExcel(response, supplierManageList, "供应商导出");
|
}
|
|
@Override
|
public Boolean importData(MultipartFile file) {
|
|
try {
|
ExcelUtil<SupplierManageExcelDto> util = new ExcelUtil<SupplierManageExcelDto>(SupplierManageExcelDto.class);
|
List<SupplierManageExcelDto> list = util.importExcel(file.getInputStream());
|
ArrayList<SupplierManage> supplierManages = new ArrayList<>();
|
list.forEach(dto -> {
|
SupplierManage supplierManage = new SupplierManage();
|
BeanUtils.copyProperties(dto, supplierManage);
|
supplierManage.setMaintainTime(LocalDate.now());
|
Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
|
|
supplierManage.setMaintainUserId(Integer.parseInt(userId + ""));
|
supplierManages.add(supplierManage);
|
});
|
|
this.saveOrUpdateBatch(supplierManages);
|
return true;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return false;
|
}
|
|
@Override
|
public IPage<SupplierTransactionsVo> supplierTransactions(Page page, String supplierName) {
|
IPage<SupplierTransactionsVo> plPage = supplierMapper.supplierTransactions(page, supplierName);
|
List<SupplierTransactionsVo> resultList = plPage.getRecords();
|
if (resultList.isEmpty()) {
|
return plPage;
|
}
|
|
List<Long> supplierIds = resultList.stream().map(SupplierTransactionsVo::getSupplierId).collect(Collectors.toList());
|
|
List<PurchaseLedger> purchaseLedgers = purchaseLedgerMapper.selectList(new LambdaQueryWrapper<PurchaseLedger>()
|
.in(PurchaseLedger::getSupplierId, supplierIds));
|
|
if (purchaseLedgers.isEmpty()) {
|
for (SupplierTransactionsVo vo : resultList) {
|
vo.setShippedAmount(BigDecimal.ZERO);
|
BigDecimal contractAmount = vo.getContractAmounts() == null ? BigDecimal.ZERO : vo.getContractAmounts();
|
vo.setUnshippedAmount(contractAmount);
|
}
|
return plPage;
|
}
|
|
List<Long> ledgerIds = purchaseLedgers.stream().map(PurchaseLedger::getId).collect(Collectors.toList());
|
|
List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<SalesLedgerProduct>()
|
.in(SalesLedgerProduct::getSalesLedgerId, ledgerIds)
|
.eq(SalesLedgerProduct::getType, 2));
|
|
// 不质检直接入库的 - 7
|
List<StockInRecord> directInboundRecords = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
|
.eq(StockInRecord::getRecordType, "7")
|
.eq(StockInRecord::getApprovalStatus, 1)
|
.in(StockInRecord::getRecordId, ledgerIds));
|
|
// 通过质检入库的
|
List<QualityInspect> qualityInspects = qualityInspectMapper.selectList(new LambdaQueryWrapper<QualityInspect>()
|
.in(QualityInspect::getPurchaseLedgerId, ledgerIds));
|
List<Long> inspectIds = qualityInspects.stream().map(QualityInspect::getId).collect(Collectors.toList());
|
|
// 质检合格入库 - 10
|
List<StockInRecord> qualifiedInboundRecords = new ArrayList<>();
|
List<QualityUnqualified> qualityUnqualifieds = new ArrayList<>();
|
if (!inspectIds.isEmpty()) {
|
qualifiedInboundRecords = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
|
.eq(StockInRecord::getRecordType, "10")
|
.eq(StockInRecord::getApprovalStatus, 1)
|
.in(StockInRecord::getRecordId, inspectIds));
|
|
qualityUnqualifieds = qualityUnqualifiedMapper.selectList(new LambdaQueryWrapper<QualityUnqualified>()
|
.in(QualityUnqualified::getInspectId, inspectIds));
|
}
|
List<Long> unqualifiedIds = qualityUnqualifieds.stream().map(QualityUnqualified::getId).collect(Collectors.toList());
|
|
// 质检不合格入库 - 11
|
List<StockInRecord> unqualifiedInboundRecords = new ArrayList<>();
|
if (!unqualifiedIds.isEmpty()) {
|
unqualifiedInboundRecords = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
|
.eq(StockInRecord::getRecordType, "11")
|
.eq(StockInRecord::getApprovalStatus, 1)
|
.in(StockInRecord::getRecordId, unqualifiedIds));
|
}
|
|
for (SupplierTransactionsVo vo : resultList) {
|
BigDecimal shippedAmount = BigDecimal.ZERO;
|
|
List<PurchaseLedger> currentLedgers = purchaseLedgers.stream().filter(pl -> pl.getSupplierId().equals(vo.getSupplierId())).toList();
|
for (PurchaseLedger ledger : currentLedgers) {
|
Long ledgerId = ledger.getId();
|
|
List<StockInRecord> currentDirectRecords = directInboundRecords.stream().filter(s -> s.getRecordId().equals(ledgerId)).toList();
|
for (StockInRecord sir : currentDirectRecords) {
|
List<SalesLedgerProduct> currentProducts = salesLedgerProducts.stream().filter(s -> s.getSalesLedgerId().equals(ledgerId)).toList();
|
for (SalesLedgerProduct slp : currentProducts) {
|
if (sir.getStockInNum() != null && slp.getTaxInclusiveUnitPrice() != null) {
|
shippedAmount = shippedAmount.add(sir.getStockInNum().multiply(slp.getTaxInclusiveUnitPrice()));
|
}
|
}
|
}
|
|
List<QualityInspect> currentInspects = qualityInspects.stream().filter(q -> q.getPurchaseLedgerId().equals(ledgerId)).toList();
|
for (QualityInspect inspect : currentInspects) {
|
List<StockInRecord> currentQualifiedRecords = qualifiedInboundRecords.stream().filter(s -> s.getRecordId().equals(inspect.getId())).toList();
|
for (StockInRecord sir : currentQualifiedRecords) {
|
BigDecimal minPrice = getMinTaxInclusiveUnitPrice(salesLedgerProducts, ledgerId, sir.getProductModelId());
|
if (sir.getStockInNum() != null && minPrice != null) {
|
shippedAmount = shippedAmount.add(sir.getStockInNum().multiply(minPrice));
|
}
|
}
|
|
List<QualityUnqualified> currentUnqualifieds = qualityUnqualifieds.stream().filter(q -> q.getInspectId().equals(inspect.getId())).toList();
|
for (QualityUnqualified unqualified : currentUnqualifieds) {
|
List<StockInRecord> currentUnqualifiedRecords = unqualifiedInboundRecords.stream().filter(s -> s.getRecordId().equals(unqualified.getId())).toList();
|
for (StockInRecord sir : currentUnqualifiedRecords) {
|
BigDecimal minPrice = getMinTaxInclusiveUnitPrice(salesLedgerProducts, ledgerId, sir.getProductModelId());
|
if (sir.getStockInNum() != null && minPrice != null) {
|
shippedAmount = shippedAmount.add(sir.getStockInNum().multiply(minPrice));
|
}
|
}
|
}
|
}
|
}
|
|
BigDecimal contractAmount = vo.getContractAmounts() == null ? BigDecimal.ZERO : vo.getContractAmounts();
|
if (shippedAmount.subtract(contractAmount).compareTo(BigDecimal.ZERO) > 0) {
|
shippedAmount = contractAmount;
|
}
|
vo.setShippedAmount(shippedAmount);
|
vo.setUnshippedAmount(contractAmount.subtract(shippedAmount));
|
}
|
|
return plPage;
|
}
|
|
@Override
|
public IPage<SupplierTransactionsDetailsVo> supplierTransactionsDetails(Page page, Long supplierId) {
|
IPage<SupplierTransactionsDetailsVo> plPage = supplierMapper.supplierTransactionsDetails(page, supplierId);
|
|
List<SupplierTransactionsDetailsVo> resultList = plPage.getRecords();
|
if (resultList.isEmpty()) {
|
return plPage;
|
}
|
|
List<Long> ledgerIds = resultList.stream().map(SupplierTransactionsDetailsVo::getPurchaseLedgerId).collect(Collectors.toList());
|
|
List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<SalesLedgerProduct>()
|
.in(SalesLedgerProduct::getSalesLedgerId, ledgerIds)
|
.eq(SalesLedgerProduct::getType, 2));
|
|
// 不质检直接入库的 - 7
|
List<StockInRecord> directInboundRecords = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
|
.eq(StockInRecord::getRecordType, "7")
|
.eq(StockInRecord::getApprovalStatus, 1)
|
.in(StockInRecord::getRecordId, ledgerIds));
|
|
// 通过质检入库的
|
List<QualityInspect> qualityInspects = qualityInspectMapper.selectList(new LambdaQueryWrapper<QualityInspect>()
|
.in(QualityInspect::getPurchaseLedgerId, ledgerIds));
|
List<Long> inspectIds = qualityInspects.stream().map(QualityInspect::getId).collect(Collectors.toList());
|
|
// 质检合格入库 - 10
|
List<StockInRecord> qualifiedInboundRecords = new ArrayList<>();
|
List<QualityUnqualified> qualityUnqualifieds = new ArrayList<>();
|
if (!inspectIds.isEmpty()) {
|
qualifiedInboundRecords = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
|
.eq(StockInRecord::getRecordType, "10")
|
.eq(StockInRecord::getApprovalStatus, 1)
|
.in(StockInRecord::getRecordId, inspectIds));
|
|
qualityUnqualifieds = qualityUnqualifiedMapper.selectList(new LambdaQueryWrapper<QualityUnqualified>()
|
.in(QualityUnqualified::getInspectId, inspectIds));
|
}
|
List<Long> unqualifiedIds = qualityUnqualifieds.stream().map(QualityUnqualified::getId).collect(Collectors.toList());
|
|
// 质检不合格入库 - 11
|
List<StockInRecord> unqualifiedInboundRecords = new ArrayList<>();
|
if (!unqualifiedIds.isEmpty()) {
|
unqualifiedInboundRecords = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
|
.eq(StockInRecord::getRecordType, "11")
|
.eq(StockInRecord::getApprovalStatus, 1)
|
.in(StockInRecord::getRecordId, unqualifiedIds));
|
}
|
|
for (SupplierTransactionsDetailsVo vo : resultList) {
|
BigDecimal shippedAmount = BigDecimal.ZERO;
|
|
List<StockInRecord> currentDirectRecords = directInboundRecords.stream().filter(s -> s.getRecordId().equals(vo.getPurchaseLedgerId())).toList();
|
for (StockInRecord sir : currentDirectRecords) {
|
List<SalesLedgerProduct> currentProducts = salesLedgerProducts.stream().filter(s -> s.getSalesLedgerId().equals(vo.getPurchaseLedgerId())).toList();
|
for (SalesLedgerProduct slp : currentProducts) {
|
if (sir.getStockInNum() != null && slp.getTaxInclusiveUnitPrice() != null) {
|
shippedAmount = shippedAmount.add(sir.getStockInNum().multiply(slp.getTaxInclusiveUnitPrice()));
|
}
|
}
|
}
|
|
List<QualityInspect> currentInspects = qualityInspects.stream().filter(q -> q.getPurchaseLedgerId().equals(vo.getPurchaseLedgerId())).toList();
|
for (QualityInspect inspect : currentInspects) {
|
List<StockInRecord> currentQualifiedRecords = qualifiedInboundRecords.stream().filter(s -> s.getRecordId().equals(inspect.getId())).toList();
|
for (StockInRecord sir : currentQualifiedRecords) {
|
BigDecimal minPrice = getMinTaxInclusiveUnitPrice(salesLedgerProducts, vo.getPurchaseLedgerId(), sir.getProductModelId());
|
if (sir.getStockInNum() != null && minPrice != null) {
|
shippedAmount = shippedAmount.add(sir.getStockInNum().multiply(minPrice));
|
}
|
}
|
|
List<QualityUnqualified> currentUnqualifieds = qualityUnqualifieds.stream().filter(q -> q.getInspectId().equals(inspect.getId())).toList();
|
for (QualityUnqualified unqualified : currentUnqualifieds) {
|
List<StockInRecord> currentUnqualifiedRecords = unqualifiedInboundRecords.stream().filter(s -> s.getRecordId().equals(unqualified.getId())).toList();
|
for (StockInRecord sir : currentUnqualifiedRecords) {
|
BigDecimal minPrice = getMinTaxInclusiveUnitPrice(salesLedgerProducts, vo.getPurchaseLedgerId(), sir.getProductModelId());
|
if (sir.getStockInNum() != null && minPrice != null) {
|
shippedAmount = shippedAmount.add(sir.getStockInNum().multiply(minPrice));
|
}
|
}
|
}
|
}
|
|
BigDecimal contractAmount = vo.getContractAmount() == null ? BigDecimal.ZERO : vo.getContractAmount();
|
if (shippedAmount.subtract(contractAmount).compareTo(BigDecimal.ZERO) > 0) {
|
shippedAmount = contractAmount;
|
}
|
vo.setShippedAmount(shippedAmount);
|
vo.setUnshippedAmount(contractAmount.subtract(shippedAmount));
|
}
|
|
return plPage;
|
}
|
|
private BigDecimal getMinTaxInclusiveUnitPrice(List<SalesLedgerProduct> allSlp, Long salesLedgerId, Long productModelId) {
|
return allSlp.stream()
|
.filter(s -> s.getSalesLedgerId().equals(salesLedgerId) && s.getProductModelId() != null && s.getProductModelId().equals(productModelId))
|
.map(SalesLedgerProduct::getTaxInclusiveUnitPrice)
|
.filter(java.util.Objects::nonNull)
|
.min(BigDecimal::compareTo)
|
.orElse(null);
|
}
|
}
|