package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBSuppliesPriceHistoryService;
import vn.azteam.tpf.domain.TBSuppliesPriceHistory;
import vn.azteam.tpf.repository.TBSuppliesPriceHistoryRepository;
import vn.azteam.tpf.repository.search.TBSuppliesPriceHistorySearchRepository;
import vn.azteam.tpf.service.dto.TBSuppliesPriceHistoryDTO;
import vn.azteam.tpf.service.mapper.TBSuppliesPriceHistoryMapper;
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 TBSuppliesPriceHistory.
 */
@Service
@Transactional
public class TBSuppliesPriceHistoryServiceImpl implements TBSuppliesPriceHistoryService {

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

    private final TBSuppliesPriceHistoryRepository tBSuppliesPriceHistoryRepository;

    private final TBSuppliesPriceHistoryMapper tBSuppliesPriceHistoryMapper;

    private final TBSuppliesPriceHistorySearchRepository tBSuppliesPriceHistorySearchRepository;

    public TBSuppliesPriceHistoryServiceImpl(TBSuppliesPriceHistoryRepository tBSuppliesPriceHistoryRepository, TBSuppliesPriceHistoryMapper tBSuppliesPriceHistoryMapper, TBSuppliesPriceHistorySearchRepository tBSuppliesPriceHistorySearchRepository) {
        this.tBSuppliesPriceHistoryRepository = tBSuppliesPriceHistoryRepository;
        this.tBSuppliesPriceHistoryMapper = tBSuppliesPriceHistoryMapper;
        this.tBSuppliesPriceHistorySearchRepository = tBSuppliesPriceHistorySearchRepository;
    }

    /**
     * Save a tBSuppliesPriceHistory.
     *
     * @param tBSuppliesPriceHistoryDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBSuppliesPriceHistoryDTO save(TBSuppliesPriceHistoryDTO tBSuppliesPriceHistoryDTO) {
        log.debug("Request to save TBSuppliesPriceHistory : {}", tBSuppliesPriceHistoryDTO);

        TBSuppliesPriceHistory tBSuppliesPriceHistory = tBSuppliesPriceHistoryMapper.toEntity(tBSuppliesPriceHistoryDTO);
        tBSuppliesPriceHistory = tBSuppliesPriceHistoryRepository.save(tBSuppliesPriceHistory);
        TBSuppliesPriceHistoryDTO result = tBSuppliesPriceHistoryMapper.toDto(tBSuppliesPriceHistory);
        tBSuppliesPriceHistorySearchRepository.save(tBSuppliesPriceHistory);
        return result;
    }

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


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

    /**
     * Delete the tBSuppliesPriceHistory by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBSuppliesPriceHistory : {}", id);
        tBSuppliesPriceHistoryRepository.deleteById(id);
        tBSuppliesPriceHistorySearchRepository.deleteById(id);
    }

    /**
     * Search for the tBSuppliesPriceHistory 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<TBSuppliesPriceHistoryDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBSuppliesPriceHistories for query {}", query);
        return tBSuppliesPriceHistorySearchRepository.search(queryStringQuery(query), pageable)
            .map(tBSuppliesPriceHistoryMapper::toDto);
    }
}
