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

/**
 * Repository class for SatuanStandar
 * 
 * @author adik
 */
@Repository("SatuanStandarDao")
public interface SatuanStandarDao extends PagingAndSortingRepository<SatuanStandar, Integer> {

	@Query("select model from SatuanStandar model where model.id=:id")
	public SatuanStandar findById(@Param("id") Integer id);

	// Loundry - Sarpras
	@Query("select ss from SatuanStandar ss where ss.id in (Select cast(s.nilaiField as int) from SettingDataFixed s where s.namaField='KdSatuanStandarLoundry')")
	public List<SatuanStandar> findSatuanStandar();

	// Loundry - Sarpras
	@Query("select ss from SatuanStandar ss where ss.id in (Select cast(s.nilaiField as int) from SettingDataFixed s where s.namaField='KdSatuanStandarLoundryKg')")
	public List<SatuanStandar> findSatuanStandarKg();

	@Query("select count(p.id) from SatuanStandar p ")
	public Integer getCount();

	// Set Alat Cssd - Sarpras
	@Query("select ss from SatuanStandar ss where ss.id in (Select cast(s.nilaiField as int) from SettingDataFixed s where s.namaField='KdSatuanStandarCssd')")
	public List<SatuanStandar> findSatuanStandarBuah();

	// Penerimaan Sterilisasi Alat Internal Cssd - Sarpras
	// public List<SatuanStandar> findSatuanStandarCssd();

	@Query("select new Map(model.id as id, model.satuanStandar as satuanStandar, model.departemenId as departemenId, model.kelompokProdukId as kelompokProdukId, "
			+ "model.satuanStandarId as satuanStandarId) from SatuanStandar model where model.id = :satuanStandarId and model.statusEnabled is true")
	public Map<String, Object> getSatuanStandarById(@Param("satuanStandarId") Integer satuanStandarId);

	@Query(value = "select distinct on (satuanstandar) id as id, satuanstandar as satuanstandar from satuanstandar_m where statusenabled is true order by satuanstandar", nativeQuery = true)
	public List<Object[]> getAll();

}
