package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBSuppliesUsingDetailsService;
import vn.azteam.tpf.domain.TBSuppliesUsingDetails;
import vn.azteam.tpf.repository.TBSuppliesUsingDetailsRepository;
import vn.azteam.tpf.repository.search.TBSuppliesUsingDetailsSearchRepository;
import vn.azteam.tpf.service.dto.TBSuppliesUsingDetailsDTO;
import vn.azteam.tpf.service.mapper.TBSuppliesUsingDetailsMapper;
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 TBSuppliesUsingDetails.
 */
@Service
@Transactional
public class TBSuppliesUsingDetailsServiceImpl implements TBSuppliesUsingDetailsService {

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

    private final TBSuppliesUsingDetailsRepository tBSuppliesUsingDetailsRepository;

    private final TBSuppliesUsingDetailsMapper tBSuppliesUsingDetailsMapper;

    private final TBSuppliesUsingDetailsSearchRepository tBSuppliesUsingDetailsSearchRepository;

    public TBSuppliesUsingDetailsServiceImpl(TBSuppliesUsingDetailsRepository tBSuppliesUsingDetailsRepository, TBSuppliesUsingDetailsMapper tBSuppliesUsingDetailsMapper, TBSuppliesUsingDetailsSearchRepository tBSuppliesUsingDetailsSearchRepository) {
        this.tBSuppliesUsingDetailsRepository = tBSuppliesUsingDetailsRepository;
        this.tBSuppliesUsingDetailsMapper = tBSuppliesUsingDetailsMapper;
        this.tBSuppliesUsingDetailsSearchRepository = tBSuppliesUsingDetailsSearchRepository;
    }

    /**
     * Save a tBSuppliesUsingDetails.
     *
     * @param tBSuppliesUsingDetailsDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBSuppliesUsingDetailsDTO save(TBSuppliesUsingDetailsDTO tBSuppliesUsingDetailsDTO) {
        log.debug("Request to save TBSuppliesUsingDetails : {}", tBSuppliesUsingDetailsDTO);

        TBSuppliesUsingDetails tBSuppliesUsingDetails = tBSuppliesUsingDetailsMapper.toEntity(tBSuppliesUsingDetailsDTO);
        tBSuppliesUsingDetails = tBSuppliesUsingDetailsRepository.save(tBSuppliesUsingDetails);
        TBSuppliesUsingDetailsDTO result = tBSuppliesUsingDetailsMapper.toDto(tBSuppliesUsingDetails);
        tBSuppliesUsingDetailsSearchRepository.save(tBSuppliesUsingDetails);
        return result;
    }

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


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

    /**
     * Delete the tBSuppliesUsingDetails by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBSuppliesUsingDetails : {}", id);
        tBSuppliesUsingDetailsRepository.deleteById(id);
        tBSuppliesUsingDetailsSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBSuppliesUsingDetails 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<TBSuppliesUsingDetailsDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBSuppliesUsingDetails for query {}", query);
        return tBSuppliesUsingDetailsSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBSuppliesUsingDetailsMapper::toDto);
    }
}
