package com.eactive.eai.rms.bat.manage.comm.code;

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

@Controller
public class BatCodeController extends BatBaseAnnotationController  {

	@Autowired
	@Qualifier("batCodeService")
	private BatCodeService batCodeService;
	
	
	@RequestMapping(value= "/bat/admin/common/codeMan.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= "/bat/admin/common/codeMan.json", params = "cmd=LIST")
	public ModelAndView selectList(HttpServletRequest request,
			JsonPageVo pageVo ,String searchMsg, String searchDesc) throws Exception {
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		if (searchMsg != null && searchDesc != null) {
			map = batCodeService.selectList( pageVo.getStartNum(), pageVo.getEndNum(), searchMsg, searchDesc);
		}
		
		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= "/bat/admin/common/codeMan.json",params = "cmd=DETAIL")
	public ModelAndView selectDetail(HttpServletRequest request,
			HttpServletResponse response,String msgId) throws Exception {
		
		HashMap<String, Object> map = batCodeService.selectDetail(msgId);

		ModelAndView modelAndView = new ModelAndView("jsonView",map);	
		
		return modelAndView;
	}
	@RequestMapping(value= "/bat/admin/common/codeMan.json",params = "cmd=INSERT")
	public String insert(HttpServletRequest request,
			HttpServletResponse response, String msgId, String msgCtnt, String treatMatrCtnt ) throws Exception {
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("msgCtnt", msgCtnt);
		paramMap.put("msgId", msgId);		
		batCodeService.insert(paramMap);
    	CommonCommand command = new CommonCommand("com.eactive.eai.agent.code.UpdateCodeMessageCommand",msgId);
		agentUtilService.broadcast(command);
		return null;
		
	}	
	@RequestMapping(value= "/bat/admin/common/codeMan.json",params = "cmd=UPDATE")
	public String update(HttpServletRequest request,
			HttpServletResponse response, String msgId, String msgCtnt, String treatMatrCtnt ) throws Exception {
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("msgCtnt", msgCtnt);
		paramMap.put("treatMatrCtnt", treatMatrCtnt);
		paramMap.put("msgId", msgId);		
		batCodeService.update(paramMap);
		
    	CommonCommand command = new CommonCommand("com.eactive.eai.agent.code.UpdateCodeMessageCommand",msgId);
		agentUtilService.broadcast(command);

		return null;
	}

	@RequestMapping(value= "/bat/admin/common/codeMan.json",params = "cmd=DELETE")
	public String delete(HttpServletRequest request,
			HttpServletResponse response, String msgId ) throws Exception {
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("msgId", msgId);		
		batCodeService.delete(paramMap);
    	CommonCommand command = new CommonCommand("com.eactive.eai.agent.code.RemoveCodeMessageCommand",msgId);
		agentUtilService.broadcast(command);

		return null;
		
	}	
}