package sikd.sikd5.sikd5ejb.dao.transaksi;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import sikd.sikd5.sikd5bean.fault.SIKDServiceException;
import sikd.sikd5.sikd5bean.tabel.transaksi.Headerdth;
import sikd.sikd5.sikd5bean.ws.transaksi.Headerdth_WS;
import sikd.sikd5.sikd5bean.ws.transaksi.Dthbelanja_WS;
import sikd.sikd5.sikd5bean.ws.transaksi.Dthpotongan_WS;
import sikd.sikd5.sikd5ejb.mapper.transaksi.HeaderDthMapper;
import sikd.sikd5.sikd5ejb.mapper.transaksi.DthMapper;

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

    public long insertHeaderDth(Headerdth_WS headerDth, long headerlogidinsert, long headerlogidupdate, SqlSession session) throws SIKDServiceException {
        long hasil = 0;

        HeaderDthMapper headerMapper = session.getMapper(HeaderDthMapper.class);
        DthMapper dthMapper = session.getMapper(DthMapper.class);
        hasil = headerMapper.insertHeaderDth(headerDth, headerlogidinsert, headerlogidupdate);
        if (hasil > 0) {
            String result = "";
            if (headerDth.getDthbelanjas() != null && headerDth.getDthbelanjas().size() > 0) {
                for (Dthbelanja_WS dthb : headerDth.getDthbelanjas()) {
                    dthMapper.insertDthBelanja(dthb, hasil);
                }
            } else {
                result = "Data detil Dth Belanja kosong";
            }
            if (headerDth.getDthpotongans() != null && headerDth.getDthpotongans().size() > 0) {
                for (Dthpotongan_WS dthp : headerDth.getDthpotongans()) {
                    dthMapper.insertDthPotongan(dthp, hasil);
                }
            } 
//            else {
//                result = "Data detil Dth Potongan kosong";
//            }
//            if (!result.trim().equals("")) {
//                throw new SIKDServiceException(result);
//            }
        }
        return hasil;
    }

    public int hapusHeaderDth(long headerdthid, SqlSession session) throws SIKDServiceException {
        HeaderDthMapper mapper = session.getMapper(HeaderDthMapper.class);
        return mapper.deleteHeaderDthByPrimaryKey(headerdthid);
    }

    public int updateHeaderDth(Headerdth_WS headerDth, Long headerDthid, long headerlogidupdate, SqlSession session)
            throws SIKDServiceException {
        int hasil = 0;
        HeaderDthMapper headerMapper = session.getMapper(HeaderDthMapper.class);
        DthMapper dthMapper = session.getMapper(DthMapper.class);
        if (headerDthid == null) {
            Headerdth header = headerMapper.selectHeaderdthByNomorSp2d(headerDth.getTahunanggaran(), headerDth.getKodesatker(),
                    headerDth.getNomorsp2d());
            if (header == null) {
                throw new SIKDServiceException("Gagal Update headerDth Nomor Sp2d "
                        + headerDth.getNomorsp2d() + " tidak ditemukan");
            }
            headerDthid = header.getHeaderdthid();
        }
        headerMapper.updateHeaderdthByPrimaryKey(headerDth, headerDthid, headerlogidupdate);
        dthMapper.deleteDthBelanjaByHeaderdthid(headerDthid);
        dthMapper.deleteDthPotonganByHeaderdthid(headerDthid);
        for (Dthbelanja_WS dthb : headerDth.getDthbelanjas()) {
            dthMapper.insertDthBelanja(dthb, headerDthid);
        }
        for (Dthpotongan_WS dthp : headerDth.getDthpotongans()) {
            dthMapper.insertDthPotongan(dthp, headerDthid);
        }
        return hasil;
    }

    public List<Headerdth_WS> selectHeaderDthByNomorSp2ds(int tahunanggaran,
            String kodesatker, String[] inspd2s, SqlSession session) throws SIKDServiceException {
        HeaderDthMapper headerMapper = session.getMapper(HeaderDthMapper.class);
        return headerMapper.selectHeaderdthByNomorSp2ds(tahunanggaran, kodesatker, inspd2s);
    }

    public int hapusHeaderDthBySp2d(Headerdth_WS headerDth, SqlSession session) throws SIKDServiceException {
        HeaderDthMapper mapper = session.getMapper(HeaderDthMapper.class);
        return mapper.deleteHeaderDthBySp2d(headerDth);
    }
}
