From 35008a03cbee378b5905e325957a926499ca540b Mon Sep 17 00:00:00 2001
From: Cui Zhi Feng <7426394+wuxixiaocui@user.noreply.gitee.com>
Date: 星期五, 25 十月 2024 17:08:11 +0800
Subject: [PATCH] 统计 花店用户统计

---
 src/main/java/com/mzl/flower/service/statistics/StatisticsService.java |  312 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 301 insertions(+), 11 deletions(-)

diff --git a/src/main/java/com/mzl/flower/service/statistics/StatisticsService.java b/src/main/java/com/mzl/flower/service/statistics/StatisticsService.java
index e70d68e..9b2aecc 100644
--- a/src/main/java/com/mzl/flower/service/statistics/StatisticsService.java
+++ b/src/main/java/com/mzl/flower/service/statistics/StatisticsService.java
@@ -1,28 +1,39 @@
 package com.mzl.flower.service.statistics;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.mzl.flower.config.exception.ValidationException;
+import com.mzl.flower.config.security.SecurityUtils;
 import com.mzl.flower.constant.Constants;
 import com.mzl.flower.dto.request.flower.FlowerQueryDTO;
 import com.mzl.flower.dto.request.payment.OrderItemSalesQueryDTO;
 import com.mzl.flower.dto.request.payment.OrderQueryDTO;
-import com.mzl.flower.dto.response.flower.FlowerListDTO;
 import com.mzl.flower.dto.response.statistics.FlowerStatisticsDTO;
+import com.mzl.flower.dto.response.statistics.OrderStatisticsDTO;
+import com.mzl.flower.dto.response.statistics.RateStatisticsDTO;
 import com.mzl.flower.dto.response.statistics.SaleStatisticsDTO;
+import com.mzl.flower.entity.customer.Customer;
+import com.mzl.flower.entity.ip.UserAccess;
 import com.mzl.flower.entity.payment.Order;
 import com.mzl.flower.entity.supplier.Supplier;
+import com.mzl.flower.mapper.customer.CustomerMapper;
 import com.mzl.flower.mapper.flower.FlowerMapper;
+import com.mzl.flower.mapper.ip.UserAccessMapper;
 import com.mzl.flower.mapper.payment.*;
 import com.mzl.flower.mapper.supplier.SupplierMapper;
 import com.mzl.flower.service.BaseService;
+import com.mzl.flower.utils.IpUtil;
 import io.micrometer.core.instrument.util.StringUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import javax.servlet.http.HttpServletRequest;
+import java.io.IOException;
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.util.ArrayList;
@@ -48,6 +59,12 @@
     @Autowired
     private OrderItemSalesMapper orderItemSalesMapper;
 
+    @Autowired
+    private CustomerMapper customerMapper;
+
+    @Autowired
+    private UserAccessMapper userAccessMapper;
+
     public SaleStatisticsDTO getSaleStatistics(String date){
         if(StringUtils.isEmpty(date)){
             throw new ValidationException("日期不能为空");
@@ -61,11 +78,11 @@
         LocalDateTime begin = end.plusDays(-1);
 
         SaleStatisticsDTO dto = new SaleStatisticsDTO();
-        BigDecimal a = orderMapper.getOrderSaleAmount(begin, end);
-        Integer c = orderItemMapper.getFlowerSaleNum(begin, end);
+        BigDecimal a = orderMapper.getOrderSaleAmount(begin, end);//今日销售额:今日全部的实际支付金额合计
+        Integer c = orderItemMapper.getFlowerSaleNum(begin, end);//今日销售扎数:今日成功交易的订单的成交数量
 
         dto.setSaleAmount(a);
-        dto.setSaleFlowerCount(c);
+        dto.setSaleFlowerCount(c == null ? 0 : c.longValue());
 
         return dto;
     }
@@ -82,26 +99,31 @@
         statusList.add(Constants.FLOWER_STATUS.FORCE_OFF.name());
         q.setStatusList(statusList);
         flowerMapper.selectFlowerList(page, q);
-        dto.setFlowerCount((int)page.getTotal());
+        dto.setFlowerCount(page.getTotal());//商品管理:待审核+上架+下架(点击跳转到商品列表)
 
         Integer sc = supplierMapper.selectCount(new QueryWrapper<Supplier>()
                 .eq("is_enabled", 1)
                 .eq("status", "P")
                 .eq("deleted", 0));
-        dto.setSupplierCount(sc);
+        dto.setSupplierCount(sc.longValue());
+
+        Integer cc = customerMapper.selectCount(new QueryWrapper<Customer>()
+                .eq("is_enabled", 1)
+                .eq("deleted", 0));
+        dto.setCustomerCount(cc.longValue());//用户管理:统计商户列表已启用的全部用户(点击跳转到商户列表)
 
         Integer oc = orderMapper.selectCount(new QueryWrapper<Order>()
                 .eq("deleted", 0)
                 .isNotNull("payment_time")
                 .isNull("cancel_time")
                 .isNull("refund_time"));
-        dto.setOrderCount(oc);
+        dto.setOrderCount(oc.longValue());//订单管理:全部订单数量-待付款-已取消-已退款(点击跳转到订单列表)
 
         page = new Page(1, 1);
         OrderQueryDTO oq = new OrderQueryDTO();
         oq.setStatusBackend(Constants.ORDER_STATUS_BACKEND.SEND.name());
         orderMapper.selectOrderList(page, oq);
-        dto.setOrderSendCount((int)page.getTotal());
+        dto.setOrderSendCount(page.getTotal());//待发货:统计订单列表待发货的数量(点击跳转到订单列表-待发货页面)
 
         page = new Page(1, 1);
         q = new FlowerQueryDTO();
@@ -109,7 +131,7 @@
         statusList.add(Constants.FLOWER_STATUS.UP.name());
         q.setStatusList(statusList);
         flowerMapper.selectFlowerList(page, q);
-        dto.setFlowerUpCount((int)page.getTotal());
+        dto.setFlowerUpCount(page.getTotal());//在售商品:统计商品列表上架的商品数量(点击跳转到商品列表-上架页面)
 
         page = new Page(1, 1);
         q = new FlowerQueryDTO();
@@ -117,14 +139,282 @@
         statusList.add(Constants.FLOWER_STATUS.PENDING.name());
         q.setStatusList(statusList);
         flowerMapper.selectFlowerList(page, q);
-        dto.setFlowerPendingCount((int)page.getTotal());
+        dto.setFlowerPendingCount(page.getTotal());//待审核商品:跳转到商品列表-待审核页面
 
         page = new Page(1, 1);
         OrderItemSalesQueryDTO sq = new OrderItemSalesQueryDTO();
         sq.setStatus(Constants.ORDER_SALES_STATUS.PENDING.name());
+        sq.setStatusList(splitParam(sq.getStatus()));
         orderItemSalesMapper.selectItemSalesList(page, sq);
-        dto.setOrderSalesCount((int)page.getTotal());
+        dto.setOrderSalesCount(page.getTotal());//待售后处理:统计售后理赔-待审核的数量(跳转到售后理赔-待审核页面)
 
         return dto;
     }
+
+    public OrderStatisticsDTO getOrderStatistics(){
+        OrderStatisticsDTO dto = new OrderStatisticsDTO();
+        BigDecimal a = orderMapper.getOrderSaleAmount(null, null);//总销售金额:全部有效订单的实际支付金额合计
+        Integer c = orderItemMapper.getFlowerSaleNum(null, null);//总销售扎数:成功交易订单的成交数量
+        dto.setTotalSaleAmount(a);
+        dto.setTotalSaleFlowerCount(c == null ? 0 : c.longValue());
+
+        dto.setSupplierPendingAmount(new BigDecimal(0));//TODO 供应商待提现:结算列表待结算(供应商)+供应商钱包余额
+        dto.setSupplierCompleteAmount(new BigDecimal(0));//TODO 供应商已提现:结算列表已结算(供应商)+供应商钱包已提现金额
+
+        return dto;
+    }
+
+    public RateStatisticsDTO getOrderRateStatistics(String date){
+        if(StringUtils.isEmpty(date)){
+            throw new ValidationException("日期不能为空");
+        }
+        LocalDate localDate = parseLocalDate(date);
+        if(localDate == null){
+            throw new ValidationException("日期无效");
+        }
+
+        LocalDateTime end = localDate.atTime(17, 0, 0);
+        LocalDateTime begin = end.plusDays(-1);
+
+        LocalDateTime endY = begin;
+        LocalDateTime beginY = endY.plusDays(-1);
+
+        RateStatisticsDTO dto = new RateStatisticsDTO();
+
+        //订单量:与订单管理一致(并计算新增量,计算日环比)
+        Integer oc = orderMapper.selectCount(new QueryWrapper<Order>()
+                .eq("deleted", 0)
+                .isNotNull("payment_time")
+                .isNull("cancel_time")
+                .isNull("refund_time"));
+        dto.setCount(oc.longValue());
+
+        Integer ocToday = orderMapper.selectCount(new QueryWrapper<Order>()
+                .eq("deleted", 0)
+                .isNotNull("payment_time")
+                .isNull("cancel_time")
+                .isNull("refund_time")
+                .gt("create_time", begin)
+                .le("create_time", end)
+        );
+        dto.setCountToday(ocToday.longValue());
+
+        Integer ocY = orderMapper.selectCount(new QueryWrapper<Order>()
+                .eq("deleted", 0)
+                .isNotNull("payment_time")
+                .isNull("cancel_time")
+                .isNull("refund_time")
+                .gt("create_time", beginY)
+                .le("create_time", endY)
+        );
+        dto.setCountRate(getRate(ocToday, ocY));
+
+        return dto;
+    }
+
+    public RateStatisticsDTO getSupplierRateStatistics(String date) {
+        if (StringUtils.isEmpty(date)) {
+            throw new ValidationException("日期不能为空");
+        }
+        LocalDate localDate = parseLocalDate(date);
+        if (localDate == null) {
+            throw new ValidationException("日期无效");
+        }
+
+        LocalDateTime end = localDate.atTime(23, 59, 59);
+        LocalDateTime begin = localDate.atTime(0, 0, 0);
+
+        LocalDateTime endY = end.plusDays(-1);
+        LocalDateTime beginY = begin.plusDays(-1);
+
+        RateStatisticsDTO dto = new RateStatisticsDTO();
+
+        //供应商数量:已启用的供应商数量(并计算新增量,计算日环比)
+        Integer sc = supplierMapper.selectCount(new QueryWrapper<Supplier>()
+                .eq("is_enabled", 1)
+                .eq("status", "P")
+                .eq("deleted", 0));
+        dto.setCount(sc.longValue());
+
+        Integer scT = supplierMapper.selectCount(new QueryWrapper<Supplier>()
+                .eq("is_enabled", 1)
+                .eq("status", "P")
+                .eq("deleted", 0)
+                .gt("create_time", begin)
+                .le("create_time", end)
+        );
+        dto.setCountToday(scT.longValue());
+
+        Integer scY = supplierMapper.selectCount(new QueryWrapper<Supplier>()
+                .eq("is_enabled", 1)
+                .eq("status", "P")
+                .eq("deleted", 0)
+                .gt("create_time", beginY)
+                .le("create_time", endY)
+        );
+        dto.setCountRate(getRate(scT, scY));
+
+        return dto;
+    }
+
+    public RateStatisticsDTO getCustomerRateStatistics(String date) {
+        if (StringUtils.isEmpty(date)) {
+            throw new ValidationException("日期不能为空");
+        }
+        LocalDate localDate = parseLocalDate(date);
+        if (localDate == null) {
+            throw new ValidationException("日期无效");
+        }
+
+        LocalDateTime end = localDate.atTime(23, 59, 59);
+        LocalDateTime begin = localDate.atTime(0, 0, 0);
+
+        LocalDateTime endY = end.plusDays(-1);
+        LocalDateTime beginY = begin.plusDays(-1);
+
+        RateStatisticsDTO dto = new RateStatisticsDTO();
+        //全部用户量:已启用的用户数量合计(并计算新增量,计算日环比)
+
+        Integer c = customerMapper.selectCount(new QueryWrapper<Customer>()
+                .eq("deleted", 0));
+        dto.setCount(c.longValue());
+
+        Integer cT = customerMapper.selectCount(new QueryWrapper<Customer>()
+                .eq("deleted", 0)
+                .gt("create_time", begin)
+                .le("create_time", end)
+        );
+        dto.setCountToday(cT.longValue());
+
+        Integer cY = customerMapper.selectCount(new QueryWrapper<Customer>()
+                .eq("deleted", 0)
+                .gt("create_time", beginY)
+                .le("create_time", endY)
+        );
+        dto.setCountRate(getRate(cT, cY));
+
+        return dto;
+    }
+
+    public RateStatisticsDTO getCustomerVisitRateStatistics(String date) {
+        if (StringUtils.isEmpty(date)) {
+            throw new ValidationException("日期不能为空");
+        }
+        LocalDate localDate = parseLocalDate(date);
+        if (localDate == null) {
+            throw new ValidationException("日期无效");
+        }
+
+        LocalDateTime end = localDate.atTime(23, 59, 59);
+        LocalDateTime begin = localDate.atTime(0, 0, 0);
+
+        LocalDateTime endY = end.plusDays(-1);
+        LocalDateTime beginY = begin.plusDays(-1);
+
+        RateStatisticsDTO dto = new RateStatisticsDTO();
+        //用户访问量:点击到交易大厅或者商品详情页面计算,同一个用户,每天只计算一次(包含游客)(并计算新增量,计算日环比)
+
+        Integer c = userAccessMapper.selectCount(new QueryWrapper<UserAccess>());
+        dto.setCount(c.longValue());
+
+        Integer cT = userAccessMapper.selectCount(new QueryWrapper<UserAccess>()
+                .gt("create_time", begin)
+                .le("create_time", end)
+        );
+        dto.setCountToday(cT.longValue());
+
+        Integer cY = userAccessMapper.selectCount(new QueryWrapper<UserAccess>()
+                .gt("create_time", beginY)
+                .le("create_time", endY)
+        );
+        dto.setCountRate(getRate(cT, cY));
+        return dto;
+    }
+
+    public RateStatisticsDTO getSalesRateStatistics(String date) {
+        if (StringUtils.isEmpty(date)) {
+            throw new ValidationException("日期不能为空");
+        }
+        LocalDate localDate = parseLocalDate(date);
+        if (localDate == null) {
+            throw new ValidationException("日期无效");
+        }
+
+        LocalDateTime end = localDate.atTime(23, 59, 59);
+        LocalDateTime begin = localDate.atTime(0, 0, 0);
+
+        LocalDateTime endY = end.plusDays(-1);
+        LocalDateTime beginY = begin.plusDays(-1);
+
+        RateStatisticsDTO dto = new RateStatisticsDTO();
+        //售后订单数量:统计待审核+已通过(并计算新增量,计算日环比)
+
+        List<String> statusList = new ArrayList<>();
+        statusList.add(Constants.ORDER_SALES_STATUS.PENDING.name());
+        statusList.add(Constants.ORDER_SALES_STATUS.AGREED.name());
+
+        Page page = new Page(1, 1);
+        OrderItemSalesQueryDTO sq = new OrderItemSalesQueryDTO();
+        sq.setStatusList(statusList);
+        orderItemSalesMapper.selectItemSalesList(page, sq);
+        dto.setCount(page.getTotal());
+
+        page = new Page(1, 1);
+        sq = new OrderItemSalesQueryDTO();
+        sq.setStatusList(statusList);
+        sq.setSalesStartDate(begin);
+        sq.setSalesStartDate(end);
+        orderItemSalesMapper.selectItemSalesList(page, sq);
+        dto.setCountToday(page.getTotal());
+
+        page = new Page(1, 1);
+        sq = new OrderItemSalesQueryDTO();
+        sq.setStatusList(statusList);
+        sq.setSalesStartDate(beginY);
+        sq.setSalesStartDate(endY);
+        orderItemSalesMapper.selectItemSalesList(page, sq);
+        dto.setCountRate(getRate(dto.getCountToday().intValue(), (int)page.getTotal()));
+
+        return dto;
+    }
+
+    private Double getRate(Integer today, Integer yesterday){
+        if(yesterday > 0){
+            Integer ty = today - yesterday;
+            BigDecimal ttyy = BigDecimal.valueOf(ty);
+            BigDecimal yy = BigDecimal.valueOf(yesterday);
+            BigDecimal rate = ttyy.divide(yy, 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
+            return rate.doubleValue();
+        } else if (today <= 0) {
+            return 0D;
+        }
+
+        return 100D;
+    }
+
+
+    public void addUserAccessRecord(HttpServletRequest request){
+        String userId = SecurityUtils.getUserId();
+        String ip = null;
+        try {
+            ip = IpUtil.getIpAddress(request);
+        } catch (IOException e) {
+            ip = "127.0.0.1";
+        }
+        String ipId = ip+":"+(StringUtils.isBlank(userId)?"0":userId);
+
+        LocalDate localDate = LocalDate.now();
+        LocalDateTime end = localDate.atTime(23, 59, 59);
+        LocalDateTime begin = localDate.atTime(0, 0, 0);
+        int count = userAccessMapper.selectCount(new LambdaQueryWrapper<UserAccess>()
+                .eq(UserAccess::getIpId, ipId)
+                .gt(UserAccess::getCreateTime, begin)
+                .le(UserAccess::getCreateTime, end));
+        if(count==0){
+            UserAccess record = new UserAccess();
+            record.setIpId(ipId);
+            record.setCreateTime(LocalDateTime.now());
+            userAccessMapper.insert(record);
+        }
+    }
 }

--
Gitblit v1.9.3