java 将实体列表与Springboot中匹配的前端和更新字段的数据列表进行比较

dphi5xsq  于 2023-03-06  发布在  Java
关注(0)|答案(1)|浏览(121)

我想扣除库存中与商品代码匹配的可用数量
json数据来自 Postman

[billamount:1000,
disocunt:500,
billcart[{itemcode:1001, quantity:4 }, {itemcode:1002, quantity:7 }]]

方法

@Autowired
private StockRepository stockRepository;

public Bill createBill(Bill bill) {

 for (Billcart data : bill.getBillcart()) {
  Stock entity = stockRepository.findByItemcode(data.getItemcode());

 if(entity.getItemcode() ==data.getItemcode()){
   entity.setAvailableQuantity(entity.getAvailableQuantity()- data.getQuantity())
stockRepository.save(entity)
  }
}

扣除前

[{itemcode:1001, availableQuantity:20 }, {itemcode:1002, availableQuantity:40 }]]

结果应该是这样的:

[{itemcode:1001, availableQuantity:16 }, {itemcode:1002, availableQuantity:33 }]]
wqsoz72f

wqsoz72f1#

您还必须更新账单的价值:

@Autowired
private StockRepository stockRepository;

@Transactional
public Bill createBill(Bill bill) {
  for (Billcart data : bill.getBillcart()) {
    Stock entity = stockRepository.findByItemcode(data.getItemcode());
    entity.setAvailableQuantity(
      entity.getAvailableQuantity()-data.getQuantity());
    entity = stockRepository.save(entity);
    data.setAvailableQuantity(entity.getAvailableQuantity());
  }
}

相关问题