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

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

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.eactive.eai.data.entity.onl.adapter.QAdapterGroup;
import com.eactive.eai.data.entity.onl.message.QEAIMessageEntity;
import com.eactive.eai.data.entity.onl.restrict.QRestrictInfo;
import com.eactive.eai.data.entity.onl.restrict.RestrictInfo;
import com.eactive.eai.data.jpa.AbstractDataService;
import com.eactive.eai.rms.onl.manage.control.control.ControlManUISearch;
import com.eactive.eai.rms.onl.manage.control.control.ControlServiceDto;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.Tuple;
import com.querydsl.jpa.impl.JPAQueryFactory;

@Service
public class RestrictInfoService extends AbstractDataService<RestrictInfo, String, RestrictInfoRepository> {

	public List<ControlServiceDto> findAllForService(ControlManUISearch uiSearch) {
		QRestrictInfo qRestricInfo = QRestrictInfo.restrictInfo;
		QEAIMessageEntity qEaiMessageEntity = QEAIMessageEntity.eAIMessageEntity;
		JPAQueryFactory queryFactory = getJPAQueryFactory();

		BooleanBuilder builder = new BooleanBuilder();
		if ("H".equals(uiSearch.getSearchIo()))
			builder.and(qEaiMessageEntity.eaisvcname.endsWith("S1"));
		else if ("O".equals(uiSearch.getSearchIo()))
			builder.and(qEaiMessageEntity.eaisvcname.endsWith("S2"));

		if (!"A".equals(uiSearch.getSearchEaiBzwkDstcd()))
			builder.and(qEaiMessageEntity.eaibzwkdstcd.eq(uiSearch.getSearchEaiBzwkDstcd()));

		if (StringUtils.isNotEmpty(uiSearch.getSearchCode()))
			builder.and(qEaiMessageEntity.eaisvcname.startsWith(uiSearch.getSearchCode().toUpperCase()));

		if ("N".equals(uiSearch.getSearchControlSelectType()))
			builder.and(qRestricInfo.eaictrlname.isNull());
		else if ("C".equals(uiSearch.getSearchControlSelectType()))
			builder.and(qRestricInfo.eaictrlname.isNotNull());

		List<Tuple> tupleList = queryFactory
				.select(qEaiMessageEntity.eaisvcname, qEaiMessageEntity.eaisvcdesc, qEaiMessageEntity.eaibzwkdstcd,
						qRestricInfo.eaictrlname, qRestricInfo.eaictrldsticctnt)
				.from(qEaiMessageEntity)
				.leftJoin(qRestricInfo)
				.on(qEaiMessageEntity.eaisvcname
						.eq(qRestricInfo.eaictrlname)
						.and(qRestricInfo.eaictrlnamedstcd.eq("1")))
				.where(builder)
				.orderBy(qEaiMessageEntity.eaisvcname.asc())
				.fetch();
		List<ControlServiceDto> dtoList = new ArrayList<>();
		for (Tuple tuple : tupleList) {
			ControlServiceDto dto = new ControlServiceDto();
			dto.setEaisvcname(tuple.get(qEaiMessageEntity.eaisvcname));
			dto.setEaisvcdesc(tuple.get(qEaiMessageEntity.eaisvcdesc));
			dto.setEaibzwkdstcd(tuple.get(qEaiMessageEntity.eaibzwkdstcd));
			dto.setEaictrlname(tuple.get(qRestricInfo.eaictrlname));
			dto.setEaictrldsticctnt(tuple.get(qRestricInfo.eaictrldsticctnt));
			dtoList.add(dto);
		}
		return dtoList;
	}

	public List<ControlServiceDto> findAllForInsti(ControlManUISearch uiSearch) {
		QRestrictInfo qRestricInfo = QRestrictInfo.restrictInfo;
		QAdapterGroup qAdapterGroup = QAdapterGroup.adapterGroup;
		JPAQueryFactory queryFactory = getJPAQueryFactory();

		BooleanBuilder subBuilder = new BooleanBuilder();
		subBuilder.and(qAdapterGroup.osidinstino.isNotEmpty());
		if (!"A".equals(uiSearch.getSearchEaiBzwkDstcd()))
			subBuilder.and(qAdapterGroup.eaibzwkdstcd.eq(uiSearch.getSearchEaiBzwkDstcd()));

		if (StringUtils.isNotEmpty(uiSearch.getSearchCode()))
			subBuilder.and(qAdapterGroup.osidinstino.contains(uiSearch.getSearchCode()));

		if ("N".equals(uiSearch.getSearchControlSelectType()))
			subBuilder.and(qRestricInfo.eaictrlname.isNull());
		else if ("C".equals(uiSearch.getSearchControlSelectType()))
			subBuilder.and(qRestricInfo.eaictrlname.isNotNull());

		List<Tuple> adapterTupleList = queryFactory
				.select(qAdapterGroup.osidinstino, qAdapterGroup.adptrbzwkgroupdesc, qAdapterGroup.eaibzwkdstcd,
						qRestricInfo.eaictrlname, qRestricInfo.eaictrldsticctnt)
				.distinct()
				.from(qAdapterGroup)
				.leftJoin(qRestricInfo)
				.on(qAdapterGroup.osidinstino.eq(qRestricInfo.eaictrlname).and(qRestricInfo.eaictrlnamedstcd.eq("2")))
				.where(subBuilder)
				.orderBy(qAdapterGroup.osidinstino.asc())
				.fetch();

		List<ControlServiceDto> dtoList = new ArrayList<>();
		for (Tuple tuple : adapterTupleList) {
			ControlServiceDto dto = new ControlServiceDto();
			dto.setEaisvcname(tuple.get(qAdapterGroup.osidinstino).trim());
			dto.setEaisvcdesc(tuple.get(qAdapterGroup.adptrbzwkgroupdesc));
			dto.setEaibzwkdstcd(tuple.get(qAdapterGroup.eaibzwkdstcd));
			dto.setEaictrlname(tuple.get(qRestricInfo.eaictrlname));
			dto.setEaictrldsticctnt(tuple.get(qRestricInfo.eaictrldsticctnt));
			dtoList.add(dto);
		}
		return dtoList;
	}

}
