package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBLimitsService;
import vn.azteam.tpf.domain.TBLimits;
import vn.azteam.tpf.repository.TBLimitsRepository;
import vn.azteam.tpf.repository.search.TBLimitsSearchRepository;
import vn.azteam.tpf.service.dto.TBLimitsDTO;
import vn.azteam.tpf.service.mapper.TBLimitsMapper;
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 TBLimits.
 */
@Service
@Transactional
public class TBLimitsServiceImpl implements TBLimitsService {

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

    private final TBLimitsRepository tBLimitsRepository;

    private final TBLimitsMapper tBLimitsMapper;

    private final TBLimitsSearchRepository tBLimitsSearchRepository;

    public TBLimitsServiceImpl(TBLimitsRepository tBLimitsRepository, TBLimitsMapper tBLimitsMapper, TBLimitsSearchRepository tBLimitsSearchRepository) {
        this.tBLimitsRepository = tBLimitsRepository;
        this.tBLimitsMapper = tBLimitsMapper;
        this.tBLimitsSearchRepository = tBLimitsSearchRepository;
    }

    /**
     * Save a tBLimits.
     *
     * @param tBLimitsDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBLimitsDTO save(TBLimitsDTO tBLimitsDTO) {
        log.debug("Request to save TBLimits : {}", tBLimitsDTO);

        TBLimits tBLimits = tBLimitsMapper.toEntity(tBLimitsDTO);
        tBLimits = tBLimitsRepository.save(tBLimits);
        TBLimitsDTO result = tBLimitsMapper.toDto(tBLimits);
        tBLimitsSearchRepository.save(tBLimits);
        return result;
    }

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


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

    /**
     * Delete the tBLimits by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBLimits : {}", id);
        tBLimitsRepository.deleteById(id);
        tBLimitsSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBLimits 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<TBLimitsDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBLimits for query {}", query);
        return tBLimitsSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBLimitsMapper::toDto);
    }
}
