📦 0372hoanghoccode / ko-co

📄 NhanVienDAO.java · 644 lines
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644package DAO;

import java.util.ArrayList;


// --- IMPORT DTO ----
import DTO.HOPDONGLAODONG;
import DTO.NHANVIEN;
import DTO.NHANVIENCHINHTHUC;
import DTO.NHANVIENTHUVIEC;
import DTO.TAIKHOAN;
import DTO.TRINHDO;
import DTO.CHUCVU;
import DTO.CMND;
import DTO.CONNGUOI;
import DTO.DIACHI;

// --- IMPORT SQL -----
import connectionSQL.ConnectionManager;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;



public class NhanVienDAO implements DAOInterface<NHANVIEN>{
	
	// cmnd, con nguoi, trinh do, chuc vu
	
	
	public static NhanVienDAO getInstance() {
		return new NhanVienDAO();
	}



	// ----------------- IMPLEMENT INTERFACE -----------------
	@Override
	public ArrayList<NHANVIEN> getList()  {
		Connection con = ConnectionManager.getConnection();
		ArrayList<NHANVIEN> list = new ArrayList<>();
		try {
			String sql = "select * from CONNGUOI "
					+ "join NHANVIEN nv on CONNGUOI.CMND = nv.CMND "
					+ "join CMND on CMND.soCMND = CONNGUOI.CMND "
					+ "join TRINHDO on nv.maTrinhDo = TRINHDO.maTrinhDo "
					+ "join CHUCVU on nv.maChucVu = CHUCVU.maChucVu "
					+ "join TAIKHOAN on TAIKHOAN.username = nv.maNhanVien "
					+ "left join HOPDONGLAODONG on HOPDONGLAODONG.maHopDong = nv.maHopDong "
					+ "where nv.trangThai = 1";
			
			PreparedStatement ps = con.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while(rs.next() ) {
				NHANVIEN nv = null;
				String str = rs.getString(5);
				String arr[] = str.split(",");
				String arr2[] = arr[0].split(" ");
				String temp ="";
				for(int i=1;i<arr2.length;i++) {
					temp+=arr2[i]+" ";
				}
				temp = temp.trim();
				
				if (rs.getString(15)==null) { // Dòng 15 truy vấn là ngày bắt đầu thử việc
					nv = new NHANVIENTHUVIEC();
					((NHANVIENTHUVIEC)nv).setNgayBatDauThuViec(rs.getDate("ngayBatDauThuViec").toLocalDate().plusDays(2));
					((NHANVIENTHUVIEC)nv).setNgayKetThucThuViec(rs.getDate("ngayKetThucThuViec").toLocalDate().plusDays(2));
					((NHANVIENTHUVIEC)nv).setLuongThuViec(rs.getDouble("luongThuViec"));
				}
				else {
					nv = new NHANVIENCHINHTHUC();
					((NHANVIENCHINHTHUC)nv).setHOPDONG(new HOPDONGLAODONG(rs.getString("maHopDong"), rs.getDate("tuNgay").toLocalDate().plusDays(2), rs.getDate("denNgay").toLocalDate().plusDays(2), rs.getString("loaiHopDong"), rs.getDouble("luongCoBan")));
				}
				
				nv.setMaNhanVien(rs.getString("maNhanVien"));
				nv.setHoTen(rs.getString("hoTen"));
				nv.setGioiTinh(rs.getString("gioiTinh"));
				nv.setNgaySinh(rs.getDate("ngaySinh").toLocalDate().plusDays(2));
				nv.setDiaChi(new DIACHI(arr2[0],temp, arr[1], arr[2], arr[3]));
				nv.setDanToc(rs.getString("danToc"));
				nv.setTonGiao(rs.getString("tonGiao"));
				nv.setEmail(rs.getString("email"));
				nv.setSdt(rs.getString("SDT"));
				nv.setCmnd(new CMND(rs.getString("soCMND"),rs.getString("noiCap"),rs.getDate("ngayCap").toLocalDate().plusDays(2)));
				
				nv.setTaiKhoan(new TAIKHOAN(rs.getString("username"), rs.getString("pass"), rs.getString("maNhomQuyen"), rs.getString("avatar")));
				nv.setChucVu(new CHUCVU(rs.getString("maChucVu"), rs.getString("tenChucVu"), rs.getDouble("phuCapChucVu"), rs.getDate("ngayNhanChuc").toLocalDate().plusDays(2)));
				nv.setMaPhong(rs.getString("maPhong"));
				nv.setTrinhDo(new TRINHDO(rs.getString("maTrinhDo"), rs.getString("trinhDoHocVan"), rs.getString("trinhDoChuyenMon"), rs.getString("chuyenNganh")));
				list.add(nv);
				
			}
			ConnectionManager.closeConnection(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public int insert(NHANVIEN x) {
		// TODO Auto-generated method stub
		int result = 0;
		Connection con = ConnectionManager.getConnection();
		try {
			// tạo cmnd -> đang soi
			CMND_DAO.getInstance().insert(x.getCmnd());
			// con người -> đang soi
			ConNguoiDAO.getInstance().insert((CONNGUOI)x);
			// trinh do -> đang soi
			TrinhDoDAO.getInstance().insert(x.getTrinhDo());
			// chuc vu -> đang soi
			ChucVuDAO.getInstance().insert(x.getChucVu());
			// nhanvien
			PreparedStatement pst;
			if(x instanceof NHANVIENCHINHTHUC) {

				// hop dong lao dong -> đang soi
				HopDongLaoDongDAO.getInstance().insert(((NHANVIENCHINHTHUC)x).getHopDong());
				// nhan vien
				pst = con.prepareStatement("insert NHANVIEN values(?,?,?,?,?,?,?,?,?,?)");
				pst.setString(1, x.getMaNhanVien());
				pst.setString(2, x.getCmnd().getSoCmnd());
				pst.setString(3, x.getMaPhong());
				pst.setString(4, x.getTrinhDo().getMaTrinhDo());
				pst.setString(5, x.getChucVu().getMaChucVu());
				pst.setString(6, ((NHANVIENCHINHTHUC)x).getHopDong().getMaHopDong());
				pst.setDate(7,null);
				pst.setDate(8,null);
				pst.setDouble(9,0);
				pst.setInt(10, 1);
				pst.executeUpdate();
			}else {
				// nhan vien
				pst = con.prepareStatement("insert NHANVIEN values(?,?,?,?,?,?,?,?,?,?)");
				pst.setString(1, x.getMaNhanVien());
				pst.setString(2, x.getCmnd().getSoCmnd());
				pst.setString(3, x.getMaPhong());
				pst.setString(4, x.getTrinhDo().getMaTrinhDo());
				pst.setString(5, x.getChucVu().getMaChucVu());
				pst.setString(6, null);
				pst.setDate(7,Date.valueOf(((NHANVIENTHUVIEC)x).getNgayBatDauThuViec()));
				pst.setDate(8,Date.valueOf(((NHANVIENTHUVIEC)x).getNgayKetThucThuViec()));
				pst.setDouble(9,((NHANVIENTHUVIEC)x).getLuongThuViec());
				pst.setInt(10, 1);
				result = pst.executeUpdate();
			}


			// tài khoản -> đang soi 
			TaiKhoanDAO.getInstance().insert(x.getTaiKhoan());
			
			ConnectionManager.closeConnection(con);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public int update(NHANVIEN t) {
		// TODO Auto-generated method stub
		int result = 0;
		Connection con = ConnectionManager.getConnection();
		try {

			// SOI đoạn này lại
			CMND_DAO.getInstance().update(t.getCmnd());
			ConNguoiDAO.getInstance().update((CONNGUOI)t);
			TrinhDoDAO.getInstance().update(t.getTrinhDo());
			ChucVuDAO.getInstance().update(t.getChucVu());

			PreparedStatement pst;
			if(t instanceof NHANVIENCHINHTHUC) {
				HopDongLaoDongDAO.getInstance().update(((NHANVIENCHINHTHUC)t).getHopDong());
				pst = con.prepareStatement("update NHANVIEN set maPhong = ?, maTrinhDo = ?, maChucVu = ?, maHopDong = ?, ngayBatDauThuViec = null, ngayKetThucThuViec = null, luongThuViec = 0 where maNhanVien = ?");
				pst.setString(1, t.getMaPhong());
				pst.setString(2, t.getTrinhDo().getMaTrinhDo());
				pst.setString(3, t.getChucVu().getMaChucVu());
				pst.setString(4, ((NHANVIENCHINHTHUC)t).getHopDong().getMaHopDong());
				pst.setString(5, t.getMaNhanVien());
				pst.executeUpdate();
			}else {
				pst = con.prepareStatement("update NHANVIEN set maPhong = ?, maTrinhDo = ?, maChucVu = ?, maHopDong = null, ngayBatDauThuViec = ?, ngayKetThucThuViec = ?, luongThuViec = ? where maNhanVien = ?");
				pst.setString(1, t.getMaPhong());
				pst.setString(2, t.getTrinhDo().getMaTrinhDo());
				pst.setString(3, t.getChucVu().getMaChucVu());
				pst.setDate(4,Date.valueOf(((NHANVIENTHUVIEC)t).getNgayBatDauThuViec()));
				pst.setDate(5,Date.valueOf(((NHANVIENTHUVIEC)t).getNgayKetThucThuViec()));
				pst.setDouble(6,((NHANVIENTHUVIEC)t).getLuongThuViec());
				pst.setString(7, t.getMaNhanVien());
				result = pst.executeUpdate();
			
			}
			TaiKhoanDAO.getInstance().update(t.getTaiKhoan());
			ConnectionManager.closeConnection(con);
		}catch (SQLException e) {
			
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public int del(String ma) { // vẫn chưa hiểu được yêu cầu nha (MẤY NÍ NHỚ COI DÙM) --> kiểu hàm này là dc rồi hay là cho nó bay màu khỏi csdl luôn
		// TODO Auto-generated method stub
		int result = 0;
		Connection con = ConnectionManager.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement("update NHANVIEN set trangThai = 0 where maNhanVien = ?");
			pst.setString(1, ma);
			result = pst.executeUpdate();
			ConnectionManager.closeConnection(con);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return result;
	}


	// ----------------- OTHER FUNCTION ----------------- chưa test nha

	public NHANVIEN getNhanVien(String maNhanVien) {
		Connection con = ConnectionManager.getConnection();
		try {
			Statement ps = con.createStatement();
			ResultSet rs = ps.executeQuery("select * from CONNGUOI join NHANVIEN on CONNGUOI.CMND = NHANVIEN.CMND join CMND on CMND.soCMND = CONNGUOI.CMND join TRINHDO on NHANVIEN.maTrinhDo = TRINHDO.maTrinhDo join CHUCVU on NHANVIEN.maChucVu = CHUCVU.maChucVu join TAIKHOAN on TAIKHOAN.username = NHANVIEN.maNhanVien left join HOPDONGLAODONG on HOPDONGLAODONG.maHopDong = NHANVIEN.maHopDong where NHANVIEN.maNhanVien = N'"+maNhanVien+"'");
			while(rs.next()) {
				NHANVIEN x = null;
				String str = rs.getString(5);
				String arr[] = str.split(",");
				String arr2[] = arr[0].split(" ");
				String temp ="";
				for(int i=1;i<arr2.length;i++) {
					temp+=arr2[i]+" ";
				}
				temp = temp.trim();
				
				if(rs.getString("maHopDong")==null) {
					x = new NHANVIENTHUVIEC();
					((NHANVIENTHUVIEC)x).setNgayBatDauThuViec(rs.getDate("ngayBatDauThuViec").toLocalDate().plusDays(2));
					((NHANVIENTHUVIEC)x).setNgayKetThucThuViec(rs.getDate("ngayKetThucThuViec").toLocalDate().plusDays(2));
					((NHANVIENTHUVIEC)x).setLuongThuViec(rs.getDouble("luongThuViec"));
				}else {
					x = new NHANVIENCHINHTHUC();
					((NHANVIENCHINHTHUC)x).setHOPDONG(new HOPDONGLAODONG(rs.getString("maHopDong"), rs.getDate("tuNgay").toLocalDate().plusDays(2), rs.getDate("denNgay").toLocalDate().plusDays(2), rs.getString("loaiHopDong"), rs.getDouble("luongCoBan")));
				}
				x.setMaNhanVien(rs.getString("maNhanVien"));
				x.setHoTen(rs.getString("hoTen"));
				x.setGioiTinh(rs.getString("gioiTinh"));
				x.setNgaySinh(rs.getDate("ngaySinh").toLocalDate().plusDays(2));
				x.setDiaChi(new DIACHI(arr2[0],temp, arr[1], arr[2], arr[3]));
				x.setDanToc(rs.getString("danToc"));
				x.setTonGiao(rs.getString("tonGiao"));
				x.setEmail(rs.getString("email"));
				x.setSdt(rs.getString("SDT"));
				x.setCmnd(new CMND(rs.getString("soCMND"),rs.getString("noiCap"),rs.getDate("ngayCap").toLocalDate().plusDays(2)));
				
				x.setTaiKhoan(new TAIKHOAN(rs.getString("username"), rs.getString("pass"), rs.getString("maNhomQuyen"), rs.getString("avatar")));
				x.setChucVu(new CHUCVU(rs.getString("maChucVu"), rs.getString("tenChucVu"), rs.getDouble("phuCapChucVu"), rs.getDate("ngayNhanChuc").toLocalDate().plusDays(2)));
				x.setMaPhong(rs.getString("maPhong"));
				x.setTrinhDo(new TRINHDO(rs.getString("maTrinhDo"), rs.getString("trinhDoHocVan"), rs.getString("trinhDoChuyenMon"), rs.getString("chuyenNganh")));
				ConnectionManager.closeConnection(con);
				return x;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	public ArrayList<String> getDanhSachMaSo() {
		Connection con = ConnectionManager.getConnection();
		try {
			Statement ps = con.createStatement();
			ResultSet rs = ps.executeQuery("select maNhanVien from NHANVIEN");
			ArrayList<String> list = new ArrayList<>();
			while(rs.next()) {
				list.add(rs.getString("maNhanVien"));
			}
			ConnectionManager.closeConnection(con);
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	public String getTenNhanVien(String maSo) {
		Connection con = ConnectionManager.getConnection();
		try {
			Statement ps = con.createStatement();
			ResultSet rs = ps.executeQuery("select hoTen from CONNGUOI join NHANVIEN on NHANVIEN.CMND = CONNGUOI.CMND where maNhanVien = N'"+maSo+"'");
			while(rs.next()) {
				return rs.getString("hoTen");
			}
			ConnectionManager.closeConnection(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	public void thayDoiMaPhongBanAll(String oldId,String newId) {
		Connection con = ConnectionManager.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement("update NHANVIEN set maPhong = ? where maPhong = ?");
			pst.setString(1, newId);
			pst.setString(2, oldId);
			pst.executeUpdate();
			ConnectionManager.closeConnection(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void thayDoiMaPhongBan(String emplopyeeId, String departmentId) {
		Connection con = ConnectionManager.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement("update NHANVIEN set maPhong = ? where maNhanVien = ?");
			pst.setString(1, departmentId);
			pst.setString(2, emplopyeeId);
			pst.executeUpdate();
			ConnectionManager.closeConnection(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}




	public void insertNHANVIENfromUNGVIEN(NHANVIEN x) {
		Connection con = ConnectionManager.getConnection();
		try {
			TrinhDoDAO.getInstance().insert(x.getTrinhDo());
			ChucVuDAO.getInstance().insert(x.getChucVu());
			PreparedStatement pst;
			if(x instanceof NHANVIENCHINHTHUC) {
				HopDongLaoDongDAO.getInstance().insert(((NHANVIENCHINHTHUC)x).getHopDong()) ;
				// nhan vien
				
				pst = con.prepareStatement("insert NHANVIEN values(?,?,?,?,?,?,?,?,?,?)");
				pst.setString(1, x.getMaNhanVien());
				pst.setString(2, x.getCmnd().getSoCmnd());
				pst.setString(3, x.getMaPhong());
				pst.setString(4, x.getTrinhDo().getMaTrinhDo());
				pst.setString(5, x.getChucVu().getMaChucVu());
				pst.setString(6, ((NHANVIENCHINHTHUC)x).getHopDong().getMaHopDong());
				pst.setDate(7,null);
				pst.setDate(8,null);
				pst.setDouble(9,0);
				pst.setInt(10, 1);
				pst.executeUpdate();
			}else {
				// nhan vien
				pst = con.prepareStatement("insert NHANVIEN values(?,?,?,?,?,?,?,?,?,?)");
				pst.setString(1, x.getMaNhanVien());
				pst.setString(2, x.getCmnd().getSoCmnd());
				pst.setString(3, x.getMaPhong());
				pst.setString(4, x.getTrinhDo().getMaTrinhDo());
				pst.setString(5, x.getChucVu().getMaChucVu());
				pst.setString(6, null);
				pst.setDate(7,Date.valueOf(((NHANVIENTHUVIEC)x).getNgayBatDauThuViec()));
				pst.setDate(8,Date.valueOf(((NHANVIENTHUVIEC)x).getNgayKetThucThuViec()));
				pst.setDouble(9,((NHANVIENTHUVIEC)x).getLuongThuViec());
				pst.setInt(10, 1);
				pst.executeUpdate();
			}
			// tài khoản
			TaiKhoanDAO.getInstance().insert(x.getTaiKhoan());
			
			ConnectionManager.closeConnection(con);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}

	
	public void dieuChinhLuong(String maNhanVien,double mucLuong) {
		String sql = " select * from NHANVIEN where maNhanVien = N'"+maNhanVien+"'";
		Connection con = ConnectionManager.getConnection();
			try {
				Statement st = con.createStatement();
			ResultSet rs = st.executeQuery(sql);
			while(rs.next()) {
				Statement st1 =con.createStatement();
				if(rs.getString("maHopDong")==null) {
					st1.executeUpdate("update NHANVIEN set luongThuViec = "+mucLuong+" where maNhanVien = N'"+maNhanVien+"'");
				}else {
					st1.executeUpdate("update HOPDONGLAODONG set luongCoBan = "+mucLuong+" where maHopDong = N'"+rs.getString("maHopDong")+"'");
				}
			}
			ConnectionManager.closeConnection(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
			
	public Object[][] getDanhSachNhanVienThuViecToRender() {
		Connection con = ConnectionManager.getConnection();
		 Object[][] obj =null;
		 try {
			 Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select count(maNhanVien) from CONNGUOI cn join NHANVIEN nv on cn.CMND = nv.CMND\r\n"
					+ "join PHONGBAN pb on nv.maPhong = pb.maPhong where nv.maHopDong is null and nv.trangThai=1");
			while(rs.next()) {
				obj = new Object[rs.getInt(1)][];
			}
			rs = st.executeQuery("select * from CONNGUOI cn join NHANVIEN nv on cn.CMND = nv.CMND\r\n"
					+ "join PHONGBAN pb on nv.maPhong = pb.maPhong where nv.maHopDong is null and nv.trangThai=1");
			int count = 0;
			while(rs.next()) {
				obj[count] = new Object[] {
						count+1+"", rs.getString("maNhanVien")+" - "+rs.getString("hoTen"), rs.getString("tenPhong"),LocalDateToString(rs.getDate("ngayBatDauThuViec").toLocalDate())
				};
				count++;
			}
			ConnectionManager.closeConnection(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		 return obj;
	}

	public Object[][] getDanhSachNhanVienThuViecToRender(String tenPhong, int sort_by, int sort_mode) {
		Connection con = ConnectionManager.getConnection();
		 Object[][] obj =null;
		 try {
			 String sql = "from CONNGUOI cn join NHANVIEN nv on cn.CMND = nv.CMND"
								 + " join PHONGBAN pb on nv.maPhong = pb.maPhong where nv.maHopDong is null and nv.trangThai=1";
			 if(!tenPhong.equals("Phòng ban")) {
				 sql += " and pb.tenPhong = N'"+tenPhong+"' ";
			 }
				
			 Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select count(maNhanVien) "+sql);
			while(rs.next()) {
				obj = new Object[rs.getInt(1)][];
			}
			if(sort_by==0) {
				 sql+= " order by nv.maNhanVien ";
				 if(sort_mode==0) {
					 sql+= "asc";
				 }else {
					 sql+= "desc";
				 }
			 }else {
				 sql+= " order by nv.ngayBatDauThuViec ";
				 if(sort_mode==0) {
					 sql+= " asc";
				 }else {
					 sql+= " desc";
				 }
			 }
			rs = st.executeQuery("select * "+sql);
			int count = 0;
			while(rs.next()) {
				obj[count] = new Object[] {
						count+1+"", rs.getString("maNhanVien")+" - "+rs.getString("hoTen"), rs.getString("tenPhong"),LocalDateToString(rs.getDate("ngayBatDauThuViec").toLocalDate())
				};
				count++;
			}
			ConnectionManager.closeConnection(con);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		 return obj;
	}

	// public String[] getThongTinNhanVienKiHopDong(String maNhanVien) {
	// 	Connection con = ConnectionManager.getConnection();
	// 	String []data = null;
	// 	try {
	// 		String sql = "select * from CONNGUOI cn join NHANVIEN nv on cn.CMND = nv.CMND\r\n"
	// 				+ "join CMND cmnd on cmnd.soCMND = cn.CMND\r\n"
	// 				+ "join PHONGBAN pb on nv.maPhong = pb.maPhong\r\n"
	// 				+ "join CHUCVU cv on cv.maChucVu = nv.maChucVu\r\n"
	// 				+ "join TRINHDO td on td.maTrinhDo = nv.maTrinhDo where maNhanVien = N'"+maNhanVien+"'";
	// 		Statement st = con.createStatement();
	// 		ResultSet rs = st.executeQuery(sql);
	// 		while(rs.next()) {
	// 			data = new String[] {
	// 				rs.getString("maNhanVien")+" - "+rs.getString("hoTen"),SUPPORT.LocalDateToString(rs.getDate("ngaySinh").toLocalDate()),rs.getString("gioiTinh"),
	// 				rs.getString("diaChi"),rs.getString("SDT"),rs.getString("email"),rs.getString("soCMND")+" - "+SUPPORT.LocalDateToString(rs.getDate("ngayCap").toLocalDate())+" - "+rs.getString("noiCap"),
	// 				rs.getString("trinhDoHocVan"),rs.getString("trinhDoChuyenMon"),rs.getString("chuyenNganh"),
	// 				rs.getString("tenPhong"),rs.getString("tenChucVu"),SUPPORT.changeSalaryToFormatString(rs.getDouble("luongThuViec")*1.25)
	// 			};
	// 		}
	// 		con.close();
	// 	} catch (SQLException e) {
	// 		e.printStackTrace();
	// 	}
	// 	return data;
	// }

	// public void updateMaHopDong(String maNhanVien, String maHopDong) {
	// 	Connection con = ConnectionManager.getConnection();
	// 	String []data = null;
	// 	try {
	// 		String sql = "update NHANVIEN set maHopDong = N'"+maHopDong+"' where maNhanVien = N'"+maNhanVien+"'";
	// 		Statement st = con.createStatement();
	// 		st.executeUpdate(sql);
	// 		con.close();
	// 	} catch (SQLException e) {
	// 		e.printStackTrace();
	// 	}
	// }
	public void hiddenNhanVien(String maNhanVien) {
		Connection con = ConnectionManager.getConnection();
		try {
			String sql = "update NHANVIEN set trangThai=0 where maNhanVien = ?";
			PreparedStatement st = con.prepareStatement(sql);
			st.setString(1, maNhanVien);
			st.executeUpdate();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public String getTen(String maNhanVien) {
		Connection con = ConnectionManager.getConnection();

		try {
			String ten = "";
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from CONNGUOI join NHANVIEN on CONNGUOI.CMND = NHANVIEN.CMND where NHANVIEN.maNhanVien = '" + maNhanVien + "' ");

			while (rs.next()) {
				ten = rs.getString("maNhanVien") + " - " + rs.getString("hoTen");
				
			}

			ConnectionManager.closeConnection(con);
			return ten;
		} catch (SQLException e) {
		}
		return null;
	}
	public double[] getLuongNhanVien(String maNhanVien) {
		Connection con = ConnectionManager.getConnection();
		String sql = "select * from NHANVIEN nv left join HOPDONGLAODONG hd on nv.maHopDong = hd.maHopDong join CHUCVU cv on nv.maChucVu = cv.maChucVu where nv.maNhanVien=?";
		
		try {
			PreparedStatement ps = con.prepareStatement(sql);
			ps.setString(1, maNhanVien);
			ResultSet rs = ps.executeQuery();
			while(rs.next()) {
				if(rs.getString("maHopDong")==null) {
					return new double[] {rs.getDouble("luongThuViec"), rs.getDouble("phuCapChucVu")};
				}
				return new double[] {rs.getDouble("luongCoBan"), rs.getDouble("phuCapChucVu")};
			}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return null;
	}


	public ArrayList<NHANVIEN> renderChangeValue(String[] default_val,String[] event_name) {
		Connection con = ConnectionManager.getConnection();
		ArrayList<NHANVIEN> list = new ArrayList<>();
		try {
			String sql = "select * from CONNGUOI join NHANVIEN on CONNGUOI.CMND = NHANVIEN.CMND join CMND on CMND.soCMND = CONNGUOI.CMND join TRINHDO on NHANVIEN.maTrinhDo = TRINHDO.maTrinhDo join CHUCVU on NHANVIEN.maChucVu = CHUCVU.maChucVu join TAIKHOAN on TAIKHOAN.username = NHANVIEN.maNhanVien left join HOPDONGLAODONG on HOPDONGLAODONG.maHopDong = NHANVIEN.maHopDong join PHONGBAN on NHANVIEN.maPhong = PHONGBAN.maPhong where NHANVIEN.trangThai=1";
			if (!default_val[0].equals(event_name[0])) {
				sql += "and tenPhong = N'"+event_name[0]+"' ";
			}
			if (!default_val[1].equals(event_name[1])) {
				sql += "and gioiTinh = N'"+event_name[1]+"' ";
			} if (!default_val[2].equals(event_name[2])) {
				String arrdoTuoi[] = event_name[2].split("-");
				sql+="and year(getdate()) - year(ngaySinh) >= "+arrdoTuoi[0] +" and year(getdate()) - year(ngaySinh) <= "+arrdoTuoi[1]+" ";
			}
			PreparedStatement ps = con.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while(rs.next() ) {
				NHANVIEN nv = null;
				String str = rs.getString(5);
				String arr[] = str.split(",");
				String arr2[] = arr[0].split(" ");
				String temp ="";
				for(int i=1;i<arr2.length;i++) {
					temp+=arr2[i]+" ";
				}
				temp = temp.trim();
				
				if (rs.getString(15)==null) { // Dòng 15 truy vấn là ngày bắt đầu thử việc
					nv = new NHANVIENTHUVIEC();
					((NHANVIENTHUVIEC)nv).setNgayBatDauThuViec(rs.getDate("ngayBatDauThuViec").toLocalDate().plusDays(2));
					((NHANVIENTHUVIEC)nv).setNgayKetThucThuViec(rs.getDate("ngayKetThucThuViec").toLocalDate().plusDays(2));
					((NHANVIENTHUVIEC)nv).setLuongThuViec(rs.getDouble("luongThuViec"));
				}
				else {
					nv = new NHANVIENCHINHTHUC();
					((NHANVIENCHINHTHUC)nv).setHOPDONG(new HOPDONGLAODONG(rs.getString("maHopDong"), rs.getDate("tuNgay").toLocalDate().plusDays(2), rs.getDate("denNgay").toLocalDate().plusDays(2), rs.getString("loaiHopDong"), rs.getDouble("luongCoBan")));
				}
				
				nv.setMaNhanVien(rs.getString("maNhanVien"));
				nv.setHoTen(rs.getString("hoTen"));
				nv.setGioiTinh(rs.getString("gioiTinh"));
				nv.setNgaySinh(rs.getDate("ngaySinh").toLocalDate().plusDays(2));
				nv.setDiaChi(new DIACHI(arr2[0],temp, arr[1], arr[2], arr[3]));
				nv.setDanToc(rs.getString("danToc"));
				nv.setTonGiao(rs.getString("tonGiao"));
				nv.setEmail(rs.getString("email"));
				nv.setSdt(rs.getString("SDT"));
				nv.setCmnd(new CMND(rs.getString("soCMND"),rs.getString("noiCap"),rs.getDate("ngayCap").toLocalDate().plusDays(2)));
				
				nv.setTaiKhoan(new TAIKHOAN(rs.getString("username"), rs.getString("pass"), rs.getString("maNhomQuyen"), rs.getString("avatar")));
				nv.setChucVu(new CHUCVU(rs.getString("maChucVu"), rs.getString("tenChucVu"), rs.getDouble("phuCapChucVu"), rs.getDate("ngayNhanChuc").toLocalDate().plusDays(2)));
				nv.setMaPhong(rs.getString("maPhong"));
				nv.setTrinhDo(new TRINHDO(rs.getString("maTrinhDo"), rs.getString("trinhDoHocVan"), rs.getString("trinhDoChuyenMon"), rs.getString("chuyenNganh")));
				list.add(nv);
				
				
				
			}
		} catch(SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionManager.closeConnection(con);
		}

		return list;
	}
	
	public static String LocalDateToString(LocalDate date) {
		String arr[] = date.toString().split("-");
		return arr[2]+"-"+arr[1]+"-"+arr[0];
		
	}
	


}