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

import com.eactive.eai.rms.data.jpa.AbstractEMSDataSerivce;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(transactionManager = "transactionManagerForEMS")
public class ChnlInfoIntegrationService extends AbstractEMSDataSerivce<ChnlInfoIntegrationEntity, ChnlInfoIntegrationId, ChnlInfoIntegrationRepository> {

//    public ChnlInfoIntegrationService(@Qualifier("entityManagerForEMS") EntityManager entityManagerForEMS) {
//        super(entityManagerForEMS);
//    }

    public Page<ChnlInfoIntegrationEntity> selectList(ChnlInfoIntegrationSearch chnlInfoIntegrationSearch, Pageable pageable) {

        QChnlInfoIntegrationEntity qChnlInfoIntegrationEntity = QChnlInfoIntegrationEntity.chnlInfoIntegrationEntity;

        JPAQuery<ChnlInfoIntegrationEntity> jpaQuery = createBaseQuery(chnlInfoIntegrationSearch);

        long totalCount = jpaQuery.select(qChnlInfoIntegrationEntity.chnlInfoIntegrationEntity.count()).fetchOne();

        List<ChnlInfoIntegrationEntity> chnlEnvInfos = jpaQuery
                .select(qChnlInfoIntegrationEntity)
                .limit(pageable.getPageSize())
                .offset(pageable.getOffset())
                .orderBy(qChnlInfoIntegrationEntity.chnlInfoIntegrationId.datadstcd.asc())
                .fetch();

        return new PageImpl<>(chnlEnvInfos, pageable, totalCount);
    }

    public List<ChnlInfoIntegrationEntity> selectList(ChnlInfoIntegrationSearch chnlInfoIntegrationSearch) {

        QChnlInfoIntegrationEntity qChnlInfoIntegrationEntity = QChnlInfoIntegrationEntity.chnlInfoIntegrationEntity;
        JPAQuery<ChnlInfoIntegrationEntity> jpaQuery = createBaseQuery(chnlInfoIntegrationSearch);
        List<ChnlInfoIntegrationEntity> chnlEnvInfos = jpaQuery
                .select(qChnlInfoIntegrationEntity)
                .orderBy(qChnlInfoIntegrationEntity.chnlInfoIntegrationId.datadstcd.asc())
                .fetch();

        return chnlEnvInfos;
    }

    private JPAQuery<ChnlInfoIntegrationEntity> createBaseQuery(ChnlInfoIntegrationSearch chnlInfoIntegrationSearch) {

        QChnlInfoIntegrationEntity qChnlInfoIntegrationEntity = QChnlInfoIntegrationEntity.chnlInfoIntegrationEntity;

        BooleanBuilder predicate = createSelectListPredicate(chnlInfoIntegrationSearch);

        return getJPAQueryFactory()
                .select(qChnlInfoIntegrationEntity)
                .from(qChnlInfoIntegrationEntity)
                .where(predicate);
    }

    public BooleanBuilder createSelectListPredicate(ChnlInfoIntegrationSearch chnlInfoIntegrationSearch) {
        QChnlInfoIntegrationEntity qChnlInfoIntegrationEntity = QChnlInfoIntegrationEntity.chnlInfoIntegrationEntity;
        BooleanBuilder predicate = new BooleanBuilder();

        if(StringUtils.isNotBlank(chnlInfoIntegrationSearch.getSearchDataDstcd())) {
            predicate.and( qChnlInfoIntegrationEntity.chnlInfoIntegrationId.datadstcd.containsIgnoreCase(chnlInfoIntegrationSearch.getSearchDataDstcd()) );
        }

        if(StringUtils.isNotBlank(chnlInfoIntegrationSearch.getServiceType())) {
            predicate.and( qChnlInfoIntegrationEntity.chnlInfoIntegrationId.servicetype.containsIgnoreCase(chnlInfoIntegrationSearch.getServiceType()) );
        }

        if(StringUtils.isNotBlank(chnlInfoIntegrationSearch.getSearchChnnl())) {
            predicate.and( qChnlInfoIntegrationEntity.chnlInfoIntegrationId.codename.containsIgnoreCase(chnlInfoIntegrationSearch.getSearchChnnl()) );
        }

        if(StringUtils.isNotBlank(chnlInfoIntegrationSearch.getSearchQueueName())) {
            predicate.and( qChnlInfoIntegrationEntity.queuename.containsIgnoreCase(chnlInfoIntegrationSearch.getSearchQueueName()) );
        }

        return predicate;
    }

    public List<ChnlInfoIntegrationEntity> selectDetail(ChnlInfoIntegrationSearch chnlInfoIntegrationSearch) {

        QChnlInfoIntegrationEntity qChnlInfoIntegrationEntity = QChnlInfoIntegrationEntity.chnlInfoIntegrationEntity;

        JPAQuery<ChnlInfoIntegrationEntity> jpaQuery = createBaseDetailQuery(chnlInfoIntegrationSearch);

        List<ChnlInfoIntegrationEntity> chnlEnvInfos = jpaQuery
                .select(qChnlInfoIntegrationEntity)
                .orderBy(qChnlInfoIntegrationEntity.chnlInfoIntegrationId.datadstcd.asc())
                .fetch();

        return chnlEnvInfos;
    }

    private JPAQuery<ChnlInfoIntegrationEntity> createBaseDetailQuery(ChnlInfoIntegrationSearch chnlInfoIntegrationSearch) {

        QChnlInfoIntegrationEntity qChnlInfoIntegrationEntity = QChnlInfoIntegrationEntity.chnlInfoIntegrationEntity;

        BooleanBuilder predicate = createSelectDetailPredicate(chnlInfoIntegrationSearch);

        return getJPAQueryFactory()
                .select(qChnlInfoIntegrationEntity)
                .from(qChnlInfoIntegrationEntity)
                .where(predicate);
    }

    public BooleanBuilder createSelectDetailPredicate(ChnlInfoIntegrationSearch chnlInfoIntegrationSearch) {
        QChnlInfoIntegrationEntity qChnlInfoIntegrationEntity = QChnlInfoIntegrationEntity.chnlInfoIntegrationEntity;
        BooleanBuilder predicate = new BooleanBuilder();

        if(StringUtils.isNotBlank(chnlInfoIntegrationSearch.getSearchDataDstcd())) {
            predicate.and( qChnlInfoIntegrationEntity.chnlInfoIntegrationId.datadstcd.containsIgnoreCase(chnlInfoIntegrationSearch.getSearchDataDstcd()) );
        }

        if(StringUtils.isNotBlank(chnlInfoIntegrationSearch.getServiceType())) {
            predicate.and( qChnlInfoIntegrationEntity.chnlInfoIntegrationId.servicetype.containsIgnoreCase(chnlInfoIntegrationSearch.getServiceType()) );
        }

        if(StringUtils.isNotBlank(chnlInfoIntegrationSearch.getSearchChnnl())) {
            predicate.and( qChnlInfoIntegrationEntity.chnlInfoIntegrationId.codename.containsIgnoreCase(chnlInfoIntegrationSearch.getSearchChnnl()) );
        }

        return predicate;
    }

    public void delete(String dataDstcd, String serviceType, String codeName) {
        this.findByIdDataDstcdAndIdServiceType(dataDstcd, serviceType, codeName).forEach(this::delete);
    }

    @Transactional(readOnly = true)
    public List<ChnlInfoIntegrationEntity> findByIdDataDstcdAndIdServiceType(String dataDstcd, String serviceType, String codeName) {
        QChnlInfoIntegrationEntity qChnlInfoIntegrationEntity = QChnlInfoIntegrationEntity.chnlInfoIntegrationEntity;

        BooleanBuilder predicate = createDeletePredicate(dataDstcd, serviceType, codeName);

        JPAQuery<ChnlInfoIntegrationEntity> jpaQuery = getJPAQueryFactory()
                .select(qChnlInfoIntegrationEntity)
                .from(qChnlInfoIntegrationEntity)
                .where(predicate);

        List<ChnlInfoIntegrationEntity> chnlEnvInfos = jpaQuery
                .select(qChnlInfoIntegrationEntity)
                .orderBy(qChnlInfoIntegrationEntity.chnlInfoIntegrationId.datadstcd.asc())
                .fetch();

        return chnlEnvInfos;
    }

    public BooleanBuilder createDeletePredicate(String dataDstcd, String serviceType, String codeName) {
        QChnlInfoIntegrationEntity qChnlInfoIntegrationEntity = QChnlInfoIntegrationEntity.chnlInfoIntegrationEntity;
        BooleanBuilder predicate = new BooleanBuilder();

        if(StringUtils.isNotBlank(dataDstcd)) {
            predicate.and( qChnlInfoIntegrationEntity.chnlInfoIntegrationId.datadstcd.containsIgnoreCase(dataDstcd) );
        }

        if(StringUtils.isNotBlank(serviceType)) {
            predicate.and( qChnlInfoIntegrationEntity.chnlInfoIntegrationId.servicetype.containsIgnoreCase(serviceType) );
        }

        if(StringUtils.isNotBlank(codeName)) {
            predicate.and( qChnlInfoIntegrationEntity.chnlInfoIntegrationId.codename.containsIgnoreCase(codeName) );
        }

        return predicate;
    }

}