package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBDetailUserService;
import vn.azteam.tpf.domain.TBDetailUser;
import vn.azteam.tpf.repository.TBDetailUserRepository;
import vn.azteam.tpf.repository.search.TBDetailUserSearchRepository;
import vn.azteam.tpf.service.dto.TBDetailUserDTO;
import vn.azteam.tpf.service.mapper.TBDetailUserMapper;
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 TBDetailUser.
 */
@Service
@Transactional
public class TBDetailUserServiceImpl implements TBDetailUserService {

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

    private final TBDetailUserRepository tBDetailUserRepository;

    private final TBDetailUserMapper tBDetailUserMapper;

    private final TBDetailUserSearchRepository tBDetailUserSearchRepository;

    public TBDetailUserServiceImpl(TBDetailUserRepository tBDetailUserRepository, TBDetailUserMapper tBDetailUserMapper, TBDetailUserSearchRepository tBDetailUserSearchRepository) {
        this.tBDetailUserRepository = tBDetailUserRepository;
        this.tBDetailUserMapper = tBDetailUserMapper;
        this.tBDetailUserSearchRepository = tBDetailUserSearchRepository;
    }

    /**
     * Save a tBDetailUser.
     *
     * @param tBDetailUserDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBDetailUserDTO save(TBDetailUserDTO tBDetailUserDTO) {
        log.debug("Request to save TBDetailUser : {}", tBDetailUserDTO);

        TBDetailUser tBDetailUser = tBDetailUserMapper.toEntity(tBDetailUserDTO);
        tBDetailUser = tBDetailUserRepository.save(tBDetailUser);
        TBDetailUserDTO result = tBDetailUserMapper.toDto(tBDetailUser);
        tBDetailUserSearchRepository.save(tBDetailUser);
        return result;
    }

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


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

    /**
     * Delete the tBDetailUser by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBDetailUser : {}", id);
        tBDetailUserRepository.deleteById(id);
        tBDetailUserSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBDetailUser 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<TBDetailUserDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBDetailUsers for query {}", query);
        return tBDetailUserSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBDetailUserMapper::toDto);
    }
}
