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.TBWaterType;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBWaterTypeRepository;
import vn.azteam.tpf.repository.search.TBWaterTypeSearchRepository;
import vn.azteam.tpf.service.dto.TBWaterTypeCriteria;
import vn.azteam.tpf.service.dto.TBWaterTypeDTO;
import vn.azteam.tpf.service.mapper.TBWaterTypeMapper;

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

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

    private final TBWaterTypeRepository tBWaterTypeRepository;

    private final TBWaterTypeMapper tBWaterTypeMapper;

    private final TBWaterTypeSearchRepository tBWaterTypeSearchRepository;

    public TBWaterTypeQueryService(TBWaterTypeRepository tBWaterTypeRepository, TBWaterTypeMapper tBWaterTypeMapper, TBWaterTypeSearchRepository tBWaterTypeSearchRepository) {
        this.tBWaterTypeRepository = tBWaterTypeRepository;
        this.tBWaterTypeMapper = tBWaterTypeMapper;
        this.tBWaterTypeSearchRepository = tBWaterTypeSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBWaterTypeDTO} 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<TBWaterTypeDTO> findByCriteria(TBWaterTypeCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBWaterType> specification = createSpecification(criteria);
        return tBWaterTypeMapper.toDto(tBWaterTypeRepository.findAll(specification));
    }

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

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