package kr.co.uplus.ez.api.stats;

import kr.co.uplus.ez.api.stats.dto.*;
import kr.co.uplus.ez.common.data.ApiResponseCode;
import kr.co.uplus.ez.common.data.ApiResponseMessage;
import kr.co.uplus.ez.common.data.Paging;
import kr.co.uplus.ez.common.utils.DateUtils;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StatsService {

	@Autowired
	@Qualifier("sqlSessionTemplateDb1")
	private SqlSessionTemplate sqlSessionMaster;

	@Autowired
	@Qualifier("sqlSessionTemplateDb2")
	private SqlSessionTemplate sqlSessionSlave;

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 월별 통계 목록 조회
	 * @param monthListReqDto
	 * @return
	 */
	public MonthListResDto monthList(MonthListReqDto monthListReqDto) {
		StatsMapper statsMapper = sqlSessionSlave.getMapper(StatsMapper.class);

		//오늘 날짜 조회인지 확인
		if(DateUtils.date2strYM().equals(monthListReqDto.getEndMon())) {
			monthListReqDto.setNowDiv("Y");
		}
		
		List<MonthList> monthLists =  statsMapper.selectMonStatList(monthListReqDto);

		MonthListRes monthListRes = new MonthListRes();
		monthListRes.setList(monthLists);

		return new MonthListResDto(ApiResponseCode.SUCCESS, monthListRes);
	}

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 월별 통계 목록 엑셀 다운로드
	 * @param paramMap
	 * @return
	 */
	@Deprecated
	public ApiResponseMessage monthListExcel(Map<String, Object> paramMap) {
		ApiResponseMessage result = new ApiResponseMessage(ApiResponseCode.SUCCESS);
		Map<String, Object> dataObj = new HashMap<>();
		List<Map<String,Object>> dataList = new ArrayList<>();
		Map<String, Object> data = new HashMap<>();

		for(int i=1; i<=4; i++) {
			data = new HashMap<>();
			data.put("date", "2022-0"+i);
			data.put("allSendCnt", "40000000");
			data.put("allSuccesCnt", "40000000(100%)");
			data.put("smsSendCnt", "10000000");
			data.put("smsSuccesCnt", "10000000(100%)");
			data.put("lmsSendCnt", "10000000");
			data.put("lmsSuccesCnt", "10000000(100%)");
			data.put("mmsSendCnt", "10000000");
			data.put("mmsSuccesCnt", "10000000(100%)");
			data.put("notiTalkSendCnt", "10000000");
			data.put("notiTalkSuccesCnt", "10000000(100%)");

			dataList.add(data);
		}
		dataObj.put("list", dataList);
		result.setData(dataObj);

		return result;
	}

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 일별 통계 목록 조회
	 * @param dayListReqDto
	 * @return
	 */
	public DayListResDto dayList(DayListReqDto dayListReqDto) {
		StatsMapper statsMapper = sqlSessionSlave.getMapper(StatsMapper.class);
		
		//오늘 날짜 조회인지 확인
		if(DateUtils.date2strYMD().equals(dayListReqDto.getEndDay())) {
			dayListReqDto.setNowDiv("Y");
		}
		
		List<DayList> dayLists =  statsMapper.selectDayStatList(dayListReqDto);

		DayListRes dayListRes = new DayListRes();
		dayListRes.setList(dayLists);

		return new DayListResDto(ApiResponseCode.SUCCESS, dayListRes);
	}

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 일별 통계 목록 엑셀 다운로드
	 * @param paramMap
	 * @return
	 */
	@Deprecated
	public ApiResponseMessage dayListExcel(Map<String, Object> paramMap) {
		ApiResponseMessage result = new ApiResponseMessage(ApiResponseCode.SUCCESS);
		Map<String, Object> dataObj = new HashMap<>();
		List<Map<String,Object>> dataList = new ArrayList<>();
		Map<String, Object> data = new HashMap<>();

		for(int i=1; i<=9; i++) {
			data = new HashMap<>();
			data.put("date", "2022-03-0"+i);
			data.put("allSendCnt", "40000000");
			data.put("allSuccesCnt", "40000000(100%)");
			data.put("smsSendCnt", "10000000");
			data.put("smsSuccesCnt", "10000000(100%)");
			data.put("lmsSendCnt", "10000000");
			data.put("lmsSuccesCnt", "10000000(100%)");
			data.put("mmsSendCnt", "10000000");
			data.put("mmsSuccesCnt", "10000000(100%)");
			data.put("notiTalkSendCnt", "10000000");
			data.put("notiTalkSuccesCnt", "10000000(100%)");

			dataList.add(data);
		}
		dataObj.put("list", dataList);
		result.setData(dataObj);

		return result;
	}

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 사업자 월별 통계 목록 조회
	 * @param bsnmMonthListReqDto
	 * @return
	 */
	public BsnmMonthListResDto bsnmMonthList(BsnmMonthListReqDto bsnmMonthListReqDto) {

		StatsMapper statsMapper = sqlSessionSlave.getMapper(StatsMapper.class);
		
		//오늘 날짜 조회인지 확인
		if(DateUtils.date2strYM().equals(bsnmMonthListReqDto.getEndMon())) {
			bsnmMonthListReqDto.setNowDiv("Y");
		}
	
		String nowPage = String.valueOf(bsnmMonthListReqDto.getPage());
		int totalCnt = statsMapper.selectCustMstatListTotalCnt(bsnmMonthListReqDto);

		if (totalCnt == 0) {

			BsnmMonthListRes bsnmMonthListRes = new BsnmMonthListRes();
			bsnmMonthListRes.setList(new ArrayList<>());

			Paging paging = new Paging();
			paging.setPage(nowPage);
			paging.setTotalCnt(String.valueOf(totalCnt));
			bsnmMonthListRes.setPaging(paging);

			return new BsnmMonthListResDto(ApiResponseCode.CM_NOT_FOUND, bsnmMonthListRes);
		}

		int page = bsnmMonthListReqDto.getPage();
		int pagePerRows = bsnmMonthListReqDto.getPagePerRows();
		page = (page - 1) * pagePerRows;
		bsnmMonthListReqDto.setPage(page);

		List<BsnmMonthList> bsnmMonthLists =  statsMapper.selectCustMstatList(bsnmMonthListReqDto);

		BsnmMonthListRes bsnmMonthListRes = new BsnmMonthListRes();
		bsnmMonthListRes.setList(bsnmMonthLists);

		Paging paging = new Paging();
		paging.setPage(nowPage);
		paging.setTotalCnt(String.valueOf(totalCnt));
		bsnmMonthListRes.setPaging(paging);

		return new BsnmMonthListResDto(ApiResponseCode.SUCCESS, bsnmMonthListRes);
	}

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 사업자 월별 통계 목록 엑셀 다운로드
	 * @param bsnmMonthListExcelReqDto
	 * @return
	 */
	public BsnmMonthListExcelResDto bsnmMonthListExcel(BsnmMonthListExcelReqDto bsnmMonthListExcelReqDto) {
		StatsMapper statsMapper = sqlSessionSlave.getMapper(StatsMapper.class);
		
		//오늘 날짜 조회인지 확인
		if(DateUtils.date2strYM().equals(bsnmMonthListExcelReqDto.getEndMon())) {
			bsnmMonthListExcelReqDto.setNowDiv("Y");
		}
		
		List<BsnmMonthList> bsnmMonthLists =  statsMapper.selectCustMstatExcelList(bsnmMonthListExcelReqDto);

		BsnmMonthListExcelRes bsnmMonthListExcelRes = new BsnmMonthListExcelRes();
		bsnmMonthListExcelRes.setList(bsnmMonthLists);

		return new BsnmMonthListExcelResDto(ApiResponseCode.SUCCESS, bsnmMonthListExcelRes);
	}

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 사업자 일별 통계 목록 조회
	 * @param bsnmDayListReqDto
	 * @return
	 */
	public BsnmDayListResDto bsnmDayList(BsnmDayListReqDto bsnmDayListReqDto) {
		StatsMapper statsMapper = sqlSessionSlave.getMapper(StatsMapper.class);

		String nowPage = String.valueOf(bsnmDayListReqDto.getPage());
		
		//오늘 날짜 조회인지 확인
		if(DateUtils.date2strYMD().equals(bsnmDayListReqDto.getEndDay())) {
			bsnmDayListReqDto.setNowDiv("Y");
		}
		
		int totalCnt = statsMapper.selectCustDstatListTotalCnt(bsnmDayListReqDto);

		if (totalCnt == 0) {

			BsnmDayListRes bsnmDayListRes = new BsnmDayListRes();
			bsnmDayListRes.setList(new ArrayList<>());

			Paging paging = new Paging();
			paging.setPage(nowPage);
			paging.setTotalCnt(String.valueOf(totalCnt));
			bsnmDayListRes.setPaging(paging);

			return new BsnmDayListResDto (ApiResponseCode.CM_NOT_FOUND, bsnmDayListRes);
		}

		int page = bsnmDayListReqDto.getPage();
		int pagePerRows = bsnmDayListReqDto.getPagePerRows();
		page = (page -1) * pagePerRows;
		bsnmDayListReqDto.setPage(page);

		List<BsnmDayList> bsnmDayLists =  statsMapper.selectCustDstatList(bsnmDayListReqDto);

		BsnmDayListRes bsnmDayListRes = new BsnmDayListRes();
		bsnmDayListRes.setList(bsnmDayLists);

		Paging paging = new Paging();
		paging.setPage(nowPage);
		paging.setTotalCnt(String.valueOf(totalCnt));
		bsnmDayListRes.setPaging(paging);

		return new BsnmDayListResDto(ApiResponseCode.SUCCESS, bsnmDayListRes);
	}

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 사업자 일별 통계 목록 엑셀 다운로드
	 * @param bsnmDayListExcelReqDto
	 * @return
	 */
	public BsnmDayListExcelResDto bsnmDayListExcel(BsnmDayListExcelReqDto bsnmDayListExcelReqDto) {

		StatsMapper statsMapper = sqlSessionSlave.getMapper(StatsMapper.class);
		
		//오늘 날짜 조회인지 확인
		if(DateUtils.date2strYMD().equals(bsnmDayListExcelReqDto.getEndDay())) {
			bsnmDayListExcelReqDto.setNowDiv("Y");
		}

		List<BsnmDayList> bsnmDayLists =  statsMapper.selectCustDstatExcelList(bsnmDayListExcelReqDto);

		BsnmDayListExcelRes bsnmDayListExcelRes = new BsnmDayListExcelRes();
		bsnmDayListExcelRes.setList(bsnmDayLists);


		return new BsnmDayListExcelResDto(ApiResponseCode.SUCCESS, bsnmDayListExcelRes);
	}

}
