liujie
昨天 378669e94558e6d4d7383604f84dabd9d2242e7c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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.List;
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;
        }
 
        if(currentValue.doubleValue()==87){
            return;
        }
        if(currentValue.doubleValue()>87){
            updateCurrentValue(new BigDecimal("87"));
            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);
 
        refreshValueOne = new BigDecimal("0");
        TSystemConfiguration sysConfigs = systemConfigurationMapper.selectList(new LambdaQueryWrapper<TSystemConfiguration>()
                .eq(TSystemConfiguration::getType,4)).get(0);
        sysConfigs.setContent("0");
        systemConfigurationMapper.updateById(sysConfigs);
 
        //更新
        this.isRunning = true;
    }
 
 
 
    public static BigDecimal refreshValueOne = new BigDecimal("0");
    /**
     *光伏发电和消纳
     */
    @Scheduled(cron = "0 */1 * * * ?")  // 每分钟点执行
    public void refreshValueOne() {
        // 判断时间是否在6:00到8:59
        LocalTime now = LocalTime.now();
        if (now.isAfter(LocalTime.of(6, 0)) && now.isBefore(LocalTime.of(8, 59))) {
            refreshValueOne = refreshValueOne.add(new BigDecimal("0.5"));
            List<TSystemConfiguration> sysConfigs = systemConfigurationMapper.selectList(new LambdaQueryWrapper<TSystemConfiguration>()
                    .in(TSystemConfiguration::getType,4,5));
            TSystemConfiguration sysConfig = sysConfigs.stream().filter(e -> e.getType() == 4).findFirst().orElse(null);
            sysConfig.setContent(refreshValueOne.toString());
            systemConfigurationMapper.updateById(sysConfig);
 
 
            TSystemConfiguration sysConfig1 = sysConfigs.stream().filter(e -> e.getType() == 5).findFirst().orElse(null);
            String string = new BigDecimal(sysConfig1.getContent()).add(new BigDecimal("0.5")).toString();
            sysConfig1.setContent(string);
            systemConfigurationMapper.updateById(sysConfig1);
            // 在6:00到8:59之间,不执行
            return;
        }
        // 9:00-16:59每分钟增加随机3 到 3.5
        if (now.isAfter(LocalTime.of(9, 0)) && now.isBefore(LocalTime.of(16, 59))) {
            refreshValueOne = refreshValueOne.add(new BigDecimal(3 + (3.5 - 3) * random.nextDouble())
                    .setScale(2, RoundingMode.HALF_UP));
            List<TSystemConfiguration> sysConfigs = systemConfigurationMapper.selectList(new LambdaQueryWrapper<TSystemConfiguration>()
                    .in(TSystemConfiguration::getType,4,5));
 
            TSystemConfiguration sysConfig = sysConfigs.stream().filter(e -> e.getType() == 4).findFirst().orElse(null);
            sysConfig.setContent(refreshValueOne.toString());
            systemConfigurationMapper.updateById(sysConfig);
 
 
            TSystemConfiguration sysConfig1 = sysConfigs.stream().filter(e -> e.getType() == 5).findFirst().orElse(null);
            String string = new BigDecimal(sysConfig1.getContent()).add(new BigDecimal(3 + (3.5 - 3) * random.nextDouble())).toString();
            sysConfig1.setContent(string);
            systemConfigurationMapper.updateById(sysConfig1);
            return;
        }
        //17:00-18:59 每分钟增加0.5
        if (now.isAfter(LocalTime.of(17, 0)) && now.isBefore(LocalTime.of(18, 59))) {
            refreshValueOne = refreshValueOne.add(new BigDecimal("0.5"));
            List<TSystemConfiguration> sysConfigs = systemConfigurationMapper.selectList(new LambdaQueryWrapper<TSystemConfiguration>()
                    .in(TSystemConfiguration::getType,4,5));
 
            TSystemConfiguration sysConfig = sysConfigs.stream().filter(e -> e.getType() == 4).findFirst().orElse(null);
            sysConfig.setContent(refreshValueOne.toString());
            systemConfigurationMapper.updateById(sysConfig);
 
 
            TSystemConfiguration sysConfig1 = sysConfigs.stream().filter(e -> e.getType() == 5).findFirst().orElse(null);
            String string = new BigDecimal(sysConfig1.getContent()).add(new BigDecimal("0.5")).toString();
            sysConfig1.setContent(string);
            systemConfigurationMapper.updateById(sysConfig1);
        }
    }
 
 
 
 
}