package com.bankaudit.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.bankaudit.constants.BankAuditConstant;
import com.bankaudit.dto.TreeviewItem;
import com.bankaudit.model.GeneralParameter;

@Repository("maintRoleEntitlementDao")
public class MaintRoleEntitlementDaoImpl extends  AbstractDao implements MaintRoleEntitlementDao{

	@Override
	public void deleteRoleEntitlements(Integer legalEntityCode, String ugRoleCode) {
		
		Session session=getSession();
		
		session.createQuery(" delete from MaintRoleEntitlement where "
				+ " legalEntityCode =:legalEntityCode and "
				+ " ugRoleCode =:ugRoleCode "
				+ " and entityStatus ='A'"
				+ " ")
		.setParameter("legalEntityCode", legalEntityCode)
		.setParameter("ugRoleCode", ugRoleCode).executeUpdate();
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TreeviewItem> getMenusByLegalEntityCodeAndKey1(Integer legalEntityCode, String key1) {
		
		Session session=getSession();
		
		return session.createSQLQuery("SELECT key_2 as text , "
				+ " value as value , "
				+ " description as description , "
				+ " 0 as checked "
				+ " FROM general_parameter WHERE "
				+ " legal_entity_code =:legalEntityCode "
				+ " and  key_1 =:key1").setParameter("legalEntityCode", legalEntityCode)
		.setParameter("key1", key1)
		.setResultTransformer(Transformers.aliasToBean(TreeviewItem.class)).list();

	}

	
	@SuppressWarnings("unchecked")
	@Override
	public List<TreeviewItem> getMenusByLegalEntityCodeAndKey1Distinct(Integer legalEntityCode, String key1) {
		
		Session session=getSession();
		
		return session.createSQLQuery("SELECT distinct key_2 as text , "
				+ " key_2 as description , " // Added to show the description 
				+ " 0 as checked "
				+ " FROM general_parameter WHERE "
				+ " legal_entity_code =:legalEntityCode "
				+ " and  key_1 =:key1").setParameter("legalEntityCode", legalEntityCode)
		.setParameter("key1", key1)
		.setResultTransformer(Transformers.aliasToBean(TreeviewItem.class)).list();

	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<TreeviewItem> getMappedEntitlement(Integer legalEntityCode, String ugRoleCode, String key2) {
		
		
		Session session=getSession();
		
		
		
		
		return session.createSQLQuery("SELECT key_2 as text , "
				+ " value as value , "
				+ " description as description , "
				+ " IF(IFNULL(h.function_id,'0')='0',FALSE,TRUE) as checked "
				+ " FROM general_parameter g LEFT JOIN  "
				+ " maint_role_entitlement h  ON g.value = h.function_id AND h.ug_role_code =:ugRoleCode AND h.legal_entity_code =:legalEntityCode "
				+ " WHERE g.key_2 =:key2 AND g.legal_entity_code =:legalEntityCode "
				).setParameter("legalEntityCode", legalEntityCode)		
				.setParameter("ugRoleCode", ugRoleCode)
		.setParameter("key2", key2)
		.setResultTransformer(Transformers.aliasToBean(TreeviewItem.class)).list();
		
	}
	
	
	
	
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<TreeviewItem> getEntitlement(Integer legalEntityCode, String key2) {
		
		
		Session session=getSession();
		
		return session.createSQLQuery("SELECT key_2 as text , "
				+ " value as value , "
				+ " description as description,  "
				+ " 0 as checked "
				+ " FROM general_parameter g "
				+ " WHERE g.key_2 =:key2 AND g.legal_entity_code =:legalEntityCode "
				).setParameter("legalEntityCode", legalEntityCode)		
				
		.setParameter("key2", key2)
		.setResultTransformer(Transformers.aliasToBean(TreeviewItem.class)).list();
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> getFunctionIds(Integer legalEntityCode, String ugRoleCode, String status) {
		
		
		Session session=getSession();
		
		if(!status.equalsIgnoreCase(BankAuditConstant.STATUS_AUTH)){
			
			return session.createQuery("select functionId from MaintRoleEntitlementWrk where "
					+ " legalEntityCode =:legalEntityCode "
					+ " and ugRoleCode =:ugRoleCode "
					+ " and entityStatus ='A' ")
			.setParameter("ugRoleCode", ugRoleCode)
			.setParameter("legalEntityCode",legalEntityCode).list();
			
		}else {
			return session.createQuery("select functionId from MaintRoleEntitlement where "
					+ " legalEntityCode =:legalEntityCode "
					+ " and ugRoleCode =:ugRoleCode "
					+ " and entityStatus ='A' ")
			.setParameter("ugRoleCode", ugRoleCode)
			.setParameter("legalEntityCode",legalEntityCode).list();
		}

	}

	@Override
	public void deleteMaintRoleEntitlementWrk(Integer legalEntityCode, String ugRoleCode) {
		
		Session session=getSession();
		
		session.createQuery(" delete from MaintRoleEntitlementWrk where "
				+ " legalEntityCode =:legalEntityCode and "
				+ " ugRoleCode =:ugRoleCode "
				+ " ")
		.setParameter("legalEntityCode", legalEntityCode)
		.setParameter("ugRoleCode", ugRoleCode).executeUpdate();
		
	}

}




















