package sikd.sikd5.sikd5ejb.dao;

import java.util.ArrayList;
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.AmbilPosisiKasBean;
import sikd.sikd5.sikd5bean.query.InfoPengiriman;
import sikd.sikd5.sikd5bean.tabel.simpatik.Posisikas;
import sikd.sikd5.sikd5bean.tabel.simpatik.Posisikasakun;
import sikd.sikd5.sikd5bean.ws.simpatik.PosisiKasAkunRinci_WS;
import sikd.sikd5.sikd5bean.ws.simpatik.PosisiKasAkun_WS;
import sikd.sikd5.sikd5bean.ws.simpatik.PosisiKas_WS;
import sikd.sikd5.sikd5ejb.mapper.PosisiKasMapper;

/**
 *
 * @author : agus
 * @tanggal : Nov 12, 2018
 */
public class PosisiKasDao {

	public long insert(PosisiKas_WS posisiKas, String nmfile, SqlSession session)
			throws SIKDServiceException {
		long hasil = 0;
		try {
			if (posisiKas == null) {
				throw new SIKDServiceException("Data Posisi Kas Kosong");
			}
			if (posisiKas.getPosisiKasAkuns() == null || posisiKas.getPosisiKasAkuns().size() < 1) {
				throw new SIKDServiceException("Data Posisi Kas Akun Kosong");
			}
			double nilai = 0;
			for (PosisiKasAkun_WS p : posisiKas.getPosisiKasAkuns()) {
				nilai = nilai + p.getNilai();
			}
			if (nilai < 1) {
				throw new SIKDServiceException("Data Posisi Kas Akun Semuanya 0 (Nol) ");
			}
			PosisiKasMapper mapper = session.getMapper(PosisiKasMapper.class);
			Date tglPengiriman = new Date();
			int uploadke = 1;

			InfoPengiriman info = mapper.getInfoPengiriman(posisiKas.getKodeSatker(), posisiKas.getTahunAnggaran(),
					posisiKas.getBulan());
			if (info != null) {
//            System.out.println(".................................................................");
//            System.out.println(info.getPenyusunanKe() == null);
//            System.out.println(posisiKas.getPenyusunanKe());

				if (info.getPenyusunanKe() >= posisiKas.getPenyusunanKe()) {
					throw new SIKDServiceException("Penyusunan Ke " + posisiKas.getPenyusunanKe() + " sudah pernah dikirim");
				}
				if (info.getUploadKe() != null) {
					uploadke = info.getUploadKe() + 1;
				}
			}
			long posisiKasIndex = mapper.insertPosisiKas(posisiKas, tglPengiriman, uploadke, nmfile);
			if (posisiKasIndex > 0) {
				for (PosisiKasAkun_WS posisiKasAkun_WS : posisiKas.getPosisiKasAkuns()) {
					long posisiKasAkunIndex = mapper.insertPosisiKasAkun(posisiKasIndex, posisiKasAkun_WS);
					if (posisiKasAkunIndex < 1) {
						throw new SIKDServiceException("Insert Posisi Kas Akun gagal");
					}
					if (posisiKasAkun_WS.getPosisiKasAkunRincis() != null) {
						for (PosisiKasAkunRinci_WS posisiKasAkunRinci_WS : posisiKasAkun_WS.getPosisiKasAkunRincis()) {
							long akunRinciIndex = mapper.insertPosisiKasAkunRinci(posisiKasAkunIndex,
									posisiKasAkunRinci_WS);
							if (akunRinciIndex < 0) {
								throw new SIKDServiceException("Insert Posisi Kas Akun Rinci gagal");
							}
						}
					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			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 {
			PosisiKasMapper mapper = session.getMapper(PosisiKasMapper.class);
			info = mapper.getInfoPengiriman(kdsatker, tahun, bulan);
		} catch (Exception e) {
			throw new SIKDServiceException(e.getMessage());
		}
		return info;
	}

	public List<InfoPengiriman> getInfoPengirimanXmlPerBulan(String kdsatker, short tahun, SqlSession session)
			throws SIKDServiceException {
		List<InfoPengiriman> infos = new ArrayList<>();
		try {
			PosisiKasMapper mapper = session.getMapper(PosisiKasMapper.class);
			infos = mapper.getInfoPengirimanXmlPerBulan(kdsatker, tahun);
		} catch (Exception e) {
			throw new SIKDServiceException(e.getMessage());
		}
		return infos;
	}

	public List<InfoPengiriman> getInfoPengirimanXmlPerPemda(short tahun, short bulan, Date tgawal, Date tgakhir, SqlSession session)
			throws SIKDServiceException {
		List<InfoPengiriman> infos = new ArrayList<>();
		try {
			PosisiKasMapper mapper = session.getMapper(PosisiKasMapper.class);
			infos = mapper.getInfoPengirimanXmlPerPemda(tahun, bulan, tgawal, tgakhir);
		} catch (Exception e) {
			throw new SIKDServiceException(e.getMessage());
		}
		return infos;
	}

	public List<Posisikasakun> getPosisiKasAkun(String kodeSatker, short tahun, short bulan, SqlSession session)
			throws SIKDServiceException {
		List<Posisikasakun> infos = new ArrayList<Posisikasakun>();
		try {
			PosisiKasMapper mapper = session.getMapper(PosisiKasMapper.class);
			Posisikas pk = mapper.getPosisiKasTerbaru(kodeSatker, tahun, bulan);
			if (pk != null) {
				if (pk.getPosisikasindex() > 0) {
					infos = mapper.getPosisiKasAkun(pk.getPosisikasindex());
				}
			}
		} catch (Exception e) {
			throw new SIKDServiceException(e.getMessage());
		}
		return infos;
	}

	public List<AmbilPosisiKasBean> ambilPosisiKasPerPemda(short tahun, short bulan, Date tgakhir, String[] inAkun, SqlSession session)
			throws SIKDServiceException {
		List<AmbilPosisiKasBean> infos = new ArrayList<AmbilPosisiKasBean>();
		try {
			PosisiKasMapper mapper = session.getMapper(PosisiKasMapper.class);
			infos = mapper.ambilPosisiKasPerPemda(tahun, bulan, tgakhir, inAkun);
		} catch (Exception e) {
			throw new SIKDServiceException(e.getMessage());
		}
		return infos;
	}

	public List<AmbilPosisiKasBean> ambilPosisiKasGakRinciPerPemda(short tahun, short bulan, Date tgakhir, String[] inAkun, SqlSession session)
			throws SIKDServiceException {
		List<AmbilPosisiKasBean> infos = new ArrayList<AmbilPosisiKasBean>();
		try {
			PosisiKasMapper mapper = session.getMapper(PosisiKasMapper.class);
			infos = mapper.ambilPosisiKasGakRinciPerPemda(tahun, bulan, tgakhir, inAkun);
		} catch (Exception e) {
			throw new SIKDServiceException(e.getMessage());
		}
		return infos;
	}
}
