package com.eactive.eai.rms.common.acl.deploy;

import java.util.HashMap;
import java.util.Map;

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

import org.springframework.beans.factory.annotation.Autowired;
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.common.base.BaseAnnotationController;
import com.eactive.eai.rms.common.util.CamelCaseUtil;
import com.eactive.eai.rms.common.vo.JsonPageVo;
import com.eactive.eai.rms.onl.loader.service.RuleDeployJobService;

@Controller
public class RuleDeployJobController extends BaseAnnotationController {

	@Autowired
	private RuleDeployJobService ruleDeployJobService;

	@RequestMapping(value = "/common/acl/deploy/ruleDeployJobMan.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 = "/common/acl/deploy/ruleDeployJobMan.json", params = "cmd=LIST")
	public ModelAndView selectList(JsonPageVo pageVo, String searchJobTitle, String searchStatus, String sortName,
			String sortOrder) throws Exception {
		HashMap<String, Object> map = ruleDeployJobService.selectList(pageVo.getStartNum(), pageVo.getEndNum(),
				searchJobTitle, searchStatus, sortName, sortOrder);

		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;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/common/acl/deploy/ruleDeployJobMan.json", params = "cmd=DETAIL")
	private ModelAndView selectDetail(HttpServletRequest request, HttpServletResponse response, String jobId)
			throws Exception {
		HashMap map = ruleDeployJobService.selectJobDetail(jobId);
		ModelAndView modelAndView = new ModelAndView("jsonView", map);
		return modelAndView;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/common/acl/deploy/ruleDeployJobMan.json", params = "cmd=DELETE")
	private String delete(HttpServletRequest request, HttpServletResponse response, String jobId) throws Exception {
		HashMap paramMap = new HashMap();
		paramMap.put("jobId", jobId);
		ruleDeployJobService.delete(paramMap);
		return null;
	}
}
