package itn.let.sts.cst.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import egovframework.rte.fdl.property.EgovPropertyService;
import egovframework.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;
import itn.let.sts.com.StatsVO;
import itn.let.sts.cst.service.EgovConectStatsService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 접속 통계 검색 컨트롤러 클래스
 * @author 공통서비스 개발팀 박지욱
 * @since 2009.03.19
 * @version 1.0
 * @see
 *
 * <pre>
 * << 개정이력(Modification Information) >>
 *
 *   수정일      수정자          수정내용
 *  -------    --------    ---------------------------
 *  2009.03.19  박지욱          최초 생성
 *  2011.06.30  이기하          패키지 분리(sts -> sts.cst)
 *  2011.08.31  JJY            경량환경 템플릿 커스터마이징버전 생성
 *
 *  </pre>
 */
@Controller
public class EgovConectStatsController {

	/** EgovConectStatsService */
	@Resource(name = "conectStatsService")
    private EgovConectStatsService conectStatsService;

	@Resource(name = "propertiesService")
	protected EgovPropertyService propertyService;
    /**
	 * 접속 통계를 조회한다
	 * @param statsVO StatsVO
	 * @return String
	 * @exception Exception
	 */
    @RequestMapping(value="/sts/cst/selectConectStats.do")
	public String selectUserStats(@ModelAttribute("statsVO") StatsVO statsVO, HttpSession session,
			ModelMap model) throws Exception {
    	
    	statsVO.setPageUnit(propertyService.getInt("pageUnit"));
    	statsVO.setPageSize(propertyService.getInt("pageSize"));

		PaginationInfo paginationInfo = new PaginationInfo();
		paginationInfo.setCurrentPageNo(statsVO.getPageIndex());
		paginationInfo.setRecordCountPerPage(statsVO.getPageUnit());
		paginationInfo.setPageSize(statsVO.getPageSize());
		statsVO.setFirstIndex(paginationInfo.getFirstRecordIndex());
		statsVO.setLastIndex(paginationInfo.getLastRecordIndex());
		statsVO.setRecordCountPerPage(paginationInfo.getRecordCountPerPage());
		
		if (statsVO.getFromDate() == null || statsVO.getToDate() == null){  //최초 접속
			SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat ( "yyyyMMdd", Locale.KOREA );
			Date currentTime = new Date ();
			statsVO.setFromDate(mSimpleDateFormat.format ( currentTime ));
			statsVO.setToDate(mSimpleDateFormat.format ( currentTime ));
			statsVO.setPdKind("D"); //일별조회
			statsVO.setStatsKind("PRSONAL"); //개인별
			statsVO.setDetailStatsKind("");
    	}
		if (statsVO.getFromDate() != null && !"".equals(statsVO.getFromDate())) {

			List<?> conectStats = conectStatsService.selectConectStats(statsVO);

			int totCnt = conectStatsService.selectConectStatsTotCnt(statsVO);
			paginationInfo.setTotalRecordCount(totCnt);
	        model.addAttribute("paginationInfo", paginationInfo);
	        
			// 1. 서비스별
			if ("SERVICE".equals(statsVO.getStatsKind())) {
				model.addAttribute("conectStats", conectStats);
				model.addAttribute("statsInfo", statsVO);
			// 2. 개인별
			} else {
				// 그래프에 표시될 이미지 길이를 결정한다.
				float iMaxUnit = 50.0f;
				for (int i = 0; i < conectStats.size(); i++) {
					StatsVO vo = (StatsVO)conectStats.get(i);
					int iCnt = vo.getStatsCo();
					if (iCnt > 10 && iCnt <= 100) {
						if (iMaxUnit > 5.0f) {
							iMaxUnit = 5.0f;
						}
					} else if (iCnt > 100 && iCnt <= 1000) {
						if (iMaxUnit > 0.5f) {
							iMaxUnit = 0.5f;
						}
					} else if (iCnt > 1000) {
						if (iMaxUnit > 0.05f) {
							iMaxUnit = 0.05f;
						}
					}
				}
				statsVO.setMaxUnit(iMaxUnit);
				model.addAttribute("conectStats", conectStats);
				model.addAttribute("statsInfo", statsVO);
			}
		}
        return "sts/cst/EgovConectStats";
	}
    
    
    /**
	 * 매뉴별 접속 통계를 조회한다
	 * @param statsVO StatsVO
	 * @return String
	 * @exception Exception
	 */
    @RequestMapping(value="/sts/cst/selectConectMenuStats.do")
	public String selectConectMenuStats(@ModelAttribute("statsVO") StatsVO statsVO,
			ModelMap model) throws Exception {
    	
    	statsVO.setPageUnit(propertyService.getInt("pageUnit"));
    	statsVO.setPageSize(propertyService.getInt("pageSize"));

		PaginationInfo paginationInfo = new PaginationInfo();
		paginationInfo.setCurrentPageNo(statsVO.getPageIndex());
		paginationInfo.setRecordCountPerPage(statsVO.getPageUnit());
		paginationInfo.setPageSize(statsVO.getPageSize());
		paginationInfo.setRecordCountPerPage(20);//목록 20개
		statsVO.setFirstIndex(paginationInfo.getFirstRecordIndex());
		statsVO.setLastIndex(paginationInfo.getLastRecordIndex());
		statsVO.setRecordCountPerPage(paginationInfo.getRecordCountPerPage());
		
		if (statsVO.getFromDate() == null || statsVO.getToDate() == null){  //최초 접속
			SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat ( "yyyyMMdd", Locale.KOREA );
			Date currentTime = new Date ();
			statsVO.setFromDate(mSimpleDateFormat.format ( currentTime ));
			statsVO.setToDate(mSimpleDateFormat.format ( currentTime ));
			String mTime = mSimpleDateFormat.format ( currentTime );
    	}
		
		if (statsVO.getFromDate() != null && !"".equals(statsVO.getFromDate())) {

			List<?> conectStats = conectStatsService.selectConectMenuStats(statsVO);

			int totCnt = conectStatsService.selectConectStatsMenuTotCnt(statsVO);
			paginationInfo.setTotalRecordCount(totCnt);
	        model.addAttribute("paginationInfo", paginationInfo);
	        
			// 1. 서비스별
			if ("SERVICE".equals(statsVO.getStatsKind())) {
				model.addAttribute("conectStats", conectStats);
				model.addAttribute("statsInfo", statsVO);
			// 2. 개인별
			} else {
				// 그래프에 표시될 이미지 길이를 결정한다.
				float iMaxUnit = 50.0f;
				for (int i = 0; i < conectStats.size(); i++) {
					StatsVO vo = (StatsVO)conectStats.get(i);
					int iCnt = vo.getStatsCo();
					if (iCnt > 10 && iCnt <= 100) {
						if (iMaxUnit > 5.0f) {
							iMaxUnit = 5.0f;
						}
					} else if (iCnt > 100 && iCnt <= 1000) {
						if (iMaxUnit > 0.5f) {
							iMaxUnit = 0.5f;
						}
					} else if (iCnt > 1000) {
						if (iMaxUnit > 0.05f) {
							iMaxUnit = 0.05f;
						}
					}
				}
				statsVO.setMaxUnit(iMaxUnit);
				model.addAttribute("conectStats", conectStats);
				model.addAttribute("statsInfo", statsVO);
			}
		}
        return "sts/cst/EgovConectMenuStats";
	}
}