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

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eactive.eai.data.entity.onl.messagekey.MessageKey;
import com.eactive.eai.data.entity.onl.messagekey.MessageKeyId;
import com.eactive.eai.data.entity.onl.messagekey.QMessageKey;
import com.eactive.eai.data.jpa.AbstractDataService;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQuery;

@Service
@Transactional
public class MessageKeyService extends AbstractDataService<MessageKey, MessageKeyId, MessageKeyEMSRepository> {

	public Page<MessageKey> findAll(Pageable pageable, String searchAdptrBzwkGroupName, String searchIoDstcd) {

		QMessageKey qMessageKey = QMessageKey.messageKey;

		BooleanBuilder predicate = new BooleanBuilder();

		if (StringUtils.isNotBlank(searchAdptrBzwkGroupName)) {
			predicate.and(qMessageKey.id.adptrbzwkgroupname.containsIgnoreCase(searchAdptrBzwkGroupName));
		}

		if (StringUtils.isNotBlank(searchIoDstcd)) {
			predicate.and(qMessageKey.id.iodstcd.equalsIgnoreCase(searchIoDstcd));
		}

		return repository.findAll(predicate, pageable);
	}

	public List<MessageKey> findAll(String adptrBzwkGroupName, String msgDstcd, String ioDstcd, Integer groupInorNo) {
		QMessageKey qMessageKey = QMessageKey.messageKey;

		JPAQuery<MessageKey> query = getJPAQueryFactory()
				.selectFrom(qMessageKey)
				.where(qMessageKey.id.adptrbzwkgroupname.eq(adptrBzwkGroupName));
		query.where(qMessageKey.id.msgdstcd.eq(msgDstcd));
		query.where(qMessageKey.id.iodstcd.eq(ioDstcd));
		query.where(qMessageKey.id.groupinorno.eq(groupInorNo));

		query
				.orderBy(qMessageKey.id.adptrbzwkgroupname.asc(), qMessageKey.id.msgdstcd.asc(),
						qMessageKey.id.iodstcd.asc(), qMessageKey.id.groupinorno.asc(),
						qMessageKey.id.fldprcssno.asc());

		return query.fetch();
	}

	public long countByAdptrBzwkGroupNameAndIoDstcd(String adptrBzwkGroupName, String ioDstcd) {
		QMessageKey qMessageKey = QMessageKey.messageKey;
		BooleanExpression predicate = qMessageKey.id.adptrbzwkgroupname
				.containsIgnoreCase(adptrBzwkGroupName)
				.and(qMessageKey.id.iodstcd.equalsIgnoreCase(ioDstcd));

		return repository.count(predicate);
	}

	public int getLastGroupKey(String adptrBzwkGroupName, String msgDstcd, String ioDstcd) {

		QMessageKey qMessageKey = QMessageKey.messageKey;
		Integer maxGroupInOrNo = getJPAQueryFactory()
				.select(qMessageKey.id.groupinorno.max())
				.from(qMessageKey)
				.where(qMessageKey.id.adptrbzwkgroupname
						.eq(adptrBzwkGroupName)
						.and(qMessageKey.id.msgdstcd.eq(msgDstcd))
						.and(qMessageKey.id.iodstcd.eq(ioDstcd)))
				.fetchOne();

		return (maxGroupInOrNo != null ? maxGroupInOrNo : 0) + 1;
	}

	public List<MessageKey> findByAdptrBzwkGroupNameAndIoDstcd(String adptrBzwkGroupName, String ioDstcd) {
		return findByAdptrBzwkGroupNameAndIoDstcd(adptrBzwkGroupName, ioDstcd, false);
	}
	public List<MessageKey> findByAdptrBzwkGroupNameAndIoDstcd(String adptrBzwkGroupName, String ioDstcd, boolean adptrBzwkGroupNameEqualCase) {
		QMessageKey qMessageKey = QMessageKey.messageKey;
		BooleanExpression predicate = qMessageKey.id.iodstcd.equalsIgnoreCase(ioDstcd); // 이 부분을 먼저 처리

		if (adptrBzwkGroupNameEqualCase) {
			// 정확한 일치를 사용
			predicate = predicate.and(qMessageKey.id.adptrbzwkgroupname.equalsIgnoreCase(adptrBzwkGroupName));
		} else {
			// 'like' 검색을 사용 (대소문자 구분 없이 포함하는지 여부)
			predicate = predicate.and(qMessageKey.id.adptrbzwkgroupname.containsIgnoreCase(adptrBzwkGroupName));
		}

		return getJPAQueryFactory()
				.selectFrom(qMessageKey)
				.where(predicate)
				.orderBy(qMessageKey.id.groupinorno.asc(), qMessageKey.id.fldprcssno.asc())
				.fetch();
	}
}
