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.TBSuppliesOrder;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBSuppliesOrderRepository;
import vn.azteam.tpf.repository.search.TBSuppliesOrderSearchRepository;
import vn.azteam.tpf.service.dto.TBSuppliesOrderCriteria;
import vn.azteam.tpf.service.dto.TBSuppliesOrderDTO;
import vn.azteam.tpf.service.mapper.TBSuppliesOrderMapper;

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

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

    private final TBSuppliesOrderRepository tBSuppliesOrderRepository;

    private final TBSuppliesOrderMapper tBSuppliesOrderMapper;

    private final TBSuppliesOrderSearchRepository tBSuppliesOrderSearchRepository;

    public TBSuppliesOrderQueryService(TBSuppliesOrderRepository tBSuppliesOrderRepository, TBSuppliesOrderMapper tBSuppliesOrderMapper, TBSuppliesOrderSearchRepository tBSuppliesOrderSearchRepository) {
        this.tBSuppliesOrderRepository = tBSuppliesOrderRepository;
        this.tBSuppliesOrderMapper = tBSuppliesOrderMapper;
        this.tBSuppliesOrderSearchRepository = tBSuppliesOrderSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBSuppliesOrderDTO} 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<TBSuppliesOrderDTO> findByCriteria(TBSuppliesOrderCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBSuppliesOrder> specification = createSpecification(criteria);
        return tBSuppliesOrderMapper.toDto(tBSuppliesOrderRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBSuppliesOrderCriteria to a {@link Specification}
     */
    private Specification<TBSuppliesOrder> createSpecification(TBSuppliesOrderCriteria criteria) {
        Specification<TBSuppliesOrder> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBSuppliesOrder_.id));
            }
            if (criteria.getStatus() != null) {
                specification = specification.and(buildStringSpecification(criteria.getStatus(), TBSuppliesOrder_.status));
            }
            if (criteria.getOrderDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getOrderDate(), TBSuppliesOrder_.orderDate));
            }
            if (criteria.getReceiveDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getReceiveDate(), TBSuppliesOrder_.receiveDate));
            }
            if (criteria.getTotalPrice() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getTotalPrice(), TBSuppliesOrder_.totalPrice));
            }
            if (criteria.getDescription() != null) {
                specification = specification.and(buildStringSpecification(criteria.getDescription(), TBSuppliesOrder_.description));
            }
            if (criteria.getCreatedDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getCreatedDate(), TBSuppliesOrder_.createdDate));
            }
            if (criteria.getModifiedDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getModifiedDate(), TBSuppliesOrder_.modifiedDate));
            }
            if (criteria.getDeletedDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getDeletedDate(), TBSuppliesOrder_.deletedDate));
            }
            if (criteria.getTbVendorId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbVendorId(),
                    root -> root.join(TBSuppliesOrder_.tbVendor, JoinType.LEFT).get(TBVendor_.id)));
            }
            if (criteria.getCreatedById() != null) {
                specification = specification.and(buildSpecification(criteria.getCreatedById(),
                    root -> root.join(TBSuppliesOrder_.createdBy, JoinType.LEFT).get(TBDetailUser_.id)));
            }
            if (criteria.getModifiedById() != null) {
                specification = specification.and(buildSpecification(criteria.getModifiedById(),
                    root -> root.join(TBSuppliesOrder_.modifiedBy, JoinType.LEFT).get(TBDetailUser_.id)));
            }
            if (criteria.getDeletedById() != null) {
                specification = specification.and(buildSpecification(criteria.getDeletedById(),
                    root -> root.join(TBSuppliesOrder_.deletedBy, JoinType.LEFT).get(TBDetailUser_.id)));
            }
        }
        return specification;
    }
}
