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.TBProductUnit;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBProductUnitRepository;
import vn.azteam.tpf.repository.search.TBProductUnitSearchRepository;
import vn.azteam.tpf.service.dto.TBProductUnitCriteria;
import vn.azteam.tpf.service.dto.TBProductUnitDTO;
import vn.azteam.tpf.service.mapper.TBProductUnitMapper;

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

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

    private final TBProductUnitRepository tBProductUnitRepository;

    private final TBProductUnitMapper tBProductUnitMapper;

    private final TBProductUnitSearchRepository tBProductUnitSearchRepository;

    public TBProductUnitQueryService(TBProductUnitRepository tBProductUnitRepository, TBProductUnitMapper tBProductUnitMapper, TBProductUnitSearchRepository tBProductUnitSearchRepository) {
        this.tBProductUnitRepository = tBProductUnitRepository;
        this.tBProductUnitMapper = tBProductUnitMapper;
        this.tBProductUnitSearchRepository = tBProductUnitSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBProductUnitDTO} 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<TBProductUnitDTO> findByCriteria(TBProductUnitCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBProductUnit> specification = createSpecification(criteria);
        return tBProductUnitMapper.toDto(tBProductUnitRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBProductUnitCriteria to a {@link Specification}
     */
    private Specification<TBProductUnit> createSpecification(TBProductUnitCriteria criteria) {
        Specification<TBProductUnit> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBProductUnit_.id));
            }
            if (criteria.getKey() != null) {
                specification = specification.and(buildStringSpecification(criteria.getKey(), TBProductUnit_.key));
            }
            if (criteria.getName() != null) {
                specification = specification.and(buildStringSpecification(criteria.getName(), TBProductUnit_.name));
            }
        }
        return specification;
    }
}
