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.TBFunctionGroup;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBFunctionGroupRepository;
import vn.azteam.tpf.repository.search.TBFunctionGroupSearchRepository;
import vn.azteam.tpf.service.dto.TBFunctionGroupCriteria;
import vn.azteam.tpf.service.dto.TBFunctionGroupDTO;
import vn.azteam.tpf.service.mapper.TBFunctionGroupMapper;

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

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

    private final TBFunctionGroupRepository tBFunctionGroupRepository;

    private final TBFunctionGroupMapper tBFunctionGroupMapper;

    private final TBFunctionGroupSearchRepository tBFunctionGroupSearchRepository;

    public TBFunctionGroupQueryService(TBFunctionGroupRepository tBFunctionGroupRepository, TBFunctionGroupMapper tBFunctionGroupMapper, TBFunctionGroupSearchRepository tBFunctionGroupSearchRepository) {
        this.tBFunctionGroupRepository = tBFunctionGroupRepository;
        this.tBFunctionGroupMapper = tBFunctionGroupMapper;
        this.tBFunctionGroupSearchRepository = tBFunctionGroupSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBFunctionGroupDTO} 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<TBFunctionGroupDTO> findByCriteria(TBFunctionGroupCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBFunctionGroup> specification = createSpecification(criteria);
        return tBFunctionGroupMapper.toDto(tBFunctionGroupRepository.findAll(specification));
    }

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

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