package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBSuppliesService;
import vn.azteam.tpf.domain.TBSupplies;
import vn.azteam.tpf.repository.TBSuppliesRepository;
import vn.azteam.tpf.repository.search.TBSuppliesSearchRepository;
import vn.azteam.tpf.service.dto.TBSuppliesDTO;
import vn.azteam.tpf.service.mapper.TBSuppliesMapper;
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 TBSupplies.
 */
@Service
@Transactional
public class TBSuppliesServiceImpl implements TBSuppliesService {

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

    private final TBSuppliesRepository tBSuppliesRepository;

    private final TBSuppliesMapper tBSuppliesMapper;

    private final TBSuppliesSearchRepository tBSuppliesSearchRepository;

    public TBSuppliesServiceImpl(TBSuppliesRepository tBSuppliesRepository, TBSuppliesMapper tBSuppliesMapper, TBSuppliesSearchRepository tBSuppliesSearchRepository) {
        this.tBSuppliesRepository = tBSuppliesRepository;
        this.tBSuppliesMapper = tBSuppliesMapper;
        this.tBSuppliesSearchRepository = tBSuppliesSearchRepository;
    }

    /**
     * Save a tBSupplies.
     *
     * @param tBSuppliesDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBSuppliesDTO save(TBSuppliesDTO tBSuppliesDTO) {
        log.debug("Request to save TBSupplies : {}", tBSuppliesDTO);

        TBSupplies tBSupplies = tBSuppliesMapper.toEntity(tBSuppliesDTO);
        tBSupplies = tBSuppliesRepository.save(tBSupplies);
        TBSuppliesDTO result = tBSuppliesMapper.toDto(tBSupplies);
        tBSuppliesSearchRepository.save(tBSupplies);
        return result;
    }

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

    /**
     * Get all the TBSupplies with eager load of many-to-many relationships.
     *
     * @return the list of entities
     */
    public Page<TBSuppliesDTO> findAllWithEagerRelationships(Pageable pageable) {
        return tBSuppliesRepository.findAllWithEagerRelationships(pageable).map(tBSuppliesMapper::toDto);
    }
    

    /**
     * Get one tBSupplies by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public Optional<TBSuppliesDTO> findOne(Long id) {
        log.debug("Request to get TBSupplies : {}", id);
        return tBSuppliesRepository.findOneWithEagerRelationships(id)
            .map(tBSuppliesMapper::toDto);
    }

    @Override
    @Transactional(readOnly = true)
    public Optional<TBSuppliesDTO> findFirstBySkuOrName(String sku, String name) {
        log.debug("Request to get TBSupplies : {}", sku, name);
        return tBSuppliesRepository.findFirstBySkuOrName(sku, name)
            .map(tBSuppliesMapper::toDto);
    }

    /**
     * Delete the tBSupplies by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBSupplies : {}", id);
        tBSuppliesRepository.deleteById(id);
        tBSuppliesSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBSupplies 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<TBSuppliesDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBSupplies for query {}", query);
        return tBSuppliesSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBSuppliesMapper::toDto);
    }
}
