package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBGuidelineDetailsService;
import vn.azteam.tpf.domain.TBGuidelineDetails;
import vn.azteam.tpf.repository.TBGuidelineDetailsRepository;
import vn.azteam.tpf.repository.search.TBGuidelineDetailsSearchRepository;
import vn.azteam.tpf.service.dto.TBGuidelineDetailsDTO;
import vn.azteam.tpf.service.mapper.TBGuidelineDetailsMapper;
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.List;
import java.util.Optional;
import java.util.stream.Collectors;

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

/**
 * Service Implementation for managing TBGuidelineDetails.
 */
@Service
@Transactional
public class TBGuidelineDetailsServiceImpl implements TBGuidelineDetailsService {

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

    private final TBGuidelineDetailsRepository tBGuidelineDetailsRepository;

    private final TBGuidelineDetailsMapper tBGuidelineDetailsMapper;

    private final TBGuidelineDetailsSearchRepository tBGuidelineDetailsSearchRepository;

    public TBGuidelineDetailsServiceImpl(TBGuidelineDetailsRepository tBGuidelineDetailsRepository, TBGuidelineDetailsMapper tBGuidelineDetailsMapper, TBGuidelineDetailsSearchRepository tBGuidelineDetailsSearchRepository) {
        this.tBGuidelineDetailsRepository = tBGuidelineDetailsRepository;
        this.tBGuidelineDetailsMapper = tBGuidelineDetailsMapper;
        this.tBGuidelineDetailsSearchRepository = tBGuidelineDetailsSearchRepository;
    }

    /**
     * Save a tBGuidelineDetails.
     *
     * @param tBGuidelineDetailsDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBGuidelineDetailsDTO save(TBGuidelineDetailsDTO tBGuidelineDetailsDTO) {
        log.debug("Request to save TBGuidelineDetails : {}", tBGuidelineDetailsDTO);

        TBGuidelineDetails tBGuidelineDetails = tBGuidelineDetailsMapper.toEntity(tBGuidelineDetailsDTO);
        tBGuidelineDetails = tBGuidelineDetailsRepository.save(tBGuidelineDetails);
        TBGuidelineDetailsDTO result = tBGuidelineDetailsMapper.toDto(tBGuidelineDetails);
        tBGuidelineDetailsSearchRepository.save(tBGuidelineDetails);
        return result;
    }

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

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

    /**
     * Delete the tBGuidelineDetails by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBGuidelineDetails : {}", id);
        tBGuidelineDetailsRepository.deleteById(id);
        tBGuidelineDetailsSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBGuidelineDetails 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<TBGuidelineDetailsDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBGuidelineDetails for query {}", query);
        return tBGuidelineDetailsSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBGuidelineDetailsMapper::toDto);
    }
}
