package vn.azteam.tpf.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;
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.TBEntityLocationOfCustomer;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBEntityLocationOfCustomerRepository;
import vn.azteam.tpf.repository.search.TBEntityLocationOfCustomerSearchRepository;
import vn.azteam.tpf.service.dto.TBEntityLocationOfCustomerCriteria;
import vn.azteam.tpf.service.dto.TBEntityLocationOfCustomerDTO;
import vn.azteam.tpf.service.mapper.TBEntityLocationOfCustomerMapper;

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

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

    private final TBEntityLocationOfCustomerRepository tBEntityLocationOfCustomerRepository;

    private final TBEntityLocationOfCustomerMapper tBEntityLocationOfCustomerMapper;

    private final EntityManager entityManager;

    private final TBEntityLocationOfCustomerSearchRepository tBEntityLocationOfCustomerSearchRepository;

    public TBEntityLocationOfCustomerQueryService(TBEntityLocationOfCustomerRepository tBEntityLocationOfCustomerRepository,
                                                  TBEntityLocationOfCustomerMapper tBEntityLocationOfCustomerMapper,
                                                  EntityManager entityManager,
                                                  TBEntityLocationOfCustomerSearchRepository tBEntityLocationOfCustomerSearchRepository) {
        this.tBEntityLocationOfCustomerRepository = tBEntityLocationOfCustomerRepository;
        this.tBEntityLocationOfCustomerMapper = tBEntityLocationOfCustomerMapper;
        this.entityManager = entityManager;
        this.tBEntityLocationOfCustomerSearchRepository = tBEntityLocationOfCustomerSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBEntityLocationOfCustomerDTO} 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<TBEntityLocationOfCustomerDTO> findByCriteria(TBEntityLocationOfCustomerCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBEntityLocationOfCustomer> specification = createSpecification(criteria);
        return tBEntityLocationOfCustomerMapper.toDto(tBEntityLocationOfCustomerRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBEntityLocationOfCustomerCriteria to a {@link Specification}
     */
    private Specification<TBEntityLocationOfCustomer> createSpecification(TBEntityLocationOfCustomerCriteria criteria) {
        Specification<TBEntityLocationOfCustomer> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBEntityLocationOfCustomer_.id));
            }
            if (criteria.getPixelX() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getPixelX(), TBEntityLocationOfCustomer_.pixelX));
            }
            if (criteria.getPixelY() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getPixelY(), TBEntityLocationOfCustomer_.pixelY));
            }
            if (criteria.getTbCustomerId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbCustomerId(),
                    root -> root.join(TBEntityLocationOfCustomer_.tbCustomer, JoinType.LEFT).get(TBCustomer_.id)));
            }
            if (criteria.getTbEntityId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbEntityId(),
                    root -> root.join(TBEntityLocationOfCustomer_.tbEntity, JoinType.LEFT).get(TBEntity_.id)));
            }
        }
        return specification;
    }

    public List<TBEntityLocationOfCustomerDTO> getListTbEntityOfCustomerByCustomer(Long tbCustomerId) {
        Query query = entityManager.createNativeQuery(
            String.format(
                "select telc.*\n" +
                    " from tb_entity_location_of_customer as telc\n" +
                    " join tb_customer as c\n" +
                    " on telc.tb_customer_id = c.id\n" +
                    " join tb_entity as e\n" +
                    " on telc.tb_entity_id = e.id\n" +
                    " where telc.tb_customer_id = :tbCustomerId and e.jhi_level = 1\n"), TBEntityLocationOfCustomer.class);
        query.setParameter("tbCustomerId", tbCustomerId);

        return tBEntityLocationOfCustomerMapper.toDto(query.getResultList());

    }
}
