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

import kr.co.uplus.ez.api.mntrng.dto.*;
import kr.co.uplus.ez.common.data.ApiResponseCode;
import kr.co.uplus.ez.common.data.Paging;
import org.apache.commons.lang3.StringUtils;
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.List;

@Service
public class MntrngService {

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

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

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 발송내역 목록 조회
	 * @param paramMap
	 * @return
	 */
	public SendListResDto sendList(SendListReqDto sendListReqDto) {

		MntrngMapper mntrngMapper = sqlSessionSlave.getMapper(MntrngMapper.class);

		String nowPage = String.valueOf(sendListReqDto.getPage());
		int totalCnt = mntrngMapper.selectSendListTotalCnt(sendListReqDto);

		if (totalCnt == 0 || StringUtils.isBlank(sendListReqDto.getSentDate())) {
			SendListRes sendListRes = new SendListRes();
			sendListRes.setList(new ArrayList<>());
			Paging paging = new Paging();
			paging.setPage(nowPage);
			paging.setTotalCnt(String.valueOf(totalCnt));
			sendListRes.setPaging(paging);
			return new SendListResDto(ApiResponseCode.CM_NOT_FOUND, sendListRes);
		}

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

		List<SendList> sendLists =  mntrngMapper.selectSendList(sendListReqDto);

		SendListRes sendListRes = new SendListRes();
		sendListRes.setList(sendLists);

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

		return new SendListResDto(ApiResponseCode.SUCCESS, sendListRes);
	}

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 실시간 발송 현황 정보 조회
	 * @param
	 * @return
	 */
	public LiveSendSttusResDto liveSendSttus() {

		MntrngMapper mntrngMapper = sqlSessionSlave.getMapper(MntrngMapper.class);
		LiveSendSttusRes liveSendSttusRes = new LiveSendSttusRes();
		LiveSendSttus reqData = new LiveSendSttus();
		LiveSendSttus resultData = new LiveSendSttus();

		try {
			// 최근1분 발송현황정보
			resultData = new LiveSendSttus();
			reqData.setReqType("M");
			reqData.setChStr("SMS");
			resultData = mntrngMapper.selectLiveSendSttus(reqData);
			liveSendSttusRes.setSendingCntSmsM(resultData.getSendCnt());
			liveSendSttusRes.setSuccesCntSmsM(resultData.getSuccCnt());
			liveSendSttusRes.setSuccesRtSmsM(resultData.getSuccRt());
			liveSendSttusRes.setStartTimeM(resultData.getStartTime());
			liveSendSttusRes.setEndTimeM(resultData.getEndTime());

			resultData = new LiveSendSttus();
			reqData.setChStr("MMS");
			resultData = mntrngMapper.selectLiveSendSttus(reqData);
			liveSendSttusRes.setSendingCntMmsM(resultData.getSendCnt());
			liveSendSttusRes.setSuccesCntMmsM(resultData.getSuccCnt());
			liveSendSttusRes.setSuccesRtMmsM(resultData.getSuccRt());

			resultData = new LiveSendSttus();
			reqData.setChStr("LMS");
			resultData = mntrngMapper.selectLiveSendSttus(reqData);
			liveSendSttusRes.setSendingCntLmsM(resultData.getSendCnt());
			liveSendSttusRes.setSuccesCntLmsM(resultData.getSuccCnt());
			liveSendSttusRes.setSuccesRtLmsM(resultData.getSuccRt());

			resultData = new LiveSendSttus();
			reqData.setChStr("ALIMTALK");
			resultData = mntrngMapper.selectLiveSendSttus(reqData);
			liveSendSttusRes.setSendingCntAlmtM(resultData.getSendCnt());
			liveSendSttusRes.setSuccesCntAlmtM(resultData.getSuccCnt());
			liveSendSttusRes.setSuccesRtAlmtM(resultData.getSuccRt());

			// 최근1시간 발송현황정보
			resultData = new LiveSendSttus();
			reqData.setReqType("H");
			reqData.setChStr("SMS");
			resultData = mntrngMapper.selectLiveSendSttus(reqData);
			liveSendSttusRes.setSendingCntSmsH(resultData.getSendCnt());
			liveSendSttusRes.setSuccesCntSmsH(resultData.getSuccCnt());
			liveSendSttusRes.setSuccesRtSmsH(resultData.getSuccRt());
			liveSendSttusRes.setStartTimeH(resultData.getStartTime());
			liveSendSttusRes.setEndTimeH(resultData.getEndTime());

			resultData = new LiveSendSttus();
			reqData.setChStr("MMS");
			resultData = mntrngMapper.selectLiveSendSttus(reqData);
			liveSendSttusRes.setSendingCntMmsH(resultData.getSendCnt());
			liveSendSttusRes.setSuccesCntMmsH(resultData.getSuccCnt());
			liveSendSttusRes.setSuccesRtMmsH(resultData.getSuccRt());

			resultData = new LiveSendSttus();
			reqData.setChStr("LMS");
			resultData = mntrngMapper.selectLiveSendSttus(reqData);
			liveSendSttusRes.setSendingCntLmsH(resultData.getSendCnt());
			liveSendSttusRes.setSuccesCntLmsH(resultData.getSuccCnt());
			liveSendSttusRes.setSuccesRtLmsH(resultData.getSuccRt());

			resultData = new LiveSendSttus();
			reqData.setChStr("ALIMTALK");
			resultData = mntrngMapper.selectLiveSendSttus(reqData);
			liveSendSttusRes.setSendingCntAlmtH(resultData.getSendCnt());
			liveSendSttusRes.setSuccesCntAlmtH(resultData.getSuccCnt());
			liveSendSttusRes.setSuccesRtAlmtH(resultData.getSuccRt());

			// 당일 발송현황정보
			resultData = new LiveSendSttus();
			reqData.setReqType("D");
			reqData.setChStr("SMS");
			resultData = mntrngMapper.selectLiveSendSttus(reqData);
			liveSendSttusRes.setSendingCntSmsD(resultData.getSendCnt());
			liveSendSttusRes.setSuccesCntSmsD(resultData.getSuccCnt());
			liveSendSttusRes.setSuccesRtSmsD(resultData.getSuccRt());
			liveSendSttusRes.setStartTimeD(resultData.getStartTime());
			liveSendSttusRes.setEndTimeD(resultData.getEndTime());

			resultData = new LiveSendSttus();
			reqData.setChStr("MMS");
			resultData = mntrngMapper.selectLiveSendSttus(reqData);
			liveSendSttusRes.setSendingCntMmsD(resultData.getSendCnt());
			liveSendSttusRes.setSuccesCntMmsD(resultData.getSuccCnt());
			liveSendSttusRes.setSuccesRtMmsD(resultData.getSuccRt());

			resultData = new LiveSendSttus();
			reqData.setChStr("LMS");
			resultData = mntrngMapper.selectLiveSendSttus(reqData);
			liveSendSttusRes.setSendingCntLmsD(resultData.getSendCnt());
			liveSendSttusRes.setSuccesCntLmsD(resultData.getSuccCnt());
			liveSendSttusRes.setSuccesRtLmsD(resultData.getSuccRt());

			resultData = new LiveSendSttus();
			reqData.setChStr("ALIMTALK");
			resultData = mntrngMapper.selectLiveSendSttus(reqData);
			liveSendSttusRes.setSendingCntAlmtD(resultData.getSendCnt());
			liveSendSttusRes.setSuccesCntAlmtD(resultData.getSuccCnt());
			liveSendSttusRes.setSuccesRtAlmtD(resultData.getSuccRt());

		} catch (Exception e) {
			// TODO: handle exception
		}

		return new LiveSendSttusResDto(ApiResponseCode.SUCCESS, liveSendSttusRes);
	}

}
