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.TBLimits;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBLimitsRepository;
import vn.azteam.tpf.repository.search.TBLimitsSearchRepository;
import vn.azteam.tpf.service.dto.TBLimitsCriteria;
import vn.azteam.tpf.service.dto.TBLimitsDTO;
import vn.azteam.tpf.service.mapper.TBLimitsMapper;

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

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

    private final TBLimitsRepository tBLimitsRepository;

    private final TBLimitsMapper tBLimitsMapper;

    private final TBLimitsSearchRepository tBLimitsSearchRepository;

    public TBLimitsQueryService(TBLimitsRepository tBLimitsRepository, TBLimitsMapper tBLimitsMapper, TBLimitsSearchRepository tBLimitsSearchRepository) {
        this.tBLimitsRepository = tBLimitsRepository;
        this.tBLimitsMapper = tBLimitsMapper;
        this.tBLimitsSearchRepository = tBLimitsSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBLimitsDTO} 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<TBLimitsDTO> findByCriteria(TBLimitsCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBLimits> specification = createSpecification(criteria);
        return tBLimitsMapper.toDto(tBLimitsRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBLimitsCriteria to a {@link Specification}
     */
    private Specification<TBLimits> createSpecification(TBLimitsCriteria criteria) {
        Specification<TBLimits> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBLimits_.id));
            }
            if (criteria.getUpperLimit() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getUpperLimit(), TBLimits_.upperLimit));
            }
            if (criteria.getLowerLimit() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getLowerLimit(), TBLimits_.lowerLimit));
            }
            if (criteria.getTbGuidelineDetailsId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbGuidelineDetailsId(),
                    root -> root.join(TBLimits_.tbGuidelineDetails, JoinType.LEFT).get(TBGuidelineDetails_.id)));
            }
            if (criteria.getTbObjectParameterId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbObjectParameterId(),
                    root -> root.join(TBLimits_.tbObjectParameter, JoinType.LEFT).get(TBObjectParameter_.id)));
            }
            if (criteria.getTbEnvironmentalParameterId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbEnvironmentalParameterId(),
                    root -> root.join(TBLimits_.tbEnvironmentalParameter, JoinType.LEFT).get(TBEnvironmentalParameter_.id)));
            }
        }
        return specification;
    }
}
