package com.bankaudit.rest.process.service;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bankaudit.dto.process.AuditPlanDto;
import com.bankaudit.process.model.AuditPlan;
import com.bankaudit.process.model.AuditPlanHst;
import com.bankaudit.process.model.AuditPlanWrk;
import com.bankaudit.process.model.AuditSchedule;
import com.bankaudit.process.model.AuditScheduleHst;
import com.bankaudit.process.model.AuditScheduleWrk;
import com.bankaudit.process.model.AuditTeam;
import com.bankaudit.process.model.AuditTeamHst;
import com.bankaudit.process.model.AuditTeamWrk;
import com.bankaudit.rest.process.dao.AuditPlanDao;
import com.bankaudit.rest.process.dao.AuditTeamDao;
import com.bankaudit.rest.process.dao.MyAuditsDao;
import com.bankaudit.service.BusinessDateService;
import com.bankaudit.service.SequenceAppenderService;
import com.bankaudit.util.BankAuditUtil;

@Service("auditPlanService")
@Transactional("transactionManager")
public class AuditPlanServiceImpl implements AuditPlanService{

	@Autowired
    AuditPlanDao auditPlanDao;
	
	@Autowired
	AuditScheduleService auditScheduleService;
	
	@Autowired
	BusinessDateService businessDateService;
	
	@Autowired
	AuditTeamService auditTeamService;
	
	@Autowired
	AuditTeamDao auditTeamDao;
	
	@Autowired
	SequenceAppenderService sequenceAppenderService;
	
	@Autowired
	MyAuditsDao myAuditsDao;	
	
	static final Logger logger = Logger.getLogger(AuditPlanServiceImpl.class);
	
	@Override
	public void createAuditPlanWrk(AuditPlanWrk auditPlanWrk) {
		
		if(BankAuditUtil.isEmptyString(auditPlanWrk.getPlanId())){
			auditPlanWrk.setPlanId(sequenceAppenderService.getAutoSequenceId());
		}else {
			auditPlanDao.deleteAuditPlanWrk(auditPlanWrk);
			auditPlanDao.flushSession();
		}
		auditPlanDao.save(auditPlanWrk);
		
		
		System.out.println("auditPlanWrk : "+ auditPlanWrk);
		
		
		for (AuditScheduleWrk auditScheduleWrk : auditPlanWrk.getAuditScheduleWrks()) {
			auditScheduleWrk.setAuditId(sequenceAppenderService.getAutoSequenceId());
			auditScheduleWrk.setBusinessdateTimestamp(auditPlanWrk.getBusinessdateTimestamp());
			auditScheduleWrk.setSysdateTimestamp(auditPlanWrk.getSysdateTimestamp());
			auditScheduleWrk.setMakerTimestamp(auditPlanWrk.getMakerTimestamp());
			auditScheduleWrk.setMaker(auditPlanWrk.getMaker());
			auditScheduleWrk.setPlanId(auditPlanWrk.getPlanId());
			auditScheduleWrk.setPlanType(auditPlanWrk.getPlanType());
			
			System.out.println(" auditScheduleWrk "+ auditScheduleWrk);
			
			auditScheduleService.createAuditScheduleWrk(auditScheduleWrk);
			
			if (auditScheduleWrk.getAuditTeamWrks()!=null){
				for (AuditTeamWrk auditTeamWrk : auditScheduleWrk.getAuditTeamWrks()) {
					auditTeamWrk.setPlanId(auditScheduleWrk.getPlanId());
					auditTeamWrk.setAuditId(auditScheduleWrk.getAuditId());
					
					System.out.println(" auditTeamWrk "+ auditTeamWrk);
					
					auditTeamService.createAuditTeamWrk(auditTeamWrk);
				}
			}
		}
		
	}

	@Override
	public AuditPlanWrk getByPlanId(String id) {
		Map<String, Object> properties=new HashMap<String,Object>();
		properties.put("planId", id);
		return(AuditPlanWrk)auditPlanDao.getUniqueEntityByMatchingProperties(AuditPlanWrk.class, properties);
	}

	@Override
	public void updateAuditPlanWrk(AuditPlanWrk auditPlanWrk) {


		if(auditPlanWrk.getStatus().equalsIgnoreCase("U")
				||auditPlanWrk.getStatus().equalsIgnoreCase("M")
				||auditPlanWrk.getStatus().equalsIgnoreCase("R")
				){

			
			//Adhoc Plan, drop id any none adhoc is conflict
			if(		!BankAuditUtil.isEmptyString(auditPlanWrk.getPlanType())
					&& auditPlanWrk.getPlanType().equals("Y")
					&& !BankAuditUtil.isEmptyString(auditPlanWrk.getAuditScheduleWrks().get(0).getPlannedAuditContinueDrop()) 
					&& auditPlanWrk.getAuditScheduleWrks().get(0).getPlannedAuditContinueDrop().equalsIgnoreCase("drop")) {
			        	 
				// get List of the plans are conflicting with Adhoc plan
				
				String plnStDate = BankAuditUtil.parseDateToString(auditPlanWrk.getPlanStartDate());
				String plnEnDate = BankAuditUtil.parseDateToString(auditPlanWrk.getPlanEndDate());  
			
				System.out.println("Pls SD :"+plnStDate);
				System.out.println("Pls SE :"+plnEnDate);
				
				String planIds=auditPlanDao.getAdhocPlanConflicts(auditPlanWrk.getLegalEntityCode(),auditPlanWrk.getAuditTypeCode(),auditPlanWrk.getAuditScheduleWrks().get(0).getUnitId(),
						plnStDate,plnEnDate);
				
				if(!BankAuditUtil.isEmptyString(planIds)) {
					String[] plns=planIds.split(",");

					for (String planId : plns) {

						AuditPlanWrk pwk =	this.getByPlanId(planId);
						
						auditPlanDao.deleteAuditPlanWrk(pwk);
						auditPlanDao.flushSession();
						auditPlanDao.save(pwk);

						for (AuditScheduleWrk auditScheduleWrk : pwk.getAuditScheduleWrks()) {
							if(BankAuditUtil.isEmptyString(auditScheduleWrk.getAuditId())){
								auditScheduleWrk.setAuditId(sequenceAppenderService.getAutoSequenceId());
							}
							auditScheduleWrk.setBusinessdateTimestamp(pwk.getBusinessdateTimestamp());
							auditScheduleWrk.setSysdateTimestamp(pwk.getSysdateTimestamp());
							auditScheduleWrk.setMakerTimestamp(pwk.getMakerTimestamp());
							auditScheduleWrk.setMaker(pwk.getMaker());
							auditScheduleWrk.setPlanId(pwk.getPlanId());
							auditScheduleService.createAuditScheduleWrk(auditScheduleWrk);
							if(auditScheduleWrk.getAuditTeamWrks()!=null){
								for (AuditTeamWrk auditTeamWrk : auditScheduleWrk.getAuditTeamWrks()) {
									auditTeamWrk.setPlanId(auditScheduleWrk.getPlanId());
									auditTeamWrk.setAuditId(auditScheduleWrk.getAuditId());
									auditTeamService.createAuditTeamWrk(auditTeamWrk);
								}
							}
						}

					}

				}//enf
				
			}
			
			Map<String, Object> properties=new HashMap<>();
			properties.put("legalEntityCode",auditPlanWrk.getLegalEntityCode());
			properties.put("auditTypeCode",auditPlanWrk.getAuditTypeCode());
			properties.put("planId",auditPlanWrk.getPlanId());

			AuditPlanWrk auditPlanWrkDb=(AuditPlanWrk)auditPlanDao.getUniqueEntityByMatchingProperties(AuditPlanWrk.class, properties);

			if(!auditPlanWrk.getStatus().equalsIgnoreCase("R")){
				//save to history table 
				createAuditPlanHst(new AuditPlanHst(auditPlanWrkDb));
			}else{
				auditPlanWrk.setCheckerTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(auditPlanWrk.getLegalEntityCode()));
			}


			// then delete
			auditPlanDao.deleteAuditPlanWrk(auditPlanWrkDb);

			auditPlanDao.flushSession();

			auditPlanDao.save(auditPlanWrk);

			for (AuditScheduleWrk auditScheduleWrk : auditPlanWrk.getAuditScheduleWrks()) {
				if(BankAuditUtil.isEmptyString(auditScheduleWrk.getAuditId())){
					auditScheduleWrk.setAuditId(sequenceAppenderService.getAutoSequenceId());
				}
				auditScheduleWrk.setBusinessdateTimestamp(auditPlanWrk.getBusinessdateTimestamp());
				auditScheduleWrk.setSysdateTimestamp(auditPlanWrk.getSysdateTimestamp());
				auditScheduleWrk.setMakerTimestamp(auditPlanWrk.getMakerTimestamp());
				auditScheduleWrk.setMaker(auditPlanWrk.getMaker());
				auditScheduleWrk.setPlanId(auditPlanWrk.getPlanId());
				auditScheduleService.createAuditScheduleWrk(auditScheduleWrk);
				if(auditScheduleWrk.getAuditTeamWrks()!=null){

					for (AuditTeamWrk auditTeamWrk : auditScheduleWrk.getAuditTeamWrks()) {
						auditTeamWrk.setPlanId(auditScheduleWrk.getPlanId());
						auditTeamWrk.setAuditId(auditScheduleWrk.getAuditId());
						auditTeamService.createAuditTeamWrk(auditTeamWrk);
					}

				}
			}
		}else if (auditPlanWrk.getStatus().equalsIgnoreCase("DF")) {

			auditPlanDao.deleteAuditPlanWrk(auditPlanWrk);
			auditPlanDao.flushSession();
			auditPlanDao.save(auditPlanWrk);

			for (AuditScheduleWrk auditScheduleWrk : auditPlanWrk.getAuditScheduleWrks()) {
				if(BankAuditUtil.isEmptyString(auditScheduleWrk.getAuditId())){
					auditScheduleWrk.setAuditId(sequenceAppenderService.getAutoSequenceId());
				}
				auditScheduleWrk.setBusinessdateTimestamp(auditPlanWrk.getBusinessdateTimestamp());
				auditScheduleWrk.setSysdateTimestamp(auditPlanWrk.getSysdateTimestamp());
				auditScheduleWrk.setMakerTimestamp(auditPlanWrk.getMakerTimestamp());
				auditScheduleWrk.setMaker(auditPlanWrk.getMaker());
				auditScheduleWrk.setPlanId(auditPlanWrk.getPlanId());
				auditScheduleService.createAuditScheduleWrk(auditScheduleWrk);
				if(auditScheduleWrk.getAuditTeamWrks()!=null){
					for (AuditTeamWrk auditTeamWrk : auditScheduleWrk.getAuditTeamWrks()) {
						auditTeamWrk.setPlanId(auditScheduleWrk.getPlanId());
						auditTeamWrk.setAuditId(auditScheduleWrk.getAuditId());
						auditTeamService.createAuditTeamWrk(auditTeamWrk);
					}
				}
			}

		}else if (auditPlanWrk.getStatus().equalsIgnoreCase("A")) {

			//save into history
			Map<String, Object> properties=new HashMap<>();
			properties.put("legalEntityCode",auditPlanWrk.getLegalEntityCode());
			properties.put("auditTypeCode",auditPlanWrk.getAuditTypeCode());
			properties.put("planId",auditPlanWrk.getPlanId());
			String status = auditPlanWrk.getStatus();
			
			
			AuditPlan auditPlanDb=(AuditPlan)auditPlanDao.getUniqueEntityByMatchingProperties(AuditPlan.class, properties);
			//auditPlanDb.setPlanType(auditPlanWrk.getPlanType());
			AuditPlanWrk auditPlanWrkDb=(AuditPlanWrk)auditPlanDao.getUniqueEntityByMatchingProperties(AuditPlanWrk.class, properties);
			//auditPlanWrkDb.setPlanType(auditPlanWrk.getPlanType());
			
			if(auditPlanDb!=null){
				/*	auditPlanDao.save(new AuditPlanHst(auditPlanDb));	*/
				createAuditPlanHst(new AuditPlanHst(auditPlanDb));
			}


			//delete from work
			auditPlanDao.deleteAuditPlanWrk(auditPlanWrk);

			//delete from master
			/*auditPlanDao.deleteAuditPlan();*/

			//save into the master
			// createAuditPlanHst(new AuditPlanHst(auditPlanWrk));
			auditPlanDao.flushSession();


			auditPlanWrkDb.setBusinessdateTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(auditPlanWrkDb.getLegalEntityCode()));
			//auditPlanWrkDb.setStatus(auditPlanWrk.getStatus());
			auditPlanWrkDb.setStatus(status);
			auditPlanWrkDb.setCheckerTimestamp(auditPlanWrkDb.getBusinessdateTimestamp());
			auditPlanWrkDb.setChecker(auditPlanWrk.getChecker());
			//auditPlanWrkDb.setPlanType(auditPlanWrk.getPlanType());
			createAuditPlan(new AuditPlan(auditPlanWrkDb));

		}else if (auditPlanWrk.getStatus().equalsIgnoreCase("D")) {
			Map<String, Object> properties=new HashMap<>();
			properties.put("legalEntityCode",auditPlanWrk.getLegalEntityCode());
			properties.put("auditTypeCode",auditPlanWrk.getAuditTypeCode());
			properties.put("planId",auditPlanWrk.getPlanId());

			AuditPlanWrk auditPlanWrkDb=(AuditPlanWrk)auditPlanDao.getUniqueEntityByMatchingProperties(AuditPlanWrk.class, properties);
			auditPlanWrkDb.setPlanType(auditPlanWrk.getPlanType());
			//save into history
			createAuditPlanHst(new AuditPlanHst(auditPlanWrkDb));

			//delete from working table 
			auditPlanDao.deleteAuditPlanWrk(auditPlanWrkDb);
		}

	}

	

	@Override
	public Boolean isAuditPlanWrk(Integer legalEntityCode, String auditTypeCode, Date planStartDate,String planType) {
		
		return auditPlanDao.isAuditPlanWrk( legalEntityCode,  auditTypeCode,  planStartDate, planType);
	}

	@Override
	public void createAuditPlanHst(AuditPlanHst auditPlanHst) {
		
		auditPlanDao.save(auditPlanHst);
		for (AuditScheduleHst auditScheduleHst : auditPlanHst.getAuditScheduleHsts()) {
			auditScheduleHst.setPlanAutoId(auditPlanHst.getId());
			auditPlanDao.save(auditScheduleHst);
			for (AuditTeamHst auditTeamHst : auditScheduleHst.getAuditTeamHsts()) {
				auditTeamHst.setScheAutoId(auditScheduleHst.getId());
				auditPlanDao.save(auditTeamHst);
			}
		}		
	}

	@Override
	public void createAuditPlan(AuditPlan auditPlan) {
		auditPlan.setCheckerTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(auditPlan.getLegalEntityCode()));
		auditPlanDao.save(auditPlan);
		for (AuditSchedule auditScheduleHst : auditPlan.getAuditSchedules()) {
			auditScheduleHst.setCheckerTimestamp(auditPlan.getCheckerTimestamp());
			if("A".equalsIgnoreCase(auditPlan.getStatus())){
				if(auditScheduleHst.getAuditStatus() !=null && auditScheduleHst.getAuditStatus().equalsIgnoreCase("AN"))
					auditScheduleHst.setAuditStatus("AU");
			}
			auditPlanDao.save(auditScheduleHst);
			if(auditScheduleHst.getAuditTeams()!=null){
				for (AuditTeam auditTeamHst : auditScheduleHst.getAuditTeams()) {
					auditPlanDao.save(auditTeamHst);
				}
			}
		}		
	}
	
	@Override
	public AuditPlanDto getPlanDetailsByPlanId(Integer legalEntityCode, String auditTypeCode, String planId, AuditPlanDto apd) {		
		logger.info("Inside getPlanDetailsByPlanId .. "+legalEntityCode+" :: "+ auditTypeCode+" :: "+planId);
		
		AuditPlan ap=auditPlanDao.getPlanDetailsByPlanId(legalEntityCode, auditTypeCode, planId);
		logger.info("PlanStartDate.. "+ ap.getPlanStartDate());
		String planStartDateStr=BankAuditUtil.parseDateToString(ap.getPlanStartDate());
		apd.setPlanYear(planStartDateStr.substring(0,4));
		logger.info("planStartDateStr.. "+ planStartDateStr + " Year::"+planStartDateStr.substring(0,4));
		apd.setPlanStartDateD(ap.getPlanStartDate());
		apd.setPlanEndDateD(ap.getPlanEndDate());
		
		return apd;
	}
	
	@Override
	public AuditPlanDto getPlanDetails(Integer legalEntityCode, AuditPlanDto apd) {
		
		logger.info("Inside getPlanDetails .. "+legalEntityCode+" :: "+ apd.getAuditTypeCode()+" :: "+apd.getPlanEndDate());
		String planEndDateStr=null;
		LocalDate planEndDateLclDt=null;
		
		/***** Logic to find out the Start and End of FY, based on PlanEndDate from XLS ******/
		// If Plan End date is NULL, then consider the current FY
		// If Plan End date is not NULL, then derive the FY
		
		if(apd.getPlanEndDate()!=null) {
			String planEndDate=apd.getPlanEndDate();
			if(planEndDate.contains("-")) planEndDateStr= planEndDate.substring(6, 10)+"-"+planEndDate.substring(3, 5)+"-"+planEndDate.substring(0, 2);
			else planEndDateStr= planEndDate.substring(4, 8)+"-"+planEndDate.substring(2, 4)+"-"+planEndDate.substring(0, 2);
			planEndDateLclDt= LocalDate.parse(planEndDateStr);
		}else planEndDateLclDt=LocalDate.now();
		
		logger.info("planEndDateStr .."+ planEndDateStr+" planEndDateLclDt .."+planEndDateLclDt);
		List<LocalDate> fyDates= BankAuditUtil.getFinancialStartEndDatesFrom(planEndDateLclDt);
				
		LocalDate fYStartDt = fyDates.get(0);  // Financial year start date
		LocalDate fYEndDt = fyDates.get(1);	  // Financial year End date
		
		logger.info("fYStartDt .."+fYStartDt+" fYEndt .."+fYEndDt);
		
		
		logger.info("FYStartDat :: "+fYStartDt+" FYEndDate:: "+fYEndDt);
		
		// To get the serial no for Plan creation based on Audit type code
		String planSlNo=apd.getAuditTypeSlNoMap()!=null?apd.getAuditTypeSlNoMap().get(apd.getAuditTypeCode()):"";
		logger.info(" planSlNo .. "+planSlNo);
		
		/*********** Fetch OR Create Plan Id ************/
		  //If PlanId=NULL && PlanEndDate=NULL => Fetch the latest PlanId
		  //If PlanId=NULL && PlanEndDate<>NULL && PlanEndDate<=CurrentFYear  => Fetch the latest PlanId
		  //If PlanId=NULL && PlanEndDate<>NULL && PlanEndDate>CurrentFYear  => Fetch the Plan created for 
		AuditPlan ap=auditPlanDao.getLatestPlan(legalEntityCode, apd.getAuditTypeCode(),fYStartDt, fYEndDt);
		logger.info("AuditPlan .. "+ ap);
		
		/*********** Create Plan Id ************/
		  //If PlanId=NULL && PlanEndDate <> NULL && No Plan for FY of(PlanEndDate) => Create the latest PlanId	
		if(ap ==null)
			ap=createAndGetPlan(legalEntityCode, apd.getAuditTypeCode(), apd.getPlanMaker(), fYStartDt, fYEndDt, planSlNo);
		
		logger.info("Plan id  .."+ap.getPlanId());
		apd.setPlanId(ap.getPlanId());
		apd.setPlanYear(String.valueOf(fYStartDt.getYear()));
		apd.setPlanStartDateD(ap.getPlanStartDate());
		apd.setPlanEndDateD(ap.getPlanEndDate());
		
		return apd;
		
	}	
	
	public AuditPlan createAndGetPlan(Integer legalEntityCode, String auditTypeCode, String maker, LocalDate fYStartDt, LocalDate fYEndDt, String slNo){
		logger.info("Inside createAndGetPlan .. "+ legalEntityCode +" :: "+auditTypeCode+" :: "+ fYStartDt + "::"+ fYEndDt+" :: "+slNo+" :: "+fYStartDt.getYear());
		AuditPlan ap=null;
		String planYear=String.valueOf(fYStartDt.getYear()).substring(2, 4);
		
		try {
			ap= new AuditPlan();
			ap.setLegalEntityCode(legalEntityCode);
			ap.setAuditTypeCode(auditTypeCode);
			ap.setPlanId("P"+planYear+"00"+slNo);
			ap.setPlanType("N");
			ap.setPlanStartDate(java.sql.Date.valueOf(fYStartDt));
			ap.setPlanEndDate(java.sql.Date.valueOf(fYEndDt));
			ap.setStatus("A");
			ap.setMaker(maker);
			ap.setMakerTimestamp(new Timestamp(System.currentTimeMillis()));
			auditPlanDao.saveOrUpdate(ap);
		
		}catch(Exception e) {
			e.printStackTrace(System.out);
		}
		return ap;
	}
	
	@Override
	public String createAndGetAuditSchedule(Integer legalEntityCode, AuditPlanDto apd){
		logger.info("Inside createAndGetAuditSchedule .. "+ legalEntityCode +" :: "+apd.getAuditTypeCode()+" :: "+ apd.getPlanId() + "::"+ apd.getUnitId() + "::"+ apd.getScheduleStartDate()+" :: "+apd.getScheduleEndDate());
		AuditSchedule sch=null;
		String planYear=apd.getPlanYear().substring(2, 4); // to get the last 2 digit of the Year
		String auditSlNo=apd.getAuditTypeSlNoMap()!=null?apd.getAuditTypeSlNoMap().get(apd.getAuditTypeCode()):"";
		String auditIdNew=null;		
		logger.info("planYear .."+planYear +" auditSlNo .."+auditSlNo);
		try {
			sch= new AuditSchedule();
			sch.setLegalEntityCode(legalEntityCode);
			sch.setAuditTypeCode(apd.getAuditTypeCode());
			sch.setPlanId(apd.getPlanId());
			sch.setAuditId("A"+planYear+apd.getUnitId()+"0"+auditSlNo);
			sch.setUnitId(apd.getUnitId());
			sch.setScheduleStartDate(BankAuditUtil.parseStringToDateddMMyyyy(apd.getScheduleStartDate()));
			sch.setScheduleEndDate(BankAuditUtil.parseStringToDateddMMyyyy(apd.getScheduleEndDate()));
			sch.setAuditStatus("AP");
			sch.setStatus("A");sch.setEntityStatus("A");
			sch.setMaker(apd.getPlanMaker());
			sch.setMakerTimestamp(new Timestamp(System.currentTimeMillis()));
			auditIdNew=sch.getAuditId();
			auditPlanDao.saveOrUpdate(sch);
			
			logger.info("auditIdNew .."+auditIdNew +"Team Lead .. "+ apd.getTeamLead());
			if(apd.getTeamLead()!=null && !"".equalsIgnoreCase(apd.getTeamLead())) {				
				AuditTeam at=null;
				at= new AuditTeam();
				at.setLegalEntityCode(legalEntityCode);
				at.setAuditTypeCode(apd.getAuditTypeCode());
				at.setPlanId(apd.getPlanId());
				at.setAuditId(auditIdNew);
				at.setUnitId(apd.getUnitId());
				at.setUserId(apd.getTeamLead());
				at.setUserType("L");
				auditPlanDao.saveOrUpdate(at);
			}
			
			logger.info("auditIdNew .."+auditIdNew +"Team Memebr 1 .. "+ apd.getTeamMember1());
			if(apd.getTeamMember1()!=null && !"".equalsIgnoreCase(apd.getTeamMember1())) {
				AuditTeam at=null;
				at= new AuditTeam();
				at.setLegalEntityCode(legalEntityCode);
				at.setAuditTypeCode(apd.getAuditTypeCode());
				at.setPlanId(apd.getPlanId());
				at.setAuditId(auditIdNew);
				at.setUnitId(apd.getUnitId());
				at.setUserId(apd.getTeamMember1());
				at.setUserType("M");
				auditPlanDao.saveOrUpdate(at);
			}
			
			logger.info("auditIdNew .."+auditIdNew +"Team Memebr 2 .. "+ apd.getTeamMember2());
			if(apd.getTeamMember2()!=null && !"".equalsIgnoreCase(apd.getTeamMember2())) {
				AuditTeam at=null;
				at= new AuditTeam();
				at.setLegalEntityCode(legalEntityCode);
				at.setAuditTypeCode(apd.getAuditTypeCode());
				at.setPlanId(apd.getPlanId());
				at.setAuditId(auditIdNew);
				at.setUnitId(apd.getUnitId());
				at.setUserId(apd.getTeamMember1());
				at.setUserType("M");
				auditPlanDao.saveOrUpdate(at);
			}
			
			logger.info("auditIdNew .."+auditIdNew +"Team Memebr 3 .. "+ apd.getTeamMember3());
			if(apd.getTeamMember3()!=null && !"".equalsIgnoreCase(apd.getTeamMember3())) {
				AuditTeam at=null;
				at= new AuditTeam();
				at.setLegalEntityCode(legalEntityCode);
				at.setAuditTypeCode(apd.getAuditTypeCode());
				at.setPlanId(apd.getPlanId());
				at.setAuditId(auditIdNew);
				at.setUnitId(apd.getUnitId());
				at.setUserId(apd.getTeamMember3());
				at.setUserType("M");
				auditPlanDao.saveOrUpdate(at);
			}
		
		}catch(Exception e) {
			e.printStackTrace(System.out);
		}
		return auditIdNew;
	}
	
	@Override
	public void createAuditTeam(Integer legalEntityCode, AuditPlanDto adp){
		logger.info("Inside createAuditTeam .. "+ legalEntityCode +" :: "+adp.getAuditTypeCode()+" :: "+ adp.getPlanId()+" :: "+ adp.getAuditId() + "::"+ adp.getUnitId());
		logger.info("Team detail .. "+ adp.getTeamLead() +" :: "+adp.getTeamMember1()+" :: "+ adp.getTeamMember2() + "::"+ adp.getTeamMember3());
		AuditTeam at=null;
		try {
			for(int i=0;i<=3;i++) {
				at= new AuditTeam();
				at.setLegalEntityCode(legalEntityCode);
				at.setAuditTypeCode(adp.getAuditTypeCode());
				at.setPlanId(adp.getPlanId());
				at.setAuditId(adp.getAuditId());
				at.setUnitId(adp.getUnitId());
				at.setUserType("M");
				if(i==0 && adp.getTeamLead()!=null) {
					at.setUserId(adp.getTeamLead());
					at.setUserType("L");
				}else if(i==1 && adp.getTeamMember1()!=null)
					at.setUserId(adp.getTeamMember1());
				else if(i==2 && adp.getTeamMember2()!=null)
					at.setUserId(adp.getTeamMember2());
				else if(i==3 && adp.getTeamMember3()!=null)
					at.setUserId(adp.getTeamMember3());			
				auditPlanDao.saveOrUpdate(at);
			}
		}catch(Exception e) {
			e.printStackTrace(System.out);
		}
	}

	
	@Override
	public void updateAuditScheduleAndTeam(Integer legalEntityCode, AuditPlanDto adp){
		logger.info("Inside updateAuditScheduleAndTeam .. "+ legalEntityCode +" :: "+adp.getAuditTypeCode()+" :: "+ adp.getAuditId() + "::"+ adp.getScheduleStartDate()+" :: "+adp.getScheduleEndDate());
		try {
			AuditSchedule sch=myAuditsDao.getAuditByAuditId(legalEntityCode, adp.getAuditTypeCode(), adp.getAuditId());
			sch.setScheduleStartDate(BankAuditUtil.parseStringToDateddMMyyyy(adp.getScheduleStartDate()));
			sch.setScheduleEndDate(BankAuditUtil.parseStringToDateddMMyyyy(adp.getScheduleEndDate()));
			auditPlanDao.update(sch);	
			
			//auditTeamService.deleteAuditTeamByIdAndType(legalEntityCode, auditId, auditTypeCode, statusUnauth);
			logger.info("Team lead .. "+ adp.getTeamLead());
			if(!BankAuditUtil.isEmptyString(adp.getTeamLead())) {
				List<AuditTeam> tmLst=auditTeamService.getAuditTeamsByAuditIdAndType(legalEntityCode, adp.getAuditTypeCode(), adp.getAuditId());
				logger.info("tmLst .. "+ tmLst);
				if(!tmLst.isEmpty()) {
					auditTeamDao.updateAuditTeamLead(legalEntityCode, adp.getAuditTypeCode(), adp.getAuditId(), adp.getTeamLead());
				}else createAuditTeam(legalEntityCode, adp);
			}
		}catch(Exception e) {
			e.printStackTrace(System.out);
		}
	}
	
}
