package com.eactive.eai.rms.bat.monitoring.inter;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.eactive.eai.agent.command.CommonCommand;
import com.eactive.eai.rms.common.acl.user.BizDao;
import com.eactive.eai.rms.common.base.BatBaseAnnotationController;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.combo.IbatisComboService;
import com.eactive.eai.rms.common.login.SessionManager;
import com.eactive.eai.rms.common.util.CamelCaseUtil;
import com.eactive.eai.rms.common.vo.JsonPageVo;

@Controller
public class BatInterfaceMonitorController extends BatBaseAnnotationController  {

	@Autowired
	@Qualifier("batInterfaceMonitorService")
	private BatInterfaceMonitorService service;
	
	@Autowired
	private ComboService comboService;
	
    @Autowired
    private IbatisComboService ibatisComboService;
	
	@Autowired
	@Qualifier("bizDao")
	private BizDao bizDao;

	@RequestMapping(value= "/bat/transaction/interface/interfaceMonitorMan.view")
	public String viewList(HttpServletRequest request, @RequestParam(value = "cmd", required = false ) String cmd ) throws Exception {
		if (cmd == null || "LIST".equals(cmd.toUpperCase())){
			cmd = "";
		}else {
			cmd = CamelCaseUtil.converCamelCase(cmd);
		}
		String path = request.getServletPath();
		return path.split("[.]")[0]+cmd;
	}
	
	/**
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value= "/bat/transaction/interface/interfaceMonitorMan.json", params = "cmd=LIST")
	public ModelAndView selectList(HttpServletRequest request,
			JsonPageVo pageVo ,@RequestParam HashMap<String,Object> paramap) throws Exception {
		
		String userid = SessionManager.getUserId(request);
		paramap.put("loginEmpno", userid);
		
		HashMap<String, Object> map = service.selectList( pageVo.getStartNum(), pageVo.getEndNum(), paramap);
		
		pageVo.setTotalCount((Integer)map.get("totalCount"));
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("total"  , pageVo.getTotal());  //전체 페이지수
		resultMap.put("page"   , pageVo.getPage());   //현재 페이지수
		resultMap.put("records", pageVo.getTotalCount());//전체 레코드건수	
		resultMap.put("rows"   , map.get("list"));
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;
		
	}

    @RequestMapping(value= "/bat/transaction/interface/interfaceMonitorMan.json",params = "cmd=LIST_INIT_COMBO")
    public ModelAndView initCombo( HttpServletRequest request,
            HttpServletResponse response) throws Exception {
    	
		String userId = SessionManager.getUserId(request);
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("userId", userId);
 
		//업무구분
		List<HashMap<String, Object>> listBiz = bizDao.selectUserBizCodes(paramMap);
		
		//서버구분
		List<Map<String, Object>> listServer = ibatisComboService.selectListComboForTable("TSEAIEP03","EAISEVRINSTNCNAME","EAISEVRINSTNCNAME");
        
        Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("bizList"         , listBiz);
		resultMap.put("svrList" 		, listServer);
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;            
    }   
    
	@RequestMapping(value= "/bat/transaction/interface/interfaceMonitorMan.json",params = "cmd=DETAIL")
	public ModelAndView selectDetail(HttpServletRequest request,
			HttpServletResponse response,String bjobDmndMsgID) throws Exception {
		
		HashMap<String, Object> map = service.selectDetail(bjobDmndMsgID);

		ModelAndView modelAndView = new ModelAndView("jsonView",map);	
		
		return modelAndView;
	}
	
	@RequestMapping(value= "/bat/transaction/interface/interfaceMonitorMan.json",params = "cmd=LIST_REQUEST")
	public ModelAndView reqeust(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String,Object> map ) throws Exception {

		String intfId = (String)map.get("intfId");
		String checkBaseDate = (String)map.get("checkBaseDate");
		String checkRowCnt = (String)map.get("checkRowCnt");
		String checkParam = (String)map.get("checkParam");
		if ( checkBaseDate == null || checkBaseDate.equals("null")) checkBaseDate = ""; 
		if ( checkRowCnt == null || checkRowCnt.equals("null")) checkRowCnt = ""; 
		if ( checkParam == null || checkParam.equals("null")) checkParam = ""; 
		String req = StringUtils.rightPad(intfId, 30, ' ' ) +  StringUtils.rightPad(checkBaseDate, 8, ' ' ) +
				     StringUtils.leftPad(checkRowCnt, 10, '0' ) + "U" + checkParam + "@@";

		CommonCommand command = new CommonCommand("com.eactive.eai.agent.transaction.RequestTransactionCommand",req);
		HashMap<String, Object> result = agentUtilService.broadcast(command);
		Iterator<String> iter = result.keySet().iterator();
		String message = iter.next();
		//System.out.println("message-->" + message);
		map.put("message", (String)result.get(message));
		ModelAndView modelAndView = new ModelAndView("jsonView",map);	
		
		return modelAndView;
		
	}	
}