package com.bb.api;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bb.jwt.JwtManager;
import com.bb.model.ApiResponse;
import com.bb.model.PageFormVO;
import com.bb.model.ReportSearch;
import com.bb.model.Site;
import com.bb.model.SiteSearch;
import com.bb.service.CreditService;
import com.bb.service.SiteService;
import com.bb.service.StatService;
import com.bb.util.DateTimeUtils;
import com.bb.util.IPKit;
import com.bb.util.PagingUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequiredArgsConstructor
@RequestMapping(value = "/api/v1/stat")
public class ApiStatController {

	@Autowired
	StatService statService;
	
	@Autowired
	SiteService siteService;
	
	@Autowired
	CreditService creditService;
	
	@Autowired
	private final JwtManager jwtManager;
	
	
	@ResponseBody	
	@PostMapping("/error")
    public ApiResponse error(@RequestHeader String token, HttpServletRequest request, @RequestBody SiteSearch search) throws Exception {
		ApiResponse apiResponse = new ApiResponse();
		
		try {
			Site site = siteService.getSiteInfoAPI(request);
			if(site == null) {
				apiResponse.setResultCode("9999");
				apiResponse.setResultMessage("Authorization apiKey check");
				return apiResponse;
			}
			
			if( !( site.getSiteIp()==null || "".equals(site.getSiteIp())
					|| "3.3.3.3".equals(site.getSiteIp()) || site.getSiteIp().indexOf(IPKit.getIpAddressByRequest(request)) >= 0) ) {
				log.error("site.getSiteIp("+site.getSiteIp()+"), denied" + IPKit.getIpAddressByRequest(request));
				apiResponse.setResultCode("10001");
				return apiResponse;
			}
			
			// TO DO 토큰체크 
			Map data = new HashMap();
			
			try {
				JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfoAdmin(token);
				final String LOG_PREFIX = "#-API::STAT::statMain::"+tokenInfo.getSid()+"::::";
				
				// POJO to JSON
				ObjectMapper objectMapper = new ObjectMapper();
				String reqJsonStr = objectMapper.writeValueAsString(search);
				JSONObject reqObj = new JSONObject(reqJsonStr);
				log.info(LOG_PREFIX+ "Request {}", reqObj);
		    	
				String nowtime = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));	
		    	if(search.getStartDate() == null || "".equals(search.getStartDate())) {
		    		search.setStartDate(nowtime);
		    	}
		    	
		    	if(search.getEndDate() == null || "".equals(search.getEndDate())) {
		    		search.setEndDate(nowtime);
		    	}
		    	
		    	PageFormVO pageVo= new PageFormVO();	    	
		    	search.setSiteId(site.getSiteId());
		    	search.setSiteIdx(site.getSiteIdx());
		    	
		    	log.info(LOG_PREFIX+ "SiteSearch {}", search);
				int totalCount = siteService.getSiteCallbackErrLogListCnt(search);
				if (totalCount != 0) {
					PageFormVO commonForm = new PageFormVO();
					commonForm.setFunction_name("goPage");
					commonForm.setPage(search.getPage());
					commonForm.setCount_per_page(20);
					if(search.getCount_per_list()==0) {
						commonForm.setCount_per_list(10);
					} else {
						commonForm.setCount_per_list(search.getCount_per_list());
					}
					commonForm.setTatal_list_count(totalCount);
					pageVo = PagingUtil.setPageUtil(commonForm);
					search.setLimit(pageVo.getLimit());
					search.setOffset(pageVo.getOffset());
					search.setTatal_list_count(totalCount);
					
					data.put("pageInfo", pageVo);
				}
				
				log.info(LOG_PREFIX+ "SiteSearch {}", search);
				List<HashMap> errorList = siteService.getSiteCallbackErrLogList(search);
				data.put("list", errorList);
				
			} catch(Exception e) {
				log.info(e.toString());
				apiResponse.setResultCode("99995");
				apiResponse.setResultMessage("token is no valid");
				return apiResponse;
			}
			
			apiResponse.setData(data);
			apiResponse.success();
			
		} catch (Exception e) {
			e.printStackTrace();
			apiResponse.fail();
		}
		
		return apiResponse;
	}
	
	
	
	@ResponseBody	
	@PostMapping("/credit")
    public ApiResponse credit(@RequestHeader String token, HttpServletRequest request, @RequestBody SiteSearch search) throws Exception {
		ApiResponse apiResponse = new ApiResponse();
		
		try {
			Site site = siteService.getSiteInfoAPI(request);
			if(site == null) {
				apiResponse.setResultCode("9999");
				apiResponse.setResultMessage("Authorization apiKey check");
				return apiResponse;
			}
			
			if( !( site.getSiteIp()==null || "".equals(site.getSiteIp())
					|| "3.3.3.3".equals(site.getSiteIp()) || site.getSiteIp().indexOf(IPKit.getIpAddressByRequest(request)) >= 0) ) {
				log.error("site.getSiteIp("+site.getSiteIp()+"), denied" + IPKit.getIpAddressByRequest(request));
				apiResponse.setResultCode("10001");
				return apiResponse;
			}
			
			// TO DO 토큰체크 
			Map data = new HashMap();
		
			try {
				JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfoAdmin(token);
				final String LOG_PREFIX = "#-API::STAT::statMain::credit::"+tokenInfo.getSid()+"::::";
				
				// POJO to JSON
				ObjectMapper objectMapper = new ObjectMapper();
				String reqJsonStr = objectMapper.writeValueAsString(search);
				JSONObject reqObj = new JSONObject(reqJsonStr);
				log.info(LOG_PREFIX+ "Request {}", reqObj);
		    	
				String nowtime = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));	
		    	if(search.getStartDate() == null || "".equals(search.getStartDate())) {
		    		search.setStartDate(nowtime);
		    	}
		    	
		    	if(search.getEndDate() == null || "".equals(search.getEndDate())) {
		    		search.setEndDate(nowtime);
		    	}
		    	
		    	log.info(LOG_PREFIX+ "BF::SiteSearch {}", search);
		    	
		    	PageFormVO pageVo= new PageFormVO();	    	
		    	
		    	if(search.getSearchSiteId() == null || "".equals(search.getSearchSiteId())) {
		    		search.setSiteIdx(site.getSiteIdx());
		    		search.setSiteId(site.getSiteId());
		    	} else {
		    		Site tergetSite = new Site();
		    		tergetSite.setSiteId(search.getSearchSiteId());
		    		tergetSite = siteService.getSite(tergetSite);
		    		search.setSiteIdx(tergetSite.getSiteIdx());
		    		search.setSiteId(search.getSearchSiteId());
		    		log.info(LOG_PREFIX+ "tergetSite {}", tergetSite);
		    	}
		    	
		    	log.info(LOG_PREFIX+ "AF::SiteSearch {}", search);
		    	

				int totalCount = creditService.getStatCreditListCnt(search);
				if (totalCount != 0) {
					PageFormVO commonForm = new PageFormVO();
					commonForm.setFunction_name("goPage");
					commonForm.setPage(search.getPage());
					commonForm.setCount_per_page(20);
					if(search.getCount_per_list()==0) {
						commonForm.setCount_per_list(10);
					} else {
						commonForm.setCount_per_list(search.getCount_per_list());
					}
					commonForm.setTatal_list_count(totalCount);
					pageVo = PagingUtil.setPageUtil(commonForm);
					search.setLimit(pageVo.getLimit());
					search.setOffset(pageVo.getOffset());
					search.setTatal_list_count(totalCount);
					
					data.put("pageInfo", pageVo);
				}
				
				List<HashMap> creditList = creditService.getStatCreditList(search);
				/*
				List<HashMap> adminBotttomList = creditService.getStatCreditAdminBotttomList(search);
				long adminBottomInSum = 0;
				long adminBottomOutSum = 0;
				for(HashMap item : creditList) {
					long tempIn = 0;
					long tmpOut = 0;
					String aDate = item.get("date").toString();
					for(HashMap botItem : adminBotttomList) {
						String bDate = botItem.get("date").toString();
						if(aDate.equals(bDate)) {
							item.put("adminBottomIn", botItem.get("adminBottomIn").toString());
							item.put("adminBottomOut", botItem.get("adminBottomOut").toString());
							tempIn = Long.parseLong(botItem.get("adminBottomIn").toString());
							tmpOut = Long.parseLong(botItem.get("adminBottomOut").toString());
						}
					}
					adminBottomInSum = adminBottomInSum + tempIn;
					adminBottomOutSum = adminBottomOutSum + tmpOut;
				}
				*/
				HashMap creditTotal = creditService.getStatCreditTotal(search);
				/*
				creditTotal.put("adminBottomInSum", Long.toString(adminBottomInSum));
				creditTotal.put("adminBottomOutSum", Long.toString(adminBottomOutSum));
				*/
				data.put("list", creditList);
				data.put("sum", creditTotal);
				
			} catch(Exception e) {
				log.info(e.toString());
				apiResponse.setResultCode("99995");
				apiResponse.setResultMessage("token is no valid");
				return apiResponse;
			}
			
			apiResponse.setData(data);
			apiResponse.success();
			
		} catch (Exception e) {
			e.printStackTrace();
			apiResponse.fail();
		}
		
		return apiResponse;
	}
	
	
	
	@ResponseBody	
	@PostMapping("/report")
    public ApiResponse report(@RequestHeader String token, HttpServletRequest request, @RequestBody SiteSearch search) throws Exception {
		ApiResponse apiResponse = new ApiResponse();
		
		try {
			Site site = siteService.getSiteInfoAPI(request);
			if(site == null) {
				apiResponse.setResultCode("9999");
				apiResponse.setResultMessage("Authorization apiKey check");
				return apiResponse;
			}
			
			if( !( site.getSiteIp()==null || "".equals(site.getSiteIp())
					|| "3.3.3.3".equals(site.getSiteIp()) || site.getSiteIp().indexOf(IPKit.getIpAddressByRequest(request)) >= 0) ) {
				log.error("site.getSiteIp("+site.getSiteIp()+"), denied" + IPKit.getIpAddressByRequest(request));
				apiResponse.setResultCode("10001");
				return apiResponse;
			}
			
			// TO DO 토큰체크 
			Map data = new HashMap();
			
			try {
				JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfoAdmin(token);
				final String LOG_PREFIX = "#-API::STAT::statMain::report::"+tokenInfo.getSid()+"::::";
				
				// POJO to JSON
				ObjectMapper objectMapper = new ObjectMapper();
				String reqJsonStr = objectMapper.writeValueAsString(search);
				JSONObject reqObj = new JSONObject(reqJsonStr);
				log.info(LOG_PREFIX+ "Request {}", reqObj);
		    	
				search.setSiteIdx(site.getSiteIdx());
				if(search.getSearchSiteId()==null || "".equals(search.getSearchSiteId())) {
					search.setSiteId(site.getSiteId());
				} else {
					search.setSiteId(search.getSearchSiteId());
				}
				
				if(search.getWithBottomYN() == null || !"Y".equals(search.getWithBottomYN())) {
					search.setWithBottomYN("N");
				}
		    	
		    	log.info(LOG_PREFIX+ "SiteSearch::"+search.toString());
		    	
		    	String startDay = search.getStartDate().substring(0, 10);
		    	String endDay = search.getEndDate().substring(0, 10);
		    	String toDay = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));
		    	
		    	log.info(LOG_PREFIX+ "startDay::"+startDay);
		    	log.info(LOG_PREFIX+ "endDay::"+endDay);
		    	log.info(LOG_PREFIX+ "toDay::"+toDay);
		    	
		    	if(startDay.equals(endDay) && startDay.equals(toDay)) {
		    		// 오늘날짜 한정 특별 검색
		    		List<HashMap> list = statService.getSiteReportListToDay(search);
					data.put("list", list);
					
					List<HashMap> listSum = statService.getSiteReportListSumToDay(search);
					data.put("sum",listSum);
		    	} else {
		    		if(search.getSearchMemId()==null || "".equals(search.getSearchMemId())) {
						List<HashMap> list = statService.getSiteReportList(search);
						data.put("list", list);
						
						List<HashMap> listSum = statService.getSiteReportListSum(search);
						data.put("sum",listSum);
			    	} else {
						List<HashMap> list = statService.getSiteReportMemList(search);
						data.put("list", list);
						
						List<HashMap> listSum = statService.getSiteReportMemListSum(search);
						data.put("sum",listSum);
			    	}
		    	}
				
			} catch(Exception e) {
				log.info(e.toString());
				apiResponse.setResultCode("99995");
				apiResponse.setResultMessage("token is no valid");
				return apiResponse;
			}
			
			apiResponse.setData(data);
			apiResponse.success();
			
		} catch (Exception e) {
			e.printStackTrace();
			apiResponse.fail();
		}
		
		return apiResponse;
	}
	
	
	
	@ResponseBody	
	@PostMapping("")
    public ApiResponse statMain(@RequestHeader String token, HttpServletRequest request, @RequestBody SiteSearch search) throws Exception {
		ApiResponse apiResponse = new ApiResponse();
		
		try {
			Site site = siteService.getSiteInfoAPI(request);
			if(site == null) {
				apiResponse.setResultCode("9999");
				apiResponse.setResultMessage("Authorization apiKey check");
				return apiResponse;
			}
			
			if( !( site.getSiteIp()==null || "".equals(site.getSiteIp())
					|| "3.3.3.3".equals(site.getSiteIp()) || site.getSiteIp().indexOf(IPKit.getIpAddressByRequest(request)) >= 0) ) {
				log.error("site.getSiteIp("+site.getSiteIp()+"), denied" + IPKit.getIpAddressByRequest(request));
				apiResponse.setResultCode("10001");
				return apiResponse;
			}
			
			// TO DO 토큰체크 
			Map data = new HashMap();
			
			try {
				JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfoAdmin(token);
				final String LOG_PREFIX = "#-API::STAT::statMain::"+tokenInfo.getSid()+"::::";
				
				// POJO to JSON
				ObjectMapper objectMapper = new ObjectMapper();
				String reqJsonStr = objectMapper.writeValueAsString(search);
				JSONObject reqObj = new JSONObject(reqJsonStr);
				log.info(LOG_PREFIX+ "Request {}", reqObj);
		    	
		    	search.setSiteIdx(site.getSiteIdx());
		    	log.info(LOG_PREFIX+ "SiteSearch::"+search.toString());
				
		    	String prev = statService.getlastMonth(search);
				data.put("prev", prev);
			
				List<HashMap> list = statService.getSiteDailyReportList(search);
				data.put("list", list);
				
				
			} catch(Exception e) {
				log.info(e.toString());
				apiResponse.setResultCode("99995");
				apiResponse.setResultMessage("token is no valid");
				return apiResponse;
			}
			
			apiResponse.setData(data);
			apiResponse.success();
			
		} catch (Exception e) {
			e.printStackTrace();
			apiResponse.fail();
		}
		
		return apiResponse;
	}
	
	
	@ResponseBody	
	@PostMapping("/bottom")
    public ApiResponse bottom(@RequestHeader String token, HttpServletRequest request, @RequestBody SiteSearch search) throws Exception {
		ApiResponse apiResponse = new ApiResponse();
		
		try {
			Site site = siteService.getSiteInfoAPI(request);
			if(site == null) {
				apiResponse.setResultCode("9999");
				apiResponse.setResultMessage("Authorization apiKey check");
				return apiResponse;
			}
			
			if( !( site.getSiteIp()==null || "".equals(site.getSiteIp())
					|| "3.3.3.3".equals(site.getSiteIp()) || site.getSiteIp().indexOf(IPKit.getIpAddressByRequest(request)) >= 0) ) {
				log.error("site.getSiteIp("+site.getSiteIp()+"), denied" + IPKit.getIpAddressByRequest(request));
				apiResponse.setResultCode("10001");
				return apiResponse;
			}
			
			// TO DO 토큰체크 
			Map data = new HashMap();
			
			try {
				JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfoAdmin(token);
				final String LOG_PREFIX = "#-API::STAT::bottom::"+tokenInfo.getSid()+"::::";
				
				// POJO to JSON
				ObjectMapper objectMapper = new ObjectMapper();
				String reqJsonStr = objectMapper.writeValueAsString(search);
				JSONObject reqObj = new JSONObject(reqJsonStr);
				log.info(LOG_PREFIX+ "Request {}", reqObj);
		    	
		    	search.setUpperSiteIdx(site.getSiteIdx());
		    	log.info(LOG_PREFIX+ "SiteSearch::"+search.toString());
				
		    	String prev = statService.getlastMonth(search);
				data.put("prev", prev);
			
				List<HashMap> list = statService.getBottomSiteDailyReportList(search);
				data.put("list", list);
				
				
			} catch(Exception e) {
				log.info(e.toString());
				apiResponse.setResultCode("99995");
				apiResponse.setResultMessage("token is no valid");
				return apiResponse;
			}
			
			apiResponse.setData(data);
			apiResponse.success();
			
		} catch (Exception e) {
			e.printStackTrace();
			apiResponse.fail();
		}
		
		return apiResponse;
	}
	
	
	@ResponseBody	
	@PostMapping("/pot")
    public ApiResponse mypage(@RequestHeader String token, HttpServletRequest request, @RequestBody SiteSearch search) throws Exception {
		ApiResponse apiResponse = new ApiResponse();
		
		try {
			Site site = siteService.getSiteInfoAPI(request);
			if(site == null) {
				apiResponse.setResultCode("9999");
				apiResponse.setResultMessage("Authorization apiKey check");
				return apiResponse;
			}
			
			if( !( site.getSiteIp()==null || "".equals(site.getSiteIp())
					|| "3.3.3.3".equals(site.getSiteIp()) || site.getSiteIp().indexOf(IPKit.getIpAddressByRequest(request)) >= 0) ) {
				log.error("site.getSiteIp("+site.getSiteIp()+"), denied" + IPKit.getIpAddressByRequest(request));
				apiResponse.setResultCode("10001");
				return apiResponse;
			}
			
			// TO DO 토큰체크 
			Map data = new HashMap();
			
			try {
				JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfoAdmin(token);
				final String LOG_PREFIX = "#-API::STAT::pot::"+tokenInfo.getSid()+"::::";
				
				// POJO to JSON
				ObjectMapper objectMapper = new ObjectMapper();
				String reqJsonStr = objectMapper.writeValueAsString(search);
				JSONObject reqObj = new JSONObject(reqJsonStr);
				log.info(LOG_PREFIX+ "Request {}", reqObj);
		    	
				String currDate = DateTimeUtils.getYyyymmdd();
		    	String yyyy = currDate.substring(0, 4);
		    	search.setSiteIdx(site.getSiteIdx());
		    	int mm = Integer.parseInt(currDate.substring(4, 6));
		    	
		    	if(search.getStartDate() == null) {
		    		String mmm = currDate.substring(4, 6);
		    		data.put("yyyy", yyyy);
		    		data.put("mmm", mmm);
		    		search.setStartDate(mmm);
		    	} else {
		    		String mmm = search.getStartDate();
		    		data.put("yyyy", yyyy);
		    		data.put("mmm", mmm);
		    		search.setStartDate(mmm);
		    	}
		    	
		    	log.info(LOG_PREFIX+ "SiteSearch::"+search.toString());
				
				data.put("mm", mm);
			
				HashMap list = statService.getMonthCredit(search);
				data.put("list", list);
				
				
			} catch(Exception e) {
				log.info(e.toString());
				apiResponse.setResultCode("99995");
				apiResponse.setResultMessage("token is no valid");
				return apiResponse;
			}
			
			apiResponse.setData(data);
			apiResponse.success();
			
		} catch (Exception e) {
			e.printStackTrace();
			apiResponse.fail();
		}
		
		return apiResponse;
	}
	
	
	@ResponseBody	
	@PostMapping("/casino")
    public ApiResponse statCasino(@RequestHeader String token, HttpServletRequest request, @RequestBody SiteSearch search) throws Exception {
		ApiResponse apiResponse = new ApiResponse();
		
		try {
			Site site = siteService.getSiteInfoAPI(request);
			if(site == null) {
				apiResponse.setResultCode("9999");
				apiResponse.setResultMessage("Authorization apiKey check");
				return apiResponse;
			}
			
			if( !( site.getSiteIp()==null || "".equals(site.getSiteIp())
					|| "3.3.3.3".equals(site.getSiteIp()) || site.getSiteIp().indexOf(IPKit.getIpAddressByRequest(request)) >= 0) ) {
				log.error("site.getSiteIp("+site.getSiteIp()+"), denied" + IPKit.getIpAddressByRequest(request));
				apiResponse.setResultCode("10001");
				return apiResponse;
			}
			
			// TO DO 토큰체크 
			Map data = new HashMap();
			
			try {
				JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfoAdmin(token);
				final String LOG_PREFIX = "#-API::STAT::pot::"+tokenInfo.getSid()+"::::";
				
				// POJO to JSON
				ObjectMapper objectMapper = new ObjectMapper();
				String reqJsonStr = objectMapper.writeValueAsString(search);
				JSONObject reqObj = new JSONObject(reqJsonStr);
				log.info(LOG_PREFIX+ "Request {}", reqObj);
		    	
				// 여기서 부터 로직
				String nowtime = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));	
		    	if(search.getStartDate() == null || "".equals(search.getStartDate())) {
		    		search.setStartDate(nowtime);
		    	}
		    	if(search.getEndDate() == null || "".equals(search.getEndDate())) {
		    		search.setEndDate(nowtime);
		    	}
		    	
		    	search.setSiteIdx(site.getSiteIdx());
		    	log.info(LOG_PREFIX+ "SiteSearch::"+search.toString());
				
		    	List<HashMap> list = statService.getVendorDailyReportList(search);
				data.put("list", list);
				
			} catch(Exception e) {
				log.info(e.toString());
				apiResponse.setResultCode("99995");
				apiResponse.setResultMessage("token is no valid");
				return apiResponse;
			}
			
			apiResponse.setData(data);
			apiResponse.success();
			
		} catch (Exception e) {
			e.printStackTrace();
			apiResponse.fail();
		}
		
		return apiResponse;
	}
	
	
	@ResponseBody	
	@PostMapping("/user")
    public ApiResponse statUser(@RequestHeader String token, HttpServletRequest request, @RequestBody SiteSearch search) throws Exception {
		ApiResponse apiResponse = new ApiResponse();
		
		try {
			Site site = siteService.getSiteInfoAPI(request);
			if(site == null) {
				apiResponse.setResultCode("9999");
				apiResponse.setResultMessage("Authorization apiKey check");
				return apiResponse;
			}
			
			if( !( site.getSiteIp()==null || "".equals(site.getSiteIp())
					|| "3.3.3.3".equals(site.getSiteIp()) || site.getSiteIp().indexOf(IPKit.getIpAddressByRequest(request)) >= 0) ) {
				log.error("site.getSiteIp("+site.getSiteIp()+"), denied" + IPKit.getIpAddressByRequest(request));
				apiResponse.setResultCode("10001");
				return apiResponse;
			}
			
			// TO DO 토큰체크 
			Map data = new HashMap();
			
			try {
				JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfoAdmin(token);
				final String LOG_PREFIX = "#-API::STAT::pot::"+tokenInfo.getSid()+"::::";
				
				// POJO to JSON
				ObjectMapper objectMapper = new ObjectMapper();
				String reqJsonStr = objectMapper.writeValueAsString(search);
				JSONObject reqObj = new JSONObject(reqJsonStr);
				log.info(LOG_PREFIX+ "Request {}", reqObj);
		    	
				// 여기서 부터 로직
				if(search.getStartDate() == null || "".equals(search.getStartDate())) {	
					String currDate = DateTimeUtils.getYyyymm();
					search.setStartDate(currDate.substring(0, 4));
					search.setEndDate(currDate.substring(4, 6));
				}
		    	
		    	search.setSiteIdx(site.getSiteIdx());
		    	log.info(LOG_PREFIX+ "SiteSearch::"+search.toString());
				
		    	List<HashMap> list = statService.getUserDailyReportList(search);
				data.put("list", list);
				
			} catch(Exception e) {
				log.info(e.toString());
				apiResponse.setResultCode("99995");
				apiResponse.setResultMessage("token is no valid");
				return apiResponse;
			}
			
			apiResponse.setData(data);
			apiResponse.success();
			
		} catch (Exception e) {
			e.printStackTrace();
			apiResponse.fail();
		}
		
		return apiResponse;
	}
	
	
	@ResponseBody	
	@PostMapping("/date")
    public ApiResponse statDate(@RequestHeader String token, HttpServletRequest request, @RequestBody SiteSearch search) throws Exception {
		ApiResponse apiResponse = new ApiResponse();
		
		try {
			Site site = siteService.getSiteInfoAPI(request);
			if(site == null) {
				apiResponse.setResultCode("9999");
				apiResponse.setResultMessage("Authorization apiKey check");
				return apiResponse;
			}
			
			if( !( site.getSiteIp()==null || "".equals(site.getSiteIp())
					|| "3.3.3.3".equals(site.getSiteIp()) || site.getSiteIp().indexOf(IPKit.getIpAddressByRequest(request)) >= 0) ) {
				log.error("site.getSiteIp("+site.getSiteIp()+"), denied" + IPKit.getIpAddressByRequest(request));
				apiResponse.setResultCode("10001");
				return apiResponse;
			}
			
			// TO DO 토큰체크 
			Map data = new HashMap();
			
			try {
				JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfoAdmin(token);
				final String LOG_PREFIX = "#-API::STAT::pot::"+tokenInfo.getSid()+"::::";
				
				// POJO to JSON
				ObjectMapper objectMapper = new ObjectMapper();
				String reqJsonStr = objectMapper.writeValueAsString(search);
				JSONObject reqObj = new JSONObject(reqJsonStr);
				log.info(LOG_PREFIX+ "Request {}", reqObj);
		    	
				// 여기서 부터 로직
				String nowtime = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));	
		    	if(search.getStartDate() == null || "".equals(search.getStartDate())) {
		    		search.setStartDate(nowtime);
		    	}
		    	if(search.getEndDate() == null || "".equals(search.getEndDate())) {
		    		search.setEndDate(nowtime);
		    	}
		    	
		    	search.setSiteIdx(site.getSiteIdx());
		    	log.info(LOG_PREFIX+ "SiteSearch::"+search.toString());
				
		    	List<HashMap> list = statService.getUserReportSum(search);
				data.put("list", list);
				
			} catch(Exception e) {
				log.info(e.toString());
				apiResponse.setResultCode("99995");
				apiResponse.setResultMessage("token is no valid");
				return apiResponse;
			}
			
			apiResponse.setData(data);
			apiResponse.success();
			
		} catch (Exception e) {
			e.printStackTrace();
			apiResponse.fail();
		}
		
		return apiResponse;
	}
	
	
	@ResponseBody	
	@PostMapping("/reportday")
    public ApiResponse reportDay(@RequestHeader String token, HttpServletRequest request, @RequestBody SiteSearch search) throws Exception {
		ApiResponse apiResponse = new ApiResponse();
		
		try {
			Site site = siteService.getSiteInfoAPI(request);
			if(site == null) {
				apiResponse.setResultCode("9999");
				apiResponse.setResultMessage("Authorization apiKey check");
				return apiResponse;
			}
			
			if( !( site.getSiteIp()==null || "".equals(site.getSiteIp())
					|| "3.3.3.3".equals(site.getSiteIp()) || site.getSiteIp().indexOf(IPKit.getIpAddressByRequest(request)) >= 0) ) {
				log.error("site.getSiteIp("+site.getSiteIp()+"), denied" + IPKit.getIpAddressByRequest(request));
				apiResponse.setResultCode("10001");
				return apiResponse;
			}
			
			// TO DO 토큰체크 
			Map data = new HashMap();
			
			try {
				JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfoAdmin(token);
				final String LOG_PREFIX = "#-API::STAT::pot::"+tokenInfo.getSid()+"::::";
				
				// POJO to JSON
				ObjectMapper objectMapper = new ObjectMapper();
				String reqJsonStr = objectMapper.writeValueAsString(search);
				JSONObject reqObj = new JSONObject(reqJsonStr);
				log.info(LOG_PREFIX+ "Request {}", reqObj);
		    	
				// 여기서 부터 로직
				String nowmonth = new SimpleDateFormat("yyyy-MM").format(new Date(System.currentTimeMillis()));	
				String nowtime = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));	
		    	if(search.getStartDate() == null || "".equals(search.getStartDate())) {
		    		search.setStartDate(nowmonth+"-01");
		    	}
		    	if(search.getEndDate() == null || "".equals(search.getEndDate())) {
		    		search.setEndDate(nowtime);
		    	}
		    	
		    	search.setSiteIdx(site.getSiteIdx());
		    	log.info(LOG_PREFIX+ "SiteSearch::"+search.toString());
				
		    	List<HashMap> list = statService.getVendorDailyReportListDay(search);
		    	List<HashMap> listSUM = statService.getVendorDailyReportListDaySUM(search);
				data.put("list", list);
				data.put("listSUM", listSUM);
				
			} catch(Exception e) {
				log.info(e.toString());
				apiResponse.setResultCode("99995");
				apiResponse.setResultMessage("token is no valid");
				return apiResponse;
			}
			
			apiResponse.setData(data);
			apiResponse.success();
			
		} catch (Exception e) {
			e.printStackTrace();
			apiResponse.fail();
		}
		
		return apiResponse;
	}
	
	
	@ResponseBody	
	@PostMapping("/reportmonth")
    public ApiResponse reportMonth(@RequestHeader String token, HttpServletRequest request, @RequestBody SiteSearch search) throws Exception {
		ApiResponse apiResponse = new ApiResponse();
		
		try {
			Site site = siteService.getSiteInfoAPI(request);
			if(site == null) {
				apiResponse.setResultCode("9999");
				apiResponse.setResultMessage("Authorization apiKey check");
				return apiResponse;
			}
			
			if( !( site.getSiteIp()==null || "".equals(site.getSiteIp())
					|| "3.3.3.3".equals(site.getSiteIp()) || site.getSiteIp().indexOf(IPKit.getIpAddressByRequest(request)) >= 0) ) {
				log.error("site.getSiteIp("+site.getSiteIp()+"), denied" + IPKit.getIpAddressByRequest(request));
				apiResponse.setResultCode("10001");
				return apiResponse;
			}
			
			// TO DO 토큰체크 
			Map data = new HashMap();
			
			try {
				JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfoAdmin(token);
				final String LOG_PREFIX = "#-API::STAT::pot::"+tokenInfo.getSid()+"::::";
				
				// POJO to JSON
				ObjectMapper objectMapper = new ObjectMapper();
				String reqJsonStr = objectMapper.writeValueAsString(search);
				JSONObject reqObj = new JSONObject(reqJsonStr);
				log.info(LOG_PREFIX+ "Request {}", reqObj);
		    	
				// 여기서 부터 로직
				String nowmonth = new SimpleDateFormat("yyyy-MM").format(new Date(System.currentTimeMillis()));	
				String nowtime = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));	
				if(search.getYear() == null || "".equals(search.getYear())) {
					search.setStartDate(nowmonth +"-01");
					
					String nowmonthArr[] =  nowmonth.split("-");
					search.setYear(nowmonthArr[0]);
					search.setMonth(nowmonthArr[1]);
				} else {
					search.setStartDate(search.getYear() +"-"+ search.getMonth() +"-01");
				}
		    	
		    	search.setSiteIdx(site.getSiteIdx());
		    	log.info(LOG_PREFIX+ "SiteSearch::"+search.toString());
				
		    	List<HashMap> list = statService.getVendorDailyReportListMonth(search);
		    	List<HashMap> listSUM = statService.getVendorDailyReportListMonthSUM(search);
				data.put("list", list);
				data.put("listSUM", listSUM);
				
			} catch(Exception e) {
				log.info(e.toString());
				apiResponse.setResultCode("99995");
				apiResponse.setResultMessage("token is no valid");
				return apiResponse;
			}
			
			apiResponse.setData(data);
			apiResponse.success();
			
		} catch (Exception e) {
			e.printStackTrace();
			apiResponse.fail();
		}
		
		return apiResponse;
	}
	
	
	@ResponseBody	
	@PostMapping("/std")
    public ApiResponse std(@RequestHeader String token, HttpServletRequest request, @RequestBody SiteSearch search) throws Exception {
		ApiResponse apiResponse = new ApiResponse();
		
		try {
			Site site = siteService.getSiteInfoAPI(request);
			if(site == null) {
				apiResponse.setResultCode("9999");
				apiResponse.setResultMessage("Authorization apiKey check");
				return apiResponse;
			}
			
			if( !( site.getSiteIp()==null || "".equals(site.getSiteIp())
					|| "3.3.3.3".equals(site.getSiteIp()) || site.getSiteIp().indexOf(IPKit.getIpAddressByRequest(request)) >= 0) ) {
				log.error("site.getSiteIp("+site.getSiteIp()+"), denied" + IPKit.getIpAddressByRequest(request));
				apiResponse.setResultCode("10001");
				return apiResponse;
			}
			
			// TO DO 토큰체크 
			Map data = new HashMap();
			
			try {
				JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfoAdmin(token);
				final String LOG_PREFIX = "#-API::STAT::pot::"+tokenInfo.getSid()+"::::";
				
				// POJO to JSON
				ObjectMapper objectMapper = new ObjectMapper();
				String reqJsonStr = objectMapper.writeValueAsString(search);
				JSONObject reqObj = new JSONObject(reqJsonStr);
				log.info(LOG_PREFIX+ "Request {}", reqObj);
		    	
				// 여기서 부터 로직
				String nowtime = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));	
		    	if(search.getStartDate() == null || "".equals(search.getStartDate())) {
		    		search.setStartDate(nowtime);
		    	}
		    	if(search.getEndDate() == null || "".equals(search.getEndDate())) {
		    		search.setEndDate(nowtime);
		    	}
		    	
		    	search.setSiteIdx(site.getSiteIdx());
		    	log.info(LOG_PREFIX+ "SiteSearch::"+search.toString());
				
		    	List<HashMap> list = statService.getUserReportSum(search);
				data.put("list", list);
				
			} catch(Exception e) {
				log.info(e.toString());
				apiResponse.setResultCode("99995");
				apiResponse.setResultMessage("token is no valid");
				return apiResponse;
			}
			
			apiResponse.setData(data);
			apiResponse.success();
			
		} catch (Exception e) {
			e.printStackTrace();
			apiResponse.fail();
		}
		
		return apiResponse;
	}
	
	
	@ResponseBody	
	@PostMapping("/hybridReport")
    public ApiResponse hybridReport(@RequestHeader String token, HttpServletRequest request, @Valid @RequestBody ReportSearch search) throws Exception {
		ApiResponse apiResponse = new ApiResponse();
		
		try {
			Site site = siteService.getSiteInfoAPI(request);
			if(site == null) {
				apiResponse.setResultCode("9999");
				apiResponse.setResultMessage("Authorization apiKey check");
				return apiResponse;
			}
			
			if( !( site.getSiteIp()==null || "".equals(site.getSiteIp())
					|| "3.3.3.3".equals(site.getSiteIp()) || site.getSiteIp().indexOf(IPKit.getIpAddressByRequest(request)) >= 0) ) {
				log.error("site.getSiteIp("+site.getSiteIp()+"), denied" + IPKit.getIpAddressByRequest(request));
				apiResponse.setResultCode("10001");
				return apiResponse;
			}
			
			// TO DO 토큰체크 
			Map data = new HashMap();
			
			JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfoAdmin(token);
			final String LOG_PREFIX = "#-API::STAT::hybridReport::"+tokenInfo.getSid()+"::::";
			
			try {
				String toDay = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));
				String startDay = search.getStartDate().substring(0, 10);
		    	String endDay = search.getEndDate().substring(0, 10);
				
		    	search.setIsToday("N");
				if(startDay.equals(toDay) || endDay.equals(toDay)) {
					search.setIsToday("Y");
				}
				
				ObjectMapper objectMapper = new ObjectMapper();
				String reqJsonStr = objectMapper.writeValueAsString(search);
				JSONObject reqObj = new JSONObject(reqJsonStr);
				log.info(LOG_PREFIX+ "Request {}", reqObj);
				
				if(search.getWithBottomYN().equals("Y")) {
					HashMap report = statService.getHybridReport(search);
					HashMap reportTotal = statService.getHybridReportTotal(search);
					//List<HashMap> list = statService.getHybridReportList(search);
					data.put("hReport", report);
					data.put("hReportTotal", reportTotal);
					data.put("hReportList", null);
				} else {
					HashMap report = statService.getHybridReport(search);
					data.put("hReport", report);
					data.put("hReportTotal", null);
					data.put("hReportList", null);
				}
				
			} catch(Exception e) {
				log.error(LOG_PREFIX+ "EXCEPTION::"+e.toString());
				apiResponse.setResultCode("99995");
				apiResponse.setResultMessage("token is no valid");
				return apiResponse;
			}
			
			apiResponse.setData(data);
			apiResponse.success();
			
		} catch (Exception e) {
			e.printStackTrace();
			apiResponse.fail();
		}
		
		return apiResponse;
	}
	
}
