package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBProductStockUpdateService;
import vn.azteam.tpf.domain.TBProductStockUpdate;
import vn.azteam.tpf.repository.TBProductStockUpdateRepository;
import vn.azteam.tpf.repository.search.TBProductStockUpdateSearchRepository;
import vn.azteam.tpf.service.dto.TBProductStockUpdateDTO;
import vn.azteam.tpf.service.mapper.TBProductStockUpdateMapper;
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 TBProductStockUpdate.
 */
@Service
@Transactional
public class TBProductStockUpdateServiceImpl implements TBProductStockUpdateService {

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

    private final TBProductStockUpdateRepository tBProductStockUpdateRepository;

    private final TBProductStockUpdateMapper tBProductStockUpdateMapper;

    private final TBProductStockUpdateSearchRepository tBProductStockUpdateSearchRepository;

    public TBProductStockUpdateServiceImpl(TBProductStockUpdateRepository tBProductStockUpdateRepository, TBProductStockUpdateMapper tBProductStockUpdateMapper, TBProductStockUpdateSearchRepository tBProductStockUpdateSearchRepository) {
        this.tBProductStockUpdateRepository = tBProductStockUpdateRepository;
        this.tBProductStockUpdateMapper = tBProductStockUpdateMapper;
        this.tBProductStockUpdateSearchRepository = tBProductStockUpdateSearchRepository;
    }

    /**
     * Save a tBProductStockUpdate.
     *
     * @param tBProductStockUpdateDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBProductStockUpdateDTO save(TBProductStockUpdateDTO tBProductStockUpdateDTO) {
        log.debug("Request to save TBProductStockUpdate : {}", tBProductStockUpdateDTO);

        TBProductStockUpdate tBProductStockUpdate = tBProductStockUpdateMapper.toEntity(tBProductStockUpdateDTO);
        tBProductStockUpdate = tBProductStockUpdateRepository.save(tBProductStockUpdate);
        TBProductStockUpdateDTO result = tBProductStockUpdateMapper.toDto(tBProductStockUpdate);
        tBProductStockUpdateSearchRepository.save(tBProductStockUpdate);
        return result;
    }

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


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

    /**
     * Delete the tBProductStockUpdate by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBProductStockUpdate : {}", id);
        tBProductStockUpdateRepository.deleteById(id);
        tBProductStockUpdateSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBProductStockUpdate 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<TBProductStockUpdateDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBProductStockUpdates for query {}", query);
        return tBProductStockUpdateSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBProductStockUpdateMapper::toDto);
    }
}
