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.TBPermission;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBPermissionRepository;
import vn.azteam.tpf.repository.search.TBPermissionSearchRepository;
import vn.azteam.tpf.service.dto.TBPermissionCriteria;
import vn.azteam.tpf.service.dto.TBPermissionDTO;
import vn.azteam.tpf.service.mapper.TBPermissionMapper;

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

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

    private final TBPermissionRepository tBPermissionRepository;

    private final TBPermissionMapper tBPermissionMapper;

    private final TBPermissionSearchRepository tBPermissionSearchRepository;

    public TBPermissionQueryService(TBPermissionRepository tBPermissionRepository, TBPermissionMapper tBPermissionMapper, TBPermissionSearchRepository tBPermissionSearchRepository) {
        this.tBPermissionRepository = tBPermissionRepository;
        this.tBPermissionMapper = tBPermissionMapper;
        this.tBPermissionSearchRepository = tBPermissionSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBPermissionDTO} 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<TBPermissionDTO> findByCriteria(TBPermissionCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBPermission> specification = createSpecification(criteria);
        return tBPermissionMapper.toDto(tBPermissionRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBPermissionCriteria to a {@link Specification}
     */
    private Specification<TBPermission> createSpecification(TBPermissionCriteria criteria) {
        Specification<TBPermission> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBPermission_.id));
            }
            if (criteria.getHasPermission() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getHasPermission(), TBPermission_.hasPermission));
            }
            if (criteria.getCreatedDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getCreatedDate(), TBPermission_.createdDate));
            }
            if (criteria.getModifiedDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getModifiedDate(), TBPermission_.modifiedDate));
            }
            if (criteria.getDeletedDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getDeletedDate(), TBPermission_.deletedDate));
            }
            if (criteria.getTbFunctionId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbFunctionId(),
                    root -> root.join(TBPermission_.tbFunction, JoinType.LEFT).get(TBFunction_.id)));
            }
            if (criteria.getTbRoleId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbRoleId(),
                    root -> root.join(TBPermission_.tbRole, JoinType.LEFT).get(TBRole_.id)));
            }
            if (criteria.getCreatedById() != null) {
                specification = specification.and(buildSpecification(criteria.getCreatedById(),
                    root -> root.join(TBPermission_.createdBy, JoinType.LEFT).get(TBDetailUser_.id)));
            }
            if (criteria.getModifiedById() != null) {
                specification = specification.and(buildSpecification(criteria.getModifiedById(),
                    root -> root.join(TBPermission_.modifiedBy, JoinType.LEFT).get(TBDetailUser_.id)));
            }
            if (criteria.getDeletedById() != null) {
                specification = specification.and(buildSpecification(criteria.getDeletedById(),
                    root -> root.join(TBPermission_.deletedBy, JoinType.LEFT).get(TBDetailUser_.id)));
            }
        }
        return specification;
    }
}
