package com.eactive.eai.rms.onl.manage.control.log.bzwkgroup;

import java.util.List;
import java.util.stream.Collectors;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.eactive.eai.data.entity.onl.restrict.QRestrictInfoLog;
import com.eactive.eai.rms.data.entity.onl.restrict.RestrictInfoLogService;
import com.eactive.eai.rms.onl.manage.control.log.ControlLogManUISearch;
import com.eactive.eai.rms.onl.manage.control.log.ControlLogService;
import com.eactive.eai.rms.onl.manage.control.log.ControlLogServiceDto;
import com.querydsl.core.Tuple;
import com.querydsl.jpa.impl.JPAQueryFactory;

@Service
public class BzwkGroupControlLogService extends RestrictInfoLogService implements ControlLogService {

	@PersistenceContext
	private EntityManager entityManager;

	public Page<ControlLogServiceDto> findALLGroupBy(Pageable pageable, ControlLogManUISearch uiSearch) {
		JPAQueryFactory jpaQueuryFactory = new JPAQueryFactory(entityManager);
		QRestrictInfoLog qLog = QRestrictInfoLog.restrictInfoLog;
		List<Tuple> groupByCount = jpaQueuryFactory
				.select(qLog.id.eaibzwkdstcd, qLog.id.eaibzwkdstcd.count())
				.from(qLog)
				.where(qLog.id.msgdpstyms
						.between(uiSearch.getSearchStartDate() + BETWEEN_START_HHmmssSSS,
								uiSearch.getSearchEndDate() + BETWEEN_END_HHmmssSSS))
				.groupBy(qLog.id.eaibzwkdstcd)
				.orderBy(qLog.id.eaibzwkdstcd.asc())
				.offset(pageable.getOffset())
				.limit(pageable.getPageSize())
				.fetch();

		List<ControlLogServiceDto> dtoList = tupleToDtoList(qLog, groupByCount);

		long totalCount = jpaQueuryFactory.select(qLog.id.eaibzwkdstcd.countDistinct()).from(qLog).fetchOne();

		return new PageImpl<>(dtoList, pageable, totalCount);
	}

	public List<ControlLogServiceDto> findALLGroupBy(ControlLogManUISearch uiSearch) {
		JPAQueryFactory jpaQueuryFactory = new JPAQueryFactory(entityManager);
		QRestrictInfoLog qLog = QRestrictInfoLog.restrictInfoLog;
		List<Tuple> groupByCount = jpaQueuryFactory
				.select(qLog.id.eaibzwkdstcd, qLog.id.eaibzwkdstcd.count())
				.from(qLog)
				.where(qLog.id.msgdpstyms
						.between(uiSearch.getSearchStartDate() + BETWEEN_START_HHmmssSSS,
								uiSearch.getSearchEndDate() + BETWEEN_END_HHmmssSSS))
				.groupBy(qLog.id.eaibzwkdstcd)
				.orderBy(qLog.id.eaibzwkdstcd.asc())
				.fetch();
		return tupleToDtoList(qLog, groupByCount);
	}

	private List<ControlLogServiceDto> tupleToDtoList(QRestrictInfoLog qLog, List<Tuple> groupByCount) {
		return groupByCount.stream().map(tuple -> {
			ControlLogServiceDto dto = new ControlLogServiceDto();
			dto.setEaibzwkdstcd(tuple.get(qLog.id.eaibzwkdstcd));
			dto.setEaisvcname(tuple.get(qLog.eaisvcname));
			dto.setCnt(tuple.get(qLog.id.eaibzwkdstcd.count()).toString());
			return dto;
		}).collect(Collectors.toList());
	}

}
