Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug enter cache data inconsistency #188

Merged
merged 2 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ public class ReceiptRetailMain implements Serializable {

private String fileId;

private String operatorId;

private Integer status;

private Integer source;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@
package com.wansenai.service.receipt.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wansenai.bo.FileDataBO;
import com.wansenai.bo.PurchaseDataBO;
import com.wansenai.dto.receipt.purchase.*;
import com.wansenai.entities.product.ProductStock;
import com.wansenai.entities.receipt.ReceiptPurchaseMain;
import com.wansenai.entities.receipt.ReceiptPurchaseSub;
import com.wansenai.entities.receipt.ReceiptRetailSub;
import com.wansenai.entities.system.SysFile;
import com.wansenai.mappers.product.ProductStockKeepUnitMapper;
import com.wansenai.mappers.product.ProductStockMapper;
import com.wansenai.mappers.receipt.ReceiptPurchaseMainMapper;
import com.wansenai.mappers.system.SysFileMapper;
import com.wansenai.service.basic.SupplierService;
Expand Down Expand Up @@ -58,15 +62,17 @@ public class ReceiptPurchaseServiceImpl extends ServiceImpl<ReceiptPurchaseMainM
private final SupplierService supplierService;
private final ReceiptPurchaseMainMapper receiptPurchaseMainMapper;
private final ReceiptPurchaseSubService receiptPurchaseSubService;
private final ProductStockMapper productStockMapper;

public ReceiptPurchaseServiceImpl(SysFileMapper fileMapper, ProductStockKeepUnitMapper productStockKeepUnitMapper, CommonService commonService, ISysUserService userService, SupplierService supplierService, ReceiptPurchaseMainMapper receiptPurchaseMainMapper, ReceiptPurchaseSubService receiptPurchaseSubService) {
public ReceiptPurchaseServiceImpl(SysFileMapper fileMapper, ProductStockKeepUnitMapper productStockKeepUnitMapper, CommonService commonService, ISysUserService userService, SupplierService supplierService, ReceiptPurchaseMainMapper receiptPurchaseMainMapper, ReceiptPurchaseSubService receiptPurchaseSubService, ProductStockMapper productStockMapper) {
this.fileMapper = fileMapper;
this.productStockKeepUnitMapper = productStockKeepUnitMapper;
this.commonService = commonService;
this.userService = userService;
this.supplierService = supplierService;
this.receiptPurchaseMainMapper = receiptPurchaseMainMapper;
this.receiptPurchaseSubService = receiptPurchaseSubService;
this.productStockMapper = productStockMapper;
}
private final Map<Long, List<ReceiptPurchaseSub>> receiptSubListCache = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -201,6 +207,38 @@ private Response<String> updatePurchaseStatus(List<Long> ids, Integer status, Pu
}
}

private void updateProductStock(List<ReceiptPurchaseSub> receiptSubList, int stockType) {
var stockMap = new ConcurrentHashMap<Long, Integer>();

receiptSubList.forEach(item -> {
var stock = productStockKeepUnitMapper.getProductSkuByBarCode(item.getProductBarcode(), item.getWarehouseId());
if (stock != null) {
var stockNumber = stock.getStock();
var productNumber = item.getProductNumber();
if (stockType == 1) {
stockNumber += productNumber;
} else if (stockType == 2) {
stockNumber -= productNumber;
}
stockMap.put(stock.getId(), stockNumber);
}
});
receiptSubList.forEach(item2 -> {
stockMap.forEach((key, value) -> {
var stock = ProductStock.builder()
.productSkuId(key)
.warehouseId(item2.getWarehouseId())
.currentStockQuantity(BigDecimal.valueOf(value))
.build();
var wrapper = new LambdaUpdateWrapper<ProductStock>()
.eq(ProductStock::getProductSkuId, stock.getProductSkuId())
.eq(ProductStock::getWarehouseId, stock.getWarehouseId())
.set(ProductStock::getCurrentStockQuantity, BigDecimal.valueOf(value));
productStockMapper.update(stock, wrapper);
});
});
}

@Override
public Response<Page<PurchaseOrderVO>> getPurchaseOrderPage(QueryPurchaseOrderDTO queryPurchaseOrderDTO) {
var result = new Page<PurchaseOrderVO>();
Expand All @@ -221,7 +259,9 @@ public Response<Page<PurchaseOrderVO>> getPurchaseOrderPage(QueryPurchaseOrderDT
var queryResult = receiptPurchaseMainMapper.selectPage(page, queryWrapper);

queryResult.getRecords().forEach(item -> {
var receiptSubList = getReceiptSubList(item.getId());
var receiptSubList = receiptPurchaseSubService.lambdaQuery()
.eq(ReceiptPurchaseSub::getReceiptPurchaseMainId, item.getId())
.list();
var productNumber = calculateProductNumber(receiptSubList);
var supplierName = getSupplierName(item.getSupplierId());
var crateBy = getUserName(item.getCreateBy());
Expand Down Expand Up @@ -435,7 +475,9 @@ public Response<Page<PurchaseStorageVO>> getPurchaseStoragePage(QueryPurchaseSto
var queryResult = receiptPurchaseMainMapper.selectPage(page, queryWrapper);

queryResult.getRecords().forEach(item -> {
var receiptSubList = getReceiptSubList(item.getId());
var receiptSubList = receiptPurchaseSubService.lambdaQuery()
.eq(ReceiptPurchaseSub::getReceiptPurchaseMainId, item.getId())
.list();
var productNumber = calculateProductNumber(receiptSubList);
var supplierName = getSupplierName(item.getSupplierId());
var crateBy = getUserName(item.getCreateBy());
Expand Down Expand Up @@ -527,6 +569,13 @@ public Response<String> addOrUpdatePurchaseStorage(PurchaseStorageDTO purchaseSt
var fileIds = StringUtils.collectionToCommaDelimitedString(fid);

if (isUpdate) {
var beforeReceipt = receiptPurchaseSubService.lambdaQuery()
.eq(ReceiptPurchaseSub::getReceiptPurchaseMainId, purchaseStorageDTO.getId())
.list();
if (!beforeReceipt.isEmpty()) {
updateProductStock(beforeReceipt, 2);
}

var updateMainResult = lambdaUpdate()
.eq(ReceiptPurchaseMain::getId, purchaseStorageDTO.getId())
.set(purchaseStorageDTO.getSupplierId() != null, ReceiptPurchaseMain::getSupplierId, purchaseStorageDTO.getSupplierId())
Expand Down Expand Up @@ -571,6 +620,9 @@ public Response<String> addOrUpdatePurchaseStorage(PurchaseStorageDTO purchaseSt
.collect(Collectors.toList());

var updateSubResult = receiptPurchaseSubService.saveBatch(receiptPurchaseStorageList);
purchaseStorageDTO.getTableData().forEach(item -> {
updateProductStock(receiptPurchaseStorageList, 1);
});

if (updateMainResult && updateSubResult) {
return Response.responseMsg(PurchaseCodeEnum.UPDATE_PURCHASE_RECEIPT_SUCCESS);
Expand Down Expand Up @@ -626,6 +678,7 @@ public Response<String> addOrUpdatePurchaseStorage(PurchaseStorageDTO purchaseSt
.collect(Collectors.toList());

var saveSubResult = receiptPurchaseSubService.saveBatch(receiptList);
updateProductStock(receiptList, 1);

if (saveMainResult && saveSubResult) {
return Response.responseMsg(PurchaseCodeEnum.ADD_PURCHASE_RECEIPT_SUCCESS);
Expand Down Expand Up @@ -665,7 +718,9 @@ public Response<Page<PurchaseRefundVO>> getPurchaseRefundPage(QueryPurchaseRefun
var queryResult = receiptPurchaseMainMapper.selectPage(page, queryWrapper);

queryResult.getRecords().forEach(item -> {
var receiptSubList = getReceiptSubList(item.getId());
var receiptSubList = receiptPurchaseSubService.lambdaQuery()
.eq(ReceiptPurchaseSub::getReceiptPurchaseMainId, item.getId())
.list();
var productNumber = calculateProductNumber(receiptSubList);
var supplierName = getSupplierName(item.getSupplierId());
var crateBy = getUserName(item.getCreateBy());
Expand Down Expand Up @@ -756,6 +811,12 @@ public Response<String> addOrUpdatePurchaseRefund(PurchaseRefundDTO purchaseRefu
var fileIds = StringUtils.collectionToCommaDelimitedString(fid);

if (isUpdate) {
var beforeReceipt = receiptPurchaseSubService.lambdaQuery()
.eq(ReceiptPurchaseSub::getReceiptPurchaseMainId, purchaseRefundDTO.getId())
.list();
if (!beforeReceipt.isEmpty()) {
updateProductStock(beforeReceipt, 1);
}
var updateMainResult = lambdaUpdate()
.eq(ReceiptPurchaseMain::getId, purchaseRefundDTO.getId())
.set(purchaseRefundDTO.getSupplierId() != null, ReceiptPurchaseMain::getSupplierId, purchaseRefundDTO.getSupplierId())
Expand Down Expand Up @@ -800,6 +861,10 @@ public Response<String> addOrUpdatePurchaseRefund(PurchaseRefundDTO purchaseRefu
.collect(Collectors.toList());

var updateSubResult = receiptPurchaseSubService.saveBatch(receiptPurchaseRefundList);
purchaseRefundDTO.getTableData().forEach(item -> {
updateProductStock(receiptPurchaseRefundList, 2);
});


if (updateMainResult && updateSubResult) {
return Response.responseMsg(PurchaseCodeEnum.UPDATE_PURCHASE_REFUND_SUCCESS);
Expand Down Expand Up @@ -855,7 +920,7 @@ public Response<String> addOrUpdatePurchaseRefund(PurchaseRefundDTO purchaseRefu
.collect(Collectors.toList());

var saveSubResult = receiptPurchaseSubService.saveBatch(receiptList);

updateProductStock(receiptList, 2);
if (saveMainResult && saveSubResult) {
return Response.responseMsg(PurchaseCodeEnum.ADD_PURCHASE_REFUND_SUCCESS);
} else {
Expand Down
Loading