首页数据改正

This commit is contained in:
liuyingfang 2024-04-18 11:44:11 +08:00
parent ed5c27c37e
commit 19ff867dee
5 changed files with 149 additions and 27 deletions

View File

@ -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
*

View File

@ -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<TbOrderDetail, In
"GROUP BY info.status")
List<TbOrderPayCountVo> 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<Object[]> 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);
}

View File

@ -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<TbOrderInfo, Intege
@Query("SELECT count(1) FROM TbOrderInfo WHERE source = :source AND shopId=:shopId")
int isRefund(@Param("source")Integer source,@Param("shopId") String shopId);
/**
*
* @param shopId
* @param startTime
* @param endTime
* @param pageable
* @return
*/
@Query("SELECT new cn.ysk.cashier.vo.TbOrderPayCountByDayVo(" +
"info.tradeDay," +
"SUM( CASE WHEN info.payType = 'scanCode' THEN info.orderAmount ELSE 0 END )," +
@ -72,8 +81,46 @@ public interface TbOrderInfoRepository extends JpaRepository<TbOrderInfo, Intege
"GROUP BY info.tradeDay " +
"ORDER BY info.tradeDay DESC")
Page<TbOrderPayCountByDayVo> 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<Object[]> 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<Object[]> 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<Object[]> 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<TbOrderInfo, Intege
"AND ((info.status = 'closed') OR ( info.status ='refund' AND info.orderType != 'return' )) ")
TbOrderPayCountVo queryOrderPayCount(@Param("shopId") String shopId, @Param("startTime") Long startTime, @Param("endTime") Long endTime);
@Query("SELECT new cn.ysk.cashier.vo.TbOrderPayCountVo(" +
"'总金额'," +
"SUM( info.orderAmount)) " +
"FROM TbOrderInfo info " +
"WHERE info.shopId = :shopId " +
"AND info.tradeDay > :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);
}

View File

@ -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<Object[]> objects = shopUserDutyRepository.sumByDateOrderNum(shopId, DateUtil.getDate30DaysAgo(), DateUtil.getDayEnd());
List<Object[]> objects = tbOrderInfoRepository.sumByDateOrderNum(shopId.toString(), DateUtil.getDate30DaysAgo(), DateUtil.getDayEnd());
List<CountDateVO> 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<String, Object> map = new HashMap<>();
Tuple tuple = shopUserDutyDetailRepository.searchByDutyIdSum(shopId, startTime, endTime);
List<Object[]> objects = shopUserDutyDetailRepository.searchByDutyGroup(shopId, DateUtil.getDate30DaysAgo(), DateUtil.getDayEnd());
Tuple tuple = detailRepository.sumByShopId(shopId, startTime, endTime);
List<Object[]> objects = detailRepository.sumByShopIdGroup(shopId, startTime, endTime);
//List<Object[]> objects = shopUserDutyDetailRepository.searchByDutyGroup(shopId, DateUtil.getDate30DaysAgo(), DateUtil.getDayEnd());
List<CountDateVO> 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<Object[]> objects1 = shopUserDutyDetailRepository.searchByGroup(shopId, DateUtil.getDate30DaysAgo(), DateUtil.getDayEnd());
List<Object[]> objects2 = tbOrderInfoRepository.queryTbOrderPaySumByDay(shopId.toString(), startTime, endTime);
List<CountDateVO> 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<Object[]> objects = shopUserDutyDetailRepository.searchByDutyId(shopId, startTime, endTime, currentPage, currentSize);
Pageable pageable = PageRequest.of(0, 5);
List<Object[]> objects = tbOrderInfoRepository.queryTbOrderPayCountByDayExt(shopId, startTime, endTime, currentPage, currentSize);
List<ProductExtVO> 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<Object[]> objects = shopUserDutyRepository.sumByDate(shopId, startTime, endTime);
List<Object[]> objects = tbOrderInfoRepository.queryTbOrderPaySumByDay(shopId.toString(), startTime, endTime);
//根据时间的销量
List<SumDateVO> 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<String, Object> selectSummaryToday(Integer shopId) {
HashMap<String, Object> 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));

View File

@ -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() {
}
}