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

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.common.base.BapBaseAnnotationController;
import com.eactive.eai.rms.common.combo.ComboService;
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 BapProcPeriodController extends BapBaseAnnotationController  {

	@Autowired
	@Qualifier("bapProcPeriodService")
	private BapProcPeriodService service;

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

	@RequestMapping(value= "/bap/admin/schedule/procPeriodMan.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/procPeriodMan.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/procPeriodMan.json",params = "cmd=DETAIL")
	public ModelAndView selectDetail(HttpServletRequest request,
			HttpServletResponse response, String bJobMsgScheId) throws Exception {

		HashMap<String, Object> map = service.selectDetail(bJobMsgScheId);

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

		service.insert(map);

		return null;
		
	}
	@RequestMapping(value= "/bap/admin/schedule/procPeriodMan.json",params = "cmd=UPDATE")
	public String update(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String, Object> map ) throws Exception {
		
		service.update(map);

		return null;
	}
	@RequestMapping(value= "/bap/admin/schedule/procPeriodMan.json",params = "cmd=DELETE")
	public String delete(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String, Object> map ) throws Exception {
		
		service.delete(map);

		return null;
	}

	@RequestMapping(value= "/bap/admin/schedule/procPeriodMan.json",params = "cmd=DETAIL_INIT_COMBO")
    public ModelAndView initGroupCoCdCombo( HttpServletRequest request,
            HttpServletResponse response, @RequestParam HashMap<String, Object> map) throws Exception {
    	// 업무구분코드
		List<Map<String, Object>> bjobBzwkDstcdList		= ibatisComboService.selectListComboForTable("TSEAIBJ02", "BJOBBZWKDSTCD", "BJOBBZWKNAME", "", "BJOBBZWKDSTCD");
		// 대외기관 구분코드
		List<Map<String, Object>> osidInstiDstcdList	= ibatisComboService.selectListComboForTable("TSEAIBJ06", "OSIDINSTIDSTCD", "OSIDINSTINAME", "", "OSIDINSTIDSTCD");
		// 파일명(BATCH작업메시지구분코드)
		List<Map<String, Object>> bjobMsgDstcdList		= ibatisComboService.selectListComboForTable("TSEAIBJ01", "BJOBMSGDSTCD", "CONCAT(CONCAT(CONCAT('[',BJOBTRANDSTCDNAME),']'),BJOBMSGDSTICNAME)", "", "BJOBMSGDSTCD");
		// 송수신 구분코드
		List<ComboVo> sndRcvTypeList		= comboService.getFromCode("SND_RCV_TYPE_CD");
        
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("bjobBzwkDstcdList"	, bjobBzwkDstcdList);
		resultMap.put("bjobMsgDstcdList"	, bjobMsgDstcdList);
		resultMap.put("osidInstiDstcdList"	, osidInstiDstcdList);
		resultMap.put("sndRcvTypeList"		, sndRcvTypeList);
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);
		return modelAndView;
    }

	@RequestMapping(value= "/bap/admin/schedule/procPeriodMan.json",params = "cmd=DETAIL_OSID_FILE_COMBO")
    public ModelAndView initOrganFileCombo( HttpServletRequest request,
            HttpServletResponse response, String bjobBzwkDstcd) throws Exception {
		
		// where
    	String strBjobBzwkDstcd = "LTRIM(RTRIM(BJOBBZWKDSTCD)) LIKE '" + bjobBzwkDstcd + "%'";
		List<Map<String, Object>> bjobMsgDstcd		= ibatisComboService.selectListComboForTable("TSEAIBJ01", "BJOBMSGDSTCD"	, "CONCAT(CONCAT(CONCAT('[',BJOBTRANDSTCDNAME),']'),BJOBMSGDSTICNAME)", strBjobBzwkDstcd, "BJOBMSGDSTCD");
		List<Map<String, Object>> osidInstiDstcd	= ibatisComboService.selectListComboForTable("TSEAIBJ06", "OSIDINSTIDSTCD", "OSIDINSTINAME"	, strBjobBzwkDstcd, "OSIDINSTIDSTCD");
        
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("bjobMsgDstcd"	, bjobMsgDstcd);
		resultMap.put("osidInstiDstcd"	, osidInstiDstcd);
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;
    }
}