天一电厂UPS动环数据对接,SNMP协议
whycxzp
2024-02-29 ac476c867af05823e33c7746eb2ec37dd26abe7a
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
package com.whyc.service;
 
import com.whyc.mapper.CommonMapper;
import com.whyc.mapper.UpsPowerDataHistoryMapper;
import com.whyc.pojo.UpsPowerDataHistory;
import com.whyc.pojo.UpsPowerSignal;
import com.whyc.pojo.UpsPowerSimulate;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
 
@Service
public class UpsPowerDataHistoryService {
 
    @Resource
    private UpsPowerDataHistoryMapper mapper;
 
    @Resource
    private CommonMapper commonMapper;
 
    @Resource
    private UpsPowerSignalService signalService;
    @Resource
    private UpsPowerSimulateService simulateService;
 
    public void getAndSaveHistory(){
        List<UpsPowerSignal> signalList = signalService.getList();
        //测试量的列表
        List<UpsPowerSimulate> simulateList = simulateService.getList();
 
        //按年份_月份
        Calendar nowCld = Calendar.getInstance();
        int year = nowCld.get(Calendar.YEAR);
        int month = nowCld.get(Calendar.MONTH);
        String monthStr = String.valueOf(month+1);
        if(monthStr.length()==1){
            monthStr = "0"+monthStr;
        }
 
        for (int i = 0; i < signalList.size(); i++) {
            UpsPowerSignal signal = signalList.get(i);
            UpsPowerSimulate simulate = simulateList.get(i);
 
            UpsPowerDataHistory dataHistory = new UpsPowerDataHistory();
 
            Integer powerDeviceId = signal.getPowerDeviceId();
            //tb_upspwrdev_historydata_116000001_2023_11
            String  tableName = "tb_upspwrdev_historydata"+"_"+powerDeviceId+"_"+year+"_"+monthStr;
            String tableNameExist = commonMapper.existTable("db_upspwrdev_data_history", tableName);
            if(tableNameExist ==null){ //->不存在,创建表
                createTable4UpsDataHistory(tableName);
            }
            //signal属性赋值到dataHistory
            BeanUtils.copyProperties(signal,dataHistory);
            //simulate属性赋值到dataHistory
            BeanUtils.copyProperties(simulate,dataHistory);
 
            //->数据插入
            add(tableName,dataHistory);
 
        }
 
    }
 
    public void createTable4UpsDataHistory(String tableName){
        mapper.createTable4UpsDataHistory(tableName);
    }
 
 
    public void add(String tableName, UpsPowerDataHistory dataHistory) {
        mapper.insert(tableName,dataHistory);
    }
 
    public void deleteTableBeyond3Month() throws ParseException {
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.MONTH,-2);
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH) + 1;
        Date monthLine = new SimpleDateFormat("yyyy-MM").parse(year + "-" + month);
        //查询表名
        List<String> tableListLike = commonMapper.getTableListLike("db_upspwrdev_data_history", "tb_upspwrdev_historydata_");
        //需要删除的表名
        List<String> tableNameListToBeDeleted = new LinkedList<>();
        for (String tableNameInDB : tableListLike) {
            String[] split = tableNameInDB.split("_");
            String monthStr = split[4] + "-" + split[5];
            Date monthInDB = new SimpleDateFormat("yyyy-MM").parse(monthStr);
            if(monthInDB.compareTo(monthLine)<0){ //3个月前的
                tableNameListToBeDeleted.add(tableNameInDB);
            }
        }
        //批量删除表
        if(tableNameListToBeDeleted.size()>0) {
            mapper.deleteTableList(tableNameListToBeDeleted);
        }
    }
 
    public static void main(String[] args) throws ParseException {
 
    }
}