package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBVendorService;
import vn.azteam.tpf.domain.TBVendor;
import vn.azteam.tpf.repository.TBVendorRepository;
import vn.azteam.tpf.repository.search.TBVendorSearchRepository;
import vn.azteam.tpf.service.dto.TBVendorDTO;
import vn.azteam.tpf.service.mapper.TBVendorMapper;
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 TBVendor.
 */
@Service
@Transactional
public class TBVendorServiceImpl implements TBVendorService {

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

    private final TBVendorRepository tBVendorRepository;

    private final TBVendorMapper tBVendorMapper;

    private final TBVendorSearchRepository tBVendorSearchRepository;

    public TBVendorServiceImpl(TBVendorRepository tBVendorRepository, TBVendorMapper tBVendorMapper, TBVendorSearchRepository tBVendorSearchRepository) {
        this.tBVendorRepository = tBVendorRepository;
        this.tBVendorMapper = tBVendorMapper;
        this.tBVendorSearchRepository = tBVendorSearchRepository;
    }

    /**
     * Save a tBVendor.
     *
     * @param tBVendorDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBVendorDTO save(TBVendorDTO tBVendorDTO) {
        log.debug("Request to save TBVendor : {}", tBVendorDTO);

        TBVendor tBVendor = tBVendorMapper.toEntity(tBVendorDTO);
        tBVendor = tBVendorRepository.save(tBVendor);
        TBVendorDTO result = tBVendorMapper.toDto(tBVendor);
        tBVendorSearchRepository.save(tBVendor);
        return result;
    }

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


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

    /**
     * Delete the tBVendor by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBVendor : {}", id);
        tBVendorRepository.deleteById(id);
        tBVendorSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBVendor 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<TBVendorDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBVendors for query {}", query);
        return tBVendorSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBVendorMapper::toDto);
    }
}
