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.TBVendor;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBVendorRepository;
import vn.azteam.tpf.repository.search.TBVendorSearchRepository;
import vn.azteam.tpf.service.dto.TBVendorCriteria;
import vn.azteam.tpf.service.dto.TBVendorDTO;
import vn.azteam.tpf.service.mapper.TBVendorMapper;

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

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

    private final TBVendorRepository tBVendorRepository;

    private final TBVendorMapper tBVendorMapper;

    private final TBVendorSearchRepository tBVendorSearchRepository;

    public TBVendorQueryService(TBVendorRepository tBVendorRepository, TBVendorMapper tBVendorMapper, TBVendorSearchRepository tBVendorSearchRepository) {
        this.tBVendorRepository = tBVendorRepository;
        this.tBVendorMapper = tBVendorMapper;
        this.tBVendorSearchRepository = tBVendorSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBVendorDTO} 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<TBVendorDTO> findByCriteria(TBVendorCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBVendor> specification = createSpecification(criteria);
        return tBVendorMapper.toDto(tBVendorRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBVendorCriteria to a {@link Specification}
     */
    private Specification<TBVendor> createSpecification(TBVendorCriteria criteria) {
        Specification<TBVendor> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBVendor_.id));
            }
            if (criteria.getName() != null) {
                specification = specification.and(buildStringSpecification(criteria.getName(), TBVendor_.name));
            }
            if (criteria.getPhone() != null) {
                specification = specification.and(buildStringSpecification(criteria.getPhone(), TBVendor_.phone));
            }
            if (criteria.getEmail() != null) {
                specification = specification.and(buildStringSpecification(criteria.getEmail(), TBVendor_.email));
            }
            if (criteria.getDescription() != null) {
                specification = specification.and(buildStringSpecification(criteria.getDescription(), TBVendor_.description));
            }
            if (criteria.getCreatedDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getCreatedDate(), TBVendor_.createdDate));
            }
            if (criteria.getModifiedDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getModifiedDate(), TBVendor_.modifiedDate));
            }
            if (criteria.getDeletedDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getDeletedDate(), TBVendor_.deletedDate));
            }
            if (criteria.getCreatedById() != null) {
                specification = specification.and(buildSpecification(criteria.getCreatedById(),
                    root -> root.join(TBVendor_.createdBy, JoinType.LEFT).get(TBDetailUser_.id)));
            }
            if (criteria.getModifiedById() != null) {
                specification = specification.and(buildSpecification(criteria.getModifiedById(),
                    root -> root.join(TBVendor_.modifiedBy, JoinType.LEFT).get(TBDetailUser_.id)));
            }
            if (criteria.getDeletedById() != null) {
                specification = specification.and(buildSpecification(criteria.getDeletedById(),
                    root -> root.join(TBVendor_.deletedBy, JoinType.LEFT).get(TBDetailUser_.id)));
            }
            if (criteria.getTbAddressId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbAddressId(),
                    root -> root.join(TBVendor_.tbAddress, JoinType.LEFT).get(TBAddress_.id)));
            }
            if (criteria.getTbCustomerId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbCustomerId(),
                    root -> root.join(TBVendor_.tbCustomer, JoinType.LEFT).get(TBCustomer_.id)));
            }
        }
        return specification;
    }
}
