package com.dsh.guns.modular.system.controller.code; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.dsh.course.feignClient.account.*; import com.dsh.course.feignClient.account.model.CityManager; import com.dsh.course.feignClient.account.model.TAppUser; import com.dsh.course.feignClient.account.model.VipPayment; import com.dsh.course.feignClient.activity.PointMercharsClient; import com.dsh.course.feignClient.competition.CompetitionClient; import com.dsh.course.feignClient.competition.model.Competition; import com.dsh.course.feignClient.competition.model.CompetitionQuery; import com.dsh.course.feignClient.competition.model.PaymentCompetition; import com.dsh.course.feignClient.course.*; import com.dsh.course.feignClient.course.model.TCoursePackage; import com.dsh.course.feignClient.course.model.TCoursePackagePayment; import com.dsh.course.feignClient.other.SiteClient; import com.dsh.course.feignClient.other.StoreClient; import com.dsh.course.model.MoneyBack; import com.dsh.guns.config.UserExt; import com.dsh.guns.core.base.controller.BaseController; import com.dsh.guns.modular.system.model.*; import com.dsh.guns.modular.system.service.ICityService; import groovyjarjarpicocli.CommandLine; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.SimpleDateFormat; import java.time.*; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.time.temporal.TemporalAdjusters; import java.util.*; import java.util.stream.Collectors; /** * 数据统计控制器 * */ @Controller @RequestMapping("/data") public class DataStatisticsController extends BaseController { private String PREFIX = "/system/dataStatistics/"; @Autowired private ICityService cityService; @Autowired private CityManagerClient cityManagerClient; @Autowired private AppUserClient appUserClient; @Autowired private VipPaymentClient vipPaymentClient; @Autowired private FinanceClient financeClient; @Autowired private CoursePackagePaymentClient coursePackagePaymentClient; @Autowired private CompetitionClient competitionClient; @Autowired private StoreClient storeClient; @Autowired private SiteClient siteClient; @Autowired private PointMercharsClient pointMercharsClient; @Autowired private RefundCoursePackageClient refundCoursePackageClient; @Resource private CourseStuddentClient courseStuddentClient; @Autowired private CoursePackageClient coursePackageClient; /** * 跳转到平台统计数据页面 */ @RequestMapping("/platform") public String index(Model model) { return PREFIX + "platformIncome.html"; } /** * 营收数据-平台 * */ @RequestMapping(value = "/getIncomeData") @ResponseBody public Object getIncomeData(Integer type) { // 充值记录查询query RechargeRecordsQuery rechargeRecordsQuery = new RechargeRecordsQuery(); // 报名课程查询query RegisterOrderQuery query = new RegisterOrderQuery(); // 赛事活动查询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 incomeVOS = new ArrayList<>(); // 会员支付记录 List 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 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 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 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 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); } // 积分商品 List amount = pointMercharsClient.getAmount(); for (PointMerchandiseIncomeVo pointMerchandiseIncomeVo : amount) { IncomeVO temp = new IncomeVO(); temp.setInsertTime(pointMerchandiseIncomeVo.getInsertTime()); if (pointMerchandiseIncomeVo.getCash()!=null){ temp.setAmount(pointMerchandiseIncomeVo.getCash()); incomeVOS.add(temp); } } // 最终返回的数据 Map> monthlyData = new HashMap<>(); // 创建一个 Map 用于存储每年的累计 amount Map yearToTotalAmountMap = new HashMap<>(); // 初始化一个 BigDecimal 用于累加 BigDecimal totalAmount1 = BigDecimal.ZERO; // 用于返回图表上的总营收 年度营收 月度 周度 日度 List objects = new ArrayList<>(); // 遍历 incomeVOS for (IncomeVO incomeVO1 : incomeVOS) { // 总营收 BigDecimal total = incomeVO1.getAmount(); // 累加到总金额 totalAmount1 = totalAmount1.add(total); Date insertTime = incomeVO1.getInsertTime(); // 假设有一个方法用于获取 insertTime BigDecimal amount1 = incomeVO1.getAmount(); // 假设有一个方法用于获取 amount Calendar calendar = Calendar.getInstance(); calendar.setTime(insertTime); int year = calendar.get(Calendar.YEAR); // 获取年份 // 更新年份对应的累计 amount if (yearToTotalAmountMap.containsKey(year)) { BigDecimal totalAmount = yearToTotalAmountMap.get(year); yearToTotalAmountMap.put(year, totalAmount.add(amount1)); } else { yearToTotalAmountMap.put(year, amount1); } } objects.add(totalAmount1); // 创建一个新的列表,按年份升序排列 List sortedYears = new ArrayList<>(yearToTotalAmountMap.keySet()); Collections.sort(sortedYears); // 创建一个新的 IncomeVO 列表,包含按年份累计的 amount List 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); } monthlyData.put("yearIncome",resultIncomeVOS); // 创建一个 Map 用于存储今年每个月的累计 amount Map monthToTotalAmountMap = new HashMap<>(); Calendar calendar = Calendar.getInstance(); int currentYear = calendar.get(Calendar.YEAR); List monthlyAmountList = new ArrayList<>(Collections.nCopies(12, BigDecimal.ZERO)); for (IncomeVO incomeVO1 : incomeVOS) { Calendar calendar1 = Calendar.getInstance(); calendar1.setTime(incomeVO1.getInsertTime()); int year = calendar1.get(Calendar.YEAR); int month = calendar1.get(Calendar.MONTH) + 1; // 月份从0开始,所以需要加1 if (year == currentYear) { BigDecimal totalAmount = monthlyAmountList.get(month - 1); totalAmount = totalAmount.add(incomeVO1.getAmount()); monthlyAmountList.set(month - 1, totalAmount); } } monthlyData.put("monthIncome", Collections.singletonList(monthlyAmountList)); // 获取当前月份 LocalDate currentDate = LocalDate.now(); int currentMonth = currentDate.getMonthValue(); // 初始化一个长度为当前月份的天数的列表,并将每一天的金额初始化为零 int daysInCurrentMonth = currentDate.lengthOfMonth(); List weekAmount = new ArrayList<>(daysInCurrentMonth); for (int i = 0; i < daysInCurrentMonth; i++) { weekAmount.add(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())); } } monthlyData.put("weekIncome", Collections.singletonList(weekAmount)); LocalDateTime currentDateTime = LocalDateTime.now(); List amountByDay = new ArrayList<>(Collections.nCopies(7, BigDecimal.ZERO)); for (IncomeVO incomeVO3 : incomeVOS) { Date insertTime = incomeVO3.getInsertTime(); // 将 Date 转换为 LocalDateTime LocalDateTime insertDateTime = LocalDateTime.ofInstant(insertTime.toInstant(), ZoneId.systemDefault()); // 计算日期与当前日期的差距 long daysDifference = ChronoUnit.DAYS.between(insertDateTime.toLocalDate(), currentDateTime.toLocalDate()); // 如果日期在七天内,累加金额 if (daysDifference >= 0 && daysDifference < 7) { int index = (int) (6 - daysDifference); // 计算对应的索引位置 BigDecimal amount3 = incomeVO3.getAmount(); BigDecimal updatedAmount = amountByDay.get(index).add(amount3); amountByDay.set(index, updatedAmount); } } monthlyData.put("dayIncome", Collections.singletonList(amountByDay)); // 创建一个 Map 用于存储今年本周的累计 amount Map 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 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 amounts = thisYearIncome.stream() .map(IncomeVO::getAmount) .collect(Collectors.toList()); //本年总收入 BigDecimal totalAmount = amounts.stream() .reduce(BigDecimal.ZERO, BigDecimal::add); // 用于展示总营收 本年度营收 本月营收 本周营收 本日营收 objects.add(totalAmount); List 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 amountMonth = thisMonthIncome.stream() .map(IncomeVO::getAmount) .collect(Collectors.toList()); // 使用 本月总收入 BigDecimal totalMonth = amountMonth.stream() .reduce(BigDecimal.ZERO, BigDecimal::add); objects.add(totalMonth); List 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 amountWeek = thisWeekIncome.stream() .map(IncomeVO::getAmount) .collect(Collectors.toList()); // 使用 本月总收入 BigDecimal totalWeek = amountWeek.stream() .reduce(BigDecimal.ZERO, BigDecimal::add); objects.add(totalWeek); List 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 amountToday = todayIncome.stream() .map(IncomeVO::getAmount) .collect(Collectors.toList()); // 使用 本月总收入 BigDecimal totalToday = amountToday.stream() .reduce(BigDecimal.ZERO, BigDecimal::add); objects.add(totalToday); // 最后put monthlyData.put("yearMount",objects); return monthlyData; } /** * 运营统计-平台-用户数据 * */ @RequestMapping(value = "/getUserData") @ResponseBody public Object getUserData() { return null; } /** * 退费-平台 * */ @RequestMapping(value = "/getRefundData") @ResponseBody public Object getRefundData(Integer type) { // 赛事活动查询query CompetitionQuery competitionQuery = new CompetitionQuery(); List incomeVOS = new ArrayList<>(); // 获取学员课包退费 List refund = refundCoursePackageClient.getRefund(); for (MoneyBack moneyBack : refund) { List coursePackagePayments = courseStuddentClient.queryByIds(moneyBack.getIds()); BigDecimal totalAmount = BigDecimal.ZERO; for (TCoursePackagePayment payment : coursePackagePayments){ //课包名称 TCoursePackage tCoursePackage = coursePackageClient.queryById(payment.getCoursePackageId()); payment.setName(tCoursePackage.getName()); payment.setId(Long.valueOf(tCoursePackage.getId())); BigDecimal cashPayment = payment.getCashPayment(); if (cashPayment==null){ cashPayment = BigDecimal.ZERO; } Integer totalClassHours = payment.getTotalClassHours(); BigDecimal result = new BigDecimal(0); if (totalClassHours!=0) { result = cashPayment.divide(BigDecimal.valueOf(totalClassHours), 2, RoundingMode.HALF_UP); } //单价 payment.setOnePrice(result); int has = payment.getTotalClassHours() - payment.getLaveClassHours(); //剩余课时 payment.setHasHours(has); BigDecimal onePrice = payment.getOnePrice(); Integer laveClassHours = payment.getLaveClassHours(); BigDecimal result1 = onePrice.multiply(BigDecimal.valueOf(laveClassHours)); //总价 payment.setRestPrice(result1); System.out.println("======="+result1); totalAmount = totalAmount.add(result1); IncomeVO temp = new IncomeVO(); temp.setInsertTime(payment.getInsertTime()); temp.setAmount(result1); incomeVOS.add(temp); } } // 获取赛事退费记录 List competitions = competitionClient.listAllPayment(competitionQuery); for (PaymentCompetition competition : competitions) { if (competition.getPayStatus() != 3) { 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); } return null; } /** * 跳转到运营商统计数据页面 */ @RequestMapping("/operator") public String operator(Model model) { Integer objectType = UserExt.getUser().getObjectType(); List cityManagers = cityManagerClient.listAll(); HashSet city = new HashSet<>(); List list = new ArrayList<>(); for (CityManager cityManager : cityManagers) { if (!city.contains(cityManager.getCity())){ city.add(cityManager.getCity()); list.add(cityManager); } } model.addAttribute("list",list); model.addAttribute("objectType",objectType); return PREFIX + "operatorIncome.html"; } /** * 跳转到门店统计数据页面 */ @RequestMapping("/store") public String store(Model model) { Integer objectType = UserExt.getUser().getObjectType(); List cityManagers = cityManagerClient.listAll(); HashSet city = new HashSet<>(); List list = new ArrayList<>(); for (CityManager cityManager : cityManagers) { if (!city.contains(cityManager.getCity())){ city.add(cityManager.getCity()); list.add(cityManager); } } model.addAttribute("list",list); model.addAttribute("objectType",objectType); return PREFIX + "storeIncome.html"; } }