ohctechv3/.svn/pristine/36/367b678787b422cbbec61109e77d3d58c93e1c72.svn-base
2024-10-28 15:03:36 +05:30

184 lines
8.2 KiB
Plaintext

package com.healthcare.ohctech.controller;
import com.healthcare.ohctech.dto.SicknessDto;
import com.healthcare.ohctech.entity.Sickness;
import com.healthcare.ohctech.repository.AilmentSystemRepo;
import com.healthcare.ohctech.repository.OhcTypeRepo;
import com.healthcare.ohctech.repository.PatientRepo;
import com.healthcare.ohctech.service.impl.AuthServiceImpl;
import com.healthcare.ohctech.service.impl.SicknessServiceImpl;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.healthcare.ohctech.util.PaginationUtil;
import java.util.LinkedHashMap;
import java.util.Map;
@RestController
@RequestMapping("/sickness")
public class SicknessController {
@Autowired
private AuthServiceImpl authServiceImpl;
@Autowired
private SicknessServiceImpl sicknessServiceImpl;
@Autowired
private OhcTypeRepo ohcTypeRepo;
@Autowired
private AilmentSystemRepo ailmentSystemRepo;
@Autowired
private PatientRepo patientRepo;
@GetMapping
public ResponseEntity<?> getAllSicknesses(@RequestParam(required = false) Integer page,
@RequestParam(required = false) Integer size,
@RequestParam(required = false) String sortBy,
@RequestParam(required = false) String sortOrder) {
Pageable pageable = PaginationUtil.getPageableWithDefaults(page, size, sortBy, sortOrder);
Page<Sickness> sicknessPage = sicknessServiceImpl.getAllSicknesses(pageable);
Page<Map<String, Object>> sicknessDtoPage = sicknessPage.map(sickness -> {
// Get the ailmentSystem name from Patient entity
String ailmentName = ailmentSystemRepo.findById(sickness.getAilmentSystem().getId())
.map(patient -> patient.getAilmentSysName())
.orElse("Unknown Ailment");
// Get the patient name from Patient entity
String patientName = patientRepo.findById(sickness.getPatientId().getId())
.map(patient -> patient.getPatientName())
.orElse("Unknown Patient");
// Get the emp code from Patient entity
String empCode = patientRepo.findById(sickness.getPatientId().getId())
.map(patient -> patient.getEmpCode())
.orElse("Unknown Emp Code");
// Get the OHC Type name from OhcType entity
String ohcTypeName = ohcTypeRepo.findById(sickness.getOhcType().getId())
.map(ohcType -> ohcType.getOhcName())
.orElse("Unknown OHC Type");
// Prepare a map with the required fields
Map<String, Object> map = new LinkedHashMap<>();
map.put("sicknessId", sickness.getId());
map.put("empCode", empCode);
map.put("ailmentSystemName", ailmentName);
map.put("patientName", patientName); // derived from patientRepo
map.put("sicknessDate", sickness.getSicknessDate());
map.put("fromDate", sickness.getFromDate());
map.put("toDate", sickness.getToDate());
map.put("dateAbsent", sickness.getDateAbsent());
map.put("dateReturn", sickness.getDateReturn());
map.put("attendedStatus", sickness.getAttendedStatus());
map.put("agency", sickness.getAgency());
map.put("sicknessName", sickness.getSicknessName());
map.put("description", sickness.getDescription());
map.put("tokenNo", sickness.getTokenNo());
map.put("ohcTypeName", ohcTypeName); // derived from ohcTypeRepo
map.put("lastModified", sickness.getLastModified());
map.put("modifiedBy", sickness.getModifiedBy());
return map;
});
// Prepare the paginated response
Map<String, Object> response = PaginationUtil.getPageResponse(sicknessDtoPage);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@GetMapping("/{sicknessId}")
public ResponseEntity<?> getSicknessById(@PathVariable Long sicknessId) {
Sickness sickness = sicknessServiceImpl.getSicknessById(sicknessId);
Map<String, Object> ailmentDetails = ailmentSystemRepo.findById(sickness.getAilmentSystem().getId())
.map(ailmentSystem -> {
Map<String, Object> map = new LinkedHashMap<>();
map.put("id",ailmentSystem .getId());
map.put("name", ailmentSystem.getAilmentSysName());
return map;
})
.orElseGet(() -> {
Map<String, Object> map = new LinkedHashMap<>();
map.put("id", " ");
map.put("name", " ");
map.put("emp_code", " ");
return map;
});
Map<String, Object> patientDetails = patientRepo.findById(sickness.getPatientId().getId())
.map(patient -> {
Map<String, Object> map = new LinkedHashMap<>();
map.put("id",patient .getId());
map.put("name", patient.getPatientName());
map.put("emp_code", patient.getEmpCode());
return map;
})
.orElseGet(() -> {
Map<String, Object> map = new LinkedHashMap<>();
map.put("id", " ");
map.put("name", " ");
map.put("emp_code", " ");
return map;
});
Map<String, Object> ohcTypeDetails = ohcTypeRepo.findById(sickness.getOhcType().getId())
.map(ohcType -> {
Map<String, Object> map = new LinkedHashMap<>();
map.put("id", ohcType.getId());
map.put("label", ohcType.getOhcName());
return map;
})
.orElseGet(() -> {
Map<String, Object> map = new LinkedHashMap<>();
map.put("id", " ");
map.put("label", " ");
return map;
});
Map<String, Object> response = new LinkedHashMap<>();
response.put("id", sickness.getId());
response.put("sicknessDate", sickness.getSicknessDate());
response.put("fromDate", sickness.getFromDate());
response.put("toDate", sickness.getToDate());
response.put("dateAbsent", sickness.getDateAbsent());
response.put("dateReturn", sickness.getDateReturn());
response.put("attendedStatus", sickness.getAttendedStatus());
response.put("agency", sickness.getAgency());
response.put("sicknessName", sickness.getSicknessName());
response.put("description", sickness.getDescription());
response.put("tokenNo", sickness.getTokenNo());
response.put("ohcType", ohcTypeDetails);
response.put("ailmentSystem", ailmentDetails);
response.put("patient", patientDetails);
response.put("lastModified", sickness.getLastModified());
response.put("modifiedBy", sickness.getModifiedBy());
return new ResponseEntity<>(response, HttpStatus.OK);
}
@PostMapping
public ResponseEntity<?> addSickness(@RequestBody SicknessDto sicknessDto ) {
Long userId = authServiceImpl.getCurrentUserId();
sicknessServiceImpl.addSickness(sicknessDto,userId);
return new ResponseEntity<>(HttpStatus.CREATED);
}
@PutMapping("/{sicknessId}")
public ResponseEntity<?> updateSickness(@Valid @RequestBody SicknessDto sicknessDto) {
Long userId = authServiceImpl.getCurrentUserId();
sicknessServiceImpl.updateSickness(sicknessDto,userId);
return new ResponseEntity<>(HttpStatus.OK);
}
@DeleteMapping("/{sicknessId}")
public ResponseEntity<?> deleteSickness(@PathVariable Long sicknessId) {
sicknessServiceImpl.deleteSickness(sicknessId);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}