package com.eactive.eai.rms.data.entity.onl.flowcontrol.jms;

import java.util.List;
import java.util.Optional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.eactive.eai.data.entity.onl.flowcontrol.jms.QQueueMonitor;
import com.eactive.eai.data.entity.onl.flowcontrol.jms.QueueMonitor;
import com.eactive.eai.data.entity.onl.flowcontrol.jms.QueueMonitorId;
import com.eactive.eai.data.jpa.AbstractDataService;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;

@Service
@Transactional
public class QueueMonitorService
		extends AbstractDataService<QueueMonitor, QueueMonitorId, QueueMonitorRepository> {
	
	@PersistenceContext
	private EntityManager entityManager;

	public Page<QueueMonitor> findPage(Pageable pageable, QueueMonitorSearch uiSearch) {
		BooleanExpression predicate = createPredicate(uiSearch);
		if (predicate == null)
			return repository.findAll(pageable);
		return repository.findAll(predicate, pageable);
	}

	public Iterable<QueueMonitor> findAll(QueueMonitorSearch uiSearch) {
		BooleanExpression predicate = createPredicate(uiSearch);
		Sort sort = Sort.by(Direction.ASC, "id.eaisevrinstncname")
				.and(Sort.by(Direction.ASC, "id.nonsyncztranquename"));
		if (predicate == null)
			return repository.findAll(sort);
		return repository.findAll(predicate, sort);
	}
	
	public Iterable<QueueMonitor> findAllOfNormalQueue(List<String> queusagdstcdList, String eaisevrinstncname) {
		QQueueMonitor qObject = QQueueMonitor.queueMonitor;
		BooleanExpression predicate = qObject.id.eaisevrinstncname.eq(eaisevrinstncname)
				.and(qObject.queusagdstcd.in(queusagdstcdList));
		Sort sort = Sort.by(Direction.ASC, "id.nonsyncztranquename");
		return repository.findAll(predicate, sort);
	}

	public Optional<QueueMonitor> findBizCount(String eaiquebzwkctnt, String eaibzwkdstcd, String queusagdstcd) {
		QQueueMonitor qObject = QQueueMonitor.queueMonitor;
		BooleanExpression predicate = qObject.eaiquebzwkctnt.eq(eaiquebzwkctnt)
				.and(qObject.eaibzwkdstcd.eq(eaibzwkdstcd)).and(qObject.queusagdstcd.eq(queusagdstcd));
		JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);
		QueueMonitor one = queryFactory.selectFrom(qObject).where(predicate).fetchOne();
		return one == null ? Optional.empty()
				: Optional.of(queryFactory.selectFrom(qObject).where(predicate).fetchOne());
	}

	private BooleanExpression createPredicate(QueueMonitorSearch uiSearch) {
		QQueueMonitor qObject = QQueueMonitor.queueMonitor;
		BooleanExpression predicate = null;
		if (StringUtils.isNotBlank(uiSearch.getSearchBzwkdstcd()))
			predicate = andExpression(predicate, qObject.eaibzwkdstcd.contains(uiSearch.getSearchBzwkdstcd()));

		if (StringUtils.isNotBlank(uiSearch.getSearchNonSynczTranQueName()))
			predicate = andExpression(predicate,
					qObject.id.nonsyncztranquename.contains(uiSearch.getSearchNonSynczTranQueName()));

		if (StringUtils.isNotBlank(uiSearch.getSearchQuebzwkdtalsctnt()))
			predicate = andExpression(predicate,
					qObject.eaiquebzwkdtalsctnt.contains(uiSearch.getSearchQuebzwkdtalsctnt()));

		if (StringUtils.isNotBlank(uiSearch.getSearchQueuseyn()))
			predicate = andExpression(predicate, qObject.queuseyn.contains(uiSearch.getSearchQueuseyn()));

		if (StringUtils.isNotBlank(uiSearch.getSearchSevrinstncname()))
			predicate = andExpression(predicate,
					qObject.id.eaisevrinstncname.contains(uiSearch.getSearchSevrinstncname()));

		return predicate;
	}

	private BooleanExpression andExpression(BooleanExpression predicate, BooleanExpression right) {
		return predicate == null ? right : predicate.and(right);
	}
}
