|  | @@ -1,6 +1,7 @@
 | 
	
		
			
				|  |  |  package cn.newfeifan.mall.module.distri.service.dailybill;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -import cn.newfeifan.mall.module.distri.service.dailybill.dto.MerchantDTO;
 | 
	
		
			
				|  |  | +import cn.newfeifan.mall.module.distri.dal.dataobject.ptdailybill.PtDailyBillDO;
 | 
	
		
			
				|  |  | +import cn.newfeifan.mall.module.distri.dal.mysql.ptdailybill.PtDailyBillMapper;
 | 
	
		
			
				|  |  |  import cn.newfeifan.mall.module.distri.service.dailybill.dto.OrderDTO;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -36,6 +37,8 @@ public class DailyBillServiceImpl implements DailyBillService {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Resource
 | 
	
		
			
				|  |  |      private DailyBillMapper dailyBillMapper;
 | 
	
		
			
				|  |  | +    @Resource
 | 
	
		
			
				|  |  | +    private PtDailyBillMapper ptDailyBillMapper;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      public Long createDailyBill(DailyBillSaveReqVO createReqVO) {
 | 
	
	
		
			
				|  | @@ -88,55 +91,83 @@ public class DailyBillServiceImpl implements DailyBillService {
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  |      public void caleDailyBill() {
 | 
	
		
			
				|  |  | -        // 获取所有商户及旗下的店铺
 | 
	
		
			
				|  |  | -        List<MerchantDTO> merchantDTOS = dailyBillMapper.getMerchantDTO();
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        merchantDTOS.forEach(merchantDTO -> {
 | 
	
		
			
				|  |  | -            merchantDTO.getShopIds().forEach(shopId -> {
 | 
	
		
			
				|  |  | -                // 获取每个店铺的订单列表
 | 
	
		
			
				|  |  | -                List<OrderDTO> orderDetails = dailyBillMapper.getOrderDetailsByMerchantIdWithsShopId(merchantDTO.getMerchantId(), shopId);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -                // 使用Stream API和Collectors.groupingBy按照日期分组,并按时间倒序排序每个日期的Order列表
 | 
	
		
			
				|  |  | -                Map<LocalDate, List<OrderDTO>> ordersByDate = orderDetails.stream()
 | 
	
		
			
				|  |  | -                        .collect(Collectors.groupingBy(order -> order.getCreateTime().toLocalDate(),
 | 
	
		
			
				|  |  | -                                LinkedHashMap::new, // 保持插入顺序
 | 
	
		
			
				|  |  | -                                Collectors.toList()));
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -                // 计算分组结果,并按订单创建时间倒序输出每个日期的订单
 | 
	
		
			
				|  |  | -                ordersByDate.forEach((date, orderList) -> {
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -                    Long price = orderList.stream()
 | 
	
		
			
				|  |  | -                            .mapToLong(OrderDTO::getPayPrice)
 | 
	
		
			
				|  |  | -                            .sum();
 | 
	
		
			
				|  |  | -                    Long refundPrice = orderList.stream()
 | 
	
		
			
				|  |  | -                            .mapToLong(OrderDTO::getRefundPrice)
 | 
	
		
			
				|  |  | -                            .sum();
 | 
	
		
			
				|  |  | -                    Long integral = orderList.stream()
 | 
	
		
			
				|  |  | -                            .mapToLong(OrderDTO::getPayIntegral)
 | 
	
		
			
				|  |  | -                            .sum();
 | 
	
		
			
				|  |  | -                    Long refundIntegral = orderList.stream()
 | 
	
		
			
				|  |  | -                            .filter(order -> order.getRefundIntegral() != null)  // 过滤掉 getRefundIntegral 为空的订单
 | 
	
		
			
				|  |  | -                            .mapToLong(OrderDTO::getRefundIntegral)
 | 
	
		
			
				|  |  | -                            .sum();
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -                    DailyBillDO dailyBillDO = DailyBillDO.builder()
 | 
	
		
			
				|  |  | -                            .price(price)
 | 
	
		
			
				|  |  | -                            .refundPrice(refundPrice)
 | 
	
		
			
				|  |  | -                            .receivedPrice(price - refundPrice)
 | 
	
		
			
				|  |  | -                            .integral(integral)
 | 
	
		
			
				|  |  | -                            .refundIntegral(refundIntegral)
 | 
	
		
			
				|  |  | -                            .receivedIntegral(integral - refundIntegral)
 | 
	
		
			
				|  |  | -                            .orderCount((long) orderList.size())
 | 
	
		
			
				|  |  | -                            .shopId(shopId)
 | 
	
		
			
				|  |  | -                            .merchantId(merchantDTO.getMerchantId())
 | 
	
		
			
				|  |  | -                            .orderIds(orderList.stream().map(OrderDTO::getId).collect(Collectors.toList()).toString())
 | 
	
		
			
				|  |  | -                            .orderCalcTime(date)
 | 
	
		
			
				|  |  | -                            .build();
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -                    dailyBillMapper.insert(dailyBillDO);
 | 
	
		
			
				|  |  | -                    dailyBillMapper.updateOrderItemByOrderId(orderList.stream().map(OrderDTO::getId).collect(Collectors.toList()));
 | 
	
		
			
				|  |  | -                });
 | 
	
		
			
				|  |  | +        // 获取每天的订单列表
 | 
	
		
			
				|  |  | +        List<OrderDTO> orderDetails = dailyBillMapper.getOrderDetails();
 | 
	
		
			
				|  |  | +        // 使用Stream API和Collectors.groupingBy按照日期分组,并按时间倒序排序每个日期的Order列表
 | 
	
		
			
				|  |  | +        Map<LocalDate, List<OrderDTO>> ordersByDate = orderDetails.stream()
 | 
	
		
			
				|  |  | +                .collect(Collectors.groupingBy(order -> order.getCreateTime().toLocalDate(),
 | 
	
		
			
				|  |  | +                        LinkedHashMap::new, // 保持插入顺序
 | 
	
		
			
				|  |  | +                        Collectors.toList()));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        ordersByDate.forEach((date, orderList) -> {
 | 
	
		
			
				|  |  | +            // 计算分组结果,并按订单创建时间倒序输出每个日期的订单
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            Long ptPrice = orderList.stream()
 | 
	
		
			
				|  |  | +                    .mapToLong(OrderDTO::getPayPrice)
 | 
	
		
			
				|  |  | +                    .sum();
 | 
	
		
			
				|  |  | +            Long ptRefundPrice = orderList.stream()
 | 
	
		
			
				|  |  | +                    .mapToLong(OrderDTO::getRefundPrice)
 | 
	
		
			
				|  |  | +                    .sum();
 | 
	
		
			
				|  |  | +            Long ptIntegral = orderList.stream()
 | 
	
		
			
				|  |  | +                    .mapToLong(OrderDTO::getPayIntegral)
 | 
	
		
			
				|  |  | +                    .sum();
 | 
	
		
			
				|  |  | +            Long ptRefundIntegral = orderList.stream()
 | 
	
		
			
				|  |  | +                    .filter(order -> order.getRefundIntegral() != null)  // 过滤掉 getRefundIntegral 为空的订单
 | 
	
		
			
				|  |  | +                    .mapToLong(OrderDTO::getRefundIntegral)
 | 
	
		
			
				|  |  | +                    .sum();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            PtDailyBillDO ptDailyBillDO = PtDailyBillDO.builder()
 | 
	
		
			
				|  |  | +                    .price(ptPrice)
 | 
	
		
			
				|  |  | +                    .refundPrice(ptRefundPrice)
 | 
	
		
			
				|  |  | +                    .receivedPrice(ptPrice - ptRefundPrice)
 | 
	
		
			
				|  |  | +                    .integral(ptIntegral)
 | 
	
		
			
				|  |  | +                    .refundIntegral(ptRefundIntegral)
 | 
	
		
			
				|  |  | +                    .receivedIntegral(ptIntegral - ptRefundIntegral)
 | 
	
		
			
				|  |  | +                    .orderCount((long) orderList.size())
 | 
	
		
			
				|  |  | +                    .orderIds(orderList.stream().map(OrderDTO::getId).collect(Collectors.toList()).toString())
 | 
	
		
			
				|  |  | +                    .orderCalcTime(date)
 | 
	
		
			
				|  |  | +                    .build();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            ptDailyBillMapper.insert(ptDailyBillDO);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            Map<Long, List<OrderDTO>> dailyBillByShop = orderList.stream()
 | 
	
		
			
				|  |  | +                    .collect(Collectors.groupingBy(OrderDTO::getShopId,
 | 
	
		
			
				|  |  | +                            Collectors.toList()));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            dailyBillByShop.forEach((shopId, shopOrderList) -> {
 | 
	
		
			
				|  |  | +                Long price = shopOrderList.stream()
 | 
	
		
			
				|  |  | +                        .mapToLong(OrderDTO::getPayPrice)
 | 
	
		
			
				|  |  | +                        .sum();
 | 
	
		
			
				|  |  | +                Long refundPrice = shopOrderList.stream()
 | 
	
		
			
				|  |  | +                        .mapToLong(OrderDTO::getRefundPrice)
 | 
	
		
			
				|  |  | +                        .sum();
 | 
	
		
			
				|  |  | +                Long integral = shopOrderList.stream()
 | 
	
		
			
				|  |  | +                        .mapToLong(OrderDTO::getPayIntegral)
 | 
	
		
			
				|  |  | +                        .sum();
 | 
	
		
			
				|  |  | +                Long refundIntegral = shopOrderList.stream()
 | 
	
		
			
				|  |  | +                        .filter(order -> order.getRefundIntegral() != null)  // 过滤掉 getRefundIntegral 为空的订单
 | 
	
		
			
				|  |  | +                        .mapToLong(OrderDTO::getRefundIntegral)
 | 
	
		
			
				|  |  | +                        .sum();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                DailyBillDO dailyBillDO = DailyBillDO.builder()
 | 
	
		
			
				|  |  | +                        .price(price)
 | 
	
		
			
				|  |  | +                        .refundPrice(refundPrice)
 | 
	
		
			
				|  |  | +                        .receivedPrice(price - refundPrice)
 | 
	
		
			
				|  |  | +                        .integral(integral)
 | 
	
		
			
				|  |  | +                        .refundIntegral(refundIntegral)
 | 
	
		
			
				|  |  | +                        .receivedIntegral(integral - refundIntegral)
 | 
	
		
			
				|  |  | +                        .orderCount((long) shopOrderList.size())
 | 
	
		
			
				|  |  | +                        .orderIds(shopOrderList.stream().map(OrderDTO::getId).collect(Collectors.toList()).toString())
 | 
	
		
			
				|  |  | +                        .orderCalcTime(date)
 | 
	
		
			
				|  |  | +                        .shopId(shopId)
 | 
	
		
			
				|  |  | +                        .merchantId(shopOrderList.get(0).getMerchantId())
 | 
	
		
			
				|  |  | +                        .ptDailyBillId(ptDailyBillDO.getId())
 | 
	
		
			
				|  |  | +                        .build();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                dailyBillMapper.insert(dailyBillDO);
 | 
	
		
			
				|  |  |              });
 | 
	
		
			
				|  |  | +            //修改订单状态为已经计算每日账单
 | 
	
		
			
				|  |  | +            dailyBillMapper.updateOrderItemByOrderId(orderList.stream().map(OrderDTO::getId).collect(Collectors.toList()));
 | 
	
		
			
				|  |  |          });
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 |