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

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.eai.data.jpa.AbstractDataService;
import com.querydsl.core.BooleanBuilder;

@Service
@Transactional
public class CodeGroupService extends AbstractDataService<CodeGroup, String, CodeGroupRepository> {

	public List<CodeGroup> selectCodeGroupList(String searchName) {
		QCodeGroup qCodeGroup = QCodeGroup.codeGroup;
		BooleanBuilder predicate = new BooleanBuilder();
		
		if (StringUtils.isNotBlank(searchName)) {
			predicate.and(qCodeGroup.codegroup.containsIgnoreCase(searchName))
					 .or(qCodeGroup.codes.any().id.code.containsIgnoreCase(searchName))
					 .or(qCodeGroup.codes.any().codename.containsIgnoreCase(searchName))
					 .or(qCodeGroup.codes.any().codenameEn.containsIgnoreCase(searchName));
			}
		return (List<CodeGroup>) repository.findAll(predicate, Sort.by(Sort.Direction.ASC, "codegroup"));
	}

}
