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

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

/**
 * <pre>
 *     1. 기능 : 업무구분코드 관리 화면의 컨트롤러
 * </pre>
 *
 * @author : 
 * @version : $Revision: 1.24 $
 * @since : EMS 3.0
 */

@Controller
public class BizDistController extends BatBaseAnnotationController  {

	@Autowired
	@Qualifier("bizDistService")
	private BizDistService service; 
	
    @Autowired
    @Qualifier("comboService")
    private ComboService comboService; 
	
	@RequestMapping(value= "/bat/admin/common/bizDistMan.view")
	private 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;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value= "/bat/admin/common/bizDistMan.json", params = "cmd=LIST")
	private ModelAndView selectList(HttpServletRequest request,
			JsonPageVo pageVo ,@RequestParam HashMap<String,Object> paramMap) throws Exception {
		
		HashMap map = service.selectList( pageVo.getStartNum(), pageVo.getEndNum(), paramMap);
		
		pageVo.setTotalCount((Integer)map.get("totalCount"));
		
		Map resultMap = new HashMap();
		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/bizDistMan.json",params = "cmd=LIST_INIT_COMBO")
	public ModelAndView initCombo( HttpServletRequest request,
	       HttpServletResponse response) throws Exception {
	   List<ComboVo> listYn  = comboService.getFromCode("SEARCH_YN");//YN
	   List<ComboVo> listInputCd  = comboService.getFromCode("INPUTDSTCD");//YN
	   
	   Map resultMap = new HashMap();
       resultMap.put("ynRows"  , listYn ); 
       resultMap.put("inputDstCdRows"  , listInputCd); 
			
	   ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
	   return modelAndView;            
	} 	

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value= "/bat/admin/common/bizDistMan.json",params = "cmd=DETAIL")
	private ModelAndView selectDetail(HttpServletRequest request,
			HttpServletResponse response,String eaiBzwkDstcd) throws Exception {
		
		HashMap map = service.selectDetail(eaiBzwkDstcd);

		ModelAndView modelAndView = new ModelAndView("jsonView",map);	
		
		return modelAndView;
	}
	@RequestMapping(value= "/bat/admin/common/bizDistMan.json",params = "cmd=INSERT")
	private String insert(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String,Object> map  ) throws Exception {
	
		service.insert(map);
		return null;
	}	
	@RequestMapping(value= "/bat/admin/common/bizDistMan.json",params = "cmd=UPDATE")
	private String update(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String,Object> map  ) throws Exception {
		
		service.update(map);
		return null;
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value= "/bat/admin/common/bizDistMan.json",params = "cmd=DELETE")
	private String delete(HttpServletRequest request,
			HttpServletResponse response, String eaiBzwkDstcd ) throws Exception {
		HashMap paramMap = new HashMap();
		paramMap.put("eaiBzwkDstcd", eaiBzwkDstcd);		
		service.delete(paramMap);
		return null;
	}	
	@RequestMapping(value= "/bat/admin/common/bizDistMan.json",params = "cmd=LIST_DELETE")
	public String deleteList(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String,Object> map ) throws Exception {

		String[] eaiBzwkDstcdList = map.get("eaiBzwkDstcdList").toString().split(",");
		for ( int i=0; i < eaiBzwkDstcdList.length; i++ ) {
			String eaiBzwkDstcd = eaiBzwkDstcdList[i];
			HashMap<String, Object> paramMap = new HashMap<String,Object>();
			paramMap.put("eaiBzwkDstcd", eaiBzwkDstcd);
			
			service.delete(paramMap);
		}
		return null;
	}
	@RequestMapping(value= "/bat/admin/common/bizDistMan.json",params = "cmd=LIST_DOWNLOAD_SQL")
	public ModelAndView downloadSql(HttpServletRequest request,
		HttpServletResponse response,@RequestParam HashMap<String,Object> map) throws Exception {		

    	ModelAndView modelAndView = new ModelAndView("jsonView",service.mkInserSqlForBzwkDstcd(map));	
		
		return modelAndView;
	}
	
	@RequestMapping(value= "/bat/admin/common/bizDistMan.view",params = "cmd=LIST_FILE_DOWNLOAD")
	public String fileDownload(HttpServletRequest request) throws Exception {
		
		return "/bat/admin/common/filedownload";
	}
}