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.PelayananNakes;

/**
 * @author salmanoe
 * @since Oct 18, 2021
 */
@Repository("pelayananNakesDao")
public interface PelayananNakesDao extends PagingAndSortingRepository<PelayananNakes, String> {

	@Query("select pn from PelayananNakes pn " + "where pn.statusEnabled is true "
			+ "and to_char(pn.tglPelayanan,'yyyy-MM') = :bulan " + "and pn.pegawaiId = :pegawaiId")
	List<PelayananNakes> findAllByKontrak(@Param("bulan") String bulan, @Param("pegawaiId") Integer idPegawai);

	@Query("select new Map(pnt.noRec as noRec," + "pnt.tglPelayanan as tglPelayanan,"
			+ "pnt.skor as skor,pnt.jumlah as jumlah," + "pnt.skor*pnt.jumlah as tSkor," + "pnt.catatan as detail,"
			+ "pn.id as produkId,pn.namaProduk as namaProduk," + "pn.kdProduk as kdProduk,"
			+ "(case when pn.kdProduk = 1 then 'Pelayanan Pasien' "
			+ "when pn.kdProduk = 2 then 'Pelayanan Non-Pasien' end) as kodeProduk,"
			+ "pg.id as pegawaiId,pg.namaLengkap as namaLengkap) " + "from PelayananNakes pnt "
			+ "inner join pnt.produk pn " + "inner join pnt.pegawai pg " + "where pnt.statusEnabled is true "
			+ "and pg.statusEnabled is true " + "and to_char(pnt.tglPelayanan,'yyyy-MM') = :bulan "
			+ "and pnt.pegawaiId = :pegawaiId " + "order by pn.namaProduk, pnt.tglPelayanan")
	List<Map<String, Object>> findPelayananNakes(@Param("bulan") String bulan, @Param("pegawaiId") Integer idPegawai);

	@Query("select new Map(pnt.tglPelayanan as tglPelayanan,"
			+ "to_char(pnt.tglPelayanan,'yyyy-MM-dd HH24:MI:ss') as tglPelayananFormatted," + "pnt.jumlah as jumlah,"
			+ "pnt.catatan as catatan) " + "from PelayananNakes pnt " + "inner join pnt.pegawai pg "
			+ "inner join pnt.produk pn " + "where pnt.statusEnabled is true " + "and pn.statusEnabled is true "
			+ "and pg.statusEnabled is true " + "and pg.id = :pegawaiId " + "and pn.id = :produkId "
			+ "and to_char(pnt.tglPelayanan,'yyyy-MM-dd') = :tglPelayanan")
	List<Map<String, Object>> findPelayananNakesDetail(@Param("pegawaiId") Integer idPegawai,
			@Param("produkId") Integer idProduk, @Param("tglPelayanan") String tglPelayanan);

}
