huliguo
昨天 a432ff3c95923f9929236de9f7a9224e8517bb70
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
package com.ruoyi.other.service.impl;
 
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.BasePojo;
import com.ruoyi.order.api.feignClient.ChargingOrderClient;
import com.ruoyi.order.api.model.TChargingOrder;
import com.ruoyi.other.api.domain.TScreenContent;
import com.ruoyi.other.api.domain.TSystemConfiguration;
import com.ruoyi.other.mapper.TScreenContentMapper;
import com.ruoyi.other.mapper.TSystemConfigurationMapper;
import com.ruoyi.other.service.TScreenContentService;
import com.ruoyi.other.service.TSystemConfigurationService;
import com.ruoyi.other.vo.EmissionReductionVO;
import com.ruoyi.other.vo.PhotovoltaicPowerGenerationVO;
import com.ruoyi.other.vo.ScreenStorageConfigVO;
import com.ruoyi.other.vo.ScreenTopVO;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import javax.json.Json;
import javax.json.JsonObject;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huliguo
 * @since 2025/5/23
 */
@Service
public class TScreenContentServiceImpl extends ServiceImpl<TScreenContentMapper, TScreenContent> implements TScreenContentService {
 
    @Resource
    private ChargingOrderClient chargingOrderClient;
    @Resource
    private TSystemConfigurationMapper systemConfigurationMapper;
 
    @Override
    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);
            if (r.getCode()==200){
                charge=r.getData();
            }
        }
        //计算电量占比率
        if (charge.compareTo(BigDecimal.ZERO) != 0) {
            // 定义乘数和除数
            BigDecimal multiplier = new BigDecimal("0.1404"); // 0.1404
            BigDecimal divisor = new BigDecimal("1000");    // 1000
            // 计算:charge × 0.1404 ÷ 1000
            charge = charge
                    .multiply(multiplier)      // 乘以 0.1404
                    .divide(divisor, 2, RoundingMode.HALF_UP); // 除以 1000,保留6位小数,四舍五入
        }
        vo.setCharge(charge);
        return vo;
    }
 
    @Override
    public PhotovoltaicPowerGenerationVO photovoltaicPowerGeneration(List<Integer> siteIds) {
        //需调用接口
 
        //假数据
        PhotovoltaicPowerGenerationVO vo = new PhotovoltaicPowerGenerationVO();
        List<LocalDate> dates = new ArrayList<>();
        List<Integer> values = new ArrayList<>();
        LocalDate today = LocalDate.now();
        // 生成每天数据
        for (int i = 7; i >= 1; i--) {//升序
            LocalDate day = today.minusDays(i);
            //统计
            dates.add(day);
            int value = new Random().nextInt(151) + 100; // 100-250
            values.add(value);
        }
        vo.setDates(dates);
        vo.setValues(values);
        return vo;
    }
 
    @Override
    public ScreenTopVO top(List<Integer> siteIds) {
        ScreenTopVO vo = new ScreenTopVO();
 
        BigDecimal charge=new BigDecimal("0.00");
        BigDecimal carCharge=new BigDecimal("0.00");
        BigDecimal greenElectricity=new BigDecimal("0.00");
        if (siteIds!=null && !siteIds.isEmpty()){
            //获取充电量
            R<BigDecimal> r = chargingOrderClient.getSumDegreeBySiteIds(siteIds);
            if (r.getCode()==200){
                charge=r.getData();
            }
            //汽车放电量
            carCharge = this.baseMapper.getCarDisCharge(siteIds);
 
            //累计绿电消纳电量
            greenElectricity = this.baseMapper.getGreenElectricity(siteIds);
        }
        vo.setCarCharge(charge);
        vo.setCarDisCharge(carCharge);
        vo.setGreenElectricity(greenElectricity);
 
        //获取系统建设日期和累计储能放电量
        TSystemConfiguration sysConfig = systemConfigurationMapper.selectOne(new LambdaQueryWrapper<TSystemConfiguration>()
                .eq(TSystemConfiguration::getType,3));
        //解析
        ScreenStorageConfigVO configVO = JSON.parseObject(sysConfig.getContent(), ScreenStorageConfigVO.class);
        //计算储能充电量
        LocalDate systemCreateTime = configVO.getSystemCreateTime();
        LocalDate today = LocalDate.now();
        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);
        vo.setStorageCharge(totalCharge);
        //获取储能放电量
 
        // 判断是否等于今天
        if (configVO.getLastUpdated().equals(today)) {
            vo.setStorageDisCharge(configVO.getStorageDisCharge());
            return vo;
        }
        //判断离今天还有几天
        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.setStorageDisCharge(storageDisCharge);
        return vo;
    }
}