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.TBCommand;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBCommandRepository;
import vn.azteam.tpf.repository.search.TBCommandSearchRepository;
import vn.azteam.tpf.service.dto.TBCommandCriteria;
import vn.azteam.tpf.service.dto.TBCommandDTO;
import vn.azteam.tpf.service.mapper.TBCommandMapper;

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

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

    private final TBCommandRepository tBCommandRepository;

    private final TBCommandMapper tBCommandMapper;

    private final TBCommandSearchRepository tBCommandSearchRepository;

    public TBCommandQueryService(TBCommandRepository tBCommandRepository, TBCommandMapper tBCommandMapper, TBCommandSearchRepository tBCommandSearchRepository) {
        this.tBCommandRepository = tBCommandRepository;
        this.tBCommandMapper = tBCommandMapper;
        this.tBCommandSearchRepository = tBCommandSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBCommandDTO} 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<TBCommandDTO> findByCriteria(TBCommandCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBCommand> specification = createSpecification(criteria);
        return tBCommandMapper.toDto(tBCommandRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBCommandCriteria to a {@link Specification}
     */
    private Specification<TBCommand> createSpecification(TBCommandCriteria criteria) {
        Specification<TBCommand> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBCommand_.id));
            }
            if (criteria.getName() != null) {
                specification = specification.and(buildStringSpecification(criteria.getName(), TBCommand_.name));
            }
            if (criteria.getKey() != null) {
                specification = specification.and(buildStringSpecification(criteria.getKey(), TBCommand_.key));
            }
            if (criteria.getDescription() != null) {
                specification = specification.and(buildStringSpecification(criteria.getDescription(), TBCommand_.description));
            }
        }
        return specification;
    }
}
