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

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
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.onl.common.util.DateUtil;
import com.eactive.eai.rms.onl.statistics.backup.service.DailyBackupService;
import com.eactive.eai.rms.onl.statistics.backup.service.MonthlyBackupService;
import com.eactive.eai.rms.onl.statistics.backup.service.NewLogDailyBackupService;



/**
 * <pre>
 *     1. 기능 : 업무별 일별 거래현황 통계 화면의 컨트롤러 
 * </pre>
 * 
 * @author : music75
 * @version : $Revision: 1.3 $
 * @since : rts 1.0
 */
@Controller
public class CreaStatisticsManagementController extends BaseController  {
    
    @Autowired
	@Qualifier("dailyBackupService")
	private DailyBackupService dailyBackupService ;

	@Autowired
	@Qualifier("monthlyBackupService")
	private MonthlyBackupService monthlyBackupService ;
	
	@Autowired
	@Qualifier("newLogDailyBackupService")
	private NewLogDailyBackupService newLogDailyBackupService ;
	
	@Autowired
	private ComboService comboService;
    
	@RequestMapping(value= "/onl/statistics/management/creaStatisticsMan.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/creaStatisticsMan.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/creaStatisticsMan.json",params = "cmd=INSERT_DAILY")
	public ModelAndView DayHandleRequest(HttpServletRequest request,
			HttpServletResponse response,String day, String serviceType) throws Exception {
		
		if (day == null || day.equals("")){
			dailyBackupService.doDelete(DateUtil.getDate(-1),serviceType);
			dailyBackupService.doAction(DateUtil.getDate(-1),serviceType);
		}else{
			dailyBackupService.doDelete(day,serviceType);
			dailyBackupService.doAction(day,serviceType);

		}

		return null;
	}
	
	@RequestMapping(value= "/onl/statistics/management/creaStatisticsMan.json",params = "cmd=INSERT_MONTHLY")
	public ModelAndView monthHandleRequest(HttpServletRequest request,
			HttpServletResponse response,String month, String serviceType) throws Exception {

		if (month == null || "".equals(month)){
	    	Calendar cal = Calendar.getInstance();
	        cal.add(Calendar.MONTH, -1);
	        Date beforeMonth = cal.getTime();

	        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
	        String date = format.format(beforeMonth);
			
			monthlyBackupService.doDelete(date, serviceType);
			monthlyBackupService.doAction(date, serviceType);
		}else{
			monthlyBackupService.doDelete(month, serviceType);
			monthlyBackupService.doAction(month, serviceType);//20121212 입력한 일을 통계낸다
		}
		return null;
	}
	@RequestMapping(value= "/onl/statistics/management/creaStatisticsMan.json",params = "cmd=INSERT_NEWLOG")
	public ModelAndView nowLogHandleRequest(HttpServletRequest request,
			HttpServletResponse response,String date, String serviceType) throws Exception {
		
		if (date == null || date.equals("")){
			newLogDailyBackupService.doDelete(DateUtil.getDate(-1), serviceType);
			newLogDailyBackupService.doAction(DateUtil.getDate(-1), serviceType);
		}else{
			newLogDailyBackupService.doDelete(date, serviceType);
			newLogDailyBackupService.doAction(date, serviceType);
		}
		return null;
	}
}
