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

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.jpa.AbstractDataService;
import com.querydsl.core.types.dsl.BooleanExpression;

@Service
@Transactional
public class SubExtendedColumnDefinitionService
		extends AbstractDataService<SubExtendedColumnDefinition, String, SubExtendedColumnDefinitionRepository> {

	public Page<SubExtendedColumnDefinition> findAll(Pageable pageable, String searchColumnName,
			String searchColumnDesc) {
		QSubExtendedColumnDefinition qSubExtendedColumnDefinition = QSubExtendedColumnDefinition.subExtendedColumnDefinition;

		BooleanExpression predicate = qSubExtendedColumnDefinition.isNotNull();
		if (StringUtils.isNotBlank(searchColumnName)) {
			predicate = predicate.and(qSubExtendedColumnDefinition.columnName.containsIgnoreCase(searchColumnName));
		}

		if (StringUtils.isNotBlank(searchColumnDesc)) {
			predicate = predicate.and(qSubExtendedColumnDefinition.columnDesc.containsIgnoreCase(searchColumnDesc));
		}

		return repository.findAll(predicate, pageable);
	}

	public List<SubExtendedColumnDefinition> findAllKeys() {
		QSubExtendedColumnDefinition qDefinition = QSubExtendedColumnDefinition.subExtendedColumnDefinition;
		return getJPAQueryFactory()
				.selectFrom(qDefinition)
				.where(qDefinition.isKey.equalsIgnoreCase("y"))
				.orderBy(qDefinition.keySeq.asc())
				.fetch();
	}

	@Override
	public List<SubExtendedColumnDefinition> findAll() {
		QSubExtendedColumnDefinition qDefinition = QSubExtendedColumnDefinition.subExtendedColumnDefinition;
		return getJPAQueryFactory()
				.selectFrom(qDefinition)
				.orderBy(qDefinition.isKey.desc(), qDefinition.keySeq.asc(), qDefinition.columnName.asc())
				.fetch();
	}

}
