首页 数据统计

This commit is contained in:
wangw 2024-09-10 15:54:40 +08:00
parent 6c480acd20
commit efd9d78227
6 changed files with 243 additions and 93 deletions

View File

@ -2,6 +2,7 @@ package cn.ysk.cashier.controller.shop;
import cn.ysk.cashier.annotation.rest.AnonymousGetMapping;
import cn.ysk.cashier.annotation.rest.AnonymousPostMapping;
import cn.ysk.cashier.dto.BaseQueryDto;
import cn.ysk.cashier.dto.shop.ShopTableSaleInfoDto;
import cn.ysk.cashier.service.SummaryService;
import lombok.RequiredArgsConstructor;
@ -23,11 +24,20 @@ public class SummaryController {
@Resource
private SummaryService summaryService;
@GetMapping
private Object shopSummary(@RequestParam Integer shopId) {
return summaryService.selectSummary(shopId);
@PostMapping("/trade")
@AnonymousPostMapping
private Object shopSummaryDate(@RequestBody BaseQueryDto param) {
return summaryService.trade(param);
}
// @GetMapping
// private Object shopSummary(@RequestParam Integer shopId) {
// return summaryService.selectSummary(shopId);
// }
@GetMapping("/date")
private Object shopSummaryDate(@RequestParam Integer shopId, @RequestParam Integer day) {
return summaryService.selectSummaryDate(shopId, day);

View File

@ -0,0 +1,36 @@
package cn.ysk.cashier.dto;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
public class BaseQueryDto {
private Integer shopId;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date startTime;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date endTime;
public Integer getShopId() {
return shopId;
}
public void setShopId(Integer shopId) {
this.shopId = shopId;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
}

View File

@ -5,9 +5,9 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
/**
* @author GYJ
@ -41,6 +41,33 @@ public interface TbShopUserFlowMapper extends BaseMapper<TbShopUserFlow> {
@Param("startTime") String startTime,
@Param("endTime") String endTime);
@Select("<script>" +
"SELECT" +
" COALESCE(SUM(CASE WHEN flow.biz_code IN ('cashMemberIn', 'inMoneyIn', 'scanMemberIn') THEN flow.amount ELSE 0 END),0) AS 'inAmount'," +
" COALESCE(SUM(CASE WHEN flow.biz_code = 'inMoneyOut' THEN flow.amount ELSE 0 END),0) AS 'outAmount'," +
" COALESCE(SUM(CASE WHEN flow.biz_code IN ('accountGroupPay', 'accountPay', 'consumeOut', 'vipCardCash') THEN flow.amount ELSE 0 END),0) AS 'useAmount'," +
" COALESCE(COUNT(CASE WHEN flow.biz_code IN ('accountGroupPay', 'accountPay', 'consumeOut', 'vipCardCash') THEN 1 ELSE NULL END),0) AS 'useNum'" +
"FROM" +
" tb_shop_user_flow flow" +
" INNER JOIN tb_shop_user vip ON flow.shop_user_id = vip.id and vip.shop_id= #{shopId} " +
"WHERE" +
" flow.create_time BETWEEN #{startTime} AND #{endTime} " +
"</script>")
Map<String, Object> tradeIndexFlow(@Param("shopId") Integer shopId,
@Param("startTime") String startTime,
@Param("endTime") String endTime);
@Select("<script>" +
"SELECT COALESCE(count(1),0) from( " +
" SELECT flow.create_time " +
" FROM tb_shop_user_flow flow " +
" INNER JOIN tb_shop_user vip ON flow.shop_user_id = vip.id AND vip.shop_id = #{shopId} " +
"WHERE biz_code IN ('cashMemberIn', 'inMoneyIn', 'scanMemberIn') " +
"GROUP BY " +
" shop_user_id) as flows where flows.create_time BETWEEN #{startTime} AND #{endTime}" +
"</script>")
Integer tradeIndexNewFlow(@Param("shopId") Integer shopId, @Param("startTime") String startTime, @Param("endTime") String endTime);
@Select(value = "select * from tb_shop_user_flow where shop_user_id=#{userId} order by id desc limit #{page},#{size}")
List<TbShopUserFlow> selectByUserId(@Param("userId") Integer userId,@Param("page") Integer page,@Param("size") Integer size);

View File

@ -31,6 +31,7 @@ import javax.persistence.Tuple;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @website https://eladmin.vip
@ -54,6 +55,22 @@ public interface TbOrderInfoRepository extends JpaRepository<TbOrderInfo, Intege
"AND info.createdAt>:start AND info.createdAt<:end")
TbOrderPayCountVo queryTbOrderRefund(@Param("shopId")String shopId, @Param("tableName") String tableName ,@Param("start") Long start, @Param("end") Long end);
@Query(value = "SELECT " +
"COALESCE(SUM( info.order_amount ),0) AS 'saleAmount'," +
"COALESCE(count( 1 ),0) AS 'saleNum'," +
"COALESCE(sum( discount_amount ),0) AS 'saveAmount'," +
"COALESCE(COUNT( CASE WHEN discount_amount IS NOT NULL AND discount_amount > 0.00 THEN 1 ELSE NULL END ),0) AS 'saveNum' " +
"FROM tb_order_info info " +
"WHERE info.shop_id = :shopId " +
"AND info.created_at > :startTime AND info.created_at < :endTime " +
"AND ((info.STATUS = 'closed' ) OR ( info.STATUS = 'refund' AND info.order_type != 'return' ))", nativeQuery = true)
Map<String,Object> tradeIndex(@Param("shopId") Integer shopId, @Param("startTime") Long startTime, @Param("endTime") Long endTime);
@Query(value = "SELECT COALESCE(SUM(info.order_amount),0) FROM tb_order_info info " +
"WHERE info.shop_id = :shopId AND info.order_type = 'return' " +
"AND info.created_at > :startTime AND info.created_at < :endTime", nativeQuery = true)
BigDecimal tradeIndexRefund(@Param("shopId") Integer shopId, @Param("startTime") Long startTime, @Param("endTime") Long endTime);
@Query(value = "SELECT COUNT(1) ,pay_type AS payType FROM tb_order_info Where shop_id = :shopId AND " +
" created_at BETWEEN :startTime AND :endTime AND status='closed'AND order_type <>'return' GROUP BY pay_type", nativeQuery = true)
List<Object[]> countByShopId(@Param("shopId") String shopId, @Param("startTime") Long startTime, @Param("endTime") Long endTime);

View File

@ -1,5 +1,6 @@
package cn.ysk.cashier.service;
import cn.ysk.cashier.dto.BaseQueryDto;
import cn.ysk.cashier.dto.ShopSummaryDto;
import cn.ysk.cashier.dto.shop.ShopTableSaleInfoDto;
import cn.ysk.cashier.vo.ShopTableSaleInfoVo;
@ -17,8 +18,9 @@ import java.util.Map;
* @author lyf
*/
public interface SummaryService {
SummaryVO selectSummary(Integer shop);
Map<String,Object> trade(BaseQueryDto param);
// SummaryVO selectSummary(Integer shop);
Map<String,Object> selectSummaryDate(Integer shopId, Integer day);
Map<String,Object> selectSummaryToday(Integer shopId);

View File

@ -1,15 +1,19 @@
package cn.ysk.cashier.service.impl;
import cn.hutool.core.util.ObjectUtil;
import cn.ysk.cashier.dto.ShopSummaryDto;
import cn.ysk.cashier.dto.BaseQueryDto;
import cn.ysk.cashier.dto.shop.ShopTableSaleInfoDto;
import cn.ysk.cashier.enums.PayTypeEnum;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.mybatis.mapper.TbShopUserFlowMapper;
import cn.ysk.cashier.mybatis.service.TbShopUserFlowService;
import cn.ysk.cashier.repository.ShopUserDutyDetailRepository;
import cn.ysk.cashier.repository.ShopUserDutyRepository;
import cn.ysk.cashier.repository.TbTokenRepository;
import cn.ysk.cashier.repository.order.TbOrderDetailRepository;
import cn.ysk.cashier.repository.order.TbOrderInfoRepository;
import cn.ysk.cashier.repository.shop.TbShopTableRepository;
import cn.ysk.cashier.repository.shop.TbShopUserRepository;
import cn.ysk.cashier.service.SummaryService;
import cn.ysk.cashier.utils.DateUtil;
@ -56,106 +60,160 @@ public class SummaryServiceImpl implements SummaryService {
@Resource
private TbTokenRepository tbTokenRepository;
@Autowired
private TbShopUserFlowMapper tbShopUserFlowMapper;
@Resource
private TbShopUserFlowService tbShopUserFlowService;
@Autowired
private TbOrderDetailRepository tbOrderDetailRepository;
private final TbShopTableRepository tbShopTableRepository;
@Override
public SummaryVO selectSummary(Integer shopId) {
SummaryVO summaryVO = new SummaryVO();
//支付笔数,
Tuple result = tbOrderInfoRepository.countByShopId(shopId.toString());
summaryVO.setPaymentsNumber(result.get(0, Long.class) == null ? 0L : result.get(0, Long.class));
summaryVO.setTotalSales(result.get(1, BigDecimal.class) == null ? new BigDecimal("0") : result.get(1, BigDecimal.class));
if (summaryVO.getPaymentsNumber() == 0) {
summaryVO.setAverageSales(new BigDecimal("0"));
} else {
summaryVO.setAverageSales(summaryVO.getTotalSales().divide(new BigDecimal(summaryVO.getPaymentsNumber()), 2, RoundingMode.DOWN));
public Map<String, Object> trade(BaseQueryDto param) {
if (ObjectUtil.isNull(param.getShopId())) {
throw new BadRequestException("参数不合法");
}
//用户数
Tuple count = tbShopUserRepository.searchByCount(shopId.toString());
summaryVO.setTotalUser(count == null ? 0L : count.get(0, Long.class));
//支付笔数柱形图
List<Object[]> objects = tbOrderInfoRepository.sumByDateOrderNum(shopId.toString(), DateUtil.getDate30DaysAgo(), DateUtil.getDayEnd());
List<CountDateVO> countDateList = new ArrayList<>();
for (Object[] o : objects) {
CountDateVO countDateVO = new CountDateVO();
BigInteger integers = (BigInteger) o[0];
countDateVO.setCount(new BigDecimal(integers.toString()));
countDateVO.setTradeDay((String) o[1]);
countDateList.add(countDateVO);
Map<String, Object> result = new HashMap<>();
Map<String, Object> sale = new HashMap<>();
Map<String, Object> vip = new HashMap<>();
Map<String, Object> count = new HashMap<>();
//订单 销售金额 saleAmount /订单数 saleNum /优惠金额 saveAmount /优惠笔数 saveNum
Map<String, Object> orderMap = tbOrderInfoRepository.tradeIndex(param.getShopId(), param.getStartTime().getTime(), param.getEndTime().getTime());
//退款金额
BigDecimal refundAmount = tbOrderInfoRepository.tradeIndexRefund(param.getShopId(), param.getStartTime().getTime(), param.getEndTime().getTime());
//会员 充值金额 inAmount /退款金额 outAmount /会员消费金额 useAmount /会员消费笔数 useNum
Map<String, Object> flowMap = tbShopUserFlowMapper.tradeIndexFlow(param.getShopId(), DateUtil.getStrTime(param.getStartTime()), DateUtil.getStrTime(param.getEndTime()));
// 新增会员数
Integer newFlow = tbShopUserFlowMapper.tradeIndexNewFlow(param.getShopId(), DateUtil.getStrTime(param.getStartTime()), DateUtil.getStrTime(param.getEndTime()));
//台桌数
int tables = tbShopTableRepository.countAllByShopId(param.getShopId());
sale.put("totalSaleAmount", new BigDecimal(orderMap.get("saleAmount").toString()).add(refundAmount));//总金额
sale.put("incomeAmount", new BigDecimal(orderMap.get("saleAmount").toString()).add(new BigDecimal(flowMap.get("inAmount").toString())));//实收
sale.put("refundAmount", refundAmount);//销售退款金额
sale.put("inAmount", flowMap.get("inAmount"));//会员充值金额
sale.put("outAmount", flowMap.get("outAmount"));//会员退款金额
result.put("sale",sale);
vip.put("useAmount", flowMap.get("useAmount"));//会员消费金额
vip.put("newFlow",newFlow);//新增会员数
vip.put("useNum",flowMap.get("useNum"));//会员消费笔数
result.put("vip",vip);
BigDecimal saleAmount = new BigDecimal(orderMap.get("saleAmount").toString()).add(refundAmount);
//客单价
if(saleAmount.compareTo(BigDecimal.ZERO) == 0){
count.put("unitPrice",BigDecimal.ZERO);
}else {
count.put("unitPrice",saleAmount.divide(new BigDecimal(orderMap.get("saleNum").toString()),2,RoundingMode.DOWN));
}
//填充日期
Map<String, CountDateVO> dataMap = new HashMap<>();
for (CountDateVO entry : countDateList) {
String tradeDay = entry.getTradeDay();
BigDecimal countOrder = entry.getCount();
dataMap.put(tradeDay, new CountDateVO(tradeDay, countOrder));
BigDecimal saleNum = new BigDecimal(orderMap.get("saleNum").toString());
if(saleNum.compareTo(BigDecimal.ZERO) == 0){
count.put("turnoverRate",BigDecimal.ZERO);//翻台率
}else {
count.put("turnoverRate",saleNum.divide(new BigDecimal(tables),2,RoundingMode.DOWN));//翻台率
}
// 获取今天的日期
LocalDate today = LocalDate.now();
// 定义日期格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 转换为字符串
List<CountDateVO> countDateLists = new ArrayList<>();
for (int i = 0; i < 30; i++) {
LocalDate tradeDayLocalDate = today.minusDays(i);
String tradeDayString = tradeDayLocalDate.format(formatter);
CountDateVO countDateVO;
// 检查数据Map中是否存在该日期的数据
if (dataMap.containsKey(tradeDayString)) {
countDateVO = dataMap.get(tradeDayString);
} else {
// 如果不存在则创建新的SumDateVO对象amount设为0
countDateVO = new CountDateVO(tradeDayString, BigDecimal.ZERO);
}
// 将SumDateVO对象添加到列表中
countDateLists.add(countDateVO);
}
countDateLists.sort((a, b) -> a.getTradeDay().compareTo(b.getTradeDay()));
summaryVO.setCountDateList(countDateLists);
//访问量
Tuple tuple = tbTokenRepository.countByAccountId(shopId);
summaryVO.setTotalVisits(tuple == null ? 0L : tuple.get(0, Long.class));
//访问量柱状图
List<Object[]> objectsVisits = tbTokenRepository.countByMonth(shopId, DateUtil.getDate30DaysAgo(), DateUtil.getDayEnd());
List<CountVisitsVO> visitsList = new ArrayList<>();
for (Object[] o : objectsVisits) {
CountVisitsVO countDateVO = new CountVisitsVO();
countDateVO.setCount((BigInteger) o[0]);
countDateVO.setTradeDay((String) o[1]);
visitsList.add(countDateVO);
}
//填充日期
Map<String, CountVisitsVO> dataVisitsMap = new HashMap<>();
for (CountVisitsVO entry : visitsList) {
String tradeDay = entry.getTradeDay();
BigInteger countOrder = entry.getCount();
dataVisitsMap.put(tradeDay, new CountVisitsVO(tradeDay, countOrder));
}
// 转换为字符串
List<CountVisitsVO> countVisitsLists = new ArrayList<>();
for (int i = 0; i < 30; i++) {
LocalDate tradeDayLocalDate = today.minusDays(i);
String tradeDayString = tradeDayLocalDate.format(formatter);
CountVisitsVO countDateVO;
// 检查数据Map中是否存在该日期的数据
if (dataVisitsMap.containsKey(tradeDayString)) {
countDateVO = dataVisitsMap.get(tradeDayString);
} else {
// 如果不存在则创建新的SumDateVO对象amount设为0
countDateVO = new CountVisitsVO(tradeDayString, BigInteger.ZERO);
}
// 将SumDateVO对象添加到列表中
countVisitsLists.add(countDateVO);
}
countVisitsLists.sort((a, b) -> a.getTradeDay().compareTo(b.getTradeDay()));
summaryVO.setVisitsCountList(countVisitsLists);
return summaryVO;
count.put("saveAmount",orderMap.get("saveAmount"));//优惠金额
count.put("saveNum",orderMap.get("saveNum"));//优惠单数
result.put("count",count);
return result;
}
// @Override
// public SummaryVO selectSummary(Integer shopId) {
// SummaryVO summaryVO = new SummaryVO();
// //支付笔数,
// Tuple result = tbOrderInfoRepository.countByShopId(shopId.toString());
// summaryVO.setPaymentsNumber(result.get(0, Long.class) == null ? 0L : result.get(0, Long.class));
// summaryVO.setTotalSales(result.get(1, BigDecimal.class) == null ? new BigDecimal("0") : result.get(1, BigDecimal.class));
// if (summaryVO.getPaymentsNumber() == 0) {
// summaryVO.setAverageSales(new BigDecimal("0"));
// } else {
// summaryVO.setAverageSales(summaryVO.getTotalSales().divide(new BigDecimal(summaryVO.getPaymentsNumber()), 2, RoundingMode.DOWN));
// }
// //用户数
// Tuple count = tbShopUserRepository.searchByCount(shopId.toString());
// summaryVO.setTotalUser(count == null ? 0L : count.get(0, Long.class));
// //支付笔数柱形图
// List<Object[]> objects = tbOrderInfoRepository.sumByDateOrderNum(shopId.toString(), DateUtil.getDate30DaysAgo(), DateUtil.getDayEnd());
// List<CountDateVO> countDateList = new ArrayList<>();
// for (Object[] o : objects) {
// CountDateVO countDateVO = new CountDateVO();
// BigInteger integers = (BigInteger) o[0];
// countDateVO.setCount(new BigDecimal(integers.toString()));
// countDateVO.setTradeDay((String) o[1]);
// countDateList.add(countDateVO);
// }
// //填充日期
// Map<String, CountDateVO> dataMap = new HashMap<>();
// for (CountDateVO entry : countDateList) {
// String tradeDay = entry.getTradeDay();
// BigDecimal countOrder = entry.getCount();
// dataMap.put(tradeDay, new CountDateVO(tradeDay, countOrder));
// }
// // 获取今天的日期
// LocalDate today = LocalDate.now();
// // 定义日期格式
// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// // 转换为字符串
// List<CountDateVO> countDateLists = new ArrayList<>();
// for (int i = 0; i < 30; i++) {
// LocalDate tradeDayLocalDate = today.minusDays(i);
// String tradeDayString = tradeDayLocalDate.format(formatter);
// CountDateVO countDateVO;
// // 检查数据Map中是否存在该日期的数据
// if (dataMap.containsKey(tradeDayString)) {
// countDateVO = dataMap.get(tradeDayString);
// } else {
// // 如果不存在则创建新的SumDateVO对象amount设为0
// countDateVO = new CountDateVO(tradeDayString, BigDecimal.ZERO);
// }
// // 将SumDateVO对象添加到列表中
// countDateLists.add(countDateVO);
// }
// countDateLists.sort((a, b) -> a.getTradeDay().compareTo(b.getTradeDay()));
// summaryVO.setCountDateList(countDateLists);
// //访问量
// Tuple tuple = tbTokenRepository.countByAccountId(shopId);
// summaryVO.setTotalVisits(tuple == null ? 0L : tuple.get(0, Long.class));
// //访问量柱状图
// List<Object[]> objectsVisits = tbTokenRepository.countByMonth(shopId, DateUtil.getDate30DaysAgo(), DateUtil.getDayEnd());
// List<CountVisitsVO> visitsList = new ArrayList<>();
// for (Object[] o : objectsVisits) {
// CountVisitsVO countDateVO = new CountVisitsVO();
// countDateVO.setCount((BigInteger) o[0]);
// countDateVO.setTradeDay((String) o[1]);
// visitsList.add(countDateVO);
// }
// //填充日期
// Map<String, CountVisitsVO> dataVisitsMap = new HashMap<>();
// for (CountVisitsVO entry : visitsList) {
// String tradeDay = entry.getTradeDay();
// BigInteger countOrder = entry.getCount();
// dataVisitsMap.put(tradeDay, new CountVisitsVO(tradeDay, countOrder));
// }
// // 转换为字符串
// List<CountVisitsVO> countVisitsLists = new ArrayList<>();
// for (int i = 0; i < 30; i++) {
// LocalDate tradeDayLocalDate = today.minusDays(i);
// String tradeDayString = tradeDayLocalDate.format(formatter);
// CountVisitsVO countDateVO;
// // 检查数据Map中是否存在该日期的数据
// if (dataVisitsMap.containsKey(tradeDayString)) {
// countDateVO = dataVisitsMap.get(tradeDayString);
// } else {
// // 如果不存在则创建新的SumDateVO对象amount设为0
// countDateVO = new CountVisitsVO(tradeDayString, BigInteger.ZERO);
// }
// // 将SumDateVO对象添加到列表中
// countVisitsLists.add(countDateVO);
// }
// countVisitsLists.sort((a, b) -> a.getTradeDay().compareTo(b.getTradeDay()));
// summaryVO.setVisitsCountList(countVisitsLists);
// return summaryVO;
// }
@Override
public Map<String, Object> selectSummaryDate(Integer shopId, Integer day) {
Date startTime = new Date();