package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBObjectUpdateService;
import vn.azteam.tpf.domain.TBObjectUpdate;
import vn.azteam.tpf.repository.TBObjectUpdateRepository;
import vn.azteam.tpf.repository.search.TBObjectUpdateSearchRepository;
import vn.azteam.tpf.service.dto.TBObjectUpdateDTO;
import vn.azteam.tpf.service.mapper.TBObjectUpdateMapper;
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.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing TBObjectUpdate.
 */
@Service
@Transactional
public class TBObjectUpdateServiceImpl implements TBObjectUpdateService {

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

    private final TBObjectUpdateRepository tBObjectUpdateRepository;

    private final TBObjectUpdateMapper tBObjectUpdateMapper;

    private final TBObjectUpdateSearchRepository tBObjectUpdateSearchRepository;

    public TBObjectUpdateServiceImpl(TBObjectUpdateRepository tBObjectUpdateRepository, TBObjectUpdateMapper tBObjectUpdateMapper, TBObjectUpdateSearchRepository tBObjectUpdateSearchRepository) {
        this.tBObjectUpdateRepository = tBObjectUpdateRepository;
        this.tBObjectUpdateMapper = tBObjectUpdateMapper;
        this.tBObjectUpdateSearchRepository = tBObjectUpdateSearchRepository;
    }

    /**
     * Save a tBObjectUpdate.
     *
     * @param tBObjectUpdateDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBObjectUpdateDTO save(TBObjectUpdateDTO tBObjectUpdateDTO) {
        log.debug("Request to save TBObjectUpdate : {}", tBObjectUpdateDTO);

        TBObjectUpdate tBObjectUpdate = tBObjectUpdateMapper.toEntity(tBObjectUpdateDTO);
        tBObjectUpdate = tBObjectUpdateRepository.save(tBObjectUpdate);
        TBObjectUpdateDTO result = tBObjectUpdateMapper.toDto(tBObjectUpdate);
        tBObjectUpdateSearchRepository.save(tBObjectUpdate);
        return result;
    }

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


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

    /**
     * Delete the tBObjectUpdate by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBObjectUpdate : {}", id);
        tBObjectUpdateRepository.deleteById(id);
        tBObjectUpdateSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBObjectUpdate 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<TBObjectUpdateDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBObjectUpdates for query {}", query);
        return tBObjectUpdateSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBObjectUpdateMapper::toDto);
    }

    @Override
    public List<TBObjectUpdateDTO> findByTBActivityId(Long tbActivityId, Pageable pageable) {
        log.debug("Request get list TBObjectUpdates by tbActivityId: ", tbActivityId);
        return tBObjectUpdateRepository.findAll(pageable).map(tBObjectUpdateMapper::toDto)
            .getContent()
            .stream()
            .filter(item -> item.getTbActivityId()!= null && item.getTbActivityId().equals(tbActivityId))
            .collect(Collectors.toList());
    }

    @Override
    public List<TBObjectUpdateDTO> findByInListTBActivityId(List<Long> tbActivityIds, Pageable pageable) {
        return tBObjectUpdateRepository.findAll(pageable).map(tBObjectUpdateMapper::toDto)
            .getContent()
            .stream()
            .filter(item -> item.getTbActivityId() !=null && tbActivityIds.contains(item.getTbActivityId()))
            .collect(Collectors.toList());
    }

}
