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

import kr.co.uplus.ez.api.riskMgt.dto.*;
import kr.co.uplus.ez.common.data.ApiResponseCode;
import kr.co.uplus.ez.common.data.Const;
import kr.co.uplus.ez.common.data.Paging;
import lombok.extern.slf4j.Slf4j;

import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class RiskMgtService {

	private static final Logger logger = LoggerFactory.getLogger(RiskMgtService.class);

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

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

	/**
	 * date : 2022. 4. 25. auth : ckr desc : 발신번호 차단 목록 조회
	 * 
	 * @param sendNumIntrcpListReqDto
	 * @return SendNumIntrcpListResDto
	 */
	public SendNumIntrcpListResDto sendNumIntrcpList(SendNumIntrcpListReqDto sendNumIntrcpListReqDto) {

		RiskMgtMapper riskMgtMapper = sqlSessionSlave.getMapper(RiskMgtMapper.class);

		// 현재 페이지.
		String nowPage = String.valueOf(sendNumIntrcpListReqDto.getPage());
		int totalCnt = riskMgtMapper.selectSndrnoBlckTotalCnt(sendNumIntrcpListReqDto);

		if (totalCnt == 0) {

			SendNumIntrcpListRes sendNumIntrcpListRes = new SendNumIntrcpListRes();
			sendNumIntrcpListRes.setList(new ArrayList<>());

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

			return new SendNumIntrcpListResDto(ApiResponseCode.CM_NOT_FOUND, sendNumIntrcpListRes);
		}

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

		List<SendNumIntrcpList> sendNumIntrcpListList = riskMgtMapper.selectSndrnoBlckList(sendNumIntrcpListReqDto);

		SendNumIntrcpListRes sendNumIntrcpListRes = new SendNumIntrcpListRes();
		sendNumIntrcpListRes.setList(sendNumIntrcpListList);

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

		return new SendNumIntrcpListResDto(ApiResponseCode.SUCCESS, sendNumIntrcpListRes);
	}

	/**
	 * date : 2022. 4. 25. auth : ckr desc : 발신번호 차단 상세 정보 조회
	 * 
	 * @param sendNumIntrcpDetailReqDto
	 * @return
	 */
	public SendNumIntrcpDetailResDto sendNumIntrcpDetail(SendNumIntrcpDetailReqDto sendNumIntrcpDetailReqDto) {

		RiskMgtMapper riskMgtMapper = sqlSessionSlave.getMapper(RiskMgtMapper.class);

		SendNumIntrcpDetail sendNumIntrcpDetail = riskMgtMapper
				.selectSndrnoBlckDetail(sendNumIntrcpDetailReqDto.getBlckSndrno());

		if (sendNumIntrcpDetail == null) {
			return new SendNumIntrcpDetailResDto(ApiResponseCode.CM_NOT_FOUND);
		}

		return new SendNumIntrcpDetailResDto(ApiResponseCode.SUCCESS, sendNumIntrcpDetail);
	}

	/**
	 * date : 2022. 4. 25. auth : ckr desc : 발신번호 차단 신규 등록
	 * 
	 * @param insertIntrcpReqDto
	 * @return
	 */
	public SendNumInsertIntrcpResDto sendNumInsertIntrcp(SendNumInsertIntrcpReqDto insertIntrcpReqDto) {

		RiskMgtMapper riskMgtMapper = sqlSessionMaster.getMapper(RiskMgtMapper.class);

		SendNumIntrcpDetail sendNumIntrcpDetail = riskMgtMapper.selectSndrnoBlckDetail(insertIntrcpReqDto.getBlckSndrno());
		if(sendNumIntrcpDetail != null){
			return new SendNumInsertIntrcpResDto(ApiResponseCode.CE_DUPLICATE_ID);
		}

		try {
			insertIntrcpReqDto.setBlckYn(Const.COMM_YES);
			// regId 들고오기
			Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			UserDetails userDetails = (UserDetails) principal;
			String regId = userDetails.getUsername();

			insertIntrcpReqDto.setRegId(regId);

			riskMgtMapper.insertSndrnoBlck(insertIntrcpReqDto);
		} catch (Exception e) {
			return new SendNumInsertIntrcpResDto(ApiResponseCode.CM_DB_QUERY_ERR);
		}

		return new SendNumInsertIntrcpResDto(ApiResponseCode.SUCCESS);
	}

	/**
	 * date : 2022. 4. 25. auth : ckr desc : 발신번호 차단 수정
	 * 
	 * @param sendNumUpdateIntrcplReqDto
	 * @return
	 */
	public SendNumUpdateIntrcpResDto sendNumUpdateIntrcp(SendNumUpdateIntrcplReqDto sendNumUpdateIntrcplReqDto) {

		RiskMgtMapper riskMgtMapper = sqlSessionMaster.getMapper(RiskMgtMapper.class);
//		sendNumUpdateIntrcplReqDto.setChgId("test");;
		// regId 들고오기
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		UserDetails userDetails = (UserDetails) principal;
		String chgId = userDetails.getUsername();
		sendNumUpdateIntrcplReqDto.setChgId(chgId);

		int result = riskMgtMapper.updateSndrnoBlck(sendNumUpdateIntrcplReqDto);

		if (result > 0) {
			return new SendNumUpdateIntrcpResDto(ApiResponseCode.SUCCESS);
		} else {
			return new SendNumUpdateIntrcpResDto(ApiResponseCode.CM_NOT_FOUND);
		}
	}

	/**
	 * date : 2022. 4. 25. auth : ckr desc : 발신번호 차단 삭제
	 * 
	 * @param sendNumDeleteIntrcpReqDto
	 * @return
	 */
	public SendNumDeleteIntrcpResDto sendNumDeleteIntrcp(SendNumDeleteIntrcpReqDto sendNumDeleteIntrcpReqDto) {
		RiskMgtMapper riskMgtMapper = sqlSessionMaster.getMapper(RiskMgtMapper.class);

		int result = riskMgtMapper.deleteSndrnoBlck(sendNumDeleteIntrcpReqDto);

		if (result > 0) {
			return new SendNumDeleteIntrcpResDto(ApiResponseCode.SUCCESS);
		} else {
			return new SendNumDeleteIntrcpResDto(ApiResponseCode.CM_NOT_FOUND);
		}
	}

	/**
	 * date : 2022. 4. 25. auth : ckr desc : 080수신번호 차단 목록 조회
	 * 
	 * @param zezNumIntrcpListReqDto
	 * @return
	 */
	public ZezNumIntrcpListResDto zezNumIntrcpList(ZezNumIntrcpListReqDto zezNumIntrcpListReqDto) {

		RiskMgtMapper riskMgtMapper = sqlSessionSlave.getMapper(RiskMgtMapper.class);

		// 현재 페이지.
		String nowPage = String.valueOf(zezNumIntrcpListReqDto.getPage());
		int totalCnt = riskMgtMapper.selectRcvblck080ListTotalCnt(zezNumIntrcpListReqDto);

		if (totalCnt == 0) {

			ZezNumIntrcpListRes zezNumIntrcpListRes = new ZezNumIntrcpListRes();
			zezNumIntrcpListRes.setList(new ArrayList<>());
			Paging paging = new Paging();
			paging.setPage(nowPage);
			paging.setTotalCnt(String.valueOf(totalCnt));
			paging.setTotalCnt(String.valueOf(totalCnt));
			zezNumIntrcpListRes.setPaging(paging);

			return new ZezNumIntrcpListResDto(ApiResponseCode.CM_NOT_FOUND, zezNumIntrcpListRes);
		}

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

		List<ZezNumIntrcpList> zezNumIntrcpLists = riskMgtMapper.selectRcvblck080List(zezNumIntrcpListReqDto);

		ZezNumIntrcpListRes zezNumIntrcpListRes = new ZezNumIntrcpListRes();
		zezNumIntrcpListRes.setList(zezNumIntrcpLists);

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

		return new ZezNumIntrcpListResDto(ApiResponseCode.SUCCESS, zezNumIntrcpListRes);
	}

	/**
	 * date : 2022. 4. 25. auth : ckr desc : 메시지 차단 목록 조회
	 * 
	 * @param msgNumIntrcpListReqDto
	 * @return
	 */
	public MsgIntrcpListResDto msgIntrcpList(MsgIntrcpListReqDto msgNumIntrcpListReqDto) {

		RiskMgtMapper riskMgtMapper = sqlSessionSlave.getMapper(RiskMgtMapper.class);

		// 현재 페이지.
		String nowPage = String.valueOf(msgNumIntrcpListReqDto.getPage());
		int totalCnt = riskMgtMapper.selectMsgBlckwordListTotalCnt(msgNumIntrcpListReqDto);

		if (totalCnt == 0) {

			MsgIntrcpListRes msgIntrcpListRes = new MsgIntrcpListRes();
			msgIntrcpListRes.setList(new ArrayList<>());

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

			return new MsgIntrcpListResDto(ApiResponseCode.CM_NOT_FOUND, msgIntrcpListRes);
		}

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

		List<MsgIntrcpList> msgIntrcpLists = riskMgtMapper.selectMsgBlckwordList(msgNumIntrcpListReqDto);

		MsgIntrcpListRes msgIntrcpListRes = new MsgIntrcpListRes();
		msgIntrcpListRes.setList(msgIntrcpLists);

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

		return new MsgIntrcpListResDto(ApiResponseCode.SUCCESS, msgIntrcpListRes);
	}

	/**
	 * date : 2022. 4. 25. auth : ckr desc : 메시지 차단 상세 정보 조회
	 * 
	 * @param msgIntrcpDetailReqDto
	 * @return
	 */
	public MsgIntrcpDetailResDto msgIntrcpDetail(MsgIntrcpDetailReqDto msgIntrcpDetailReqDto) {

		RiskMgtMapper riskMgtMapper = sqlSessionSlave.getMapper(RiskMgtMapper.class);

		MsgIntrcpDetail msgIntrcpDetail = riskMgtMapper.selectMsgBlckwordDetail(msgIntrcpDetailReqDto);

		if (msgIntrcpDetail == null) {
			return new MsgIntrcpDetailResDto(ApiResponseCode.CM_NOT_FOUND);
		}

		List<MsgBlckwordList> msgBlckwordLists = riskMgtMapper.selectBlckwordDtlList(msgIntrcpDetailReqDto);

		msgIntrcpDetail.setList(msgBlckwordLists);

		return new MsgIntrcpDetailResDto(ApiResponseCode.SUCCESS, msgIntrcpDetail);
	}

	/**
	 * date : 2022. 4. 25. auth : ckr desc : 메시지 차단 신규 등록
	 * 
	 * @param paramMap
	 * @return
	 */
	@Transactional(value="db1TransactionManager")
	public MsgInsertIntrcpResDto msgInsertIntrcp(MsgInsertIntrcpReqDto msgInsertIntrcpReqDto) {

		RiskMgtMapper riskMgtMapper = sqlSessionMaster.getMapper(RiskMgtMapper.class);
		// regId 들고오기
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		UserDetails userDetails = (UserDetails) principal;
		String regId = userDetails.getUsername();
		
			String seqNoStr = riskMgtMapper.selectMsgBlckWordSeq();
			long seqNo = Long.parseLong(seqNoStr);
			// seq select
			msgInsertIntrcpReqDto.setSeqNo(seqNo);
			msgInsertIntrcpReqDto.setRegId(regId);
			msgInsertIntrcpReqDto.setBlckYn(Const.COMM_YES);
			riskMgtMapper.insertMsgBlckword(msgInsertIntrcpReqDto);
			// 리스트에 regId 넣기
			List<MsgBlckwordList> list = msgInsertIntrcpReqDto.getList();
			for (MsgBlckwordList msgBlckword : list) {
				msgBlckword.setSeqNo(seqNo);
				msgBlckword.setRegId(regId);
			}
			riskMgtMapper.insertBlckwordDtl(list);

		return new MsgInsertIntrcpResDto(ApiResponseCode.SUCCESS);
	}

	/**
	 * date : 2022. 4. 25. auth : ckr desc : 메시지 차단 수정
	 * 
	 * @param msgUpdateIntrcplReqDto
	 * @return
	 */
	@Transactional(value="db1TransactionManager")
	public MsgUpdateIntrcpResDto msgUpdateIntrcp(MsgUpdateIntrcplReqDto msgUpdateIntrcplReqDto) {

		RiskMgtMapper riskMgtMapper = sqlSessionMaster.getMapper(RiskMgtMapper.class);

		// regId 들고오기
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		UserDetails userDetails = (UserDetails) principal;
		String regId = userDetails.getUsername();

		msgUpdateIntrcplReqDto.setRegId(regId);
		int result = riskMgtMapper.updateMsgBlckword(msgUpdateIntrcplReqDto);

		if (result > 0) {

			int subResult = riskMgtMapper.deleteBlckwordDtl(msgUpdateIntrcplReqDto.getSeqNo());

			if (subResult > 0) {
				//들고온 seqNo Long 형변환
				long seqNo = Long.parseLong(msgUpdateIntrcplReqDto.getSeqNo());
				
				List<MsgBlckwordList> list = msgUpdateIntrcplReqDto.getList();
				
				for (MsgBlckwordList msgBlckword : list) {
					msgBlckword.setSeqNo(seqNo);
					msgBlckword.setRegId(regId);
				}
				riskMgtMapper.insertBlckwordDtl(list);
			}

			return new MsgUpdateIntrcpResDto(ApiResponseCode.SUCCESS);
		} else {
			return new MsgUpdateIntrcpResDto(ApiResponseCode.CM_NOT_FOUND);
		}

	}

	/**
	 * date : 2022. 4. 25. auth : ckr desc : 메시지 차단 삭제
	 * 
	 * @param msgDeleteIntrcpReqDto
	 * @return
	 */
	public MsgDeleteIntrcpResDto msgDeleteIntrcp(MsgDeleteIntrcpReqDto msgDeleteIntrcpReqDto) {

		RiskMgtMapper riskMgtMapper = sqlSessionMaster.getMapper(RiskMgtMapper.class);

		int result = riskMgtMapper.deleteMsgBlckword(msgDeleteIntrcpReqDto);
		riskMgtMapper.deleteBlckwordDtl(msgDeleteIntrcpReqDto.getSeqNo());

		return new MsgDeleteIntrcpResDto(ApiResponseCode.SUCCESS);
	}

	/**
	 * date : 2022. 4. 25. auth : ckr desc : 차단 내역 목록 조회
	 * 
	 * @param webInsertIntrcpReqDto
	 * @return
	 */
	public WebIntrcpListResDto webIntrcpList(WebInsertIntrcpReqDto webInsertIntrcpReqDto) {

		RiskMgtMapper riskMgtMapper = sqlSessionSlave.getMapper(RiskMgtMapper.class);

		// 현재 페이지.
		String nowPage = String.valueOf(webInsertIntrcpReqDto.getPage());
		int totalCnt = riskMgtMapper.selectWebsndBlckListTotalCnt(webInsertIntrcpReqDto);

		if (totalCnt == 0) {

			WebIntrcpListRes webIntrcpListRes = new WebIntrcpListRes();
			webIntrcpListRes.setList(new ArrayList<>());

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

			return new WebIntrcpListResDto(ApiResponseCode.CM_NOT_FOUND, webIntrcpListRes);
		}

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

		List<WebIntrcpList> webIntrcpLists = riskMgtMapper.selectWebsndBlckList(webInsertIntrcpReqDto);

		WebIntrcpListRes webIntrcpListRes = new WebIntrcpListRes();
		webIntrcpListRes.setList(webIntrcpLists);

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

		return new WebIntrcpListResDto(ApiResponseCode.SUCCESS, webIntrcpListRes);
	}

}
