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.TBSuppliesPriceHistory;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBSuppliesPriceHistoryRepository;
import vn.azteam.tpf.repository.search.TBSuppliesPriceHistorySearchRepository;
import vn.azteam.tpf.service.dto.TBSuppliesPriceHistoryCriteria;
import vn.azteam.tpf.service.dto.TBSuppliesPriceHistoryDTO;
import vn.azteam.tpf.service.mapper.TBSuppliesPriceHistoryMapper;

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

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

    private final TBSuppliesPriceHistoryRepository tBSuppliesPriceHistoryRepository;

    private final TBSuppliesPriceHistoryMapper tBSuppliesPriceHistoryMapper;

    private final TBSuppliesPriceHistorySearchRepository tBSuppliesPriceHistorySearchRepository;

    public TBSuppliesPriceHistoryQueryService(TBSuppliesPriceHistoryRepository tBSuppliesPriceHistoryRepository, TBSuppliesPriceHistoryMapper tBSuppliesPriceHistoryMapper, TBSuppliesPriceHistorySearchRepository tBSuppliesPriceHistorySearchRepository) {
        this.tBSuppliesPriceHistoryRepository = tBSuppliesPriceHistoryRepository;
        this.tBSuppliesPriceHistoryMapper = tBSuppliesPriceHistoryMapper;
        this.tBSuppliesPriceHistorySearchRepository = tBSuppliesPriceHistorySearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBSuppliesPriceHistoryDTO} 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<TBSuppliesPriceHistoryDTO> findByCriteria(TBSuppliesPriceHistoryCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBSuppliesPriceHistory> specification = createSpecification(criteria);
        return tBSuppliesPriceHistoryMapper.toDto(tBSuppliesPriceHistoryRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBSuppliesPriceHistoryCriteria to a {@link Specification}
     */
    private Specification<TBSuppliesPriceHistory> createSpecification(TBSuppliesPriceHistoryCriteria criteria) {
        Specification<TBSuppliesPriceHistory> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBSuppliesPriceHistory_.id));
            }
            if (criteria.getPriceForm() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getPriceForm(), TBSuppliesPriceHistory_.priceForm));
            }
            if (criteria.getPrice() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getPrice(), TBSuppliesPriceHistory_.price));
            }
            if (criteria.getTbSuppliesOrderId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbSuppliesOrderId(),
                    root -> root.join(TBSuppliesPriceHistory_.tbSuppliesOrder, JoinType.LEFT).get(TBSuppliesOrder_.id)));
            }
            if (criteria.getTbSuppliesInWarehouseId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbSuppliesInWarehouseId(),
                    root -> root.join(TBSuppliesPriceHistory_.tbSuppliesInWarehouse, JoinType.LEFT).get(TBSuppliesInWarehouse_.id)));
            }
        }
        return specification;
    }
}
