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.TBMaintenanceSchedule;
import vn.azteam.tpf.domain.*; // for static metamodels
import vn.azteam.tpf.repository.TBMaintenanceScheduleRepository;
import vn.azteam.tpf.repository.search.TBMaintenanceScheduleSearchRepository;
import vn.azteam.tpf.service.dto.TBMaintenanceScheduleCriteria;
import vn.azteam.tpf.service.dto.TBMaintenanceScheduleDTO;
import vn.azteam.tpf.service.mapper.TBMaintenanceScheduleMapper;

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

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

    private final TBMaintenanceScheduleRepository tBMaintenanceScheduleRepository;

    private final TBMaintenanceScheduleMapper tBMaintenanceScheduleMapper;

    private final TBMaintenanceScheduleSearchRepository tBMaintenanceScheduleSearchRepository;

    public TBMaintenanceScheduleQueryService(TBMaintenanceScheduleRepository tBMaintenanceScheduleRepository, TBMaintenanceScheduleMapper tBMaintenanceScheduleMapper, TBMaintenanceScheduleSearchRepository tBMaintenanceScheduleSearchRepository) {
        this.tBMaintenanceScheduleRepository = tBMaintenanceScheduleRepository;
        this.tBMaintenanceScheduleMapper = tBMaintenanceScheduleMapper;
        this.tBMaintenanceScheduleSearchRepository = tBMaintenanceScheduleSearchRepository;
    }

    /**
     * Return a {@link List} of {@link TBMaintenanceScheduleDTO} 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<TBMaintenanceScheduleDTO> findByCriteria(TBMaintenanceScheduleCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<TBMaintenanceSchedule> specification = createSpecification(criteria);
        return tBMaintenanceScheduleMapper.toDto(tBMaintenanceScheduleRepository.findAll(specification));
    }

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

    /**
     * Function to convert TBMaintenanceScheduleCriteria to a {@link Specification}
     */
    private Specification<TBMaintenanceSchedule> createSpecification(TBMaintenanceScheduleCriteria criteria) {
        Specification<TBMaintenanceSchedule> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildSpecification(criteria.getId(), TBMaintenanceSchedule_.id));
            }
            if (criteria.getDescription() != null) {
                specification = specification.and(buildStringSpecification(criteria.getDescription(), TBMaintenanceSchedule_.description));
            }
            if (criteria.getIsRepeat() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getIsRepeat(), TBMaintenanceSchedule_.isRepeat));
            }
            if (criteria.getRepeatEvery() != null) {
                specification = specification.and(buildStringSpecification(criteria.getRepeatEvery(), TBMaintenanceSchedule_.repeatEvery));
            }
            if (criteria.getIsRepeatOnSunday() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getIsRepeatOnSunday(), TBMaintenanceSchedule_.isRepeatOnSunday));
            }
            if (criteria.getIsRepeatOnMonday() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getIsRepeatOnMonday(), TBMaintenanceSchedule_.isRepeatOnMonday));
            }
            if (criteria.getIsRepeatOnTuesday() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getIsRepeatOnTuesday(), TBMaintenanceSchedule_.isRepeatOnTuesday));
            }
            if (criteria.getIsRepeatOnWednesday() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getIsRepeatOnWednesday(), TBMaintenanceSchedule_.isRepeatOnWednesday));
            }
            if (criteria.getIsRepeatOnThursday() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getIsRepeatOnThursday(), TBMaintenanceSchedule_.isRepeatOnThursday));
            }
            if (criteria.getIsRepeatOnFriday() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getIsRepeatOnFriday(), TBMaintenanceSchedule_.isRepeatOnFriday));
            }
            if (criteria.getIsRepeatOnSaturday() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getIsRepeatOnSaturday(), TBMaintenanceSchedule_.isRepeatOnSaturday));
            }
            if (criteria.getDayOfMonth() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getDayOfMonth(), TBMaintenanceSchedule_.dayOfMonth));
            }
            if (criteria.getEndsOn() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getEndsOn(), TBMaintenanceSchedule_.endsOn));
            }
            if (criteria.getEndsAfterXOccurrences() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getEndsAfterXOccurrences(), TBMaintenanceSchedule_.endsAfterXOccurrences));
            }
            if (criteria.getCreatedDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getCreatedDate(), TBMaintenanceSchedule_.createdDate));
            }
            if (criteria.getModifiedDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getModifiedDate(), TBMaintenanceSchedule_.modifiedDate));
            }
            if (criteria.getDeletedDate() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getDeletedDate(), TBMaintenanceSchedule_.deletedDate));
            }
        }
        return specification;
    }
}
