package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBMaintenanceScheduleService;
import vn.azteam.tpf.domain.TBMaintenanceSchedule;
import vn.azteam.tpf.repository.TBMaintenanceScheduleRepository;
import vn.azteam.tpf.repository.search.TBMaintenanceScheduleSearchRepository;
import vn.azteam.tpf.service.dto.TBMaintenanceScheduleDTO;
import vn.azteam.tpf.service.mapper.TBMaintenanceScheduleMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing TBMaintenanceSchedule.
 */
@Service
@Transactional
public class TBMaintenanceScheduleServiceImpl implements TBMaintenanceScheduleService {

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

    private final TBMaintenanceScheduleRepository tBMaintenanceScheduleRepository;

    private final TBMaintenanceScheduleMapper tBMaintenanceScheduleMapper;

    private final TBMaintenanceScheduleSearchRepository tBMaintenanceScheduleSearchRepository;

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

    /**
     * Save a tBMaintenanceSchedule.
     *
     * @param tBMaintenanceScheduleDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBMaintenanceScheduleDTO save(TBMaintenanceScheduleDTO tBMaintenanceScheduleDTO) {
        log.debug("Request to save TBMaintenanceSchedule : {}", tBMaintenanceScheduleDTO);

        TBMaintenanceSchedule tBMaintenanceSchedule = tBMaintenanceScheduleMapper.toEntity(tBMaintenanceScheduleDTO);
        tBMaintenanceSchedule = tBMaintenanceScheduleRepository.save(tBMaintenanceSchedule);
        TBMaintenanceScheduleDTO result = tBMaintenanceScheduleMapper.toDto(tBMaintenanceSchedule);
        tBMaintenanceScheduleSearchRepository.save(tBMaintenanceSchedule);
        return result;
    }

    /**
     * Get all the tBMaintenanceSchedules.
     *
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<TBMaintenanceScheduleDTO> findAll(Pageable pageable) {
        log.debug("Request to get all TBMaintenanceSchedules");
        return tBMaintenanceScheduleRepository.findAll(pageable)
            .map(tBMaintenanceScheduleMapper::toDto);
    }


    /**
     * Get one tBMaintenanceSchedule by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public Optional<TBMaintenanceScheduleDTO> findOne(Long id) {
        log.debug("Request to get TBMaintenanceSchedule : {}", id);
        return tBMaintenanceScheduleRepository.findById(id)
            .map(tBMaintenanceScheduleMapper::toDto);
    }

    /**
     * Delete the tBMaintenanceSchedule by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBMaintenanceSchedule : {}", id);
        tBMaintenanceScheduleRepository.deleteById(id);
        tBMaintenanceScheduleSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBMaintenanceSchedule corresponding to the query.
     *
     * @param query the query of the search
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<TBMaintenanceScheduleDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBMaintenanceSchedules for query {}", query);
        return tBMaintenanceScheduleSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBMaintenanceScheduleMapper::toDto);
    }
}
