package com.eactive.eai.rms.data.entity.onl.circuitbreaker;

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.circuitbreaker.CircuitBreakerConfig;
import com.eactive.eai.data.entity.onl.circuitbreaker.QCircuitBreakerConfig;
import com.eactive.eai.data.entity.onl.message.QEAIMessageEntity;
import com.eactive.eai.data.jpa.AbstractDataService;
import com.eactive.eai.rms.onl.manage.circuitbreaker.CircuitBreakerManMapper;
import com.querydsl.core.Tuple;
import com.querydsl.jpa.impl.JPAQueryFactory;

@Service
public class CircuitBreakerService extends AbstractDataService<CircuitBreakerConfig, String, CircuitBreakerRepository> {

	public static final String APIID_TYPE = "01";

	@PersistenceContext
	EntityManager entityManager;

	@Autowired
	CircuitBreakerManMapper mapper;

	public Page<CircuitBreakerServiceDto> findAllForInterface(Pageable pageable, String searchName) {
		JPAQueryFactory jpaQueryFactory = new JPAQueryFactory(entityManager);
		QCircuitBreakerConfig qCircuitBreakerConfig = QCircuitBreakerConfig.circuitBreakerConfig;
		QEAIMessageEntity qEAIMessageEntity = QEAIMessageEntity.eAIMessageEntity;

		List<Tuple> tupleList = jpaQueryFactory
				.select(qCircuitBreakerConfig, qEAIMessageEntity.eaisvcname, qEAIMessageEntity.eaisvcdesc)
				.from(qEAIMessageEntity)
				.leftJoin(qCircuitBreakerConfig)
				.on(qCircuitBreakerConfig.cbreakerType.eq(APIID_TYPE).and(qCircuitBreakerConfig.cbreakerName.eq(qEAIMessageEntity.eaisvcname)))
				.where(qEAIMessageEntity.eaisvcname.contains(searchName))
				.orderBy(qEAIMessageEntity.eaisvcname.asc())
				.offset(pageable.getOffset())
				.limit(pageable.getPageSize())
				.fetch();

		List<CircuitBreakerServiceDto> dtoList = tupleList
				.stream()
				.map(tuple -> toDto(qEAIMessageEntity, qCircuitBreakerConfig, tuple))
				.collect(Collectors.toList());

		long totalCount = jpaQueryFactory
				.select(qEAIMessageEntity.eaisvcname.count())
				.from(qEAIMessageEntity)
				.where(qEAIMessageEntity.eaisvcname.contains(searchName))
				.fetchOne();
		return new PageImpl<>(dtoList, pageable, totalCount);
	}

	private CircuitBreakerServiceDto toDto(QEAIMessageEntity qEAIMessageEntity, QCircuitBreakerConfig qCircuitBreakerConfig,
			Tuple tuple) {
		CircuitBreakerServiceDto dto = null;
		if (tuple.get(qCircuitBreakerConfig) != null)
			dto = mapper.toDto(tuple.get(qCircuitBreakerConfig));
		else {
			dto = new CircuitBreakerServiceDto();
			dto.setCbreakerName(tuple.get(qEAIMessageEntity.eaisvcname));
			dto.setCbreakerType(APIID_TYPE);
			dto.setUseYn("0");//사용안함
		}
		dto.setCbreakerDesc(tuple.get(qEAIMessageEntity.eaisvcdesc));
		return dto;
	}

	public CircuitBreakerServiceDto findByIdForInterface(String eaisvcname) {
		JPAQueryFactory jpaQueryFactory = new JPAQueryFactory(entityManager);
		QCircuitBreakerConfig qCircuitBreakerConfig = QCircuitBreakerConfig.circuitBreakerConfig;
		QEAIMessageEntity qEAIMessageEntity = QEAIMessageEntity.eAIMessageEntity;

		Tuple tuple = jpaQueryFactory
				.select(qCircuitBreakerConfig, qEAIMessageEntity.eaisvcname, qEAIMessageEntity.eaisvcdesc)
				.from(qEAIMessageEntity)
				.leftJoin(qCircuitBreakerConfig)
				.on(qCircuitBreakerConfig.cbreakerType.eq(APIID_TYPE).and(qCircuitBreakerConfig.cbreakerName.eq(qEAIMessageEntity.eaisvcname)))
				.where(qEAIMessageEntity.eaisvcname.eq(eaisvcname))
				.fetchOne();

		return toDto(qEAIMessageEntity, qCircuitBreakerConfig, tuple);
	}
	
}
