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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436package DAO;
import DTO.PHONGBAN;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import connectionSQL.ConnectionManager;
import java.sql.Statement;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class PhongBanDAO implements DAOInterface<PHONGBAN> {
public static PhongBanDAO getInstance() {
return new PhongBanDAO();
}
@Override
public ArrayList<PHONGBAN> getList() {
ArrayList<PHONGBAN> list = new ArrayList<>();
Connection con = ConnectionManager.getConnection();
try {
PreparedStatement pst = con.prepareStatement("SELECT * FROM PHONGBAN ORDER BY tenPhong");
ResultSet rs = pst.executeQuery();
while (rs.next()) {
PHONGBAN phongBan = new PHONGBAN();
phongBan.setMaPhong(rs.getString("maPhong"));
phongBan.setTenPhong(rs.getString("tenPhong"));
phongBan.setNgayThanhLap(rs.getDate("ngayThanhLap").toLocalDate().plusDays(2));
if (rs.getString("maTruongPhong") == null) {
phongBan.setMaTruongPhong("Chưa có");
} else {
phongBan.setMaTruongPhong(rs.getString("maTruongPhong"));
}
list.add(phongBan);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(con);
}
return list;
}
@Override
public int insert(PHONGBAN phongBan) {
int rowsAffected = 0;
Connection con = ConnectionManager.getConnection();
try {
PreparedStatement pst = con.prepareStatement("INSERT INTO PHONGBAN VALUES (?, ?, ?, ?)");
pst.setString(1, phongBan.getMaPhong());
pst.setString(2, phongBan.getTenPhong());
pst.setDate(3, java.sql.Date.valueOf(phongBan.getNgayThanhLap()));
if (phongBan.getMaTruongPhong().equals("Chưa có") || phongBan.getMaTruongPhong().equals("")) {
pst.setNull(4, java.sql.Types.VARCHAR);
} else {
pst.setString(4, phongBan.getMaTruongPhong());
}
rowsAffected = pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(con);
}
return rowsAffected;
}
@Override
public int update(PHONGBAN phongBan) {
int rowsAffected = 0;
Connection con = ConnectionManager.getConnection();
try {
PreparedStatement pst = con.prepareStatement("UPDATE PHONGBAN SET tenPhong = ?, ngayThanhLap = ?, maTruongPhong = ? WHERE maPhong = ?");
pst.setString(1, phongBan.getTenPhong());
pst.setDate(2, java.sql.Date.valueOf(phongBan.getNgayThanhLap()));
if (phongBan.getMaTruongPhong().equals("Chưa có") || phongBan.getMaTruongPhong().equals("")) {
pst.setNull(3, java.sql.Types.VARCHAR);
} else {
pst.setString(3, phongBan.getMaTruongPhong());
}
pst.setString(4, phongBan.getMaPhong());
rowsAffected = pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(con);
}
return rowsAffected;
}
@Override
public int del(String maPhong) {
int rowsAffected = 0;
Connection con = ConnectionManager.getConnection();
try {
PreparedStatement pst = con.prepareStatement("DELETE FROM PHONGBAN WHERE maPhong = ?");
pst.setString(1, maPhong);
rowsAffected = pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(con);
}
return rowsAffected;
}
public String getMaSoTuTen(String tenPhong) {
String maPhong = null;
Connection con = ConnectionManager.getConnection();
try {
PreparedStatement pst = con.prepareStatement("SELECT maPhong FROM PHONGBAN WHERE tenPhong = ?");
pst.setString(1, tenPhong);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
maPhong = rs.getString("maPhong");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(con);
}
return maPhong;
}
public String getTenTuMaSo(String maPhong) {
if (maPhong == null || maPhong.trim().isEmpty()) {
return "Không xác định"; // Hoặc một giá trị mặc định phù hợp
}
String tenPhong = null;
Connection con = ConnectionManager.getConnection();
try {
PreparedStatement pst = con.prepareStatement("SELECT tenPhong FROM PHONGBAN WHERE maPhong = ?");
pst.setString(1, maPhong);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
tenPhong = rs.getString("tenPhong");
} else {
tenPhong = "Không xác định"; // Trường hợp không tìm thấy phòng ban
}
} catch (SQLException e) {
e.printStackTrace();
tenPhong = "Lỗi khi truy vấn"; // Để biết lỗi truy vấn
} finally {
ConnectionManager.closeConnection(con);
}
return tenPhong;
}
public int getSoLuongNhanVien(String maPhongBan) {
int soLuong = 0;
Connection con = ConnectionManager.getConnection();
try {
PreparedStatement pst = con.prepareStatement("SELECT COUNT(maNhanVien) AS count FROM NHANVIEN WHERE NHANVIEN.trangThai=1 AND maPhong = ?");
pst.setString(1, maPhongBan);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
soLuong = rs.getInt("count");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(con);
}
return soLuong;
}
public double[] getAverageSalaryData(String maPhongBan) {
double[] data = new double[3];
Connection con = ConnectionManager.getConnection();
try {
PreparedStatement pst = con.prepareStatement("SELECT COUNT(maNhanVien), SUM(luongThuViec), SUM(luongCoBan) FROM NHANVIEN LEFT JOIN HOPDONGLAODONG ON NHANVIEN.maHopDong = HOPDONGLAODONG.maHopDong WHERE maPhong = ?");
pst.setString(1, maPhongBan);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
data[0] = rs.getInt(1);
data[1] = rs.getDouble(2);
data[2] = rs.getDouble(3);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(con);
}
return data;
}
public Object[][] getNhanVienCuaPhongBanData(String maPhong) {
Object[][] data = null;
Connection con = ConnectionManager.getConnection();
try {
PreparedStatement pst1 = con.prepareStatement("SELECT COUNT(maNhanVien) AS count FROM NHANVIEN WHERE trangThai=1 AND maPhong = ?");
pst1.setString(1, maPhong);
ResultSet rs1 = pst1.executeQuery();
if (rs1.next()) {
data = new Object[rs1.getInt("count")][];
}
PreparedStatement pst2 = con.prepareStatement("SELECT * FROM CONNGUOI JOIN NHANVIEN ON CONNGUOI.CMND = NHANVIEN.CMND JOIN CHUCVU ON CHUCVU.maChucVu = NHANVIEN.maChucVu WHERE NHANVIEN.trangThai=1 AND maPhong = ?");
pst2.setString(1, maPhong);
ResultSet rs2 = pst2.executeQuery();
int count = 0;
while (rs2.next()) {
String loaiHinh = "Chính thức";
if (rs2.getString("maHopDong") == null) {
loaiHinh = "Thử việc";
}
data[count] = new String[] {String.valueOf(count + 1), rs2.getString("maNhanVien") + " - " + rs2.getString("hoTen"), loaiHinh, rs2.getString("tenChucVu")};
count++;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(con);
}
return data;
}
public ArrayList<String> getDanhSachMaSo() {
ArrayList<String> data = new ArrayList<>();
Connection con = ConnectionManager.getConnection();
try {
PreparedStatement pst = con.prepareStatement("SELECT * FROM PHONGBAN");
ResultSet rs = pst.executeQuery();
while (rs.next()) {
data.add(rs.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(con);
}
return data;
}
public ArrayList<Object[]> getDuLieuChucVuThongKe(String maPhong) {
ArrayList<Object[]> list = new ArrayList<>();
Connection con = ConnectionManager.getConnection();
try {
PreparedStatement pst = con.prepareStatement("SELECT TOP 5 cv.tenChucVu, COUNT(nv.maNhanVien) AS sl FROM CHUCVU cv JOIN NHANVIEN nv ON nv.maChucVu = cv.maChucVu JOIN PHONGBAN pb ON nv.maPhong = pb.maPhong WHERE nv.trangThai=1 AND pb.maPhong = ? GROUP BY cv.tenChucVu ORDER BY sl DESC");
pst.setString(1, maPhong);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
list.add(new Object[] {rs.getString(1), rs.getInt(2)});
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(con);
}
return list;
}
public int[] getDuLieuGioiTinhThongKe(String maPhong) {
int[] data = new int[2];
Connection con = ConnectionManager.getConnection();
try {
PreparedStatement pst = con.prepareStatement("SELECT cn.gioiTinh, COUNT(nv.maNhanVien) FROM NHANVIEN nv JOIN CONNGUOI cn ON nv.CMND = cn.CMND JOIN PHONGBAN pb ON nv.maPhong = pb.maPhong WHERE nv.trangThai=1 AND pb.maPhong = ? GROUP BY cn.gioiTinh");
pst.setString(1, maPhong);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
String gioiTinh = rs.getString(1);
int soLuong = rs.getInt(2);
if (gioiTinh.equals("Nam")) {
data[0] = soLuong;
} else {
data[1] = soLuong;
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(con);
}
return data;
}
public int[] getDuLieuDoTuoiThongKe(String maPhong) {
int[] data = new int[4];
Connection con = ConnectionManager.getConnection();
try {
PreparedStatement pst = con.prepareStatement("SELECT COUNT(maNhanVien) FROM NHANVIEN JOIN CONNGUOI ON CONNGUOI.CMND = NHANVIEN.CMND WHERE DATEDIFF(YEAR,CONNGUOI.ngaySinh,GETDATE()) >= 16 AND DATEDIFF(YEAR,CONNGUOI.ngaySinh,GETDATE()) <= 25 AND NHANVIEN.trangThai=1 AND maPhong=?");
pst.setString(1, maPhong);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
data[0] = rs.getInt(1);
}
pst = con.prepareStatement("SELECT COUNT(maNhanVien) FROM NHANVIEN JOIN CONNGUOI ON CONNGUOI.CMND = NHANVIEN.CMND WHERE DATEDIFF(YEAR,CONNGUOI.ngaySinh,GETDATE()) > 25 AND DATEDIFF(YEAR,CONNGUOI.ngaySinh,GETDATE()) <= 40 AND NHANVIEN.trangThai=1 AND maPhong=?");
pst.setString(1, maPhong);
rs = pst.executeQuery();
if (rs.next()) {
data[1] = rs.getInt(1);
}
pst = con.prepareStatement("SELECT COUNT(maNhanVien) FROM NHANVIEN JOIN CONNGUOI ON CONNGUOI.CMND = NHANVIEN.CMND WHERE DATEDIFF(YEAR,CONNGUOI.ngaySinh,GETDATE()) > 40 AND DATEDIFF(YEAR,CONNGUOI.ngaySinh,GETDATE()) <= 55 AND NHANVIEN.trangThai=1 AND maPhong=?");
pst.setString(1, maPhong);
rs = pst.executeQuery();
if (rs.next()) {
data[2] = rs.getInt(1);
}
pst = con.prepareStatement("SELECT COUNT(maNhanVien) FROM NHANVIEN JOIN CONNGUOI ON CONNGUOI.CMND = NHANVIEN.CMND WHERE DATEDIFF(YEAR,CONNGUOI.ngaySinh,GETDATE()) > 55 AND DATEDIFF(YEAR,CONNGUOI.ngaySinh,GETDATE()) <= 65 AND NHANVIEN.trangThai=1 AND maPhong=?");
pst.setString(1, maPhong);
rs = pst.executeQuery();
if (rs.next()) {
data[3] = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(con);
}
return data;
}
public ArrayList<Object[]> getDanhSachTenVaSoLuongNhanVienPhongBan() {
ArrayList<Object[]> list = new ArrayList<>();
Connection con = ConnectionManager.getConnection();
try {
PreparedStatement pst = con.prepareStatement("SELECT TOP 5 pb.tenPhong, COUNT(nv.maNhanVien) AS sl FROM PHONGBAN pb JOIN NHANVIEN nv ON pb.maPhong = nv.maPhong WHERE nv.trangThai=1 GROUP BY pb.maPhong, pb.tenPhong ORDER BY sl DESC");
ResultSet rs = pst.executeQuery();
while (rs.next()) {
list.add(new Object[] {rs.getString(1), rs.getInt(2)});
}
if (list.size() == 5) {
list.add(new Object[] {"Các phòng khác", 2}); // Giả sử có 2 phòng khác
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(con);
}
return list;
}
public String[] getDanhSachPhongBan() {
String[] data = null;
Connection con = ConnectionManager.getConnection();
try {
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT COUNT(maPhong) AS count FROM PHONGBAN");
if (rs.next()) {
data = new String[rs.getInt("count")];
}
rs = st.executeQuery("SELECT * FROM PHONGBAN ORDER BY maPhong");
int count = 0;
while (rs.next()) {
data[count] = rs.getString(1);
count++;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(con);
}
return data;
}
public Object[][] getDataNhanVienCuaPhong(String maPhong) {
int sonv = getSoLuongNhanVien(maPhong);
Object[][] data = new Object[sonv][];
Connection con = ConnectionManager.getConnection();
try {
String sql = "SELECT * FROM CONNGUOI JOIN NHANVIEN ON CONNGUOI.CMND = NHANVIEN.CMND JOIN CHUCVU ON CHUCVU.maChucVu = NHANVIEN.maChucVu WHERE NHANVIEN.trangThai=1 AND maPhong = ?";
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, maPhong);
ResultSet rs = pst.executeQuery();
int index = 0;
while (rs.next()) {
data[index] = new Object[] {
index + 1 + "",
rs.getString("maNhanVien"),
rs.getString("hoTen"),
rs.getString("tenChucVu"),
};
index++;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(con);
}
return data;
}
public ArrayList<String> getDataNhanVienTablePhongBan(String maPhong, String maNV) {
ArrayList<String> data = new ArrayList<>();
Connection con = ConnectionManager.getConnection();
try {
PreparedStatement pst = con.prepareStatement("SELECT * FROM CONNGUOI JOIN NHANVIEN ON CONNGUOI.CMND = NHANVIEN.CMND JOIN CHUCVU ON CHUCVU.maChucVu = NHANVIEN.maChucVu WHERE NHANVIEN.trangThai=1 AND maPhong = ? AND NHANVIEN.maNhanVien = ?");
pst.setString(1, maPhong);
pst.setString(2, maNV);
ResultSet rs = pst.executeQuery();
while(rs.next()) {
data.add(rs.getString("maNhanVien"));
data.add(rs.getString("hoTen"));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate ngaySinh = rs.getDate("ngaySinh").toLocalDate();
data.add(ngaySinh.format(formatter));
data.add(rs.getString("diaChi"));
data.add(rs.getString("tenChucVu"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(con);
}
return data;
}
public ArrayList<String> getTenPhongBan() {
ArrayList<String> data = new ArrayList<>();
Connection con = ConnectionManager.getConnection();
try {
String sql = "SELECT tenPhong FROM PHONGBAN";
PreparedStatement pst = con.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while(rs.next()) {
data.add(rs.getString("tenPhong"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(con);
}
return data;
}
}