WaveController.java
7.2 KB
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
package com.zhazhapan.util.visual.controller;
import com.zhazhapan.modules.constant.ValueConsts;
import com.zhazhapan.util.Checker;
import com.zhazhapan.util.Formatter;
import com.zhazhapan.util.dialog.Alerts;
import com.zhazhapan.util.visual.WeUtils;
import com.zhazhapan.util.visual.constant.LocalValueConsts;
import com.zhazhapan.util.visual.model.ConfigModel;
import com.zhazhapan.util.visual.model.ControllerModel;
import com.zhazhapan.util.visual.model.WaverModel;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.chart.AreaChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import java.sql.*;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
/**
* @author pantao
* @since 2018/4/19
*/
public class WaveController {
private final String INSERT = "插入";
private final String DELETE = "删除";
private final String UPDATE = "更新";
@FXML
public ComboBox<String> tableCombo;
@FXML
public DatePicker startDate;
@FXML
public DatePicker endDate;
@FXML
public AreaChart<String, Object> chart;
@FXML
public ComboBox<String> crudMethod;
@FXML
public TextField sqlNumber;
@FXML
public TextField sql;
@FXML
public DatePicker sqlDate;
public Connection connection = null;
public Statement statement = null;
@FXML
private void initialize() {
List<WaverModel> waves = ConfigModel.getWaver();
if (Checker.isNotEmpty(waves)) {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://" + ConfigModel.getHost() + "/" + ConfigModel.getDatabase() + "?" +
ConfigModel.getCondition();
connection = DriverManager.getConnection(url, ConfigModel.getUsername(), ConfigModel.getPassword());
statement = connection.createStatement();
} catch (Exception e) {
Platform.runLater(() -> Alerts.showError(LocalValueConsts.MAIN_TITLE, e.getMessage()));
}
waves.forEach(waverModel -> tableCombo.getItems().add(waverModel.getTableName()));
tableCombo.getSelectionModel().selectedItemProperty().addListener((o, ov, nv) -> getWaveDataToLineChart());
tableCombo.getSelectionModel().selectFirst();
crudMethod.getItems().addAll(INSERT, DELETE, UPDATE);
crudMethod.getSelectionModel().selectFirst();
crudMethod.getSelectionModel().selectedItemProperty().addListener((o, ov, nv) -> generateSql());
}
ControllerModel.setWaveController(this);
}
public void generateSql() {
WaverModel wave = ConfigModel.getWaver().get(tableCombo.getSelectionModel().getSelectedIndex());
LocalDate localDate = sqlDate.getValue();
String date;
if (Checker.isNull(localDate)) {
date = Formatter.dateToString(new Date());
} else {
date = Formatter.dateToString(Formatter.localDateToDate(localDate));
}
switch (crudMethod.getSelectionModel().getSelectedItem()) {
case INSERT:
sql.setText("insert into " + wave.getTableName() + "(" + wave.getDataField() + "," + wave
.getDateField() + ") values('" + sqlNumber.getText() + "','" + date + "')");
break;
case DELETE:
sql.setText("delete from " + wave.getTableName() + " where " + wave.getDateField() + "='" + date + "'");
break;
case UPDATE:
sql.setText("update " + wave.getTableName() + " set " + wave.getDataField() + "='" + sqlNumber
.getText() + "' where " + wave.getDateField() + "='" + date + "'");
default:
break;
}
}
public void getWaveDataToLineChart() {
if (Checker.isNotNull(statement)) {
chart.getData().clear();
WaverModel wave = ConfigModel.getWaver().get(tableCombo.getSelectionModel().getSelectedIndex());
String sql = "select " + wave.getDataField() + "," + wave.getDateField() + " from " + wave.getTableName();
LocalDate startLocalDate = startDate.getValue();
LocalDate endLocalDate = endDate.getValue();
boolean startIsNull = Checker.isNull(startLocalDate);
boolean endIsNull = Checker.isNull(endLocalDate);
if (!startIsNull) {
String start = Formatter.dateToString(Formatter.localDateToDate(startDate.getValue()));
sql += " where " + wave.getDateField() + ">='" + start + "' and";
}
String and = "and";
if (!endIsNull) {
String end = Formatter.dateToString(Formatter.localDateToDate(endDate.getValue()));
if (sql.endsWith(and)) {
sql += " " + wave.getDateField() + "<='" + end + "'";
} else {
sql += " where " + wave.getDateField() + "<='" + end + "'";
}
}
if (sql.endsWith(and)) {
sql = sql.substring(0, sql.length() - 4);
}
sql += " order by " + wave.getDateField();
if (startIsNull && endIsNull) {
sql += " limit 0," + wave.getFirstResultSize();
}
XYChart.Series<String, Object> series = new XYChart.Series<>();
series.setName(wave.getTitle());
try {
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
String data = resultSet.getString(ValueConsts.ONE_INT);
String date = resultSet.getString(ValueConsts.TWO_INT);
Object value = data;
if (Checker.isDecimal(data)) {
if (data.contains(ValueConsts.DOT_SIGN)) {
int idx = data.lastIndexOf(ValueConsts.DOT_SIGN) + 3;
if (data.length() > idx) {
data = data.substring(0, idx);
}
value = WeUtils.stringToDouble(data);
} else {
value = WeUtils.stringToInt(data);
}
}
series.getData().add(new XYChart.Data<>(date.split(" ")[0], value));
}
} catch (SQLException e) {
System.out.println(sql);
Alerts.showError(LocalValueConsts.MAIN_TITLE, e.getMessage());
}
chart.getData().add(series);
chart.setTitle(wave.getTitle());
}
}
public void executeSql() {
if (Checker.isNotNull(statement)) {
try {
statement.executeUpdate(WeUtils.replaceVariable(sql.getText()));
Alerts.showInformation(LocalValueConsts.MAIN_TITLE, LocalValueConsts.OPERATION_SUCCESS);
getWaveDataToLineChart();
} catch (SQLException e) {
Alerts.showError(LocalValueConsts.MAIN_TITLE, e.getMessage());
}
}
}
}