/*
 * 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.Insert;
import org.apache.ibatis.annotations.Options;
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.InfoPengiriman;
import sikd.sikd5.sikd5bean.tabel.UploadFile;

/**
 *
 * @author agus
 * @tanggal Nov 22, 2018
 */
public interface UploadFileMapper {

	@Insert("insert into public.uploadpdf (kodesatker, tahunanggaran, ikd, filename, tglupload,kodeikd,uploadke) "
			+ "values ("
			+ "#{kdsatker}, #{tahun}, #{ikd}, #{filename}, #{tglupload},#{kodeikd},#{uploadke})")
	@Options(useGeneratedKeys = true, keyProperty = "id")

	public void insert(UploadFile uploadFile);

	@Select("SELECT id, kodesatker, tahunanggaran, ikd, filename, tglupload, kodeikd,uploadke \n" +
			"FROM public.uploadpdf  where kodesatker=#{kodeSatker} and tahunanggaran=#{tahun} "
			+ " and kodeikd=#{kodeikd} order by uploadke desc")
	@Results({
			@Result(id = true, property = "id", column = "id"),
			@Result(property = "kdsatker", column = "kodesatker"),
			@Result(property = "tahun", column = "tahunanggaran"),
			@Result(property = "ikd", column = "ikd"),
			@Result(property = "filename", column = "filename"),
			@Result(property = "tglupload", column = "tglupload"),
			@Result(property = "kodeikd", column = "kodeikd"),
			@Result(property = "uploadke", column = "uploadke")
	})
	public List<UploadFile> getUploadFiles(@Param("kodeSatker") String kodeSatker, @Param("tahun") short tahun,
			@Param("kodeikd") String kodeikd);

	@Select("select * FROM public.uploadpdf WHERE id IN\n" +
			"  (SELECT id  FROM \n" +
			"    (SELECT id, ROW_NUMBER() OVER( PARTITION BY kodeikd\n" +
			"      ORDER BY  tglupload desc ) AS row_num \n" +
			"      FROM public.uploadpdf where kodesatker=#{kodeSatker} and tahunanggaran=#{tahun}) t \n" +
			"      WHERE t.row_num = 1 )")
	@Results({
			@Result(id = true, property = "id", column = "id"),
			@Result(property = "kdsatker", column = "kodesatker"),
			@Result(property = "tahun", column = "tahunanggaran"),
			@Result(property = "ikd", column = "ikd"),
			@Result(property = "filename", column = "filename"),
			@Result(property = "tglupload", column = "tglupload"),
			@Result(property = "kodeikd", column = "kodeikd"),
			@Result(property = "uploadke", column = "uploadke")
	})
	public List<UploadFile> getAllUploadFiles(@Param("kodeSatker") String kodeSatker, @Param("tahun") short tahun);

	@Select("select * FROM public.uploadpdf WHERE id IN\n" +
			"  (SELECT id  FROM \n" +
			"    (SELECT id, ROW_NUMBER() OVER( PARTITION BY kodesatker,kodeikd\n" +
			"      ORDER BY  tglupload desc ) AS row_num \n" +
			"      FROM public.uploadpdf where tahunanggaran=#{tahun}  and substring(right(kodeikd,2) from '[0-9]+')::int = #{bulan}"
			+ "     and tglupload <= #{tgakhir} and tglupload >= #{tgawal} ) t \n" +
			"      WHERE t.row_num = 1 )")
	@Results({
			@Result(id = true, property = "id", column = "id"),
			@Result(property = "kdsatker", column = "kodesatker"),
			@Result(property = "tahun", column = "tahunanggaran"),
			@Result(property = "ikd", column = "ikd"),
			@Result(property = "filename", column = "filename"),
			@Result(property = "tglupload", column = "tglupload"),
			@Result(property = "kodeikd", column = "kodeikd"),
			@Result(property = "uploadke", column = "uploadke")
	})
	public List<UploadFile> getAllUploadFilesPerPemda(@Param("tahun") short tahun,@Param("bulan") short bulan,
			@Param("tgawal") Date tgawal, @Param("tgakhir") Date tgakhir);

	@Select("select * FROM public.uploadpdf WHERE id IN\n" +
         "  (SELECT id  FROM \n" +
         "    (SELECT id, ROW_NUMBER() OVER( PARTITION BY kodesatker,kodeikd\n" +
         "      ORDER BY  tglupload desc ) AS row_num \n" +
         "      FROM public.uploadpdf where tahunanggaran=#{tahun}  and left(kodeikd,3) = #{kdikdSingkat}"
         + "     and tglupload <= #{tgakhir} ) t \n" +
         "      WHERE t.row_num = 1 )")
   @Results({
         @Result(id = true, property = "id", column = "id"),
         @Result(property = "kdsatker", column = "kodesatker"),
         @Result(property = "tahun", column = "tahunanggaran"),
         @Result(property = "ikd", column = "ikd"),
         @Result(property = "filename", column = "filename"),
         @Result(property = "tglupload", column = "tglupload"),
         @Result(property = "kodeikd", column = "kodeikd"),
         @Result(property = "uploadke", column = "uploadke")
   })
   public List<UploadFile> getUploadFilesPerPemda(@Param("tahun") short tahun, @Param("kdikdSingkat") String kdikdSingkat, @Param("tgakhir") Date tgakhir);
	
	@Select("with \n"
			+ "datanya as (select * from public.uploadpdf where kodesatker=#{kodeSatker} and tahunanggaran=#{tahun} and kodeikd=#{kodeikd} "
			+ " order by tglupload desc limit 1)\n"
			+ "select ('Informasi Pengiriman Pdf '::text||#{nmikd}::text)::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 = "tglupload"),
//			@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("kodeikd") String kodeikd, @Param("nmikd") String nmikd);
	
}
