package com.eactive.eai.rms.data.entity.man.scheduler;

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.rms.common.scheduler.ui.SchedulerUISearch;
import com.eactive.eai.rms.data.jpa.AbstractEMSDataSerivce;
import com.querydsl.core.types.dsl.BooleanExpression;

@Service
@Transactional(transactionManager = "transactionManagerForEMS")
public class JobInfoService extends AbstractEMSDataSerivce<JobInfo, String, JobInfoRepository> {

	public Page<JobInfo> findAll(Pageable pageable, SchedulerUISearch schedulerUISearch) {
		QJobInfo qJobInfo = QJobInfo.jobInfo;
		BooleanExpression predicate = QJobInfo.jobInfo.isNotNull();

		if (StringUtils.isNotBlank(schedulerUISearch.getSearchJobName())) {
			predicate = predicate.and(qJobInfo.id.containsIgnoreCase(schedulerUISearch.getSearchJobName()));
		}

		if (StringUtils.isNotBlank(schedulerUISearch.getSearchJobDesc())) {
			predicate = predicate.and(qJobInfo.jobDesc.containsIgnoreCase(schedulerUISearch.getSearchJobDesc()));
		}

		if (StringUtils.isNotBlank(schedulerUISearch.getSearchJobClass())) {
			predicate = predicate.and(qJobInfo.jobClassName.containsIgnoreCase(schedulerUISearch.getSearchJobClass()));
		}

		if (StringUtils.isNotBlank(schedulerUISearch.getSearchJobUseYn())) {
			predicate = predicate.and(qJobInfo.useYn.equalsIgnoreCase(schedulerUISearch.getSearchJobUseYn()));
		}

		if (StringUtils.isNotBlank(schedulerUISearch.getSearchJobInst())) {
			predicate = predicate.and(qJobInfo.instanceName.equalsIgnoreCase(schedulerUISearch.getSearchJobInst()));
		}

		return repository.findAll(predicate, pageable);
	}

}
