package com.eactive.eai.rms.onl.common.service;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.eactive.eai.data.entity.onl.message.EAIMessageEntity;
import com.eactive.eai.rms.common.datasource.DataSourceContextHolder;
import com.eactive.eai.rms.data.entity.onl.eaimsg.EAIMessageService;

@Service
@Qualifier("eaiSvcCodeMapService")
public class EAISvcCodeMapService {

    @Autowired
    private EAIMessageService eaiMessageQueryService;
    

    private Map<String, HashMap<String,String>> eaiSvcCodeMap = new ConcurrentHashMap<>();

    /*
     * EAI 서비스코드와 Description 을 TSEAIHE01 테이블에서 읽어와서
     * Map 에 등록한다.
     */
    public void init(String serviceKey) {        
        HashMap<String,String> map = eaiSvcCodeMap.get(serviceKey);
        if( map == null ) {
            map = new HashMap<>();
        } else {
            map.clear();
        }
        
		List<EAIMessageEntity> list = eaiMessageQueryService.findAll();
		if (list != null) {
			for (EAIMessageEntity tmp : list) {
				map.put(tmp.getEaisvcname(), tmp.getEaisvcdesc());
			}
			eaiSvcCodeMap.put(serviceKey, map);
		}
    }
    
    /*
     * EAI 서비스코드 와  Desction 에 대한 전체 Map 을 리턴한다.
     */
    public HashMap<String,String> selectAllMap() throws Exception {
        String serviceKey = DataSourceContextHolder.getDataSourceType().getName();
        
        if( eaiSvcCodeMap == null ) {
            init(serviceKey);
        }
        
        HashMap<String,String> map = eaiSvcCodeMap.get(serviceKey);
        if( map == null ) {
            init(serviceKey);
            map = eaiSvcCodeMap.get(serviceKey);
        }
        
        return map ;       
    }
}
