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.Apbd_WS;
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.Headerapbd_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.HeaderapbdMapper;
import sikd.sikd5.sikd5ejb.mapper.transaksi.HeaderjurnalMapper;
import sikd.sikd5.sikd5ejb.mapper.transaksi.JurnalMapper;

/**
 * @oleh : soraturaes
 */
public class HeaderapbdDao {

    public long insertHeaderApbd(Headerapbd_WS headerapbd, SqlSession session) throws SIKDServiceException {
        long hasil = 0;

        HeaderapbdMapper headerMapper = session.getMapper(HeaderapbdMapper.class);
        hasil = headerMapper.insertHeaderApbd(headerapbd, new Date());
        if (hasil > 0) {
            if (headerapbd.getApbds() != null && headerapbd.getApbds().size() > 0) {
//                headerMapper.insertApbd(headerapbd.getApbds(), hasil);
                for (Apbd_WS ap : headerapbd.getApbds()) {
                    headerMapper.insertApbd(ap, hasil);
                }
            } else {
                throw new SIKDServiceException("Data detil jurnal kosong");
            }
        }
        return hasil;
    }

//	public int hapusHeaderApbd(Headerapbd_WS record, SqlSession session) throws SIKDServiceException {
//		HeaderapbdMapper mapper = session.getMapper(HeaderapbdMapper.class);
//		return mapper.deleteApbdByPemdaTahunKodeDataCoa(record);
//	}
    public int updateHeaderApbd(Headerapbd_WS headerapbd, Long headerapbdid, SqlSession session)
            throws SIKDServiceException {
        int hasil = 0;
        HeaderapbdMapper headerMapper = session.getMapper(HeaderapbdMapper.class);
        if (headerapbdid == null) {
            long headerId = headerMapper.selectHeaderApbdid(headerapbd.getTahunanggaran(), headerapbd.getKodesatker(),
                    headerapbd.getKodedata(), headerapbd.getJeniscoa());
            if (headerId <= 0) {
                SimpleDateFormat format = new SimpleDateFormat("dd-MM-YYYY");
                throw new SIKDServiceException("Gagal Update headerapbd  tidak ditemukan");
            }
            headerapbdid = headerId;
        }
        headerMapper.updateHeaderApbd(headerapbdid, headerapbd, new Date());
        headerMapper.deleteApbdByHeaderApbdId(headerapbdid);
//        headerMapper.insertApbd(headerapbd.getApbds(), headerapbdid);
        for (Apbd_WS ap : headerapbd.getApbds()) {
            headerMapper.insertApbd(ap, headerapbdid);
        }
        return hasil;
    }
    
    public int updateHeaderApbdBySkpd(Headerapbd_WS headerapbd, Long headerapbdid, SqlSession session)
            throws SIKDServiceException {
        int hasil = 0;
        HeaderapbdMapper headerMapper = session.getMapper(HeaderapbdMapper.class);
        if (headerapbdid == null) {
            long headerId = headerMapper.selectHeaderApbdid(headerapbd.getTahunanggaran(), headerapbd.getKodesatker(),
                    headerapbd.getKodedata(), headerapbd.getJeniscoa());
            if (headerId <= 0) {
                SimpleDateFormat format = new SimpleDateFormat("dd-MM-YYYY");
                throw new SIKDServiceException("Gagal Update headerapbd  tidak ditemukan");
            }
            headerapbdid = headerId;
        }
        headerMapper.updateHeaderApbd(headerapbdid, headerapbd, new Date());
        if (headerapbd.getApbds() != null && !headerapbd.getApbds().isEmpty() && headerapbd.getApbds().get(0) != null && headerapbd.getApbds().get(0).getNamaskpd() != null) {
            headerMapper.deleteApbdByHeaderApbdIdSkpd(headerapbdid, headerapbd.getApbds().get(0).getNamaskpd());
        }
        for (Apbd_WS ap : headerapbd.getApbds()) {
            headerMapper.insertApbd(ap, headerapbdid);
        }
        return hasil;
    }
    
    public long selectHeaderApbdId(int tahunanggaran,
			String kodesatker, int kodedata, int jeniscoa, SqlSession session) throws SIKDServiceException {
		HeaderapbdMapper headerMapper = session.getMapper(HeaderapbdMapper.class);
		return headerMapper.selectHeaderApbdid(tahunanggaran, kodesatker, kodedata, jeniscoa);
	}
    
       public Headerapbd_WS selectHeaderApbd(int tahunanggaran,
            String kodesatker, int kodedata, int jeniscoa, SqlSession session) throws SIKDServiceException {
        HeaderapbdMapper headerMapper = session.getMapper(HeaderapbdMapper.class);
        //TODO buat mapper cari header
        return headerMapper.selectHeaderApbd(tahunanggaran, kodesatker, kodedata, jeniscoa);
    }
}
