Update PasienTask.java
Penerapan penggunaan kdprofile dan penyesuaian scheduler untuk tengah malem
This commit is contained in:
parent
6d3a1ecc42
commit
ca1a915123
@ -9,7 +9,6 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
@ -18,12 +17,12 @@ import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.net.URI;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.springframework.http.HttpMethod.GET;
|
||||
import static org.springframework.http.HttpStatus.CREATED;
|
||||
import static org.springframework.http.HttpStatus.OK;
|
||||
|
||||
/**
|
||||
* @author salmanoe
|
||||
@ -43,7 +42,7 @@ public class PasienTask {
|
||||
@Autowired
|
||||
private PasienDao pasienDao;
|
||||
|
||||
@Scheduled(fixedRate = 1000 * 60 * 60 * 24)
|
||||
@Scheduled(cron = "0 0 0 * * *")
|
||||
public void nikPesertaBpjs() {
|
||||
LOGGER.info("Task PasienTask.nikPesertaBpjs {}", LocalDateTime.now());
|
||||
|
||||
@ -52,26 +51,58 @@ public class PasienTask {
|
||||
|
||||
@Transactional
|
||||
private void adjustNikPesertaBpjs() {
|
||||
try {
|
||||
LOGGER.info("Adjusting NIK Peserta BPJS {}", LocalDateTime.now());
|
||||
LOGGER.info("Adjusting NIK Peserta BPJS {}", LocalDateTime.now());
|
||||
|
||||
List<Pasien> listPasien = new ArrayList<>();
|
||||
List<Pasien> pasienValidBpjs = pasienDao.findByValidBpjs();
|
||||
for (Pasien pasien : pasienValidBpjs) {
|
||||
List<PesertaBpjsDto> listDtoUpdate = new ArrayList<>();
|
||||
List<PesertaBpjsDto> listDtoNotUpdate = new ArrayList<>();
|
||||
List<Pasien> pasienValidBpjs = pasienDao.findByValidBpjs();
|
||||
for (Pasien pasien : pasienValidBpjs) {
|
||||
try {
|
||||
PesertaBpjsDto pesertaBpjsDto = cekKepesertaan(pasien.getNoBpjs());
|
||||
if (pesertaBpjsDto != null) {
|
||||
Pasien pasien_ = new Pasien();
|
||||
pasien_.setId(pasien.getId());
|
||||
pasien_.setNoIdentitas(pesertaBpjsDto.getNik());
|
||||
listPasien.add(pasien_);
|
||||
}
|
||||
if (pesertaBpjsDto.getKdProfile() == 0)
|
||||
listDtoUpdate.add(pesertaBpjsDto);
|
||||
if (pesertaBpjsDto.getKdProfile() == 1)
|
||||
listDtoNotUpdate.add(pesertaBpjsDto);
|
||||
|
||||
Thread.sleep((long) (Math.random() * 1000 * 8));
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Error adjusting NIK Peserta BPJS {} -> {}", pasien.getNoBpjs(), e.getMessage());
|
||||
}
|
||||
}
|
||||
{
|
||||
List<String> listNoKartu = listDtoUpdate.stream().map(PesertaBpjsDto::getNoKartu)
|
||||
.collect(Collectors.toList());
|
||||
List<Pasien> availableValid = pasienValidBpjs.stream()
|
||||
.filter(pasien -> listNoKartu.contains(pasien.getNoBpjs())).collect(Collectors.toList());
|
||||
availableValid.forEach(pasien -> {
|
||||
Optional<PesertaBpjsDto> first = listDtoUpdate.stream()
|
||||
.filter(dto -> pasien.getNoBpjs().equals(dto.getNoKartu())).findFirst();
|
||||
first.ifPresent(dto -> {
|
||||
pasien.setKdProfile(dto.getKdProfile());
|
||||
pasien.setNoIdentitas(dto.getNik());
|
||||
});
|
||||
});
|
||||
if (CommonUtil.isNotNullOrEmpty(availableValid)) {
|
||||
LOGGER.info("Save available valid data");
|
||||
|
||||
pasienDao.save(availableValid);
|
||||
}
|
||||
}
|
||||
{
|
||||
List<String> listNoKartu = listDtoNotUpdate.stream().map(PesertaBpjsDto::getNoKartu)
|
||||
.collect(Collectors.toList());
|
||||
List<Pasien> availableValid = pasienValidBpjs.stream()
|
||||
.filter(pasien -> listNoKartu.contains(pasien.getNoBpjs())).collect(Collectors.toList());
|
||||
availableValid.forEach(pasien -> {
|
||||
Optional<PesertaBpjsDto> first = listDtoNotUpdate.stream()
|
||||
.filter(dto -> pasien.getNoBpjs().equals(dto.getNoKartu())).findFirst();
|
||||
first.ifPresent(dto -> pasien.setKdProfile(dto.getKdProfile()));
|
||||
});
|
||||
if (CommonUtil.isNotNullOrEmpty(availableValid)) {
|
||||
LOGGER.info("Flag kdProfile can not update");
|
||||
|
||||
pasienDao.save(availableValid);
|
||||
}
|
||||
if (CommonUtil.isNotNullOrEmpty(listPasien))
|
||||
pasienDao.save(listPasien);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Error adjusting NIK Peserta BPJS {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@ -82,20 +113,23 @@ public class PasienTask {
|
||||
URI uri = new URI("https://daftar.rsabhk.co.id/api/data/pasien/jaminan/bpjs/cek-kepesertaan/" + noKartu);
|
||||
HttpEntity<Object> request = new HttpEntity<>(null);
|
||||
ResponseEntity<Object> response = restTemplate.exchange(uri, GET, request, Object.class);
|
||||
if (HttpStatus.OK.equals(response.getStatusCode())) {
|
||||
if (OK.equals(response.getStatusCode()) || CREATED.equals(response.getStatusCode())) {
|
||||
Map<String, Object> responseBody = objectMapper.convertValue(response.getBody(), Map.class);
|
||||
Map<String, Object> responseMap = objectMapper.convertValue(responseBody.get("response"),
|
||||
HashMap.class);
|
||||
Map<String, Object> responsePeserta = objectMapper.convertValue(responseMap.get("peserta"),
|
||||
HashMap.class);
|
||||
return PesertaBpjsDto.builder().noKartu(responsePeserta.get("noKartu").toString())
|
||||
.nik(responsePeserta.get("nik").toString()).build();
|
||||
.nik(responsePeserta.get("nik").toString()).kdProfile((short) 0).build();
|
||||
} else {
|
||||
LOGGER.error("Error handshake cek kepesertaan BPJS {}", response.getStatusCode().getReasonPhrase());
|
||||
|
||||
return PesertaBpjsDto.builder().noKartu(noKartu).kdProfile((short) 1).build();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Error cek kepesertaan BPJS {}", e.getMessage());
|
||||
|
||||
return PesertaBpjsDto.builder().noKartu(noKartu).kdProfile((short) 1).build();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user