package sikd.sikd5.sikd5ejb.dao.transaksi;
/**
*
* @author : agus
* @tanggal : Mar 28, 2019
*/

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

import org.apache.ibatis.session.SqlSession;

import sikd.sikd5.sikd5bean.fault.SIKDServiceException;
import sikd.sikd5.sikd5bean.tabel.transaksi.Belanja;
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.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;

public class BelanjaDao {

	public List<Belanja_WS> getBelanjaDanAnaks(String kodeSatker, short tahun, Date tgawal, Date tgakhir,
			SqlSession session) throws SIKDServiceException {
		List<Belanja_WS> hasil = new ArrayList<>();
		BelanjaMapper mapper = session.getMapper(BelanjaMapper.class);
//		BelanjaExample ex = new BelanjaExample();
//		Criteria crit = ex.createCriteria();
//		crit.andTahunanggaranEqualTo(tahun);
//		crit.andKodesatkerEqualTo(kodeSatker);
//		crit.andTanggalsp2dBetween(tgawal, tgakhir);
//		hasil = mapper.selectBelanjaDanAnakByExample(ex);
		return hasil;
	}

	public List<Belanja_WS> getBelanjaDanAnaksByNomorSp2d(String kodeSatker, short tahun, List<String> nomorsp2ds,
			SqlSession session) throws SIKDServiceException {
		List<Belanja_WS> hasil = new ArrayList<>();
		BelanjaMapper mapper = session.getMapper(BelanjaMapper.class);
//		BelanjaExample ex = new BelanjaExample();
//		Criteria crit = ex.createCriteria();
//		crit.andTahunanggaranEqualTo(tahun);
//		crit.andKodesatkerEqualTo(kodeSatker);
//		crit.andNomorsp2dIn(nomorsp2ds);
//		hasil = mapper.selectBelanjaDanAnakByExample(ex);
		return hasil;
	}

	public long inserBelanja(Belanja_WS belanja, long headerlogid, SqlSession session) throws SIKDServiceException {
		long hasil = 0;
		long belanjaakunid = 0;
		BelanjaMapper mapper = session.getMapper(BelanjaMapper.class);
		BelanjaakunMapper mapperAkun = session.getMapper(BelanjaakunMapper.class);
		BelanjapotonganMapper mapperPotongan = session.getMapper(BelanjapotonganMapper.class);
		BelanjaspjbuktiMapper mapperSpjbukti = session.getMapper(BelanjaspjbuktiMapper.class);
		hasil = mapper.insertBelanja(belanja, headerlogid);
		if (hasil > 0) {
			for (Belanjaakun_WS akun : belanja.getBelanjaakuns()) {
				belanjaakunid = mapperAkun.insertBelanjakun(akun, hasil);
				for (Belanjaspjbukti_WS spjbukti : akun.getBelanjaspjbuktis()) {
					mapperSpjbukti.insertBelanjaspjbukti(spjbukti, belanjaakunid);
				}

			}
			for (Belanjapotongan_WS potongan : belanja.getBelanjapotongans()) {
				mapperPotongan.insertBelanjapotongan(potongan, hasil);
			}
		}
		return hasil;
	}

	public int hapusBelanja(long belanjaid, SqlSession session) throws SIKDServiceException {
		BelanjaMapper mapper = session.getMapper(BelanjaMapper.class);
		return mapper.deleteBelanjaByPrimaryKey(belanjaid);
	}

	public int updateBelanja(Belanja_WS belanja, long belanjaid, SqlSession session) throws SIKDServiceException {
		int hasil = 0;
		long belanjaakunid = 0;
		BelanjaMapper mapper = session.getMapper(BelanjaMapper.class);
		BelanjaakunMapper mapperAkun = session.getMapper(BelanjaakunMapper.class);
		BelanjapotonganMapper mapperPotongan = session.getMapper(BelanjapotonganMapper.class);
		BelanjaspjbuktiMapper mapperSpjbukti = session.getMapper(BelanjaspjbuktiMapper.class);
		hasil = mapper.updateBelanjaByPrimaryKey(belanja, belanjaid);
		mapperAkun.deleteBelanjaakunByBelanjaid(belanjaid);
		mapperPotongan.deleteBelanjapotonganByBelanjaid(belanjaid);
//		mapperSpjbukti.deleteBelanjaspjbuktiByBelanjaakunid(belanjaakunid);
		for (Belanjaakun_WS akun : belanja.getBelanjaakuns()) {
			belanjaakunid = mapperAkun.insertBelanjakun(akun, hasil);
			for (Belanjaspjbukti_WS spjbukti : akun.getBelanjaspjbuktis()) {
				mapperSpjbukti.insertBelanjaspjbukti(spjbukti, belanjaakunid);
			}
		}
		for (Belanjapotongan_WS potongan : belanja.getBelanjapotongans()) {
			mapperPotongan.insertBelanjapotongan(potongan, hasil);
		}
		return hasil;
	}

	public int updateBelanja(Belanja_WS belanja, SqlSession session) throws SIKDServiceException {
		int hasil = 0;

		BelanjaMapper mapper = session.getMapper(BelanjaMapper.class);
		BelanjaakunMapper mapperAkun = session.getMapper(BelanjaakunMapper.class);
		BelanjapotonganMapper mapperPotongan = session.getMapper(BelanjapotonganMapper.class);

//		BelanjaExample ex = new BelanjaExample();
//		Criteria crit = ex.createCriteria();
//		crit.andTahunanggaranEqualTo(belanja.getTahunanggaran());
//		crit.andKodesatkerEqualTo(belanja.getKodesatker());
//		crit.andNomorsp2dEqualTo(belanja.getNomorsp2d());
//		List<Belanja> bljs = mapper.selectBelanjaByExample(ex);
		List<Belanja> bljs = new ArrayList<>();
		if (bljs.size() > 0) {
			Belanja blj = bljs.get(0);
			hasil = mapper.updateBelanjaByPrimaryKey(belanja, blj.getBelanjaid());
			mapperAkun.deleteBelanjaakunByBelanjaid(blj.getBelanjaid());
			mapperPotongan.deleteBelanjapotonganByBelanjaid(blj.getBelanjaid());
			for (Belanjaakun_WS akun : belanja.getBelanjaakuns()) {
				mapperAkun.insertBelanjakun(akun, hasil);
			}
			for (Belanjapotongan_WS potongan : belanja.getBelanjapotongans()) {
				mapperPotongan.insertBelanjapotongan(potongan, hasil);
			}
		}

		return hasil;
	}
}
