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


import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
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(transactionManager = "transactionManagerForEMS")
public class MMILogService extends AbstractDataService<MMILog, MMILogId, MMILogRepository> {

	public Page<MMILog> findAll(Pageable pageable, MMILogSearch mmiLogSearch) {
		QMMILog qMmiLog = QMMILog.mMILog;
		BooleanExpression predicate = booleanPredicate(mmiLogSearch, qMmiLog);
		return repository.findAll(predicate, pageable);
	}
	
	public Iterable<MMILog> findAll(MMILogSearch mmiLogSearch) {
		QMMILog qMmiLog = QMMILog.mMILog;
		BooleanExpression predicate = booleanPredicate(mmiLogSearch, qMmiLog);
		return repository.findAll(predicate, Sort.by("id.msgDpstHms").descending());
	}

	private BooleanExpression booleanPredicate(MMILogSearch mmiLogSearch, QMMILog qMmiLog) {//getSearchStartTime, getSearchEndTime 은 long타입
		BooleanExpression predicate = qMmiLog.id.msgDpstHms.between( mmiLogSearch.getSearchStartTime(), mmiLogSearch.getSearchEndTime());
		
		if(StringUtils.isNotBlank(mmiLogSearch.getSearchEaiSvcName())) {
			predicate = predicate.and(qMmiLog.id.eaiSvcName.containsIgnoreCase(mmiLogSearch.getSearchEaiSvcName()));
		}
		
		if(StringUtils.isNotBlank(mmiLogSearch.getSearchEaiSevrDstcd())) {
			predicate = predicate.and(qMmiLog.eaiSevrDstcd.equalsIgnoreCase(mmiLogSearch.getSearchEaiSevrDstcd()));
		}
		
		if(StringUtils.isNotBlank(mmiLogSearch.getSearchCnvsnName())) {
			predicate = predicate.and(qMmiLog.cnvsnName.containsIgnoreCase(mmiLogSearch.getSearchCnvsnName()));
		}
		
		if(StringUtils.isNotBlank(mmiLogSearch.getSearchLoutName())) {
			predicate = predicate.and(qMmiLog.loutName.containsIgnoreCase(mmiLogSearch.getSearchLoutName()));
		}
		
		if(StringUtils.isNotBlank(mmiLogSearch.getSearchInterfaceId())) {
			predicate = predicate.and(qMmiLog.interfaceId.containsIgnoreCase(mmiLogSearch.getSearchInterfaceId()));
		}
		
		if(StringUtils.isNotBlank(mmiLogSearch.getSearchDeployId())) {
			predicate = predicate.and(qMmiLog.deployId.containsIgnoreCase(mmiLogSearch.getSearchDeployId()));
		}
		return predicate;
	}

}
