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

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

@Controller
public class BapScheduleMonitorController extends BapBaseAnnotationController  {

	@Autowired
	@Qualifier("bapScheduleMonitorService")
	private BapScheduleMonitorService service;
	
	@Autowired
	@Qualifier("bapTransactionStatusService")
	private BapTransactionStatusService transactionService;
	
	@Autowired
	private ComboService comboService;
	
    @Autowired
    private IbatisComboService ibatisComboService;
	
	@RequestMapping(value= "/bap/admin/schedule/scheduleMonitorMan.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;
	}
	
	@RequestMapping(value= "/bap/admin/schedule/scheduleMonitorMan.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/scheduleMonitorMan.json",params = "cmd=LIST_INIT_COMBO")
    public ModelAndView initGroupCoCdCombo( HttpServletRequest request, HttpServletResponse response) throws Exception {
		String bjobBzwkDstcdParam = request.getParameter("bjobBzwkDstcd");
		
		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("bjobBzwkDstcd", bjobBzwkDstcd);
		resultMap.put("osidInstiDstcd", osidInstiDstcd);
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);
		return modelAndView;      
    }
}