调整异步更新库存
This commit is contained in:
parent
0cd334cac0
commit
068b90ed89
@ -222,7 +222,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
|
|||||||
item.setRecordNum(item.getRecordNum() - recordNumIsEnough.get(item.getId()));
|
item.setRecordNum(item.getRecordNum() - recordNumIsEnough.get(item.getId()));
|
||||||
});
|
});
|
||||||
productRecordService.updateBatch(productRecords);
|
productRecordService.updateBatch(productRecords);
|
||||||
//TODO:通知到云?
|
//TODO: 通知到云?
|
||||||
//修改订单状态
|
//修改订单状态
|
||||||
return this.updateChain().set(ORDER.ORDER_STATUS, 3).where(ORDER.ID.eq(orderId)).update();
|
return this.updateChain().set(ORDER.ORDER_STATUS, 3).where(ORDER.ID.eq(orderId)).update();
|
||||||
|
|
||||||
@ -317,11 +317,6 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
|
|||||||
.where(ORDER_DETAIL.ORDER_ID.eq(orderId)).list();
|
.where(ORDER_DETAIL.ORDER_ID.eq(orderId)).list();
|
||||||
//订单数量集合
|
//订单数量集合
|
||||||
Map<String, Integer> orderNumMap = orderDetails.stream().collect(Collectors.toMap(OrderDetail::getProductRecordId, OrderDetail::getNumber));
|
Map<String, Integer> orderNumMap = orderDetails.stream().collect(Collectors.toMap(OrderDetail::getProductRecordId, OrderDetail::getNumber));
|
||||||
ProductRecordService productRecordService = SpringUtils.getBean(ProductRecordService.class);
|
|
||||||
List<ProductRecord> productRecords = productRecordService.listByIds(orderDetails.stream().map(OrderDetail::getProductRecordId).collect(Collectors.toSet()));
|
|
||||||
productRecords.forEach(item -> {
|
|
||||||
item.setRecordNum(item.getRecordNum() - orderNumMap.get(item.getId()));
|
|
||||||
});
|
|
||||||
//TODO:分账先注释
|
//TODO:分账先注释
|
||||||
ProfitSharing profitSharing = new ProfitSharing();
|
ProfitSharing profitSharing = new ProfitSharing();
|
||||||
profitSharing.setOrderId(orderId)
|
profitSharing.setOrderId(orderId)
|
||||||
@ -351,7 +346,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
|
|||||||
.set(PROFIT_SHARING.AMOUNT, ceil.longValue()).set(PROFIT_SHARING.PROFIT_SHARING_STATUS, 1).set(PROFIT_SHARING.PAY_ACCOUNT, wxPayProperties.getSharingAccount())
|
.set(PROFIT_SHARING.AMOUNT, ceil.longValue()).set(PROFIT_SHARING.PROFIT_SHARING_STATUS, 1).set(PROFIT_SHARING.PAY_ACCOUNT, wxPayProperties.getSharingAccount())
|
||||||
.where(PROFIT_SHARING.ID.eq(profitSharing.getId())).update();
|
.where(PROFIT_SHARING.ID.eq(profitSharing.getId())).update();
|
||||||
//异步更新
|
//异步更新
|
||||||
SpringUtils.getBean(ProductRecordSyncStockTask.class).asyncUpdateRecords(productRecords, orderNumMap);
|
SpringUtils.getBean(ProductRecordSyncStockTask.class).asyncUpdateRecords(orderNumMap);
|
||||||
} catch (WxPayException e) {
|
} catch (WxPayException e) {
|
||||||
throw new ServiceException(e.getMessage());
|
throw new ServiceException(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,11 +9,14 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.scheduling.annotation.Async;
|
import org.springframework.scheduling.annotation.Async;
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.transaction.annotation.Isolation;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
import static com.cpop.mall.business.entity.table.ProductRecordTableDef.PRODUCT_RECORD;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author DB
|
* @author DB
|
||||||
@ -45,40 +48,41 @@ public class ProductRecordSyncStockTask {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description: 异步更新数据库
|
* @Description: 异步更新数据库
|
||||||
* @param productRecords 产品记录
|
* @param orderNumMap 产品记录库存
|
||||||
* @Author DB
|
* @Author DB
|
||||||
* @Date: 2023/11/1 0:09
|
* @Date: 2023/11/1 0:09
|
||||||
*/
|
*/
|
||||||
@Async("customAsyncThreadPool")
|
@Async("customAsyncThreadPool")
|
||||||
public void asyncUpdateRecords(List<ProductRecord> productRecords, Map<String, Integer> orderNumMap) {
|
public void asyncUpdateRecords(Map<String, Integer> orderNumMap) {
|
||||||
loopUpdateStock(productRecords, orderNumMap);
|
loopUpdateStock(orderNumMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param orderNumMap 库存修改记录
|
||||||
* @descriptions 循环插入库存修改记录
|
* @descriptions 循环插入库存修改记录
|
||||||
* @author DB
|
* @author DB
|
||||||
* @date 2023/11/01 17:01
|
* @date 2023/11/01 17:01
|
||||||
* @param productRecords 库存修改记录
|
|
||||||
* @return: void
|
* @return: void
|
||||||
*/
|
*/
|
||||||
private void loopUpdateStock(List<ProductRecord> productRecords,Map<String, Integer> orderNumMap) {
|
@Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
|
||||||
|
protected void loopUpdateStock(Map<String, Integer> orderNumMap) {
|
||||||
ProductRecordService productRecordService = SpringUtils.getBean(ProductRecordService.class);
|
ProductRecordService productRecordService = SpringUtils.getBean(ProductRecordService.class);
|
||||||
//用迭代器
|
//用迭代器
|
||||||
Iterator<ProductRecord> iterator = productRecords.iterator();
|
Iterator<Map.Entry<String, Integer>> iterator = orderNumMap.entrySet().iterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
ProductRecord next = iterator.next();
|
Map.Entry<String, Integer> next = iterator.next();
|
||||||
next.setRecordNum(next.getRecordNum() - orderNumMap.get(next.getId()));
|
boolean update = productRecordService.updateChain()
|
||||||
boolean update = productRecordService.updateById(next);
|
.setRaw(PRODUCT_RECORD.RECORD_NUM, "record_num +" + next.getValue())
|
||||||
|
.where(PRODUCT_RECORD.ID.eq(next.getKey())).update();
|
||||||
//如果更新成功,移除
|
//如果更新成功,移除
|
||||||
if (update) {
|
if (update) {
|
||||||
iterator.remove();
|
iterator.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//存在更新失败(乐观锁),需要重新更新,直到全部成功
|
//存在更新失败(乐观锁),需要重新更新,直到全部成功
|
||||||
if (!productRecords.isEmpty()){
|
if (!orderNumMap.isEmpty()) {
|
||||||
//获取最新数据进行下一次循环
|
//获取最新数据进行下一次循环
|
||||||
loopUpdateStock(productRecordService.listByIds(productRecords.stream().map(ProductRecord::getId).collect(Collectors.toSet())),
|
loopUpdateStock(orderNumMap);
|
||||||
orderNumMap);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user