@@ -1,33 +1,20 @@
package com.czg.service.order.service.impl ;
import cn.hutool.core.bean.BeanUtil ;
import cn.hutool.core.collection.CollUtil ;
import cn.hutool.core.date.DateTime ;
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.ShopOrderStatistic ;
import com.czg.order.service.OrderInfoService ;
import com.czg.order.param.DataSummaryTradeParam ;
import com.czg.order.service.DataSummaryService ;
import com.czg.order.service.ShopOrderStatisticService ;
import com.czg.service.order.mapper.ShopOrderStatisticMapper ;
import com.mybatisflex.core.query.QueryWrapper ;
import com.mybatisflex.spring.service.impl.ServiceImpl ;
import jakarta.annotation.Resource ;
import org.apache.dubbo.config.annotation.DubboReference ;
import org.springframework.stereotype.Service ;
import java.math.BigDecimal ;
import java.math.RoundingMode ;
import java.time.LocalDate ;
import java.time.LocalDateTime ;
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
import java.util.stream.Collectors ;
/**
* 服务层实现。
@@ -38,11 +25,7 @@ import java.util.stream.Collectors;
@Service
public class ShopOrderStatisticServiceImpl extends ServiceImpl < ShopOrderStatisticMapper , ShopOrderStatistic > implements ShopOrderStatisticService {
@Resource
private OrderInfo Service orderInfo Service;
@DubboReference
private ShopUserFlowService shopUserFlowService ;
@DubboReference
private ShopTableService shopTableService ;
private DataSummary Service dataSummary Service;
@Override
@@ -53,108 +36,21 @@ public class ShopOrderStatisticServiceImpl extends ServiceImpl<ShopOrderStatisti
DateTime startOfDay = DateUtil . beginOfDay ( yesterday ) ;
// 获取前一天的结束时间( 23:59:59)
DateTime endOfDay = DateUtil . endOfDay ( yesterday ) ;
List < OrderInfo > orderInfos = orderInfoService . list ( new QueryWrapper ( )
. ge ( OrderInfo : : getPaidTime , startOfDay )
. le ( OrderInfo : : getPaidTime , endOfDay )
. ne ( OrderInfo : : getStatus , " unpaid " ) . ne ( OrderInfo : : getStatus , " cancelled " ) ) ;
// 统计充值记录
Map < Long , BigDecimal > flowMap = shopUserFlowService . list ( new QueryWrapper ( )
. ge ( ShopUserFlow : : getCreateTime , startOfDay . toLocalDateTime ( ) )
. le ( ShopUserFlow : : getCreateTime , endOfDay . toLocalDateTime ( ) )
. 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 < > ( ) ;
for ( OrderInfo item : orderInfos ) {
ShopOrderStatistic statisticTask = countInfo . get ( item . getShopId ( ) ) ;
if ( statisticTask = = null ) {
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 ( ) ) ) {
statisticTask . setRefundAmount ( statisticTask . getRefundAmount ( ) . add ( item . getRefundAmount ( ) ) ) ;
statisticTask . setRefundCount ( statisticTask . getRefundCount ( ) + 1 ) ;
if ( item . getRefundAmount ( ) . compareTo ( item . getPayAmount ( ) ) < 0 ) {
statisticTask . setSaleAmount ( statisticTask . getSaleAmount ( ) . add ( item . getPayAmount ( ) . subtract ( item . getRefundAmount ( ) ) ) ) ;
}
} else {
statisticTask . setSaleCount ( statisticTask . getSaleCount ( ) + 1 ) ;
statisticTask . setSaleAmount ( statisticTask . getSaleAmount ( ) . add ( item . getPayAmount ( ) ) ) ;
}
switch ( item . getPayType ( ) ) {
case " wechat-mini " :
statisticTask . setWechatPayAmount ( statisticTask . getWechatPayAmount ( ) . add ( item . getPayAmount ( ) ) ) ;
statisticTask . setWechatPayCount ( statisticTask . getWechatPayCount ( ) + 1 ) ;
break ;
case " main-scan " , " back-scan " :
statisticTask . setScanPayAmount ( statisticTask . getScanPayAmount ( ) . add ( item . getPayAmount ( ) ) ) ;
statisticTask . setScanPayCount ( statisticTask . getScanPayCount ( ) + 1 ) ;
break ;
case " alipay-mini " :
statisticTask . setAliPayAmount ( statisticTask . getAliPayAmount ( ) . add ( item . getPayAmount ( ) ) ) ;
statisticTask . setAliPayCount ( statisticTask . getAliPayCount ( ) + 1 ) ;
break ;
case " vip-pay " :
statisticTask . setMemberPayAmount ( statisticTask . getMemberPayAmount ( ) . add ( item . getPayAmount ( ) ) ) ;
statisticTask . setMemberPayCount ( statisticTask . getMemberPayCount ( ) + 1 ) ;
break ;
case " credit-pay " :
statisticTask . setCreditPayAmount ( statisticTask . getCreditPayAmount ( ) . add ( item . getPayAmount ( ) ) ) ;
statisticTask . setCreditPayCount ( statisticTask . getCreditPayCount ( ) + 1 ) ;
break ;
case " cash-pay " :
statisticTask . setCashPayAmount ( statisticTask . getCashPayAmount ( ) . add ( item . getPayAmount ( ) ) ) ;
statisticTask . setCashPayCount ( statisticTask . getCashPayCount ( ) + 1 ) ;
}
List < Long > shopIdList = dataSummaryService . getShopIdList ( ) ;
if ( CollUtil . isEmpty ( shopIdList ) ) {
return ;
}
countInfo . forEach ( ( shopId , info ) - > {
ShopOrderStatistic statistic = getOne ( new QueryWrapper ( ) . eq ( ShopOrderStatistic : : g etShopId, shopId ) . eq ( ShopOrderStatistic : : getCreateDay , yesterday . toSqlDate ( ) ) ) ;
if ( statistic = = null ) {
statistic = new ShopOrderStatistic ( ) ;
statistic . s etShopId ( shopId ) ;
statistic . setCreateDay ( LocalDate . now ( ) ) ;
}
BigDecimal totalAmount = statistic . getSaleAmount ( ) . add ( statistic . getRefundAmount ( ) ) ;
BigDecimal totalCount = BigDecimal . valueOf ( statistic . getSaleCount ( ) + statistic . getRefundCount ( ) ) ;
// 充值金额
// statistic.setRechargeAmount(BigDecimal.ZERO);
//客单价
if ( totalAmount . compareTo ( BigDecimal . ZERO ) ! = 0 ) {
statistic . setCustomerUnitPrice ( totalAmount . divide ( totalCount , 2 , RoundingMode . DOWN ) ) ;
}
// 查询台桌数量
long count = shopTableService . count ( new QueryWrapper ( ) . eq ( ShopTable : : getShopId , shopId ) ) ;
//翻台率
if ( count > 0 ) {
statistic . setTableTurnoverRate ( totalCount . subtract ( BigDecimal . valueOf ( count ) ) . divide ( BigDecimal . valueOf ( count ) , 2 , RoundingMode . DOWN ) . multiply ( BigDecimal . valueOf ( 100 ) ) ) ;
}
statistic . setUpdateTime ( LocalDateTime . now ( ) ) ;
BeanUtil . copyProperties ( info , statistic ) ;
shopIdList . parallelStream ( ) . forEach ( shopId - > {
DataSummaryTradeParam param = new DataSummaryTradeParam ( ) ;
param . s etShopId( shopId ) ;
param . setBeginDate ( startOfDay . toStringDefaultTimeZone ( ) ) ;
param . setEndDate ( endOfDay . toStringDefaultTimeZone ( ) ) ;
ShopOrderStatistic statistic = dataSummaryService . g etTradeData ( param ) ;
statistic . setShopId ( shopId ) ;
statistic . setCreateDay ( LocalDate . now ( ) ) ;
statistic . setUpdateTime ( LocalDateTime . now ( ) ) ;
saveOrUpdate ( statistic ) ;
} ) ;
}
}