package com.eactive.eai.rms.bap.manage.schedule.shedule;

import java.net.URLDecoder;
import java.util.HashMap;
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.bap.tansaction.transactionStatus.BapTransactionStatusService;
import com.eactive.eai.rms.common.base.BapBaseAnnotationController;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.combo.ComboVo;
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 BapScheduleController extends BapBaseAnnotationController  {

	@Autowired
	@Qualifier("bapScheduleService")
	private BapScheduleService service;
	
	@Autowired
	@Qualifier("bapTransactionStatusService")
	private BapTransactionStatusService transactionService;

	@Autowired
	private ComboService comboService;
	
    @Autowired
    private IbatisComboService ibatisComboService;    

	@RequestMapping(value= "/bap/admin/schedule/scheduleMan.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/admin/schedule/scheduleMan.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/admin/schedule/scheduleMan.json",params = "cmd=DETAIL")
	public ModelAndView selectDetail(HttpServletRequest request,
			HttpServletResponse response, String bJobMsgScheId) throws Exception {
		
		HashMap<String, Object> map = service.selectDetail(URLDecoder.decode(bJobMsgScheId, "UTF-8"));

		ModelAndView modelAndView = new ModelAndView("jsonView", map);	
		
		return modelAndView;
	}
	@RequestMapping(value= "/bap/admin/schedule/scheduleMan.json",params = "cmd=INSERT")
	public String insert(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String, Object> paramMap) throws Exception {

		String userid = com.eactive.eai.rms.common.login.SessionManager.getUserId(request);
		paramMap.put("userid", userid );
		paramMap.put("msgSndrID", userid );
		service.insert(paramMap);
		return null;
		
	}	
	@RequestMapping(value= "/bap/admin/schedule/scheduleMan.json",params = "cmd=UPDATE")
	public String update(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String, Object> paramMap ) throws Exception {

		String userid = com.eactive.eai.rms.common.login.SessionManager.getUserId(request);
		paramMap.put("userid", userid );
		service.update(paramMap);

		return null;
		
	}
	@RequestMapping(value= "/bap/admin/schedule/scheduleMan.json",params = "cmd=DELETE")
	public String delete(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String, Object> paramMap ) throws Exception {
		service.delete(paramMap);
		return null;
		
	}
	@RequestMapping(value= "/bap/admin/schedule/scheduleMan.json",params = "cmd=LIST_DELETE")
	public String deleteList(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String, Object> paramMap ) throws Exception {
		for (String bjobMsgScheId : paramMap.get("bjobMsgScheIdList").toString().split(",")) {
			HashMap<String, Object> paramDetail = new HashMap<String,Object>();
			paramDetail.put("bjobMsgScheID", bjobMsgScheId);			
			service.delete(paramDetail);
		}
		return null;
		
	}

	@RequestMapping(value= "/bap/admin/schedule/scheduleMan.json",params = "cmd=LIST_INIT_COMBO")
    public ModelAndView initGroupCoCdCombo( HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		
		String bjobBzwkDstcdParam = request.getParameter("bjobBzwkDstcd");
    	
		List<ComboVo> groupCoCd     = comboService.getFromCode("EAIGROUPCOCD");		
		List<ComboVo> useYn     = comboService.getFromCode("USE_YN");
		
		List<Map<String, Object>> bjobBzwkDstcd  = ibatisComboService.selectListComboForTableWithCode("TSEAIBJ02", "BJOBBZWKDSTCD", "BJOBBZWKNAME");
		
		List<Map<String, Object>> osidInstiDstcd = null;
		
		if(bjobBzwkDstcdParam == null || bjobBzwkDstcdParam.equals("")) {
			osidInstiDstcd  = transactionService.selectOsidInstiDstcd("");
		}else {
			osidInstiDstcd  = transactionService.selectOsidInstiDstcd(bjobBzwkDstcdParam);
		}
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("groupCoCd"         , groupCoCd);
		resultMap.put("useYn"         	  , useYn);
		resultMap.put("bjobBzwkDstcd"         , bjobBzwkDstcd);
		resultMap.put("osidInstiDstcd"         , osidInstiDstcd);
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;            
    }     

	@RequestMapping(value= "/bap/admin/schedule/scheduleMan.json",params = "cmd=LIST_BJOB_BZWK_COMBO")
    public ModelAndView initBjobBzwkDstcdCombo( HttpServletRequest request,
            HttpServletResponse response) throws Exception {
    	
		List<Map<String, Object>> bjobBzwkDstcd     = ibatisComboService.selectListComboForTableWithCode("TSEAIBJ02", "BJOBBZWKDSTCD", "BJOBBZWKNAME");		
        
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("bjobBzwkDstcd"         , bjobBzwkDstcd);
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;            
    }     
	
	@RequestMapping(value= "/bap/admin/schedule/scheduleMan.json",params = "cmd=LIST_ORGAN_FILE")
    public ModelAndView initOrganFileCombo( HttpServletRequest request,
            HttpServletResponse response, String bjobBzwkDstcd) throws Exception {
    	
		List<Map<String, Object>> bjobMsgDstcd     = service.selectBjobMsgDstcd(bjobBzwkDstcd);
		List<Map<String, Object>> osidInstiDstcd   = service.selectOsidInstiDstcd(bjobBzwkDstcd);
        
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("bjobMsgDstcd"         , bjobMsgDstcd);
		resultMap.put("osidInstiDstcd"       , osidInstiDstcd);
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;            
    }

	@RequestMapping(value= "/bap/admin/schedule/scheduleMan.json",params = "cmd=LIST_SEND_RECV")
    public ModelAndView initSendRecvCombo( HttpServletRequest request,
            HttpServletResponse response, String bjobMsgDstcd) throws Exception {
    	
		List<Map<String, Object>> bjobPtrnDstcd     = service.selectBjobPtrnDstcd(bjobMsgDstcd);
        
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("bjobPtrnDstcd"         , bjobPtrnDstcd);
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;            
    }

	@RequestMapping(value= "/bap/admin/schedule/scheduleMan.json",params = "cmd=TRANSACTION")
	public String transaction(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String, Object> map ) throws Exception {

		String[] args = new String[2];
		args[0] = (String)map.get("bJobMsgScheId");
		args[1] = (String)map.get("standYmd");
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.schedule.RequestRecieveCommand", args);
		agentUtilService.broadcast(command);
		return null;
	}
	
	@RequestMapping(value= "/bap/admin/schedule/scheduleMan.json",params = "cmd=TRANSACTION_SEND")
	public String transactionSend(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String, Object> map ) throws Exception {

		String[] args = new String[5];
		args[0] = (String)map.get("bjobBzwkDstcd");
		args[1] = (String)map.get("osidInstiDstcd");
		args[2] = (String)map.get("bjobMsgDstcd");
		args[3] = (String)map.get("recCount");
		args[4] = (String)map.get("standYmd");
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.testData.MakeTestFileCommand", args);
		agentUtilService.broadcast(command);
		return null;
	}

	@RequestMapping(value= "/bap/admin/schedule/scheduleMan.json",params = "cmd=LIST_DOWNLOAD_SQL")
	public ModelAndView downloadSql(HttpServletRequest request,
		HttpServletResponse response,@RequestParam HashMap<String,Object> map) throws Exception {		

		map.put("userid",com.eactive.eai.rms.common.login.SessionManager.getUserId(request));
    	ModelAndView modelAndView = new ModelAndView("jsonView",service.mkInsertSqlForSched(map));	
		
		return modelAndView;
	}
	
	@RequestMapping(value= "/bap/admin/schedule/scheduleMan.view",params = "cmd=LIST_FILE_DOWNLOAD")
	public String fileDownload(HttpServletRequest request) throws Exception {
		
		return "/bap/admin/common/filedownload";
	}
}