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.TBDashboardType;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBDashboardTypeRepository;
import vn.azteam.tpf.repository.search.TBDashboardTypeSearchRepository;
import vn.azteam.tpf.service.dto.TBDashboardTypeCriteria;
import vn.azteam.tpf.service.dto.TBDashboardTypeDTO;
import vn.azteam.tpf.service.mapper.TBDashboardTypeMapper;

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

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

    private final TBDashboardTypeRepository tBDashboardTypeRepository;

    private final TBDashboardTypeMapper tBDashboardTypeMapper;

    private final TBDashboardTypeSearchRepository tBDashboardTypeSearchRepository;

    public TBDashboardTypeQueryService(TBDashboardTypeRepository tBDashboardTypeRepository, TBDashboardTypeMapper tBDashboardTypeMapper, TBDashboardTypeSearchRepository tBDashboardTypeSearchRepository) {
        this.tBDashboardTypeRepository = tBDashboardTypeRepository;
        this.tBDashboardTypeMapper = tBDashboardTypeMapper;
        this.tBDashboardTypeSearchRepository = tBDashboardTypeSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBDashboardTypeDTO} 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<TBDashboardTypeDTO> findByCriteria(TBDashboardTypeCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBDashboardType> specification = createSpecification(criteria);
        return tBDashboardTypeMapper.toDto(tBDashboardTypeRepository.findAll(specification));
    }

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

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