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.TBLocationOfCustomer;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBLocationOfCustomerRepository;
import vn.azteam.tpf.repository.search.TBLocationOfCustomerSearchRepository;
import vn.azteam.tpf.service.dto.TBLocationOfCustomerCriteria;
import vn.azteam.tpf.service.dto.TBLocationOfCustomerDTO;
import vn.azteam.tpf.service.mapper.TBLocationOfCustomerMapper;

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

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

    private final TBLocationOfCustomerRepository tBLocationOfCustomerRepository;

    private final TBLocationOfCustomerMapper tBLocationOfCustomerMapper;

    private final TBLocationOfCustomerSearchRepository tBLocationOfCustomerSearchRepository;

    public TBLocationOfCustomerQueryService(TBLocationOfCustomerRepository tBLocationOfCustomerRepository, TBLocationOfCustomerMapper tBLocationOfCustomerMapper, TBLocationOfCustomerSearchRepository tBLocationOfCustomerSearchRepository) {
        this.tBLocationOfCustomerRepository = tBLocationOfCustomerRepository;
        this.tBLocationOfCustomerMapper = tBLocationOfCustomerMapper;
        this.tBLocationOfCustomerSearchRepository = tBLocationOfCustomerSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBLocationOfCustomerDTO} 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<TBLocationOfCustomerDTO> findByCriteria(TBLocationOfCustomerCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBLocationOfCustomer> specification = createSpecification(criteria);
        return tBLocationOfCustomerMapper.toDto(tBLocationOfCustomerRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBLocationOfCustomerCriteria to a {@link Specification}
     */
    private Specification<TBLocationOfCustomer> createSpecification(TBLocationOfCustomerCriteria criteria) {
        Specification<TBLocationOfCustomer> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBLocationOfCustomer_.id));
            }
            if (criteria.getTbCustomerId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbCustomerId(),
                    root -> root.join(TBLocationOfCustomer_.tbCustomer, JoinType.LEFT).get(TBCustomer_.id)));
            }
            if (criteria.getLatitude() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getLatitude(), TBLocationOfCustomer_.latitude));
            }
            if (criteria.getLongitude() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getLongitude(), TBLocationOfCustomer_.longitude));
            }
        }
        return specification;
    }
}
