package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBGuidelinePeriodService;
import vn.azteam.tpf.domain.TBGuidelinePeriod;
import vn.azteam.tpf.repository.TBGuidelinePeriodRepository;
import vn.azteam.tpf.repository.search.TBGuidelinePeriodSearchRepository;
import vn.azteam.tpf.service.dto.TBGuidelinePeriodDTO;
import vn.azteam.tpf.service.mapper.TBGuidelinePeriodMapper;
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 TBGuidelinePeriod.
 */
@Service
@Transactional
public class TBGuidelinePeriodServiceImpl implements TBGuidelinePeriodService {

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

    private final TBGuidelinePeriodRepository tBGuidelinePeriodRepository;

    private final TBGuidelinePeriodMapper tBGuidelinePeriodMapper;

    private final TBGuidelinePeriodSearchRepository tBGuidelinePeriodSearchRepository;

    public TBGuidelinePeriodServiceImpl(TBGuidelinePeriodRepository tBGuidelinePeriodRepository, TBGuidelinePeriodMapper tBGuidelinePeriodMapper, TBGuidelinePeriodSearchRepository tBGuidelinePeriodSearchRepository) {
        this.tBGuidelinePeriodRepository = tBGuidelinePeriodRepository;
        this.tBGuidelinePeriodMapper = tBGuidelinePeriodMapper;
        this.tBGuidelinePeriodSearchRepository = tBGuidelinePeriodSearchRepository;
    }

    /**
     * Save a tBGuidelinePeriod.
     *
     * @param tBGuidelinePeriodDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBGuidelinePeriodDTO save(TBGuidelinePeriodDTO tBGuidelinePeriodDTO) {
        log.debug("Request to save TBGuidelinePeriod : {}", tBGuidelinePeriodDTO);

        TBGuidelinePeriod tBGuidelinePeriod = tBGuidelinePeriodMapper.toEntity(tBGuidelinePeriodDTO);
        tBGuidelinePeriod = tBGuidelinePeriodRepository.save(tBGuidelinePeriod);
        TBGuidelinePeriodDTO result = tBGuidelinePeriodMapper.toDto(tBGuidelinePeriod);
        tBGuidelinePeriodSearchRepository.save(tBGuidelinePeriod);
        return result;
    }

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


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

    /**
     * Delete the tBGuidelinePeriod by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBGuidelinePeriod : {}", id);
        tBGuidelinePeriodRepository.deleteById(id);
        tBGuidelinePeriodSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBGuidelinePeriod 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<TBGuidelinePeriodDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBGuidelinePeriods for query {}", query);
        return tBGuidelinePeriodSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBGuidelinePeriodMapper::toDto);
    }
}
