/*
 * 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.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;

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

/**
 *
 * @author : agus
 * @tanggal : Nov 6, 2018
 */
public class UploadFileDao {
   private static Logger logger = Logger.getLogger(UploadFileDao.class);

   public void insert(UploadFile uploadFile, SqlSession session) throws SIKDServiceException {
      try {
         UploadFileMapper mapper = session.getMapper(UploadFileMapper.class);
         Date tglPengiriman = new Date();
         short uploadke = 1;

         InfoPengiriman info = mapper.getInfoPengiriman(uploadFile.getKdsatker(), uploadFile.getTahun(),
               uploadFile.getKodeikd(), "");
         if (info != null) {
            if (info.getUploadKe() != null) {
               uploadke = (short) (info.getUploadKe().shortValue() + (short) 1);
            }
         }
         uploadFile.setUploadke(uploadke);
         mapper.insert(uploadFile);
         if (logger.isDebugEnabled()) {
            logger.debug("Tes debug");
         }
      } catch (Exception e) {
         throw new SIKDServiceException(e.getMessage());
      }
   }

   public InfoPengiriman getInfoPengiriman(String kdsatker, short tahun, String kdikd, String nmikd,
         SqlSession session) throws SIKDServiceException {
      InfoPengiriman info = new InfoPengiriman();
      try {
         UploadFileMapper mapper = session.getMapper(UploadFileMapper.class);
         info = mapper.getInfoPengiriman(kdsatker, tahun, kdikd, nmikd);
      } catch (Exception e) {
         throw new SIKDServiceException(e.getMessage());
      }
      return info;
   }

   public List<UploadFile> getUploadFiles(String kodeSatker, short tahun, String kodeikd, SqlSession session)
         throws SIKDServiceException {
      List<UploadFile> uploadFiles = new ArrayList<>();
      try {
         UploadFileMapper mapper = session.getMapper(UploadFileMapper.class);
         uploadFiles = mapper.getUploadFiles(kodeSatker, tahun, kodeikd);
      } catch (Exception e) {
         throw new SIKDServiceException(e.getMessage());
      }
      return uploadFiles;
   }

   public List<UploadFile> getAllUploadFiles(String kodeSatker, short tahun, SqlSession session)
         throws SIKDServiceException {
      List<UploadFile> uploadFiles = new ArrayList<>();
      try {
         UploadFileMapper mapper = session.getMapper(UploadFileMapper.class);
         uploadFiles = mapper.getAllUploadFiles(kodeSatker, tahun);
         System.out.println(logger.isDebugEnabled());
         if (logger.isDebugEnabled()) {
            logger.debug("Tes debug");
         }
      } catch (Exception e) {
         throw new SIKDServiceException(e.getMessage());
      }
      return uploadFiles;
   }

   public List<UploadFile> getAllUploadFilesPerPemda(short tahun, short bulan, Date tgawal, Date tgakhir, SqlSession session)
         throws SIKDServiceException {
      List<UploadFile> uploadFiles = new ArrayList<>();
      try {
         UploadFileMapper mapper = session.getMapper(UploadFileMapper.class);
         uploadFiles = mapper.getAllUploadFilesPerPemda(tahun, bulan, tgawal, tgakhir);
//			System.out.println(logger.isDebugEnabled());
//			if (logger.isDebugEnabled()) {
//				logger.debug("Tes debug");
//			}
      } catch (Exception e) {
         throw new SIKDServiceException(e.getMessage());
      }
      return uploadFiles;
   }

   public List<UploadFile> getUploadFilesPerPemda(short tahun, String kdikdSingkat, Date tgakhir, SqlSession session) throws SIKDServiceException {
      UploadFileMapper mapper = session.getMapper(UploadFileMapper.class);
      return mapper.getUploadFilesPerPemda(tahun, kdikdSingkat, tgakhir);
   }
}
