package com.bankaudit.service;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bankaudit.constants.BankAuditConstant;
import com.bankaudit.dao.MaintReportSequenceProcessDao;
import com.bankaudit.dto.DataTableResponse;
import com.bankaudit.dto.SequenceDto;
import com.bankaudit.model.MaintReportSequenceProcess;
import com.bankaudit.model.MaintReportSequenceProcessHst;
import com.bankaudit.model.MaintReportSequenceProcessWrk;
import com.bankaudit.util.BankAuditUtil;

@Service("maintReportSequenceProcessService")
@Transactional("transactionManager")
public class MaintReportSequenceProcessServiceImpl implements MaintReportSequenceProcessService {

	@Autowired
	private MaintReportSequenceProcessDao maintReportSequenceProcessDao;
	@Autowired
	BusinessDateService businessDateService;

	@Override
	public void saveChapter(List<MaintReportSequenceProcess> mrslist) throws Exception {
		if (mrslist != null && !mrslist.isEmpty()) {
			List<MaintReportSequenceProcess> maRSPListdb = getMaintReportSequenceProcessChapterList(mrslist.get(0));
			List<MaintReportSequenceProcessWrk> mrspWrkListdb = getMaintReportSequenceProcessWrkChapterList(mrslist.get(0));
			if (maRSPListdb.isEmpty() && mrspWrkListdb.isEmpty()) {
				for (MaintReportSequenceProcess maintReportSequenceProcess : mrslist) {
					if (maintReportSequenceProcess.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
						maintReportSequenceProcessDao.save(maintReportSequenceProcess);
					} else {
						MaintReportSequenceProcessWrk maintReportSequenceProcessWrk = new MaintReportSequenceProcessWrk();
						BeanUtils.copyProperties(maintReportSequenceProcess, maintReportSequenceProcessWrk);
						maintReportSequenceProcessDao.save(maintReportSequenceProcessWrk);
					}
				}
			} else {
				throw new Exception("Sequence is alredy present");
			}
		}
	}

	@Override
	public void updateChapter(List<MaintReportSequenceProcess> maintReportSequenceProcesslist) throws Exception {
		if (maintReportSequenceProcesslist != null && !maintReportSequenceProcesslist.isEmpty()) {
			deleteChapter(maintReportSequenceProcesslist.get(0));
			for (MaintReportSequenceProcess maintReportSequenceProcess : maintReportSequenceProcesslist) {
				if (maintReportSequenceProcess.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
					maintReportSequenceProcessDao.save(maintReportSequenceProcess);
				} else {
					MaintReportSequenceProcessWrk maintReportSequenceProcessWrk = new MaintReportSequenceProcessWrk();
					BeanUtils.copyProperties(maintReportSequenceProcess, maintReportSequenceProcessWrk);
					maintReportSequenceProcessDao.save(maintReportSequenceProcessWrk);
				}
			}
		}
	}

	public void deleteChapter(MaintReportSequenceProcess maintReportSequenceProcess) {
		if (maintReportSequenceProcess.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
			List<MaintReportSequenceProcess> maintReportSequenceProcessListdb = getMaintReportSequenceProcessChapterList(maintReportSequenceProcess);
			List<MaintReportSequenceProcessWrk> maintReportSequenceProcessWrkListdb = getMaintReportSequenceProcessWrkChapterList(
					maintReportSequenceProcess);
			for (MaintReportSequenceProcessWrk maintReportSequenceProcess2 : maintReportSequenceProcessWrkListdb) {
				MaintReportSequenceProcessHst hst = new MaintReportSequenceProcessHst();
				BeanUtils.copyProperties(maintReportSequenceProcess2, hst);
				maintReportSequenceProcessDao.save(hst);
			}
			maintReportSequenceProcessDao.flushSession();
			for (MaintReportSequenceProcess maintReportSequenceProcess2 : maintReportSequenceProcessListdb) {
				maintReportSequenceProcessDao.delete(maintReportSequenceProcess2);
			}
			for (MaintReportSequenceProcessWrk maintReportSequenceProcessWrk : maintReportSequenceProcessWrkListdb) {
				maintReportSequenceProcessDao.delete(maintReportSequenceProcessWrk);
			}
		} else {
			List<MaintReportSequenceProcessWrk> maintReportSequenceProcessWrkListdb = getMaintReportSequenceProcessWrkChapterList(
					maintReportSequenceProcess);
			if (maintReportSequenceProcessWrkListdb != null) {
				for (MaintReportSequenceProcessWrk maintReportSequenceProcess2 : maintReportSequenceProcessWrkListdb) {
					MaintReportSequenceProcessHst hst = new MaintReportSequenceProcessHst();
					BeanUtils.copyProperties(maintReportSequenceProcess2, hst);
					maintReportSequenceProcessDao.save(hst);
				}
				maintReportSequenceProcessDao.flushSession();
				for (MaintReportSequenceProcessWrk maintReportSequenceProcessWrk : maintReportSequenceProcessWrkListdb) {
					maintReportSequenceProcessDao.delete(maintReportSequenceProcessWrk);
				}
			} else {
				List<MaintReportSequenceProcess> maintReportSequenceProcessListdb = getMaintReportSequenceProcessChapterList(
						maintReportSequenceProcess);
				for (MaintReportSequenceProcess maintReportSequenceProcess2 : maintReportSequenceProcessListdb) {
					MaintReportSequenceProcessHst hst = new MaintReportSequenceProcessHst();
					BeanUtils.copyProperties(maintReportSequenceProcess2, hst);
					maintReportSequenceProcessDao.save(hst);
				}
				maintReportSequenceProcessDao.flushSession();
				for (MaintReportSequenceProcess maintReportSequenceProcessdel : maintReportSequenceProcessListdb) {
					maintReportSequenceProcessDao.delete(maintReportSequenceProcessdel);
				}
			}
		}
	}

	private List<MaintReportSequenceProcessWrk> getMaintReportSequenceProcessWrkChapterList(MaintReportSequenceProcess chseqw) {
		return maintReportSequenceProcessDao.getMaintReportSequenceProcessWrkChapaterList(chseqw.getLegalEntityCode(), chseqw.getAuditTypeCode());
	}

	private List<MaintReportSequenceProcess> getMaintReportSequenceProcessChapterList(MaintReportSequenceProcess chseq) {
		return maintReportSequenceProcessDao.getMaintReportSequenceProcessChapaterList(chseq.getLegalEntityCode(), chseq.getAuditTypeCode());
	}

	@Override
	public void saveSection(List<MaintReportSequenceProcess> mrslist) throws Exception {
		if (mrslist != null && !mrslist.isEmpty()) {
			List<MaintReportSequenceProcess> maRSPListdb = getMaintReportSequenceProcessSectionList(mrslist.get(0));
			List<MaintReportSequenceProcessWrk> mrspWrkListdb = getMaintReportSequenceProcessWrkSectionList(mrslist.get(0));
			if (maRSPListdb.isEmpty() && mrspWrkListdb.isEmpty()) {
				for (MaintReportSequenceProcess maintReportSequenceProcess : mrslist) {
					if (maintReportSequenceProcess.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
						maintReportSequenceProcessDao.save(maintReportSequenceProcess);
					} else {
						MaintReportSequenceProcessWrk maintReportSequenceProcessWrk = new MaintReportSequenceProcessWrk();
						BeanUtils.copyProperties(maintReportSequenceProcess, maintReportSequenceProcessWrk);
						maintReportSequenceProcessDao.save(maintReportSequenceProcessWrk);
					}
				}
			} else {
				throw new Exception("Sequence is alredy present");
			}
		}
	}

	@Override
	public void updateSection(List<MaintReportSequenceProcess> maintReportSequenceProcesslist) {
		if (maintReportSequenceProcesslist != null && !maintReportSequenceProcesslist.isEmpty()) {
			deleteSection(maintReportSequenceProcesslist.get(0));
			for (MaintReportSequenceProcess maintReportSequenceProcess : maintReportSequenceProcesslist) {
				if (maintReportSequenceProcess.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
					maintReportSequenceProcessDao.save(maintReportSequenceProcess);
				} else {
					MaintReportSequenceProcessWrk maintReportSequenceProcessWrk = new MaintReportSequenceProcessWrk();
					BeanUtils.copyProperties(maintReportSequenceProcess, maintReportSequenceProcessWrk);
					maintReportSequenceProcessDao.save(maintReportSequenceProcessWrk);
				}
			}
		}
	}

	public void deleteSection(MaintReportSequenceProcess maintReportSequenceProcess) {
		if (maintReportSequenceProcess.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
			List<MaintReportSequenceProcess> maintReportSequenceProcessListdb = getMaintReportSequenceProcessSectionList(maintReportSequenceProcess);
			List<MaintReportSequenceProcessWrk> maintReportSequenceProcessWrkListdb = getMaintReportSequenceProcessWrkSectionList(
					maintReportSequenceProcess);
			for (MaintReportSequenceProcessWrk maintReportSequenceProcess2 : maintReportSequenceProcessWrkListdb) {
				MaintReportSequenceProcessHst hst = new MaintReportSequenceProcessHst();
				BeanUtils.copyProperties(maintReportSequenceProcess2, hst);
				maintReportSequenceProcessDao.save(hst);
			}
			maintReportSequenceProcessDao.flushSession();
			for (MaintReportSequenceProcess maintReportSequenceProcess2 : maintReportSequenceProcessListdb) {
				maintReportSequenceProcessDao.delete(maintReportSequenceProcess2);
			}
			for (MaintReportSequenceProcessWrk maintReportSequenceProcessWrk : maintReportSequenceProcessWrkListdb) {
				maintReportSequenceProcessDao.delete(maintReportSequenceProcessWrk);
			}
		} else {
			List<MaintReportSequenceProcessWrk> maintReportSequenceProcessWrkListdb = getMaintReportSequenceProcessWrkSectionList(
					maintReportSequenceProcess);
			if (maintReportSequenceProcessWrkListdb != null) {
				for (MaintReportSequenceProcessWrk maintReportSequenceProcess2 : maintReportSequenceProcessWrkListdb) {
					MaintReportSequenceProcessHst hst = new MaintReportSequenceProcessHst();
					BeanUtils.copyProperties(maintReportSequenceProcess2, hst);
					maintReportSequenceProcessDao.save(hst);
				}
				maintReportSequenceProcessDao.flushSession();
				for (MaintReportSequenceProcessWrk maintReportSequenceProcessWrk : maintReportSequenceProcessWrkListdb) {
					maintReportSequenceProcessDao.delete(maintReportSequenceProcessWrk);
				}
			} else {
				List<MaintReportSequenceProcess> maintReportSequenceProcessListdb = getMaintReportSequenceProcessSectionList(
						maintReportSequenceProcess);
				for (MaintReportSequenceProcess maintReportSequenceProcess2 : maintReportSequenceProcessListdb) {
					MaintReportSequenceProcessHst hst = new MaintReportSequenceProcessHst();
					BeanUtils.copyProperties(maintReportSequenceProcess2, hst);
					maintReportSequenceProcessDao.save(hst);
				}
				maintReportSequenceProcessDao.flushSession();
				for (MaintReportSequenceProcess maintReportSequenceProcessdel : maintReportSequenceProcessListdb) {
					maintReportSequenceProcessDao.delete(maintReportSequenceProcessdel);
				}
			}
		}
	}

	private List<MaintReportSequenceProcessWrk> getMaintReportSequenceProcessWrkSectionList(MaintReportSequenceProcess sesqw) {
		return maintReportSequenceProcessDao.getMaintReportSequenceProcessWrkSectionList(sesqw.getLegalEntityCode(), sesqw.getAuditTypeCode(),
				sesqw.getAuditGroupCode());
	}

	private List<MaintReportSequenceProcess> getMaintReportSequenceProcessSectionList(MaintReportSequenceProcess sesq) {
		return maintReportSequenceProcessDao.getMaintReportSequenceProcessSectionList(sesq.getLegalEntityCode(), sesq.getAuditTypeCode(),
				sesq.getAuditGroupCode());
	}

	@Override
	public void saveProduct(List<MaintReportSequenceProcess> mrslist) throws Exception {
		if (mrslist != null && !mrslist.isEmpty()) {
			List<MaintReportSequenceProcess> maRSPListdb = getMaintReportSequenceProcessProductList(mrslist.get(0));
			List<MaintReportSequenceProcessWrk> mrspWrkListdb = getMaintReportSequenceProcessWrkProductList(mrslist.get(0));
			if (maRSPListdb.isEmpty() && mrspWrkListdb.isEmpty()) {
				for (MaintReportSequenceProcess maintReportSequenceProcess : mrslist) {
					if (maintReportSequenceProcess.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
						maintReportSequenceProcessDao.save(maintReportSequenceProcess);
					} else {
						MaintReportSequenceProcessWrk maintReportSequenceProcessWrk = new MaintReportSequenceProcessWrk();
						BeanUtils.copyProperties(maintReportSequenceProcess, maintReportSequenceProcessWrk);
						maintReportSequenceProcessDao.save(maintReportSequenceProcessWrk);
					}
				}
			} else {
				throw new Exception("Sequence is alredy present");
			}
		}
	}

	@Override
	public void updateProduct(List<MaintReportSequenceProcess> maintReportSequenceProcesslist) {
		if (maintReportSequenceProcesslist != null && !maintReportSequenceProcesslist.isEmpty()) {
			deleteProductList(maintReportSequenceProcesslist.get(0));
			for (MaintReportSequenceProcess maintReportSequenceProcess : maintReportSequenceProcesslist) {
				if (maintReportSequenceProcess.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
					maintReportSequenceProcessDao.save(maintReportSequenceProcess);
				} else {
					MaintReportSequenceProcessWrk maintReportSequenceProcessWrk = new MaintReportSequenceProcessWrk();
					BeanUtils.copyProperties(maintReportSequenceProcess, maintReportSequenceProcessWrk);
					maintReportSequenceProcessDao.save(maintReportSequenceProcessWrk);
				}
			}
		}
	}

	public void deleteProductList(MaintReportSequenceProcess maintReportSequenceProcess) {
		if (maintReportSequenceProcess.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
			List<MaintReportSequenceProcess> maintReportSequenceProcessListdb = getMaintReportSequenceProcessProductList(maintReportSequenceProcess);
			List<MaintReportSequenceProcessWrk> maintReportSequenceProcessWrkListdb = getMaintReportSequenceProcessWrkProductList(
					maintReportSequenceProcess);
			for (MaintReportSequenceProcessWrk maintReportSequenceProcess2 : maintReportSequenceProcessWrkListdb) {
				MaintReportSequenceProcessHst hst = new MaintReportSequenceProcessHst();
				BeanUtils.copyProperties(maintReportSequenceProcess2, hst);
				maintReportSequenceProcessDao.save(hst);
			}
			maintReportSequenceProcessDao.flushSession();
			for (MaintReportSequenceProcess maintReportSequenceProcess2 : maintReportSequenceProcessListdb) {
				maintReportSequenceProcessDao.delete(maintReportSequenceProcess2);
			}
			for (MaintReportSequenceProcessWrk maintReportSequenceProcessWrk : maintReportSequenceProcessWrkListdb) {
				maintReportSequenceProcessDao.delete(maintReportSequenceProcessWrk);
			}
		} else {
			List<MaintReportSequenceProcessWrk> maintReportSequenceProcessWrkListdb = getMaintReportSequenceProcessWrkProductList(
					maintReportSequenceProcess);
			if (maintReportSequenceProcessWrkListdb != null) {
				for (MaintReportSequenceProcessWrk maintReportSequenceProcess2 : maintReportSequenceProcessWrkListdb) {
					MaintReportSequenceProcessHst hst = new MaintReportSequenceProcessHst();
					BeanUtils.copyProperties(maintReportSequenceProcess2, hst);
					maintReportSequenceProcessDao.save(hst);
				}
				maintReportSequenceProcessDao.flushSession();
				for (MaintReportSequenceProcessWrk maintReportSequenceProcessWrk : maintReportSequenceProcessWrkListdb) {
					maintReportSequenceProcessDao.delete(maintReportSequenceProcessWrk);
				}
			} else {
				List<MaintReportSequenceProcess> maintReportSequenceProcessListdb = getMaintReportSequenceProcessProductList(
						maintReportSequenceProcess);
				for (MaintReportSequenceProcess maintReportSequenceProcess2 : maintReportSequenceProcessListdb) {
					MaintReportSequenceProcessHst hst = new MaintReportSequenceProcessHst();
					BeanUtils.copyProperties(maintReportSequenceProcess2, hst);
					maintReportSequenceProcessDao.save(hst);
				}
				maintReportSequenceProcessDao.flushSession();
				for (MaintReportSequenceProcess maintReportSequenceProcessdel : maintReportSequenceProcessListdb) {
					maintReportSequenceProcessDao.delete(maintReportSequenceProcessdel);
				}
			}
		}
	}

	private List<MaintReportSequenceProcessWrk> getMaintReportSequenceProcessWrkProductList(MaintReportSequenceProcess sesqw) {
		return maintReportSequenceProcessDao.getMaintReportSequenceProcessWrkProductList(sesqw.getLegalEntityCode(), sesqw.getAuditTypeCode(),
				sesqw.getAuditGroupCode(), sesqw.getAuditSubGroupCode());
	}

	private List<MaintReportSequenceProcess> getMaintReportSequenceProcessProductList(MaintReportSequenceProcess sesq) {
		return maintReportSequenceProcessDao.getMaintReportSequenceProcessProductList(sesq.getLegalEntityCode(), sesq.getAuditTypeCode(),
				sesq.getAuditGroupCode(), sesq.getAuditSubGroupCode());
	}

	@Override
	public void saveProcess(List<MaintReportSequenceProcess> mrslist) throws Exception {
		if (mrslist != null && !mrslist.isEmpty()) {
			List<MaintReportSequenceProcess> maRSPListdb = getMaintReportSequenceProcessProcessList(mrslist.get(0));
			List<MaintReportSequenceProcessWrk> mrspWrkListdb = getMaintReportSequenceProcessWrkProcessList(mrslist.get(0));
			if (maRSPListdb.isEmpty() && mrspWrkListdb.isEmpty()) {
				for (MaintReportSequenceProcess maintReportSequenceProcess : mrslist) {
					if (maintReportSequenceProcess.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
						maintReportSequenceProcessDao.save(maintReportSequenceProcess);
					} else {
						MaintReportSequenceProcessWrk maintReportSequenceProcessWrk = new MaintReportSequenceProcessWrk();
						BeanUtils.copyProperties(maintReportSequenceProcess, maintReportSequenceProcessWrk);
						maintReportSequenceProcessDao.save(maintReportSequenceProcessWrk);
					}
				}
			} else {
				throw new Exception("Sequence is already exists");
			}
		}
	}

	@Override
	public void updateProcess(List<MaintReportSequenceProcess> maintReportSequenceProcesslist) {
		if (maintReportSequenceProcesslist != null && !maintReportSequenceProcesslist.isEmpty()) {
			deleteProcess(maintReportSequenceProcesslist.get(0));
			for (MaintReportSequenceProcess maintReportSequenceProcess : maintReportSequenceProcesslist) {
				if (maintReportSequenceProcess.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
					maintReportSequenceProcessDao.save(maintReportSequenceProcess);
				} else {
					MaintReportSequenceProcessWrk maintReportSequenceProcessWrk = new MaintReportSequenceProcessWrk();
					BeanUtils.copyProperties(maintReportSequenceProcess, maintReportSequenceProcessWrk);
					maintReportSequenceProcessDao.save(maintReportSequenceProcessWrk);
				}
			}
		}
	}

	public void deleteProcess(MaintReportSequenceProcess maintReportSequenceProcess) {
		if (maintReportSequenceProcess.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
			List<MaintReportSequenceProcess> maintReportSequenceProcessListdb = getMaintReportSequenceProcessProcessList(maintReportSequenceProcess);
			List<MaintReportSequenceProcessWrk> maintReportSequenceProcessWrkListdb = getMaintReportSequenceProcessWrkProcessList(
					maintReportSequenceProcess);
			for (MaintReportSequenceProcessWrk maintReportSequenceProcess2 : maintReportSequenceProcessWrkListdb) {
				MaintReportSequenceProcessHst hst = new MaintReportSequenceProcessHst();
				BeanUtils.copyProperties(maintReportSequenceProcess2, hst);
				maintReportSequenceProcessDao.save(hst);
			}
			maintReportSequenceProcessDao.flushSession();
			for (MaintReportSequenceProcess maintReportSequenceProcess2 : maintReportSequenceProcessListdb) {
				maintReportSequenceProcessDao.delete(maintReportSequenceProcess2);
			}
			for (MaintReportSequenceProcessWrk maintReportSequenceProcessWrk : maintReportSequenceProcessWrkListdb) {
				maintReportSequenceProcessDao.delete(maintReportSequenceProcessWrk);
			}
		} else {
			List<MaintReportSequenceProcessWrk> maintReportSequenceProcessWrkListdb = getMaintReportSequenceProcessWrkProcessList(
					maintReportSequenceProcess);
			if (maintReportSequenceProcessWrkListdb != null) {
				for (MaintReportSequenceProcessWrk maintReportSequenceProcess2 : maintReportSequenceProcessWrkListdb) {
					MaintReportSequenceProcessHst hst = new MaintReportSequenceProcessHst();
					BeanUtils.copyProperties(maintReportSequenceProcess2, hst);
					maintReportSequenceProcessDao.save(hst);
				}
				maintReportSequenceProcessDao.flushSession();
				for (MaintReportSequenceProcessWrk maintReportSequenceProcessWrk : maintReportSequenceProcessWrkListdb) {
					maintReportSequenceProcessDao.delete(maintReportSequenceProcessWrk);
				}
			} else {
				List<MaintReportSequenceProcess> maintReportSequenceProcessListdb = getMaintReportSequenceProcessProcessList(
						maintReportSequenceProcess);
				for (MaintReportSequenceProcess maintReportSequenceProcess2 : maintReportSequenceProcessListdb) {
					MaintReportSequenceProcessHst hst = new MaintReportSequenceProcessHst();
					BeanUtils.copyProperties(maintReportSequenceProcess2, hst);
					maintReportSequenceProcessDao.save(hst);
				}
				maintReportSequenceProcessDao.flushSession();
				for (MaintReportSequenceProcess maintReportSequenceProcessdel : maintReportSequenceProcessListdb) {
					maintReportSequenceProcessDao.delete(maintReportSequenceProcessdel);
				}
			}
		}
	}

	private List<MaintReportSequenceProcessWrk> getMaintReportSequenceProcessWrkProcessList(MaintReportSequenceProcess sesqw) {
		return maintReportSequenceProcessDao.getMaintReportSequenceProcessWrkList(sesqw.getLegalEntityCode(), sesqw.getAuditTypeCode(),
				sesqw.getAuditGroupCode(), sesqw.getAuditSubGroupCode(), sesqw.getActivityCode());
	}

	private List<MaintReportSequenceProcess> getMaintReportSequenceProcessProcessList(MaintReportSequenceProcess sesq) {
		return maintReportSequenceProcessDao.getMaintReportSequenceProcessList(sesq.getLegalEntityCode(), sesq.getAuditTypeCode(),
				sesq.getAuditGroupCode(), sesq.getAuditSubGroupCode(), sesq.getActivityCode());

	}

	@Override
	public void saveCheckList(List<MaintReportSequenceProcess> mrslist) throws Exception {
		if (mrslist != null && !mrslist.isEmpty()) {
			List<MaintReportSequenceProcess> maRSPListdb = getMaintReportSequenceProcessCheckListList(mrslist.get(0));
			List<MaintReportSequenceProcessWrk> mrspWrkListdb = getMaintReportSequenceProcessWrkCheckListList(mrslist.get(0));
			if (maRSPListdb.isEmpty() && mrspWrkListdb.isEmpty()) {
				for (MaintReportSequenceProcess maintReportSequenceProcess : mrslist) {
					if (maintReportSequenceProcess.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
						maintReportSequenceProcessDao.save(maintReportSequenceProcess);
					} else {
						MaintReportSequenceProcessWrk maintReportSequenceProcessWrk = new MaintReportSequenceProcessWrk();
						BeanUtils.copyProperties(maintReportSequenceProcess, maintReportSequenceProcessWrk);
						maintReportSequenceProcessDao.save(maintReportSequenceProcessWrk);
					}
				}
			} else {
				throw new Exception("Sequence is alredy present");
			}
		}
	}

	@Override
	public void updateCheckList(List<MaintReportSequenceProcess> maintReportSequenceProcesslist) {
		if (maintReportSequenceProcesslist != null && !maintReportSequenceProcesslist.isEmpty()) {
			deleteCheckList(maintReportSequenceProcesslist.get(0));
			for (MaintReportSequenceProcess maintReportSequenceProcess : maintReportSequenceProcesslist) {
				if (maintReportSequenceProcess.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
					maintReportSequenceProcessDao.save(maintReportSequenceProcess);
				} else {
					MaintReportSequenceProcessWrk maintReportSequenceProcessWrk = new MaintReportSequenceProcessWrk();
					BeanUtils.copyProperties(maintReportSequenceProcess, maintReportSequenceProcessWrk);
					maintReportSequenceProcessDao.save(maintReportSequenceProcessWrk);
				}
			}
		}
	}

	public void deleteCheckList(MaintReportSequenceProcess maintReportSequenceProcess) {
		if (maintReportSequenceProcess.getStatus().equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)) {
			List<MaintReportSequenceProcess> maintReportSequenceProcessListdb = getMaintReportSequenceProcessCheckListList(
					maintReportSequenceProcess);
			List<MaintReportSequenceProcessWrk> maintReportSequenceProcessWrkListdb = getMaintReportSequenceProcessWrkCheckListList(
					maintReportSequenceProcess);
			for (MaintReportSequenceProcessWrk maintReportSequenceProcess2 : maintReportSequenceProcessWrkListdb) {
				MaintReportSequenceProcessHst hst = new MaintReportSequenceProcessHst();
				BeanUtils.copyProperties(maintReportSequenceProcess2, hst);
				maintReportSequenceProcessDao.save(hst);
			}
			maintReportSequenceProcessDao.flushSession();
			for (MaintReportSequenceProcess maintReportSequenceProcess2 : maintReportSequenceProcessListdb) {
				maintReportSequenceProcessDao.delete(maintReportSequenceProcess2);
			}
			for (MaintReportSequenceProcessWrk maintReportSequenceProcessWrk : maintReportSequenceProcessWrkListdb) {
				maintReportSequenceProcessDao.delete(maintReportSequenceProcessWrk);
			}
		} else {
			List<MaintReportSequenceProcessWrk> maintReportSequenceProcessWrkListdb = getMaintReportSequenceProcessWrkCheckListList(
					maintReportSequenceProcess);
			if (maintReportSequenceProcessWrkListdb != null) {
				for (MaintReportSequenceProcessWrk maintReportSequenceProcess2 : maintReportSequenceProcessWrkListdb) {
					MaintReportSequenceProcessHst hst = new MaintReportSequenceProcessHst();
					BeanUtils.copyProperties(maintReportSequenceProcess2, hst);
					maintReportSequenceProcessDao.save(hst);
				}
				maintReportSequenceProcessDao.flushSession();
				for (MaintReportSequenceProcessWrk maintReportSequenceProcessWrk : maintReportSequenceProcessWrkListdb) {
					maintReportSequenceProcessDao.delete(maintReportSequenceProcessWrk);
				}
			} else {
				List<MaintReportSequenceProcess> maintReportSequenceProcessListdb = getMaintReportSequenceProcessCheckListList(
						maintReportSequenceProcess);
				for (MaintReportSequenceProcess maintReportSequenceProcess2 : maintReportSequenceProcessListdb) {
					MaintReportSequenceProcessHst hst = new MaintReportSequenceProcessHst();
					BeanUtils.copyProperties(maintReportSequenceProcess2, hst);
					maintReportSequenceProcessDao.save(hst);
				}
				maintReportSequenceProcessDao.flushSession();
				for (MaintReportSequenceProcess maintReportSequenceProcessdel : maintReportSequenceProcessListdb) {
					maintReportSequenceProcessDao.delete(maintReportSequenceProcessdel);
				}
			}
		}
	}

	private List<MaintReportSequenceProcessWrk> getMaintReportSequenceProcessWrkCheckListList(MaintReportSequenceProcess sesqw) {
		return maintReportSequenceProcessDao.getMaintReportSequenceProcessWrkChekListList(sesqw.getLegalEntityCode(), sesqw.getAuditTypeCode(),
				sesqw.getAuditGroupCode(), sesqw.getAuditSubGroupCode(), sesqw.getActivityCode(), sesqw.getProcessCode());
	}

	private List<MaintReportSequenceProcess> getMaintReportSequenceProcessCheckListList(MaintReportSequenceProcess sesq) {
		return maintReportSequenceProcessDao.getMaintReportSequenceProcessChekListList(sesq.getLegalEntityCode(), sesq.getAuditTypeCode(),
				sesq.getAuditGroupCode(), sesq.getAuditSubGroupCode(), sesq.getActivityCode(), sesq.getProcessCode());
	}
	
	@Override
	public List<SequenceDto> getMaintReportSequenceProcessMappingValues(String legalEntityCode, String auditTypeCode, String auditGroupCode,
			String auditSubGroupCode, String activityId, String processId, String findingId, String status, String maker) {
		if (BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(status)) {
			return maintReportSequenceProcessDao.getMaintReportSequenceProcessMappingValues(legalEntityCode, auditTypeCode, auditGroupCode,
					auditSubGroupCode, activityId, processId, findingId, status);
		} else {
			List<SequenceDto> list1 = maintReportSequenceProcessDao.getMaintReportSequenceProcessWrkMappingValues(legalEntityCode, auditTypeCode, auditGroupCode,
					auditSubGroupCode, activityId, processId, findingId, status);
			
			if(auditGroupCode == null && activityId == null && processId == null && list1.isEmpty()) {
				 initiateMaintReportSequenceProcess(Integer.parseInt(legalEntityCode),auditTypeCode,maker);
			}
			
			List<SequenceDto> list2 = maintReportSequenceProcessDao.getActivityProcessFindingMappings(legalEntityCode, auditTypeCode, auditGroupCode, 
					auditSubGroupCode, activityId, processId, findingId, status);
			 Set<SequenceDto> result = new LinkedHashSet<SequenceDto>();
			 if(list1 != null)
				 result.addAll(list1);
			 if(list2 != null)
				 result.addAll(list2);
			return new ArrayList<SequenceDto>(result);
		}

	}

	@Override
	public DataTableResponse getMaintReportSequenceProcess(Integer legalEntityCode, String search, Integer orderColumn, String orderDirection,
			Integer page, Integer size, String auditTypeCode) {
		return maintReportSequenceProcessDao.getMaintReportSequenceProcess(legalEntityCode, search, orderColumn, orderDirection, page, size, auditTypeCode);
	}

	@Override
	public void deleteRejectedMaintReportSequenceProcessByAuditGroupCode(Integer legalEntityCode, String auditTypeCode, String auditGroupCode) {
		List<MaintReportSequenceProcessWrk> deleteList = maintReportSequenceProcessDao
				.getRejectedMaintReportSequenceProcessByAuditGroupCode(legalEntityCode, auditTypeCode, auditGroupCode);
		for (MaintReportSequenceProcessWrk maintReportSequenceProcessWrk : deleteList) {
			MaintReportSequenceProcessHst hst = new MaintReportSequenceProcessHst();
			BeanUtils.copyProperties(maintReportSequenceProcessWrk, hst);
			maintReportSequenceProcessDao.save(hst);
		}
		maintReportSequenceProcessDao.deleteRejectedMaintReportSequenceProcessByAuditGroupCode(legalEntityCode, auditTypeCode, auditGroupCode);
	}
	
	public List<String> getActivityProcessFindingMappingValues(String legalEntityCode, String auditTypeCode, String auditGroupCode, String auditSubGroupCode,
			String activityId, String processId, String findingId, String createOrUpdate) {
		return maintReportSequenceProcessDao.getActivityProcessFindingMappingValues(legalEntityCode, auditTypeCode, auditGroupCode, auditSubGroupCode, activityId, processId, findingId, createOrUpdate);
	}

	@Override
	public boolean getDraftMaintReportSequenceProcess(Integer legalEntityCode, String auditTypeCode, String status) {
		List<MaintReportSequenceProcessWrk> list = maintReportSequenceProcessDao.getDraftMaintReportSequenceProcess(legalEntityCode, auditTypeCode, status);
		if(!list.isEmpty())
			return true;
		return false;
	}

	@Override
	public void initiateMaintReportSequenceProcess(Integer legalEntityCode, String auditTypeCode, String maker) {
		List<SequenceDto> chapterList = maintReportSequenceProcessDao.getActivityProcessFindingMappings(legalEntityCode+"", auditTypeCode, null, null, null, null, null, null);
		for (SequenceDto sequenceDto : chapterList) {
			MaintReportSequenceProcessWrk mwrk = new MaintReportSequenceProcessWrk();
			mwrk.setLegalEntityCode(legalEntityCode);
			mwrk.setAuditTypeCode(auditTypeCode);
			mwrk.setAuditGroupCode(sequenceDto.getCode());
			mwrk.setAgSequence(sequenceDto.getSequenceNo());
			mwrk.setMaker(maker);
			mwrk.setStatus(BankAuditConstant.STATUS_UNAUTH);
			maintReportSequenceProcessDao.save(mwrk);
			saveAllSection(legalEntityCode, auditTypeCode, maker, sequenceDto.getCode(), sequenceDto.getSequenceNo());
		}
	}
	
	
	private void saveAllSection(Integer legalEntityCode, String auditTypeCode, String maker, String auditGroupCode, String auditGroupCodeSq) {
		List<SequenceDto> sectionList = maintReportSequenceProcessDao.getActivityProcessFindingMappings(legalEntityCode+"", auditTypeCode, auditGroupCode, null, null, null, null, null);
		for (SequenceDto sequenceDto : sectionList) {
			MaintReportSequenceProcessWrk mwrk = new MaintReportSequenceProcessWrk();
			mwrk.setLegalEntityCode(legalEntityCode);
			mwrk.setAuditTypeCode(auditTypeCode);
			mwrk.setAuditGroupCode(auditGroupCode);
			mwrk.setAgSequence(auditGroupCodeSq);
			mwrk.setAuditSubGroupCode(sequenceDto.getCode());
			mwrk.setAsgSequence(sequenceDto.getSequenceNo());
			mwrk.setMaker(maker);
			mwrk.setStatus(BankAuditConstant.STATUS_UNAUTH);
			maintReportSequenceProcessDao.save(mwrk);
			saveAllActivity(legalEntityCode, auditTypeCode, maker, auditGroupCode, auditGroupCodeSq, sequenceDto.getCode(), sequenceDto.getSequenceNo());
		}
	}
	
	private void saveAllActivity(Integer legalEntityCode, String auditTypeCode, String maker, String auditGroupCode, String auditGroupCodeSq, String auditSubGroupCode, String auditSubGroupCodeSq) {
		List<SequenceDto> productList = maintReportSequenceProcessDao.getActivityProcessFindingMappings(legalEntityCode+"", auditTypeCode, auditGroupCode, auditSubGroupCode, null, null, null, null);
		for (SequenceDto sequenceDto : productList) {
			MaintReportSequenceProcessWrk mwrk = new MaintReportSequenceProcessWrk();
			mwrk.setLegalEntityCode(legalEntityCode);
			mwrk.setAuditTypeCode(auditTypeCode);
			mwrk.setAuditGroupCode(auditGroupCode);
			mwrk.setAgSequence(auditGroupCodeSq);
			mwrk.setAuditSubGroupCode(auditSubGroupCode);
			mwrk.setAsgSequence(auditSubGroupCodeSq);
			mwrk.setActivityCode(sequenceDto.getCode());
			mwrk.setActSequence(sequenceDto.getSequenceNo());
			mwrk.setMaker(maker);
			mwrk.setStatus(BankAuditConstant.STATUS_UNAUTH);
			maintReportSequenceProcessDao.save(mwrk);
			saveAllProcess(legalEntityCode, auditTypeCode, maker, auditGroupCode, auditGroupCodeSq, auditSubGroupCode, auditSubGroupCodeSq, sequenceDto.getCode(), sequenceDto.getSequenceNo());
		}
	}
	
	private void saveAllProcess(Integer legalEntityCode, String auditTypeCode, String maker, String auditGroupCode,
			String auditGroupCodeSq, String auditSubGroupCode, String auditSubGroupCodeSq, String activityCode, String actSequence) {
		List<SequenceDto> processList = maintReportSequenceProcessDao.getActivityProcessFindingMappings(legalEntityCode+"", auditTypeCode, auditGroupCode, auditSubGroupCode, activityCode, null, null, null);
		for (SequenceDto sequenceDto : processList) {
			MaintReportSequenceProcessWrk mwrk = new MaintReportSequenceProcessWrk();
			mwrk.setLegalEntityCode(legalEntityCode);
			mwrk.setAuditTypeCode(auditTypeCode);
			mwrk.setAuditGroupCode(auditGroupCode);
			mwrk.setAgSequence(auditGroupCodeSq);
			mwrk.setAuditSubGroupCode(auditSubGroupCode);
			mwrk.setAsgSequence(auditSubGroupCodeSq);
			mwrk.setActivityCode(activityCode);
			mwrk.setActSequence(actSequence);
			mwrk.setProcessCode(sequenceDto.getCode());
			mwrk.setPrcSequence(sequenceDto.getSequenceNo());
			mwrk.setMaker(maker);
			mwrk.setStatus(BankAuditConstant.STATUS_UNAUTH);
			maintReportSequenceProcessDao.save(mwrk);
			saveAllFindings(legalEntityCode, auditTypeCode, maker, auditGroupCode, auditGroupCodeSq, 
					auditSubGroupCode, auditSubGroupCodeSq, activityCode, actSequence, sequenceDto.getCode(), sequenceDto.getSequenceNo());
		}
	}
	
	private void saveAllFindings(Integer legalEntityCode, String auditTypeCode, String maker, String auditGroupCode,
			String auditGroupCodeSq, String auditSubGroupCode, String auditSubGroupCodeSq, String activityCode, String actSequence, String processCode, String processSqn) {
		List<SequenceDto> findingList = maintReportSequenceProcessDao.getActivityProcessFindingMappings(legalEntityCode+"", auditTypeCode, auditGroupCode, auditSubGroupCode, activityCode, processCode, null, null);
		for (SequenceDto sequenceDto : findingList) {
			MaintReportSequenceProcessWrk mwrk = new MaintReportSequenceProcessWrk();
			mwrk.setLegalEntityCode(legalEntityCode);
			mwrk.setAuditTypeCode(auditTypeCode);
			mwrk.setAuditGroupCode(auditGroupCode);
			mwrk.setAgSequence(auditGroupCodeSq);
			mwrk.setAuditSubGroupCode(auditSubGroupCode);
			mwrk.setAsgSequence(auditSubGroupCodeSq);
			mwrk.setActivityCode(activityCode);
			mwrk.setActSequence(actSequence);
			mwrk.setProcessCode(processCode);
			mwrk.setPrcSequence(processSqn);
			mwrk.setFindingCode(sequenceDto.getCode());
			mwrk.setFndSequence(sequenceDto.getSequenceNo());
			mwrk.setStatus(BankAuditConstant.STATUS_UNAUTH);
			mwrk.setMaker(maker);
			maintReportSequenceProcessDao.save(mwrk);
		}
	}

	@Override
	public void authoriseMaintReportSequenceProcess(Integer legalEntityCode, String auditTypeCode, String checker, String remarks) {
		List<MaintReportSequenceProcess> mainList = maintReportSequenceProcessDao.getMaintReportSequenceProcessList(legalEntityCode, auditTypeCode);
		for (MaintReportSequenceProcess maintReportSequenceProcess : mainList) {
			MaintReportSequenceProcessHst hst = new MaintReportSequenceProcessHst();
			BeanUtils.copyProperties(maintReportSequenceProcess, hst);
			maintReportSequenceProcessDao.save(hst);
		}
		maintReportSequenceProcessDao.flushSession();
		maintReportSequenceProcessDao.deleteMaintReportSequenceProcess(legalEntityCode, auditTypeCode);
		
		List<MaintReportSequenceProcessWrk> wrkList = maintReportSequenceProcessDao.getMaintReportSequenceProcessWrkList(legalEntityCode, auditTypeCode);
		for (MaintReportSequenceProcessWrk wrk : wrkList) {
			MaintReportSequenceProcess main = new MaintReportSequenceProcess();
			BeanUtils.copyProperties(wrk, main);
			main.setChecker(checker);
			main.setAuthRejRemarks(remarks);
			main.setStatus(BankAuditConstant.STATUS_AUTH);
			maintReportSequenceProcessDao.save(main);
		}
		maintReportSequenceProcessDao.flushSession();
		maintReportSequenceProcessDao.deleteMaintReportSequenceProcessWrk(legalEntityCode, auditTypeCode);
	}
	
}
