package com.bankaudit.rest.process.dao;

import java.util.Map;

import org.hibernate.Session;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.bankaudit.dao.AbstractDao;
import com.bankaudit.process.model.AuditScheduleStagesStatusHst; 

@Repository("auditScheduleStagesStatusHstDao")
@Transactional("transactionManager")
public class AuditScheduleStagesStatusHstDaoImpl extends AbstractDao implements AuditScheduleStagesStatusHstDao {

	@Override
	public AuditScheduleStagesStatusHst getLatestRecordByMatchingProperties(Map<String, Object> auditSchStagesStatusHstProperties ){
		
		StringBuilder qryStr = new StringBuilder("SELECT hst as schStatusHst FROM AuditScheduleStagesStatusHst hst "
				+ " Where hst.id = (SELECT MAX(hstInner.id) AS maxId FROM AuditScheduleStagesStatusHst hstInner "
					+ "	Where hstInner.legalEntityCode = hst.legalEntityCode "
					+ " AND hstInner.auditTypeCode = hst.auditTypeCode "
					+ " AND hstInner.auditId = hst.auditId "
					+ " AND hstInner.actionType = hst.actionType "
					+ " AND hstInner.code = hst.code  "
					+ " ) "
				+ " AND hst.legalEntityCode = :legalEntityCode "
				+ " AND hst.auditTypeCode = :auditTypeCode "
				+ " AND hst.auditId = :auditId "
				+ " AND hst.actionType = :actionType "
				+ " AND hst.code = :code  "
				+ " AND hst.makerId = :makerId"
				+ " AND hst.assigneeId = :assigneeId"
				+ " AND hst.stagesStatus = :stagesStatus ");
		
		Session session = getSession();
		session.setDefaultReadOnly(true);
		
		return (AuditScheduleStagesStatusHst) session.createQuery(qryStr.toString())
			.setParameter("legalEntityCode", auditSchStagesStatusHstProperties.get("legalEntityCode"))
			.setParameter("auditTypeCode", auditSchStagesStatusHstProperties.get("auditTypeCode"))
			.setParameter("auditId", auditSchStagesStatusHstProperties.get("auditId"))
			.setParameter("actionType", auditSchStagesStatusHstProperties.get("actionType"))
			.setParameter("code", auditSchStagesStatusHstProperties.get("code"))
			.setParameter("makerId", auditSchStagesStatusHstProperties.get("makerId"))
			.setParameter("assigneeId", auditSchStagesStatusHstProperties.get("assigneeId"))
			.setParameter("stagesStatus", auditSchStagesStatusHstProperties.get("stagesStatus"))
			.uniqueResult();
		
	}
}
