package com.eactive.eai.rms.common.acl.sendsmslog;

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.common.base.BaseAnnotationController;
import com.eactive.eai.rms.common.context.MonitoringContext;
import com.eactive.eai.rms.common.util.CamelCaseUtil;
import com.eactive.eai.rms.common.vo.JsonPageVo;
import com.google.gson.Gson;


@Controller
public class TranErrorSmsLogController extends BaseAnnotationController {
	
	@Autowired
	@Qualifier("tranErrorSmsLogService")
	private TranErrorSmsLogService service;
	
	@Autowired
    @Qualifier("monitoringContext")
    private MonitoringContext monitoringContext;
	
	@RequestMapping(value = "/common/acl/sendsmslog/tranErrorSmsLogMan.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 errCount = monitoringContext.getStringProperty(MonitoringContext.SMS_ERROR_COUNT,"0");
    	
    	request.setAttribute("errCount", errCount);
    	
		String path = request.getServletPath();
		return path.split("[.]")[0] + cmd;
	}
	
	@RequestMapping(value = "/common/acl/sendsmslog/recvTranErrorSmsSendLogMan.view")
	public String viewList2(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 = "/common/acl/sendsmslog/tranErrorSmsLogMan.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 = "/common/acl/sendsmslog/tranErrorSmsLogMan.json", params = "cmd=LIST2")
	private ModelAndView selectList2(HttpServletRequest request,
			JsonPageVo pageVo ,@RequestParam HashMap<String, Object> paramMap
			) throws Exception {
		HashMap map = service.selectRecvTranList( 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= "/common/acl/sendsmslog/tranErrorSmsLogMan.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>();
		
		HashMap<String, String>[] list2 ;
    	Gson gson = new Gson();
    	String headersStr = (String) paramMap.get("headersStr"); 
		headersStr = headersStr.substring(0, headersStr.length()-1);
		paramMap.put("headersStr", headersStr);
		
		@SuppressWarnings("unchecked")
    	HashMap<String, Object> list = service.selectListToExcel(request,paramMap);
    	
    	String jsonStr = gson.toJson(list.get("list"));
    	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;
    }	
	
	
	@RequestMapping(value= "/common/acl/sendsmslog/tranErrorSmsLogMan.excel",params = "cmd=LIST_GRID_TO_EXCEL2")
	public ModelAndView listGridToExcel2(HttpServletRequest request, HttpServletResponse response, @RequestParam HashMap<String,Object> paramMap) throws Exception {
		HashMap<String,Object> resultMap = new HashMap<String,Object>();
		
		
		HashMap<String, String>[] list2 ;
    	Gson gson = new Gson();
    	String headersStr = (String) paramMap.get("headersStr"); 
		headersStr = headersStr.substring(0, headersStr.length()-1);
		paramMap.put("headersStr", headersStr);
		
		@SuppressWarnings("unchecked")
    	HashMap<String, Object> list = service.selectListToExcel2(request,paramMap);
    	
    	String jsonStr = gson.toJson(list.get("list"));
    	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;
    }	
	
	

}
