package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBCustomerService;
import vn.azteam.tpf.domain.TBCustomer;
import vn.azteam.tpf.repository.TBCustomerRepository;
import vn.azteam.tpf.repository.search.TBCustomerSearchRepository;
import vn.azteam.tpf.service.dto.TBCustomerDTO;
import vn.azteam.tpf.service.mapper.TBCustomerMapper;
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 TBCustomer.
 */
@Service
@Transactional
public class TBCustomerServiceImpl implements TBCustomerService {

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

    private final TBCustomerRepository tBCustomerRepository;

    private final TBCustomerMapper tBCustomerMapper;

    private final TBCustomerSearchRepository tBCustomerSearchRepository;

    public TBCustomerServiceImpl(TBCustomerRepository tBCustomerRepository, TBCustomerMapper tBCustomerMapper, TBCustomerSearchRepository tBCustomerSearchRepository) {
        this.tBCustomerRepository = tBCustomerRepository;
        this.tBCustomerMapper = tBCustomerMapper;
        this.tBCustomerSearchRepository = tBCustomerSearchRepository;
    }

    /**
     * Save a tBCustomer.
     *
     * @param tBCustomerDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBCustomerDTO save(TBCustomerDTO tBCustomerDTO) {
        log.debug("Request to save TBCustomer : {}", tBCustomerDTO);

        TBCustomer tBCustomer = tBCustomerMapper.toEntity(tBCustomerDTO);
        tBCustomer = tBCustomerRepository.save(tBCustomer);
        TBCustomerDTO result = tBCustomerMapper.toDto(tBCustomer);
        tBCustomerSearchRepository.save(tBCustomer);
        return result;
    }

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


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

    /**
     * Delete the tBCustomer by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBCustomer : {}", id);
        tBCustomerRepository.deleteById(id);
        tBCustomerSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBCustomer 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<TBCustomerDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBCustomers for query {}", query);
        return tBCustomerSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBCustomerMapper::toDto);
    }
}
