package vn.azteam.tpf.service;

import java.util.List;

import javax.persistence.criteria.JoinType;

import io.github.jhipster.service.filter.InstantFilter;
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.TBActivityExtendType;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBActivityExtendTypeRepository;
import vn.azteam.tpf.repository.search.TBActivityExtendTypeSearchRepository;
import vn.azteam.tpf.service.dto.TBActivityExtendTypeCriteria;
import vn.azteam.tpf.service.dto.TBActivityExtendTypeDTO;
import vn.azteam.tpf.service.mapper.TBActivityExtendTypeMapper;

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

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

    private final TBActivityExtendTypeRepository tBActivityExtendTypeRepository;

    private final TBActivityExtendTypeMapper tBActivityExtendTypeMapper;

    private final TBActivityExtendTypeSearchRepository tBActivityExtendTypeSearchRepository;

    public TBActivityExtendTypeQueryService(TBActivityExtendTypeRepository tBActivityExtendTypeRepository, TBActivityExtendTypeMapper tBActivityExtendTypeMapper, TBActivityExtendTypeSearchRepository tBActivityExtendTypeSearchRepository) {
        this.tBActivityExtendTypeRepository = tBActivityExtendTypeRepository;
        this.tBActivityExtendTypeMapper = tBActivityExtendTypeMapper;
        this.tBActivityExtendTypeSearchRepository = tBActivityExtendTypeSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBActivityExtendTypeDTO} 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<TBActivityExtendTypeDTO> findByCriteria(TBActivityExtendTypeCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBActivityExtendType> specification = createSpecification(criteria);
        return tBActivityExtendTypeMapper.toDto(tBActivityExtendTypeRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBActivityExtendTypeCriteria to a {@link Specification}
     */
    private Specification<TBActivityExtendType> createSpecification(TBActivityExtendTypeCriteria criteria) {
        Specification<TBActivityExtendType> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBActivityExtendType_.id));
            }
            if (criteria.getName() != null) {
                specification = specification.and(buildStringSpecification(criteria.getName(), TBActivityExtendType_.name));
            }
            if (criteria.getExternalTable() != null) {
                specification = specification.and(buildStringSpecification(criteria.getExternalTable(), TBActivityExtendType_.externalTable));
            }
            if (criteria.getObjectType() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getObjectType(), TBActivityExtendType_.objectType));
            }
            if (criteria.getCondition() != null) {
                specification = specification.and(buildStringSpecification(criteria.getCondition(), TBActivityExtendType_.condition));
            }
            if (criteria.getIsGuidelineUsing() != null) {
                specification = specification.and(buildSpecification(criteria.getIsGuidelineUsing(), TBActivityExtendType_.isGuidelineUsing));
            }
            if (criteria.getActivityTypeId() != null) {
                specification = specification.and(buildSpecification(criteria.getActivityTypeId(),
                    root -> root.join(TBActivityExtendType_.tbActivityType, JoinType.LEFT).get(TBActivityType_.id)));
            }
            if (criteria.getCreatedDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getCreatedDate(), TBActivityExtendType_.createdDate));
            }
            if (criteria.getModifiedDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getModifiedDate(), TBActivityExtendType_.modifiedDate));
            }

            //Filter deletedDate == null
            InstantFilter instantFilter = new InstantFilter();
            instantFilter.setSpecified(criteria.getDeletedDate() != null);
            criteria.setDeletedDate(instantFilter);
            specification = specification.and(buildRangeSpecification(criteria.getDeletedDate(), TBActivityExtendType_.deletedDate));

            if (criteria.getDeletedById() != null) {
                specification = specification.and(buildSpecification(criteria.getDeletedById(),
                    root -> root.join(TBActivityExtendType_.deletedBy, JoinType.LEFT).get(TBDetailUser_.id)));
            }
            if (criteria.getCreatedById() != null) {
                specification = specification.and(buildSpecification(criteria.getCreatedById(),
                    root -> root.join(TBActivityExtendType_.createdBy, JoinType.LEFT).get(TBDetailUser_.id)));
            }
            if (criteria.getModifiedById() != null) {
                specification = specification.and(buildSpecification(criteria.getModifiedById(),
                    root -> root.join(TBActivityExtendType_.modifiedBy, JoinType.LEFT).get(TBDetailUser_.id)));
            }
        }
        return specification;
    }
}
