|
@@ -0,0 +1,147 @@
|
|
|
+package cn.newfeifan.mall.module.distri.service.dailybill;
|
|
|
+
|
|
|
+import cn.newfeifan.mall.framework.common.pojo.PageResult;
|
|
|
+import cn.newfeifan.mall.framework.common.util.object.BeanUtils;
|
|
|
+import cn.newfeifan.mall.module.distri.controller.admin.dailybill.vo.DailyBillPageReqVO;
|
|
|
+import cn.newfeifan.mall.module.distri.controller.admin.dailybill.vo.DailyBillSaveReqVO;
|
|
|
+import cn.newfeifan.mall.module.distri.dal.dataobject.dailybill.DailyBillDO;
|
|
|
+import cn.newfeifan.mall.module.distri.dal.mysql.dailybill.DailyBillMapper;
|
|
|
+import cn.newfeifan.mall.module.distri.service.dailybill.dto.MerchantDTO;
|
|
|
+import cn.newfeifan.mall.module.distri.service.dailybill.dto.OrderDTO;
|
|
|
+import cn.newfeifan.mall.module.system.controller.admin.user.vo.user.UserShopDetailsVO;
|
|
|
+import cn.newfeifan.mall.module.system.service.user.AdminUserService;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
+import static cn.newfeifan.mall.module.distri.enums.ErrorCodeConstants.DAILY_BILL_NOT_EXISTS;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 每日账单 Service 实现类
|
|
|
+ *
|
|
|
+ * @author 非繁人
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Validated
|
|
|
+public class DailyBillServiceImpl implements DailyBillService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private DailyBillMapper dailyBillMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private AdminUserService userService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Long createDailyBill(DailyBillSaveReqVO createReqVO) {
|
|
|
+ // 插入
|
|
|
+ DailyBillDO dailyBill = BeanUtils.toBean(createReqVO, DailyBillDO.class);
|
|
|
+ dailyBillMapper.insert(dailyBill);
|
|
|
+ // 返回
|
|
|
+ return dailyBill.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateDailyBill(DailyBillSaveReqVO updateReqVO) {
|
|
|
+ // 校验存在
|
|
|
+ validateDailyBillExists(updateReqVO.getId());
|
|
|
+ // 更新
|
|
|
+ DailyBillDO updateObj = BeanUtils.toBean(updateReqVO, DailyBillDO.class);
|
|
|
+ dailyBillMapper.updateById(updateObj);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteDailyBill(Long id) {
|
|
|
+ // 校验存在
|
|
|
+ validateDailyBillExists(id);
|
|
|
+ // 删除
|
|
|
+ dailyBillMapper.deleteById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void validateDailyBillExists(Long id) {
|
|
|
+ if (dailyBillMapper.selectById(id) == null) {
|
|
|
+ throw exception(DAILY_BILL_NOT_EXISTS);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public DailyBillDO getDailyBill(Long id) {
|
|
|
+ return dailyBillMapper.selectById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageResult<DailyBillDO> getDailyBillPage(DailyBillPageReqVO pageReqVO) {
|
|
|
+ UserShopDetailsVO userShopDetails = userService.getUserShopDetails();
|
|
|
+ pageReqVO.setMerchantId(userShopDetails.getMerId());
|
|
|
+ pageReqVO.setShopId(userShopDetails.getShopId());
|
|
|
+ return dailyBillMapper.selectPage(pageReqVO);
|
|
|
+ }
|
|
|
+
|
|
|
+ @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)
|
|
|
+ .receivedIntegral(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()));
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public DailyBillDO getDailyBill(Long merchantId, Long shopId, LocalDate localDate) {
|
|
|
+ return dailyBillMapper.selectOne(DailyBillDO::getMerchantId, merchantId, DailyBillDO::getShopId, shopId, DailyBillDO::getOrderCalcTime, localDate);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|