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.TBEntityType;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBEntityTypeRepository;
import vn.azteam.tpf.repository.search.TBEntityTypeSearchRepository;
import vn.azteam.tpf.service.dto.TBEntityTypeCriteria;
import vn.azteam.tpf.service.dto.TBEntityTypeDTO;
import vn.azteam.tpf.service.mapper.TBEntityTypeMapper;

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

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

    private final TBEntityTypeRepository tBEntityTypeRepository;

    private final TBEntityTypeMapper tBEntityTypeMapper;

    private final TBEntityTypeSearchRepository tBEntityTypeSearchRepository;

    public TBEntityTypeQueryService(TBEntityTypeRepository tBEntityTypeRepository, TBEntityTypeMapper tBEntityTypeMapper, TBEntityTypeSearchRepository tBEntityTypeSearchRepository) {
        this.tBEntityTypeRepository = tBEntityTypeRepository;
        this.tBEntityTypeMapper = tBEntityTypeMapper;
        this.tBEntityTypeSearchRepository = tBEntityTypeSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBEntityTypeDTO} 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<TBEntityTypeDTO> findByCriteria(TBEntityTypeCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBEntityType> specification = createSpecification(criteria);
        return tBEntityTypeMapper.toDto(tBEntityTypeRepository.findAll(specification));
    }

    /**
     * Return a {@link Page} of {@link TBEntityTypeDTO} 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<TBEntityTypeDTO> findByCriteria(TBEntityTypeCriteria criteria, Pageable page) {
        log.debug("find by criteria : {}, page: {}", criteria, page);
        final Specification<TBEntityType> specification = createSpecification(criteria);
        return tBEntityTypeRepository.findAll(specification, page)
            .map(tBEntityTypeMapper::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(TBEntityTypeCriteria criteria) {
        log.debug("count by criteria : {}", criteria);
        final Specification<TBEntityType> specification = createSpecification(criteria);
        return tBEntityTypeRepository.count(specification);
    }

    /**
     * Function to convert TBEntityTypeCriteria to a {@link Specification}
     */
    private Specification<TBEntityType> createSpecification(TBEntityTypeCriteria criteria) {
        Specification<TBEntityType> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBEntityType_.id));
            }
            if (criteria.getName() != null) {
                specification = specification.and(buildStringSpecification(criteria.getName(), TBEntityType_.name));
            }
        }
        return specification;
    }
}
