/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
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.AmbilKontakPemdaBean;
import sikd.sikd5.sikd5bean.query.AmbilPosisiKasBean;
import sikd.sikd5.sikd5bean.query.InfoPengiriman;
import sikd.sikd5.sikd5bean.ws.simpatik.KontakPemda_WS;

/**
 *
 * @author agus
 * @tanggal Nov 6, 2018
 */
public interface KontakPemdaMapper {

   final String insert = "INSERT INTO simpatik.kontakpemda\n "
         + "(kodesatker, kodepemda, namapemda, tahunanggaran, bulan, tglpengiriman, statusdata, namaaplikasi, pengembangaplikasi,"
         + " uploadke, versisinergi, nama1, jabatan1, hp1, email1, nama2, jabatan2, hp2, email2, nmfile)\n"
         + "VALUES("
         + "#{kt.kodeSatker}, #{kt.kodePemda}, #{kt.namaPemda}, #{kt.tahunAnggaran}, #{kt.bulan}, #{tglPengiriman}, #{kt.statusData}, #{kt.namaAplikasi}, #{kt.pengembangAplikasi},"
         + " #{uploadke}, #{kt.versiSinergi}, #{kt.nama1}, #{kt.jabatan1}, #{kt.hp1}, #{kt.email1}, #{kt.nama2}, #{kt.jabatan2}, #{kt.hp2}, #{kt.email2}, #{nmfile}"
         + ") returning kontakpemdaindex";

//    @Insert({"insert into simpatik.kontakpemda (kontakpemdaindex, kodesatker, ", "kodepemda, namapemda, ",
//        "tahunanggaran, bulan, ", "tglpengiriman, statusdata, ", "namaaplikasi, pengembangaplikasi, ",
//        "uploadke, versisinergi, ", "nama1, jabatan1, ", "hp1, email1, nama2, ", "jabatan2, hp2, email2, ",
//        "nmfile)", "values (#{kontakpemdaindex,jdbcType=BIGINT}, #{kodesatker,jdbcType=CHAR}, ",
//        "#{kodepemda,jdbcType=CHAR}, #{namapemda,jdbcType=VARCHAR}, ",
//        "#{tahunanggaran,jdbcType=SMALLINT}, #{bulan,jdbcType=SMALLINT}, ",
//        "#{tglpengiriman,jdbcType=TIMESTAMP}, #{statusdata,jdbcType=SMALLINT}, ",
//        "#{namaaplikasi,jdbcType=VARCHAR}, #{pengembangaplikasi,jdbcType=VARCHAR}, ",
//        "#{uploadke,jdbcType=SMALLINT}, #{versisinergi,jdbcType=VARCHAR}, ",
//        "#{nama1,jdbcType=VARCHAR}, #{jabatan1,jdbcType=VARCHAR}, ",
//        "#{hp1,jdbcType=VARCHAR}, #{email1,jdbcType=VARCHAR}, #{nama2,jdbcType=VARCHAR}, ",
//        "#{jabatan2,jdbcType=VARCHAR}, #{hp2,jdbcType=VARCHAR}, #{email2,jdbcType=VARCHAR}, ",
//        "#{nmfile,jdbcType=VARCHAR})"})
//            @Options(useGeneratedKeys=true,keyProperty="userId")
   @Select(insert)
   public long insert(@Param("kt") KontakPemda_WS kt, @Param("tglPengiriman") Date tglPengiriman, @Param("uploadke") int uploadke,
         @Param("nmfile") String nmfile);

   @Select("with \n"
         + "datanya as (select * from simpatik.kontakpemda where kodesatker=#{kodeSatker} and tahunanggaran=#{tahun} and "
         + "bulan=#{bulan} order by tglpengiriman desc limit 1)\n"
         + "select 'Informasi Pengiriman Data Kontak Pemda'::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(id = true, property = "userId", column = "user_id"),
         @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")
   })
   public InfoPengiriman getInfoPengiriman(@Param("kodeSatker") String kodeSatker, @Param("tahun") short tahun, @Param("bulan") short bulan);

   @Select("with indexnya as "
         + " (SELECT kontakpemdaindex  FROM \n" +
         "   (SELECT kontakpemdaindex, ROW_NUMBER() OVER( PARTITION BY kodesatker\n" +
         "     ORDER BY  tglpengiriman desc ) AS row_num \n" +
         "     FROM simpatik.kontakpemda where tahunanggaran=#{tahun} and trim(nama1)<>'' ) t \n" +
         "     WHERE t.row_num = 1 ),"
         + "kontaknya as "
         + "(SELECT * FROM simpatik.kontakpemda where kontakpemdaindex in "
         + "  (select kontakpemdaindex from indexnya)),"
         + "provnya as "
         + "(SELECT pemdaindex, tahunanggaran, kdsatker, kdprov, kdpemda, urpemda\n" +
         "   FROM public.tpemda where tahunanggaran=#{tahun} and kdpemda='00') "
         + "select c.tahunanggaran tahun, d.kdprov kdprovinsi,d.urpemda urprovinsi,c.kdpemda,c.kdsatker kdsatker,"
         + "c.urpemda,a.* "
         + "from kontaknya a "
         + "right join public.tpemda c on a.kodesatker=c.kdsatker "
         + "right join provnya d on c.kdprov=d.kdprov "
         + "where c.tahunanggaran=#{tahun} and c.kdprov<>'00' "
         + " order by d.kdprov,c.kdpemda")
   @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 = "nama1", column = "nama1"),
         @Result(property = "jabatan1", column = "jabatan1"),
         @Result(property = "hp1", column = "hp1"),
         @Result(property = "email1", column = "email1"),
         @Result(property = "nama2", column = "nama2"),
         @Result(property = "jabatan2", column = "jabatan2"),
         @Result(property = "hp2", column = "hp2"),
         @Result(property = "email2", column = "email2")
   })
   public List<AmbilKontakPemdaBean> ambilKontakPemda(@Param("tahun") short tahun);
}
