package com.bankaudit.rest.process.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.dto.ChecklistDto;
import com.bankaudit.process.model.AuditLevelDocumentDetails;
import com.bankaudit.process.model.AuditObservation;
import com.bankaudit.process.model.AuditRatingComplianceObsScoreTmp;
import com.bankaudit.process.model.AuditRatingParameterScoreTmp;
import com.bankaudit.process.model.AuditSchedule;
import com.bankaudit.process.model.ChecklistOfflineInspectionTmp; 
import com.bankaudit.rest.process.dao.IfcEnsureBaasDataDao;
import com.bankaudit.util.BankAuditUtil;
 

@Service("ifcEnsureBaasDataOfflineUploadTransactionServiceImpl")
public class IfcDataOfflineUploadTransactionServiceImpl {

	
	@Autowired 
	IfcEnsureBaasDataDao ifcEnsureBaasDataDao;	 	
	
	@Transactional("transactionManager")
	public AuditLevelDocumentDetails savePartial(AuditLevelDocumentDetails auditLevelDocumentDetailsToSave){ 	
		auditLevelDocumentDetailsToSave.setReferenceNo(ifcEnsureBaasDataDao.getMaxReferenceNumber()); 
		ifcEnsureBaasDataDao.save(auditLevelDocumentDetailsToSave);
		ifcEnsureBaasDataDao.flushSession();
		return auditLevelDocumentDetailsToSave;
	}
	
	@Transactional("transactionManager")
	public AuditLevelDocumentDetails updateDocStatus(AuditLevelDocumentDetails auditLevelDocumentDetailsToSave){ 
		ifcEnsureBaasDataDao.update(auditLevelDocumentDetailsToSave);  
		ifcEnsureBaasDataDao.flushSession();
		return auditLevelDocumentDetailsToSave;
	}
		
	@Transactional("transactionManager")
	public void saveChecklistData(AuditLevelDocumentDetails auditLevelDocumentDetailsToSave, List<ChecklistOfflineInspectionTmp> checklistOfflineInspectionList,
			int docId, String reportLanguage, Map<String, AuditObservation>  auditObservationMap, String role){
		
		List<String> rolesListInspector = Arrays.asList(("G2,G8").split(","));  //Inspection
		List<String> rolesListCompliance = Arrays.asList(("G3,G31").split(","));	//Compliance or Response
		List<String> rolesListReviewer = Arrays.asList(("G4,G7").split(","));       //review/Scrutiny	 
		
		Map<String, ChecklistDto> standardStmtDataMap = ifcEnsureBaasDataDao.getAllStandardStmtMap(auditLevelDocumentDetailsToSave.getLegalEntityCode(), auditLevelDocumentDetailsToSave.getAuditId(), auditLevelDocumentDetailsToSave.getAuditTypeCode());
		
		Map<String , Object> properties = new HashMap<String, Object>();
		properties.put("legalEntityCode", auditLevelDocumentDetailsToSave.getLegalEntityCode());
		properties.put("auditTypeCode", auditLevelDocumentDetailsToSave.getAuditTypeCode());
		properties.put("auditId", auditLevelDocumentDetailsToSave.getAuditId());			
		AuditSchedule auditSchedule = (AuditSchedule) ifcEnsureBaasDataDao.getUniqueEntityByMatchingProperties(AuditSchedule.class, properties);
		
		List<String> nonComplianceMappingIds = new ArrayList<>(); 		
		if(rolesListInspector.contains(role)){
			for (ChecklistOfflineInspectionTmp checklistOfflineInspection : checklistOfflineInspectionList) {
			//try{
				if(BankAuditUtil.isEmptyString(checklistOfflineInspection.getComplianceStatus()) ){
					checklistOfflineInspection.setUpdateStatus(BankAuditConstant.NOT_APPLICABLE);
					checklistOfflineInspection.setUpdateStatusMessage("Not attended"); 
				}
				else if( BankAuditConstant.COMPLIANCE_YES.equalsIgnoreCase(checklistOfflineInspection.getComplianceStatus())
								|| BankAuditConstant.COMPLIANCE_NO.equalsIgnoreCase(checklistOfflineInspection.getComplianceStatus())
								|| BankAuditConstant.COMPLIANCE_NA.equalsIgnoreCase(checklistOfflineInspection.getComplianceStatus()) ){
					
					if(BankAuditConstant.COMPLIANCE_NO.equalsIgnoreCase(checklistOfflineInspection.getComplianceStatus())
							&& ( BankAuditUtil.isEmptyString(checklistOfflineInspection.getIoComments()) 
									||	BankAuditUtil.isEmptyString(checklistOfflineInspection.getCriticality()))  ){
						
						checklistOfflineInspection.setUpdateStatus(BankAuditConstant.FAILED);
						if(BankAuditUtil.isEmptyString(checklistOfflineInspection.getIoComments()) 
								&&	BankAuditUtil.isEmptyString(checklistOfflineInspection.getCriticality())){
							checklistOfflineInspection.setUpdateStatusMessage("IO remarks and Criticality not given"); 
						}
						else if(BankAuditUtil.isEmptyString(checklistOfflineInspection.getIoComments()) ){
							checklistOfflineInspection.setUpdateStatusMessage("IO remarks not given"); 
						}else{ 
							checklistOfflineInspection.setUpdateStatusMessage("Criticality  not given"); 								 
						}
					} 
					else{ 							
						//AuditObservation auditObservation =  ifcEnsureBaasDataDao.getAuditObservation(checklistOfflineInspection);
						AuditObservation auditObservation =  auditObservationMap.get(checklistOfflineInspection.getAuditId()+checklistOfflineInspection.getObservationId()+checklistOfflineInspection.getMappingId());
						if(auditObservation!=null && !BankAuditUtil.isEmptyString(auditObservation.getObservationId())){
							 
							String checklistStatus = auditObservation.getSubmissionStatus();
							if(BankAuditUtil.isEmptyString(checklistStatus) || 
									!checklistStatus.equalsIgnoreCase(BankAuditConstant.STATUS_UNAUTH)){
								checklistOfflineInspection.setUpdateStatus(BankAuditConstant.NOT_APPLICABLE);
								checklistOfflineInspection.setUpdateStatusMessage("Already processed"); 
							}							
							else{ 							
								String standardStmt = ifcEnsureBaasDataDao.getStandardStatementForReportTable(checklistOfflineInspection, reportLanguage);	 								
								//standardStmt = BankAuditUtil.isEmptyString(standardStmt) || standardStmt.trim().equals("#::#")?" #::#  #::# ":standardStmt; 							
								if(!BankAuditConstant.FAILED.equals(standardStmt)){
									if(!BankAuditUtil.isEmptyString(standardStmt) && standardStmt.split("#::#").length==3){
										String[] standardStmtArr = standardStmt.split("#::#");
										standardStmt  = standardStmtArr[0].trim();
										String standardStmtHindi = "";
										if(BankAuditConstant.REPORT_LANGUAGE_HI.equalsIgnoreCase(reportLanguage)){
											standardStmtHindi = standardStmtArr[1].trim();
										} 
										checklistOfflineInspection.setStandardStmt(standardStmt);
										checklistOfflineInspection.setStandardStmtHindi(standardStmtHindi);
										checklistOfflineInspection.setStandardStmtPrintFlag(standardStmtArr[2].trim());
									}
									
									checklistOfflineInspection.setNonCompliancePersistence(BankAuditConstant.NOT_APPLICABLE);
									checklistOfflineInspection.setUpdateStatus(BankAuditConstant.SUCCESS);
									checklistOfflineInspection.setUpdateStatusMessage("Success");
									 
							    	if(BankAuditConstant.COMPLIANCE_NO.equalsIgnoreCase(checklistOfflineInspection.getComplianceStatus())){
							    		nonComplianceMappingIds.add(checklistOfflineInspection.getMappingId());
							    		checklistOfflineInspection.setNonCompliancePersistence(BankAuditConstant.NO);
							    	}
									 
								}else{
									checklistOfflineInspection.setUpdateStatus(BankAuditConstant.FAILED);
									checklistOfflineInspection.setUpdateStatusMessage("Failed to get standard statement"); 
								}
							}
						}else{
							checklistOfflineInspection.setUpdateStatus(BankAuditConstant.FAILED);
							checklistOfflineInspection.setUpdateStatusMessage("No observation(checklist) found"); 
						}							
					}			
					
				}else{
					checklistOfflineInspection.setUpdateStatus(BankAuditConstant.FAILED);
					checklistOfflineInspection.setUpdateStatusMessage("Compliance category not matched"); 
				} 			
				checklistOfflineInspection.setDocId(docId); 
				ifcEnsureBaasDataDao.save(checklistOfflineInspection);		 
			}
		}else if(rolesListCompliance.contains(role) || rolesListReviewer.contains(role)){
			for (ChecklistOfflineInspectionTmp checklistOfflineInspection : checklistOfflineInspectionList) {
				
				AuditObservation auditObservation =  auditObservationMap.get(checklistOfflineInspection.getAuditId()+checklistOfflineInspection.getObservationId()+checklistOfflineInspection.getMappingId());
				if(!BankAuditConstant.COMPLIANCE_NO.equalsIgnoreCase(checklistOfflineInspection.getComplianceStatus())){	
					checklistOfflineInspection.setUpdateStatus(BankAuditConstant.NOT_APPLICABLE);
					checklistOfflineInspection.setUpdateStatusMessage("Complied Checklist"); 
				}else if(!BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(auditObservation.getSubmissionStatus())){
					checklistOfflineInspection.setUpdateStatus(BankAuditConstant.NOT_APPLICABLE);
					checklistOfflineInspection.setUpdateStatusMessage("Observation not Submited"); 
				}else if(rolesListCompliance.contains(role) 
						&& ((!BankAuditUtil.isEmptyString(auditObservation.getResponseStatus())
									&& !checklistOfflineInspection.getResponseStatus().equalsIgnoreCase(auditObservation.getResponseStatus()))
								|| (!BankAuditUtil.isEmptyString(auditObservation.getReviewStatus()) 
										&& !checklistOfflineInspection.getReviewStatus().equalsIgnoreCase(auditObservation.getReviewStatus()))
								|| (!BankAuditUtil.isEmptyString(auditObservation.getReviewStatusAttribute()) && !BankAuditUtil.isEmptyString(checklistOfflineInspection.getReviewStatusAttribute()) 
										&& !checklistOfflineInspection.getReviewStatusAttribute().split("-")[0].equalsIgnoreCase(auditObservation.getReviewStatusAttribute())))){
					checklistOfflineInspection.setUpdateStatus(BankAuditConstant.NOT_APPLICABLE);
					checklistOfflineInspection.setUpdateStatusMessage("Please download latest file and try again."); 
				}else if(rolesListReviewer.contains(role) 
						&&(!checklistOfflineInspection.getResponseStatus().equalsIgnoreCase(auditObservation.getResponseStatus())
								|| !checklistOfflineInspection.getReviewStatus().equalsIgnoreCase(auditObservation.getReviewStatus()))){
					checklistOfflineInspection.setUpdateStatus(BankAuditConstant.NOT_APPLICABLE);
					checklistOfflineInspection.setUpdateStatusMessage("Please download latest file and try again."); 
				}else{					
					nonComplianceMappingIds.add(checklistOfflineInspection.getMappingId());					
					com.bankaudit.dto.ChecklistDto standardStmtDto = standardStmtDataMap.get(checklistOfflineInspection.getMappingId());
					if(standardStmtDto!=null){
						if(BankAuditConstant.COMPLIANCE_YES.equalsIgnoreCase(checklistOfflineInspection.getComplianceStatus()) 
								&& !BankAuditConstant.NO.equalsIgnoreCase(standardStmtDto.getStandardStmntCompliancePrint())){									 
							checklistOfflineInspection.setStandardStmt(standardStmtDto.getStandardStmntCompliance());
							checklistOfflineInspection.setStandardStmtHindi( standardStmtDto.getStandardStmntComplianceHindi()); 
						}else if(BankAuditConstant.COMPLIANCE_NO.equalsIgnoreCase(checklistOfflineInspection.getComplianceStatus())
								&& !BankAuditConstant.NO.equalsIgnoreCase(standardStmtDto.getStandardStmntNonCompliancePrint())){
							checklistOfflineInspection.setStandardStmt(standardStmtDto.getStandardStmntNonCompliance());
							checklistOfflineInspection.setStandardStmtHindi( standardStmtDto.getStandardStmntNonComplianceHindi());							    								 
						}							
					}		
					
					if(rolesListCompliance.contains(role)){  // Condition for Bank user
						if(BankAuditUtil.isEmptyString(checklistOfflineInspection.getResponseReviewRemarks())){
							checklistOfflineInspection.setUpdateStatus(BankAuditConstant.NOT_APPLICABLE);
							checklistOfflineInspection.setUpdateStatusMessage("Not attended"); 
						}else if(BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(auditObservation.getResponseStatus())){
							checklistOfflineInspection.setUpdateStatus(BankAuditConstant.NOT_APPLICABLE);
							checklistOfflineInspection.setUpdateStatusMessage("Remark already submitted");
						}else if(!BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(auditObservation.getResponseStatus())
								|| (BankAuditConstant.STATUS_REJ.equalsIgnoreCase(auditObservation.getResponseStatus()) &&
										BankAuditConstant.STATUS_FURTHER_COMPLIANCE.equalsIgnoreCase(auditObservation.getReviewStatusAttribute()))){ 
							checklistOfflineInspection.setUpdateStatus(BankAuditConstant.SUCCESS);
							checklistOfflineInspection.setUpdateStatusMessage("Success");								
						}else{
							checklistOfflineInspection.setUpdateStatus(BankAuditConstant.NOT_APPLICABLE);
							checklistOfflineInspection.setUpdateStatusMessage("Not Applicable");
						}						
					}else if(rolesListReviewer.contains(role)){ // Condition for Scrutiny/Reviewer user
						
						Boolean isRatingGiven = true;
						if(auditSchedule.getReviewStatus().equalsIgnoreCase(BankAuditConstant.STATUS_SCRUTINY_INITIATED)){ 
							if(BankAuditUtil.isEmptyString(checklistOfflineInspection.getDeskEffectiveScore())){
								checklistOfflineInspection.setUpdateStatus(BankAuditConstant.FAILED);
								checklistOfflineInspection.setUpdateStatusMessage("Rating not given"); 
								isRatingGiven = false;
							}else if(checklistOfflineInspection.getDeskEffectiveScore().equalsIgnoreCase(BankAuditConstant.NOT_APPLICABLE)
									&& BankAuditUtil.isEmptyString(checklistOfflineInspection.getDeskReviewerMakerRemarks())){
								checklistOfflineInspection.setUpdateStatus(BankAuditConstant.FAILED);
								checklistOfflineInspection.setUpdateStatusMessage("For NA Rating reamrks is mandatory"); 
								isRatingGiven = false;
							} 
						}
						
						if(isRatingGiven){
							if(BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(auditObservation.getResponseStatus())
									&& BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(auditObservation.getReviewStatus())
									&& !BankAuditConstant.STATUS_FURTHER_COMPLIANCE_WITH_DESCRIPTION.contains(checklistOfflineInspection.getReviewStatusAttribute())
									&& !BankAuditConstant.STATUS_FURTHER_COMPLIANCE.equalsIgnoreCase(auditObservation.getReviewStatusAttribute())){ 
								checklistOfflineInspection.setUpdateStatus(BankAuditConstant.NOT_APPLICABLE);
								checklistOfflineInspection.setUpdateStatusMessage("Not Applicable");								
							}else if(BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(auditObservation.getResponseStatus())
									&& BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(auditObservation.getReviewStatus()) 
									&& BankAuditConstant.STATUS_FURTHER_COMPLIANCE.equalsIgnoreCase(auditObservation.getReviewStatusAttribute())
									&& BankAuditUtil.isEmptyString(checklistOfflineInspection.getResponseReviewRemarks())){
								checklistOfflineInspection.setUpdateStatus(BankAuditConstant.SUCCESS);
								checklistOfflineInspection.setUpdateStatusMessage("Success");	
							}else if(BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(auditObservation.getResponseStatus())
									&& BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(auditObservation.getReviewStatus()) 
									&& BankAuditConstant.STATUS_FURTHER_COMPLIANCE.equalsIgnoreCase(auditObservation.getReviewStatusAttribute())
									&& !BankAuditUtil.isEmptyString(checklistOfflineInspection.getResponseReviewRemarks())
									&& BankAuditConstant.STATUS_FURTHER_COMPLIANCE_WITH_DESCRIPTION.contains(checklistOfflineInspection.getReviewStatusAttribute()) ){
								checklistOfflineInspection.setUpdateStatus(BankAuditConstant.SUCCESS);
								checklistOfflineInspection.setUpdateStatusMessage("Success");	
							}else if(!BankAuditUtil.isEmptyString(checklistOfflineInspection.getResponseReviewRemarks()) 
									&& (BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(auditObservation.getResponseStatus())
									|| !BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(auditObservation.getReviewStatus()))
									&& checklistOfflineInspection.getReviewStatusAttribute().contains(BankAuditConstant.STATUS_FURTHER_COMPLIANCE_WITH_DESCRIPTION)){ 
								checklistOfflineInspection.setUpdateStatus(BankAuditConstant.SUCCESS);
								checklistOfflineInspection.setUpdateStatusMessage("Success");								
							}else if(BankAuditUtil.isEmptyString(checklistOfflineInspection.getResponseReviewRemarks())
									&& !BankAuditUtil.isEmptyString(checklistOfflineInspection.getReviewStatusAttribute())
									&& checklistOfflineInspection.getReviewStatusAttribute().contains(BankAuditConstant.STATUS_FURTHER_COMPLIANCE_WITH_DESCRIPTION)){
								checklistOfflineInspection.setUpdateStatus(BankAuditConstant.NOT_APPLICABLE);
								checklistOfflineInspection.setUpdateStatusMessage("Further Compliance Remarks not given"); 
							}else if(BankAuditUtil.isEmptyString(checklistOfflineInspection.getResponseReviewRemarks()) 
									&& (BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(auditObservation.getResponseStatus())
									|| !BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(auditObservation.getReviewStatus()))
									&& !checklistOfflineInspection.getReviewStatusAttribute().contains(BankAuditConstant.STATUS_FURTHER_COMPLIANCE_WITH_DESCRIPTION)
									&& !BankAuditUtil.isEmptyString(checklistOfflineInspection.getReviewStatusAttribute())){
								checklistOfflineInspection.setUpdateStatus(BankAuditConstant.SUCCESS);
								checklistOfflineInspection.setUpdateStatusMessage("Success");	
							}else if(!BankAuditUtil.isEmptyString(checklistOfflineInspection.getResponseReviewRemarks())
									&& BankAuditUtil.isEmptyString(checklistOfflineInspection.getReviewStatusAttribute())){
								checklistOfflineInspection.setUpdateStatus(BankAuditConstant.NOT_APPLICABLE);
								checklistOfflineInspection.setUpdateStatusMessage("Compliance status not given"); 
							}else if(BankAuditUtil.isEmptyString(checklistOfflineInspection.getResponseReviewRemarks())){
								checklistOfflineInspection.setUpdateStatus(BankAuditConstant.NOT_APPLICABLE);
								checklistOfflineInspection.setUpdateStatusMessage("Not Applicable"); 
							}else{
								checklistOfflineInspection.setUpdateStatus(BankAuditConstant.NOT_APPLICABLE);
								checklistOfflineInspection.setUpdateStatusMessage("Not Applicable");
							}
						}
					}
					else{
						checklistOfflineInspection.setUpdateStatus(BankAuditConstant.FAILED);
						checklistOfflineInspection.setUpdateStatusMessage("Compliance category not matched"); 
					}					
				}
				checklistOfflineInspection.setDocId(docId); 
				ifcEnsureBaasDataDao.save(checklistOfflineInspection);	
			}
			
		}
		
		if(nonComplianceMappingIds!=null && nonComplianceMappingIds.size()>0){
			List<String> persistenceMappingIds = ifcEnsureBaasDataDao.getAllNonComplianceBehavior(auditLevelDocumentDetailsToSave.getLegalEntityCode(),
					auditLevelDocumentDetailsToSave.getAuditId(), auditLevelDocumentDetailsToSave.getAuditTypeCode(), auditLevelDocumentDetailsToSave.getUnitCode(),
					nonComplianceMappingIds);
			if(persistenceMappingIds!=null && persistenceMappingIds.size()>0){
				ifcEnsureBaasDataDao.updateAllNonComplianceBehaviorToTemp(auditLevelDocumentDetailsToSave.getLegalEntityCode(), auditLevelDocumentDetailsToSave.getAuditId(),
						auditLevelDocumentDetailsToSave.getAuditTypeCode(), persistenceMappingIds, docId);
			}
		} 
	}
	
	@Transactional("transactionManager")
	public void updateComplianceStatusRemarksAndStandardStmt(Integer legalEntityCode,  String auditId,  String auditTypeCode, String reportLanguage, Integer docId,
			String role, String maker) throws Exception{
	 
		List<String> rolesListInspector = Arrays.asList(("G2,G8").split(","));  //Inspection
		List<String> rolesListCompliance = Arrays.asList(("G3,G31").split(","));	//Compliance or Response
		List<String> rolesListReviewer = Arrays.asList(("G4,G7").split(","));       //review/Scrutiny	
		
		String complianceCategory= null;;
		if(rolesListInspector.contains(role)){
			complianceCategory = null;
		}else{
			complianceCategory =  BankAuditConstant.COMPLIANCE_NO;
		} 		 
			
		Map<String , Object> properties = new HashMap<String, Object>();
		properties.put("legalEntityCode", legalEntityCode);
		properties.put("auditTypeCode", auditTypeCode);
		properties.put("auditId", auditId);			
		AuditSchedule auditSchedule = (AuditSchedule) ifcEnsureBaasDataDao.getUniqueEntityByMatchingProperties(AuditSchedule.class, properties);
		if(rolesListInspector.contains(role)){
			List<Integer> idsStandardStmt = ifcEnsureBaasDataDao.getListofIdsFormReportTable(legalEntityCode, auditId, auditTypeCode,
					BankAuditConstant.FOR_CHECKLIST, BankAuditConstant.FOR_STANDARD_STMT, complianceCategory);
			List<Integer> idsRemarks = ifcEnsureBaasDataDao.getListofIdsFormReportTable(legalEntityCode, auditId, auditTypeCode,
					BankAuditConstant.FOR_CHECKLIST, BankAuditConstant.FOR_REMARKS, complianceCategory);
			/*List<Integer> idsChecklist = ifcEnsureBaasDataDao.getListofIdsFormReportTable(legalEntityCode, auditId, auditTypeCode,
					BankAuditConstant.FOR_CHECKLIST, BankAuditConstant.FOR_CHECKLIST, complianceCategory);*/	
			List<Integer> idsChecklist = null;
			ifcEnsureBaasDataDao.flushSession();
			
			ifcEnsureBaasDataDao.updateReportStandardStmtFormOfflineUpload(legalEntityCode, auditId, auditTypeCode, reportLanguage, docId, idsStandardStmt);
			ifcEnsureBaasDataDao.updateReportRemarkFormOfflineUpload(legalEntityCode, auditId, auditTypeCode, reportLanguage, docId, idsRemarks, 
					BankAuditConstant.FOR_REMARKS, role);
			ifcEnsureBaasDataDao.updateReportChecklistFormOfflineUpload(legalEntityCode, auditId, auditTypeCode, reportLanguage, docId, idsChecklist);
		
		}else{	
						 
			List<String> idsResponseReviewRemarks = ifcEnsureBaasDataDao.getAllResponseReviewRemarks(legalEntityCode, auditTypeCode, auditId, role);					
			if(idsResponseReviewRemarks!=null && idsResponseReviewRemarks.size()>0){
				ifcEnsureBaasDataDao.updateResponseReviewRemarksFormOfflineUpload(legalEntityCode, auditId, auditTypeCode, docId, role, maker);
			}
			ifcEnsureBaasDataDao.insertResponseReviewRemarksFormOfflineUpload(legalEntityCode, auditTypeCode, auditId, auditSchedule.getPlanId(), 
					docId, role, maker, idsResponseReviewRemarks);
			 			
			if(rolesListCompliance.contains(role)){
				List<Integer> idsRemarksBank = ifcEnsureBaasDataDao.getListofIdsFormReportTable(legalEntityCode, auditId, auditTypeCode,
						BankAuditConstant.FOR_CHECKLIST, BankAuditConstant.FOR_REMARKS_BANK, complianceCategory);				
				ifcEnsureBaasDataDao.updateReportRemarkFormOfflineUpload(legalEntityCode, auditId, auditTypeCode, reportLanguage, docId, idsRemarksBank, 
						BankAuditConstant.FOR_REMARKS_BANK, role);
				
			}else if(rolesListReviewer.contains(role)){
				List<Integer> idsRemarksScrutiny = ifcEnsureBaasDataDao.getListofIdsFormReportTable(legalEntityCode, auditId, auditTypeCode,
						BankAuditConstant.FOR_CHECKLIST, BankAuditConstant.FOR_REMARKS_SCRUTINY, complianceCategory);
				ifcEnsureBaasDataDao.updateReportRemarkFormOfflineUpload(legalEntityCode, auditId, auditTypeCode, reportLanguage, docId, idsRemarksScrutiny, 
						BankAuditConstant.FOR_REMARKS_SCRUTINY, role); 
				
				//update Desk compliance rating
				if(auditSchedule.getReviewStatus().equalsIgnoreCase(BankAuditConstant.STATUS_SCRUTINY_INITIATED)){ 
					ifcEnsureBaasDataDao.updateDeskComplinaceRatingFromOfflineUpload(legalEntityCode, auditId, auditTypeCode, reportLanguage, docId, role, maker);
				}
			}			
		}
		
		ifcEnsureBaasDataDao.updateAuditObservationFormOfflineUpload(legalEntityCode, auditId, auditTypeCode, reportLanguage, docId, role, maker, auditSchedule.getReviewStatus());
		ifcEnsureBaasDataDao.insertIntoChecklistOfflineInspectionFromTempToMaster(legalEntityCode, auditId, auditTypeCode, reportLanguage, docId); 
		ifcEnsureBaasDataDao.updateUploadStatus(legalEntityCode, auditId, auditTypeCode, reportLanguage, docId);
		ifcEnsureBaasDataDao.deleteChecklistOfflineInspectionTmp(legalEntityCode, auditId, auditTypeCode, reportLanguage, docId);	
	}
	
	@Transactional("transactionManager")
	public void saveRatingParameterScoresToTemp(List<AuditRatingParameterScoreTmp> auditRatingParameterScoreTmpList){
		for (AuditRatingParameterScoreTmp auditRatingParameterScoreTmp : auditRatingParameterScoreTmpList) {
			ifcEnsureBaasDataDao.save(auditRatingParameterScoreTmp);
		}
	}
	
	@Transactional("transactionManager")
	public void updateRatingParameterScoresToMaster(Integer legalEntityCode,  String auditTypeCode, String auditId, Integer docId, 
			String rmCode,  String role, String userType){
		ifcEnsureBaasDataDao.updateRatingParameterScoresToMaster(legalEntityCode, auditTypeCode, auditId, docId, 
				rmCode, role, userType);
		ifcEnsureBaasDataDao.updateUploadStatus(legalEntityCode, auditId, auditTypeCode, "", docId);
		ifcEnsureBaasDataDao.deleteAuditRatingParameterScoreTmp(legalEntityCode, auditTypeCode, auditId, docId);
	}
	
	@Transactional("transactionManager")
	public Map<String, AuditObservation> getAllAuditObservation(Integer legalEntityCode, String auditTypeCode, String auditId){
		return ifcEnsureBaasDataDao.getAllAuditObservation(legalEntityCode, auditTypeCode, auditId);
	}
		 
	@Transactional("transactionManager")
	public void saveAuditRatingComplianceObsScoreTmp(List<AuditRatingComplianceObsScoreTmp> auditRatingComplianceObsScoreList){
		for (AuditRatingComplianceObsScoreTmp auditRatingComplianceObsScoreTmp : auditRatingComplianceObsScoreList) {
			ifcEnsureBaasDataDao.save(auditRatingComplianceObsScoreTmp);
		}
	}
	
	@Transactional("transactionManager")
	public void updateAuditRatingComplianceObsScoreToMaster(Integer legalEntityCode,  String auditTypeCode, String auditId, Integer docId, 
			String role, String ratingComplianceStatus){
		ifcEnsureBaasDataDao.updateAuditRatingComplianceObsScoreToMaster(legalEntityCode, auditTypeCode, auditId, docId, role, ratingComplianceStatus);
		ifcEnsureBaasDataDao.updateUploadStatus(legalEntityCode, auditId, auditTypeCode, "", docId);
		ifcEnsureBaasDataDao.deleteAuditRatingComplianceObsScoreTmp(legalEntityCode, auditTypeCode, auditId, docId);
	}
	
	@Transactional("transactionManager")
	public void deleteAuditRatingComplianceObsScoreTmp(Integer legalEntityCode,  String auditTypeCode, String auditId, Integer docId){
		ifcEnsureBaasDataDao.deleteAuditRatingComplianceObsScoreTmp(legalEntityCode, auditTypeCode, auditId, docId);
	}
}
