package com.bankaudit.service;

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

import javax.persistence.Column;

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.dao.MaintAuditTypeDescDao;
import com.bankaudit.dto.DataTableResponse;
import com.bankaudit.model.MaintAuditTypeDesc;
import com.bankaudit.rest.process.dao.ReportDao;

@Service("maintAuditTypeDescService")
@Transactional("transactionManager")
public class MaintAuditTypeDescServiceImpl implements MaintAuditTypeDescService {

	
	@Autowired
	MaintAuditTypeDescDao maintAuditTypeDescDao;
	
	@Autowired
	ReportDao reportDao;
	
	@Override
	public void createMaintAuditTypeDesc(MaintAuditTypeDesc maintAuditTypeDesc) {

		maintAuditTypeDescDao.save(maintAuditTypeDesc);
	}

	@Override
	public void updateMaintAuditTypeDesc(MaintAuditTypeDesc maintAuditTypeDesc) {

		maintAuditTypeDescDao.update(maintAuditTypeDesc);
	}

	@Override
	public MaintAuditTypeDesc getMaintAuditTypeDesc(Integer legalEntityCode, String auditTypeCode) {
		
		Map<String , Object> properties=new HashMap<String, Object>();
		properties.put("legalEntityCode",legalEntityCode );
		properties.put("auditTypeCode", auditTypeCode);
		return (MaintAuditTypeDesc)maintAuditTypeDescDao.getUniqueEntityByMatchingProperties(MaintAuditTypeDesc.class, properties);
	}



	@Override
	public DataTableResponse getMaintAuditTypeDesc(Integer legalEntityCode, String search, Integer orderColumn,
			String orderDirection, Integer page, Integer size) {
		return maintAuditTypeDescDao.getMaintAuditTypeDesc( legalEntityCode,  search,  orderColumn,
				 orderDirection,  page,  size);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MaintAuditTypeDesc> getMaintAuditTypeDescByLegalEntityCode(Integer legalEntityCode) {
		Map<String , Object> properties=new HashMap<String, Object>();
		properties.put("legalEntityCode",legalEntityCode );
		properties.put("entityStatus",BankAuditConstant.STATUS_ACTIVE );
	
		return maintAuditTypeDescDao.getEntitiesByMatchingProperties(MaintAuditTypeDesc.class, properties);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MaintAuditTypeDesc> getAuditTypeByLEAndUser(Integer legalEntityCode,String userId,String role) {
		
		List<String> deptList= null;
		boolean isDeptAsRMD =false;
		isDeptAsRMD = reportDao.isDeptAsRMD(legalEntityCode, role, userId);
		if(!isDeptAsRMD) 
			deptList =reportDao.getDeptList(legalEntityCode, role, userId);
	
		return maintAuditTypeDescDao.getAuditTypeByLEAndUser(legalEntityCode, role, userId,  deptList, isDeptAsRMD);
	}

	
}
