package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBBlockDetailsReportService;
import vn.azteam.tpf.domain.TBBlockDetailsReport;
import vn.azteam.tpf.repository.TBBlockDetailsReportRepository;
import vn.azteam.tpf.repository.search.TBBlockDetailsReportSearchRepository;
import vn.azteam.tpf.service.dto.TBBlockDetailsReportDTO;
import vn.azteam.tpf.service.mapper.TBBlockDetailsReportMapper;
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 TBBlockDetailsReport.
 */
@Service
@Transactional
public class TBBlockDetailsReportServiceImpl implements TBBlockDetailsReportService {

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

    private final TBBlockDetailsReportRepository tBBlockDetailsReportRepository;

    private final TBBlockDetailsReportMapper tBBlockDetailsReportMapper;

    private final TBBlockDetailsReportSearchRepository tBBlockDetailsReportSearchRepository;

    public TBBlockDetailsReportServiceImpl(TBBlockDetailsReportRepository tBBlockDetailsReportRepository, TBBlockDetailsReportMapper tBBlockDetailsReportMapper, TBBlockDetailsReportSearchRepository tBBlockDetailsReportSearchRepository) {
        this.tBBlockDetailsReportRepository = tBBlockDetailsReportRepository;
        this.tBBlockDetailsReportMapper = tBBlockDetailsReportMapper;
        this.tBBlockDetailsReportSearchRepository = tBBlockDetailsReportSearchRepository;
    }

    /**
     * Save a tBBlockDetailsReport.
     *
     * @param tBBlockDetailsReportDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBBlockDetailsReportDTO save(TBBlockDetailsReportDTO tBBlockDetailsReportDTO) {
        log.debug("Request to save TBBlockDetailsReport : {}", tBBlockDetailsReportDTO);

        TBBlockDetailsReport tBBlockDetailsReport = tBBlockDetailsReportMapper.toEntity(tBBlockDetailsReportDTO);
        tBBlockDetailsReport = tBBlockDetailsReportRepository.save(tBBlockDetailsReport);
        TBBlockDetailsReportDTO result = tBBlockDetailsReportMapper.toDto(tBBlockDetailsReport);
        tBBlockDetailsReportSearchRepository.save(tBBlockDetailsReport);
        return result;
    }

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


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

    /**
     * Delete the tBBlockDetailsReport by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBBlockDetailsReport : {}", id);
        tBBlockDetailsReportRepository.deleteById(id);
        tBBlockDetailsReportSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBBlockDetailsReport 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<TBBlockDetailsReportDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBBlockDetailsReports for query {}", query);
        return tBBlockDetailsReportSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBBlockDetailsReportMapper::toDto);
    }
}
