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.TBGuidelineActivity;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBGuidelineActivityRepository;
import vn.azteam.tpf.repository.search.TBGuidelineActivitySearchRepository;
import vn.azteam.tpf.service.dto.TBGuidelineActivityCriteria;
import vn.azteam.tpf.service.dto.TBGuidelineActivityDTO;
import vn.azteam.tpf.service.mapper.TBGuidelineActivityMapper;

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

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

    private final TBGuidelineActivityRepository tBGuidelineActivityRepository;

    private final TBGuidelineActivityMapper tBGuidelineActivityMapper;

    private final TBGuidelineActivitySearchRepository tBGuidelineActivitySearchRepository;

    public TBGuidelineActivityQueryService(TBGuidelineActivityRepository tBGuidelineActivityRepository, TBGuidelineActivityMapper tBGuidelineActivityMapper, TBGuidelineActivitySearchRepository tBGuidelineActivitySearchRepository) {
        this.tBGuidelineActivityRepository = tBGuidelineActivityRepository;
        this.tBGuidelineActivityMapper = tBGuidelineActivityMapper;
        this.tBGuidelineActivitySearchRepository = tBGuidelineActivitySearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBGuidelineActivityDTO} 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<TBGuidelineActivityDTO> findByCriteria(TBGuidelineActivityCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBGuidelineActivity> specification = createSpecification(criteria);
        return tBGuidelineActivityMapper.toDto(tBGuidelineActivityRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBGuidelineActivityCriteria to a {@link Specification}
     */
    private Specification<TBGuidelineActivity> createSpecification(TBGuidelineActivityCriteria criteria) {
        Specification<TBGuidelineActivity> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBGuidelineActivity_.id));
            }
            if (criteria.getTbGuidelineDetailsId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbGuidelineDetailsId(),
                    root -> root.join(TBGuidelineActivity_.tbGuidelineDetails, JoinType.LEFT).get(TBGuidelineDetails_.id)));
            }
            if (criteria.getTbActivityTypeId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbActivityTypeId(),
                    root -> root.join(TBGuidelineActivity_.tbActivityType, JoinType.LEFT).get(TBActivityType_.id)));
            }
            if (criteria.getExternalTable() != null) {
                specification = specification.and(buildStringSpecification(criteria.getExternalTable(), TBGuidelineActivity_.externalTable));
            }
        }
        return specification;
    }
}
