订单统计定时任务修改

This commit is contained in:
张松 2025-03-12 19:34:53 +08:00
parent 1fdfe4b772
commit 66dfc9994f
1 changed files with 48 additions and 6 deletions

View File

@ -1,8 +1,14 @@
package com.czg.service.order.service.impl; package com.czg.service.order.service.impl;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import com.czg.account.entity.ShopTable;
import com.czg.account.entity.ShopUserFlow;
import com.czg.account.service.ShopTableService;
import com.czg.account.service.ShopUserFlowService;
import com.czg.order.entity.OrderInfo; import com.czg.order.entity.OrderInfo;
import com.czg.order.entity.ShopOrderStatistic; import com.czg.order.entity.ShopOrderStatistic;
import com.czg.order.service.OrderInfoService; import com.czg.order.service.OrderInfoService;
@ -11,13 +17,17 @@ import com.czg.service.order.mapper.ShopOrderStatisticMapper;
import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl; import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/** /**
* 服务层实现 * 服务层实现
@ -29,6 +39,10 @@ import java.util.List;
public class ShopOrderStatisticServiceImpl extends ServiceImpl<ShopOrderStatisticMapper, ShopOrderStatistic> implements ShopOrderStatisticService { public class ShopOrderStatisticServiceImpl extends ServiceImpl<ShopOrderStatisticMapper, ShopOrderStatistic> implements ShopOrderStatisticService {
@Resource @Resource
private OrderInfoService orderInfoService; private OrderInfoService orderInfoService;
@DubboReference
private ShopUserFlowService shopUserFlowService;
@DubboReference
private ShopTableService shopTableService;
@Override @Override
@ -44,12 +58,32 @@ public class ShopOrderStatisticServiceImpl extends ServiceImpl<ShopOrderStatisti
.le(OrderInfo::getCreateTime, endOfDay) .le(OrderInfo::getCreateTime, endOfDay)
.ne(OrderInfo::getStatus, "unpaid").ne(OrderInfo::getStatus, "cancelled")); .ne(OrderInfo::getStatus, "unpaid").ne(OrderInfo::getStatus, "cancelled"));
// 统计充值记录
Map<Long, BigDecimal> flowMap = shopUserFlowService.list(new QueryWrapper()
.ge(ShopUserFlow::getCreateTime, startOfDay)
.le(ShopUserFlow::getCreateTime, endOfDay)
.in(ShopUserFlow::getBizCode, CollUtil.newArrayList("cashIn", "wechatIn", "alipayIn", "awardIn"))).stream()
.collect(Collectors.groupingBy(
ShopUserFlow::getShopId,
Collectors.reducing(
BigDecimal.ZERO,
ShopUserFlow::getAmount,
BigDecimal::add
)
));
HashMap<Long, ShopOrderStatistic> countInfo = new HashMap<>(); HashMap<Long, ShopOrderStatistic> countInfo = new HashMap<>();
for (OrderInfo item : orderInfos) { for (OrderInfo item : orderInfos) {
ShopOrderStatistic statisticTask = countInfo.get(item.getShopId()); ShopOrderStatistic statisticTask = countInfo.get(item.getShopId());
if (statisticTask == null) { if (statisticTask == null) {
countInfo.put(item.getShopId(), statisticTask = new ShopOrderStatistic()); countInfo.put(item.getShopId(), statisticTask = new ShopOrderStatistic());
} }
BigDecimal bigDecimal = flowMap.get(item.getShopId());
if (bigDecimal != null) {
statisticTask.setRechargeAmount(bigDecimal);
}
if ("refunding".equals(item.getStatus()) || "refund".equals(item.getStatus()) || "part-refund".equals(item.getStatus())) { if ("refunding".equals(item.getStatus()) || "refund".equals(item.getStatus()) || "part-refund".equals(item.getStatus())) {
statisticTask.setRefundAmount(statisticTask.getRefundAmount().add(item.getRefundAmount())); statisticTask.setRefundAmount(statisticTask.getRefundAmount().add(item.getRefundAmount()));
statisticTask.setRefundCount(statisticTask.getRefundCount() + 1); statisticTask.setRefundCount(statisticTask.getRefundCount() + 1);
@ -61,6 +95,8 @@ public class ShopOrderStatisticServiceImpl extends ServiceImpl<ShopOrderStatisti
statisticTask.setSaleAmount(statisticTask.getSaleAmount().add(item.getPayAmount())); statisticTask.setSaleAmount(statisticTask.getSaleAmount().add(item.getPayAmount()));
} }
switch (item.getPayType()) { switch (item.getPayType()) {
case "wechat-mini": case "wechat-mini":
statisticTask.setWechatPayAmount(statisticTask.getWechatPayAmount().add(item.getPayAmount())); statisticTask.setWechatPayAmount(statisticTask.getWechatPayAmount().add(item.getPayAmount()));
@ -97,12 +133,18 @@ public class ShopOrderStatisticServiceImpl extends ServiceImpl<ShopOrderStatisti
statistic.setShopId(shopId); statistic.setShopId(shopId);
statistic.setCreateDay(LocalDate.now()); statistic.setCreateDay(LocalDate.now());
} }
//TODO 充值金额 BigDecimal totalAmount = statistic.getSaleAmount().add(statistic.getRefundAmount());
statistic.setRechargeAmount(BigDecimal.ZERO); BigDecimal totalCount = BigDecimal.valueOf(statistic.getSaleCount() + statistic.getRefundCount());
//TODO 客单价 // 充值金额
statistic.setCustomerUnitPrice(BigDecimal.ZERO); // statistic.setRechargeAmount(BigDecimal.ZERO);
//TODO 翻台率 //客单价
statistic.setTableTurnoverRate(BigDecimal.ZERO); statistic.setCustomerUnitPrice(totalAmount.divide(totalCount, 2, RoundingMode.DOWN));
// 查询台桌数量
long count = shopTableService.count(new QueryWrapper().eq(ShopTable::getShopId, shopId));
//翻台率
statistic.setTableTurnoverRate(totalCount.subtract(BigDecimal.valueOf(count)).divide(BigDecimal.valueOf(count), 2, RoundingMode.DOWN).multiply(BigDecimal.valueOf(100)));
statistic.setUpdateTime(LocalDateTime.now()); statistic.setUpdateTime(LocalDateTime.now());
BeanUtil.copyProperties(info, statistic); BeanUtil.copyProperties(info, statistic);
saveOrUpdate(statistic); saveOrUpdate(statistic);