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.TBMaintenanceDetails;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBMaintenanceDetailsRepository;
import vn.azteam.tpf.repository.search.TBMaintenanceDetailsSearchRepository;
import vn.azteam.tpf.service.dto.TBMaintenanceDetailsCriteria;
import vn.azteam.tpf.service.dto.TBMaintenanceDetailsDTO;
import vn.azteam.tpf.service.mapper.TBMaintenanceDetailsMapper;

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

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

    private final TBMaintenanceDetailsRepository tBMaintenanceDetailsRepository;

    private final TBMaintenanceDetailsMapper tBMaintenanceDetailsMapper;

    private final TBMaintenanceDetailsSearchRepository tBMaintenanceDetailsSearchRepository;

    public TBMaintenanceDetailsQueryService(TBMaintenanceDetailsRepository tBMaintenanceDetailsRepository, TBMaintenanceDetailsMapper tBMaintenanceDetailsMapper, TBMaintenanceDetailsSearchRepository tBMaintenanceDetailsSearchRepository) {
        this.tBMaintenanceDetailsRepository = tBMaintenanceDetailsRepository;
        this.tBMaintenanceDetailsMapper = tBMaintenanceDetailsMapper;
        this.tBMaintenanceDetailsSearchRepository = tBMaintenanceDetailsSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBMaintenanceDetailsDTO} 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<TBMaintenanceDetailsDTO> findByCriteria(TBMaintenanceDetailsCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBMaintenanceDetails> specification = createSpecification(criteria);
        return tBMaintenanceDetailsMapper.toDto(tBMaintenanceDetailsRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBMaintenanceDetailsCriteria to a {@link Specification}
     */
    private Specification<TBMaintenanceDetails> createSpecification(TBMaintenanceDetailsCriteria criteria) {
        Specification<TBMaintenanceDetails> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBMaintenanceDetails_.id));
            }
            if (criteria.getStartTime() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getStartTime(), TBMaintenanceDetails_.startTime));
            }
            if (criteria.getDurationInMins() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getDurationInMins(), TBMaintenanceDetails_.durationInMins));
            }
            if (criteria.getAction() != null) {
                specification = specification.and(buildStringSpecification(criteria.getAction(), TBMaintenanceDetails_.action));
            }
            if (criteria.getDescription() != null) {
                specification = specification.and(buildStringSpecification(criteria.getDescription(), TBMaintenanceDetails_.description));
            }
            if (criteria.getStatus() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getStatus(), TBMaintenanceDetails_.status));
            }
            if (criteria.getCreatedDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getCreatedDate(), TBMaintenanceDetails_.createdDate));
            }
            if (criteria.getModifiedDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getModifiedDate(), TBMaintenanceDetails_.modifiedDate));
            }
            if (criteria.getDeletedDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getDeletedDate(), TBMaintenanceDetails_.deletedDate));
            }
            if (criteria.getTbEquipmentOfCustomerId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbEquipmentOfCustomerId(),
                    root -> root.join(TBMaintenanceDetails_.tbEquipmentOfCustomer, JoinType.LEFT).get(TBEquipmentOfCustomer_.id)));
            }
            if (criteria.getTbMaintenanceScheduleId() != null) {
                specification = specification.and(buildSpecification(criteria.getTbMaintenanceScheduleId(),
                    root -> root.join(TBMaintenanceDetails_.tbMaintenanceSchedule, JoinType.LEFT).get(TBMaintenanceSchedule_.id)));
            }
            if (criteria.getCreatedById() != null) {
                specification = specification.and(buildSpecification(criteria.getCreatedById(),
                    root -> root.join(TBMaintenanceDetails_.createdBy, JoinType.LEFT).get(TBDetailUser_.id)));
            }
            if (criteria.getModifiedById() != null) {
                specification = specification.and(buildSpecification(criteria.getModifiedById(),
                    root -> root.join(TBMaintenanceDetails_.modifiedBy, JoinType.LEFT).get(TBDetailUser_.id)));
            }
            if (criteria.getDeletedById() != null) {
                specification = specification.and(buildSpecification(criteria.getDeletedById(),
                    root -> root.join(TBMaintenanceDetails_.deletedBy, JoinType.LEFT).get(TBDetailUser_.id)));
            }
        }
        return specification;
    }
}
