package vn.azteam.tpf.service;

import java.util.List;

import javax.persistence.criteria.JoinType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import io.github.jhipster.service.QueryService;

import vn.azteam.tpf.domain.TBExternalTable;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBExternalTableRepository;
import vn.azteam.tpf.repository.search.TBExternalTableSearchRepository;
import vn.azteam.tpf.service.dto.TBExternalTableCriteria;
import vn.azteam.tpf.service.dto.TBExternalTableDTO;
import vn.azteam.tpf.service.mapper.TBExternalTableMapper;

/**
 * Service for executing complex queries for TBExternalTable entities in the database.
 * The main input is a {@link TBExternalTableCriteria} which gets converted to {@link Specification},
 * in a way that all the filters must apply.
 * It returns a {@link List} of {@link TBExternalTableDTO} or a {@link Page} of {@link TBExternalTableDTO} which fulfills the criteria.
 */
@Service
@Transactional(readOnly = true)
public class TBExternalTableQueryService extends QueryService<TBExternalTable> {

    private final Logger log = LoggerFactory.getLogger(TBExternalTableQueryService.class);

    private final TBExternalTableRepository tBExternalTableRepository;

    private final TBExternalTableMapper tBExternalTableMapper;

    private final TBExternalTableSearchRepository tBExternalTableSearchRepository;

    public TBExternalTableQueryService(TBExternalTableRepository tBExternalTableRepository, TBExternalTableMapper tBExternalTableMapper, TBExternalTableSearchRepository tBExternalTableSearchRepository) {
        this.tBExternalTableRepository = tBExternalTableRepository;
        this.tBExternalTableMapper = tBExternalTableMapper;
        this.tBExternalTableSearchRepository = tBExternalTableSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBExternalTableDTO} which matches the criteria from the database
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public List<TBExternalTableDTO> findByCriteria(TBExternalTableCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBExternalTable> specification = createSpecification(criteria);
        return tBExternalTableMapper.toDto(tBExternalTableRepository.findAll(specification));
    }

    /**
     * Return a {@link Page} of {@link TBExternalTableDTO} which matches the criteria from the database
     * @param criteria The object which holds all the filters, which the entities should match.
     * @param page The page, which should be returned.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public Page<TBExternalTableDTO> findByCriteria(TBExternalTableCriteria criteria, Pageable page) {
        log.debug("find by criteria : {}, page: {}", criteria, page);
        final Specification<TBExternalTable> specification = createSpecification(criteria);
        return tBExternalTableRepository.findAll(specification, page)
            .map(tBExternalTableMapper::toDto);
    }

    /**
     * Return the number of matching entities in the database
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the number of matching entities.
     */
    @Transactional(readOnly = true)
    public long countByCriteria(TBExternalTableCriteria criteria) {
        log.debug("count by criteria : {}", criteria);
        final Specification<TBExternalTable> specification = createSpecification(criteria);
        return tBExternalTableRepository.count(specification);
    }

    /**
     * Function to convert TBExternalTableCriteria to a {@link Specification}
     */
    private Specification<TBExternalTable> createSpecification(TBExternalTableCriteria criteria) {
        Specification<TBExternalTable> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBExternalTable_.id));
            }
            if (criteria.getName() != null) {
                specification = specification.and(buildStringSpecification(criteria.getName(), TBExternalTable_.name));
            }
            if (criteria.getDescription() != null) {
                specification = specification.and(buildStringSpecification(criteria.getDescription(), TBExternalTable_.description));
            }
            if (criteria.getForeignKey() != null) {
                specification = specification.and(buildStringSpecification(criteria.getForeignKey(), TBExternalTable_.foreignKey));
            }
            if (criteria.getRelationTable() != null) {
                specification = specification.and(buildStringSpecification(criteria.getRelationTable(), TBExternalTable_.relationTable));
            }
            if (criteria.getObjectType() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getObjectType(), TBExternalTable_.objectType));
            }
        }
        return specification;
    }
}
