package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBMaintenanceDetailsService;
import vn.azteam.tpf.domain.TBMaintenanceDetails;
import vn.azteam.tpf.repository.TBMaintenanceDetailsRepository;
import vn.azteam.tpf.repository.search.TBMaintenanceDetailsSearchRepository;
import vn.azteam.tpf.service.dto.TBMaintenanceDetailsDTO;
import vn.azteam.tpf.service.mapper.TBMaintenanceDetailsMapper;
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 TBMaintenanceDetails.
 */
@Service
@Transactional
public class TBMaintenanceDetailsServiceImpl implements TBMaintenanceDetailsService {

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

    private final TBMaintenanceDetailsRepository tBMaintenanceDetailsRepository;

    private final TBMaintenanceDetailsMapper tBMaintenanceDetailsMapper;

    private final TBMaintenanceDetailsSearchRepository tBMaintenanceDetailsSearchRepository;

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

    /**
     * Save a tBMaintenanceDetails.
     *
     * @param tBMaintenanceDetailsDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBMaintenanceDetailsDTO save(TBMaintenanceDetailsDTO tBMaintenanceDetailsDTO) {
        log.debug("Request to save TBMaintenanceDetails : {}", tBMaintenanceDetailsDTO);

        TBMaintenanceDetails tBMaintenanceDetails = tBMaintenanceDetailsMapper.toEntity(tBMaintenanceDetailsDTO);
        tBMaintenanceDetails = tBMaintenanceDetailsRepository.save(tBMaintenanceDetails);
        TBMaintenanceDetailsDTO result = tBMaintenanceDetailsMapper.toDto(tBMaintenanceDetails);
        tBMaintenanceDetailsSearchRepository.save(tBMaintenanceDetails);
        return result;
    }

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


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

    /**
     * Delete the tBMaintenanceDetails by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBMaintenanceDetails : {}", id);
        tBMaintenanceDetailsRepository.deleteById(id);
        tBMaintenanceDetailsSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBMaintenanceDetails 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<TBMaintenanceDetailsDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBMaintenanceDetails for query {}", query);
        return tBMaintenanceDetailsSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBMaintenanceDetailsMapper::toDto);
    }
}
