From 19ff867deebbcf3c2863566bd0356a4c23854146 Mon Sep 17 00:00:00 2001 From: liuyingfang <1357764963@qq.com> Date: Thu, 18 Apr 2024 11:44:11 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A6=96=E9=A1=B5=E6=95=B0=E6=8D=AE=E6=94=B9?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/ysk/cashier/utils/DateUtil.java | 13 ++++ .../order/TbOrderDetailRepository.java | 31 ++++++++++ .../order/TbOrderInfoRepository.java | 60 ++++++++++++++++++- .../service/impl/SummaryServiceImpl.java | 60 ++++++++++++------- .../java/cn/ysk/cashier/vo/ProductExtVO.java | 12 ++-- 5 files changed, 149 insertions(+), 27 deletions(-) diff --git a/eladmin-common/src/main/java/cn/ysk/cashier/utils/DateUtil.java b/eladmin-common/src/main/java/cn/ysk/cashier/utils/DateUtil.java index 93815e40..20eefd76 100644 --- a/eladmin-common/src/main/java/cn/ysk/cashier/utils/DateUtil.java +++ b/eladmin-common/src/main/java/cn/ysk/cashier/utils/DateUtil.java @@ -44,6 +44,19 @@ public class DateUtil { return localDateTime.atZone(ZoneId.systemDefault()).toEpochSecond(); } + + /** + * 将Date对象转换为时间戳(毫秒) + * @param date 要转换的Date对象 + * @return 转换后的时间戳(毫秒) + */ + public static long convertDateToTimestamp(Date date) { + if (date == null) { + throw new IllegalArgumentException("Date cannot be null"); + } + return date.getTime(); + } + /** * 时间戳转LocalDateTime * diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/repository/order/TbOrderDetailRepository.java b/eladmin-system/src/main/java/cn/ysk/cashier/repository/order/TbOrderDetailRepository.java index e89f72de..f6c11083 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/repository/order/TbOrderDetailRepository.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/repository/order/TbOrderDetailRepository.java @@ -26,6 +26,7 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Query; +import javax.persistence.Tuple; import java.time.LocalDateTime; import java.util.Date; import java.util.List; @@ -79,4 +80,34 @@ public interface TbOrderDetailRepository extends JpaRepository queryTbOrderSalesCount(@Param("shopId") Integer shopId, @Param("startTime") Date startTime, @Param("endTime") Date endTime); + + @Query("SELECT new cn.ysk.cashier.vo.TbOrderPayCountVo(" + + "info.status," + + "SUM( info.num )) " + + "FROM TbOrderDetail info " + + "WHERE info.shopId = :shopId " + + "AND info.createTime > :startTime AND info.createTime < :endTime " + + "AND ((info.status = 'closed') OR ( info.status ='refund')) " ) + TbOrderPayCountVo queryTbOrderSalesCountExt(@Param("shopId") Integer shopId, @Param("startTime") Date startTime, @Param("endTime") Date endTime); + + + @Query(value = "select sum(num),count(1) from tb_order_detail where shop_id = :shopId " + + "AND ((status = 'closed') OR (status ='refund')) " + + "AND create_time > :startTime AND create_time < :endTime",nativeQuery = true) + Tuple sumByShopId(@Param("shopId") Integer shopId, @Param("startTime") Date startTime, @Param("endTime") Date endTime); + + @Query(value = "select sum(num),count(1),DATE(create_time) from tb_order_detail where shop_id = :shopId AND status = 'closed' " + + "AND create_time > :startTime AND create_time < :endTime " + + "GROUP BY DATE(create_time)",nativeQuery = true) + List sumByShopIdGroup(@Param("shopId") Integer shopId, @Param("startTime") Date startTime, @Param("endTime") Date endTime); + + @Query(value = "SELECT COUNT( 0 ) FROM ( SELECT product_id FROM tb_order_detail WHERE shop_id = :shopId " + + "AND create_time > :startTime AND create_time < :endTime AND (( STATUS = 'closed' ) \n" + + "\t\tOR ( STATUS = 'refund' )) \n" + + "\tGROUP BY\n" + + "\t\tproduct_id,\n" + + "\t\tproduct_sku_id \n" + + "\t) AS total ", nativeQuery = true) + Tuple searchCount(@Param("shopId") Integer shopId, @Param("startTime") Date startTime, @Param("endTime") Date endTime); + } \ No newline at end of file diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/repository/order/TbOrderInfoRepository.java b/eladmin-system/src/main/java/cn/ysk/cashier/repository/order/TbOrderInfoRepository.java index 9f505d8c..7cd0a288 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/repository/order/TbOrderInfoRepository.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/repository/order/TbOrderInfoRepository.java @@ -17,6 +17,7 @@ package cn.ysk.cashier.repository.order; import cn.ysk.cashier.pojo.order.TbOrderInfo; import cn.ysk.cashier.repository.mapping.CountPayTypeMapping; +import cn.ysk.cashier.vo.ProductExtVO; import cn.ysk.cashier.vo.TbOrderPayCountByDayVo; import cn.ysk.cashier.vo.TbOrderPayCountVo; import org.apache.ibatis.annotations.Param; @@ -26,6 +27,7 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Query; +import javax.persistence.Tuple; import java.util.Date; import java.util.List; @@ -58,7 +60,14 @@ public interface TbOrderInfoRepository extends JpaRepository queryTbOrderPayCountByDay(@Param("shopId") String shopId, @Param("startTime") Long startTime, @Param("endTime") Long endTime, Pageable pageable); + @Query(value = "SELECT " + + "row_number() over ( ORDER BY SUM( num ) DESC ) AS productId," + + "product_name," + + "SUM( CASE WHEN status = 'closed' THEN num ELSE 0 END ) as salesNum," + + "SUM( price ) " + + "FROM tb_order_detail " + + "WHERE shop_id = :shopId " + + "AND create_time > :startTime AND create_time < :endTime " + + "AND ((status = 'closed') OR (status ='refund')) " + + "GROUP BY product_id,product_sku_id " + + "ORDER BY salesNum DESC " + + "Limit :currentPage, :currentSize",nativeQuery = true) + List queryTbOrderPayCountByDayExt(@Param("shopId") Integer shopId, @Param("startTime") Date startTime, @Param("endTime") Date endTime, + @Param("currentPage") Integer currentPage, @Param("currentSize")Integer currentSize); + @Query(value = "SELECT ifnull( sum( order_amount ), 0 ) AS amount,trade_day as tradeDay " + + "FROM tb_order_info WHERE shop_id = :shopId AND ((status = 'closed') OR ( status ='refund' AND order_type != 'return' ))" + + "AND trade_day BETWEEN :startTime AND :endTime " + + "GROUP BY shop_id,trade_day",nativeQuery = true) + List queryTbOrderPaySumByDay(@Param("shopId") String shopId,@Param("startTime") Date startTime, @Param("endTime") Date endTime); + @Query(value = "SELECT ifnull( sum( order_amount ), 0 )" + + "FROM tb_order_info WHERE shop_id = :shopId AND ((status = 'closed') OR ( status ='refund' AND order_type != 'return' ))" + + "AND created_at > :startTime AND created_at < :endTime " ,nativeQuery = true) + Tuple queryPaySumByDayAll(@Param("shopId") String shopId,@Param("startTime") Long startTime, @Param("endTime") Long endTime); + + + @Query("select count(1),sum (info.payAmount) FROM TbOrderInfo info WHERE info.shopId = :shopId AND info.status = 'closed' ") + Tuple countByShopId(@Param("shopId") String shopId); + + + @Query(value = "select count(1), trade_day FROM tb_order_info WHERE shop_id = :shopId " + + "AND ((status = 'closed') OR ( status ='refund' AND order_type != 'return' ))" + + "AND trade_day BETWEEN :startTime AND :endTime " + + "GROUP BY shop_id,trade_day",nativeQuery = true) + List sumByDateOrderNum(@Param("shopId") String shopId,@Param("startTime") Date startTime, @Param("endTime") Date endTime); + @Query(value = "select count(1), sum(pay_amount) FROM tb_order_info WHERE shop_id = :shopId AND status = 'closed' " + + "AND trade_day BETWEEN :startTime AND :endTime ",nativeQuery = true) + Tuple sumByShopIdToday(@Param("shopId") String shopId,@Param("startTime") Date startTime, @Param("endTime") Date endTime); + @Query("SELECT new cn.ysk.cashier.vo.TbOrderPayCountByDayVo(" + "info.tradeDay," + "SUM( CASE WHEN info.payType = 'scanCode' THEN info.orderAmount ELSE 0 END )," + @@ -97,4 +144,15 @@ public interface TbOrderInfoRepository extends JpaRepository :startTime AND info.tradeDay < :endTime " + + "AND ((info.status = 'closed') OR ( info.status ='refund' AND info.orderType != 'return' )) ") + TbOrderPayCountVo queryOrderPayCountExt(@Param("shopId") String shopId, @Param("startTime") String startTime, @Param("endTime") String endTime); + + } \ No newline at end of file diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/SummaryServiceImpl.java b/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/SummaryServiceImpl.java index 07e8e002..2449b3fe 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/SummaryServiceImpl.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/SummaryServiceImpl.java @@ -14,6 +14,7 @@ import cn.ysk.cashier.utils.DateUtil; import cn.ysk.cashier.utils.FileUtil; import cn.ysk.cashier.vo.*; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -34,6 +35,7 @@ import java.util.*; @Service @RequiredArgsConstructor +@Slf4j public class SummaryServiceImpl implements SummaryService { @Resource private ShopUserDutyRepository shopUserDutyRepository; @@ -52,8 +54,8 @@ public class SummaryServiceImpl implements SummaryService { @Override public SummaryVO selectSummary(Integer shopId) { SummaryVO summaryVO = new SummaryVO(); - //支付笔数 - Tuple result = shopUserDutyRepository.sumByShopId(shopId); + //支付笔数, + 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) { @@ -65,11 +67,12 @@ public class SummaryServiceImpl implements SummaryService { Tuple count = tbShopUserRepository.searchByCount(shopId.toString()); summaryVO.setTotalUser(count == null ? 0L : count.get(0, Long.class)); //支付笔数柱形图 - List objects = shopUserDutyRepository.sumByDateOrderNum(shopId, DateUtil.getDate30DaysAgo(), DateUtil.getDayEnd()); + List objects = tbOrderInfoRepository.sumByDateOrderNum(shopId.toString(), DateUtil.getDate30DaysAgo(), DateUtil.getDayEnd()); List countDateList = new ArrayList<>(); for (Object[] o : objects) { CountDateVO countDateVO = new CountDateVO(); - countDateVO.setCount((BigDecimal) o[0]); + BigInteger integers = (BigInteger) o[0]; + countDateVO.setCount(new BigDecimal(integers.toString())); countDateVO.setTradeDay((String) o[1]); countDateList.add(countDateVO); } @@ -160,25 +163,28 @@ public class SummaryServiceImpl implements SummaryService { endTime = DateUtil.getDayEnd(); } HashMap map = new HashMap<>(); - Tuple tuple = shopUserDutyDetailRepository.searchByDutyIdSum(shopId, startTime, endTime); - List objects = shopUserDutyDetailRepository.searchByDutyGroup(shopId, DateUtil.getDate30DaysAgo(), DateUtil.getDayEnd()); + Tuple tuple = detailRepository.sumByShopId(shopId, startTime, endTime); + List objects = detailRepository.sumByShopIdGroup(shopId, startTime, endTime); + //List objects = shopUserDutyDetailRepository.searchByDutyGroup(shopId, DateUtil.getDate30DaysAgo(), DateUtil.getDayEnd()); List numList = new ArrayList<>(); for (Object[] o : objects) { CountDateVO countDateVO = new CountDateVO(); - countDateVO.setCount((BigDecimal) o[0]); - countDateVO.setTradeDay((String) o[1]); + BigInteger integers = (BigInteger)o[1]; + countDateVO.setCount(new BigDecimal(integers.toString())); + Date date =(Date)o[2]; + countDateVO.setTradeDay(date.toString()); numList.add(countDateVO); } - List objects1 = shopUserDutyDetailRepository.searchByGroup(shopId, DateUtil.getDate30DaysAgo(), DateUtil.getDayEnd()); + List objects2 = tbOrderInfoRepository.queryTbOrderPaySumByDay(shopId.toString(), startTime, endTime); List amountList = new ArrayList<>(); - for (Object[] o : objects1) { + for (Object[] o : objects2) { CountDateVO countDateVO = new CountDateVO(); countDateVO.setCount((BigDecimal) o[0]); countDateVO.setTradeDay((String) o[1]); amountList.add(countDateVO); } map.put("productCount", tuple == null ? 0 : tuple.get(0, BigDecimal.class)); - map.put("productSum", tuple == null ? 0 : tuple.get(1, BigDecimal.class)); + map.put("productSum", tuple == null ? 0 : tuple.get(1, BigInteger.class)); map.put("numList", numList); map.put("amountList", amountList); return map; @@ -201,12 +207,17 @@ public class SummaryServiceImpl implements SummaryService { startTime = DateUtil.getBeginDayOfYear(); endTime = DateUtil.getEndDayOfYear(); } else if (day == 1) { + startTime = DateUtil.getDayBegin(); + log.info("1天时间"+startTime.toString()); endTime = DateUtil.getDayEnd(); + log.info("1天时间末"+endTime.toString()); } else { throw new BadRequestException("日期有误"); } - List objects = shopUserDutyDetailRepository.searchByDutyId(shopId, startTime, endTime, currentPage, currentSize); + Pageable pageable = PageRequest.of(0, 5); + List objects = tbOrderInfoRepository.queryTbOrderPayCountByDayExt(shopId, startTime, endTime, currentPage, currentSize); + List list = new ArrayList<>(); for (Object[] o : objects) { ProductExtVO productVO = new ProductExtVO(); @@ -216,12 +227,18 @@ public class SummaryServiceImpl implements SummaryService { productVO.setAmount((BigDecimal) o[3]); list.add(productVO); } - //汇总数据 - Tuple tuple = shopUserDutyDetailRepository.searchByDutyIdSum(shopId, startTime, endTime); + //分页数据 - Tuple tuple1 = shopUserDutyDetailRepository.searchCount(shopId, startTime, endTime); - map.put("productCount", tuple == null ? 0 : tuple.get(0, BigDecimal.class)); - map.put("productSum", tuple == null ? 0 : tuple.get(1, BigDecimal.class)); + Tuple tuple1 = detailRepository.searchCount(shopId, startTime, endTime); + //销售数量 + Tuple payCount = detailRepository.sumByShopId(shopId, startTime, endTime); + //销售金额 + Long startTimeLong = DateUtil.convertDateToTimestamp(startTime); + Long endTimeLong = DateUtil.convertDateToTimestamp(endTime); + Tuple paySum = tbOrderInfoRepository.queryPaySumByDayAll(shopId.toString(), startTimeLong, endTimeLong); + + map.put("productCount", payCount == null ? 0 : payCount.get(0, BigDecimal.class)); + map.put("productSum", paySum == null ? 0 : paySum.get(0,BigDecimal.class)); map.put("totalProduct", list); map.put("total", tuple1 == null ? 0 : tuple1.get(0, BigInteger.class)); return map; @@ -242,7 +259,8 @@ public class SummaryServiceImpl implements SummaryService { throw new BadRequestException("日期有误"); } - List objects = shopUserDutyRepository.sumByDate(shopId, startTime, endTime); + List objects = tbOrderInfoRepository.queryTbOrderPaySumByDay(shopId.toString(), startTime, endTime); + //根据时间的销量 List sumDateVOList = new ArrayList<>(); for (Object[] o : objects) { @@ -278,8 +296,6 @@ public class SummaryServiceImpl implements SummaryService { // 将SumDateVO对象添加到列表中 sumDateList.add(sumDateVO); } - - sumDateList.sort((a, b) -> a.getTradeDay().compareTo(b.getTradeDay())); map.put("total", sumDateList); return map; @@ -288,8 +304,8 @@ public class SummaryServiceImpl implements SummaryService { @Override public Map selectSummaryToday(Integer shopId) { HashMap map = new HashMap<>(); - Tuple tuple = shopUserDutyRepository.sumByShopIdToday(shopId, DateUtil.getDayBegin(), DateUtil.getDayEnd()); - map.put("paymentsNumberToday", tuple == null ? 0L : tuple.get(0, Long.class)); + Tuple tuple = tbOrderInfoRepository.sumByShopIdToday(shopId.toString(), DateUtil.getDayBegin(), DateUtil.getDayEnd()); + map.put("paymentsNumberToday", tuple == null ? 0L : tuple.get(0, BigInteger.class)); map.put("totalSalesToday", tuple == null ? new BigDecimal("0") : tuple.get(1, BigDecimal.class)); Tuple tuple1 = tbShopUserRepository.searchByCountToday(shopId.toString(), DateUtil.getTodayStartTimestamp(), DateUtil.getTodayEndTimestamp()); map.put("userToday", tuple1.get(0, Long.class)); diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/vo/ProductExtVO.java b/eladmin-system/src/main/java/cn/ysk/cashier/vo/ProductExtVO.java index 8bf21ecf..7aaa405e 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/vo/ProductExtVO.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/vo/ProductExtVO.java @@ -1,6 +1,7 @@ package cn.ysk.cashier.vo; import lombok.Data; +import org.hibernate.annotations.Formula; import java.math.BigDecimal; import java.math.BigInteger; @@ -10,17 +11,20 @@ import java.math.BigInteger; */ @Data public class ProductExtVO { - private BigInteger productId; private String productName; - private BigDecimal productNum; - private BigDecimal amount; + private Object productNum; + private Object amount; - public ProductExtVO() { + public ProductExtVO(BigInteger productId, String productName, Object productNum, Object amount) { this.productId = productId; this.productName = productName; this.productNum = productNum; + this.amount = amount; + } + + public ProductExtVO() { } }