[fix]: fix save checkup

This commit is contained in:
Lê Thành Đạt committed 2025-07-12 00:06:53 +07:00
1 parent 1efc40027e
commit 2e6dad480d
5 files changed
+150 -93

No files matched your search

@@ -128,6 +128,7 @@ import BsK.common.packet.res.AddPatientResponse;
import BsK.common.packet.res.RegisterSuccessResponse;
import BsK.common.packet.res.SaveCheckupRes;
import BsK.common.packet.res.UploadCheckupImageResponse;
import BsK.common.util.date.DateUtils;
@Slf4j
public class CheckUpPage extends JPanel {
@@ -250,29 +251,13 @@ public class CheckUpPage extends JPanel {
return new String[][]{};
}
String[][] tableData = new String[patients.size()][8];
SimpleDateFormat displayFormat = new SimpleDateFormat("dd/MM/yyyy");
for (int i = 0; i < patients.size(); i++) {
Patient p = patients.get(i);
tableData[i][0] = p.getCheckupId();
// Convert DOB to display format
String dobStr = p.getCustomerDob();
try {
if (dobStr != null && !dobStr.trim().isEmpty()) {
if (dobStr.matches("\\d+")) { // If it's a timestamp
Date dobDate = new Date(Long.parseLong(dobStr));
tableData[i][1] = displayFormat.format(dobDate);
} else { // If it's already in date format
tableData[i][1] = dobStr;
}
} else {
tableData[i][1] = "";
}
} catch (NumberFormatException e) {
log.warn("Invalid DOB format for patient {}: {}", p.getCheckupId(), dobStr);
tableData[i][1] = dobStr; // Use original string if parsing fails
}
// Convert DOB to display format using utility function
tableData[i][1] = DateUtils.convertToDisplayFormat(p.getCustomerDob());
tableData[i][2] = p.getCustomerLastName();
tableData[i][3] = p.getCustomerFirstName();
@@ -1725,40 +1710,27 @@ public class CheckUpPage extends JPanel {
serDialog = null;
updatePrescriptionTree();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
try {
Date parsedDate; String dateStr = selectedPatient.getCheckupDate();
if (dateStr != null && dateStr.matches("\\d+")) { // Check if string is all digits (timestamp)
parsedDate = new Date(Long.parseLong(dateStr));
} else if (dateStr != null && !dateStr.trim().isEmpty()) { // Attempt to parse as dd/MM/yyyy
parsedDate = dateFormat.parse(dateStr);
} else {
log.error("Invalid date format for checkup date: {}", selectedPatient.getCheckupDate());
return;
}
Calendar calendar = Calendar.getInstance(); calendar.setTime(parsedDate);
// Set checkup date using utility function
Date checkupDate = DateUtils.convertToDate(selectedPatient.getCheckupDate());
if (checkupDate != null) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(checkupDate);
datePicker.getModel().setDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
datePicker.getModel().setSelected(true);
} catch (ParseException | NumberFormatException exception) {
log.error("Invalid date format for checkup date: {}", selectedPatient.getCheckupDate(), exception);
} else {
log.error("Invalid date format for checkup date: {}", selectedPatient.getCheckupDate());
JOptionPane.showMessageDialog(null, "Định dạng ngày hoặc dấu thời gian không hợp lệ: " + selectedPatient.getCheckupDate(), "Lỗi định dạng ngày", JOptionPane.ERROR_MESSAGE);
}
try {
Date parsedDate; String dobStr = selectedPatient.getCustomerDob();
if (dobStr != null && dobStr.matches("\\d+")) { // Timestamp
parsedDate = new Date(Long.parseLong(dobStr));
} else if (dobStr != null && !dobStr.trim().isEmpty()){ // Date string
parsedDate = new SimpleDateFormat("dd/MM/yyyy").parse(dobStr);
} else {
log.error("Invalid date format for DOB: {}", selectedPatient.getCustomerDob());
return;
}
Calendar calendar = Calendar.getInstance(); calendar.setTime(parsedDate);
// Set DOB using utility function
Date dobDate = DateUtils.convertToDate(selectedPatient.getCustomerDob());
if (dobDate != null) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(dobDate);
dobPicker.getModel().setDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
dobPicker.getModel().setSelected(true);
} catch (ParseException | NumberFormatException exception) {
log.error("Invalid date format for DOB: {}", selectedPatient.getCustomerDob(), exception);
} else {
log.error("Invalid date format for DOB: {}", selectedPatient.getCustomerDob());
JOptionPane.showMessageDialog(null, "Định dạng ngày không hợp lệ cho ngày sinh: " + selectedPatient.getCustomerDob(), "Lỗi định dạng ngày", JOptionPane.ERROR_MESSAGE);
}
@@ -1991,14 +1963,18 @@ public class CheckUpPage extends JPanel {
String ho = patient.getCustomerLastName(); String ten = patient.getCustomerFirstName();
String customerDob = patient.getCustomerDob(); String namSinh = "N/A";
if (customerDob != null && !customerDob.isEmpty()) {
try {
Date parsedDate;
if (customerDob.matches("\\d+")) parsedDate = new Date(Long.parseLong(customerDob)); // Timestamp
else parsedDate = new SimpleDateFormat("dd/MM/yyyy").parse(customerDob); // Date string
Calendar calendar = Calendar.getInstance(); calendar.setTime(parsedDate);
namSinh = String.valueOf(calendar.get(Calendar.YEAR));
} catch (ParseException | NumberFormatException e) {
log.error("Error parsing DoB for TV display: {} for patient ID {}", customerDob, patientIdToFind, e);
// Extract year from DOB for display
int year = DateUtils.extractYearFromTimestamp(customerDob);
if (year != -1) {
namSinh = String.valueOf(year);
} else {
// Try to parse as date string and extract year
Date dobDate = DateUtils.convertToDate(customerDob);
if (dobDate != null) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(dobDate);
namSinh = String.valueOf(calendar.get(Calendar.YEAR));
}
}
}
patientDisplayInfo = ho + " " + ten + " (" + namSinh + ")";
@@ -3240,27 +3216,30 @@ public class CheckUpPage extends JPanel {
log.info("customerCccdDdcn: {}", customerCccdDdcn);
log.info("=== End SaveCheckupRequest Data ===");
Long customerDobLong = DateUtils.convertToDate(customerDob).getTime();
Long reCheckupDateLong = DateUtils.convertToDate(reCheckupDate).getTime();
Long checkupDateLong = DateUtils.convertToDate(checkupDate).getTime();
SaveCheckupRequest request = new SaveCheckupRequest(
checkupId,
checkupDate,
customerId,
customerLastName,
Integer.parseInt(checkupId),
Integer.parseInt(customerId),
Integer.parseInt(doctorId),
checkupDateLong,
suggestions,
diagnosis,
rtfContent,
status,
checkupType,
conclusion,
reCheckupDateLong,
customerFirstName,
customerDob,
customerLastName,
customerDobLong,
customerGender,
fullAddress,
customerNumber,
customerWeight,
customerHeight,
doctorId,
suggestions, // De nghi
diagnosis, // Chuan doan
rtfContent, // Send RTF content as notes
status,
checkupType,
conclusion, // Ket luan
reCheckupDate,
Double.parseDouble(customerWeight),
Double.parseDouble(customerHeight),
customerCccdDdcn,
medicinePrescription,
servicePrescription
@@ -8,10 +8,10 @@ import lombok.EqualsAndHashCode;
@Data
@AllArgsConstructor
public class SaveCheckupRequest implements Packet {
String checkupId;
String customerId;
String doctorId;
String checkupDate;
Integer checkupId;
Integer customerId;
Integer doctorId;
Long checkupDate;
// Checkup Details
String suggestions;
@@ -20,17 +20,17 @@ public class SaveCheckupRequest implements Packet {
String status;
String checkupType;
String conclusion;
String reCheckupDate;
Long reCheckupDate;
// Patient Details
String customerFirstName;
String customerLastName;
String customerDob;
Long customerDob;
String customerGender;
String customerAddress;
String customerNumber;
String customerWeight;
String customerHeight;
Double customerWeight;
Double customerHeight;
String customerCccdDdcn;
// Prescriptions
@@ -3,8 +3,13 @@ package BsK.common.util.date;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
public class DateUtils {
private static final SimpleDateFormat DISPLAY_FORMAT = new SimpleDateFormat("dd/MM/yyyy");
/**
* Extracts the year from a given timestamp string in milliseconds.
*
@@ -25,6 +30,73 @@ public class DateUtils {
}
}
/**
* Converts a date input (timestamp string or dd/MM/yyyy string) to display format (dd/MM/yyyy).
*
* @param dateInput the date input - can be a timestamp string or already formatted date string
* @return the date in dd/MM/yyyy format, or the original string if conversion fails
*/
public static String convertToDisplayFormat(String dateInput) {
if (dateInput == null || dateInput.trim().isEmpty()) {
return "";
}
try {
// Check if it's a timestamp (all digits)
if (dateInput.matches("\\d+")) {
Date date = new Date(Long.parseLong(dateInput));
return DISPLAY_FORMAT.format(date);
} else {
// If it's already in date format, try to parse and reformat to ensure consistency
Date date = DISPLAY_FORMAT.parse(dateInput);
return DISPLAY_FORMAT.format(date);
}
} catch (NumberFormatException | ParseException e) {
// If parsing fails, return the original string
System.err.println("Invalid date format: " + dateInput);
return dateInput;
}
}
/**
* Converts a date input (timestamp string or dd/MM/yyyy string) to Date object.
*
* @param dateInput the date input - can be a timestamp string or dd/MM/yyyy formatted string
* @return the Date object, or null if conversion fails
*/
public static Date convertToDate(String dateInput) {
if (dateInput == null || dateInput.trim().isEmpty()) {
// return plain date
return new Date();
}
try {
// Check if it's a timestamp (all digits)
if (dateInput.matches("\\d+")) {
return new Date(Long.parseLong(dateInput));
} else {
// Try to parse as dd/MM/yyyy format
return DISPLAY_FORMAT.parse(dateInput);
}
} catch (NumberFormatException | ParseException e) {
System.err.println("Invalid date format: " + dateInput);
return new Date();
}
}
/**
* Converts a Date object to display format (dd/MM/yyyy).
*
* @param date the Date object to convert
* @return the date in dd/MM/yyyy format, or empty string if date is null
*/
public static String formatToDisplay(Date date) {
if (date == null) {
return "";
}
return DISPLAY_FORMAT.format(date);
}
public static void main(String[] args) {
// Test the method with valid and invalid input
String validTimestamp = "1085875200000";
@@ -35,5 +107,11 @@ public class DateUtils {
int invalidYear = DateUtils.extractYearFromTimestamp(invalidTimestamp);
System.out.println("Year: " + invalidYear); // Output: Year: -1
// Test new utility methods
System.out.println("Display format: " + convertToDisplayFormat(validTimestamp));
System.out.println("Display format: " + convertToDisplayFormat("15/06/2023"));
System.out.println("Date object: " + convertToDate(validTimestamp));
System.out.println("Date object: " + convertToDate("15/06/2023"));
}
}
@@ -741,15 +741,15 @@ public class ServerHandler extends SimpleChannelInboundHandler<TextWebSocketFram
""";
PreparedStatement customerStmt = conn.prepareStatement(customerSql);
customerStmt.setString(1, saveCheckupRequest.getCustomerId());
customerStmt.setInt(1, saveCheckupRequest.getCustomerId());
customerStmt.setString(2, saveCheckupRequest.getCustomerFirstName());
customerStmt.setString(3, saveCheckupRequest.getCustomerLastName());
customerStmt.setString(4, saveCheckupRequest.getCustomerDob());
customerStmt.setLong(4, saveCheckupRequest.getCustomerDob());
customerStmt.setString(5, saveCheckupRequest.getCustomerGender());
customerStmt.setString(6, saveCheckupRequest.getCustomerAddress());
customerStmt.setString(7, saveCheckupRequest.getCustomerNumber());
customerStmt.setString(8, saveCheckupRequest.getCustomerWeight());
customerStmt.setString(9, saveCheckupRequest.getCustomerHeight());
customerStmt.setDouble(8, saveCheckupRequest.getCustomerWeight());
customerStmt.setDouble(9, saveCheckupRequest.getCustomerHeight());
customerStmt.setString(10, saveCheckupRequest.getCustomerCccdDdcn());
customerStmt.executeUpdate();
log.info("Customer saved successfully");
@@ -787,10 +787,10 @@ public class ServerHandler extends SimpleChannelInboundHandler<TextWebSocketFram
""";
PreparedStatement checkupStmt = conn.prepareStatement(checkupSql);
checkupStmt.setString(1, saveCheckupRequest.getCheckupId());
checkupStmt.setString(2, saveCheckupRequest.getCustomerId());
checkupStmt.setString(3, saveCheckupRequest.getDoctorId());
checkupStmt.setString(4, saveCheckupRequest.getCheckupDate());
checkupStmt.setInt(1, saveCheckupRequest.getCheckupId());
checkupStmt.setInt(2, saveCheckupRequest.getCustomerId());
checkupStmt.setInt(3, saveCheckupRequest.getDoctorId());
checkupStmt.setLong(4, saveCheckupRequest.getCheckupDate());
checkupStmt.setString(5, saveCheckupRequest.getSuggestions());
checkupStmt.setString(6, saveCheckupRequest.getDiagnosis());
checkupStmt.setString(7, prescriptionId);
@@ -798,7 +798,7 @@ public class ServerHandler extends SimpleChannelInboundHandler<TextWebSocketFram
checkupStmt.setString(9, saveCheckupRequest.getStatus());
checkupStmt.setString(10, saveCheckupRequest.getCheckupType());
checkupStmt.setString(11, saveCheckupRequest.getConclusion());
checkupStmt.setString(12, saveCheckupRequest.getReCheckupDate());
checkupStmt.setLong(12, saveCheckupRequest.getReCheckupDate());
checkupStmt.executeUpdate();
log.info("Prescription ID generated or edited: {}", prescriptionId);
log.info("Checkup saved successfully");
@@ -806,11 +806,11 @@ public class ServerHandler extends SimpleChannelInboundHandler<TextWebSocketFram
// 4. Handle Medicine Prescriptions
// First, delete all existing items for this checkup to handle edits/removals
try (PreparedStatement deleteOrderItemsStmt = conn.prepareStatement("DELETE FROM OrderItem WHERE checkup_id = ?")) {
deleteOrderItemsStmt.setString(1, saveCheckupRequest.getCheckupId());
deleteOrderItemsStmt.setInt(1, saveCheckupRequest.getCheckupId());
deleteOrderItemsStmt.executeUpdate();
}
try (PreparedStatement deleteMedicineOrderStmt = conn.prepareStatement("DELETE FROM MedicineOrder WHERE checkup_id = ?")) {
deleteMedicineOrderStmt.setString(1, saveCheckupRequest.getCheckupId());
deleteMedicineOrderStmt.setInt(1, saveCheckupRequest.getCheckupId());
deleteMedicineOrderStmt.executeUpdate();
}
log.info("Cleared previous medicine prescription for checkup_id: {}", saveCheckupRequest.getCheckupId());
@@ -850,8 +850,8 @@ public class ServerHandler extends SimpleChannelInboundHandler<TextWebSocketFram
PreparedStatement medicineOrderStmt = conn.prepareStatement(medicineOrderSql);
medicineOrderStmt.setString(1, prescriptionId);
medicineOrderStmt.setString(2, saveCheckupRequest.getCheckupId());
medicineOrderStmt.setString(3, saveCheckupRequest.getCustomerId());
medicineOrderStmt.setInt(2, saveCheckupRequest.getCheckupId());
medicineOrderStmt.setInt(3, saveCheckupRequest.getCustomerId());
medicineOrderStmt.setDouble(4, totalMedicineAmount);
medicineOrderStmt.setString(5, ""); // payment_method
medicineOrderStmt.setString(6, "Pending"); // status
@@ -891,7 +891,7 @@ public class ServerHandler extends SimpleChannelInboundHandler<TextWebSocketFram
orderItemStmt.setString(5, ""); // duration - could be calculated or added to request
orderItemStmt.setString(6, medicine[7]); // unit_price
orderItemStmt.setString(7, medicine[8]); // total_price
orderItemStmt.setString(8, saveCheckupRequest.getCheckupId());
orderItemStmt.setInt(8, saveCheckupRequest.getCheckupId());
orderItemStmt.setString(9, medicine[9]); // notes
orderItemStmt.addBatch();
}
@@ -903,7 +903,7 @@ public class ServerHandler extends SimpleChannelInboundHandler<TextWebSocketFram
// 5. Handle Service Prescriptions
// First, delete all existing services for this checkup
try (PreparedStatement deleteServiceStmt = conn.prepareStatement("DELETE FROM CheckupService WHERE checkup_id = ?")) {
deleteServiceStmt.setString(1, saveCheckupRequest.getCheckupId());
deleteServiceStmt.setInt(1, saveCheckupRequest.getCheckupId());
deleteServiceStmt.executeUpdate();
}
log.info("Cleared previous service prescription for checkup_id: {}", saveCheckupRequest.getCheckupId());
@@ -926,7 +926,7 @@ public class ServerHandler extends SimpleChannelInboundHandler<TextWebSocketFram
for (String[] service : saveCheckupRequest.getServicePrescription()) {
if (service.length >= 6) {
serviceStmt.setString(1, saveCheckupRequest.getCheckupId());
serviceStmt.setInt(1, saveCheckupRequest.getCheckupId());
serviceStmt.setString(2, service[0]); // service_id
serviceStmt.setString(3, service[2]); // quantity
serviceStmt.setString(4, service[4]); // total_cost
Binary file not shown.