package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBFunctionGroupService;
import vn.azteam.tpf.domain.TBFunctionGroup;
import vn.azteam.tpf.repository.TBFunctionGroupRepository;
import vn.azteam.tpf.repository.search.TBFunctionGroupSearchRepository;
import vn.azteam.tpf.service.dto.TBFunctionGroupDTO;
import vn.azteam.tpf.service.mapper.TBFunctionGroupMapper;
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 TBFunctionGroup.
 */
@Service
@Transactional
public class TBFunctionGroupServiceImpl implements TBFunctionGroupService {

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

    private final TBFunctionGroupRepository tBFunctionGroupRepository;

    private final TBFunctionGroupMapper tBFunctionGroupMapper;

    private final TBFunctionGroupSearchRepository tBFunctionGroupSearchRepository;

    public TBFunctionGroupServiceImpl(TBFunctionGroupRepository tBFunctionGroupRepository, TBFunctionGroupMapper tBFunctionGroupMapper, TBFunctionGroupSearchRepository tBFunctionGroupSearchRepository) {
        this.tBFunctionGroupRepository = tBFunctionGroupRepository;
        this.tBFunctionGroupMapper = tBFunctionGroupMapper;
        this.tBFunctionGroupSearchRepository = tBFunctionGroupSearchRepository;
    }

    /**
     * Save a tBFunctionGroup.
     *
     * @param tBFunctionGroupDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBFunctionGroupDTO save(TBFunctionGroupDTO tBFunctionGroupDTO) {
        log.debug("Request to save TBFunctionGroup : {}", tBFunctionGroupDTO);

        TBFunctionGroup tBFunctionGroup = tBFunctionGroupMapper.toEntity(tBFunctionGroupDTO);
        tBFunctionGroup = tBFunctionGroupRepository.save(tBFunctionGroup);
        TBFunctionGroupDTO result = tBFunctionGroupMapper.toDto(tBFunctionGroup);
        tBFunctionGroupSearchRepository.save(tBFunctionGroup);
        return result;
    }

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


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

    /**
     * Delete the tBFunctionGroup by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBFunctionGroup : {}", id);
        tBFunctionGroupRepository.deleteById(id);
        tBFunctionGroupSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBFunctionGroup 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<TBFunctionGroupDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBFunctionGroups for query {}", query);
        return tBFunctionGroupSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBFunctionGroupMapper::toDto);
    }
}
