package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBLocationOfCustomerService;
import vn.azteam.tpf.domain.TBLocationOfCustomer;
import vn.azteam.tpf.repository.TBLocationOfCustomerRepository;
import vn.azteam.tpf.repository.search.TBLocationOfCustomerSearchRepository;
import vn.azteam.tpf.service.dto.TBLocationOfCustomerDTO;
import vn.azteam.tpf.service.mapper.TBLocationOfCustomerMapper;
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 TBLocationOfCustomer.
 */
@Service
@Transactional
public class TBLocationOfCustomerServiceImpl implements TBLocationOfCustomerService {

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

    private final TBLocationOfCustomerRepository tBLocationOfCustomerRepository;

    private final TBLocationOfCustomerMapper tBLocationOfCustomerMapper;

    private final TBLocationOfCustomerSearchRepository tBLocationOfCustomerSearchRepository;

    public TBLocationOfCustomerServiceImpl(TBLocationOfCustomerRepository tBLocationOfCustomerRepository, TBLocationOfCustomerMapper tBLocationOfCustomerMapper, TBLocationOfCustomerSearchRepository tBLocationOfCustomerSearchRepository) {
        this.tBLocationOfCustomerRepository = tBLocationOfCustomerRepository;
        this.tBLocationOfCustomerMapper = tBLocationOfCustomerMapper;
        this.tBLocationOfCustomerSearchRepository = tBLocationOfCustomerSearchRepository;
    }

    /**
     * Save a tBLocationOfCustomer.
     *
     * @param tBLocationOfCustomerDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBLocationOfCustomerDTO save(TBLocationOfCustomerDTO tBLocationOfCustomerDTO) {
        log.debug("Request to save TBLocationOfCustomer : {}", tBLocationOfCustomerDTO);

        TBLocationOfCustomer tBLocationOfCustomer = tBLocationOfCustomerMapper.toEntity(tBLocationOfCustomerDTO);
        tBLocationOfCustomer = tBLocationOfCustomerRepository.save(tBLocationOfCustomer);
        TBLocationOfCustomerDTO result = tBLocationOfCustomerMapper.toDto(tBLocationOfCustomer);
        tBLocationOfCustomerSearchRepository.save(tBLocationOfCustomer);
        return result;
    }

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


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

    /**
     * Delete the tBLocationOfCustomer by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBLocationOfCustomer : {}", id);
        tBLocationOfCustomerRepository.deleteById(id);
        tBLocationOfCustomerSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBLocationOfCustomer 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<TBLocationOfCustomerDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBLocationOfCustomers for query {}", query);
        return tBLocationOfCustomerSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBLocationOfCustomerMapper::toDto);
    }
}
