From fb9e286342938f63af535eb2a363f783a63213db Mon Sep 17 00:00:00 2001
From: xuhy <3313886187@qq.com>
Date: 星期三, 28 五月 2025 11:13:57 +0800
Subject: [PATCH] Merge branch 'dev' of http://120.76.84.145:10101/gitblit/r/java/mx_charging_pile into dev

---
 ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/util/EnergyRefreshService.java |  110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 110 insertions(+), 0 deletions(-)

diff --git a/ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/util/EnergyRefreshService.java b/ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/util/EnergyRefreshService.java
new file mode 100644
index 0000000..fa1937a
--- /dev/null
+++ b/ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/util/EnergyRefreshService.java
@@ -0,0 +1,110 @@
+package com.ruoyi.other.util;
+
+import com.alibaba.fastjson2.JSON;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.ruoyi.other.api.domain.TSystemConfiguration;
+import com.ruoyi.other.mapper.TSystemConfigurationMapper;
+import com.ruoyi.other.vo.ScreenStorageConfigVO;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.Value;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.time.LocalTime;
+import java.util.Random;
+
+@Service
+public class EnergyRefreshService {
+    private final Random random = new Random();
+    @Resource
+    private TSystemConfigurationMapper systemConfigurationMapper;
+
+    private final BigDecimal targetLow = new BigDecimal("85");
+
+    private final BigDecimal targetHigh = new BigDecimal("87");
+
+
+    private final int maxIncrement = 10;
+
+    private boolean isRunning = true; // 控制任务是否继续执行
+
+    // 定时任务方法
+    @Scheduled(cron = "0 */15 * * * ?")//15分钟执行一次
+    public synchronized void refreshValue() {
+        // 检查当前时间是否在允许的时间段内
+        LocalTime now = LocalTime.now();
+        boolean isInMorning = now.isAfter(LocalTime.of(10, 0)) && now.isBefore(LocalTime.of(12, 0));
+        boolean isInAfternoon = now.isAfter(LocalTime.of(15, 0)) && now.isBefore(LocalTime.of(21, 0));
+
+        if (!isInMorning && !isInAfternoon) {
+            return;
+        }
+
+
+        //获取当前值
+        BigDecimal currentValue = getCurrentValue();
+        // 检查是否已停止或达到目标范围
+        if (!isRunning || isWithinTargetRange(currentValue)) {
+            isRunning = false;
+            return;
+        }
+        
+
+        // 生成随机增量并更新值
+        int increment = random.nextInt(maxIncrement + 1);
+        currentValue = currentValue.add(BigDecimal.valueOf(increment))
+                                   .setScale(2, RoundingMode.HALF_UP);
+        //更新值
+        updateCurrentValue(currentValue);
+        System.out.printf("定时刷新:当前时间 %s,当前值:%.2f%n", now, currentValue);
+    }
+    
+    private boolean isWithinTargetRange(BigDecimal value) {
+        return value.compareTo(targetLow) >= 0 && value.compareTo(targetHigh) <= 0;
+    }
+
+    /**
+     * 获取当前值
+     */
+    public BigDecimal getCurrentValue() {
+        //获取系统建设日期和累计储能放电量
+        TSystemConfiguration sysConfig = systemConfigurationMapper.selectOne(new LambdaQueryWrapper<TSystemConfiguration>()
+                .eq(TSystemConfiguration::getType,3));
+        //解析
+        ScreenStorageConfigVO configVO = JSON.parseObject(sysConfig.getContent(), ScreenStorageConfigVO.class);
+        return configVO.getCurrentValue();
+    }
+
+    /**
+     * 更新当前值
+     */
+    private void updateCurrentValue(BigDecimal currentValue) {
+        //获取系统建设日期和累计储能放电量
+        TSystemConfiguration sysConfig = systemConfigurationMapper.selectOne(new LambdaQueryWrapper<TSystemConfiguration>()
+                .eq(TSystemConfiguration::getType,3));
+        //解析
+        ScreenStorageConfigVO configVO = JSON.parseObject(sysConfig.getContent(), ScreenStorageConfigVO.class);
+        //更新
+        configVO.setCurrentValue(currentValue);
+        String json = JSON.toJSONString(configVO);
+        sysConfig.setContent(json);
+        systemConfigurationMapper.updateById(sysConfig);
+    }
+
+
+
+    // 重置任务
+    @Scheduled(cron = "0 0 0 * * ?")  // 每天凌晨0点执行
+    public void reset() {
+        BigDecimal currentValue = BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);
+        updateCurrentValue(currentValue);
+        //更新
+        this.isRunning = true;
+    }
+
+
+}
\ No newline at end of file

--
Gitblit v1.7.1