/*
 * 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.dao;

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

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;

import sikd.sikd5.sikd5bean.fault.SIKDServiceException;
import sikd.sikd5.sikd5bean.query.AmbilKontakPemdaBean;
import sikd.sikd5.sikd5bean.query.InfoPengiriman;
import sikd.sikd5.sikd5bean.ws.simpatik.KontakPemda_WS;
import sikd.sikd5.sikd5ejb.mapper.KontakPemdaMapper;

/**
 *
 * @author : agus
 * @tanggal : Nov 6, 2018
 */
public class KontakPemdaDao {

   public long insert(KontakPemda_WS kontakPemda, String nmfile, SqlSession session) throws SIKDServiceException {
      long hasil = 0;
      try {
         KontakPemdaMapper mapper = session.getMapper(KontakPemdaMapper.class);
         Date tglPengiriman = new Date();
         int uploadke = 1;

         InfoPengiriman info = mapper.getInfoPengiriman(kontakPemda.getKodeSatker(), kontakPemda.getTahunAnggaran(), kontakPemda.getBulan());
         if (info != null) {
            if (info.getUploadKe() != null) {
               uploadke = info.getUploadKe() + 1;
            }
         }
         hasil = mapper.insert(kontakPemda, tglPengiriman, uploadke, nmfile);
      } catch (Exception e) {
         throw new SIKDServiceException(e.getMessage());
      }
      return hasil;
   }

   public InfoPengiriman getInfoPengiriman(String kdsatker, short tahun, short bulan, SqlSession session) throws SIKDServiceException {
      InfoPengiriman info = new InfoPengiriman();
      try {
         KontakPemdaMapper mapper = session.getMapper(KontakPemdaMapper.class);
         info = mapper.getInfoPengiriman(kdsatker, tahun, bulan);
//             Logger.getLogger(KontakPemdaDao.class.getName()).log(Level.INFO, null, info);
      } catch (Exception e) {
         throw new SIKDServiceException(e.getMessage());
      }
      return info;
   }

   public List<AmbilKontakPemdaBean> ambilKontakPemda(short tahun, SqlSession session) throws SIKDServiceException {
      KontakPemdaMapper mapper = session.getMapper(KontakPemdaMapper.class);
      return mapper.ambilKontakPemda(tahun);
   }
}
