package com.ruoyi.approve.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.ruoyi.common.enums.StockInQualifiedRecordTypeEnum;
|
import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum;
|
import com.ruoyi.procurementrecord.utils.StockUtils;
|
import com.ruoyi.purchase.mapper.PurchaseLedgerMapper;
|
import com.ruoyi.purchase.pojo.PurchaseLedger;
|
import com.ruoyi.quality.mapper.QualityInspectMapper;
|
import com.ruoyi.quality.mapper.QualityInspectParamMapper;
|
import com.ruoyi.quality.mapper.QualityTestStandardMapper;
|
import com.ruoyi.quality.mapper.QualityTestStandardParamMapper;
|
import com.ruoyi.quality.pojo.QualityInspect;
|
import com.ruoyi.quality.pojo.QualityInspectParam;
|
import com.ruoyi.quality.pojo.QualityTestStandard;
|
import com.ruoyi.quality.pojo.QualityTestStandardParam;
|
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
|
import com.ruoyi.sales.mapper.SalesQuotationMapper;
|
import com.ruoyi.sales.mapper.ShippingInfoMapper;
|
import com.ruoyi.sales.pojo.SalesLedgerProduct;
|
import com.ruoyi.sales.pojo.SalesQuotation;
|
import com.ruoyi.sales.pojo.ShippingInfo;
|
import lombok.RequiredArgsConstructor;
|
import org.springframework.stereotype.Service;
|
import org.springframework.util.StringUtils;
|
|
import java.util.Date;
|
import java.util.List;
|
|
@Service
|
@RequiredArgsConstructor
|
public class ApproveBusinessStatusService {
|
|
private final PurchaseLedgerMapper purchaseLedgerMapper;
|
private final SalesQuotationMapper salesQuotationMapper;
|
private final ShippingInfoMapper shippingInfoMapper;
|
private final SalesLedgerProductMapper salesLedgerProductMapper;
|
private final StockUtils stockUtils;
|
private final QualityInspectMapper qualityInspectMapper;
|
private final QualityTestStandardMapper qualityTestStandardMapper;
|
private final QualityTestStandardParamMapper qualityTestStandardParamMapper;
|
private final QualityInspectParamMapper qualityInspectParamMapper;
|
|
/**
|
* 统一同步审批结果对应的业务单据状态。
|
* status:1-审核中,2-审核完成,3-审核未通过。
|
*/
|
public void syncBusinessStatus(Integer approveType, String approveReason, Integer status) {
|
if (approveType == null || status == null || !StringUtils.hasText(approveReason)) {
|
return;
|
}
|
switch (approveType) {
|
case 5:
|
syncPurchaseStatus(approveReason, status);
|
break;
|
case 6:
|
syncSalesQuotationStatus(approveReason, status);
|
break;
|
case 7:
|
syncShippingStatus(approveReason, status);
|
break;
|
default:
|
break;
|
}
|
}
|
|
// 采购审批通过时,按产品质检配置决定生成质检单或直接入库。
|
private void syncPurchaseStatus(String approveReason, Integer status) {
|
PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectOne(new LambdaQueryWrapper<PurchaseLedger>()
|
.eq(PurchaseLedger::getPurchaseContractNumber, approveReason)
|
.last("limit 1"));
|
if (purchaseLedger == null) {
|
return;
|
}
|
if (status.equals(2)) {
|
purchaseLedger.setApprovalStatus(3);
|
List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(new QueryWrapper<SalesLedgerProduct>()
|
.lambda().eq(SalesLedgerProduct::getSalesLedgerId, purchaseLedger.getId()).eq(SalesLedgerProduct::getType, 2));
|
for (SalesLedgerProduct salesLedgerProduct : salesLedgerProducts) {
|
if (Boolean.TRUE.equals(salesLedgerProduct.getIsChecked())) {
|
addQualityInspect(purchaseLedger, salesLedgerProduct);
|
} else {
|
stockUtils.addStockWithBatchNo(
|
salesLedgerProduct.getProductModelId(),
|
salesLedgerProduct.getQuantity(),
|
StockInQualifiedRecordTypeEnum.PURCHASE_STOCK_IN.getCode(),
|
purchaseLedger.getId(),
|
purchaseLedger.getPurchaseContractNumber() + "-" + salesLedgerProduct.getId());
|
}
|
}
|
} else if (status.equals(3)) {
|
purchaseLedger.setApprovalStatus(4);
|
} else if (status.equals(1)) {
|
purchaseLedger.setApprovalStatus(2);
|
} else {
|
return;
|
}
|
purchaseLedgerMapper.updateById(purchaseLedger);
|
}
|
|
// 报价审批状态回写到销售报价单状态。
|
private void syncSalesQuotationStatus(String approveReason, Integer status) {
|
SalesQuotation salesQuote = salesQuotationMapper.selectOne(new LambdaQueryWrapper<SalesQuotation>()
|
.eq(SalesQuotation::getQuotationNo, approveReason)
|
.last("limit 1"));
|
if (salesQuote == null) {
|
return;
|
}
|
if (status.equals(2)) {
|
salesQuote.setStatus("通过");
|
} else if (status.equals(3)) {
|
salesQuote.setStatus("拒绝");
|
} else if (status.equals(1)) {
|
salesQuote.setStatus("审核中");
|
} else {
|
return;
|
}
|
salesQuotationMapper.updateById(salesQuote);
|
}
|
|
// 发货审批通过时同步发货状态和出库审批状态;拒绝时删除待确认出库记录。
|
private void syncShippingStatus(String approveReason, Integer status) {
|
ShippingInfo shippingInfo = shippingInfoMapper.selectOne(new LambdaQueryWrapper<ShippingInfo>()
|
.eq(ShippingInfo::getShippingNo, approveReason)
|
.orderByDesc(ShippingInfo::getCreateTime)
|
.last("limit 1"));
|
if (shippingInfo == null) {
|
return;
|
}
|
if (status.equals(2)) {
|
shippingInfo.setStatus("审核通过");
|
shippingInfo.setShippingDate(new Date());
|
stockUtils.shipmentStatus(StockOutQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode(), shippingInfo.getId());
|
} else if (status.equals(3)) {
|
stockUtils.deleteStockOutRecord(shippingInfo.getId(), StockOutQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode());
|
shippingInfo.setStatus("审核拒绝");
|
} else if (status.equals(1)) {
|
shippingInfo.setStatus("审核中");
|
} else {
|
return;
|
}
|
shippingInfoMapper.updateById(shippingInfo);
|
}
|
|
// 生成采购质检单,并按产品质检标准初始化质检参数。
|
private void addQualityInspect(PurchaseLedger purchaseLedger, SalesLedgerProduct saleProduct) {
|
QualityInspect qualityInspect = new QualityInspect();
|
qualityInspect.setInspectType(0);
|
qualityInspect.setSupplier(purchaseLedger.getSupplierName());
|
qualityInspect.setPurchaseLedgerId(purchaseLedger.getId());
|
qualityInspect.setProductId(saleProduct.getProductId());
|
qualityInspect.setProductName(saleProduct.getProductCategory());
|
qualityInspect.setModel(saleProduct.getSpecificationModel());
|
qualityInspect.setProductModelId(saleProduct.getProductModelId());
|
qualityInspect.setUnit(saleProduct.getUnit());
|
qualityInspect.setQuantity(saleProduct.getQuantity());
|
qualityInspectMapper.insert(qualityInspect);
|
List<QualityTestStandard> qualityTestStandard = qualityTestStandardMapper.getQualityTestStandardByProductId(saleProduct.getProductId(), 0, null);
|
if (qualityTestStandard.size() > 0) {
|
qualityInspect.setTestStandardId(qualityTestStandard.get(0).getId());
|
qualityInspectMapper.updateById(qualityInspect);
|
qualityTestStandardParamMapper.selectList(Wrappers.<QualityTestStandardParam>lambdaQuery()
|
.eq(QualityTestStandardParam::getTestStandardId, qualityTestStandard.get(0).getId()))
|
.forEach(qualityTestStandardParam -> {
|
QualityInspectParam param = new QualityInspectParam();
|
com.ruoyi.common.utils.bean.BeanUtils.copyProperties(qualityTestStandardParam, param);
|
param.setId(null);
|
param.setInspectId(qualityInspect.getId());
|
qualityInspectParamMapper.insert(param);
|
});
|
}
|
}
|
}
|