package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBExternalTableService;
import vn.azteam.tpf.domain.TBExternalTable;
import vn.azteam.tpf.repository.TBExternalTableRepository;
import vn.azteam.tpf.repository.search.TBExternalTableSearchRepository;
import vn.azteam.tpf.service.dto.TBExternalTableDTO;
import vn.azteam.tpf.service.mapper.TBExternalTableMapper;
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 TBExternalTable.
 */
@Service
@Transactional
public class TBExternalTableServiceImpl implements TBExternalTableService {

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

    private final TBExternalTableRepository tBExternalTableRepository;

    private final TBExternalTableMapper tBExternalTableMapper;

    private final TBExternalTableSearchRepository tBExternalTableSearchRepository;

    public TBExternalTableServiceImpl(TBExternalTableRepository tBExternalTableRepository, TBExternalTableMapper tBExternalTableMapper, TBExternalTableSearchRepository tBExternalTableSearchRepository) {
        this.tBExternalTableRepository = tBExternalTableRepository;
        this.tBExternalTableMapper = tBExternalTableMapper;
        this.tBExternalTableSearchRepository = tBExternalTableSearchRepository;
    }

    /**
     * Save a tBExternalTable.
     *
     * @param tBExternalTableDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBExternalTableDTO save(TBExternalTableDTO tBExternalTableDTO) {
        log.debug("Request to save TBExternalTable : {}", tBExternalTableDTO);

        TBExternalTable tBExternalTable = tBExternalTableMapper.toEntity(tBExternalTableDTO);
        tBExternalTable = tBExternalTableRepository.save(tBExternalTable);
        TBExternalTableDTO result = tBExternalTableMapper.toDto(tBExternalTable);
        tBExternalTableSearchRepository.save(tBExternalTable);
        return result;
    }

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


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

    /**
     * Delete the tBExternalTable by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBExternalTable : {}", id);
        tBExternalTableRepository.deleteById(id);
        tBExternalTableSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBExternalTable 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<TBExternalTableDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBExternalTables for query {}", query);
        return tBExternalTableSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBExternalTableMapper::toDto);
    }
}
