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

import java.io.Serializable;
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.ComboService;
import com.eactive.eai.rms.common.combo.IbatisComboService;
import com.eactive.eai.rms.common.util.CamelCaseUtil;
import com.eactive.eai.rms.common.vo.JsonPageVo;

@Controller
public class BapTransactionStatusController extends BapBaseAnnotationController  {

	@Autowired
	@Qualifier("bapTransactionStatusService")
	private BapTransactionStatusService service;

    @Autowired
    private IbatisComboService comboService;    

	@RequestMapping(value= "/bap/transaction/transactionStatusMan.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/transactionStatusMan.json", params = "cmd=LIST")
	public ModelAndView selectList(HttpServletRequest request, JsonPageVo pageVo,
			@RequestParam HashMap<String, Object> paramMap) throws Exception {
		
		String date = (String)paramMap.get("searchStartYYYYMMDD");
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.schedule.GetScheduleBasisListCommand",date);
		
		Map<String, Object> returnMap = agentUtilService.broadcast(command);
		Iterator<String> iter = returnMap.keySet().iterator();
		String svrName = iter.next();
		String[] basis = null;
		
		if(returnMap.get(svrName) instanceof String[]) {
			basis = (String[])returnMap.get(svrName);
		}

		if ( basis == null ){
			basis = new String[1];
			basis[0] = "D";
		}
		paramMap.put("basis", basis);
		
		String activeTab = (String)paramMap.get("activeTab");
		if (  activeTab.equals("tabS") ){
			command = new CommonCommand("com.eactive.eai.agent.transaction.ProcessingFileTransferCommand","");
			returnMap = agentUtilService.broadcast(command);
			iter = returnMap.keySet().iterator();
			svrName = iter.next();
			
			Object value = returnMap.get(svrName);
			
			
			if(value instanceof List) {
				@SuppressWarnings("unchecked")
				List<Map<String, Serializable>> list = (List<Map<String, Serializable>>)value;
				paramMap.put("procList", list);
			}else {
				paramMap.put("procList", null);
			}
			
		}
		
		//Map<String, Object> resultMap = service.selectList( paramMap);

		//ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);
		
		Map<String, Object> listMap = service.selectList(pageVo.getStartNum(), pageVo.getEndNum(), paramMap);

		pageVo.setTotalCount((Integer)listMap.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"   , listMap.get("rows"));
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		
		return modelAndView;
		
	}

	@RequestMapping(value= "/bap/transaction/transactionStatusMan.json",params = "cmd=DETAIL_FILE")
	public ModelAndView selectDetailFile(HttpServletRequest request, JsonPageVo pageVo,
			HttpServletResponse response, @RequestParam HashMap<String,Object> param) throws Exception {
		
		HashMap<String, Object> map = service.selectDetailFile(pageVo.getStartNum(), pageVo.getEndNum(), param);
		
		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("file"));
		resultMap.putAll(map);  // map(detail,file,stage,totalCount)

		ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);
		
		return modelAndView;
	}

	@RequestMapping(value= "/bap/transaction/transactionStatusMan.json",params = "cmd=DETAIL_STAGE")
	public ModelAndView selectDetailStage(HttpServletRequest request, JsonPageVo pageVo,
			HttpServletResponse response, @RequestParam HashMap<String,Object> param) throws Exception {
		
		HashMap<String, Object> map = service.selectDetailStage(pageVo.getStartNum(), pageVo.getEndNum(), param);
		
		pageVo.setTotalCount((Integer)map.get("totalCount"));

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("total"  , pageVo.getTotal());
		resultMap.put("pageFile"   , pageVo.getPage());
		resultMap.put("records", pageVo.getTotalCount());
		resultMap.put("rows"   , map.get("stage"));
		resultMap.putAll(map);  // map(detail,file,stage,totalCount)

		ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);
		
		return modelAndView;
	}

	@RequestMapping(value= "/bap/transaction/transactionStatusMan.json",params = "cmd=LIST_INIT_COMBO")
    public ModelAndView initCombo( HttpServletRequest request,
            HttpServletResponse response) throws Exception {
    	
		List<Map<String, Object>> bjobBzwkDstcd  = comboService.selectListComboForTableWithCode("TSEAIBJ02", "BJOBBZWKDSTCD", "BJOBBZWKNAME");		
		List<Map<String, Object>> osidInstiDstcd  = service.selectOsidInstiDstcd("");		
//		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("osidInstiDstcd" , osidInstiDstcd);
//		resultMap.put("uapplCd"       , uapplCd);
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;            
    }     

	@RequestMapping(value= "/bap/transaction/transactionStatusMan.json",params = "cmd=LIST_ORGN_COMBO")
	public ModelAndView initOrgnCombo( HttpServletRequest request,
			HttpServletResponse response, String bjobBzwkDstcd) throws Exception {

		List<Map<String, Object>> osidInstiDstcd  = service.selectOsidInstiDstcd(bjobBzwkDstcd);

		Map<String, Object> resultMap = new HashMap<String, Object>();

		resultMap.put("osidInstiDstcd" , osidInstiDstcd);
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);
		return modelAndView;
	}

	@RequestMapping(value= "/bap/transaction/transactionStatusMan.json",params = "cmd=DETAIL_FILE_DATA")
	public ModelAndView getFileData(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String,Object> param) throws Exception {
		
		String[] args = null;
		args = new String[4];
		args[0] = (String) param.get("rcvFileName");
		args[1] = (String) param.get("skipCnt");
		args[2] = (String) param.get("loopCnt");
		args[3] = (String) param.get("bjobMsgDstcd");

		Iterator<String> iter = null;
		String svrName = "";
		CommonCommand command = new CommonCommand("com.eactive.eai.common.util.FileDataCommand", args);
		HashMap<String, Object> returnMap = agentUtilService.broadcast(command);
		
		iter = returnMap.keySet().iterator();
		svrName = iter.next();
		@SuppressWarnings("unchecked")
		HashMap<String, Object> resultMap = (HashMap<String, Object>)returnMap.get(svrName);

		ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);

		return modelAndView;
	}

	@RequestMapping(value= "/bap/transaction/transactionStatusMan.json",params = "cmd=TRANSACTION_CHANGE")
	public ModelAndView statusChenge(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String,Object> param) throws Exception {
		String message = "";
		try {
			service.updateStatusChangeMaster(param);
			service.updateStatusChange(param);
			message = "전송상태를 변경하였습니다.";
		} catch(Exception e) {
			message = "전송상태를 변경시 오류가 발생하였습니다.";
		}

		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("result"   , message);
		ModelAndView modelAndView = new ModelAndView("jsonView", returnMap);

		return modelAndView;
	}
}