package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBProductBlockDetailsReportService;
import vn.azteam.tpf.domain.TBProductBlockDetailsReport;
import vn.azteam.tpf.repository.TBProductBlockDetailsReportRepository;
import vn.azteam.tpf.repository.search.TBProductBlockDetailsReportSearchRepository;
import vn.azteam.tpf.service.dto.TBProductBlockDetailsReportDTO;
import vn.azteam.tpf.service.mapper.TBProductBlockDetailsReportMapper;
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 TBProductBlockDetailsReport.
 */
@Service
@Transactional
public class TBProductBlockDetailsReportServiceImpl implements TBProductBlockDetailsReportService {

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

    private final TBProductBlockDetailsReportRepository tBProductBlockDetailsReportRepository;

    private final TBProductBlockDetailsReportMapper tBProductBlockDetailsReportMapper;

    private final TBProductBlockDetailsReportSearchRepository tBProductBlockDetailsReportSearchRepository;

    public TBProductBlockDetailsReportServiceImpl(TBProductBlockDetailsReportRepository tBProductBlockDetailsReportRepository, TBProductBlockDetailsReportMapper tBProductBlockDetailsReportMapper, TBProductBlockDetailsReportSearchRepository tBProductBlockDetailsReportSearchRepository) {
        this.tBProductBlockDetailsReportRepository = tBProductBlockDetailsReportRepository;
        this.tBProductBlockDetailsReportMapper = tBProductBlockDetailsReportMapper;
        this.tBProductBlockDetailsReportSearchRepository = tBProductBlockDetailsReportSearchRepository;
    }

    /**
     * Save a tBProductBlockDetailsReport.
     *
     * @param tBProductBlockDetailsReportDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBProductBlockDetailsReportDTO save(TBProductBlockDetailsReportDTO tBProductBlockDetailsReportDTO) {
        log.debug("Request to save TBProductBlockDetailsReport : {}", tBProductBlockDetailsReportDTO);

        TBProductBlockDetailsReport tBProductBlockDetailsReport = tBProductBlockDetailsReportMapper.toEntity(tBProductBlockDetailsReportDTO);
        tBProductBlockDetailsReport = tBProductBlockDetailsReportRepository.save(tBProductBlockDetailsReport);
        TBProductBlockDetailsReportDTO result = tBProductBlockDetailsReportMapper.toDto(tBProductBlockDetailsReport);
        tBProductBlockDetailsReportSearchRepository.save(tBProductBlockDetailsReport);
        return result;
    }

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


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

    /**
     * Delete the tBProductBlockDetailsReport by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBProductBlockDetailsReport : {}", id);
        tBProductBlockDetailsReportRepository.deleteById(id);
        tBProductBlockDetailsReportSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBProductBlockDetailsReport 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<TBProductBlockDetailsReportDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBProductBlockDetailsReports for query {}", query);
        return tBProductBlockDetailsReportSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBProductBlockDetailsReportMapper::toDto);
    }
}
