From f7e51fc7c91e474e5c0bcc79c47f074c0a59b5c3 Mon Sep 17 00:00:00 2001
From: 44323 <443237572@qq.com>
Date: 星期三, 11 十月 2023 20:03:35 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 cloud-server-management/src/main/java/com/dsh/guns/modular/system/controller/code/DataStatisticsController.java |  392 ++++++++++++++++++++++++++-----------------------------
 1 files changed, 186 insertions(+), 206 deletions(-)

diff --git a/cloud-server-management/src/main/java/com/dsh/guns/modular/system/controller/code/DataStatisticsController.java b/cloud-server-management/src/main/java/com/dsh/guns/modular/system/controller/code/DataStatisticsController.java
index e23922a..7b8c8e3 100644
--- a/cloud-server-management/src/main/java/com/dsh/guns/modular/system/controller/code/DataStatisticsController.java
+++ b/cloud-server-management/src/main/java/com/dsh/guns/modular/system/controller/code/DataStatisticsController.java
@@ -2,11 +2,9 @@
 
 import cn.hutool.core.date.DateUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.dsh.course.feignClient.account.*;
-import com.dsh.course.feignClient.account.model.CityManager;
-import com.dsh.course.feignClient.account.model.QueryDataFee;
-import com.dsh.course.feignClient.account.model.TAppUser;
-import com.dsh.course.feignClient.account.model.VipPayment;
+import com.dsh.course.feignClient.account.model.*;
 import com.dsh.course.feignClient.activity.PointMercharsClient;
 import com.dsh.course.feignClient.competition.CompetitionClient;
 import com.dsh.course.feignClient.competition.model.Competition;
@@ -114,89 +112,119 @@
      */
     @RequestMapping(value = "/getIncomeData")
     @ResponseBody
-    public Object getIncomeData(Integer type) {
-        // 充值记录查询query
-        RechargeRecordsQuery rechargeRecordsQuery = new RechargeRecordsQuery();
+    public Object getIncomeData(Integer operatorId,Integer storeId) {
         // 报名课程查询query
         RegisterOrderQuery query = new RegisterOrderQuery();
+    // 充值记录查询query
+        RechargeRecordsQuery rechargeRecordsQuery = new RechargeRecordsQuery();
         // 赛事活动查询query
         CompetitionQuery competitionQuery = new CompetitionQuery();
+
         // 会员支付查询query
         IncomeQuery incomeQuery = new IncomeQuery();
         // 场地预约记录查询query
         SiteBookingQuery siteBookingQuery = new SiteBookingQuery();
-        Integer roleType = UserExt.getUser().getObjectType();
-        IncomeVO incomeVO = new IncomeVO();
-        List<IncomeVO> incomeVOS = new ArrayList<>();
-                // 会员支付记录
-                List<VipPayment> vipPayments = vipPaymentClient.registrationList(incomeQuery);
-                for (VipPayment vipPayment : vipPayments) {
-                    IncomeVO temp = new IncomeVO();
-                    if (vipPayment.getPayStatus()==1){
-                        temp.setInsertTime(vipPayment.getInsertTime());
-                        incomeVOS.add(temp);
-                    }
-                    if (vipPayment.getAmount() != null) {
-                        temp.setAmount(new BigDecimal(vipPayment.getAmount().toString()));
-                    }
-                }
-                // 充值记录
-                List<RechargeRecordsVO> rechargeRecordsVOS = financeClient.rechargeList(rechargeRecordsQuery);
-                for (RechargeRecordsVO rechargeRecordsVO : rechargeRecordsVOS) {
-                    IncomeVO temp = new IncomeVO();
-                    temp.setInsertTime(rechargeRecordsVO.getInsertTime());
-                    if (rechargeRecordsVO.getAmount() != null) {
-                        temp.setAmount(rechargeRecordsVO.getAmount());
-                    }
-                    if (rechargeRecordsVO.getPayStatus() ==1){
-                        incomeVOS.add(temp);
-                    }
+        // 筛选运营商的是时候使用
+        List<Integer> storeIds = new ArrayList<>();
+        // 根据运营商ID 查询运营商下的所有门店
+        if (storeId==null){
+            if (operatorId!=null){
+                List<TStore> stores = storeService.list(new QueryWrapper<TStore>().eq("operatorId", operatorId));
+                // 拿到该运营商下的门店id集合
+                storeIds = stores.stream().map(TStore::getId).collect(Collectors.toList());
+                query.setStoresIds(storeIds);
+                siteBookingQuery.setStoreIds(storeIds);
+                competitionQuery.setOperatorId(operatorId);
+            }
+        }else {
+            List<Integer> list = new ArrayList<>();
+            list.add(storeId);
+            query.setStoresIds(list);
+            siteBookingQuery.setStoreIds(list);
+            competitionQuery.setStoreId(storeId);
+        }
 
+
+        Integer roleType = UserExt.getUser().getObjectType();
+        rechargeRecordsQuery.setInsertType(roleType);
+                       query.setInsertType(roleType);
+            competitionQuery.setInsertType(roleType);
+                 incomeQuery.setInsertType(roleType);
+        List<IncomeVO> incomeVOS = new ArrayList<>();
+        // 会员支付记录
+        List<VipPayment> vipPayments = vipPaymentClient.registrationList(incomeQuery);
+        for (VipPayment vipPayment : vipPayments) {
+            IncomeVO temp = new IncomeVO();
+            if (vipPayment.getPayStatus()==1){
+                temp.setInsertTime(vipPayment.getInsertTime());
+                incomeVOS.add(temp);
+            }
+            if (vipPayment.getAmount() != null) {
+                temp.setAmount(new BigDecimal(vipPayment.getAmount().toString()));
+            }
+        }
+        // 充值记录
+        List<RechargeRecordsVO> rechargeRecordsVOS = financeClient.rechargeList(rechargeRecordsQuery);
+        for (RechargeRecordsVO rechargeRecordsVO : rechargeRecordsVOS) {
+            IncomeVO temp = new IncomeVO();
+            temp.setInsertTime(rechargeRecordsVO.getInsertTime());
+            if (rechargeRecordsVO.getAmount() != null) {
+                temp.setAmount(rechargeRecordsVO.getAmount());
+            }
+            if (rechargeRecordsVO.getPayStatus() ==1){
+                incomeVOS.add(temp);
+            }
+
+        }
+        // 报名课程记录
+        List<RegisterOrderVO> registerOrderVOS = coursePackagePaymentClient.listAllRegister(query);
+        for (RegisterOrderVO registerOrderVO : registerOrderVOS) {
+            if (registerOrderVO.getMoney() == null) {
+                continue;
+            }
+            IncomeVO temp = new IncomeVO();
+            temp.setInsertTime(registerOrderVO.getInsertTime());
+            if (registerOrderVO.getMoney() != null) {
+                temp.setAmount(registerOrderVO.getMoney());
+            }
+            if (registerOrderVO.getPayStatus()==2){
+                incomeVOS.add(temp);
+            }
+        }
+        // 报名活动
+        List<PaymentCompetition> competitions = competitionClient.listAllPayment(competitionQuery);
+        for (PaymentCompetition competition : competitions) {
+            if (competition.getPayStatus() != 2) {
+                continue;
+            }
+            if (competition.getPayType()==3 || competition.getPayType()==4){
+                continue;
+            }
+            IncomeVO temp = new IncomeVO();
+            temp.setInsertTime(competition.getInsertTime());
+            if (competition.getAmount() != null) {
+                temp.setAmount(new BigDecimal(competition.getAmount().toString()));
+            }
+            incomeVOS.add(temp);
+        }
+        // 预约场地
+        // 说明当前运营商下没有门店
+        if (operatorId!=null && storeIds.size()==0){
+        }else{
+            List<SiteBooking> siteBookings = siteClient.listAll(siteBookingQuery);
+            for (SiteBooking siteBooking : siteBookings) {
+                IncomeVO temp = new IncomeVO();
+                if(siteBooking.getStatus()!=3){
+                    continue;
                 }
-                // 报名课程记录
-                List<RegisterOrderVO> registerOrderVOS = coursePackagePaymentClient.listAllRegister(query);
-                for (RegisterOrderVO registerOrderVO : registerOrderVOS) {
-                    if (registerOrderVO.getMoney() == null) {
-                        continue;
-                    }
-                    IncomeVO temp = new IncomeVO();
-                    temp.setInsertTime(registerOrderVO.getInsertTime());
-                    if (registerOrderVO.getMoney() != null) {
-                        temp.setAmount(registerOrderVO.getMoney());
-                    }
-                    if (registerOrderVO.getPayStatus()==2){
-                        incomeVOS.add(temp);
-                    }
+                temp.setInsertTime(siteBooking.getPayTime());
+                if (siteBooking.getPayMoney() != null) {
+                    temp.setAmount(new BigDecimal(siteBooking.getPayMoney().toString()));
                 }
-                    // 报名活动
-                    List<PaymentCompetition> competitions = competitionClient.listAllPayment(competitionQuery);
-                    for (PaymentCompetition competition : competitions) {
-                        if (competition.getPayStatus() != 2) {
-                            continue;
-                        }
-                        if (competition.getPayType()==3 || competition.getPayType()==4){
-                            continue;
-                        }
-                        IncomeVO temp = new IncomeVO();
-                        temp.setInsertTime(competition.getInsertTime());
-                        if (competition.getAmount() != null) {
-                            temp.setAmount(new BigDecimal(competition.getAmount().toString()));
-                        }
-                        incomeVOS.add(temp);
-                    }
-                // 预约场地
-                List<SiteBooking> siteBookings = siteClient.listAll(siteBookingQuery);
-                for (SiteBooking siteBooking : siteBookings) {
-                    IncomeVO temp = new IncomeVO();
-                    if(siteBooking.getStatus()!=3){
-                        continue;
-                    }
-                    temp.setInsertTime(siteBooking.getPayTime());
-                    if (siteBooking.getPayMoney() != null) {
-                        temp.setAmount(new BigDecimal(siteBooking.getPayMoney().toString()));
-                    }
-                    incomeVOS.add(temp);
-                }
+                incomeVOS.add(temp);
+            }
+        }
+
         // 积分商品
         List<PointMerchandiseIncomeVo> amount = pointMercharsClient.getAmount();
         for (PointMerchandiseIncomeVo pointMerchandiseIncomeVo : amount) {
@@ -207,7 +235,6 @@
                 incomeVOS.add(temp);
             }
         }
-
         // 最终返回的数据
         Map<String, List<Object>> monthlyData = new HashMap<>();
         // 创建一个 Map 用于存储每年的累计 amount
@@ -223,8 +250,8 @@
             // 累加到总金额
             totalAmount1 = totalAmount1.add(total);
 
-            Date insertTime = incomeVO1.getInsertTime(); // 假设有一个方法用于获取 insertTime
-            BigDecimal amount1 = incomeVO1.getAmount(); // 假设有一个方法用于获取 amount
+            Date insertTime = incomeVO1.getInsertTime();
+            BigDecimal amount1 = incomeVO1.getAmount();
             Calendar calendar = Calendar.getInstance();
             calendar.setTime(insertTime);
             int year = calendar.get(Calendar.YEAR); // 获取年份
@@ -242,16 +269,25 @@
         Collections.sort(sortedYears);
         // 创建一个新的 IncomeVO 列表,包含按年份累计的 amount
         List<Object> resultIncomeVOS = new ArrayList<>();
-        // 循环遍历2023年到2028年的年份
-        for (int year = 2023; year <= 2028; year++) {
-            BigDecimal totalAmount = yearToTotalAmountMap.getOrDefault(year, BigDecimal.ZERO);
-            // 创建一个新的 IncomeVO 对象,并设置年份和累计金额
-            IncomeVO resultIncomeVO = new IncomeVO();
-            resultIncomeVO.setAmount(totalAmount);
-            resultIncomeVOS.add(resultIncomeVO);
+        // 当前年往前推十年
+        List<Integer> integers1 = new ArrayList<>();
+        int years = DateUtil.year(new Date());
+        for (int i = 0; i < 10; i++) {
+            integers1.add(years-i);
         }
-        monthlyData.put("yearIncome",resultIncomeVOS);
-
+        List<Object> collect = integers1.stream().sorted().collect(Collectors.toList());
+        List<Object> objects1 = new ArrayList<>();
+        for (Object o : collect) {
+            BigDecimal orDefault = yearToTotalAmountMap.getOrDefault(Integer.valueOf(o.toString()), BigDecimal.ZERO);
+            objects1.add(orDefault);
+        }
+        monthlyData.put("yearIncome",objects1);
+        BigDecimal yearIn = new BigDecimal("0.00");
+        for (Object o : objects1) {
+            yearIn = yearIn.add(new BigDecimal(o.toString()));
+        }
+        BigDecimal orDefault = yearToTotalAmountMap.getOrDefault(years, BigDecimal.ZERO);
+        objects.add(orDefault);
         // 创建一个 Map 用于存储今年每个月的累计 amount
         Map<Integer, BigDecimal> monthToTotalAmountMap = new HashMap<>();
         Calendar calendar = Calendar.getInstance();
@@ -270,28 +306,71 @@
             }
         }
         monthlyData.put("monthIncome", Collections.singletonList(monthlyAmountList));
-
+        //yearMount
+        BigDecimal monthI = new BigDecimal("0.00");
+        for (BigDecimal bigDecimal : monthlyAmountList) {
+            monthI = monthI.add(bigDecimal);
+        }
+        objects.add(monthI);
         // 获取当前月份
         LocalDate currentDate = LocalDate.now();
         int currentMonth = currentDate.getMonthValue();
         // 初始化一个长度为当前月份的天数的列表,并将每一天的金额初始化为零
         int daysInCurrentMonth = currentDate.lengthOfMonth();
-        List<BigDecimal> weekAmount = new ArrayList<>(daysInCurrentMonth);
-        for (int i = 0; i < daysInCurrentMonth; i++) {
-            weekAmount.add(BigDecimal.ZERO);
-        }
+        List<BigDecimal> weekAmount = new ArrayList<>();
+
+        // 获取最近四周
+        LocalDateTime minNow = LocalDateTime.now().with(LocalTime.MIN);
+        LocalDateTime maxNow = LocalDateTime.now().with(LocalTime.MAX);
+        // 周度数据
+        LocalDateTime week1Start = minNow.minusDays(6);
+        LocalDateTime week1End   = maxNow;
+
+        LocalDateTime week2Start = minNow.minusDays(13);
+        LocalDateTime week2End = minNow.minusDays(7);
+
+        LocalDateTime week3Start = minNow.minusDays(20);
+        LocalDateTime week3End = minNow.minusDays(14);
+
+        LocalDateTime week4Start = minNow.minusDays(27);
+        LocalDateTime week4End = minNow.minusDays(21);
+
+        // 计算每个周的amount总和
+        BigDecimal tAmount1 = BigDecimal.ZERO;
+        BigDecimal tAmount2 = BigDecimal.ZERO;
+        BigDecimal tAmount3 = BigDecimal.ZERO;
+        BigDecimal tAmount4 = BigDecimal.ZERO;
         for (IncomeVO incomeVO1 : incomeVOS) {
-            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-            String string = dateFormat.format(incomeVO1.getInsertTime());
-            LocalDate insertDate = LocalDate.parse(string, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
-            if (insertDate.getMonthValue() == currentMonth) {
-                int dayOfMonth = insertDate.getDayOfMonth();
-                BigDecimal currentAmount = weekAmount.get(dayOfMonth - 1); // 列表索引从0开始
-                weekAmount.set(dayOfMonth - 1, currentAmount.add(incomeVO1.getAmount()));
+            Date insertTime = incomeVO1.getInsertTime();
+            // 将Date转换为LocalDateTime
+            Instant instant = insertTime.toInstant();
+            ZoneId zoneId = ZoneId.systemDefault();
+            LocalDateTime dateTime = instant.atZone(zoneId).toLocalDateTime();
+            // 判断该数据属于哪个周
+            if (dateTime.isAfter(week1Start) && dateTime.isBefore(week1End)) {
+                BigDecimal amount1 = incomeVO1.getAmount();
+                tAmount1=tAmount1.add(amount1);
+            } else if (dateTime.isAfter(week2Start) && dateTime.isBefore(week2End)) {
+                BigDecimal amount1 = incomeVO1.getAmount();
+                tAmount2=tAmount2.add(amount1);
+            } else if (dateTime.isAfter(week3Start) && dateTime.isBefore(week3End)) {
+                BigDecimal amount1 = incomeVO1.getAmount();
+                tAmount4=tAmount3.add(amount1);
+            } else if (dateTime.isAfter(week4Start) && dateTime.isBefore(week4End)) {
+                BigDecimal amount1 = incomeVO1.getAmount();
+                tAmount4=tAmount4.add(amount1);
             }
         }
+        weekAmount.add(tAmount1);
+        weekAmount.add(tAmount2);
+        weekAmount.add(tAmount3);
+        weekAmount.add(tAmount4);
         monthlyData.put("weekIncome", Collections.singletonList(weekAmount));
-
+        BigDecimal weekIn = new BigDecimal("0.00");
+        for (BigDecimal bigDecimal : weekAmount) {
+            weekIn = weekIn.add(bigDecimal);
+        }
+        objects.add(weekIn);
         LocalDateTime currentDateTime = LocalDateTime.now();
         List<BigDecimal> amountByDay = new ArrayList<>(Collections.nCopies(7, BigDecimal.ZERO));
         for (IncomeVO incomeVO3 : incomeVOS) {
@@ -309,108 +388,11 @@
 
             }
         }
-
         monthlyData.put("dayIncome", Collections.singletonList(amountByDay));
-        // 创建一个 Map 用于存储今年本周的累计 amount
-        Map<Integer, BigDecimal> dayToTotalAmountMap = new HashMap<>();
-        BigDecimal yearAmount = BigDecimal.ZERO; // 初始化累加变量为0
-        BigDecimal monthAmount = BigDecimal.ZERO; // 初始化累加变量为0
-        BigDecimal todayAmount = BigDecimal.ZERO; // 初始化累加变量为0
-        // 获取当前日期和时间
-        LocalDateTime now = LocalDateTime.now();
-        // 今年的起始日期和结束日期
-        LocalDateTime startOfYear = now.with(TemporalAdjusters.firstDayOfYear()).with(LocalTime.MIN);
-        LocalDateTime endOfYear = now.with(TemporalAdjusters.lastDayOfYear()).with(LocalTime.MAX);
-        // 本月的起始日期和结束日期
-        LocalDateTime startOfMonth = now.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
-        LocalDateTime endOfMonth = now.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX);
-        // 本周的起始日期和结束日期
-        LocalDateTime startOfWeek = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).with(LocalTime.MIN);
-        LocalDateTime endOfWeek = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).with(LocalTime.MAX);
-        // 今天的起始日期和结束日期
-        LocalDateTime startOfDay = now.with(LocalTime.MIN);
-        LocalDateTime endOfDay = now.with(LocalTime.MAX);
-        // 创建日期格式化器
-        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-        // 筛选符合条件的IncomeVO对象
-        List<IncomeVO> thisYearIncome = incomeVOS.stream()
-                .filter(income -> {
-                    Date insertTime = income.getInsertTime();
-                    try {
-                        LocalDateTime insertDateTime = LocalDateTime.ofInstant(insertTime.toInstant(), ZoneId.systemDefault());
-                        return insertDateTime.isAfter(startOfYear) && insertDateTime.isBefore(endOfYear);
-                    } catch (Exception e) {
-                        e.printStackTrace();
-                        return false;
-                    }
-                })
-                .collect(Collectors.toList());
-        // 使用 map 方法将 IncomeVO 转换为 BigDecimal,提取出 amount 字段
-        List<BigDecimal> amounts = thisYearIncome.stream()
-                .map(IncomeVO::getAmount)
-                .collect(Collectors.toList());
-        //本年总收入
-        BigDecimal totalAmount = amounts.stream()
-                .reduce(BigDecimal.ZERO, BigDecimal::add);
-        // 用于展示总营收 本年度营收 本月营收 本周营收 本日营收
-        objects.add(totalAmount);
-        List<IncomeVO> thisMonthIncome = incomeVOS.stream()
-                .filter(income -> {
-                    Date insertTime = income.getInsertTime();
-                    try {
-                        LocalDateTime insertDateTime = LocalDateTime.ofInstant(insertTime.toInstant(), ZoneId.systemDefault());
-                        return insertDateTime.isAfter(startOfMonth) && insertDateTime.isBefore(endOfMonth);
-                    } catch (Exception e) {
-                        e.printStackTrace();
-                        return false;
-                    }
-                })
-                .collect(Collectors.toList());
-        List<BigDecimal> amountMonth = thisMonthIncome.stream()
-                .map(IncomeVO::getAmount)
-                .collect(Collectors.toList());
-        // 使用 本月总收入
-        BigDecimal totalMonth = amountMonth.stream()
-                .reduce(BigDecimal.ZERO, BigDecimal::add);
-
-        objects.add(totalMonth);
-        List<IncomeVO> thisWeekIncome = incomeVOS.stream()
-                .filter(income -> {
-                    Date insertTime = income.getInsertTime();
-                    try {
-                        LocalDateTime insertDateTime = LocalDateTime.ofInstant(insertTime.toInstant(), ZoneId.systemDefault());
-                        return insertDateTime.isAfter(startOfWeek) && insertDateTime.isBefore(endOfWeek);
-                    } catch (Exception e) {
-                        e.printStackTrace();
-                        return false;
-                    }
-                })
-                .collect(Collectors.toList());
-        List<BigDecimal> amountWeek = thisWeekIncome.stream()
-                .map(IncomeVO::getAmount)
-                .collect(Collectors.toList());
-        // 使用 本月总收入
-        BigDecimal totalWeek = amountWeek.stream()
-                .reduce(BigDecimal.ZERO, BigDecimal::add);
-        objects.add(totalWeek);
-        List<IncomeVO> todayIncome = incomeVOS.stream()
-                .filter(income -> {
-                    Date insertTime = income.getInsertTime();
-                    try {
-                        LocalDateTime insertDateTime = LocalDateTime.ofInstant(insertTime.toInstant(), ZoneId.systemDefault());
-                        return insertDateTime.isAfter(startOfDay) && insertDateTime.isBefore(endOfDay);
-                    } catch (Exception e) {
-                        e.printStackTrace();
-                        return false;
-                    }
-                })
-                .collect(Collectors.toList());
-        List<BigDecimal> amountToday = todayIncome.stream()
-                .map(IncomeVO::getAmount)
-                .collect(Collectors.toList());
-        // 使用 本月总收入
-        BigDecimal totalToday = amountToday.stream()
-                .reduce(BigDecimal.ZERO, BigDecimal::add);
+        BigDecimal totalToday = new BigDecimal("0.00");
+        for (BigDecimal bigDecimal : amountByDay) {
+            totalToday = totalToday.add(bigDecimal);
+        }
         objects.add(totalToday);
         // 最后put
         monthlyData.put("yearMount",objects);
@@ -420,12 +402,10 @@
         for (int i = 0; i < 10; i++) {
             integers.add(year-i);
         }
-        List<Object> collect = integers.stream().sorted().collect(Collectors.toList());
-        monthlyData.put("year",collect);
+        List<Object> collect1 = integers.stream().sorted().collect(Collectors.toList());
+        monthlyData.put("year",collect1);
         return monthlyData;
     }
-
-
 
     /**
      * 运营统计-平台-用户数据

--
Gitblit v1.7.1