package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBEquipmentOfCustomerService;
import vn.azteam.tpf.domain.TBEquipmentOfCustomer;
import vn.azteam.tpf.repository.TBEquipmentOfCustomerRepository;
import vn.azteam.tpf.repository.search.TBEquipmentOfCustomerSearchRepository;
import vn.azteam.tpf.service.dto.TBEquipmentOfCustomerDTO;
import vn.azteam.tpf.service.mapper.TBEquipmentOfCustomerMapper;
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 TBEquipmentOfCustomer.
 */
@Service
@Transactional
public class TBEquipmentOfCustomerServiceImpl implements TBEquipmentOfCustomerService {

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

    private final TBEquipmentOfCustomerRepository tBEquipmentOfCustomerRepository;

    private final TBEquipmentOfCustomerMapper tBEquipmentOfCustomerMapper;

    private final TBEquipmentOfCustomerSearchRepository tBEquipmentOfCustomerSearchRepository;

    public TBEquipmentOfCustomerServiceImpl(TBEquipmentOfCustomerRepository tBEquipmentOfCustomerRepository, TBEquipmentOfCustomerMapper tBEquipmentOfCustomerMapper, TBEquipmentOfCustomerSearchRepository tBEquipmentOfCustomerSearchRepository) {
        this.tBEquipmentOfCustomerRepository = tBEquipmentOfCustomerRepository;
        this.tBEquipmentOfCustomerMapper = tBEquipmentOfCustomerMapper;
        this.tBEquipmentOfCustomerSearchRepository = tBEquipmentOfCustomerSearchRepository;
    }

    /**
     * Save a tBEquipmentOfCustomer.
     *
     * @param tBEquipmentOfCustomerDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBEquipmentOfCustomerDTO save(TBEquipmentOfCustomerDTO tBEquipmentOfCustomerDTO) {
        log.debug("Request to save TBEquipmentOfCustomer : {}", tBEquipmentOfCustomerDTO);

        TBEquipmentOfCustomer tBEquipmentOfCustomer = tBEquipmentOfCustomerMapper.toEntity(tBEquipmentOfCustomerDTO);
        tBEquipmentOfCustomer = tBEquipmentOfCustomerRepository.save(tBEquipmentOfCustomer);
        TBEquipmentOfCustomerDTO result = tBEquipmentOfCustomerMapper.toDto(tBEquipmentOfCustomer);
        tBEquipmentOfCustomerSearchRepository.save(tBEquipmentOfCustomer);
        return result;
    }

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


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

    /**
     * Delete the tBEquipmentOfCustomer by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBEquipmentOfCustomer : {}", id);
        tBEquipmentOfCustomerRepository.deleteById(id);
        tBEquipmentOfCustomerSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBEquipmentOfCustomer 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<TBEquipmentOfCustomerDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBEquipmentOfCustomers for query {}", query);
        return tBEquipmentOfCustomerSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBEquipmentOfCustomerMapper::toDto);
    }
}
