package sikd.sikd5.sikd5ejb.mapper;

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

import org.apache.ibatis.annotations.Many;
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.query.AmbilPerkiraanBelanjaBean;
import sikd.sikd5.sikd5bean.query.AmbilPosisiKasBean;
import sikd.sikd5.sikd5bean.query.InfoPengiriman;
import sikd.sikd5.sikd5bean.tabel.simpatik.Perkiraanbelanja;
import sikd.sikd5.sikd5bean.tabel.simpatik.Perkiraanbelanjaakun;
import sikd.sikd5.sikd5bean.tabel.simpatik.Posisikas;
import sikd.sikd5.sikd5bean.tabel.simpatik.Posisikasakun;
import sikd.sikd5.sikd5bean.ws.simpatik.PerkiraanBelanjaAkun_WS;
import sikd.sikd5.sikd5bean.ws.simpatik.PerkiraanBelanja_WS;

/**
 *
 * @author : agus
 * @tanggal : Nov 12, 2018
 */
public interface PerkiraanBelanjaMapper {

   @Select("with \n"
         + "datanya as (select * from simpatik.perkiraanbelanja where kodesatker=#{kodeSatker} and tahunanggaran=#{tahun} and "
         + "bulan=#{bulan} order by tglpengiriman desc limit 1)\n"
         + "select 'Informasi Pengiriman Data Perkiraan Belanja '::text jenisinfo,'bulan '::text namaperiode, a.urpemda,b.* from public.tpemda a \n"
         + "left join datanya b on a.kdsatker=b.kodesatker\n"
         + "where a.kdsatker=#{kodeSatker} and a.tahunanggaran=#{tahun}")
   @Results({ @Result(property = "jenisInfo", column = "jenisinfo"),
         @Result(property = "namaPemda", column = "namapemda"),
         @Result(property = "tahunAnggaran", column = "tahunanggaran"),
         @Result(property = "periode", column = "bulan"),
         @Result(property = "namaPeriode", column = "namaperiode"),
         @Result(property = "tglPengiriman", column = "tglpengiriman"),
         @Result(property = "statusData", column = "statusdata"),
         @Result(property = "kodeData", column = "kodedata"),
         @Result(property = "jenisCoa", column = "jeniscoa"),
         @Result(property = "namaAplikasi", column = "namaaplikasi"),
         @Result(property = "pengembangAplikasi", column = "pengembangaplikasi"),
         @Result(property = "versiSinergi", column = "versisinergi"),
         @Result(property = "uploadKe", column = "uploadke"),
         @Result(property = "penyusunanKe", column = "penyusunanke") })
   public InfoPengiriman getInfoPengiriman(@Param("kodeSatker") String kodeSatker, @Param("tahun") short tahun,
         @Param("bulan") short bulan);

   @Select("INSERT INTO simpatik.perkiraanbelanja \n"
         + "(kodesatker, kodepemda, namapemda, tahunanggaran, bulan, tglpengiriman, statusdata, namaaplikasi, \n"
         + "pengembangaplikasi, uploadke, versisinergi, nmfile,penyusunanke)\n"
         + "VALUES("
         + "#{kt.kodeSatker}, #{kt.kodePemda}, #{kt.namaPemda}, #{kt.tahunAnggaran}, #{kt.bulan}, #{tglPengiriman}, #{kt.statusData}, #{kt.namaAplikasi},\n"
         + "#{kt.pengembangAplikasi},#{uploadke}, #{kt.versiSinergi},#{nmfile},#{kt.penyusunanKe}) returning perkiraanbelanjaindex")

   public long insertPerkiraanBelanja(@Param("kt") PerkiraanBelanja_WS kt, @Param("tglPengiriman") Date tglPengiriman,
         @Param("uploadke") int uploadke, @Param("nmfile") String nmfile);

   @Select("INSERT INTO simpatik.perkiraanbelanjaakun\n" +
         "(perkiraanbelanjaindex, kdakun, "
         + "nilaib1, nilaib2, nilaib3, nilaib4, nilaib5, nilaib6, nilaib7, nilaib8, "
         + "nilaib9, nilaib10, nilaib11, nilaib12, parentindex) \n"
         + "VALUES(#{perkiraanbelanjaindex}, #{akun.kdAkun},"
         + "#{akun.nilaib1}, #{akun.nilaib2}, #{akun.nilaib3}, #{akun.nilaib4}, #{akun.nilaib5}, #{akun.nilaib6}, #{akun.nilaib7},#{akun.nilaib8},"
         + "#{akun.nilaib9}, #{akun.nilaib10}, #{akun.nilaib11}, #{akun.nilaib12}, 0) returning perkiraanbelanjaakunindex \n")

   public long insertPerkiraanBelanjaAkun(@Param("perkiraanbelanjaindex") long perkiraanbelanjaindex,
         @Param("akun") PerkiraanBelanjaAkun_WS perkiraanBelanjaAkun_WS);

   @Select("select * FROM simpatik.perkiraanbelanja WHERE perkiraanbelanjaindex IN\n" +
         " (SELECT perkiraanbelanjaindex  FROM \n" +
         "   (SELECT perkiraanbelanjaindex, ROW_NUMBER() OVER( PARTITION BY bulan\n" +
         "     ORDER BY  tglpengiriman desc ) AS row_num \n" +
         "     FROM simpatik.perkiraanbelanja 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 = "namaAplikasi", column = "namaaplikasi"),
         @Result(property = "pengembangAplikasi", column = "pengembangaplikasi"),
         @Result(property = "versiSinergi", column = "versisinergi"),
         @Result(property = "uploadKe", column = "uploadke") })
   public List<InfoPengiriman> getInfoPengirimanXmlPerBulan(@Param("kodeSatker") String kodeSatker,
         @Param("tahun") short tahun);

   @Select("select * FROM simpatik.perkiraanbelanja WHERE perkiraanbelanjaindex IN\n" +
         " (SELECT perkiraanbelanjaindex  FROM \n" +
         "   (SELECT perkiraanbelanjaindex, ROW_NUMBER() OVER( PARTITION BY kodesatker\n" +
         "     ORDER BY  tglpengiriman desc ) AS row_num \n" +
         "     FROM simpatik.perkiraanbelanja where tahunanggaran=#{tahun} and bulan=#{bulan} \n" +
         "	  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 = "namaAplikasi", column = "namaaplikasi"),
         @Result(property = "pengembangAplikasi", column = "pengembangaplikasi"),
         @Result(property = "versiSinergi", column = "versisinergi"),
         @Result(property = "uploadKe", column = "uploadke") })
   public List<InfoPengiriman> getInfoPengirimanXmlPerPemda(@Param("tahun") short tahun, @Param("bulan") short bulan,
         @Param("tgawal") Date tgawal, @Param("tgakhir") Date tgakhir);

   @Select("select * from simpatik.perkiraanbelanja where kodesatker=#{kodeSatker} and tahunanggaran=#{tahun} and "
         + "bulan=#{bulan} order by tglpengiriman desc limit 1")
   @Results({
         @Result(property = "perkiraanbelanjaindex", column = "perkiraanbelanjaindex"),
         @Result(property = "kodesatker", column = "kodesatker"),
         @Result(property = "kodepemda", column = "kodepemda"),
         @Result(property = "tglpengiriman", column = "tglpengiriman"),
         @Result(property = "uploadke", column = "uploadke"),
         @Result(property = "namapemda", column = "namapemda") })
   public Perkiraanbelanja getPerkiraanBelanjaTerbaru(@Param("kodeSatker") String kodeSatker, @Param("tahun") short tahun,
         @Param("bulan") short bulan);

   @Select("SELECT perkiraanbelanjaakunindex, perkiraanbelanjaindex, kdakun, nilaib1, nilaib2, nilaib3, nilaib4, nilaib5, nilaib6, nilaib7, "
         + "nilaib8, nilaib9, nilaib10, nilaib11, nilaib12, parentindex\n" +
         "FROM simpatik.perkiraanbelanjaakun \n"
         + "where perkiraanbelanjaindex=#{perkiraanbelanjaindex}")
   @Results({
         @Result(property = "perkiraanbelanjaakunindex", column = "perkiraanbelanjaakunindex"),
         @Result(property = "kdakun", column = "kdakun"),
         @Result(property = "nilaib1", column = "nilaib1"),
         @Result(property = "nilaib2", column = "nilaib2"),
         @Result(property = "nilaib3", column = "nilaib3"),
         @Result(property = "nilaib4", column = "nilaib4"),
         @Result(property = "nilaib5", column = "nilaib5"),
         @Result(property = "nilaib6", column = "nilaib6"),
         @Result(property = "nilaib7", column = "nilaib7"),
         @Result(property = "nilaib8", column = "nilaib8"),
         @Result(property = "nilaib9", column = "nilaib9"),
         @Result(property = "nilaib10", column = "nilaib10"),
         @Result(property = "nilaib11", column = "nilaib11"),
         @Result(property = "nilaib12", column = "nilaib12")
   })
   public List<Perkiraanbelanjaakun> getPerkiraanBelanjaAkun(@Param("perkiraanbelanjaindex") long perkiraanbelanjaindex);

   @Select("with indexnya as "
         + " (SELECT perkiraanbelanjaindex  FROM \n" +
         "   (SELECT perkiraanbelanjaindex, ROW_NUMBER() OVER( PARTITION BY kodesatker\n" +
         "     ORDER BY  tglpengiriman desc ) AS row_num \n" +
         "     FROM simpatik.perkiraanbelanja where tahunanggaran=#{tahun} and bulan=#{bulan} "
         + "    and tglpengiriman <= #{tgakhir}) t \n" +
         "     WHERE t.row_num = 1 ),"
         + "akunnya as "
         + "(SELECT * FROM simpatik.perkiraanbelanjaakun where perkiraanbelanjaindex in "
         + "  (select perkiraanbelanjaindex from indexnya) and left(kdakun,1)=ANY(#{inAkun})),"
         + "provnya as "
         + "(SELECT pemdaindex, tahunanggaran, kdsatker, kdprov, kdpemda, urpemda\n" +
         "   FROM public.tpemda where tahunanggaran=#{tahun} and kdpemda='00') "
         + "select b.tahunanggaran tahun, b.bulan,d.kdprov kdprovinsi,d.urpemda urprovinsi,c.kdpemda,b.kodesatker kdsatker,"
         + "c.urpemda,e.urakun,a.* "
         + "from akunnya a "
         + "left join simpatik.perkiraanbelanja b on a.perkiraanbelanjaindex=b.perkiraanbelanjaindex "
         + "left join public.tpemda c on b.kodesatker=c.kdsatker "
         + "left join provnya d on c.kdprov=d.kdprov "
         + "left join simpatik.refsimpatik e on a.kdakun=e.kdakun "
         + "where c.tahunanggaran=#{tahun} and e.tahunanggaran=#{tahun} "
         + " order by d.kdprov,c.kdpemda,a.kdakun")
   @Results({
         @Result(property = "tahun", column = "tahun"),
         @Result(property = "bulan", column = "bulan"),
         @Result(property = "kdprovinsi", column = "kdprovinsi"),
         @Result(property = "urprovinsi", column = "urprovinsi"),
         @Result(property = "kdpemda", column = "kdpemda"),
         @Result(property = "kdsatker", column = "kdsatker"),
         @Result(property = "urpemda", column = "urpemda"),
         @Result(property = "kdakun", column = "kdakun"),
         @Result(property = "urakun", column = "urakun"),
         @Result(property = "nilaib1", column = "nilaib1"),
         @Result(property = "nilaib2", column = "nilaib2"),
         @Result(property = "nilaib3", column = "nilaib3"),
         @Result(property = "nilaib4", column = "nilaib4"),
         @Result(property = "nilaib5", column = "nilaib5"),
         @Result(property = "nilaib6", column = "nilaib6"),
         @Result(property = "nilaib7", column = "nilaib7"),
         @Result(property = "nilaib8", column = "nilaib8"),
         @Result(property = "nilaib9", column = "nilaib9"),
         @Result(property = "nilaib10", column = "nilaib10"),
         @Result(property = "nilaib11", column = "nilaib11"),
         @Result(property = "nilaib12", column = "nilaib12")
   })
   public List<AmbilPerkiraanBelanjaBean> ambilPerkiraanBelanjaPerPemda(@Param("tahun") short tahun, @Param("bulan") short bulan,
         @Param("tgakhir") Date tgakhir, @Param("inAkun") String[] inAkun);

}
