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.TBProductBlockDetailsReport;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBProductBlockDetailsReportRepository;
import vn.azteam.tpf.repository.search.TBProductBlockDetailsReportSearchRepository;
import vn.azteam.tpf.service.dto.TBProductBlockDetailsReportCriteria;
import vn.azteam.tpf.service.dto.TBProductBlockDetailsReportDTO;
import vn.azteam.tpf.service.mapper.TBProductBlockDetailsReportMapper;

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

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

    private final TBProductBlockDetailsReportRepository tBProductBlockDetailsReportRepository;

    private final TBProductBlockDetailsReportMapper tBProductBlockDetailsReportMapper;

    private final TBProductBlockDetailsReportSearchRepository tBProductBlockDetailsReportSearchRepository;

    public TBProductBlockDetailsReportQueryService(TBProductBlockDetailsReportRepository tBProductBlockDetailsReportRepository, TBProductBlockDetailsReportMapper tBProductBlockDetailsReportMapper, TBProductBlockDetailsReportSearchRepository tBProductBlockDetailsReportSearchRepository) {
        this.tBProductBlockDetailsReportRepository = tBProductBlockDetailsReportRepository;
        this.tBProductBlockDetailsReportMapper = tBProductBlockDetailsReportMapper;
        this.tBProductBlockDetailsReportSearchRepository = tBProductBlockDetailsReportSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBProductBlockDetailsReportDTO} 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<TBProductBlockDetailsReportDTO> findByCriteria(TBProductBlockDetailsReportCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBProductBlockDetailsReport> specification = createSpecification(criteria);
        return tBProductBlockDetailsReportMapper.toDto(tBProductBlockDetailsReportRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBProductBlockDetailsReportCriteria to a {@link Specification}
     */
    private Specification<TBProductBlockDetailsReport> createSpecification(TBProductBlockDetailsReportCriteria criteria) {
        Specification<TBProductBlockDetailsReport> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBProductBlockDetailsReport_.id));
            }
            if (criteria.getCode() != null) {
                specification = specification.and(buildStringSpecification(criteria.getCode(), TBProductBlockDetailsReport_.code));
            }
            if (criteria.getProductLevel1() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getProductLevel1(), TBProductBlockDetailsReport_.productLevel1));
            }
            if (criteria.getProductLevel2() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getProductLevel2(), TBProductBlockDetailsReport_.productLevel2));
            }
            if (criteria.getProductLevel3() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getProductLevel3(), TBProductBlockDetailsReport_.productLevel3));
            }
            if (criteria.getCreateDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getCreateDate(), TBProductBlockDetailsReport_.createDate));
            }
            if (criteria.getTbProductBlockDetailsId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbProductBlockDetailsId(),
                    root -> root.join(TBProductBlockDetailsReport_.tbProductBlockDetails, JoinType.LEFT).get(TBProductBlockDetails_.id)));
            }
            if (criteria.getTbCropId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbCropId(),
                    root -> root.join(TBProductBlockDetailsReport_.tbCrop, JoinType.LEFT).get(TBCrop_.id)));
            }
        }
        return specification;
    }
}
