package com.dbs.mplus.controller.admin;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.dbs.mplus.exception.admin.AdminControllerException;
import com.dbs.mplus.framework.exception.MPlusServiceException;
import com.dbs.mplus.framework.service.admin.agg.AggAccessDailyService;
import com.dbs.mplus.framework.service.admin.agg.AggAccessHourlyService;
import com.dbs.mplus.framework.service.admin.agg.AggEventLogService;
import com.dbs.mplus.framework.service.admin.agg.AggMenuService;
import com.dbs.mplus.framework.service.admin.agg.AggMsgSendResultLogService;
import com.dbs.mplus.framework.service.admin.agg.AggPlatformService;
import com.dbs.mplus.framework.service.admin.agg.AggUserPlatformService;
import com.dbs.mplus.framework.service.admin.agg.AggUserRegisterService;
import com.dbs.mplus.framework.service.admin.agg.repository.dao.AggAccessDaily;
import com.dbs.mplus.framework.service.admin.agg.repository.dao.AggAccessHourly;
import com.dbs.mplus.framework.service.admin.agg.repository.dao.AggDailyMsgSendErrorLog;
import com.dbs.mplus.framework.service.admin.agg.repository.dao.AggEventLog;
import com.dbs.mplus.framework.service.admin.agg.repository.dao.AggMenu;
import com.dbs.mplus.framework.service.admin.agg.repository.dao.AggMsgSendResultLog;
import com.dbs.mplus.framework.service.admin.agg.repository.dao.AggPlatform;
import com.dbs.mplus.framework.service.admin.agg.repository.dao.AggUserPlatform;
import com.dbs.mplus.framework.service.admin.agg.repository.dao.AggUserRegister;
import com.dbs.mplus.service.admin.agg.AggUserAgeService;
import com.dbs.mplus.service.admin.agg.AggUserPostnoService;
import com.dbs.mplus.service.admin.agg.repository.dao.AggUserAge;
import com.dbs.mplus.service.admin.agg.repository.dao.AggUserPostno;

/**
 * 통계정보 보내주기 
 * @author DBS 
 * - 2015. 08. 10. hyeeun. 코드정리 
 */
@Controller
@RequestMapping("/admin/stats")
public class AggregationController {
	private static final Logger logger = LoggerFactory.getLogger(AggregationController.class); 
	// 시작날짜. 테이블 컬럼과 맵핑되지 않는, UI와 약속된 검색범위 
	private static final String KEY_START_DATE = "strDate"; 
	// 종료날짜 
	private static final String KEY_END_DATE = "endDate"; 
	
	@Autowired 
	private AggAccessDailyService aggDailyService; 
	@Autowired 
	private AggAccessHourlyService aggHourlyService; 
	@Autowired 
	private AggPlatformService aggPlatformService; 
	@Autowired 
	private AggMenuService aggAccessService; 
	@Autowired
	private AggEventLogService aggDailyEventLogService;
	@Autowired 
	private AggMsgSendResultLogService aggDailyMsgSendResultLogService; 
	@Autowired 
	private AggUserAgeService aggUserAgeService; 
	@Autowired 
	private AggUserPostnoService aggUserPostnoService; 
	@Autowired 
	private AggUserRegisterService aggUserRegisterService; 
	@Autowired 
	private AggUserPlatformService aggUserPlatformService; 
	
	
	// 날짜별 방문자 수 
	@RequestMapping(value = "/getDailyList.json", method = RequestMethod.POST)
	public List<AggAccessDaily> findHitCountByDate(@RequestBody Map<String, String> map) throws AdminControllerException { 		
		final String strDate = map.get(AggregationController.KEY_START_DATE).toString();
		final String endDate = map.get(AggregationController.KEY_END_DATE).toString();
		
		try { 
			return this.aggDailyService.findByDate(strDate, endDate); 
		}
		catch(final MPlusServiceException ex) {
			logger.error("통계정보 수집 실패. strDate : " + strDate + ", endDate : " + endDate, ex); 
			throw new AdminControllerException(ex); 
		}
	} 
	
	// 요일별 방문자 수 
	@RequestMapping(value = "/getWeeklyList.json", method = RequestMethod.POST)
	public List<AggAccessDaily> findHitCountByWeek( @RequestBody Map<String, String> map ) throws AdminControllerException {
		final String strDate = map.get(AggregationController.KEY_START_DATE).toString();
		final String endDate = map.get(AggregationController.KEY_END_DATE).toString();
		
		try { 
			return this.aggDailyService.findByWeek(strDate, endDate); 
		}
		catch(final MPlusServiceException ex) {
			logger.error("통계정보 수집 실패. strDate : " + strDate + ", endDate : " + endDate, ex); 
			throw new AdminControllerException(ex); 
		}
	}
	
	// 시간별 방문자 수 
	@RequestMapping(value = "/getHourlyList.json", method = RequestMethod.POST)
	public List<AggAccessHourly> findHitCountByHour( @RequestBody Map<String, String> map ) throws AdminControllerException {
		final String strDate = map.get(AggregationController.KEY_START_DATE).toString();
		final String endDate = map.get(AggregationController.KEY_END_DATE).toString();
		
		try { 
			return this.aggHourlyService.findByHour(strDate, endDate); 
		}
		catch(final MPlusServiceException ex) {
			logger.error("통계정보 수집 실패. strDate : " + strDate + ", endDate : " + endDate, ex); 
			throw new AdminControllerException(ex); 
		}		
	}	
	
	// 플랫폼별 방문자 수 
	@RequestMapping(value = "/getPlatformList.json", method = RequestMethod.POST)
	public List<AggPlatform> findHitCountByPlatform( @RequestBody Map<String, String> map ) throws AdminControllerException {
		final String strDate = map.get(AggregationController.KEY_START_DATE).toString();
		final String endDate = map.get(AggregationController.KEY_END_DATE).toString();
		
		try { 
			return this.aggPlatformService.findByDate(strDate, endDate); 
		}
		catch(final MPlusServiceException ex) {
			logger.error("통계정보 수집 실패. strDate : " + strDate + ", endDate : " + endDate, ex); 
			throw new AdminControllerException(ex); 
		}	
	}
	
	// 메뉴별 방문자 수 
	@RequestMapping(value = "/getAccessList.json", method = RequestMethod.POST)
	public List<AggMenu> findHistCountByMenu( @RequestBody Map<String, String> map ) throws AdminControllerException {
		final String strDate = map.get(AggregationController.KEY_START_DATE).toString();
		final String endDate = map.get(AggregationController.KEY_END_DATE).toString();
		
		try { 
			return this.aggAccessService.findByDate(strDate, endDate); 
		}
		catch(final MPlusServiceException ex) {
			logger.error("통계정보 수집 실패. strDate : " + strDate + ", endDate : " + endDate, ex); 
			throw new AdminControllerException(ex); 
		}	
	}
	
	// 이벤트 발생 수 
	@RequestMapping(value = "/getEventLogList.json", method = RequestMethod.POST)
	public List<AggEventLog> findHistCountByEventLog( @RequestBody Map<String, String> map ) throws AdminControllerException {
		final String strDate = map.get(AggregationController.KEY_START_DATE).toString();
		final String endDate = map.get(AggregationController.KEY_END_DATE).toString();
		final String searchEvent = map.get("searchEvent");
			
		try { 
			if( searchEvent.equals("") ){					
				return this.aggDailyEventLogService.findByDate(strDate, endDate); 
			} else {
				return this.aggDailyEventLogService.findEventByDate(searchEvent, strDate, endDate); 
			}
		} catch(final MPlusServiceException ex) {
			logger.error("통계정보 수집 실패. strDate : " + strDate + ", endDate : " + endDate, ex); 
			throw new AdminControllerException(ex); 
		}	
	}
	
	// push 전송결과  
	@RequestMapping(value = "/getMsgResultLogList.json", method = RequestMethod.POST)
	public List<AggMsgSendResultLog> findResultCountByMsgResultLog( @RequestBody Map<String, String> map ) throws AdminControllerException {
		final String strDate = map.get(AggregationController.KEY_START_DATE).toString();
		final String endDate = map.get(AggregationController.KEY_END_DATE).toString();
			
		try { 
			return this.aggDailyMsgSendResultLogService.findByDate(strDate, endDate); 
		} 
		catch(final MPlusServiceException ex) {
			logger.error("통계정보 수집 실패. strDate : " + strDate + ", endDate : " + endDate, ex); 
			throw new AdminControllerException(ex); 
		}	
	}	

	// push 전송결과 실패원인 
	@RequestMapping(value = "/getMsgResultErrorTypeList.json", method = RequestMethod.POST)
	public List<AggDailyMsgSendErrorLog> findErrorTypeByMsgResultLog( @RequestBody Map<String, String> map ) throws AdminControllerException {
		final String aggDt = map.get(AggregationController.KEY_START_DATE).toString();
	
		try { 
			return this.aggDailyMsgSendResultLogService.findByErrorType(aggDt); 
		} 
		catch(final MPlusServiceException ex) {
			logger.error("통계정보 수집 실패. aggDt=" + aggDt); 
			throw new AdminControllerException(ex); 
		}	
	}	
	
	// 연령별 가입통계 
	@RequestMapping(value = "/getUserAgeList.json", method = RequestMethod.POST)
	public List<AggUserAge> findUserAgeList( @RequestBody Map<String, String> map ) throws AdminControllerException {
		final String strDate = map.get(AggregationController.KEY_START_DATE).toString();
		final String endDate = map.get(AggregationController.KEY_END_DATE).toString();
		
		try { 
			return this.aggUserAgeService.findByDate(strDate, endDate); 
		}
		catch(final MPlusServiceException ex) {
			logger.error("통계정보 수집 실패. strDate : " + strDate + ", endDate : " + endDate, ex); 
			throw new AdminControllerException(ex); 
		}		
	}	
	
	// 지역별 가입통계 
	@RequestMapping(value = "/getUserPostnoList.json", method = RequestMethod.POST)
	public List<AggUserPostno> findUserPostnoList( @RequestBody Map<String, String> map ) throws AdminControllerException {
		final String strDate = map.get(AggregationController.KEY_START_DATE).toString();
		final String endDate = map.get(AggregationController.KEY_END_DATE).toString();
		
		try { 
			return this.aggUserPostnoService.findByDate(strDate, endDate); 
		}
		catch(final MPlusServiceException ex) {
			logger.error("통계정보 수집 실패. strDate : " + strDate + ", endDate : " + endDate, ex); 
			throw new AdminControllerException(ex); 
		}		
	}		
	
	// 일별 사용자 등록 통계 
	@RequestMapping(value = "/getUserRegisterList.json", method = RequestMethod.POST)
	public List<AggUserRegister> findUserRegisterList( @RequestBody Map<String, String> map ) throws AdminControllerException {
		final String strDate = map.get(AggregationController.KEY_START_DATE).toString();
		final String endDate = map.get(AggregationController.KEY_END_DATE).toString();
		
		try { 
			return this.aggUserRegisterService.findByDate(strDate, endDate); 
		}
		catch(final MPlusServiceException ex) {
			logger.error("통계정보 수집 실패. strDate : " + strDate + ", endDate : " + endDate, ex); 
			throw new AdminControllerException(ex); 
		}		
	}		
	
	// 플랫폼 타입 정보 
	@RequestMapping(value = "/getUserPlatformList.json", method = RequestMethod.POST)
	public List<AggUserPlatform> getUserPlatformList() throws AdminControllerException {
		try { 
			return this.aggUserPlatformService.getList();
		}
		catch(final MPlusServiceException ex) {
			logger.error("통계정보 수집 실패. ", ex); 
			throw new AdminControllerException(ex); 
		}		
	}
	
	// 등록된 사용자와 토큰에 대한 정보 
	@RequestMapping(value = "/getUserTokenInfo.json", method = RequestMethod.POST)
	public Map<String, Long> getUserTokenInfo(@RequestBody Map<String, String> map ) throws AdminControllerException {
	
		try { 
			return this.aggUserPlatformService.getRegisteredInfo(); 
		}
		catch(final MPlusServiceException ex) {
			logger.error("통계정보 수집 실패. ", ex); 
			throw new AdminControllerException(ex); 
		}	
	}
}
