0% found this document useful (0 votes)
7 views18 pages

Thêm M I Patient

Uploaded by

thanhdp881
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views18 pages

Thêm M I Patient

Uploaded by

thanhdp881
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

API

 Patient
- Thêm mới patient

@PostMapping("/patient/add")
public ResponseEntity<Map<String, Object>> addPatient(@RequestBody
PatientDTO patientDTO) {
Map<String, Object> response = new HashMap<>();

try {
PatientEntity patientEntity = new PatientEntity();
patientEntity.setFirstName(patientDTO.getFirstName());
patientEntity.setLastName(patientDTO.getLastName());
patientEntity.setGender(patientDTO.getGender());
patientEntity.setDateOfBirth(patientDTO.getDob());
patientEntity.setStreet(patientDTO.getStreet());
patientEntity.setPhoneNumber(patientDTO.getPhoneNumber());

// Setting city, district, and ward based on IDs


Cities city = new Cities();
city.setCityId(patientDTO.getCityId());
patientEntity.setCity(city);

Districts district = new Districts();


district.setDistrictId(patientDTO.getDistrictId());
patientEntity.setDistrict(district);

Wards ward = new Wards();


ward.setWardId(patientDTO.getWardId());
patientEntity.setWard(ward);

// Luu benh nhan


PatientEntity result = patientRepository.save(patientEntity);

response.put("message", "Thêm mới thành công!");


response.put("patient", result);
return new ResponseEntity<>(response, HttpStatus.CREATED); //
HTTP 201 for created resource
} catch (Exception e) {
response.put("message", "Thêm mới thất bại: " + e.getMessage());
return new ResponseEntity<>(response,
HttpStatus.INTERNAL_SERVER_ERROR); // HTTP 500 for server error
}
}

- Update patient

@PutMapping("patient/edit/{id}")
public ResponseEntity<Map<String,String>>
updatePatient(@PathVariable Integer id, @RequestBody PatientDTO
patientDetails) {

try {
Optional<PatientEntity> existingPatient =
patientRepository.findById(id);

PatientEntity patient = existingPatient.get();

// Update patient fields


patient.setFirstName(patientDetails.getFirstName());
patient.setLastName(patientDetails.getLastName());
patient.setDateOfBirth(patientDetails.getDob());
patient.setStreet(patientDetails.getStreet());
patient.setPhoneNumber(patientDetails.getPhoneNumber());

// Set City, District, and Ward (check if they exist)


Optional<Cities> city =
cityRepository.findById(patientDetails.getCityId());
if (city.isPresent()) {
patient.setCity(city.get());
} else {
return new ResponseEntity<>(Map.of("message", "City not
found"), HttpStatus.BAD_REQUEST);
}

Optional<Districts> district =
districtRepository.findById(patientDetails.getDistrictId());
if (district.isPresent()) {
patient.setDistrict(district.get());
} else {
return new ResponseEntity<>(Map.of("message", "District not
found"), HttpStatus.BAD_REQUEST);
}

Optional<Wards> ward =
wardRepository.findById(patientDetails.getWardId());
if (ward.isPresent()) {
patient.setWard(ward.get());
} else {
return new ResponseEntity<>(Map.of("message", "Ward not
found"), HttpStatus.BAD_REQUEST);
}

patientRepository.save(patient);

// Return success response


return new ResponseEntity<>(Map.of("message", "Chỉnh sửa thành
công"), HttpStatus.OK);

} catch (Exception e) {
return new ResponseEntity<>(Map.of("message", e.getMessage()),
HttpStatus.INTERNAL_SERVER_ERROR);
}
}

- Delete Patient

@DeleteMapping("patient/delete/{id}")
public ResponseEntity<Map<String, String>>
deletePatient(@PathVariable Integer id) {
Map<String, String> response = new HashMap<>();

try {
int updated = patientRepository.softDeletePatient(id);

if (updated > 0) {
response.put("message", "Xóa thành công");
return new ResponseEntity<>(response, HttpStatus.OK);
} else {
response.put("message", "Bệnh nhân không tồn tại.");
return new ResponseEntity<>(response,
HttpStatus.NOT_FOUND);
}
} catch (Exception e) {
response.put("message", "Không thành công: " + e.getMessage());
return new ResponseEntity<>(response,
HttpStatus.INTERNAL_SERVER_ERROR);
}
}

 Admission
- Thêm mới admission
@PostMapping("/admission/add")
public ResponseEntity<Map<String, Object>> addAdmission(@RequestBody
AdmissionDTO admissionDTO) {
Map<String, Object> response = new HashMap<>();

try {
// Check if Patient exists
PatientEntity patient =
patientRepository.findById(admissionDTO.getInpatientId())
.orElseThrow(() -> new IllegalArgumentException("Patient not found
with ID: " + admissionDTO.getInpatientId()));

System.out.println("Patient found: " + patient);

Optional<InPatientEntity> optionalInPatient =
inPatientRepository.findByPatient_ID(patient.getID());
InPatientEntity inPatient;

if (optionalInPatient.isPresent()) {
inPatient = optionalInPatient.get();
} else {
inPatient = new InPatientEntity();
inPatient.setPatient(patient);

String generatedCode = String.format("IP%09d", patient.getID());


inPatient.setCode(generatedCode);
inPatientRepository.save(inPatient);
}

// Log the examination DTO


System.out.println("Admission DTO before saving: " + admissionDTO);

// Call the service to add the examination


// Step 3: Fetch related entities (Doctor, Nurse, Room)
DoctorEntity doctor =
doctorRepository.findById(admissionDTO.getDoctorId())
.orElseThrow(() -> new IllegalArgumentException("Doctor not found
with ID: " + admissionDTO.getDoctorId()));
NurseEntity nurse =
nurseRepository.findById(admissionDTO.getNurseId())
.orElseThrow(() -> new IllegalArgumentException("Nurse not found
with ID: " + admissionDTO.getNurseId()));
RoomEntity room =
roomRepository.findById(admissionDTO.getSickroom())
.orElseThrow(() -> new IllegalArgumentException("Room not found
with ID: " + admissionDTO.getSickroom()));

AdmissionEntity admissionEntity = new AdmissionEntity();


admissionEntity.setInPatient(inPatient); // Ensure the inPatient entity is set
correctly
admissionEntity.setDoctor(doctor); // Set the doctor entity
admissionEntity.setNurse(nurse); // Set the nurse entity
admissionEntity.setRoom(room); // Set the room entity
admissionEntity.setDateAdmission(admissionDTO.getDateAdmission());
admissionEntity.setDiagnosis(admissionDTO.getDiagnosis());

admissionEntity.setDateOfDischarge(admissionDTO.getDateOfDischarge());
admissionEntity.setFee(admissionDTO.getFee());
admissionRepository.save(admissionEntity);

response.put("message", "Thêm mới thành công!");


return new ResponseEntity<>(response, HttpStatus.CREATED);
} catch (Exception e) {
e.printStackTrace(); // Print stack trace for better debugging
response.put("message", "Thêm mới không thành công: " +
e.getMessage());
return new ResponseEntity<>(response,
HttpStatus.INTERNAL_SERVER_ERROR);
}
}

- Update Admission
@PostMapping(value = "/admission/edit", consumes =
MediaType.APPLICATION_JSON_VALUE)
@Transactional
public ResponseEntity<Map<String, Object>>
editAdmission(@RequestParam("id") Integer admissionId, @RequestBody
AdmissionDTO admissionDTO) {
Map<String, Object> response = new HashMap<>();
try {
// Find existing admission by ID
Optional<AdmissionEntity> optionalAdmission =
admissionRepository.findById(admissionId);

if (!optionalAdmission.isPresent()) {
response.put("status", "error");
response.put("message", "Admission not found.");
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
}

AdmissionEntity admission = optionalAdmission.get();

// Update related entities (doctor, nurse, and room)


DoctorEntity doctor = new DoctorEntity();
doctor.setID(admissionDTO.getDoctorId());
admission.setDoctor(doctor);

NurseEntity nurse = new NurseEntity();


nurse.setID(admissionDTO.getNurseId());
admission.setNurse(nurse);

RoomEntity sickroom = new RoomEntity();


sickroom.setRoomNo(admissionDTO.getSickroom());
admission.setRoom(sickroom);
// Update
admission.setDateAdmission(admissionDTO.getDateAdmission());
admission.setDateOfDischarge(admissionDTO.getDateOfDischarge());
admission.setDiagnosis(admissionDTO.getDiagnosis());
admission.setFee(admissionDTO.getFee());

// Save updated admission


admissionRepository.save(admission);

response.put("status", "success");
response.put("message", "Cập nhật thành công!");
response.put("admissionId", admissionId);
return ResponseEntity.ok(response);
} catch (Exception e) {
response.put("status", "error");
response.put("message", "Không thành công: " + e.getMessage());

return
ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response)
;
}
}
- Admission delete
@DeleteMapping("admission/delete/{id}")
public ResponseEntity<Map<String, String>> admission_del(@PathVariable
Integer id) {
Map<String, String> response = new HashMap<>();

try {
int updated = admissionRepository.admission_del(id);

if (updated > 0) {
response.put("message", "Xóa thành công");
return new ResponseEntity<>(response, HttpStatus.OK);
} else {
response.put("message", "Phiếu đăng kí không tồn tại");
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
} catch (Exception e) {
response.put("message", "Không thành công: " + e.getMessage());
return new ResponseEntity<>(response,
HttpStatus.INTERNAL_SERVER_ERROR);
}
}

 Examination
- Thêm mới examination
@PostMapping("examination/add")
public ResponseEntity<Map<String, Object>>
addExamination(@RequestBody ExaminationDTO examinationDTO) {
Map<String, Object> response = new HashMap<>();

try {

PatientEntity patient =
patientRepository.findById(examinationDTO.getOutPatientId())
.orElseThrow(() -> new IllegalArgumentException("Patient not
found with ID: " + examinationDTO.getOutPatientId()));

System.out.println("Patient found: " + patient);

Optional<OutPatientEntity> optionalOutPatient =
outPatientRepository.findByPatient_ID(patient.getID());
OutPatientEntity outPatient;

if (optionalOutPatient.isPresent()) {
outPatient = optionalOutPatient.get();
} else {
outPatient = new OutPatientEntity();
outPatient.setPatient(patient);

String generatedCode = String.format("OP%09d", patient.getID());


outPatient.setCode(generatedCode);
outPatientRepository.save(outPatient);
}

System.out.println("Examination DTO before saving: " +


examinationDTO);

examinationService.addExamination(examinationDTO);

response.put("message", "Thêm thành công !");


return new ResponseEntity<>(response, HttpStatus.CREATED);
} catch (Exception e) {
e.printStackTrace(); // Print stack trace for better debugging
response.put("message", "lỗi, thêm không thành công " +
e.getMessage());
return new ResponseEntity<>(response,
HttpStatus.INTERNAL_SERVER_ERROR);
}
}

- Update Exmination
@PostMapping("examination/edit")
@Transactional
public ResponseEntity<Map<String, Object>>
editExamination(@RequestParam("id") Integer examinationId, @RequestBody
ExaminationDTO examinationDTO) {
Map<String, Object> response = new HashMap<>();

// Ensure the received DTO has the correct ID


examinationDTO.setId(examinationId);

try {
// Call the service layer to update the examination
examinationService.updateExamination(examinationDTO);

// Construct success response


response.put("status", "success");
response.put("message", "Cập nhật thành công!");
response.put("examinationId", examinationId); // Optionally return
the examination ID

return ResponseEntity.ok(response);
} catch (Exception e) {
// Handle any errors
response.put("status", "error");
response.put("message", "Cập nhật không thành công: " +
e.getMessage());
return
ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(respo
nse);
}
}

- Examination Delete

@DeleteMapping("/examination/delete/{id}")
public ResponseEntity<Map<String, String>>
examination_del(@PathVariable Integer id) {
Map<String, String> response = new HashMap<>();

try {
int updated = examinationRepository.examination_del(id);

if (updated > 0) {
response.put("message", "Xóa thành công");
return new ResponseEntity<>(response, HttpStatus.OK);
} else {
response.put("message", "Đợt khám không tồn tại");
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
} catch (Exception e) {
response.put("message", "Không thành công: " + e.getMessage());
return new ResponseEntity<>(response,
HttpStatus.INTERNAL_SERVER_ERROR);
}
}

 Treatment

- Thêm mới Treatment


@PostMapping("/treatment/add")
public ResponseEntity<Map<String, Object>> addTreatment(@RequestBody
TreatmentDTO treatmentDTO) {
Map<String, Object> response = new HashMap<>();

try {
// Call the service layer to update the examination
treatmentService.addTreatment(treatmentDTO);

// Construct success response


response.put("status", "success");
response.put("message", "Thành công!");
return ResponseEntity.ok(response);
} catch (Exception e) {
// Handle any errors
response.put("status", "error");
response.put("message", "lỗi: " + e.getMessage());

return
ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response)
;
}
}

- Update Treatment
@PostMapping("/treatment/edit")
@Transactional
public ResponseEntity<Map<String, Object>>
editTreatment(@RequestParam("id") Integer treatmentId, @RequestBody
TreatmentDTO treatmentDTO) {
Map<String, Object> response = new HashMap<>();

treatmentDTO.setId(treatmentId);

try {

treatmentService.updateTreatment(treatmentDTO);
// Construct success response
response.put("status", "success");
response.put("message", "Cập nhật thành công!");
response.put("treatmentId", treatmentId); // Optionally return the
examination ID

return ResponseEntity.ok(response);
} catch (Exception e) {
// Handle any errors
response.put("status", "error");
response.put("message", "Lỗi " + e.getMessage());

return
ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response)
;
}
}

- Delete Treatment
@DeleteMapping("/treatment/delete/{id}")
public ResponseEntity<Map<String, String>> treatment_del(@PathVariable
Integer id) {
Map<String, String> response = new HashMap<>();

try {
int updated = treatmentRepository.treatment_del(id);

if (updated > 0) {
response.put("message", "Xóa thành công");
return new ResponseEntity<>(response, HttpStatus.OK);
} else {
response.put("message", "Quá trình điều trị không tồn tại");
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
} catch (Exception e) {
response.put("message", "Không thành công: " + e.getMessage());
return new ResponseEntity<>(response,
HttpStatus.INTERNAL_SERVER_ERROR);
}
}

You might also like