package sikd.sikd5.sikd5ejb.mapper.transaksi;

import java.util.Date;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
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 org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;
import sikd.sikd5.sikd5bean.ws.transaksi.Apbd_WS;
import sikd.sikd5.sikd5bean.ws.transaksi.Headerapbd_WS;

/**
 * @oleh : soraturaes
 */
@SuppressWarnings("el-syntax")
public interface HeaderapbdMapper {

    @Delete({
        "delete from transaksi.apbd ",
        "where headerapbdid in (select headerapbdid from apbd where kodesatker = #{record.kodesatker} and tahunanggaran = #{record.tahunanggaran} and kodedata = #{record.kodedata} and jeniscoa = #{record.jeniscoa}) "
    })
    int deleteApbdByPemdaTahunKodeDataCoa(@Param("record") Headerapbd_WS record);

    @Delete({
        "delete from transaksi.apbd ",
        "where headerapbdid  = #{headerid} "
    })
    int deleteApbdByHeaderApbdId(@Param("headerid") Long headerid);
    
    @Delete({
        "delete from transaksi.apbd ",
        "where headerapbdid  = #{headerid} AND namaskpd = #{namaskpd}"
    })
    int deleteApbdByHeaderApbdIdSkpd(@Param("headerid") Long headerid, @Param("namaskpd") String namaskpd);

    @Select({
        "INSERT INTO transaksi.headerapbd(",
        "kodesatker, kodepemda, namapemda, tahunanggaran, tglpengiriman, kodedata, statusdata, jeniscoa, nomorperda, tanggalperda, namaaplikasi, pengembangaplikasi, versisinergi, nmfile, sha256) ",
        "values (#{record.kodesatker,jdbcType=VARCHAR},#{record.kodepemda,jdbcType=VARCHAR}, #{record.namapemda,jdbcType=VARCHAR}, ",
        "#{record.tahunanggaran,jdbcType=INTEGER}, #{tanggalpengiriman}, ",
        "#{record.kodedata,jdbcType=INTEGER}, #{record.statusdata,jdbcType=INTEGER}, #{record.jeniscoa,jdbcType=INTEGER}, ",
        "#{record.nomorperda,jdbcType=VARCHAR}, #{record.tanggalperda,jdbcType=TIMESTAMP}, ",
        "#{record.namaaplikasi,jdbcType=VARCHAR}, #{record.pengembangaplikasi,jdbcType=VARCHAR}, ",
        "#{record.versisinergi,jdbcType=VARCHAR}, #{record.nmfile,jdbcType=VARCHAR}, ",
        "#{record.sha256,jdbcType=CHAR})",
        "returning headerapbdid"
    })
    long insertHeaderApbd(@Param("record") Headerapbd_WS record, @Param("tanggalpengiriman") Date tanggalpengiriman);

    @Update({
        "UPDATE transaksi.headerapbd ",
        " SET tglpengiriman=#{tanggalpengiriman}, statusdata=#{record.statusdata,jdbcType=INTEGER}, ",
        " nomorperda=#{record.nomorperda,jdbcType=VARCHAR}, tanggalperda=#{record.tanggalperda,jdbcType=TIMESTAMP}, ",
        " versisinergi=#{record.versisinergi,jdbcType=VARCHAR}, nmfile=#{record.nmfile,jdbcType=VARCHAR}, ",
        " sha256=#{record.sha256,jdbcType=CHAR} ",
        " WHERE headerapbdid = #{headerapbdid,jdbcType=BIGINT}"
    })
    int updateHeaderApbd(@Param("headerapbdid") long headerapbdid, @Param("record") Headerapbd_WS record, @Param("tanggalpengiriman") Date tanggalpengiriman);

    @Select({
        "select",
        "headerapbdid ",
        "from transaksi.headerapbd",
        "where ",
        "kodesatker=#{kodesatker,jdbcType=VARCHAR} and tahunanggaran=#{tahunanggaran,jdbcType=INTEGER} ",
        " and kodedata=#{kodedata,jdbcType=INTEGER} and jeniscoa=#{jeniscoa,jdbcType=INTEGER} "})
    @Results({
        @Result(column = "headerapbdid", property = "headerapbdid", jdbcType = JdbcType.BIGINT, id = true)
    })
    long selectHeaderApbdid(@Param("tahunanggaran") int tahunanggaran,
            @Param("kodesatker") String kodesatker, @Param("kodedata") int kodedata, @Param("jeniscoa") int jeniscoa);
    
    @Select({
        "select ",
        "kodesatker, kodepemda, namapemda, tahunanggaran, tglpengiriman, kodedata, statusdata, jeniscoa, ",
        "nomorperda, tanggalperda, namaaplikasi, pengembangaplikasi, versisinergi, nmfile, sha256 ",
        "from transaksi.headerapbd ",
        "where ",
        "kodesatker=#{kodesatker,jdbcType=VARCHAR} and tahunanggaran=#{tahunanggaran,jdbcType=INTEGER} ",
        " and kodedata=#{kodedata,jdbcType=INTEGER} and jeniscoa=#{jeniscoa,jdbcType=INTEGER} ",})
    
    @Results({
			@Result(column = "kodesatker", property = "kodesatker", jdbcType = JdbcType.VARCHAR),
			@Result(column = "kodepemda", property = "kodepemda", jdbcType = JdbcType.VARCHAR),
			@Result(column = "namapemda", property = "namapemda", jdbcType = JdbcType.VARCHAR),
			@Result(column = "tahunanggaran", property = "tahunanggaran", jdbcType = JdbcType.SMALLINT),
			@Result(column = "kodedata", property = "kodedata", jdbcType = JdbcType.SMALLINT),
			@Result(column = "statusdata", property = "statusdata", jdbcType = JdbcType.SMALLINT),
			@Result(column = "jeniscoa", property = "jeniscoa", jdbcType = JdbcType.SMALLINT),
			@Result(column = "nomorperda", property = "nomorperda", jdbcType = JdbcType.VARCHAR),
			@Result(column = "tanggalperda", property = "tanggalperda", jdbcType = JdbcType.TIMESTAMP),
                        @Result(column = "namaaplikasi", property = "namaaplikasi", jdbcType = JdbcType.VARCHAR),
                        @Result(column = "pengembangaplikasi", property = "pengembangaplikasi", jdbcType = JdbcType.VARCHAR),
                        @Result(column = "versisinergi", property = "versisinergi", jdbcType = JdbcType.VARCHAR),
                        @Result(column = "nmfile", property = "nmfile", jdbcType = JdbcType.VARCHAR),
			@Result(column = "sha256", property = "sha256")
    })
    Headerapbd_WS selectHeaderApbd(@Param("tahunanggaran") int tahunanggaran,
            @Param("kodesatker") String kodesatker, @Param("kodedata") int kodedata, @Param("jeniscoa") int jeniscoa);

    @Insert({
        "INSERT INTO transaksi.apbd( ",
        " headerapbdid, kodefungsi, namafungsi, kodeurusanpelaksana, namaurusanpelaksana, kodeurusanprogram, namaurusanprogram, ",
        " kodeunitskpd, namaunitskpd, kodeskpd, namaskpd, kodeprogram, namaprogram, ",
        " kodekegiatan, namakegiatan, namaoutcome, targetoutcome, satuanoutcome, ",
        " namaoutput, targetoutput, satuanoutput, kodelokasi, namalokasi, kodeakunutama, namaakunutama, kodeakunkelompok, namaakunkelompok, kodeakunjenis, namaakunjenis, kodeakunobjek, namaakunobjek, kodeakunrinci, namaakunrinci, kodeakunsubrinci, namaakunsubrinci, kodeakunsubsubrinci, namaakunsubsubrinci, volume, satuan, hargasatuan, nilaianggaran, catatan) ",
        " VALUES (#{headerapbdid,jdbcType=BIGINT}, ",
        "#{data.kodefungsi,jdbcType=VARCHAR}, #{data.namafungsi,jdbcType=VARCHAR}, ",
        "#{data.kodeurusanpelaksana,jdbcType=VARCHAR}, #{data.namaurusanpelaksana,jdbcType=VARCHAR}, ",
        "#{data.kodeurusanprogram,jdbcType=VARCHAR}, #{data.namaurusanprogram,jdbcType=VARCHAR}, ",
        "#{data.kodeunitskpd,jdbcType=VARCHAR}, #{data.namaunitskpd,jdbcType=VARCHAR}, ",
        "#{data.kodeskpd,jdbcType=VARCHAR}, #{data.namaskpd,jdbcType=VARCHAR}, ",
        "#{data.kodeprogram,jdbcType=VARCHAR}, #{data.namaprogram,jdbcType=VARCHAR}, ",
        "#{data.kodekegiatan,jdbcType=VARCHAR}, #{data.namakegiatan,jdbcType=VARCHAR}, ",
        "#{data.namaoutcome,jdbcType=VARCHAR}, #{data.targetoutcome,jdbcType=DOUBLE}, #{data.satuanoutcome,jdbcType=VARCHAR}, ",
        "#{data.namaoutput,jdbcType=VARCHAR}, #{data.targetoutput,jdbcType=DOUBLE}, #{data.satuanoutput,jdbcType=VARCHAR}, ",
        "#{data.kodelokasi,jdbcType=VARCHAR}, #{data.namalokasi,jdbcType=VARCHAR}, ",
        "#{data.kodeakunutama,jdbcType=VARCHAR}, #{data.namaakunutama,jdbcType=VARCHAR}, ",
        "#{data.kodeakunkelompok,jdbcType=VARCHAR}, #{data.namaakunkelompok,jdbcType=VARCHAR}, ",
        "#{data.kodeakunjenis,jdbcType=VARCHAR}, #{data.namaakunjenis,jdbcType=VARCHAR}, ",
        "#{data.kodeakunobjek,jdbcType=VARCHAR}, #{data.namaakunobjek,jdbcType=VARCHAR}, ",
        "#{data.kodeakunrinci,jdbcType=VARCHAR}, #{data.namaakunrinci,jdbcType=VARCHAR}, ",
        "#{data.kodeakunsubrinci,jdbcType=VARCHAR}, #{data.namaakunsubrinci,jdbcType=VARCHAR}, ",
        "#{data.kodeakunsubsubrinci,jdbcType=VARCHAR}, #{data.namaakunsubsubrinci,jdbcType=VARCHAR}, ",
        "#{data.volume,jdbcType=DOUBLE}, #{data.satuan,jdbcType=VARCHAR}, ",
        "#{data.hargasatuan,jdbcType=DOUBLE}, #{data.nilaianggaran,jdbcType=DOUBLE}, #{data.catatan,jdbcType=VARCHAR})",
    })
    void insertApbd(@Param("data") Apbd_WS data, @Param("headerapbdid") long headerapbdid);
    
}
