package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBSuppliesTypeService;
import vn.azteam.tpf.domain.TBSuppliesType;
import vn.azteam.tpf.repository.TBSuppliesTypeRepository;
import vn.azteam.tpf.repository.search.TBSuppliesTypeSearchRepository;
import vn.azteam.tpf.service.dto.TBSuppliesTypeDTO;
import vn.azteam.tpf.service.mapper.TBSuppliesTypeMapper;
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 TBSuppliesType.
 */
@Service
@Transactional
public class TBSuppliesTypeServiceImpl implements TBSuppliesTypeService {

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

    private final TBSuppliesTypeRepository tBSuppliesTypeRepository;

    private final TBSuppliesTypeMapper tBSuppliesTypeMapper;

    private final TBSuppliesTypeSearchRepository tBSuppliesTypeSearchRepository;

    public TBSuppliesTypeServiceImpl(TBSuppliesTypeRepository tBSuppliesTypeRepository, TBSuppliesTypeMapper tBSuppliesTypeMapper, TBSuppliesTypeSearchRepository tBSuppliesTypeSearchRepository) {
        this.tBSuppliesTypeRepository = tBSuppliesTypeRepository;
        this.tBSuppliesTypeMapper = tBSuppliesTypeMapper;
        this.tBSuppliesTypeSearchRepository = tBSuppliesTypeSearchRepository;
    }

    /**
     * Save a tBSuppliesType.
     *
     * @param tBSuppliesTypeDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBSuppliesTypeDTO save(TBSuppliesTypeDTO tBSuppliesTypeDTO) {
        log.debug("Request to save TBSuppliesType : {}", tBSuppliesTypeDTO);

        TBSuppliesType tBSuppliesType = tBSuppliesTypeMapper.toEntity(tBSuppliesTypeDTO);
        tBSuppliesType = tBSuppliesTypeRepository.save(tBSuppliesType);
        TBSuppliesTypeDTO result = tBSuppliesTypeMapper.toDto(tBSuppliesType);
        tBSuppliesTypeSearchRepository.save(tBSuppliesType);
        return result;
    }

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


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

    @Override
    @Transactional(readOnly = true)
    public Optional<TBSuppliesTypeDTO> findFirstByName(String name) {
        log.debug("Request to get TBSuppliesType : {}", name);
        return tBSuppliesTypeRepository.findFirstByName(name)
            .map(tBSuppliesTypeMapper::toDto);
    }

    /**
     * Delete the tBSuppliesType by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBSuppliesType : {}", id);
        tBSuppliesTypeRepository.deleteById(id);
        tBSuppliesTypeSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBSuppliesType 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<TBSuppliesTypeDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBSuppliesTypes for query {}", query);
        return tBSuppliesTypeSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBSuppliesTypeMapper::toDto);
    }
}
