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.DashboardChartValue;
import sikd.sikd5.sikd5bean.DashboardValue;
import sikd.sikd5.sikd5bean.StatusData;

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

/**
 * @author  : soraturaes
 */

public interface HomeMapper {

//        @Select("with aa as(\n"
//            + "	select \n"
//            + "kodeakunutama, saldonormal, bln,\n"
//            + "case when saldonormal='D' then debet-kredit\n"
//            + "     when saldonormal='K' then kredit-debet\n"
//            + "     end\n"
//            + "nilairealisasi\n"
//            + "from\n"
//            + "(select  a.kodesatker, a.kodepemda, a.namapemda, a.tahunanggaran, a.jeniscoa, a.tanggalbukti,  extract(month from a.tanggalbukti) bln,\n"
//            + "b.kodeakunutama, b.saldonormal,\n"
//            + "sum(b.nilaidebet) debet, sum(b.nilaikredit) kredit\n"
//            + "from transaksi.headerjurnal a\n"
//            + "join transaksi.jurnal b on a.headerjurnalid=b.headerjurnalid\n"
//            + "where b.kodeakunutama in('4','5') and \n"
//            + " a.tahunanggaran=#{tahun} and extract(month from a.tanggalbukti) <= #{bulan} \n"
//            + "group by  a.kodesatker, a.kodepemda, a.namapemda, a.tahunanggaran, a.jeniscoa, a.tanggalbukti, bln,  b.saldonormal,\n"
//            + "b.kodeakunutama, b.namaakunutama, b.kodeakunkelompok, b.namaakunkelompok, b.kodeakunjenis, b.namaakunjenis \n"
//            + "order by b.kodeakunutama, b.kodeakunkelompok, b.kodeakunjenis) RE\n"
//            + "), \n"
//            + "bb as(\n"
//            + "   select kodeakunutama kode, bln per, sum(nilairealisasi) nilai from aa group by kodeakunutama, bln\n"
//            + "),\n"
//            + "sem as(\n"
//            + "	select distinct per, sum(n4) n4, sum(n5) n5 from (\n"
//            + "	select distinct per, sum(nilai) n4, 0::double precision n5 from bb where kode = '4' group by per\n"
//            + "	union\n"
//            + "	select distinct per, 0::double precision n4, sum(nilai) n5 from bb where kode = '5' group by per order by per\n"
//            + "	) itu group by per\n"
//            + "),\n"
//            + "blns as(\n"
//            + "	select 1::smallint per, 'Jan'::text bln\n"
//            + "	union select 2::smallint per, 'Feb'::text bln\n"
//            + "	union select 3::smallint per, 'Mar'::text bln\n"
//            + "	union select 4::smallint per, 'Apr'::text bln\n"
//            + "	union select 5::smallint per, 'Mei'::text bln\n"
//            + "	union select 6::smallint per, 'Jun'::text bln\n"
//            + "	union select 7::smallint per, 'Jul'::text bln\n"
//            + "	union select 8::smallint per, 'Agt'::text bln\n"
//            + "	union select 9::smallint per, 'Sep'::text bln\n"
//            + "	union select 10::smallint per, 'Okt'::text bln\n"
//            + "	union select 11::smallint per, 'Nov'::text bln\n"
//            + "	union select 12::smallint per, 'Des'::text bln\n"
//            + ")\n"
//            + "select blns.per per, blns.bln bulan, coalesce(n4,0) pendapatan, coalesce(n5,0) belanja\n"
//            + "from blns left join sem on blns.per=sem.per\n"
//            + "order by blns.per")
        @Select("with ap as(\n" 
            + "	select * from transaksi.pendapatan_belanja_apbd_nasional_year(#{tahun})\n"
            + ")\n"
            + "select per, bulan, \n"
            + " CASE WHEN ap.pendapatan > 0 THEN (lra.pendapatan/ap.pendapatan)*100\n"
            + " ELSE "
            + "     CASE WHEN lra.pendapatan > 0 THEN 100 "
            + "     ELSE 0 "
            + "     END "
            + " END pendapatan,\n"
            + " CASE WHEN ap.belanja > 0 THEN (lra.belanja/ap.belanja)*100\n"
            + " ELSE "
            + "     CASE WHEN lra.belanja > 0 THEN 100 "
            + "     ELSE 0 "
            + "     END "
            + " END belanja \n"
            + "from transaksi.pendapatan_belanja_lra_nasional_monthly(#{tahun},#{bulan}) lra, ap")
	@Results({ 
			@Result(property = "per", column = "per"),
			@Result(property = "bulan", column = "bulan"),
			@Result(property = "pendapatan", column = "pendapatan"),
			@Result(property = "belanja", column = "belanja"),
			})
	public List<DashboardChartValue> getDashboardNasionalChartValue(@Param("tahun") short tahun, @Param("bulan") short bulan);
        
//        @Select("with aa as(\n"
//            + "	select \n"
//            + "kodeakunutama, saldonormal, bln,\n"
//            + "case when saldonormal='D' then debet-kredit\n"
//            + "     when saldonormal='K' then kredit-debet\n"
//            + "     end\n"
//            + "nilairealisasi\n"
//            + "from\n"
//            + "(select  a.kodesatker, a.kodepemda, a.namapemda, a.tahunanggaran, a.jeniscoa, a.tanggalbukti,  extract(month from a.tanggalbukti) bln,\n"
//            + "b.kodeakunutama, b.saldonormal,\n"
//            + "sum(b.nilaidebet) debet, sum(b.nilaikredit) kredit\n"
//            + "from transaksi.headerjurnal a\n"
//            + "join transaksi.jurnal b on a.headerjurnalid=b.headerjurnalid\n"
//            + "where b.kodeakunutama in('4','5') and \n"
//            + " a.tahunanggaran=#{tahun} and kodesatker=#{kodesatker} and extract(month from a.tanggalbukti) <= #{bulan} \n"
//            + "group by  a.kodesatker, a.kodepemda, a.namapemda, a.tahunanggaran, a.jeniscoa, a.tanggalbukti, bln,  b.saldonormal,\n"
//            + "b.kodeakunutama, b.namaakunutama, b.kodeakunkelompok, b.namaakunkelompok, b.kodeakunjenis, b.namaakunjenis \n"
//            + "order by b.kodeakunutama, b.kodeakunkelompok, b.kodeakunjenis) RE\n"
//            + "), \n"
//            + "bb as(\n"
//            + "   select kodeakunutama kode, bln per, sum(nilairealisasi) nilai from aa group by kodeakunutama, bln\n"
//            + "),\n"
//            + "sem as(\n"
//            + "	select distinct per, sum(n4) n4, sum(n5) n5 from (\n"
//            + "	select distinct per, sum(nilai) n4, 0::double precision n5 from bb where kode = '4' group by per\n"
//            + "	union\n"
//            + "	select distinct per, 0::double precision n4, sum(nilai) n5 from bb where kode = '5' group by per order by per\n"
//            + "	) itu group by per\n"
//            + "),\n"
//            + "blns as(\n"
//            + "	select 1::smallint per, 'Jan'::text bln\n"
//            + "	union select 2::smallint per, 'Feb'::text bln\n"
//            + "	union select 3::smallint per, 'Mar'::text bln\n"
//            + "	union select 4::smallint per, 'Apr'::text bln\n"
//            + "	union select 5::smallint per, 'Mei'::text bln\n"
//            + "	union select 6::smallint per, 'Jun'::text bln\n"
//            + "	union select 7::smallint per, 'Jul'::text bln\n"
//            + "	union select 8::smallint per, 'Agt'::text bln\n"
//            + "	union select 9::smallint per, 'Sep'::text bln\n"
//            + "	union select 10::smallint per, 'Okt'::text bln\n"
//            + "	union select 11::smallint per, 'Nov'::text bln\n"
//            + "	union select 12::smallint per, 'Des'::text bln\n"
//            + ")\n"
//            + "select blns.per per, blns.bln bulan, coalesce(n4,0) pendapatan, coalesce(n5,0) belanja\n"
//            + "from blns left join sem on blns.per=sem.per\n"
//            + "order by blns.per")
        @Select("with ap as(\n"
            + "	select * from transaksi.pendapatan_belanja_apbd_pemda_year(#{tahun}, #{kodesatker})\n"
            + ")\n"
            + "select per, bulan, \n"
            + " CASE WHEN ap.pendapatan > 0 THEN (lra.pendapatan/ap.pendapatan)*100\n"
            + " ELSE "
            + "     CASE WHEN lra.pendapatan > 0 THEN 100 "
            + "     ELSE 0 "
            + "     END "
            + " END pendapatan,\n"
            + " CASE WHEN ap.belanja > 0 THEN (lra.belanja/ap.belanja)*100\n"
            + " ELSE "
            + "     CASE WHEN lra.belanja > 0 THEN 100 "
            + "     ELSE 0 "
            + "     END "
            + " END belanja \n"            
            + "from transaksi.pendapatan_belanja_lra_pemda_monthly(#{tahun}, #{kodesatker}, #{bulan}) lra, ap")
	@Results({ 
			@Result(property = "per", column = "per"),
			@Result(property = "bulan", column = "bulan"),
			@Result(property = "pendapatan", column = "pendapatan"),
			@Result(property = "belanja", column = "belanja"),
			})
	public List<DashboardChartValue> getDashboardChartValue(@Param("tahun") short tahun, @Param("bulan") short bulan, @Param("kodesatker") String kodesatker);
        
        @Select(" select * from transaksi.rangking_persen_belanja_nasional(#{tahun}, #{bulan})")
	@Results({             
			@Result(property = "bulan", column = "namapemda"),
			@Result(property = "pendapatan", column = "realisasi"),
			@Result(property = "belanja", column = "persen"),
			})
	public List<DashboardChartValue> getRangkingPemdaNasional(@Param("tahun") short tahun, @Param("bulan") short bulan);
        
        @Select(" select * from transaksi.rangking_persen_belanja_pemda(#{tahun}, #{bulan}, #{kodesatker})")
	@Results({             
			@Result(property = "bulan", column = "namaskpd"),
			@Result(property = "pendapatan", column = "realisasi"),
			@Result(property = "belanja", column = "persen"),
			})
	public List<DashboardChartValue> getRangkingSkpdPemda(@Param("tahun") short tahun, @Param("bulan") short bulan, @Param("kodesatker") String kodesatker);
        
        
        @Select("with qry as(\n"
            + "	select * from transaksi.dasboard_nasional_Level2(#{tahun}, #{bulan})\n"
            + "),\n"
            + "smua as(\n"
            + "     select kode, \n"
            + "     case when kode = '5' then (nilai + (select coalesce(nilai,0) from qry where kode= '6'))\n"
            + "     else nilai END \n"
            + "     nilai, \n"
            + "     case when kode = '5' then (ap + (select coalesce(ap,0) from qry where kode= '6'))\n"
            + "     else ap END ap\n"
            + "     FROM qry \n"
            + ")\n"
            + " select kode, nilai, \n"
            + " CASE WHEN ap > 0 THEN (nilai/ap)*100\n"
            + " ELSE \n"
            + "     CASE WHEN nilai > 0 THEN 100 \n"
            + "     ELSE 0 \n"
            + "     END \n"
            + " END persen\n"
            + " from smua order by kode")
	@Results({ 
			@Result(property = "bulan", column = "kode"),
			@Result(property = "pendapatan", column = "nilai"),
			@Result(property = "belanja", column = "persen"),
			})
	public List<DashboardChartValue> getDashboardNasionalValue(@Param("tahun") short tahun, @Param("bulan") short bulan);
        
        @Select("with qry as(\n"
            + "	select * from transaksi.dasboard_pemda_Level2(#{tahun}, #{bulan}, #{kodesatker})\n"
            + "),\n"
            + "	smua as(\n"
            + "     select kode, \n"
            + "     case when kode = '5' then (nilai + (select coalesce(nilai,0) from qry where kode= '6'))\n"
            + "     else nilai END \n"
            + "     nilai, \n"
            + "     case when kode = '5' then (ap + (select coalesce(ap,0) from qry where kode= '6'))\n"
            + "     else ap END ap\n"
            + "     FROM qry \n"
            + " )\n"
            + "	select kode, nilai, \n"
            + " CASE WHEN ap > 0 THEN (nilai/ap)*100\n"
            + " ELSE \n"
            + "     CASE WHEN nilai > 0 THEN 100 \n"
            + "     ELSE 0 \n"
            + "     END \n"
            + " END persen\n"
            + " from smua order by kode")
	@Results({ 
			@Result(property = "bulan", column = "kode"),
			@Result(property = "pendapatan", column = "nilai"),
			@Result(property = "belanja", column = "persen"),
			})
	public List<DashboardChartValue> getDashboardPemdaValue(@Param("tahun") short tahun, @Param("bulan") short bulan, @Param("kodesatker") String kodesatker);
}
