package cokr.xit.fims.crdn.service.bean;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cokr.xit.base.file.FileInfo;
import cokr.xit.base.file.service.bean.FileBean;
import cokr.xit.fims.cmmn.service.bean.CrdnPayerHstryBean;
import cokr.xit.fims.cmmn.service.bean.CrdnSttsHstryBean;
import cokr.xit.fims.crdn.Crdn;
import cokr.xit.fims.crdn.CrdnQuery;
import cokr.xit.fims.crdn.dao.CrdnInfoMapper;
import cokr.xit.fims.crdn.dao.CrdnInstMapper;
import cokr.xit.fims.crdn.dao.CrdnListMapper;
import cokr.xit.fims.crdn.dao.CrdnUpdtMapper;
import cokr.xit.fims.crdn.dao.GlobalStngMapper;
import cokr.xit.fims.crdn.service.CrdnService;
import cokr.xit.fims.cvlc.dao.CrdnCvlcptMapper;
import cokr.xit.fims.cvlc.service.bean.CrdnCvlcptMainBean;
import cokr.xit.fims.excl.service.bean.Excl01Bean;
import cokr.xit.foundation.component.AbstractServiceBean;
import cokr.xit.foundation.data.DataObject;

@Service("crdnService")
public class CrdnServiceBean extends AbstractServiceBean implements CrdnService {

	@Resource(name="crdnCvlcptMapper")
	protected CrdnCvlcptMapper crdnCvlcptMapper;

	@Resource(name="crdnListMapper")
	protected CrdnListMapper crdnListMapper;

	@Resource(name="crdnInstMapper")
	protected CrdnInstMapper crdnInstMapper;

	@Resource(name="crdnUpdtMapper")
	protected CrdnUpdtMapper crdnUpdtMapper;

	@Resource(name="crdnInfoMapper")
	private CrdnInfoMapper crdnInfoMapper;

	@Resource(name="globalStngMapper")
	protected GlobalStngMapper globalStngMapper;

	@Resource(name="fileBean")
	private FileBean fileBean;

	@Resource(name="crdnStngBean")
	protected CrdnStngBean crdnStngBean;

	@Resource(name="crdnCvlcptMainBean")
	protected CrdnCvlcptMainBean crdnCvlcptMainBean;

	@Resource(name="crdnBean")
	protected CrdnBean crdnBean;

	@Resource(name="excl01Bean")
	protected Excl01Bean excl01Bean;

	@Resource(name="crdnSttsHstryBean")
	protected CrdnSttsHstryBean crdnSttsHstryBean;

	@Resource(name="crdnPayerHstryBean")
	protected CrdnPayerHstryBean crdnPayerHstryBean;

	@Override
	public List<DataObject> getCrackdownList(CrdnQuery query) {

		List<DataObject> dataObjectList = new ArrayList<DataObject>();
        switch (ifEmpty(query.getGridType(), () -> "")) {
	        case "tagInformationUndefinedData":
	        	dataObjectList = crdnListMapper.selectTagInformationUndefinedDataList(query.setOrderBy("CRDN_ID"));
	        	break;
	        case "photo":
	        	dataObjectList = crdnListMapper.selectPhotoInspectionDataList(query.setOrderBy("VHRNO, CRDN_PLC, CRDN_ID"));
	        	break;
	        case "sameVehicleMain":
	        	if(ifEmpty(query.getSchOnlySamePlace(),() -> "").equals("on")) {
	        		query.setOrderBy("VHRNO, CRDN_STDG_NM");
	        	} else {
	        		query.setOrderBy("VHRNO");
	        	}
	        	dataObjectList = crdnListMapper.selectSameVehicleMainList(query);
	        	break;
	        case "sameVehicleSub":
	        	dataObjectList = crdnListMapper.selectSameVehicleSubList(query.setOrderBy("CRDN_ID"));
	        	break;
	        case "todayInsert":
	        	query.setOrderBy("CRDN_ID");
	    		dataObjectList = crdnListMapper.selectTodayCrackdownList(query);
	    		break;
	        default:
	        	dataObjectList = crdnListMapper.selectCrackdownList(query.setOrderBy("CRDN_ID"));
	        	break;
	    }

        if(ifEmpty(query.getGridType(), () -> "").equals("tagInformationUndefinedData")) {
        	Iterator<DataObject> iterator = dataObjectList.iterator();
        	while(iterator.hasNext()) {
        		DataObject dataObject = iterator.next();
        		String state = dataObject.string("CRDN_STTS_CD");
        		int advntceAmt = dataObject.number("ADVNTCE_AMT").intValue();
        		int ffnlgAmt = dataObject.number("FFNLG_AMT").intValue();
        		int minusAmt = dataObject.number("MINUS_AMT").intValue();
        		int adtnAmt = dataObject.number("ADTN_AMT").intValue();
        		int rcvmtAmt = dataObject.number("RCVMT_AMT").intValue();

        		dataObject.set("BLNC", crdnStngBean.getBalance(state,advntceAmt,ffnlgAmt,minusAmt,adtnAmt,rcvmtAmt));
            }
        } else if(ifEmpty(query.getGridType(), () -> "").equals("photo")) {
        	Iterator<DataObject> iterator = dataObjectList.iterator();
        	while(iterator.hasNext()) {
        		DataObject dataObject = iterator.next();

        		if(dataObject.number("ATCH_FILE_CNT").intValue() < 1) {
        			dataObject.set("INSP_RSLT", "사진없음");
        			dataObject.set("PRCS_MTHD", "해당자료에 사진을 등록하시기 바랍니다.");
        		} else if(dataObject.number("CRDN_CNT").intValue() > 1) {
                	dataObject.set("INSP_RSLT", "중복자료");
                	if(query.getTaskSeCd().equals("PVS")) {
                		dataObject.set("PRCS_MTHD", "한건을 시간초과로 처리하십시요.");
                	} else {
                		dataObject.set("PRCS_MTHD", "한건을 서손으로 처리하십시요.");
                	}
                } else {
                	dataObject.set("INSP_RSLT", "정상자료");
                	dataObject.set("PRCS_MTHD", "정상자료");
                }
            }
        }

        return dataObjectList;
	}

	@Override
	public boolean remove(String... crdnIDs) {
		return crdnBean.remove(crdnIDs);
	}



	@Override
	public boolean create(Map<String, Object> nonQueryRequest, Crdn crdn, List<FileInfo> fileInfoList) {
		return crdnBean.create(nonQueryRequest, crdn, fileInfoList);
	}

	@Override
	public boolean update(Map<String, Object> nonQueryRequest, Crdn crdn, List<FileInfo> newFileInfoList) {
		boolean result = crdnBean.updateLedger(nonQueryRequest, crdn);
		if(!result) {
			throw new RuntimeException("단속 정보 수정 중 오류가 발생하였습니다.");
		}

		return true;
	}

	@Override
	public boolean updateOverTimeInfo(String chgAmtSe, String crdnId) {

		DataObject crdnInfo = crdnInfoMapper.selectCrdnInfo(crdnId);
		Crdn crdn = new Crdn();
		crdn.setCrdnId(crdnInfo.string("CRDN_ID"));
		crdn.setTaskSeCd(crdnInfo.string("TASK_SE_CD"));
		crdn.setCrdnYmd(crdnInfo.string("CRDN_YMD"));
		crdn.setCrdnSpareaCd(crdnInfo.string("CRDN_SPAREA_CD"));
		crdn.setCrdnTm(crdnInfo.string("CRDN_TM"));
		crdn.setFfnlgCarmdlCd(crdnInfo.string("FFNLG_CARMDL_CD"));

		if(chgAmtSe.equals("0")) {
			crdn.setOvtimeYn("N");
			crdn.setOvtimePrttnYn("N");
		} else if(chgAmtSe.equals("1")) {
			crdn.setOvtimeYn("Y");
			crdn.setOvtimePrttnYn("N");
		} else if(chgAmtSe.equals("2")) {
			crdn.setOvtimeYn("Y");
			crdn.setOvtimePrttnYn("Y");
		} else {
			throw new RuntimeException("2시간 초과 처리 중 오류가 발생하였습니다.");
		}

		if(crdnUpdtMapper.updateOverTimeInfo(crdn) != 1) {
			return false;
		}

		if(chgAmtSe.equals("0") || chgAmtSe.equals("1")) {
			int[] basicAmt = crdnStngBean.getBasicAmt(crdn);
			crdn.setFfnlgCrdnAmt(basicAmt[0]);
			crdn.setFfnlgAmt(basicAmt[0]);
			crdn.setAdvntceAmt(basicAmt[1]);
		} else if(chgAmtSe.equals("2")) {
			crdn.setFfnlgCrdnAmt(10000);
			crdn.setFfnlgAmt(10000);
			crdn.setAdvntceAmt(8000);
		}

		int effected = crdnUpdtMapper.updateCrdnAmt(crdn);
		if(effected != 1) {
			return false;
		}
		return true;
	}

	@Override
	public boolean updateTagInfo(Crdn crdn) {
		return crdnUpdtMapper.updateTagInfo(crdn) == 1 ? true : false;
	}

	@Override
	public String removeCrackdown(Crdn crdn) {
		return crdnBean.removeCrackdown(crdn);
	}

}
