package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBStockUpdateService;
import vn.azteam.tpf.domain.TBStockUpdate;
import vn.azteam.tpf.repository.TBStockUpdateRepository;
import vn.azteam.tpf.repository.search.TBStockUpdateSearchRepository;
import vn.azteam.tpf.service.dto.TBStockUpdateDTO;
import vn.azteam.tpf.service.mapper.TBStockUpdateMapper;
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 TBStockUpdate.
 */
@Service
@Transactional
public class TBStockUpdateServiceImpl implements TBStockUpdateService {

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

    private final TBStockUpdateRepository tBStockUpdateRepository;

    private final TBStockUpdateMapper tBStockUpdateMapper;

    private final TBStockUpdateSearchRepository tBStockUpdateSearchRepository;

    public TBStockUpdateServiceImpl(TBStockUpdateRepository tBStockUpdateRepository, TBStockUpdateMapper tBStockUpdateMapper, TBStockUpdateSearchRepository tBStockUpdateSearchRepository) {
        this.tBStockUpdateRepository = tBStockUpdateRepository;
        this.tBStockUpdateMapper = tBStockUpdateMapper;
        this.tBStockUpdateSearchRepository = tBStockUpdateSearchRepository;
    }

    /**
     * Save a tBStockUpdate.
     *
     * @param tBStockUpdateDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBStockUpdateDTO save(TBStockUpdateDTO tBStockUpdateDTO) {
        log.debug("Request to save TBStockUpdate : {}", tBStockUpdateDTO);

        TBStockUpdate tBStockUpdate = tBStockUpdateMapper.toEntity(tBStockUpdateDTO);
        tBStockUpdate = tBStockUpdateRepository.save(tBStockUpdate);
        TBStockUpdateDTO result = tBStockUpdateMapper.toDto(tBStockUpdate);
        tBStockUpdateSearchRepository.save(tBStockUpdate);
        return result;
    }

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


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

    /**
     * Delete the tBStockUpdate by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBStockUpdate : {}", id);
        tBStockUpdateRepository.deleteById(id);
        tBStockUpdateSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBStockUpdate 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<TBStockUpdateDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBStockUpdates for query {}", query);
        return tBStockUpdateSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBStockUpdateMapper::toDto);
    }
}
