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.TBGuidelineDetailsObjectUpdate;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBGuidelineDetailsObjectUpdateRepository;
import vn.azteam.tpf.repository.search.TBGuidelineDetailsObjectUpdateSearchRepository;
import vn.azteam.tpf.service.dto.TBGuidelineDetailsObjectUpdateCriteria;
import vn.azteam.tpf.service.dto.TBGuidelineDetailsObjectUpdateDTO;
import vn.azteam.tpf.service.mapper.TBGuidelineDetailsObjectUpdateMapper;

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

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

    private final TBGuidelineDetailsObjectUpdateRepository tBGuidelineDetailsObjectUpdateRepository;

    private final TBGuidelineDetailsObjectUpdateMapper tBGuidelineDetailsObjectUpdateMapper;

    private final TBGuidelineDetailsObjectUpdateSearchRepository tBGuidelineDetailsObjectUpdateSearchRepository;

    public TBGuidelineDetailsObjectUpdateQueryService(TBGuidelineDetailsObjectUpdateRepository tBGuidelineDetailsObjectUpdateRepository, TBGuidelineDetailsObjectUpdateMapper tBGuidelineDetailsObjectUpdateMapper, TBGuidelineDetailsObjectUpdateSearchRepository tBGuidelineDetailsObjectUpdateSearchRepository) {
        this.tBGuidelineDetailsObjectUpdateRepository = tBGuidelineDetailsObjectUpdateRepository;
        this.tBGuidelineDetailsObjectUpdateMapper = tBGuidelineDetailsObjectUpdateMapper;
        this.tBGuidelineDetailsObjectUpdateSearchRepository = tBGuidelineDetailsObjectUpdateSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBGuidelineDetailsObjectUpdateDTO} 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<TBGuidelineDetailsObjectUpdateDTO> findByCriteria(TBGuidelineDetailsObjectUpdateCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBGuidelineDetailsObjectUpdate> specification = createSpecification(criteria);
        return tBGuidelineDetailsObjectUpdateMapper.toDto(tBGuidelineDetailsObjectUpdateRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBGuidelineDetailsObjectUpdateCriteria to a {@link Specification}
     */
    private Specification<TBGuidelineDetailsObjectUpdate> createSpecification(TBGuidelineDetailsObjectUpdateCriteria criteria) {
        Specification<TBGuidelineDetailsObjectUpdate> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBGuidelineDetailsObjectUpdate_.id));
            }
            if (criteria.getIndex() != null) {
                specification = specification.and(buildStringSpecification(criteria.getIndex(), TBGuidelineDetailsObjectUpdate_.index));
            }
            if (criteria.getTbObjectParameterId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbObjectParameterId(),
                    root -> root.join(TBGuidelineDetailsObjectUpdate_.tbObjectParameter, JoinType.LEFT).get(TBObjectParameter_.id)));
            }
            if (criteria.getTbGuidelineActivityId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbGuidelineActivityId(),
                    root -> root.join(TBGuidelineDetailsObjectUpdate_.tbGuidelineActivity, JoinType.LEFT).get(TBGuidelineActivity_.id)));
            }
        }
        return specification;
    }
}
