package com.eactive.eai.rms.bap.statistics.eaibzwkdstcd.daily;

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.rms.bat.statistics.daily.BatMessageDailyService;
import com.eactive.eai.rms.common.spring.BaseController;
import com.eactive.eai.rms.common.util.CamelCaseUtil;
import com.google.gson.Gson;



/**
 * <pre>
 *     1. 기능 : 업무별 일별 거래현황 통계 화면의 컨트롤러 
 * </pre>
 * 
 * @author : music75
 * @version : $Revision: 1.3 $
 * @since : rts 1.0
 */
@Controller
public class ComBjobBzwkDstcdDailyController extends BaseController  {
	
    @Autowired
    @Qualifier("bjobBzwkDstcdDailyService")
    private BjobBzwkDstcdDailyService bapService ;
    
    @Autowired
    @Qualifier("batMessageDailyService")
    private BatMessageDailyService batService ;
    
    
	@RequestMapping(value= "/bap/statistics/comEaibzwkdstcd/comBjobBzwkDstcdDailyMan.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;
	}      

	@RequestMapping(value= "/bap/statistics/comEaibzwkdstcd/comBjobBzwkDstcdDailyMan.json",params = "cmd=LIST")
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response,@RequestParam HashMap<String,Object> paramMap) throws Exception {
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		if((null==paramMap.get("searchSvrDstcd") || "".equals(paramMap.get("searchSvrDstcd")))|| "BAP".equals(paramMap.get("searchSvrDstcd"))){
			map =bapService.selectByEaiBzwkDstcd(paramMap); 
		}else{
			map = batService.selectByIntfId(paramMap);
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();

		resultMap.put("rows"		, map.get("list"));
		resultMap.put("totalSum"	, map.get("totalSum"));
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value= "/bap/statistics/comEaibzwkdstcd/comBjobBzwkDstcdDailyMan.excel",params = "cmd=LIST_GRID_TO_EXCEL")
    public ModelAndView listGridToExcel(HttpServletRequest request, HttpServletResponse response, String gridData, String header, String headerTitle, String fileName, String merge) throws Exception {
		HashMap<String,Object> resultMap = new HashMap<String,Object>();
		
    	Gson gson = new Gson();
		HashMap<String, Object>[] list = gson.fromJson(gridData, HashMap[].class);
		
		String[] headers = gson.fromJson(header, String[].class);
		String[] headerTitles = gson.fromJson(headerTitle, String[].class);
		
		HashMap<String, Object>[] lmerge = null; 
		if (merge !=null){
			lmerge = gson.fromJson(merge, HashMap[].class);
		}
    	
    	resultMap.put("headers", headers);
    	resultMap.put("headerTitles", headerTitles);
    	resultMap.put("datas", list);
    	resultMap.put("fileName", fileName);
    	resultMap.put("merge", lmerge);
		ModelAndView modelAndView = new ModelAndView("excelView",resultMap);	
        
        return modelAndView;
    }	
}
