package sikd.sikd5.sikd5ejb.mapper;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import sikd.sikd5.sikd5bean.StatusData;
import sikd.sikd5.sikd5bean.laporan.Lampiran1Apbd;

import sikd.sikd5.sikd5bean.query.InfoPengiriman;
import sikd.sikd5.sikd5bean.query.MonitorSimpatikDataKirimPemdaChart;
import sikd.sikd5.sikd5bean.query.MonitorSimpatikPemdaKoneksi;

/**
 * @author  : agus
 * @tanggal : Jan 10, 2019
 */

public interface LraMapper {


	@Select("select * FROM public.realisasiapbd WHERE apbdindex IN\n" + 
			" (SELECT apbdindex  FROM \n" + 
			"   (SELECT apbdindex, ROW_NUMBER() OVER( PARTITION BY bulan\n" + 
			"     ORDER BY  tglpengiriman desc ) AS row_num \n" + 
			"     FROM public.realisasiapbd where kodesatker=#{kodeSatker} and tahunanggaran=#{tahun} ) t \n" + 
			"     WHERE t.row_num = 1 )")
	@Results({ 
			@Result(property = "tahunAnggaran", column = "tahunanggaran"),
			@Result(property = "periode", column = "bulan"),
			@Result(property = "tglPengiriman", column = "tglpengiriman"),
			@Result(property = "statusData", column = "statusdata"),
			@Result(property = "jenisCoa", column = "jeniscoa"),
			@Result(property = "namaAplikasi", column = "namaaplikasi"),
			@Result(property = "pengembangAplikasi", column = "pengembangaplikasi"),
			})
	public List<InfoPengiriman> getInfoPengirimanXmlPerBulan(@Param("kodeSatker") String kodeSatker,
			@Param("tahun") short tahun);

	
	@Select("select * FROM public.logkirim WHERE logkirimindex IN\n" + 
			" (SELECT logkirimindex  FROM \n" + 
			"   (SELECT logkirimindex, ROW_NUMBER() OVER( PARTITION BY kodesatker\n" + 
			"     ORDER BY  tglpengiriman desc ) AS row_num \n" + 
			"     FROM public.logkirim where jenisdata='LRA' and tahunanggaran=#{tahun} and bulan=#{bulan} "
			+ "   and tglpengiriman >= #{tgawal} and tglpengiriman <= #{tgakhir} ) t \n" + 
			"     WHERE t.row_num = 1 )")
	@Results({ 
			@Result(property = "tahunAnggaran", column = "tahunanggaran"),
			@Result(property = "kodeSatker", column = "kodesatker"),
			@Result(property = "periode", column = "bulan"),
			@Result(property = "tglPengiriman", column = "tglpengiriman"),
			@Result(property = "statusData", column = "statusdata"),
			@Result(property = "jenisCoa", column = "jeniscoa"),
			@Result(property = "namaAplikasi", column = "namaaplikasi"),
			@Result(property = "pengembangAplikasi", column = "pengembangaplikasi"),
			})
	public List<InfoPengiriman> getInfoPengirimanXmlPerPemda(@Param("tahun") short tahun, @Param("bulan") short bulan,
			@Param("tgawal") Date tgawal, @Param("tgakhir") Date tgakhir);
        
        @Select("with peri as (\n"
               + " select 1 per union select 2 per union select 3 per union select 4 per union \n"
               + " select 5 per union select 6 per union select 7 per union select 8 per union \n"
               + " select 9 per union select 10 per union select 11 per union select 12 per \n"
               + "),\n"
               + "lra as( \n "
               + "  select EXTRACT(MONTH FROM tglpengiriman) b, count(distinct kodesatker) jum from realisasiapbd \n"
               + "  where EXTRACT(YEAR FROM tglpengiriman)=#{tahun} group by b \n"
               + ")\n"
               + " select per, CASE WHEN per=1 THEN 'Januari'\n"
            + "				 WHEN per=2 THEN 'Februari'\n"
            + "				 WHEN per=3 THEN 'Maret'\n"
            + "				 WHEN per=4 THEN 'April'\n"
            + "				 WHEN per=5 THEN 'Mei'\n"
            + "				 WHEN per=6 THEN 'Juni'\n"
            + "				 WHEN per=7 THEN 'Juli'\n"
            + "				 WHEN per=8 THEN 'Agustus'\n"
            + "				 WHEN per=9 THEN 'September'\n"
            + "				 WHEN per=10 THEN 'Oktober'\n"
            + "				 WHEN per=11 THEN 'November'\n"
            + "				 WHEN per=12 THEN 'Desember'\n"
            + "				 END \n"
            + "				 bln, coalesce(jum,0) jum  from peri \n"
            + "                 left join lra on peri.per=lra.b\n"
            + "                 order by per")
	@Results({ 
			@Result(property = "kode", column = "jum"),
			@Result(property = "nama", column = "bln"),
			})
	public List<StatusData> getLraPemda(@Param("tahun") short tahun);
        
        @Select("with kodes as(\n"
                + "	select distinct kodesatker, namapemda from transaksi.headerlog where EXTRACT(YEAR FROM tglpengiriman) = #{tahun} \n"
                + "),\n"
                + "a as(\n"
                + "	select kodesatker, max(tglpengiriman) tgl FROM transaksi.headerlog \n"
                + "	where EXTRACT(YEAR FROM tglpengiriman) = #{tahun} and jenisdata = 'Apbd' group by kodesatker\n"
                + "),\n"
                + "b as(\n"
                + "	select kodesatker, max(tglpengiriman) tgl FROM transaksi.headerlog \n"
                + "	where EXTRACT(YEAR FROM tglpengiriman) = #{tahun} and jenisdata = 'Jurnal' group by kodesatker\n"
                + "),\n"
                + "c as(\n"
                + "	select kodesatker, max(tglpengiriman) tgl FROM transaksi.headerlog \n"
                + "	where EXTRACT(YEAR FROM tglpengiriman) = #{tahun} and jenisdata = 'Dth' group by kodesatker\n"
                + ")\n"
                + "select kodes.kodesatker, kodes.namapemda, a.tgl ap, b.tgl lr, c.tgl dt from kodes \n"
                + "left join a on kodes.kodesatker = a.kodesatker\n"
                + "left join b on kodes.kodesatker = b.kodesatker\n"
                + "left join c on kodes.kodesatker = c.kodesatker			\n"
                + "            order by kodes.kodesatker"
            )
	@Results({
			@Result(property = "kodesatker", column = "kodesatker"),
			@Result(property = "nama", column = "namapemda"),
			@Result(property = "apbd", column = "ap"),
			@Result(property = "lra", column = "lr"),
			@Result(property = "dth", column = "dt")
			})
	public List<MonitorSimpatikPemdaKoneksi> getMonitorPemdaKoneksi(@Param("tahun") short tahun);
        
//        @Select("with peri as ( \n"
//            + "    select 1 per, date(#{tanggal}) tgl union select 2 per, date(#{tanggal})-Integer'1' tgl union \n"
//            + "    select 3 per, date(#{tanggal})-Integer'2' tgl union select 4 per, date(#{tanggal})-Integer'3' tgl union \n"
//            + "    select 5 per, date(#{tanggal})-Integer'4' tgl union select 6 per, date(#{tanggal})-Integer'5' tgl union \n"
//            + "	   select 7 per, date(#{tanggal})-Integer'6' tgl union select 8 per, date(#{tanggal})-Integer'7' tgl union \n"
//            + "    select 9 per, date(#{tanggal})-Integer'8' tgl union select 10 per, date(#{tanggal})-Integer'9' tgl \n"
//            + " ), \n"
//            + " lra as( \n"
//            + "     select date(tanggalbukti) b, count(distinct kodesatker) jumlra from transaksi.headerjurnal  \n"
//            + "     where EXTRACT(YEAR FROM tanggalbukti)=EXTRACT(YEAR FROM date(#{tanggal}))   group by b \n"
//            + " ),\n"
//            + " apb as( \n"
//            + "     select date(tglpengiriman) b, count(distinct kodesatker) jumap from transaksi.headerapbd  \n"
//            + "     where EXTRACT(YEAR FROM tglpengiriman)=EXTRACT(YEAR FROM date(#{tanggal}))   group by b \n"
//            + " ),\n"
//            + " dt as( \n"
//            + "     select date(tanggalsp2d) b, count(distinct kodesatker) jumdt from transaksi.headerdth \n"
//            + "     where EXTRACT(YEAR FROM tanggalsp2d)=EXTRACT(YEAR FROM date(#{tanggal}))   group by b \n"
//            + " )\n"
//            + " select per, \n"
//            + "	tgl, coalesce(jumlra,0) jumlra, coalesce(jumap,0) jumap, coalesce(jumdt,0) jumdt  from peri \n"
//            + " left join lra on peri.tgl=lra.b \n"
//            + "	left join apb on peri.tgl=apb.b \n"
//            + "	left join dt on peri.tgl=dt.b \n"
//            + " order by per")
        @Select("select * from transaksi.monitoring_data_terkirim_nasional(#{tanggal})")
	@Results({ 
			@Result(property = "no", column = "per"),
			@Result(property = "tanggal", column = "tgl"),
			@Result(property = "jumlahApbd", column = "jumlra"),
			@Result(property = "jumlahLra", column = "jumap"),
			@Result(property = "jumlahDth", column = "jumdt")
			})
	public List<MonitorSimpatikDataKirimPemdaChart> getMonitorDataKirimChartNasional(@Param("tanggal") Date tanggal);
        
        @Select("select * from transaksi.monitoring_data_terkirim_pemda(#{tanggal}, #{kodesatker})")
	@Results({ 
			@Result(property = "no", column = "per"),
			@Result(property = "tanggal", column = "tgl"),
			@Result(property = "jumlahApbd", column = "jumlra"),
			@Result(property = "jumlahLra", column = "jumap"),
			@Result(property = "jumlahDth", column = "jumdt")
			})
	public List<MonitorSimpatikDataKirimPemdaChart> getMonitorDataKirimChartPemda(@Param("tanggal") Date tanggal, @Param("kodesatker") String kodesatker);
}
