package com.eactive.eai.rms.onl.apim.approval.prodclient;

import com.eactive.apim.portal.app.entity.ProdClient;
import com.eactive.apim.portal.app.entity.QProdClient;
import com.eactive.eai.data.jpa.AbstractDataService;
import com.querydsl.core.types.dsl.BooleanExpression;
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;

@Service
@Transactional
public class ProdClientService extends AbstractDataService<ProdClient, String, ProdClientEMSRepository> {

    public Page<ProdClient> findAll(Pageable pageable, ProdClientSearch clientSearch) {
        QProdClient qClientEntity = QProdClient.prodClient;

        BooleanExpression predicate = qClientEntity.clientname.containsIgnoreCase(clientSearch.getSearchClientName());

        if (StringUtils.isNotBlank(clientSearch.getSearchClientId())) {
            predicate = predicate.and(qClientEntity.clientid.containsIgnoreCase(clientSearch.getSearchClientId()));
        }

        if (StringUtils.isNotBlank(clientSearch.getSearchOrgId())) {
            predicate = predicate.and(qClientEntity.orgid.eq(clientSearch.getSearchOrgId()));
        }

        return repository.findAll(predicate, pageable);
    }
}
