package com.eactive.eai.rms.bap.manage.work.message;

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.util.StringUtils;
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;
import com.google.gson.Gson;

@Controller
public class BapMessageProcController extends BapBaseAnnotationController  {

	@Autowired
	@Qualifier("bapMessageProcService")
	private BapMessageProcService service;

    @Autowired
    private IbatisComboService comboService;

	@RequestMapping(value= "/bap/admin/work/messageProcMan.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/work/messageProcMan.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/work/messageProcMan.json",params = "cmd=DETAIL")
	public ModelAndView selectDetail(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String,Object> param) throws Exception {
		
		HashMap<String, Object> map = service.selectDetail(param);

		ModelAndView modelAndView = new ModelAndView("jsonView", map);
		
		return modelAndView;
	}
	
	@RequestMapping(value= "/bap/admin/work/messageProcMan.json",params = "cmd=INSERT")
	public String insert(HttpServletRequest request,
			HttpServletResponse response,@RequestParam HashMap<String,Object> map, String gridData, String sysConnList ) throws Exception {
    	Gson gson = new Gson();
    	@SuppressWarnings("unchecked")
		HashMap<String, Object>[] list = gson.fromJson(gridData, HashMap[].class);
    	@SuppressWarnings("unchecked")
    	HashMap<String, Object> connList = gson.fromJson(sysConnList, HashMap.class);
		String userid = com.eactive.eai.rms.common.login.SessionManager.getUserId(request);
		map.put("userid", userid );
    	service.insert(map,list, connList);
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.rule.dirInfo.RefreshAllJobCommand");
		agentUtilService.broadcast(command);
    	command = new CommonCommand("com.eactive.eai.agent.property.UpdatePropertyCommand",(String)map.get("prptyGroupName"));
		agentUtilService.broadcast(command);

    	return null;
	}	

	@RequestMapping(value= "/bap/admin/work/messageProcMan.json",params = "cmd=UPDATE")
	public String update(HttpServletRequest request,
			HttpServletResponse response,@RequestParam HashMap<String,Object> map, String gridData, String sysConnList ) throws Exception {

    	Gson gson = new Gson();
    	@SuppressWarnings("unchecked")
		HashMap<String, Object>[] list = gson.fromJson(gridData, HashMap[].class);
    	@SuppressWarnings("unchecked")
    	HashMap<String, Object> connList = gson.fromJson(sysConnList, HashMap.class);
		String userid = com.eactive.eai.rms.common.login.SessionManager.getUserId(request);
		map.put("userid", userid);
		service.update(map, list, connList);
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.rule.dirInfo.RefreshAllJobCommand");
		agentUtilService.broadcast(command);
    	command = new CommonCommand("com.eactive.eai.agent.property.UpdatePropertyCommand",(String)map.get("prptyGroupName"));
		agentUtilService.broadcast(command);

		return null;
	}

	@RequestMapping(value= "/bap/admin/work/messageProcMan.json",params = "cmd=DELETE")
	public String delete(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String, Object> paramMap ) throws Exception {
		service.delete(paramMap);
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.rule.dirInfo.RefreshAllJobCommand");
		agentUtilService.broadcast(command);
    	command = new CommonCommand("com.eactive.eai.agent.property.RemovePropertyCommand",(String)paramMap.get("prptyGroupName"));
		agentUtilService.broadcast(command);
		return null;
		
	}

	@RequestMapping(value= "/bap/admin/work/messageProcMan.json",params = "cmd=LIST_INIT_COMBO")
    public ModelAndView initGroupCoCdCombo( HttpServletRequest request,
            HttpServletResponse response) throws Exception {
    	
//		List<Map<String, Object>> groupCoCd      = comboService.selectListCombo("EAIGROUPCOCD");
		List<Map<String, Object>> bjobBzwkDstcd  = comboService.selectListComboForTableWithCode("TSEAIBJ02", "BJOBBZWKDSTCD", "BJOBBZWKNAME");
		List<Map<String, Object>> bjobInstiDstcd = comboService.selectListComboForTableWithCode("TSEAIBJ06", "OSIDINSTIDSTCD", "OSIDINSTINAME");
//		
		List<Map<String, Object>> sysCd          = comboService.selectListComboForTableWithCode("TSEAIBJ11", "SYSCD", "SYSNAME");
//		List<Map<String, Object>> uapplCd        =  //comboService.selectListComboForTableWithCode("TSEAICM01", "TRIM(EAIBZWKDSTCD)", "BZWKDSTICNAME");
//		comboService.selectListComboForTable("TSEAICM01", "TRIM(EAIBZWKDSTCD)",  "BZWKDSTICNAME","systeminoutdstcd = 'I' ");
//		List<Map<String, Object>> bJobCycleGb    = //comboService.selectListCombo("BAP_CYCLE_GB"); // BATCH작업주기구분
//		comboService.selectListComboForTable("TSEAICM20", "code",  "codename","codegroup = 'BAP_CYCLE_GB' ");		
		List<Map<String, Object>> uapplCd = comboService.selectCM01ComboList("TRIM(EAIBZWKDSTCD)", "BZWKDSTICNAME");

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

		resultMap.put("bjobBzwkDstcd" , bjobBzwkDstcd);
		resultMap.put("bjobInstiDstcd", bjobInstiDstcd);
//		resultMap.put("groupCoCd"     , groupCoCd);
		resultMap.put("sysCd"         , sysCd);
		resultMap.put("uapplCd"       , uapplCd);
//		resultMap.put("bJobCycleGb"   , bJobCycleGb);
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);
		return modelAndView;
    }
	@RequestMapping(value= "/bap/admin/work/messageProcMan.json",params = "cmd=LIST_PROP")
    public ModelAndView getProps( HttpServletRequest request,
            HttpServletResponse response, 
            @RequestParam  String bjobBzwkDstcd
            ) throws Exception {

		HashMap<String, Object> map     = service.getProps(bjobBzwkDstcd);		

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

		return modelAndView;
    }

	@RequestMapping(value= "/bap/admin/work/messageProcMan.json",params = "cmd=LIST_OSID_INSTI")
	public ModelAndView selectOsidInstiDstcd( HttpServletRequest request,
            HttpServletResponse response, @RequestParam  String bjobBzwkDstcd) throws Exception {
		
		HashMap<String, Object> map = service.selectOsidInstiDstcd(bjobBzwkDstcd);
		ModelAndView modelAndView = new ModelAndView("jsonView", map);
		
		return modelAndView;
	}

	@RequestMapping(value= "/bap/admin/work/messageProcMan.json",params = "cmd=INITIALIZE_PK")
	public ModelAndView checkPK( HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam  String tableName,
            @RequestParam  String columnName,
            @RequestParam  String columnValue
            ) throws Exception {
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("tableName", tableName);
		param.put("columnName", columnName);
		param.put("columnValue", columnValue);

    	Map<String, Object> resultMap = new HashMap<String, Object>();
    	resultMap.put("checkPK", service.checkPK(param));

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

	@RequestMapping(value= "/bap/admin/work/messageProcMan.json",params = "cmd=DETAIL_VERIFY_INFO")
	public ModelAndView selectVerifyInfo(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String,Object> param) throws Exception {
		
		HashMap<String, Object> map = service.selectVerifyInfo(param);
		ModelAndView modelAndView = new ModelAndView("jsonView", map);
		
		return modelAndView;
	}

	@RequestMapping(value= "/bap/admin/work/messageProcMan.json",params = "cmd=UPDATE_VERIFY_INFO")
	public String updateVerifyInfo(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String, Object> param) throws Exception {
		
		String userid = com.eactive.eai.rms.common.login.SessionManager.getUserId(request);
		param.put("userid", userid );
		service.updateVerifyInfo(param);
		
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.rule.dirInfo.RefreshAllJobCommand");
		agentUtilService.broadcast(command);
		return null;
	}
}