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.AmbilPosisiKasBean;
import sikd.sikd5.sikd5bean.query.InfoPengiriman;
import sikd.sikd5.sikd5bean.tabel.simpatik.Posisikas;
import sikd.sikd5.sikd5bean.tabel.simpatik.Posisikasakun;
import sikd.sikd5.sikd5bean.tabel.simpatik.Posisikasakunrinci;
import sikd.sikd5.sikd5bean.ws.simpatik.PosisiKasAkunRinci_WS;
import sikd.sikd5.sikd5bean.ws.simpatik.PosisiKasAkun_WS;
import sikd.sikd5.sikd5bean.ws.simpatik.PosisiKas_WS;

/**
 *
 * @author : agus
 * @tanggal : Nov 12, 2018
 */
public interface PosisiKasMapper {
   @Select("with \n"
         + "datanya as (select * from simpatik.posisikas where kodesatker=#{kodeSatker} and tahunanggaran=#{tahun} and "
         + "bulan=#{bulan} order by tglpengiriman desc limit 1)\n"
         + "select 'Informasi Pengiriman Data Posisi Kas'::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.posisikas\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 posisikasindex")

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

   @Select("INSERT INTO simpatik.posisikasakun \n" +
         "(posisikasindex,kdakun, nilai) \n" +
         "VALUES (#{posisikasindex},#{akun.kdAkun},#{akun.nilai}) returning posisikasakunindex")

   public long insertPosisiKasAkun(@Param("posisikasindex") long posisikasindex,
         @Param("akun") PosisiKasAkun_WS posisiKasAkun_WS);

   @Select("INSERT INTO simpatik.posisikasakunrinci\n" +
         "(posisikasakunindex, tahun, nilai)\n" +
         "VALUES(#{posisikasakunindex}, #{rinci.tahun}, #{rinci.nilai}) returning posisikasakunrinciindex")

   public long insertPosisiKasAkunRinci(@Param("posisikasakunindex") long posisikasakunindex,
         @Param("rinci") PosisiKasAkunRinci_WS posisiKasAkunRinci_WS);

   @Select("select * FROM simpatik.posisikas WHERE posisikasindex IN\n" +
         " (SELECT posisikasindex  FROM \n" +
         "   (SELECT posisikasindex, ROW_NUMBER() OVER( PARTITION BY bulan\n" +
         "     ORDER BY  tglpengiriman desc ) AS row_num \n" +
         "     FROM simpatik.posisikas 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.posisikas WHERE posisikasindex IN\n" +
         " (SELECT posisikasindex  FROM \n" +
         "   (SELECT posisikasindex, ROW_NUMBER() OVER( PARTITION BY kodesatker\n" +
         "     ORDER BY  tglpengiriman desc ) AS row_num \n" +
         "     FROM simpatik.posisikas where 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 = "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.posisikas where kodesatker=#{kodeSatker} and tahunanggaran=#{tahun} and "
         + "bulan=#{bulan} order by tglpengiriman desc limit 1")
   @Results({
         @Result(property = "posisikasindex", column = "posisikasindex"),
         @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 Posisikas getPosisiKasTerbaru(@Param("kodeSatker") String kodeSatker, @Param("tahun") short tahun,
         @Param("bulan") short bulan);

   @Select("SELECT posisikasakunindex, posisikasindex, kdakun, nilai, parentindex FROM simpatik.posisikasakun "
         + "where posisikasindex=#{posisikasindex} order by kdakun")
   @Results({
         @Result(property = "posisikasakunindex", column = "posisikasakunindex"),
         @Result(property = "kdakun", column = "kdakun"),
         @Result(property = "nilai", column = "nilai"),
         @Result(property = "posisikasakunrincis", column = "posisikasakunindex", javaType = List.class, many = @Many(select = "getPosisiKasAkunRinci"))
   })
   public List<Posisikasakun> getPosisiKasAkun(@Param("posisikasindex") long posisikasindex);

   @Select("SELECT posisikasakunrinciindex, posisikasakunindex, tahun, nilai FROM simpatik.posisikasakunrinci "
         + "where posisikasakunindex=#{posisikasakunindex} order by tahun")
   @Results({
         @Result(property = "posisikasakunrinciindex", column = "posisikasakunrinciindex"),
         @Result(property = "tahun", column = "tahun"),
         @Result(property = "nilai", column = "nilai")
   })
   public List<Posisikasakunrinci> getPosisiKasAkunRinci(@Param("posisikasakunindex") long posisikasakunindex);
//SELECT posisikasakunindex, kdakun, nilai FROM simpatik.posisikasakun
   
   @Select("with indexnya as "
         + " (SELECT posisikasindex  FROM \n" +
         "   (SELECT posisikasindex, ROW_NUMBER() OVER( PARTITION BY kodesatker\n" +
         "     ORDER BY  tglpengiriman desc ) AS row_num \n" +
         "     FROM simpatik.posisikas where tahunanggaran=#{tahun} and bulan=#{bulan} "
         + "    and tglpengiriman <= #{tgakhir}) t \n" +
         "     WHERE t.row_num = 1 ),"
         + "akunnya as "
         + "(SELECT posisikasakunindex,posisikasindex, kdakun, nilai FROM simpatik.posisikasakun where posisikasindex in "
         + "  (select posisikasindex 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,a.kdakun,e.urakun,a.nilai,a.posisikasakunindex "
         + "from akunnya a "
         + "left join simpatik.posisikas b on a.posisikasindex=b.posisikasindex "
         + "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 = "nilai", column = "nilai"),
         @Result(property = "rincis", column = "posisikasakunindex", javaType = List.class, many = @Many(select = "getPosisiKasAkunRinci"))
         })
   public List<AmbilPosisiKasBean> ambilPosisiKasPerPemda(@Param("tahun") short tahun, @Param("bulan") short bulan,
         @Param("tgakhir") Date tgakhir, @Param("inAkun") String[] inAkun);
   @Select("with indexnya as "
	         + " (SELECT posisikasindex  FROM \n" +
	         "   (SELECT posisikasindex, ROW_NUMBER() OVER( PARTITION BY kodesatker\n" +
	         "     ORDER BY  tglpengiriman desc ) AS row_num \n" +
	         "     FROM simpatik.posisikas where tahunanggaran=#{tahun} and bulan=#{bulan} "
	         + "    and tglpengiriman <= #{tgakhir}) t \n" +
	         "     WHERE t.row_num = 1 ),"
	         + "akunnya as "
	         + "(SELECT posisikasakunindex,posisikasindex, kdakun, nilai FROM simpatik.posisikasakun where posisikasindex in "
	         + "  (select posisikasindex 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,a.kdakun,e.urakun,a.nilai,a.posisikasakunindex "
	         + "from akunnya a "
	         + "left join simpatik.posisikas b on a.posisikasindex=b.posisikasindex "
	         + "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 = "nilai", column = "nilai"),
	         })
	   public List<AmbilPosisiKasBean> ambilPosisiKasGakRinciPerPemda(@Param("tahun") short tahun, @Param("bulan") short bulan,
	         @Param("tgakhir") Date tgakhir, @Param("inAkun") String[] inAkun);
}
