/** * 将消费信息初始化到对应的购买项中 * * @param items 订单购买项 * @param orderTickets 实际消费项 */ private void initOrderConsumptionInfo(List<OrderItem> items, List<OrderTickets> orderTickets) { for (OrderItem item : items) { // 没有消费记录就设为0 item.setConsumptionQuantity(0); item.setConsumptionSubtotal(0.0); item.setConsumptionSavings(0.0); if (orderTickets == null || orderTickets.isEmpty()) { continue; } for (OrderTickets ticket : orderTickets) { if (!StringUtils.equals(item.getMid(), ticket.getMid())) { continue; } // 初始化消费信息 ticket.setMid(item.getMid()); ticket.setName(item.getName()); ticket.setSellingPrice(item.getSellingPrice()); // 初始化购买信息 Integer q = ticket.getStatflag(); BigDecimal quantity = new BigDecimal(q + ""); BigDecimal price = new BigDecimal(item.getSellingPrice() + ""); price = price.setScale(2, RoundingMode.HALF_UP); // 精确到小数点2位,四舍五入 BigDecimal subtotal = price.multiply(quantity); item.setConsumptionQuantity(q); item.setConsumptionSubtotal(subtotal.doubleValue()); item.setConsumptionSavings(null); break; } } }
@Transactional(propagation = Propagation.REQUIRED) public void updateOrderItem(OrderItem orderItem) { // 取得要修改的数据 String itemId = orderItem.getId(); String orderNumber = orderItem.getOrderNumber(); Double priceItem = orderItem.getSellingPrice(); Integer quantityItem = orderItem.getQuantity(); Double actualSellingPriceItem = orderItem.getActualSellingPrice(); // 将item要修改的数据转换成精确类型,并重新赋值给item BigDecimal priceDecimal = new BigDecimal(priceItem + ""); priceDecimal = priceDecimal.setScale(2, RoundingMode.HALF_UP); // 精确到小数点2位,四舍五入 BigDecimal actualSellingPriceDecimal = new BigDecimal(actualSellingPriceItem + ""); actualSellingPriceDecimal = actualSellingPriceDecimal.setScale(2, RoundingMode.HALF_UP); // 精确到小数点2位,四舍五入 BigDecimal quantityDecimal = new BigDecimal(quantityItem + ""); BigDecimal subtotalDecimal = priceDecimal.multiply(quantityDecimal); BigDecimal totalDecimal = actualSellingPriceDecimal.multiply(quantityDecimal); BigDecimal savingsDecimal = subtotalDecimal.subtract(totalDecimal); orderItem.setActualSellingPrice(actualSellingPriceDecimal.doubleValue()); // 将用户输入的金额四舍五入 orderItem.setSavings(savingsDecimal.doubleValue()); orderItem.setSubtotal(subtotalDecimal.doubleValue()); orderItem.setTotal(totalDecimal.doubleValue()); // 取得订单基本信息和商品信息 Order order = orderManageMapper.getOrderByOrderNumber(orderNumber); List<OrderItem> items = orderManageMapper.getOrderItemByOrderNumber(orderNumber); // 初始化订单统计要用到的精确类型 Integer quantity = 0; BigDecimal subtotal = new BigDecimal("0"); BigDecimal savings = new BigDecimal("0"); BigDecimal total = new BigDecimal("0"); String savingsInfo = ""; // 重新计算订单总价 for (OrderItem item : items) { // 判断item是否是要修改的,如果是用前面转换的精确类型 if (StringUtils.equals(item.getId(), itemId)) { quantity += quantityItem; subtotal = subtotal.add(subtotalDecimal); savings = savings.add(savingsDecimal); total = total.add(totalDecimal); // 将所有商品的优惠信息去重合并到一起,放到订单的优惠信息中 if (StringUtils.isNotBlank(orderItem.getSavingInfo()) && !StringUtils.contains(savingsInfo, orderItem.getSavingInfo())) { savingsInfo += orderItem.getSavingInfo() + "<br>"; } continue; } quantity += item.getQuantity(); BigDecimal itemSubtotal = new BigDecimal(item.getSubtotal() + ""); BigDecimal itemSavings = new BigDecimal(item.getSavings() + ""); BigDecimal itemTotal = new BigDecimal(item.getTotal() + ""); subtotal = subtotal.add(itemSubtotal); savings = savings.add(itemSavings); total = total.add(itemTotal); String savingInfo = item.getSavingInfo(); // 设置优惠计算信息 if (StringUtils.isNotBlank(savingInfo) && !StringUtils.contains(savingsInfo, savingInfo)) { savingsInfo += savingInfo + "<br>"; } } order.setSavingsInfo(savingsInfo); order.setQuantity(quantity); order.setSubtotal(subtotal.doubleValue()); order.setSavings(savings.doubleValue()); order.setTotal(total.doubleValue()); order.setUpdatedById(orderItem.getUpdatedById()); order.setUpdatedByName(orderItem.getUpdatedByName()); order.setUpdatedTime(orderItem.getUpdatedTime()); // 将重新计算的订单统计信息放入订单对像中 // 更新数据库 orderManageMapper.updateOrderItem(orderItem); orderManageMapper.updateOrderFee(order); }