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

import kr.co.uplus.ez.api.servMgt.dto.RejectRecvInfo;
import kr.co.uplus.ez.api.servMgt.dto.RejectRecvListReqDto;
import kr.co.uplus.ez.api.servMgt.dto.RejectRecvListRes;
import kr.co.uplus.ez.api.servMgt.dto.RejectRecvListResDto;
import kr.co.uplus.ez.common.data.ApiResponseCode;
import kr.co.uplus.ez.common.data.Paging;
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.stereotype.Service;

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

@Service
public class ServMgtService {

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

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

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


	/**
	 * date : 2022. 4. 25.
	 * auth : ckr
	 * desc : 080수신거부 목록 조회
	 * @param rejectRecvListReqDto
	 * @return
	 */
	public RejectRecvListResDto rejectRecvList(RejectRecvListReqDto rejectRecvListReqDto) {
		ServMgtMapper servMgtMapper = sqlSessionSlave.getMapper(ServMgtMapper.class);
		String nowPage = String.valueOf(rejectRecvListReqDto.getPage());
		int totalCnt = servMgtMapper.selectRejectRecvTotalCnt(rejectRecvListReqDto);


		if(totalCnt == 0) {
			Paging paging = new Paging();
			paging.setPage(nowPage);
			paging.setTotalCnt(String.valueOf(totalCnt));
			RejectRecvListRes rejectRecvListRes = new RejectRecvListRes();
			rejectRecvListRes.setList(new ArrayList<>());
			rejectRecvListRes.setPaging(paging);
			return new RejectRecvListResDto(ApiResponseCode.CM_NOT_FOUND, rejectRecvListRes);
		}
		logger.info("perPage : "+rejectRecvListReqDto.getPagePerRows());
		int page = rejectRecvListReqDto.getPage();
		int pagePerRows = rejectRecvListReqDto.getPagePerRows();
		page = (page -1) * pagePerRows;
		rejectRecvListReqDto.setPage(page);

		List<RejectRecvInfo> rejectRecvInfos =  servMgtMapper.selectRejectRecvList(rejectRecvListReqDto);

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

		RejectRecvListRes rejectRecvListRes = new RejectRecvListRes();
		rejectRecvListRes.setList(rejectRecvInfos);
		rejectRecvListRes.setPaging(paging);

		return new RejectRecvListResDto(ApiResponseCode.SUCCESS, rejectRecvListRes);
	}
}
