package vn.azteam.tpf.service;

import java.util.List;
import java.util.Optional;

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.TBDetailUser;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBDetailUserRepository;
import vn.azteam.tpf.repository.search.TBDetailUserSearchRepository;
import vn.azteam.tpf.service.dto.TBDetailUserCriteria;
import vn.azteam.tpf.service.dto.TBDetailUserDTO;
import vn.azteam.tpf.service.mapper.TBDetailUserMapper;

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

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

    private final TBDetailUserRepository tBDetailUserRepository;

    private final TBDetailUserMapper tBDetailUserMapper;

    private final TBDetailUserSearchRepository tBDetailUserSearchRepository;

    public TBDetailUserQueryService(TBDetailUserRepository tBDetailUserRepository, TBDetailUserMapper tBDetailUserMapper, TBDetailUserSearchRepository tBDetailUserSearchRepository) {
        this.tBDetailUserRepository = tBDetailUserRepository;
        this.tBDetailUserMapper = tBDetailUserMapper;
        this.tBDetailUserSearchRepository = tBDetailUserSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBDetailUserDTO} 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<TBDetailUserDTO> findByCriteria(TBDetailUserCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBDetailUser> specification = createSpecification(criteria);
        return tBDetailUserMapper.toDto(tBDetailUserRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBDetailUserCriteria to a {@link Specification}
     */
    private Specification<TBDetailUser> createSpecification(TBDetailUserCriteria criteria) {
        Specification<TBDetailUser> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBDetailUser_.id));
            }
            if (criteria.getFullName() != null) {
                specification = specification.and(buildStringSpecification(criteria.getFullName(), TBDetailUser_.fullName));
            }
            if (criteria.getFcmToken() != null) {
                specification = specification.and(buildStringSpecification(criteria.getFcmToken(), TBDetailUser_.fcmToken));
            }
            if (criteria.getPhone() != null) {
                specification = specification.and(buildStringSpecification(criteria.getPhone(), TBDetailUser_.phone));
            }
            if (criteria.getBirthday() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getBirthday(), TBDetailUser_.birthday));
            }
            if (criteria.getGender() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getGender(), TBDetailUser_.gender));
            }
            if (criteria.getTbRoleId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbRoleId(),
                    root -> root.join(TBDetailUser_.tbRole, JoinType.LEFT).get(TBRole_.id)));
            }
            if (criteria.getTbCustomerId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbCustomerId(),
                    root -> root.join(TBDetailUser_.tbCustomer, JoinType.LEFT).get(TBCustomer_.id)));
            }
            if (criteria.getTbAddressId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbAddressId(),
                    root -> root.join(TBDetailUser_.tbAddress, JoinType.LEFT).get(TBAddress_.id)));
            }
            if (criteria.getTbCropId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbCropId(),
                    root -> root.join(TBDetailUser_.tbCrops, JoinType.LEFT).get(TBCrop_.id)));
            }
        }
        return specification;
    }

    /**
     * Return a {@link Page} of {@link TBDetailUserDTO} which matches the criteria from the database
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public Optional<TBDetailUser> findOneByPhone(String phone) {
        return tBDetailUserRepository.findOneByPhone(phone);
    }

}
