package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBSuppliesInWarehouseService;
import vn.azteam.tpf.domain.TBSuppliesInWarehouse;
import vn.azteam.tpf.repository.TBSuppliesInWarehouseRepository;
import vn.azteam.tpf.repository.search.TBSuppliesInWarehouseSearchRepository;
import vn.azteam.tpf.service.dto.TBSuppliesInWarehouseDTO;
import vn.azteam.tpf.service.mapper.TBSuppliesInWarehouseMapper;
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 TBSuppliesInWarehouse.
 */
@Service
@Transactional
public class TBSuppliesInWarehouseServiceImpl implements TBSuppliesInWarehouseService {

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

    private final TBSuppliesInWarehouseRepository tBSuppliesInWarehouseRepository;

    private final TBSuppliesInWarehouseMapper tBSuppliesInWarehouseMapper;

    private final TBSuppliesInWarehouseSearchRepository tBSuppliesInWarehouseSearchRepository;

    public TBSuppliesInWarehouseServiceImpl(TBSuppliesInWarehouseRepository tBSuppliesInWarehouseRepository, TBSuppliesInWarehouseMapper tBSuppliesInWarehouseMapper, TBSuppliesInWarehouseSearchRepository tBSuppliesInWarehouseSearchRepository) {
        this.tBSuppliesInWarehouseRepository = tBSuppliesInWarehouseRepository;
        this.tBSuppliesInWarehouseMapper = tBSuppliesInWarehouseMapper;
        this.tBSuppliesInWarehouseSearchRepository = tBSuppliesInWarehouseSearchRepository;
    }

    /**
     * Save a tBSuppliesInWarehouse.
     *
     * @param tBSuppliesInWarehouseDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBSuppliesInWarehouseDTO save(TBSuppliesInWarehouseDTO tBSuppliesInWarehouseDTO) {
        log.debug("Request to save TBSuppliesInWarehouse : {}", tBSuppliesInWarehouseDTO);

        TBSuppliesInWarehouse tBSuppliesInWarehouse = tBSuppliesInWarehouseMapper.toEntity(tBSuppliesInWarehouseDTO);
        tBSuppliesInWarehouse = tBSuppliesInWarehouseRepository.save(tBSuppliesInWarehouse);
        TBSuppliesInWarehouseDTO result = tBSuppliesInWarehouseMapper.toDto(tBSuppliesInWarehouse);
        tBSuppliesInWarehouseSearchRepository.save(tBSuppliesInWarehouse);
        return result;
    }

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


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

    /**
     * Delete the tBSuppliesInWarehouse by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBSuppliesInWarehouse : {}", id);
        tBSuppliesInWarehouseRepository.deleteById(id);
        tBSuppliesInWarehouseSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBSuppliesInWarehouse 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<TBSuppliesInWarehouseDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBSuppliesInWarehouses for query {}", query);
        return tBSuppliesInWarehouseSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBSuppliesInWarehouseMapper::toDto);
    }
}
