fg电池监控平台的达梦数据库版本
whycxzp
2024-11-12 92ba7a968c67e8c8a64efea1759929649189f390
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.whyc.dto.Response;
import com.whyc.dto.StaticEleprice;
import com.whyc.mapper.BattInfMapper;
import com.whyc.mapper.PeakStatisticsMapper;
import com.whyc.pojo.Battinf;
import com.whyc.pojo.PeakStatistics;
import com.whyc.util.ActionUtil;
import com.whyc.util.DateUtil;
import com.whyc.util.ThreadLocalUtil;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
@Service
public class PeakStatisticsService {
 
    @Resource
    private PeakStatisticsMapper mapper;
    @Resource
    private BattInfMapper battInfMapper;
 
    public List<PeakStatistics> searchByCondition(PeakStatistics peakStatistics){
        QueryWrapper<PeakStatistics> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("battGroupId",peakStatistics.getBattGroupId());
        queryWrapper.ge("record_time",peakStatistics.getRecordTime());
        queryWrapper.le("record_time",peakStatistics.getRecordTime1());
        queryWrapper.orderByAsc("record_time","num");
        List<PeakStatistics> list = mapper.selectList(queryWrapper);
        return list;
    }
 
 
    public Response search(PeakStatistics ps){
        List<PeakStatistics> list = searchByCondition(ps);
        //保存电价系数
        savePriceFactor(ps.getBattGroupId(),ps.getPriceFactor());
        //最终结果集
        List<StaticEleprice> listAll=new ArrayList();
        //上一次的电量信息统计
        StaticEleprice se=new StaticEleprice();
        //统计编号
        int numStatic=1;//编号
        int numcharge=0;//充电数
        int numdischarge=0;//放电数
        float elePrice=0f;//价格
        int monthOrday=0;
        // 获取统计方式(近30天按照日统计,近12个月按照月统计)
        int method = ps.getNum();
        SimpleDateFormat sdfwithOut = new SimpleDateFormat("yyyy-MM-dd");
        if(list!=null&&list.size()>0){
            for (int i=0;i<list.size();i++) {
                PeakStatistics p=list.get(i);
                // 时间段
                String perid_Time = "";
                // 年份,季度,月份
                Date dev_recordtime = p.getRecordTime();
                // 获取具体年份,月份和该月的总天数
                int year = dev_recordtime.getYear() + 1900;
                int month = dev_recordtime.getMonth() + 1;
                int day=dev_recordtime.getDate();
                if (method==1) {// 近30天按照日统计
                    perid_Time = sdfwithOut.format(dev_recordtime)+" 00:00:00";
                    perid_Time += " ";
                    perid_Time += sdfwithOut.format(dev_recordtime)+" 23:59:59";
                    monthOrday=day;
                }  else if (method==3) {// 近12个月按照月统计
                    perid_Time = ActionUtil.getFirstDayOfMonth(year, month - 1);
                    perid_Time += " ";
                    perid_Time += ActionUtil.getLastDayOfMonth(year, month - 1);
                    monthOrday=month;
                }
 
                if(i==0||se.getPerid_Time().equals(perid_Time)){
                    se.setPerid_Time(perid_Time);
                    se.setBattgroupid(p.getBattGroupId());
                    se.setNum(numStatic);
                    if(p.getTestType()==3){
                        numdischarge++;
                        elePrice=se.getPrice()+p.getSumPrice();
                    }else{
                        numcharge++;
                        elePrice=se.getPrice()-p.getSumPrice();
                    }
                    se.setMonthOrday(monthOrday);
                    se.setEletric_discharge(numdischarge);//放电次数
                    se.setEletric_charge(numcharge);
                    se.setPrice(elePrice);
                }else{
                    //当记录日期改变则时下一笔数据
                    listAll.add(se); //
                    se=new StaticEleprice();
                    numStatic++;
                    numdischarge=0;
                    numcharge=0;
                    //开始重新统计下一笔
                    se.setPerid_Time(perid_Time);
                    se.setBattgroupid(p.getBattGroupId());
                    if(p.getTestType()==3){
                        numdischarge++;
                        elePrice=se.getPrice()+p.getSumPrice();
                    }else{
                        numcharge++;
                        elePrice=se.getPrice()-p.getSumPrice();
                    }
                    se.setNum(numStatic);
                    se.setMonthOrday(monthOrday);
                    se.setEletric_discharge(numdischarge);//放电次数
                    se.setEletric_charge(numcharge);
                    se.setPrice(elePrice);
                }
                if(i==list.size()-1){
                    listAll.add(se); //最后一笔数据
                }
            }
        }
 
        return new Response().set(1,listAll,"统计成功");
 
    }
 
    public void savePriceFactor(int battGroupId,float priceFactor){
        QueryWrapper<Battinf> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("battGroupId",battGroupId);
        Battinf battinf = battInfMapper.selectOne(queryWrapper);
        UpdateWrapper<Battinf> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("price_factor",priceFactor);
        updateWrapper.eq("battGroupId",battGroupId);
        battInfMapper.update(battinf,updateWrapper);
    }
 
    public Map<Integer, List<PeakStatistics>> getTodayMapByBattGroupId(int battGroupId) {
        QueryWrapper<PeakStatistics> query = Wrappers.query();
        Date now = new Date();
        //String today = DateUtil.YYYY_MM_DD.format(now);
        String today = ThreadLocalUtil.format(now,3);
        query.eq("record_time",today).eq("BattGroupId",battGroupId).in("test_type",2,3);
        List<PeakStatistics> peakStatistics = mapper.selectList(query);
        Map<Integer, List<PeakStatistics>> collect = peakStatistics.stream().collect(Collectors.groupingBy(PeakStatistics::getTestType));
        if(!collect.containsKey(2)){
            collect.put(2,null);
        };
        if(!collect.containsKey(3)){
            collect.put(3,null);
        };
        return collect;
    }
}