package sikd.sikd5.sikd5ejb.dao.transaksi;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
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.tabel.transaksi.Headerjurnal;
import sikd.sikd5.sikd5bean.ws.transaksi.Belanja_WS;
import sikd.sikd5.sikd5bean.ws.transaksi.Belanjaakun_WS;
import sikd.sikd5.sikd5bean.ws.transaksi.Belanjapotongan_WS;
import sikd.sikd5.sikd5bean.ws.transaksi.Belanjaspjbukti_WS;
import sikd.sikd5.sikd5bean.ws.transaksi.Headerjurnal_WS;
import sikd.sikd5.sikd5bean.ws.transaksi.Jurnal_WS;
import sikd.sikd5.sikd5ejb.mapper.transaksi.BelanjaMapper;
import sikd.sikd5.sikd5ejb.mapper.transaksi.BelanjaakunMapper;
import sikd.sikd5.sikd5ejb.mapper.transaksi.BelanjapotonganMapper;
import sikd.sikd5.sikd5ejb.mapper.transaksi.BelanjaspjbuktiMapper;
import sikd.sikd5.sikd5ejb.mapper.transaksi.HeaderjurnalMapper;
import sikd.sikd5.sikd5ejb.mapper.transaksi.JurnalMapper;

/**
 * @oleh : agus
 * @tanggal : May 17, 2019
 */
public class HeaderjurnalDao {
	public long insertHeaderjurnal(Headerjurnal_WS headerjurnal, long headerlogid, SqlSession session) throws SIKDServiceException {
		long hasil = 0;

		HeaderjurnalMapper headerMapper = session.getMapper(HeaderjurnalMapper.class);
		JurnalMapper jurnalMapper = session.getMapper(JurnalMapper.class);
		hasil = headerMapper.insertHeaderJurnal(headerjurnal, headerlogid);
		if (hasil > 0) {
			if (headerjurnal.getJurnals() != null && headerjurnal.getJurnals().size() > 0) {
				for (Jurnal_WS jurnal : headerjurnal.getJurnals()) {
					jurnalMapper.insertJurnal(jurnal, hasil);
				}
			} else {
				throw new SIKDServiceException("Data detil jurnal kosong");
			}
		}
		return hasil;
	}

	public int hapusHeaderjurnal(long headerjurlid, SqlSession session) throws SIKDServiceException {
		HeaderjurnalMapper mapper = session.getMapper(HeaderjurnalMapper.class);
		return mapper.deleteHeaderJurnalByPrimaryKey(headerjurlid);
	}

	public int updateHeaderjurnal(Headerjurnal_WS headerjurnal, Long headerjurnalid, long headerlogidiupdate, SqlSession session)
			throws SIKDServiceException {
		int hasil = 0;
		HeaderjurnalMapper headerMapper = session.getMapper(HeaderjurnalMapper.class);
		JurnalMapper jurnalMapper = session.getMapper(JurnalMapper.class);
		if (headerjurnalid == null) {
			Headerjurnal header = headerMapper.selectHeaderjurnalByNomorbukti(headerjurnal.getTahunanggaran(), headerjurnal.getKodesatker(),
					headerjurnal.getNomorbukti(), headerjurnal.getTanggalbukti());
			if (header == null) {
				SimpleDateFormat format = new SimpleDateFormat("dd-MM-YYYY");
				throw new SIKDServiceException("Gagal Update headerjurnal Nomor bukti " +
						headerjurnal.getNomorbukti() + " dan tanggal bukti " + format.format(headerjurnal.getTanggalbukti()) + " tidak ditemukan");
			}
			headerjurnalid = header.getHeaderjurnalid();
		}
		headerMapper.updateHeaderjurnalByPrimaryKey(headerjurnal, headerjurnalid, headerlogidiupdate);
		jurnalMapper.deleteJurnalByHeaderjurnalid(headerjurnalid);
		for (Jurnal_WS jurnal : headerjurnal.getJurnals()) {
			jurnalMapper.insertJurnal(jurnal, headerjurnalid);
		}
		return hasil;
	}

	public List<Headerjurnal_WS> selectHeaderjurnalByNomorbuktis(int tahunanggaran,
			String kodesatker, String[] inBuktis, SqlSession session) throws SIKDServiceException {
		HeaderjurnalMapper headerMapper = session.getMapper(HeaderjurnalMapper.class);
		return headerMapper.selectHeaderjurnalByNomorbuktis(tahunanggaran, kodesatker, inBuktis);
	}

	public int hapusHeaderjurnalByBukti(Headerjurnal_WS headerjurnal, SqlSession session) throws SIKDServiceException {
		HeaderjurnalMapper mapper = session.getMapper(HeaderjurnalMapper.class);
		return mapper.deleteHeaderJurnalByBukti(headerjurnal);
	}
}
