/*
 * 
 */
package com.bankaudit.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.query.Query;
import org.hibernate.transform.ResultTransformer;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.bankaudit.dto.MaintZoneMappingDto;
import com.bankaudit.service.MaintEntityService; 

/**
 * The Interface {@link MaintZoneMappingDaoImpl} define the methods
 * that can be exposed to controller layer in order to implements rest
 * end-points and define the business logic.
 *
 * @author amit.patel
 * @version 1.0
 */

@Repository("maintZoneMappingDao")
public class MaintZoneMappingDaoImpl extends AbstractDao implements MaintZoneMappingDao {

	static final Logger logger = Logger.getLogger(MaintZoneMappingDaoImpl.class);
	 
	@Autowired
	MaintEntityService maintEntityService;
	
	public List<MaintZoneMappingDto> getZoneByLegalEntityCode(Integer legalEntityCode, String userId){
		
		Session session=getSession();		
		session.setDefaultReadOnly(true);
		try{
			List<String> unitIdlist = maintEntityService.getSubBranchesGyUserIdOrUnitId(legalEntityCode, "user", userId);
			StringBuilder strQuery = new StringBuilder(" Select DISTINCT zone.zoneCode, zone.zoneName  From MaintZoneMapping zone   "   		  
					+" where  zone.legalEntityCode = :legalEntityCodeP "
					+ " and zone.unitCode in (:unitIdlist) " );
					 					 
			Query qry =   session.createQuery(strQuery.toString()).setResultTransformer(new ResultTransformer() {
						@Override
						public Object transformTuple(Object[] tuple, String[] aliases) {
							
							MaintZoneMappingDto maintZoneMapping =new MaintZoneMappingDto();
							
							maintZoneMapping.setZoneCode(tuple[0].toString());
							maintZoneMapping.setZoneName(tuple[1].toString());
							
							return maintZoneMapping;
						}
						@Override
						public List transformList(List collection) {
							return collection;
						}
					})
					.setParameter("legalEntityCodeP", legalEntityCode)
					.setParameterList("unitIdlist", unitIdlist);
			
			return qry.list();
			
		}catch (Exception exception) {
			logger.error("Inside method getZoneByLegalEntityCode", exception);
		}
		
		return null;		
	}
	
	public List<MaintZoneMappingDto> getRegionalOfficeOfAllZones(Integer legalEntityCode, String userId){
		 
		Session session=getSession();		
		session.setDefaultReadOnly(true);
		try{
			List<String> unitIdlist = maintEntityService.getSubBranchesGyUserIdOrUnitId(legalEntityCode, "user", userId);
			StringBuilder strQuery = new StringBuilder(" Select zone.zone_code, zone.zone_name, "
					//+ " GROUP_CONCAT( DISTINCT me.unit_code, '-', me.unit_name  SEPARATOR '#@::@#') "
					+ " CONCAT(me.unit_code, '-', me.unit_name)    "
					+ " From maint_zone_mapping zone  Inner JOIN maint_entity me ON me.unit_code = zone.unit_code "
					+ " AND me.legal_entity_code = zone.legal_entity_code "
					+ " and me.level_code = zone.level  "
					+ " where zone.legal_entity_code = :legalEntityCodeP "
					+ " and zone.unit_code in (:unitIdlist) "
					//+ " GROUP BY zone.zone_code, zone.zone_name "
					+ " ");
					 					 
			Query qry =   session.createNativeQuery(strQuery.toString()).setResultTransformer(new ResultTransformer() {
						@Override
						public Object transformTuple(Object[] tuple, String[] aliases) {
							
							MaintZoneMappingDto maintZoneMapping =new MaintZoneMappingDto();
							
							maintZoneMapping.setZoneCode(tuple[0].toString());
							maintZoneMapping.setZoneName(tuple[1].toString());
							maintZoneMapping.setUnitName(tuple[2].toString());
							 
							return maintZoneMapping;
						}
						@Override
						public List transformList(List collection) {
							return collection;
						}
					})
					.setParameter("legalEntityCodeP", legalEntityCode)
					.setParameterList("unitIdlist", unitIdlist);
			
			return qry.list(); 
			
		}catch (Exception exception) {
			logger.error("Inside method getRegionalOfficeOfAllZones", exception);
		}
		
		return null;		
	}
	
}
