From 568eac31d05d075075a69285a2c77b2d3afec23e Mon Sep 17 00:00:00 2001 From: huliguo <2023611923@qq.com> Date: 星期四, 29 五月 2025 15:43:56 +0800 Subject: [PATCH] 优化 --- ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/service/impl/TScreenContentServiceImpl.java | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 50 insertions(+), 4 deletions(-) diff --git a/ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/service/impl/TScreenContentServiceImpl.java b/ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/service/impl/TScreenContentServiceImpl.java index 52dc72a..88032be 100644 --- a/ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/service/impl/TScreenContentServiceImpl.java +++ b/ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/service/impl/TScreenContentServiceImpl.java @@ -56,7 +56,7 @@ public EmissionReductionVO emissionReduction(List<Integer> siteIds) { //需调用接口 计算光伏减排 EmissionReductionVO vo = new EmissionReductionVO(); - //获取总电量 计算电量 + //获取总电量 计算累计充电二氧化碳减排量 BigDecimal charge=new BigDecimal("0.00"); if (siteIds!=null && !siteIds.isEmpty()){ R<BigDecimal> r = chargingOrderClient.getSumDegreeBySiteIds(siteIds); @@ -64,7 +64,7 @@ charge=r.getData(); } } - //计算电量占比率 + //计算累计充电二氧化碳减排量 if (charge.compareTo(BigDecimal.ZERO) != 0) { // 定义乘数和除数 BigDecimal multiplier = new BigDecimal("0.1404"); // 0.1404 @@ -75,7 +75,54 @@ .divide(divisor, 2, RoundingMode.HALF_UP); // 除以 1000,保留6位小数,四舍五入 } vo.setCharge(charge); + + //获取累计储能放电量 + TSystemConfiguration sysConfig = systemConfigurationMapper.selectOne(new LambdaQueryWrapper<TSystemConfiguration>() + .eq(TSystemConfiguration::getType,3)); + //解析 + ScreenStorageConfigVO configVO = JSON.parseObject(sysConfig.getContent(), ScreenStorageConfigVO.class); + LocalDate today = LocalDate.now(); + // 判断是否等于今天 + if (configVO.getLastUpdated().equals(today)) { + vo.setEnergyStorage(configVO.getStorageDisCharge()); + }else { + //判断离今天还有几天 + int count = (int) ChronoUnit.DAYS.between(configVO.getLastUpdated(), today) +1;//包括今天 + BigDecimal storageDisCharge = configVO.getStorageDisCharge(); + // 每天生成一个随机值(不超过100)并累加 + for (int i = 0; i < count; i++) { + int dailyCharge = ThreadLocalRandom.current().nextInt(0, 101); // 0-100的随机数 + storageDisCharge = storageDisCharge.add(new BigDecimal(dailyCharge)); + } + + // 更新回对象 + configVO.setStorageDisCharge(storageDisCharge); + configVO.setLastUpdated(today); + String json = JSON.toJSONString(configVO); + sysConfig.setContent(json); + + systemConfigurationMapper.updateById(sysConfig); + vo.setEnergyStorage(storageDisCharge); + } + + //总数: + BigDecimal total = vo.getPhotovoltaic().add(vo.getEnergyStorage()).add(vo.getCharge()); + vo.setTotal(total); + //计算比率 + vo.setPhotovoltaicRate(calculateRatio(vo.getPhotovoltaic(),vo.getTotal())); + vo.setEnergyStorageRate(calculateRatio(vo.getEnergyStorage(),vo.getTotal())); + vo.setChargeRate(calculateRatio(vo.getCharge(),vo.getTotal())); + return vo; + } + + //百分比计算 + public static BigDecimal calculateRatio(BigDecimal part, BigDecimal total) { + if (total.compareTo(BigDecimal.ZERO) == 0) { + throw new ArithmeticException("分母不能为零"); + } + + return part.divide(total, 2, RoundingMode.HALF_UP); } @Override @@ -134,8 +181,7 @@ int days = (int) ChronoUnit.DAYS.between(systemCreateTime, today) +1;//包括今天 BigDecimal dailyRate = new BigDecimal("100"); - BigDecimal totalCharge = dailyRate.multiply(BigDecimal.valueOf(days)) - .divide(BigDecimal.valueOf(10000), 2, RoundingMode.HALF_UP); + BigDecimal totalCharge = dailyRate.multiply(BigDecimal.valueOf(days)); vo.setStorageCharge(totalCharge); //获取储能放电量 -- Gitblit v1.7.1