package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBFunctionService;
import vn.azteam.tpf.domain.TBFunction;
import vn.azteam.tpf.repository.TBFunctionRepository;
import vn.azteam.tpf.repository.search.TBFunctionSearchRepository;
import vn.azteam.tpf.service.dto.TBFunctionDTO;
import vn.azteam.tpf.service.mapper.TBFunctionMapper;
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 static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing TBFunction.
 */
@Service
@Transactional
public class TBFunctionServiceImpl implements TBFunctionService {

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

    private final TBFunctionRepository tBFunctionRepository;

    private final TBFunctionMapper tBFunctionMapper;

    private final TBFunctionSearchRepository tBFunctionSearchRepository;

    public TBFunctionServiceImpl(TBFunctionRepository tBFunctionRepository, TBFunctionMapper tBFunctionMapper, TBFunctionSearchRepository tBFunctionSearchRepository) {
        this.tBFunctionRepository = tBFunctionRepository;
        this.tBFunctionMapper = tBFunctionMapper;
        this.tBFunctionSearchRepository = tBFunctionSearchRepository;
    }

    /**
     * Save a tBFunction.
     *
     * @param tBFunctionDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBFunctionDTO save(TBFunctionDTO tBFunctionDTO) {
        log.debug("Request to save TBFunction : {}", tBFunctionDTO);

        TBFunction tBFunction = tBFunctionMapper.toEntity(tBFunctionDTO);
        tBFunction = tBFunctionRepository.save(tBFunction);
        TBFunctionDTO result = tBFunctionMapper.toDto(tBFunction);
        tBFunctionSearchRepository.save(tBFunction);
        return result;
    }

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


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

    /**
     * Delete the tBFunction by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBFunction : {}", id);
        tBFunctionRepository.deleteById(id);
        tBFunctionSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBFunction 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<TBFunctionDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBFunctions for query {}", query);
        return tBFunctionSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBFunctionMapper::toDto);
    }

    @Override
    @Transactional(readOnly = true)
    public List<TBFunction> findByFunctionGroupId(Long functionGroupId) {
        log.debug("Request to search for a page of Functions for query {}", functionGroupId);
        return tBFunctionRepository.findTBFunctionsByTbFunctionGroupId(functionGroupId);
    }
}
