package com.eactive.eai.rms.data.entity.man.monitoringCode.service;

import java.util.List;

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

import com.eactive.apim.portal.monitoringcode.entity.MonitoringCodeGroup;
import com.eactive.apim.portal.monitoringcode.entity.QMonitoringCodeGroup;
import com.eactive.eai.data.jpa.AbstractDataService;
import com.eactive.eai.rms.common.acl.monitoringCode.UI.MonitoringCodeGroupUISearch;
import com.eactive.eai.rms.data.entity.man.monitoringCode.repository.MonitoringCodeGroupRepository;
import com.querydsl.core.BooleanBuilder;

@Service
@Transactional(transactionManager = "transactionManagerForEMS")
public class MonitoringCodeGroupService
		extends AbstractDataService<MonitoringCodeGroup, String, MonitoringCodeGroupRepository> {

	public List<MonitoringCodeGroup> selectCodeGroupList(MonitoringCodeGroupUISearch search) {
		QMonitoringCodeGroup qMonitoringCodeGroup = QMonitoringCodeGroup.monitoringCodeGroup;
		BooleanBuilder predicate = new BooleanBuilder();

		if (!StringUtils.isBlank(search.getSearchCodeGroup())) {
			predicate.and(qMonitoringCodeGroup.codeGroup.eq(search.getSearchCodeGroup()));
		}
		if (!StringUtils.isBlank(search.getSearchGroupDesc())) {
			predicate.and(qMonitoringCodeGroup.description.containsIgnoreCase(search.getSearchGroupDesc()));
		}
		if (!StringUtils.isBlank(search.getSearchCode()) || !StringUtils.isBlank(search.getSearchCodeName())) {
			predicate
					.and(qMonitoringCodeGroup.monitoringCodes.any().id.code.containsIgnoreCase(search.getSearchCode()));
			predicate.and(
					qMonitoringCodeGroup.monitoringCodes.any().codeName.containsIgnoreCase(search.getSearchCodeName()));
		}

		return (List<MonitoringCodeGroup>) repository.findAll(predicate, Sort.by(Sort.Direction.ASC, "codeGroup"));
	}

	public List<MonitoringCodeGroup> selectCodeGroupList(String searchCodeGroup) {
		QMonitoringCodeGroup qMonitoringCodeGroup = QMonitoringCodeGroup.monitoringCodeGroup;

		return (List<MonitoringCodeGroup>) repository.findAll(qMonitoringCodeGroup.codeGroup.eq(searchCodeGroup), Sort.by(Sort.Direction.ASC, "codeGroup"));
	}

}
