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

import kr.co.uplus.ez.api.attractMgt.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 org.mybatis.spring.SqlSessionTemplate;
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.thymeleaf.util.StringUtils;

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

@Service
public class AttractMgtService {

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

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

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 유치채널 목록조회
	 * @param channelListReqDto
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public ChannelListResDto channelList(ChannelListReqDto channelListReqDto) {
		AttractMgtMapper attractMgtMapper = sqlSessionSlave.getMapper(AttractMgtMapper.class);

		String nowPage = String.valueOf(channelListReqDto.getPage());

		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		UserDetails userDetails = (UserDetails) principal;
		String attrctorId = userDetails.getUsername();
		ArrayList auth = (ArrayList) userDetails.getAuthorities();

		if(StringUtils.equals(Const.AUTH_CD_AGENCY, auth.get(0).toString())){
			channelListReqDto.setAttrctorId(attrctorId);
		}
		int totalCnt = attractMgtMapper.selectAttractListTotalCnt(channelListReqDto);

		if (totalCnt == 0) {

			ChannelListRes channelListRes = new ChannelListRes();
			channelListRes.setList(new ArrayList<>());

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

			return new ChannelListResDto(ApiResponseCode.CM_NOT_FOUND, channelListRes);
		}

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

		List<ChannelInfo> channelInfos = attractMgtMapper.selectAttractList(channelListReqDto);

		ChannelListRes channelListRes = new ChannelListRes();
		channelListRes.setList(channelInfos);

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

		return new ChannelListResDto(ApiResponseCode.SUCCESS, channelListRes);
	}

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 유치채널 목록 엑셀 다운로드
	 * @param channelListExcelReqDto
	 * @return
	 */
	public ChannelListExcelResDto channelListExcel(ChannelListExcelReqDto channelListExcelReqDto) {
		AttractMgtMapper attractMgtMapper = sqlSessionSlave.getMapper(AttractMgtMapper.class);

		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		UserDetails userDetails = (UserDetails) principal;
		String attrctorId = userDetails.getUsername();
		ArrayList auth = (ArrayList) userDetails.getAuthorities();

		if(StringUtils.equals(Const.AUTH_CD_AGENCY, auth.get(0).toString())){
			channelListExcelReqDto.setAttrctorId(attrctorId);
		}

		List<ChannelInfo> channelInfos = attractMgtMapper.selectAttractExcelList(channelListExcelReqDto);
		ChannelListExcelRes channelListExcelRes = new ChannelListExcelRes();
		channelListExcelRes.setList(channelInfos);
		return new ChannelListExcelResDto(ApiResponseCode.SUCCESS, channelListExcelRes);
	}

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 유치 채널 상세 조회
	 * @param channelDetailReqDto
	 * @return
	 */
	public ChannelDetailResDto channelDetail(ChannelDetailReqDto channelDetailReqDto) {
		AttractMgtMapper attractMgtMapper = sqlSessionSlave.getMapper(AttractMgtMapper.class);

		ChannelDetailRes channelDetailRes = attractMgtMapper.selectAttractDetail(channelDetailReqDto);
		List<ChannelDetail> channelDetails = attractMgtMapper.selectSndCountList(channelDetailReqDto);
		channelDetailRes.setList(channelDetails);

		return new ChannelDetailResDto(ApiResponseCode.SUCCESS, channelDetailRes);
	}

	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 발송건수 엑셀 다운로드
	 * @param channelListExcelReqDto
	 * @return
	 */
	public ChannelDetailResDto sendNumberListExcel(ChannelDetailReqDto channelDetailReqDto) {
		AttractMgtMapper attractMgtMapper = sqlSessionSlave.getMapper(AttractMgtMapper.class);

		ChannelDetailRes channelDetailRes = new ChannelDetailRes();
		List<ChannelDetail> channelDetails = attractMgtMapper.sendNumberListExcel(channelDetailReqDto);
		channelDetailRes.setList(channelDetails);


		return new ChannelDetailResDto(ApiResponseCode.SUCCESS, channelDetailRes);
	}

}
