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.TBSuppliesOrderDetails;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBSuppliesOrderDetailsRepository;
import vn.azteam.tpf.repository.search.TBSuppliesOrderDetailsSearchRepository;
import vn.azteam.tpf.service.dto.TBSuppliesOrderDetailsCriteria;
import vn.azteam.tpf.service.dto.TBSuppliesOrderDetailsDTO;
import vn.azteam.tpf.service.mapper.TBSuppliesOrderDetailsMapper;

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

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

    private final TBSuppliesOrderDetailsRepository tBSuppliesOrderDetailsRepository;

    private final TBSuppliesOrderDetailsMapper tBSuppliesOrderDetailsMapper;

    private final TBSuppliesOrderDetailsSearchRepository tBSuppliesOrderDetailsSearchRepository;

    public TBSuppliesOrderDetailsQueryService(TBSuppliesOrderDetailsRepository tBSuppliesOrderDetailsRepository, TBSuppliesOrderDetailsMapper tBSuppliesOrderDetailsMapper, TBSuppliesOrderDetailsSearchRepository tBSuppliesOrderDetailsSearchRepository) {
        this.tBSuppliesOrderDetailsRepository = tBSuppliesOrderDetailsRepository;
        this.tBSuppliesOrderDetailsMapper = tBSuppliesOrderDetailsMapper;
        this.tBSuppliesOrderDetailsSearchRepository = tBSuppliesOrderDetailsSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBSuppliesOrderDetailsDTO} 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<TBSuppliesOrderDetailsDTO> findByCriteria(TBSuppliesOrderDetailsCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBSuppliesOrderDetails> specification = createSpecification(criteria);
        return tBSuppliesOrderDetailsMapper.toDto(tBSuppliesOrderDetailsRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBSuppliesOrderDetailsCriteria to a {@link Specification}
     */
    private Specification<TBSuppliesOrderDetails> createSpecification(TBSuppliesOrderDetailsCriteria criteria) {
        Specification<TBSuppliesOrderDetails> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBSuppliesOrderDetails_.id));
            }
            if (criteria.getQuantity() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getQuantity(), TBSuppliesOrderDetails_.quantity));
            }
            if (criteria.getPrice() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getPrice(), TBSuppliesOrderDetails_.price));
            }
            if (criteria.getTbSuppliesId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbSuppliesId(),
                    root -> root.join(TBSuppliesOrderDetails_.tbSupplies, JoinType.LEFT).get(TBSupplies_.id)));
            }
            if (criteria.getTbSuppliesOrderId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbSuppliesOrderId(),
                    root -> root.join(TBSuppliesOrderDetails_.tbSuppliesOrder, JoinType.LEFT).get(TBSuppliesOrder_.id)));
            }
        }
        return specification;
    }
}
