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.TBFunction;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBFunctionRepository;
import vn.azteam.tpf.repository.search.TBFunctionSearchRepository;
import vn.azteam.tpf.service.dto.TBFunctionCriteria;
import vn.azteam.tpf.service.dto.TBFunctionDTO;
import vn.azteam.tpf.service.mapper.TBFunctionMapper;

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

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

    private final TBFunctionRepository tBFunctionRepository;

    private final TBFunctionMapper tBFunctionMapper;

    private final TBFunctionSearchRepository tBFunctionSearchRepository;

    public TBFunctionQueryService(TBFunctionRepository tBFunctionRepository, TBFunctionMapper tBFunctionMapper, TBFunctionSearchRepository tBFunctionSearchRepository) {
        this.tBFunctionRepository = tBFunctionRepository;
        this.tBFunctionMapper = tBFunctionMapper;
        this.tBFunctionSearchRepository = tBFunctionSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBFunctionDTO} 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<TBFunctionDTO> findByCriteria(TBFunctionCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBFunction> specification = createSpecification(criteria);
        return tBFunctionMapper.toDto(tBFunctionRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBFunctionCriteria to a {@link Specification}
     */
    private Specification<TBFunction> createSpecification(TBFunctionCriteria criteria) {
        Specification<TBFunction> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBFunction_.id));
            }
            if (criteria.getName() != null) {
                specification = specification.and(buildStringSpecification(criteria.getName(), TBFunction_.name));
            }
            if (criteria.getTypeRequest() != null) {
                specification = specification.and(buildStringSpecification(criteria.getTypeRequest(), TBFunction_.typeRequest));
            }
            if (criteria.getPath() != null) {
                specification = specification.and(buildStringSpecification(criteria.getPath(), TBFunction_.path));
            }
            if (criteria.getMethod() != null) {
                specification = specification.and(buildStringSpecification(criteria.getMethod(), TBFunction_.method));
            }
            if (criteria.getTbFunctionGroupId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbFunctionGroupId(),
                    root -> root.join(TBFunction_.tbFunctionGroup, JoinType.LEFT).get(TBFunctionGroup_.id)));
            }
        }
        return specification;
    }
}
