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.TBRole;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBRoleRepository;
import vn.azteam.tpf.repository.search.TBRoleSearchRepository;
import vn.azteam.tpf.service.dto.TBRoleCriteria;
import vn.azteam.tpf.service.dto.TBRoleDTO;
import vn.azteam.tpf.service.mapper.TBRoleMapper;

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

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

    private final TBRoleRepository tBRoleRepository;

    private final TBRoleMapper tBRoleMapper;

    private final TBRoleSearchRepository tBRoleSearchRepository;

    public TBRoleQueryService(TBRoleRepository tBRoleRepository, TBRoleMapper tBRoleMapper, TBRoleSearchRepository tBRoleSearchRepository) {
        this.tBRoleRepository = tBRoleRepository;
        this.tBRoleMapper = tBRoleMapper;
        this.tBRoleSearchRepository = tBRoleSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBRoleDTO} 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<TBRoleDTO> findByCriteria(TBRoleCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBRole> specification = createSpecification(criteria);
        return tBRoleMapper.toDto(tBRoleRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBRoleCriteria to a {@link Specification}
     */
    private Specification<TBRole> createSpecification(TBRoleCriteria criteria) {
        Specification<TBRole> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBRole_.id));
            }
            if (criteria.getName() != null) {
                specification = specification.and(buildStringSpecification(criteria.getName(), TBRole_.name));
            }
            if (criteria.getIsAllCustomer() != null) {
                specification = specification.and(buildSpecification(criteria.getIsAllCustomer(), TBRole_.isAllCustomer));
            }
            if (criteria.getIsAllCrop() != null) {
                specification = specification.and(buildSpecification(criteria.getIsAllCrop(), TBRole_.isAllCrop));
            }
            if (criteria.getRoleLevel() != null) {
                specification = specification.and(buildSpecification(criteria.getRoleLevel(), TBRole_.roleLevel));
            }
            if (criteria.getDescription() != null) {
                specification = specification.and(buildStringSpecification(criteria.getDescription(), TBRole_.description));
            }
            if (criteria.getCreatedDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getCreatedDate(), TBRole_.createdDate));
            }
            if (criteria.getModifiedDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getModifiedDate(), TBRole_.modifiedDate));
            }
            if (criteria.getTbCustomerId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbCustomerId(),
                    root -> root.join(TBRole_.tbCustomer, JoinType.LEFT).get(TBCustomer_.id)));
            }
            if (criteria.getCreatedById() != null) {
                specification = specification.and(buildSpecification(criteria.getCreatedById(),
                    root -> root.join(TBRole_.createdBy, JoinType.LEFT).get(TBDetailUser_.id)));
            }
            if (criteria.getCreatedById() != null) {
                specification = specification.and(buildSpecification(criteria.getCreatedById(),
                    root -> root.join(TBRole_.createdBy, JoinType.LEFT).get(TBDetailUser_.id)));
            }
            if (criteria.getModifiedById() != null) {
                specification = specification.and(buildSpecification(criteria.getModifiedById(),
                    root -> root.join(TBRole_.modifiedBy, JoinType.LEFT).get(TBDetailUser_.id)));
            }
            if (criteria.getDeletedById() != null) {
                specification = specification.and(buildSpecification(criteria.getDeletedById(),
                    root -> root.join(TBRole_.deletedBy, JoinType.LEFT).get(TBDetailUser_.id)));
            }
        }
        return specification;
    }
}
