package com.eactive.eai.rms.bap.tansaction.transactionTrace;

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.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.base.BapBaseAnnotationController;
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 BapTransactionTraceController extends BapBaseAnnotationController  {

	@Autowired
	@Qualifier("bapTransactionTraceService")
	private BapTransactionTraceService service;

    @Autowired
    private IbatisComboService comboService;    

	@RequestMapping(value= "/bap/transaction/transactionTraceMan.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= "/bap/transaction/transactionTraceMan.json", params = "cmd=LIST")
	public ModelAndView selectList(HttpServletRequest request,
			JsonPageVo pageVo , @RequestParam HashMap<String, Object> paramMap) throws Exception {
		
		HashMap<String, Object> map = service.selectList( pageVo.getStartNum(), pageVo.getEndNum(), paramMap);
		
		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= "/bap/transaction/transactionTraceMan.json",params = "cmd=LIST_INIT_COMBO")
    public ModelAndView initCombo( HttpServletRequest request,
            HttpServletResponse response) throws Exception {
    	
//		List<Map<String, Object>> groupCoCd      = comboService.selectListComboForTable("TSEAICM20", "code",  "codename","codegroup = 'EAIGROUPCOCD' ");
		List<Map<String, Object>> bjobBzwkDstcd  = comboService.selectListComboForTableWithCode("TSEAIBJ02", "BJOBBZWKDSTCD", "BJOBBZWKNAME");		
//		List<Map<String, Object>> uapplCd        = comboService.selectListComboForTableWithCode("TSEAICM01", "TRIM(EAIBZWKDSTCD)", "BZWKDSTICNAME");	
//		List<Map<String, Object>> uapplCd = comboService.selectCM01ComboList("TRIM(EAIBZWKDSTCD)", "BZWKDSTICNAME");
        
		Map<String, Object> resultMap = new HashMap<String, Object>();

		resultMap.put("bjobBzwkDstcd" , bjobBzwkDstcd);
//		resultMap.put("groupCoCd"     , groupCoCd);
//		resultMap.put("uapplCd"       , uapplCd);
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;            
    }     

	@RequestMapping(value= "/bap/transaction/transactionTraceMan.json",params = "cmd=TRANSACTION_RESEND")
	public ModelAndView resend(HttpServletRequest request, String bjobDmndMsgID, String bjobDmndSubMsgID, String fileTrsmtLogID, String sndRecvFileName, String bjobMsgDstcd) throws Exception {
		
		String[] args = null;
		CommonCommand command = null;

		if ( !bjobDmndMsgID.trim().equals("")){
			args = new String[3];
			args[0] = bjobDmndMsgID;
			args[1] = bjobDmndSubMsgID;
			args[2] = sndRecvFileName;
			command = new CommonCommand("com.eactive.eai.agent.transaction.ReSendRSDataCommand",args);
		}else{
			args = new String[3];
			args[0] = fileTrsmtLogID;
			args[1] = SessionManager.getUserId(request);
			args[2] = bjobMsgDstcd;
			command = new CommonCommand("com.eactive.eai.agent.ftp.RemoteTransferCommand",args);
		}
		
		
		HashMap<String, Object> resultMap = agentUtilService.broadcast(command);
		Iterator<String> iter = resultMap.keySet().iterator();
		Map<String, Object> returnMap = new HashMap<String, Object>();
		StringBuffer sb = new StringBuffer();
		while (iter.hasNext()) {
			String svrName = iter.next();
			sb.append( "[" + svrName + "]:" + (String)resultMap.get(svrName) + "\r" );
		}
		
		returnMap.put("result"   , new String(sb));
		
		ModelAndView modelAndView = new ModelAndView("jsonView",returnMap);
		return modelAndView;
	}

	@RequestMapping(value= "/bap/transaction/transactionTraceMan.json",params = "cmd=TRANSACTION_RERECV")
	public ModelAndView rerecv(HttpServletRequest request, String bjobDmndMsgID, String bjobDmndSubMsgID, String rqstRecvFileStorgDirname, String bkupSndrcvFilename) throws Exception {

		String[] args = null;
		CommonCommand command = null;

		args = new String[4];
		args[0] = bjobDmndMsgID;
		args[1] = bjobDmndSubMsgID;
		args[2] = rqstRecvFileStorgDirname;
		args[3] = bkupSndrcvFilename;
		command = new CommonCommand("com.eactive.eai.agent.transaction.ReRecvRSDataCommand", args);

		HashMap<String, Object> resultMap = agentUtilService.broadcast(command);
		Iterator<String> iter = resultMap.keySet().iterator();
		Map<String, Object> returnMap = new HashMap<String, Object>();
		StringBuffer sb = new StringBuffer();
		while (iter.hasNext()) {
			String svrName = iter.next();
			sb.append( "[" + svrName + "]:" + (String)resultMap.get(svrName) + "\r" );
		}
		
		returnMap.put("result"   , new String(sb));
		
		ModelAndView modelAndView = new ModelAndView("jsonView", returnMap);
		return modelAndView;
	}
}