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.TBNurseryDetails;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBNurseryDetailsRepository;
import vn.azteam.tpf.repository.search.TBNurseryDetailsSearchRepository;
import vn.azteam.tpf.service.dto.TBNurseryDetailsCriteria;
import vn.azteam.tpf.service.dto.TBNurseryDetailsDTO;
import vn.azteam.tpf.service.mapper.TBNurseryDetailsMapper;

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

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

    private final TBNurseryDetailsRepository tBNurseryDetailsRepository;

    private final TBNurseryDetailsMapper tBNurseryDetailsMapper;

    private final TBNurseryDetailsSearchRepository tBNurseryDetailsSearchRepository;

    public TBNurseryDetailsQueryService(TBNurseryDetailsRepository tBNurseryDetailsRepository, TBNurseryDetailsMapper tBNurseryDetailsMapper, TBNurseryDetailsSearchRepository tBNurseryDetailsSearchRepository) {
        this.tBNurseryDetailsRepository = tBNurseryDetailsRepository;
        this.tBNurseryDetailsMapper = tBNurseryDetailsMapper;
        this.tBNurseryDetailsSearchRepository = tBNurseryDetailsSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBNurseryDetailsDTO} 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<TBNurseryDetailsDTO> findByCriteria(TBNurseryDetailsCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBNurseryDetails> specification = createSpecification(criteria);
        return tBNurseryDetailsMapper.toDto(tBNurseryDetailsRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBNurseryDetailsCriteria to a {@link Specification}
     */
    private Specification<TBNurseryDetails> createSpecification(TBNurseryDetailsCriteria criteria) {
        Specification<TBNurseryDetails> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBNurseryDetails_.id));
            }
            if (criteria.getWorkerName() != null) {
                specification = specification.and(buildStringSpecification(criteria.getWorkerName(), TBNurseryDetails_.workerName));
            }
            if (criteria.getTrayNumber() != null) {
                specification = specification.and(buildStringSpecification(criteria.getTrayNumber(), TBNurseryDetails_.trayNumber));
            }
            if (criteria.getTbActivityId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbActivityId(),
                    root -> root.join(TBNurseryDetails_.tbActivity, JoinType.LEFT).get(TBActivity_.id)));
            }
        }
        return specification;
    }
}
