package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBSuppliesOrderDetailsService;
import vn.azteam.tpf.domain.TBSuppliesOrderDetails;
import vn.azteam.tpf.repository.TBSuppliesOrderDetailsRepository;
import vn.azteam.tpf.repository.search.TBSuppliesOrderDetailsSearchRepository;
import vn.azteam.tpf.service.dto.TBSuppliesOrderDetailsDTO;
import vn.azteam.tpf.service.mapper.TBSuppliesOrderDetailsMapper;
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 TBSuppliesOrderDetails.
 */
@Service
@Transactional
public class TBSuppliesOrderDetailsServiceImpl implements TBSuppliesOrderDetailsService {

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

    private final TBSuppliesOrderDetailsRepository tBSuppliesOrderDetailsRepository;

    private final TBSuppliesOrderDetailsMapper tBSuppliesOrderDetailsMapper;

    private final TBSuppliesOrderDetailsSearchRepository tBSuppliesOrderDetailsSearchRepository;

    public TBSuppliesOrderDetailsServiceImpl(TBSuppliesOrderDetailsRepository tBSuppliesOrderDetailsRepository, TBSuppliesOrderDetailsMapper tBSuppliesOrderDetailsMapper, TBSuppliesOrderDetailsSearchRepository tBSuppliesOrderDetailsSearchRepository) {
        this.tBSuppliesOrderDetailsRepository = tBSuppliesOrderDetailsRepository;
        this.tBSuppliesOrderDetailsMapper = tBSuppliesOrderDetailsMapper;
        this.tBSuppliesOrderDetailsSearchRepository = tBSuppliesOrderDetailsSearchRepository;
    }

    /**
     * Save a tBSuppliesOrderDetails.
     *
     * @param tBSuppliesOrderDetailsDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBSuppliesOrderDetailsDTO save(TBSuppliesOrderDetailsDTO tBSuppliesOrderDetailsDTO) {
        log.debug("Request to save TBSuppliesOrderDetails : {}", tBSuppliesOrderDetailsDTO);

        TBSuppliesOrderDetails tBSuppliesOrderDetails = tBSuppliesOrderDetailsMapper.toEntity(tBSuppliesOrderDetailsDTO);
        tBSuppliesOrderDetails = tBSuppliesOrderDetailsRepository.save(tBSuppliesOrderDetails);
        TBSuppliesOrderDetailsDTO result = tBSuppliesOrderDetailsMapper.toDto(tBSuppliesOrderDetails);
        tBSuppliesOrderDetailsSearchRepository.save(tBSuppliesOrderDetails);
        return result;
    }

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


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

    /**
     * Delete the tBSuppliesOrderDetails by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBSuppliesOrderDetails : {}", id);
        tBSuppliesOrderDetailsRepository.deleteById(id);
        tBSuppliesOrderDetailsSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBSuppliesOrderDetails 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<TBSuppliesOrderDetailsDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBSuppliesOrderDetails for query {}", query);
        return tBSuppliesOrderDetailsSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBSuppliesOrderDetailsMapper::toDto);
    }
}
