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.TBBlockDetailsReport;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBBlockDetailsReportRepository;
import vn.azteam.tpf.repository.search.TBBlockDetailsReportSearchRepository;
import vn.azteam.tpf.service.dto.TBBlockDetailsReportCriteria;
import vn.azteam.tpf.service.dto.TBBlockDetailsReportDTO;
import vn.azteam.tpf.service.mapper.TBBlockDetailsReportMapper;

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

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

    private final TBBlockDetailsReportRepository tBBlockDetailsReportRepository;

    private final TBBlockDetailsReportMapper tBBlockDetailsReportMapper;

    private final TBBlockDetailsReportSearchRepository tBBlockDetailsReportSearchRepository;

    public TBBlockDetailsReportQueryService(TBBlockDetailsReportRepository tBBlockDetailsReportRepository, TBBlockDetailsReportMapper tBBlockDetailsReportMapper, TBBlockDetailsReportSearchRepository tBBlockDetailsReportSearchRepository) {
        this.tBBlockDetailsReportRepository = tBBlockDetailsReportRepository;
        this.tBBlockDetailsReportMapper = tBBlockDetailsReportMapper;
        this.tBBlockDetailsReportSearchRepository = tBBlockDetailsReportSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBBlockDetailsReportDTO} 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<TBBlockDetailsReportDTO> findByCriteria(TBBlockDetailsReportCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBBlockDetailsReport> specification = createSpecification(criteria);
        return tBBlockDetailsReportMapper.toDto(tBBlockDetailsReportRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBBlockDetailsReportCriteria to a {@link Specification}
     */
    private Specification<TBBlockDetailsReport> createSpecification(TBBlockDetailsReportCriteria criteria) {
        Specification<TBBlockDetailsReport> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBBlockDetailsReport_.id));
            }
            if (criteria.getCode() != null) {
                specification = specification.and(buildStringSpecification(criteria.getCode(), TBBlockDetailsReport_.code));
            }
            if (criteria.getQuantity() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getQuantity(), TBBlockDetailsReport_.quantity));
            }
            if (criteria.getCreateDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getCreateDate(), TBBlockDetailsReport_.createDate));
            }
            if (criteria.getTbBlockDetailsId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbBlockDetailsId(),
                    root -> root.join(TBBlockDetailsReport_.tbBlockDetails, JoinType.LEFT).get(TBBlockDetails_.id)));
            }
            if (criteria.getTbSuppliesTypeId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbSuppliesTypeId(),
                    root -> root.join(TBBlockDetailsReport_.tbSuppliesType, JoinType.LEFT).get(TBSuppliesType_.id)));
            }
            if (criteria.getTbSuppliesInWarehouseId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbSuppliesInWarehouseId(),
                    root -> root.join(TBBlockDetailsReport_.tbSuppliesInWarehouse, JoinType.LEFT).get(TBSuppliesInWarehouse_.id)));
            }
        }
        return specification;
    }
}
