package com.jasamedika.medifirst2000.dao;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.entities.SkoringTindakanPerawat;

@Repository("skoringTindakanPerawatDao")
public interface SkoringTindakanPerawatDao extends PagingAndSortingRepository<SkoringTindakanPerawat, String> {

	String strAllEnabled = "select new Map(stp.noRec as noRec," + "stp.statusEnabled as statusEnabled,"
			+ "stp.skor as skor," + "stp.statusVerifikasi as kdVerif,"
			+ "(case when stp.statusVerifikasi is false then 'Belum Verifikasi' "
			+ "when stp.statusVerifikasi is true then 'Terverifikasi' end) as stVerif,"
			+ "stp.tanggalMulaiBerlaku as tglMulaiBerlaku," + "stp.tanggalPembaharuanData as tglPembaharuanData,"
			+ "prp.id as produkId,prp.namaProduk as namaProduk," + "prp.klasifikasi as kdKlasif,"
			+ "(case when prp.klasifikasi = 0 then 'Tidak Ada' " + "when prp.klasifikasi = 1 then 'Asuhan' "
			+ "when prp.klasifikasi = 2 then 'Tindakan' " + "when prp.klasifikasi = 3 then 'Laporan' end) as stKlasif) "
			+ "from SkoringTindakanPerawat stp " + "inner join stp.produk prp " + "where stp.statusEnabled is true "
			+ "and to_char(stp.tanggalMulaiBerlaku,'yyyy-MM-dd') <= to_char(now(),'yyyy-MM-dd')";

	String produk = " and lower(prp.namaProduk) like %:namaProduk%";

	String statusVerifikasi = " and stp.statusVerifikasi = :isVerif";

	String statusKlasifikasi = " and prp.klasifikasi = :kdKlasif";

	String sortSearch = " order by prp.namaProduk asc, "
			+ "stp.tanggalMulaiBerlaku desc, stp.tanggalPembaharuanData desc";

	@Query(strAllEnabled + sortSearch)
	List<Map<String, Object>> findAllEnabled();

	@Query(strAllEnabled + produk + sortSearch)
	List<Map<String, Object>> findAllEnabledByProduk(@Param("namaProduk") String namaProduk);

	@Query(strAllEnabled + statusVerifikasi + sortSearch)
	List<Map<String, Object>> findAllEnabledByVerif(@Param("isVerif") Boolean isVerif);

	@Query(strAllEnabled + statusKlasifikasi + sortSearch)
	List<Map<String, Object>> findAllEnabledByKlasif(@Param("kdKlasif") Short kdKlasif);

	@Query(strAllEnabled + produk + statusVerifikasi + sortSearch)
	List<Map<String, Object>> findAllEnabledByProdukVerif(@Param("namaProduk") String namaProduk,
			@Param("isVerif") Boolean isVerif);

	@Query(strAllEnabled + produk + statusKlasifikasi + sortSearch)
	List<Map<String, Object>> findAllEnabledByProdukKlasif(@Param("namaProduk") String namaProduk,
			@Param("kdKlasif") Short kdKlasif);

	@Query(strAllEnabled + statusVerifikasi + statusKlasifikasi + sortSearch)
	List<Map<String, Object>> findAllEnabledByVerifKlasif(@Param("isVerif") Boolean isVerif,
			@Param("kdKlasif") Short kdKlasif);

	@Query(strAllEnabled + produk + statusVerifikasi + statusKlasifikasi + sortSearch)
	List<Map<String, Object>> findAllEnabledByProdukVerifKlasif(@Param("namaProduk") String namaProduk,
			@Param("isVerif") Boolean isVerif, @Param("kdKlasif") Short kdKlasif);

	String strCekDupSkoring = "select stp.noRec " + "from SkoringTindakanPerawat stp " + "inner join stp.produk prp "
			+ "where stp.statusEnabled is true "
			+ "and lower(regexp_replace(prp.namaProduk,:patternText,'','g')) = :namaProduk";

	String skor = " and stp.skor = :skor";

	String tglBerlaku = " and to_char(stp.tanggalMulaiBerlaku,'yyyy-MM-dd') = :tglMulaiBerlaku";

	String idSkoring = " and stp.noRec <> :noRec";

	String sortTglBerlaku = " order by stp.tanggalMulaiBerlaku desc";

	@Query(strCekDupSkoring + skor + sortTglBerlaku)
	List<String> findLastDupSkoring(@Param("namaProduk") String namaProduk, @Param("skor") Double skor,
			@Param("patternText") String patternText);

	@Query(strCekDupSkoring + tglBerlaku)
	List<String> findDupSkoring(@Param("namaProduk") String namaProduk,
			@Param("tglMulaiBerlaku") String tglMulaiBerlaku, @Param("patternText") String patternText);

	@Query(strCekDupSkoring + skor + idSkoring + sortTglBerlaku)
	List<String> findLastDupSkoring(@Param("namaProduk") String namaProduk, @Param("skor") Double skor,
			@Param("patternText") String patternText, @Param("noRec") String noRec);

	@Query(strCekDupSkoring + tglBerlaku + idSkoring)
	List<String> findDupSkoring(@Param("namaProduk") String namaProduk,
			@Param("tglMulaiBerlaku") String tglMulaiBerlaku, @Param("patternText") String patternText,
			@Param("noRec") String noRec);

	@Query("select new Map(stp.noRec as noRec,stp.skor as skor," + "prp.namaProduk as namaProduk) "
			+ "from SkoringTindakanPerawat stp " + "inner join stp.produk prp " + "where stp.statusEnabled is true "
			+ "and stp.noRec in (:listNorec)")
	List<Map<String, Object>> findAllDup(@Param("listNorec") List<String> listNorec);

	@Query(value = "select * from sdm_skoringtindakanperawat_t "
			+ "where statusenabled is true and statusverifikasi is true "
			+ "and objectprodukperawatfk = :produkId and to_char(tanggalmulaiberlaku,'yyyy-MM-dd') <= :tglMulaiBerlaku "
			+ "order by tanggalmulaiberlaku desc, tanggalpembaharuandata desc limit 1", nativeQuery = true)
	SkoringTindakanPerawat findLast(@Param("produkId") Integer idProduk,
			@Param("tglMulaiBerlaku") String tglMulaiBerlaku);

}
