Update schedule tasks

Pembuatan pasien task : update nik pasien peserta bpjs
This commit is contained in:
Salman Manoe 2023-10-11 19:42:21 +07:00
parent 4ddd3446b6
commit a71b25f989
4 changed files with 149 additions and 8 deletions

View File

@ -1,22 +1,20 @@
package com.jasamedika.medifirst2000.dao;
import java.util.List;
import java.util.Map;
import javax.persistence.LockModeType;
import com.jasamedika.medifirst2000.entities.Alamat;
import com.jasamedika.medifirst2000.entities.Pasien;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.jasamedika.medifirst2000.entities.Alamat;
import com.jasamedika.medifirst2000.entities.Pasien;
import javax.persistence.LockModeType;
import java.util.List;
import java.util.Map;
/**
* Repository class for Pasien
*
*
* @author Askur
*/
@Repository("PasienDao")
@ -111,4 +109,11 @@ public interface PasienDao extends JpaRepository<Pasien, Integer> {
+ "and ibu.statusEnabled is true and anak.statusEnabled is true "
+ "and to_char(anak.tglLahir,'yyyy-MM-dd') between :tglAwal and :tglAkhir " + "order by ibu.namaPasien")
List<Map<String, Object>> findIbuAnak(@Param("tglAwal") String tglAwal, @Param("tglAkhir") String tglAkhir);
@Query(value = "select ps.* " + "from pasien_m ps " + "where ps.statusenabled is true "
+ "and (ps.nobpjs is not null " + "and trim(ps.nobpjs) <> '' " + "and ps.nobpjs <> '-' "
+ "and ps.nobpjs ~ '[0-9]+' " + "and length(ps.nobpjs) = 13) " + "and (ps.noidentitas is null "
+ "or length(ps.noidentitas) <> 16) "
+ "order by ps.statusenabled, ps.tgldaftar desc limit 300", nativeQuery = true)
List<Pasien> findByValidBpjs();
}

View File

@ -0,0 +1,20 @@
package com.jasamedika.medifirst2000.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author salmanoe
* @version 1.0.0
* @since 11/10/2023
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class PesertaBpjsDto {
private String noKartu;
private String nik;
}

View File

@ -0,0 +1,104 @@
package com.jasamedika.medifirst2000.task.schedule;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jasamedika.medifirst2000.dao.PasienDao;
import com.jasamedika.medifirst2000.dto.PesertaBpjsDto;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
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;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.springframework.http.HttpMethod.GET;
/**
* @author salmanoe
* @version 1.0.0
* @since 11/10/2023
*/
@Component
public class PasienTask {
private static final Logger LOGGER = LoggerFactory.getLogger(PasienTask.class);
@Autowired
private ObjectMapper objectMapper;
@Autowired
private RestTemplate restTemplate;
@Autowired
private PasienDao pasienDao;
@Scheduled(fixedRate = 1000 * 60 * 60 * 24)
public void nikPesertaBpjs() {
LOGGER.info("Start task PasienTask.nikPesertaBpjs {}", LocalDateTime.now());
adjustNikPesertaBpjs();
LOGGER.info("End task PasienTask.nikPesertaBpjs {}", LocalDateTime.now());
}
@Transactional
private void adjustNikPesertaBpjs() {
try {
LOGGER.info("Start adjusting NIK Peserta BPJS {}", LocalDateTime.now());
List<Pasien> pasienValidBpjs = pasienDao.findByValidBpjs();
for (Pasien pasien : pasienValidBpjs) {
PesertaBpjsDto pesertaBpjsDto = cekKepesertaan(pasien.getNoBpjs());
pasien.setNoIdentitas(pesertaBpjsDto.getNik());
Thread.sleep((long) (Math.random() * 1000 * 60 * 5));
}
pasienDao.save(pasienValidBpjs);
LOGGER.info("End adjusting NIK Peserta BPJS {}", LocalDateTime.now());
} catch (Exception e) {
LOGGER.error("Error adjusting NIK Peserta BPJS {}", e.getMessage());
}
}
private PesertaBpjsDto cekKepesertaan(String noKartu) {
try {
LOGGER.info("Start cek kepesertaan BPJS {} {}", noKartu, LocalDateTime.now());
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())) {
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);
LOGGER.info("End cek kepesertaan BPJS {} {}", noKartu, LocalDateTime.now());
return PesertaBpjsDto.builder().noKartu(responsePeserta.get("noKartu").toString())
.nik(responsePeserta.get("nik").toString()).build();
} else {
LOGGER.error("Error handshake cek kepesertaan BPJS {}", response.getStatusCode().getReasonPhrase());
throw new ServiceVOException(
"Error cek kepesertaan BPJS " + response.getStatusCode().getReasonPhrase());
}
} catch (Exception e) {
LOGGER.error("Error cek kepesertaan BPJS {}", e.getMessage());
throw new ServiceVOException("Error cek kepesertaan BPJS " + e.getMessage());
}
}
}

View File

@ -1,11 +1,13 @@
package com.jasamedika.medifirst2000.task.schedule.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.client.RestTemplate;
/**
* @author Salman
@ -24,4 +26,14 @@ public class ScheduleTaskConfig {
return threadPoolTaskScheduler;
}
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper();
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}