Merge remote-tracking branch 'origin/test' into test
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package com.czg.service.order.mapper;
|
||||
|
||||
import com.czg.order.entity.ShopOrderStatistic;
|
||||
import com.czg.order.vo.CostLineChartVO;
|
||||
import com.czg.order.vo.ProductCostAmountVO;
|
||||
import com.czg.order.vo.ProfitRateVO;
|
||||
import com.czg.order.vo.TotalVo;
|
||||
import com.mybatisflex.core.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
@@ -141,6 +143,31 @@ public interface ShopOrderStatisticMapper extends BaseMapper<ShopOrderStatistic>
|
||||
Map<String, BigDecimal> getPayTypeDateRangeRaw(Long shopId, LocalDate start, LocalDate end);
|
||||
|
||||
|
||||
@Select("SELECT" +
|
||||
" profit_rate as profitRate, " +
|
||||
" net_profit_rate as netProfitRate, " +
|
||||
" statistic_date as tradeDay" +
|
||||
" FROM" +
|
||||
" tb_shop_order_statistic " +
|
||||
" WHERE" +
|
||||
" shop_id = #{shopId} " +
|
||||
"and statistic_date >= #{start} " +
|
||||
"and statistic_date <= #{end} ")
|
||||
List<ProfitRateVO> profitRateBarChart(Long shopId, LocalDate start, LocalDate end);
|
||||
|
||||
|
||||
@Select("SELECT" +
|
||||
" product_cost_amount as productCostAmount, " +
|
||||
" statistic_date as tradeDay" +
|
||||
" FROM" +
|
||||
" tb_shop_order_statistic " +
|
||||
" WHERE" +
|
||||
" shop_id = #{shopId} " +
|
||||
"and statistic_date >= #{start} " +
|
||||
"and statistic_date <= #{end} ")
|
||||
List<CostLineChartVO> costLineChart(Long shopId, LocalDate start, LocalDate end);
|
||||
|
||||
|
||||
//*********************以下为日常统计********************************************************************************
|
||||
|
||||
|
||||
|
||||
@@ -6,9 +6,7 @@ import cn.hutool.core.collection.CollUtil;
|
||||
import com.czg.exception.CzgException;
|
||||
import com.czg.order.entity.ShopOrderStatistic;
|
||||
import com.czg.order.service.ShopOrderStatisticService;
|
||||
import com.czg.order.vo.CountPayTypeVo;
|
||||
import com.czg.order.vo.ProductCostAmountVO;
|
||||
import com.czg.order.vo.TotalVo;
|
||||
import com.czg.order.vo.*;
|
||||
import com.czg.service.order.mapper.ShopOrderStatisticMapper;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.spring.service.impl.ServiceImpl;
|
||||
@@ -65,23 +63,56 @@ public class ShopOrderStatisticServiceImpl extends ServiceImpl<ShopOrderStatisti
|
||||
@Override
|
||||
public List<TotalVo> getDateAmount(Long shopId, Integer day) {
|
||||
LocalDate currentDate = LocalDate.now();
|
||||
LocalDate startDate = currentDate;
|
||||
if (day == 7) {
|
||||
startDate = currentDate.minusDays(6);
|
||||
} else if (day == 30) {
|
||||
startDate = currentDate.minusDays(29);
|
||||
}
|
||||
LocalDate startDate;
|
||||
TotalVo onlineDataAmount = mapper.getOnlineDataAmount(shopId, currentDate);
|
||||
if (onlineDataAmount == null) {
|
||||
onlineDataAmount = new TotalVo(currentDate);
|
||||
}
|
||||
if (day <= 1) {
|
||||
return List.of(onlineDataAmount);
|
||||
}else {
|
||||
startDate = currentDate.minusDays(day - 1);
|
||||
}
|
||||
List<TotalVo> statDateRange = mapper.getStatDateRange(shopId, startDate, currentDate);
|
||||
|
||||
return TotalVo.mergeAndFillData(onlineDataAmount, statDateRange, startDate, currentDate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CountPayTypeVo> getSummaryPayTypeData(Long shopId, Integer day) {
|
||||
LocalDate currentDate = LocalDate.now();
|
||||
LocalDate startDate;
|
||||
if (day <= 1) {
|
||||
Map<String, BigDecimal> onlinePayTypeDate = mapper.getOnlinePayTypeDate(shopId, currentDate);
|
||||
return CountPayTypeVo.realTimeDataByDay(onlinePayTypeDate);
|
||||
}else {
|
||||
startDate = currentDate.minusDays(day - 1);
|
||||
}
|
||||
return CountPayTypeVo.mergePayTypeData(
|
||||
mapper.getOnlinePayTypeDate(shopId, currentDate),
|
||||
mapper.getPayTypeDateRangeRaw(shopId, startDate, currentDate));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ProfitRateVO> profitRateBarChart(Long shopId, Integer day) {
|
||||
LocalDate currentDate = LocalDate.now();
|
||||
LocalDate startDate;
|
||||
ShopOrderStatistic onlineDataAmount = getRealTimeDataByDay(shopId, currentDate);
|
||||
ProfitRateVO onlineProfitRateBarChart = new ProfitRateVO(currentDate);
|
||||
if (onlineDataAmount != null) {
|
||||
onlineProfitRateBarChart.setProfitRate(onlineDataAmount.getProfitRate());
|
||||
onlineProfitRateBarChart.setNetProfitRate(onlineDataAmount.getNetProfitRate());
|
||||
}
|
||||
if (day <= 1) {
|
||||
return List.of(onlineProfitRateBarChart);
|
||||
}else {
|
||||
startDate = currentDate.minusDays(day - 1);
|
||||
}
|
||||
List<ProfitRateVO> statDateRange = mapper.profitRateBarChart(shopId, startDate, currentDate);
|
||||
return ProfitRateVO.mergeAndFillData(onlineProfitRateBarChart, statDateRange, startDate, currentDate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CostLineChartVO> costLineChart(Long shopId, Integer day) {
|
||||
LocalDate currentDate = LocalDate.now();
|
||||
LocalDate startDate = currentDate;
|
||||
if (day == 7) {
|
||||
@@ -89,11 +120,17 @@ public class ShopOrderStatisticServiceImpl extends ServiceImpl<ShopOrderStatisti
|
||||
} else if (day == 30) {
|
||||
startDate = currentDate.minusDays(29);
|
||||
}
|
||||
return CountPayTypeVo.mergePayTypeData(
|
||||
mapper.getOnlinePayTypeDate(shopId, currentDate),
|
||||
mapper.getPayTypeDateRangeRaw(shopId, startDate, currentDate));
|
||||
CostLineChartVO onlineCostLineChart = new CostLineChartVO(currentDate);
|
||||
BigDecimal productCostAmount = getProductCostAmount(shopId, currentDate);
|
||||
onlineCostLineChart.setProductCostAmount(productCostAmount);
|
||||
|
||||
List<CostLineChartVO> statDateRange = mapper.costLineChart(shopId, startDate, currentDate);
|
||||
return CostLineChartVO.mergeAndFillData(onlineCostLineChart, statDateRange, startDate, currentDate);
|
||||
}
|
||||
|
||||
|
||||
//u--------------------------------------------------_---------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public void statisticAndInsert(Long shopId, LocalDate day) {
|
||||
ShopOrderStatistic realTimeData = getRealTimeDataByDay(shopId, day);
|
||||
@@ -185,7 +222,7 @@ public class ShopOrderStatisticServiceImpl extends ServiceImpl<ShopOrderStatisti
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品成本价
|
||||
* 获取商品总成本价
|
||||
*/
|
||||
private BigDecimal getProductCostAmount(Long shopId, LocalDate day) {
|
||||
BigDecimal productCostAmount = BigDecimal.ZERO;
|
||||
@@ -223,43 +260,77 @@ public class ShopOrderStatisticServiceImpl extends ServiceImpl<ShopOrderStatisti
|
||||
return productCostAmount;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 统一入口方法
|
||||
* 计算 客单价 翻台率 净利润 净利率 毛利润 毛利率
|
||||
*/
|
||||
private void calculateShopOrderStatistic(ShopOrderStatistic result) {
|
||||
//毛利润(订单实付金额-商品成本)
|
||||
calculateProfitInfo(result);
|
||||
calculateAvgPayAmount(result);
|
||||
calculateTurnoverRate(result);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 计算毛利润、毛利率(净利润、净利率与之一致)
|
||||
*/
|
||||
private void calculateProfitInfo(ShopOrderStatistic result) {
|
||||
// 初始化商品成本金额(避免null)
|
||||
if (result.getProductCostAmount() == null) {
|
||||
result.setProductCostAmount(BigDecimal.ZERO);
|
||||
}
|
||||
result.setProfitAmount(result.getPayAmount().subtract(result.getProductCostAmount()));
|
||||
//毛利率(订单实付金额-商品成本)/订单实付金额*100%
|
||||
|
||||
// 计算毛利润(订单实付金额-商品成本)
|
||||
BigDecimal profitAmount = result.getPayAmount().subtract(result.getProductCostAmount());
|
||||
result.setProfitAmount(profitAmount);
|
||||
|
||||
// 计算毛利率((订单实付金额-商品成本)/订单实付金额*100%)
|
||||
BigDecimal profitRate = BigDecimal.ZERO;
|
||||
if (result.getPayAmount().compareTo(BigDecimal.ZERO) > 0) {
|
||||
BigDecimal profitRate = result.getProfitAmount().divide(result.getPayAmount(), 4, RoundingMode.HALF_DOWN).multiply(BigDecimal.valueOf(100));
|
||||
result.setProfitRate(profitRate);
|
||||
} else {
|
||||
result.setProfitRate(BigDecimal.ZERO);
|
||||
profitRate = profitAmount.divide(result.getPayAmount(), 4, RoundingMode.HALF_DOWN)
|
||||
.multiply(BigDecimal.valueOf(100));
|
||||
}
|
||||
//净利润 净利率 目前和 毛利润 毛利率 一致
|
||||
result.setNetProfitRate(result.getProfitRate());
|
||||
result.setNetProfitAmount(result.getProfitAmount());
|
||||
//客单价 实付金额(包括线上支付 包含现金支付 包含会员支付 包含挂账)/就餐人数
|
||||
result.setProfitRate(profitRate);
|
||||
|
||||
// 净利润、净利率目前和毛利润、毛利率一致
|
||||
result.setNetProfitAmount(profitAmount);
|
||||
result.setNetProfitRate(profitRate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算客单价
|
||||
* 客单价 = 实付金额 / 就餐人数(就餐人数为0时直接使用实付金额)
|
||||
*/
|
||||
private void calculateAvgPayAmount(ShopOrderStatistic result) {
|
||||
BigDecimal avgPayAmount;
|
||||
// 校验就餐人数有效性
|
||||
if (result.getCustomerCount() != null && result.getCustomerCount() > 0) {
|
||||
result.setAvgPayAmount(result.getPayAmount().divide(new BigDecimal(result.getCustomerCount()), 2, RoundingMode.HALF_DOWN));
|
||||
avgPayAmount = result.getPayAmount().divide(
|
||||
new BigDecimal(result.getCustomerCount()), 2, RoundingMode.HALF_DOWN
|
||||
);
|
||||
} else {
|
||||
result.setAvgPayAmount(result.getPayAmount());
|
||||
avgPayAmount = result.getPayAmount();
|
||||
}
|
||||
//翻台率 (订单数-桌台数)/桌台数*100%
|
||||
result.setAvgPayAmount(avgPayAmount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算翻台率
|
||||
* 翻台率 = (订单数-桌台数)/桌台数*100%(差值≤0时翻台率为0)
|
||||
*/
|
||||
private void calculateTurnoverRate(ShopOrderStatistic result) {
|
||||
BigDecimal turnoverRate = BigDecimal.ZERO;
|
||||
|
||||
// 校验桌台数有效性
|
||||
if (result.getTableCount() != null && result.getTableCount() > 0) {
|
||||
long orderTableDifference = result.getOrderCount() - result.getTableCount();
|
||||
BigDecimal turnoverRate = BigDecimal.ZERO;
|
||||
// 仅当差值大于0时计算翻台率
|
||||
if (orderTableDifference > 0) {
|
||||
turnoverRate = new BigDecimal(orderTableDifference).divide(new BigDecimal(result.getTableCount()), 2, RoundingMode.HALF_DOWN);
|
||||
turnoverRate = new BigDecimal(orderTableDifference)
|
||||
.divide(new BigDecimal(result.getTableCount()), 2, RoundingMode.HALF_DOWN);
|
||||
}
|
||||
result.setTurnoverRate(turnoverRate);
|
||||
} else {
|
||||
result.setTurnoverRate(BigDecimal.ZERO);
|
||||
}
|
||||
result.setTurnoverRate(turnoverRate);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user