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.TBControlType;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBControlTypeRepository;
import vn.azteam.tpf.repository.search.TBControlTypeSearchRepository;
import vn.azteam.tpf.service.dto.TBControlTypeCriteria;
import vn.azteam.tpf.service.dto.TBControlTypeDTO;
import vn.azteam.tpf.service.mapper.TBControlTypeMapper;

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

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

    private final TBControlTypeRepository tBControlTypeRepository;

    private final TBControlTypeMapper tBControlTypeMapper;

    private final TBControlTypeSearchRepository tBControlTypeSearchRepository;

    public TBControlTypeQueryService(TBControlTypeRepository tBControlTypeRepository, TBControlTypeMapper tBControlTypeMapper, TBControlTypeSearchRepository tBControlTypeSearchRepository) {
        this.tBControlTypeRepository = tBControlTypeRepository;
        this.tBControlTypeMapper = tBControlTypeMapper;
        this.tBControlTypeSearchRepository = tBControlTypeSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBControlTypeDTO} 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<TBControlTypeDTO> findByCriteria(TBControlTypeCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBControlType> specification = createSpecification(criteria);
        return tBControlTypeMapper.toDto(tBControlTypeRepository.findAll(specification));
    }

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

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