package com.eactive.eai.rms.onl.manage.rule.transform;

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

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

import com.eactive.eai.agent.command.Command;
import com.eactive.eai.agent.eaimessage.ReloadEAIMessageCommand;
import com.eactive.eai.rms.common.base.OnlBaseService;

@Service("transformMappingManService")
public class TransformMappingManService extends OnlBaseService {

    @Autowired
    @Qualifier("transformMappingDao")
    private TransformMappingDao dao;

    
    
	public int insertRefMsgID(HashMap<String,Object> vo) throws Exception {
		int ret = 0;

		// 참조정보 삭제
		HashMap<String,Object> delVo = new HashMap<String,Object>();
		delVo.put("eaiSvcName",vo.get("eaiSvcName"));
		delVo.put("svcPrcssNo","0");
		delVo.put("refMsgIdNo","0");
		dao.deleteRefMsgID(delVo);
		
		// 순차등록
		String[] refMsgIdNames = ((String)vo.get("refMsgIdName")).split(";");
		
		//validation
		if(refMsgIdNames.length == 0){
			throw new Exception("Layout 세팅이 잘못되었습니다.");
		}
		if(refMsgIdNames.length == 1 && (refMsgIdNames[0] == null || "".equals(refMsgIdNames[0].trim()))){	
			return 0;
		}
	
		for( int i =0 ; i < refMsgIdNames.length-1; i++){
			if(refMsgIdNames[i] == null || "".equals(refMsgIdNames[i].trim())){		
				throw new Exception("Layout 세팅이 잘못되었습니다.");		
			}
		}

		for ( int i=0; i< refMsgIdNames.length; i++ ) {

			HashMap<String,Object> transform = new HashMap<String,Object>();
			transform.put("eaiSvcName", vo.get("eaiSvcName") );
			transform.put("refMsgIdName", refMsgIdNames[i] );
			transform.put("refMsgIdNo", i+1 );
			transform.put("svcPrcssNo",1);
	        ret += dao.insertRefMsgID(transform);
		}
		
		return ret;
	}    
    
    
    
    /**
     * 전문 레이아웃 매 핑 리스트 조회
     * 
     */
    public List<TransformVo> listTransformPage(TransformVo vo) throws Exception {        
        int nCnt = 0 ; 
        List<TransformVo> list = null;
        
        try {
        	// 전체 목록수 얻기
            nCnt = dao.findTotalCount(vo) ;            
            vo.setTotalCount(nCnt);
            if( nCnt > 0 ) {
            	list = dao.listTransformPage(vo);
            } else {
            	list = null;
            }
        } catch ( Exception e ) {
            //e.printStackTrace();
            logger.error(e.getMessage());
        }
        
        if( list == null ) {
            logger.error("returned list is null !" );
        } else {
            if( logger.isDebugEnabled() ) {
                logger.debug("returned list count=" + list.size() );
            }
        }

        return list;
    }

	/**
	 * 전문 레이아웃 목록 가져오기
	 * @param vo
	 * @param request
	 * @return
	 */
	public List<TransformVo> getRefMsgIDList(TransformVo vo) {
        return dao.getRefMsgIDList(vo);
	}
	

	
	public int deleteRefMsgID(HashMap<String,Object> vo)  throws Exception {
        return dao.deleteRefMsgID(vo);
	}

	public List<TransformVo> getRefMsgInfo(TransformVo vo) {
		return dao.getRefMsgInfo(vo);
	}

	public String getSvcMotivUseDstcd(TransformVo vo) {
		return dao.getSvcMotivUseDstcd(vo);
	}
	
    /**
     * 초기화  
     * 
     */
	public HashMap<String, Object> initializeTramsforms() throws Exception {
        //메모리 정보 Reload
        Command command = new ReloadEAIMessageCommand();
        command.setArgs("ALL");
        return agentUtilService.broadcast(command);
    }
}
