yangys
2024-04-04 ed4a5236bab800094be4a8378f5098eebe3de6ac
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
package com.qianwen.smartman.modules.mdc.mapper;
 
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import java.sql.Timestamp;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.WeekFields;
import java.util.List;
import java.util.Random;
import javax.sql.DataSource;
import com.qianwen.smartman.common.constant.CalendarConstant;
import com.qianwen.core.tool.utils.DateUtil;
import com.qianwen.smartman.modules.cps.entity.Workstation;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
 
@Component
/* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/modules/mdc/mapper/TimeUsedAnalysisMapper.class */
public class TimeUsedAnalysisMapper {
    private final JdbcTemplate jdbcTemplate;
 
    public TimeUsedAnalysisMapper(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(((DynamicRoutingDataSource) dataSource).getDataSource("tdengine"));
    }
 
    public void save(List<Workstation> workstations) {
        dropTable(workstations);
        createTable(workstations);
        insert(workstations);
    }
 
    public void saveOutput(List<Workstation> workstations) {
        dropTable(workstations);
        createTable(workstations);
        insertOutput(workstations);
    }
 
    public void saveAlarm(List<Workstation> workstations) {
        dropTableAlarm(workstations);
        createTableAlarm(workstations);
        insertAlarm(workstations);
    }
 
    public void dropTable(List<Workstation> workstations) {
        workstations.forEach(w -> {
            String sql = "drop table if exists iot_data.aggregate_state_" + w.getId();
            this.jdbcTemplate.execute(sql);
        });
    }
 
    public void dropTableAlarm(List<Workstation> workstations) {
        workstations.forEach(w -> {
            String sql = "drop table if exists alarm_" + w.getId();
            this.jdbcTemplate.execute(sql);
        });
    }
 
    public void createTable(List<Workstation> workstations) {
        workstations.forEach(w -> {
            String createTable = "create table aggregate_state_" + w.getId() + " using super_aggregate_state tags(" + w.getId() + ")";
            this.jdbcTemplate.execute(createTable);
        });
    }
 
    public void processData(Long workstationId, String name, String value) {
        createProcess(workstationId, name);
        insertProcess(workstationId, name, value);
    }
 
    public void createProcess(Long workstationId, String name) {
        String createTable = "create table if not exists iot_data.process_param_" + workstationId + "_" + name + " using super_collect_data tags(" + workstationId + ",'" + name + "')";
        this.jdbcTemplate.execute(createTable);
    }
 
    public void insertProcess(Long workstationId, String name, String value) {
        String sql = "insert into process_param_" + workstationId + "_" + name + " values" + value;
        System.out.println(sql);
        this.jdbcTemplate.update(sql);
    }
 
    public void createTableAlarm(List<Workstation> workstations) {
        workstations.forEach(w -> {
            String createTable = "create table alarm_" + w.getId() + " using super_alarm tags(" + w.getId() + ")";
            this.jdbcTemplate.execute(createTable);
        });
    }
 
    public void insert(List<Workstation> workstations) {
        Random random = new Random();
        workstations.forEach(workstation -> {
            String calendarCode = workstation.getCalendarCode();
            String sql = "insert into aggregate_state_" + workstation.getId() + " values" + insert(random, calendarCode == null ? CalendarConstant.DEFAULT_CALENDAR : calendarCode);
            System.out.println(sql);
            this.jdbcTemplate.update(sql);
        });
    }
 
    public void insertOutput(List<Workstation> workstations) {
        Random random = new Random();
        workstations.forEach(workstation -> {
            String calendarCode = workstation.getCalendarCode();
            String sql = "insert into aggregate_output_" + workstation.getId() + " values" + insertOutput(random, calendarCode == null ? CalendarConstant.DEFAULT_CALENDAR : calendarCode);
            this.jdbcTemplate.update(sql);
        });
    }
 
    private String insertOutput(Random random, String calendarCode) {
        long time = System.currentTimeMillis();
        LocalDate now = LocalDate.now();
        StringBuilder sb = new StringBuilder(sqlOutput(time - 1, random.nextInt(1000), random.nextInt(6), now, calendarCode));
        for (int i = 0; i < 50; i++) {
            sb.append(",").append(sqlOutput(time + (i * 1000), random.nextInt(1000), random.nextInt(6), now, calendarCode));
        }
        return sb.toString();
    }
 
    private String sqlOutput(long time, int duration, int value, LocalDate now, String calendarCode) {
        return "('" + new Timestamp(time) + "','" + new Timestamp(time + 5000) + "','" + new Random().nextInt(6) + "','" + new Random().nextInt(6) + "','" + new Random().nextInt(6) + "','" + calendarCode + "','" + now.getYear() + "','" + now.getMonthValue() + "','" + now.get(WeekFields.of(DayOfWeek.MONDAY, 1).weekOfYear()) + "','" + DateUtil.format(now, "yyyyMMdd") + "','1','1','" + value + "','" + new Random().nextInt(6) + "',0)";
    }
 
    private String insert(Random random, String calendarCode) {
        long time = System.currentTimeMillis();
        LocalDate now = LocalDate.now();
        StringBuilder sb = new StringBuilder(sql(time - 5000, time, random.nextInt(5) + 1, now, calendarCode));
        for (int i = 1; i < 50; i++) {
            long endTime = time + (i * 500000);
            sb.append(",").append(sql(time, endTime, random.nextInt(5) + 1, now, calendarCode));
            time = endTime;
        }
        return sb.toString();
    }
 
    private String sql(long startTime, long endTime, int value, LocalDate now, String calendarCode) {
        int duration = Math.toIntExact((endTime - startTime) / 1000);
        int wcs = new Random().nextInt(3) + 1;
        int rps = new Random().nextInt(5) + 1;
        return "('" + new Timestamp(startTime) + "','" + new Timestamp(endTime) + "','" + duration + "','" + value + "','" + calendarCode + "','" + now.getYear() + "','" + now.getMonthValue() + "','" + now.get(WeekFields.of(DayOfWeek.MONDAY, 1).weekOfYear()) + "','" + DateUtil.format(now, "yyyyMMdd") + "','" + wcs + "','1','" + value + "','" + rps + "',0," + (rps == 2 ? 0 : 1) + ")";
    }
 
    private String sql(long time, int value, LocalDate now, String calendarCode) {
        return "('" + new Timestamp(time) + "'," + ((Object) null) + "," + ((Object) null) + ",'" + value + "','" + calendarCode + "','" + now.getYear() + "','" + now.getMonthValue() + "','" + now.get(WeekFields.of(DayOfWeek.MONDAY, 1).weekOfYear()) + "','" + DateUtil.formatDate(now) + "','1','1','0','0',0)";
    }
 
    public void insertAlarm(List<Workstation> workstations) {
        workstations.forEach(workstation -> {
            String calendarCode = workstation.getCalendarCode();
            String sql = "insert into alarm_" + workstation.getId() + " values" + insertAlarm(calendarCode == null ? CalendarConstant.DEFAULT_CALENDAR : calendarCode);
            this.jdbcTemplate.update(sql);
        });
    }
 
    private String insertAlarm(String calendarCode) {
        long time = System.currentTimeMillis();
        LocalDate now = LocalDate.now();
        StringBuilder sb = new StringBuilder(sqlAlarm(time - 1, now, calendarCode));
        for (int i = 0; i < 50; i++) {
            sb.append(",").append(sqlAlarm(time + i, now, calendarCode));
        }
        return sb.toString();
    }
 
    private String sqlAlarm(long time, LocalDate now, String calendarCode) {
        return "('" + new Timestamp(time) + "','" + calendarCode + "','" + now.getYear() + "','" + now.getMonthValue() + "','" + now.get(WeekFields.of(DayOfWeek.MONDAY, 1).weekOfYear()) + "','" + DateUtil.formatDate(now) + "','1','1','1710001','报警啦','1')";
    }
}