package com.jasamedika.medifirst2000.dao;

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

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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.Alamat;

/**
 * Repository class for Alamat
 * 
 * @author Askur
 */
@Repository("AlamatDao")
public interface AlamatDao extends PagingAndSortingRepository<Alamat, Integer> {
	@Query("select model from Alamat model where model.pasien.namaPasien=:nama")
	public List<Alamat> findAlamatBynamaPasien(@Param("nama") String nama);

	@Query("select model from Alamat model where model.pasien.namaPasien=:nama")
	public List<Alamat> findAlamatByNamaNoCm(@Param("nama") String nama);

	@Query("select model from Alamat model where model.pasien.id=:id")
	public Alamat findAlamatByPasienId(@Param("id") Integer id);

	@Query("select model from Alamat model where model.pasien.namaPasien=:nama")
	public Page<Alamat> findPageAlamatBynamaPasien(@Param("nama") String nama, Pageable Page);

	@Query("select new map(model.id as id, model.pasien.namaPasien as nama) from Alamat model")
	public List<Map<String, String>> getMapAlamat();

	@Query(value = "SELECT " + "alamatlengkap AS NAMA " + "FROM Alamat", nativeQuery = true)
	List<Object[]> getNativeAlamat();

	@Query("select p from Alamat p left join fetch p.desaKelurahan d where p.pasienId=:id")
	public List<Alamat> findAlamatByIdPasien(@Param("id") Integer id);

	@Query("select new Map(a.alamatLengkap as alamat," + "p.namaPropinsi as namaprop,"
			+ "b.namaKotaKabupaten as namadati2," + "c.namaKecamatan as namakec," + "d.namaDesaKelurahan as namakel) "
			+ "from Alamat a " + "left join a.propinsi p " + "left join a.kotaKabupaten b " + "left join a.kecamatan c "
			+ "left join a.desaKelurahan d " + "where a.statusEnabled is true " + "and a.pasien.noCm = :noCm")
	List<Map<String, Object>> findAlamatByNoRM(@Param("noCm") String noCm);

	@Query("select new Map(a.alamatLengkap as alamatLengkap, " + "a.rtrw as rtrw, "
			+ "a.namaDesaKelurahan as desaKelurahan, " + "a.namaKecamatan as kecamatan, "
			+ "a.namaKotaKabupaten as kotaKabupaten, " + "p.namaPropinsi as namaPropinsi) " + "from Alamat a "
			+ "left join a.desaKelurahan dk " + "left join a.propinsi p " + "where a.pasien.id=:patientId")
	Map<String, Object> findFullAddressByPatientId(@Param("patientId") Integer pasienId);

}
