package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBSuppliesOrderService;
import vn.azteam.tpf.domain.TBSuppliesOrder;
import vn.azteam.tpf.repository.TBSuppliesOrderRepository;
import vn.azteam.tpf.repository.search.TBSuppliesOrderSearchRepository;
import vn.azteam.tpf.service.dto.TBSuppliesOrderDTO;
import vn.azteam.tpf.service.mapper.TBSuppliesOrderMapper;
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 TBSuppliesOrder.
 */
@Service
@Transactional
public class TBSuppliesOrderServiceImpl implements TBSuppliesOrderService {

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

    private final TBSuppliesOrderRepository tBSuppliesOrderRepository;

    private final TBSuppliesOrderMapper tBSuppliesOrderMapper;

    private final TBSuppliesOrderSearchRepository tBSuppliesOrderSearchRepository;

    public TBSuppliesOrderServiceImpl(TBSuppliesOrderRepository tBSuppliesOrderRepository, TBSuppliesOrderMapper tBSuppliesOrderMapper, TBSuppliesOrderSearchRepository tBSuppliesOrderSearchRepository) {
        this.tBSuppliesOrderRepository = tBSuppliesOrderRepository;
        this.tBSuppliesOrderMapper = tBSuppliesOrderMapper;
        this.tBSuppliesOrderSearchRepository = tBSuppliesOrderSearchRepository;
    }

    /**
     * Save a tBSuppliesOrder.
     *
     * @param tBSuppliesOrderDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBSuppliesOrderDTO save(TBSuppliesOrderDTO tBSuppliesOrderDTO) {
        log.debug("Request to save TBSuppliesOrder : {}", tBSuppliesOrderDTO);

        TBSuppliesOrder tBSuppliesOrder = tBSuppliesOrderMapper.toEntity(tBSuppliesOrderDTO);
        tBSuppliesOrder = tBSuppliesOrderRepository.save(tBSuppliesOrder);
        TBSuppliesOrderDTO result = tBSuppliesOrderMapper.toDto(tBSuppliesOrder);
        tBSuppliesOrderSearchRepository.save(tBSuppliesOrder);
        return result;
    }

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


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

    /**
     * Delete the tBSuppliesOrder by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBSuppliesOrder : {}", id);
        tBSuppliesOrderRepository.deleteById(id);
        tBSuppliesOrderSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBSuppliesOrder 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<TBSuppliesOrderDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBSuppliesOrders for query {}", query);
        return tBSuppliesOrderSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBSuppliesOrderMapper::toDto);
    }
}
