package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBGuidelineActivityService;
import vn.azteam.tpf.domain.TBGuidelineActivity;
import vn.azteam.tpf.repository.TBGuidelineActivityRepository;
import vn.azteam.tpf.repository.search.TBGuidelineActivitySearchRepository;
import vn.azteam.tpf.service.dto.TBGuidelineActivityDTO;
import vn.azteam.tpf.service.mapper.TBGuidelineActivityMapper;
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 TBGuidelineActivity.
 */
@Service
@Transactional
public class TBGuidelineActivityServiceImpl implements TBGuidelineActivityService {

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

    private final TBGuidelineActivityRepository tBGuidelineActivityRepository;

    private final TBGuidelineActivityMapper tBGuidelineActivityMapper;

    private final TBGuidelineActivitySearchRepository tBGuidelineActivitySearchRepository;

    public TBGuidelineActivityServiceImpl(TBGuidelineActivityRepository tBGuidelineActivityRepository, TBGuidelineActivityMapper tBGuidelineActivityMapper, TBGuidelineActivitySearchRepository tBGuidelineActivitySearchRepository) {
        this.tBGuidelineActivityRepository = tBGuidelineActivityRepository;
        this.tBGuidelineActivityMapper = tBGuidelineActivityMapper;
        this.tBGuidelineActivitySearchRepository = tBGuidelineActivitySearchRepository;
    }

    /**
     * Save a tBGuidelineActivity.
     *
     * @param tBGuidelineActivityDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBGuidelineActivityDTO save(TBGuidelineActivityDTO tBGuidelineActivityDTO) {
        log.debug("Request to save TBGuidelineActivity : {}", tBGuidelineActivityDTO);

        TBGuidelineActivity tBGuidelineActivity = tBGuidelineActivityMapper.toEntity(tBGuidelineActivityDTO);
        tBGuidelineActivity = tBGuidelineActivityRepository.save(tBGuidelineActivity);
        TBGuidelineActivityDTO result = tBGuidelineActivityMapper.toDto(tBGuidelineActivity);
        tBGuidelineActivitySearchRepository.save(tBGuidelineActivity);
        return result;
    }

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


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

    /**
     * Delete the tBGuidelineActivity by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBGuidelineActivity : {}", id);
        tBGuidelineActivityRepository.deleteById(id);
        tBGuidelineActivitySearchRepository.deleteById(id);
    }

    /**
     * Search for the tBGuidelineActivity 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<TBGuidelineActivityDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBGuidelineActivities for query {}", query);
        return tBGuidelineActivitySearchRepository.search(queryStringQuery(query), pageable)
            .map(tBGuidelineActivityMapper::toDto);
    }
}
