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.TBSuppliesUsingDetails;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBSuppliesUsingDetailsRepository;
import vn.azteam.tpf.repository.search.TBSuppliesUsingDetailsSearchRepository;
import vn.azteam.tpf.service.dto.TBSuppliesUsingDetailsCriteria;
import vn.azteam.tpf.service.dto.TBSuppliesUsingDetailsDTO;
import vn.azteam.tpf.service.mapper.TBSuppliesUsingDetailsMapper;

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

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

    private final TBSuppliesUsingDetailsRepository tBSuppliesUsingDetailsRepository;

    private final TBSuppliesUsingDetailsMapper tBSuppliesUsingDetailsMapper;

    private final TBSuppliesUsingDetailsSearchRepository tBSuppliesUsingDetailsSearchRepository;

    public TBSuppliesUsingDetailsQueryService(TBSuppliesUsingDetailsRepository tBSuppliesUsingDetailsRepository, TBSuppliesUsingDetailsMapper tBSuppliesUsingDetailsMapper, TBSuppliesUsingDetailsSearchRepository tBSuppliesUsingDetailsSearchRepository) {
        this.tBSuppliesUsingDetailsRepository = tBSuppliesUsingDetailsRepository;
        this.tBSuppliesUsingDetailsMapper = tBSuppliesUsingDetailsMapper;
        this.tBSuppliesUsingDetailsSearchRepository = tBSuppliesUsingDetailsSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBSuppliesUsingDetailsDTO} 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<TBSuppliesUsingDetailsDTO> findByCriteria(TBSuppliesUsingDetailsCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBSuppliesUsingDetails> specification = createSpecification(criteria);
        return tBSuppliesUsingDetailsMapper.toDto(tBSuppliesUsingDetailsRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBSuppliesUsingDetailsCriteria to a {@link Specification}
     */
    private Specification<TBSuppliesUsingDetails> createSpecification(TBSuppliesUsingDetailsCriteria criteria) {
        Specification<TBSuppliesUsingDetails> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBSuppliesUsingDetails_.id));
            }
            if (criteria.getDosage() != null) {
                specification = specification.and(buildStringSpecification(criteria.getDosage(), TBSuppliesUsingDetails_.dosage));
            }
            if (criteria.getQuantity() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getQuantity(), TBSuppliesUsingDetails_.quantity));
            }
            if (criteria.getCost() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getCost(), TBSuppliesUsingDetails_.cost));
            }
            if (criteria.getHowToUse() != null) {
                specification = specification.and(buildStringSpecification(criteria.getHowToUse(), TBSuppliesUsingDetails_.howToUse));
            }
            if (criteria.getTbEquipmentOfCustomerId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbEquipmentOfCustomerId(),
                    root -> root.join(TBSuppliesUsingDetails_.tbEquipmentOfCustomer, JoinType.LEFT).get(TBEquipmentOfCustomer_.id)));
            }
            if (criteria.getTbSuppliesInWarehouseId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbSuppliesInWarehouseId(),
                    root -> root.join(TBSuppliesUsingDetails_.tbSuppliesInWarehouse, JoinType.LEFT).get(TBSuppliesInWarehouse_.id)));
            }
            if (criteria.getTbBlockDetailsId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbBlockDetailsId(),
                    root -> root.join(TBSuppliesUsingDetails_.tbBlockDetails, JoinType.LEFT).get(TBBlockDetails_.id)));
            }
            if (criteria.getTbActivityId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbActivityId(),
                    root -> root.join(TBSuppliesUsingDetails_.tbActivity, JoinType.LEFT).get(TBActivity_.id)));
            }
            if (criteria.getTbGuidelineActivityId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbGuidelineActivityId(),
                    root -> root.join(TBSuppliesUsingDetails_.tbGuidelineActivity, JoinType.LEFT).get(TBGuidelineActivity_.id)));
            }
        }
        return specification;
    }
}
