package com.eactive.eai.rms.onl.statistics.management.condition;

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.combo.ComboService;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.common.spring.BaseController;
import com.eactive.eai.rms.common.util.CamelCaseUtil;
import com.eactive.eai.rms.common.vo.JsonPageVo;
import com.google.gson.Gson;



/**
 * <pre>
 *     1. 기능 : 업무별 일별 거래현황 통계 화면의 컨트롤러 
 * </pre>
 * 
 * @author : music75
 * @version : $Revision: 1.3 $
 * @since : rts 1.0
 */
@Controller
public class StatisticsManagementController extends BaseController  {
	
    @Autowired
    @Qualifier("statisticsManagementService")
    private StatisticsManagementService service ;
    
    @Autowired
    private ComboService comboService;
    
	@RequestMapping(value= "/onl/statistics/management/statisticsMan.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= "/onl/statistics/management/statisticsMan.json",params = "cmd=LIST_INIT_COMBO")
    public ModelAndView initCombo( HttpServletRequest request,
            HttpServletResponse response) throws Exception {

		List<ComboVo> listAdptrIoDstcd	= comboService.getFromCode("JOB_TYPE_CD");
        
		Map resultMap = new HashMap();
		resultMap.put("jobTypeCdRows"	, listAdptrIoDstcd);
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;            
    }


	@RequestMapping(value= "/onl/statistics/management/statisticsMan.json",params = "cmd=LIST")
	public ModelAndView handleRequest(JsonPageVo pageVo, @RequestParam HashMap<String, Object> paramMap) throws Exception {
		
		HashMap<String, Object> map = service.selectStatisticsManagement(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 = "/onl/statistics/management/statisticsMan.json", params = "cmd=DETAIL")
	public ModelAndView selectDetail(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String,Object> paramMap) throws Exception {

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

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

		return modelAndView;
	}
	
	@RequestMapping(value = "/onl/statistics/management/statisticsMan.json", params = "cmd=INSERT")
	public ModelAndView insert(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String, Object> map) throws Exception {
		String message = "success";	
		service.insert(map);

		Map resultMap = new HashMap();
		resultMap.put("message"   , message);
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;

	}

	@RequestMapping(value = "/onl/statistics/management/statisticsMan.json", params = "cmd=UPDATE")
	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response,@RequestParam HashMap<String, Object> map) throws Exception {
		String message = "success";	
		service.update(map);

		Map resultMap = new HashMap();
		resultMap.put("message"   , message);
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;
	}

	@RequestMapping(value = "/onl/statistics/management/statisticsMan.json", params = "cmd=DELETE")
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String, Object> map) throws Exception {
		String message = "success";	
		service.delete(map);

		Map resultMap = new HashMap();
		resultMap.put("message"   , message);
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;

	}
	
	  @RequestMapping(value= "/onl/statistics/management/statisticsMan.excel",params = "cmd=LIST_GRID_TO_EXCEL")
		public ModelAndView listGridToExcel(HttpServletRequest request, HttpServletResponse response, @RequestParam HashMap<String,Object> paramMap) throws Exception {
			HashMap<String,Object> resultMap = new HashMap<String,Object>();
			
			String headersStr = (String) paramMap.get("headersStr"); 
			headersStr = headersStr.substring(0, headersStr.length()-1);
			paramMap.put("headersStr", headersStr);
			
	    	Gson gson = new Gson();
	    	@SuppressWarnings("unchecked")
	    	HashMap<String, Object> list = service.selectListToExcel(paramMap);
	    	
	    	String jsonStr = gson.toJson(list.get("list"));
	    	HashMap<String, Object>[] list2 = gson.fromJson(jsonStr, HashMap[].class);
	    	
			String[] headers = gson.fromJson((String) paramMap.get("header"), String[].class);
			String[] headerTitles = gson.fromJson((String)paramMap.get("headerTitle"), String[].class);
			
			HashMap<String, Object>[] lmerge = null; 
			String merge = (String)paramMap.get("merge");
			if (merge !=null){
				lmerge = gson.fromJson(merge, HashMap[].class);
			}
	    	
	    	resultMap.put("headers", headers);
	    	resultMap.put("headerTitles", headerTitles);
	    	resultMap.put("datas", list2);
	    	resultMap.put("fileName", (String)paramMap.get("fileName"));
	    	resultMap.put("merge", lmerge);
			ModelAndView modelAndView = new ModelAndView("excelView",resultMap);	
	        return modelAndView;
	    }	
	
	
}
