package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBEnvironmentalParameterService;
import vn.azteam.tpf.domain.TBEnvironmentalParameter;
import vn.azteam.tpf.repository.TBEnvironmentalParameterRepository;
import vn.azteam.tpf.repository.search.TBEnvironmentalParameterSearchRepository;
import vn.azteam.tpf.service.dto.TBEnvironmentalParameterDTO;
import vn.azteam.tpf.service.mapper.TBEnvironmentalParameterMapper;
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 TBEnvironmentalParameter.
 */
@Service
@Transactional
public class TBEnvironmentalParameterServiceImpl implements TBEnvironmentalParameterService {

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

    private final TBEnvironmentalParameterRepository tBEnvironmentalParameterRepository;

    private final TBEnvironmentalParameterMapper tBEnvironmentalParameterMapper;

    private final TBEnvironmentalParameterSearchRepository tBEnvironmentalParameterSearchRepository;

    public TBEnvironmentalParameterServiceImpl(TBEnvironmentalParameterRepository tBEnvironmentalParameterRepository, TBEnvironmentalParameterMapper tBEnvironmentalParameterMapper, TBEnvironmentalParameterSearchRepository tBEnvironmentalParameterSearchRepository) {
        this.tBEnvironmentalParameterRepository = tBEnvironmentalParameterRepository;
        this.tBEnvironmentalParameterMapper = tBEnvironmentalParameterMapper;
        this.tBEnvironmentalParameterSearchRepository = tBEnvironmentalParameterSearchRepository;
    }

    /**
     * Save a tBEnvironmentalParameter.
     *
     * @param tBEnvironmentalParameterDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBEnvironmentalParameterDTO save(TBEnvironmentalParameterDTO tBEnvironmentalParameterDTO) {
        log.debug("Request to save TBEnvironmentalParameter : {}", tBEnvironmentalParameterDTO);

        TBEnvironmentalParameter tBEnvironmentalParameter = tBEnvironmentalParameterMapper.toEntity(tBEnvironmentalParameterDTO);
        tBEnvironmentalParameter = tBEnvironmentalParameterRepository.save(tBEnvironmentalParameter);
        TBEnvironmentalParameterDTO result = tBEnvironmentalParameterMapper.toDto(tBEnvironmentalParameter);
        tBEnvironmentalParameterSearchRepository.save(tBEnvironmentalParameter);
        return result;
    }

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


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

    /**
     * Delete the tBEnvironmentalParameter by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBEnvironmentalParameter : {}", id);
        tBEnvironmentalParameterRepository.deleteById(id);
        tBEnvironmentalParameterSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBEnvironmentalParameter 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<TBEnvironmentalParameterDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBEnvironmentalParameters for query {}", query);
        return tBEnvironmentalParameterSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBEnvironmentalParameterMapper::toDto);
    }
}
