package com.eactive.eai.rms.common.acl.user;

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 com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.common.datasource.DataSourceType;

@Service("bizService")
public class BizService extends BaseService {

	@Autowired
	private BizDao dao;
	
	public HashMap<String, Object> selectList(int startNum,int endNum, String searchBzwkDstcd) throws Exception {
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		//검색 조건 

		paramMap.put("searchBzwkDstcd", searchBzwkDstcd.toUpperCase());
 
		// 전체 목록수 얻기
		int totalCount = dao.selectListCount(paramMap);

		paramMap.put("startNum" , startNum);// 시작 record
		paramMap.put("endNum"   , endNum);// 끝 record

		List<Map<String, Object>> list = dao.selectList(paramMap);

		HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("totalCount", totalCount);

		map.put("list", list);

		return map;
	}	

	public HashMap<String, Object> selectDetail(String bzwkDstcd) throws Exception {

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("bzwkDstcd", bzwkDstcd);

		HashMap<String, Object>   detail = dao.selectDetail(paramMap);

		HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("detail", detail);

		return map;

	}	

	public void insert(HashMap<String, Object> paramMap, HashMap<String, Object>[] list) throws Exception {
		dao.insert(paramMap);
	}

	public void update(HashMap<String, Object> paramMap, HashMap<String, Object>[] list) throws Exception {
		dao.update(paramMap);
	}	

	public void delete(HashMap<String, Object> paramMap) throws Exception {
		dao.delete(paramMap);
	}	

	public List<HashMap<String, Object>> selectUserBizList(HashMap<String, Object> paramMap ) throws Exception {
		List searchList = new ArrayList();
		
		String searchBzwkName = (String)paramMap.get("searchBzwkName");
		String targetGridYn = (String)paramMap.get("targetGridYn");
		
		searchList = Arrays.asList(searchBzwkName.split(","));
		
		paramMap.put("searchBzwkName", "Y".equals(targetGridYn) ? "" : searchList);
		
		List<HashMap<String, Object>> list = dao.selectUserBizList(paramMap);
		List<HashMap<String, Object>> resultList = new ArrayList<HashMap<String, Object>>(); 

		// 사용자에게 등록된 업무권한 목록
		if("Y".equals(targetGridYn))
		{
			for(HashMap<String, Object> m : list)
			{
				if("true".equals(m.get("CHK")))
				{
					resultList.add(m);
				}
			}
		}
		else
		{	// 사용자에게 등록되지 않은 업무권한 목록
			for(HashMap<String, Object> m : list)
			{
				if("false".equals(m.get("CHK")))
				{
					resultList.add(m);
				}
			}
		}
		
		
		return resultList;
	}

	public void transactionUserBiz(String userId, HashMap<String, Object>[] list) throws Exception {
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("userId", userId);
		dao.deleteUserBiz(paramMap);

		for (HashMap<String, Object> map : list) {
			map.put("BIZCODE", ((String)map.get("BIZCODE")).trim());
			map.put("userId", userId);
			if("".equals(map.get("SAVETYPE")) || map.get("SAVETYPE") == null){
				map.put("SAVETYPE","R");
			}else{
				map.put("SAVETYPE",map.get("SAVETYPE"));
			}
			String check = (String) map.get("CHK");

			if ("true".equals(check)) {
				try{
					dao.insertUserBiz(map);
					map.put("EAIBZWKDSTCD", map.get("BIZCODE"));
					//선택한 업무코드 + 그 업무코드로 조회되는 EAI업무구분코드  에 대해서 권한 부여  추가 2020.02.05
					dao.insertMergeRM06(map);
				}catch(Exception e){
					logger.warn("업무코드["+ map.get("BIZCODE")+"]"+" 중복입니다");
				}
				
				
			}
		}
	}

	/*
	 * 사용자 의 계정 및 서비스에 해당하는 업무코드 리스트를 리턴한다.
	 */
	public List<HashMap<String, Object>> selectUserBizCodes(String userId) {

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("userId", userId);
		return dao.selectUserBizCodes(map);
	}

	public String selectBizCodesString(DataSourceType serviceType, String userId) {
		// 사용자 권한별 업무코드 리스트를 code1,code2,code3 ... 형태로 구한다.
		List<HashMap<String, Object>> codeList = selectUserBizCodes(userId);
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < codeList.size(); i++) {
			HashMap<String, Object> map = codeList.get(i);
			buf.append(map.get("BIZCODE"));
			buf.append(",");
		}

		return buf.toString();
	}

	public List<HashMap<String, Object>> selectBizList() throws Exception {
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		return dao.selectBizList(paramMap);
	}
	
	public void deleteRmsUserBiz(HashMap<String, Object> paramMap) throws Exception {
		dao.deleteUserBiz(paramMap);
	}
	
	public void mergeRM06(HashMap<String,Object> paramMap) throws Exception{
		 dao.mergeRM06(paramMap);		
	}
	
	public void insertMergeRM06(HashMap<String,Object> paramMap) throws Exception{
		 dao.insertMergeRM06(paramMap);		
	}
	
	public void deleteUserBizCodeRole(HashMap<String, Object> paramMap) throws Exception {
		dao.deleteUserBizCodeRole(paramMap);
	}
	
	public void insertUserBiz(HashMap<String,Object> paramMap) throws Exception{
		 dao.insertUserBiz(paramMap);		
	}

	public String selectGroupCoCdByEAIBzwkDstCd(String eaiBzwkDstCd) {
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("eaiBzwkDstCd", eaiBzwkDstCd);
		return dao.selectGroupCoCdByEAIBzwkDstCd(paramMap);
	}
	
	public void deleteUserBizCode(HashMap<String, Object> paramMap) throws Exception {
		dao.deleteUserBizCode(paramMap);
	}
}
