package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBEquipmentHistoryService;
import vn.azteam.tpf.domain.TBEquipmentHistory;
import vn.azteam.tpf.repository.TBEquipmentHistoryRepository;
import vn.azteam.tpf.repository.search.TBEquipmentHistorySearchRepository;
import vn.azteam.tpf.service.dto.TBEquipmentHistoryDTO;
import vn.azteam.tpf.service.mapper.TBEquipmentHistoryMapper;
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 TBEquipmentHistory.
 */
@Service
@Transactional
public class TBEquipmentHistoryServiceImpl implements TBEquipmentHistoryService {

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

    private final TBEquipmentHistoryRepository tBEquipmentHistoryRepository;

    private final TBEquipmentHistoryMapper tBEquipmentHistoryMapper;

    private final TBEquipmentHistorySearchRepository tBEquipmentHistorySearchRepository;

    public TBEquipmentHistoryServiceImpl(TBEquipmentHistoryRepository tBEquipmentHistoryRepository, TBEquipmentHistoryMapper tBEquipmentHistoryMapper, TBEquipmentHistorySearchRepository tBEquipmentHistorySearchRepository) {
        this.tBEquipmentHistoryRepository = tBEquipmentHistoryRepository;
        this.tBEquipmentHistoryMapper = tBEquipmentHistoryMapper;
        this.tBEquipmentHistorySearchRepository = tBEquipmentHistorySearchRepository;
    }

    /**
     * Save a tBEquipmentHistory.
     *
     * @param tBEquipmentHistoryDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBEquipmentHistoryDTO save(TBEquipmentHistoryDTO tBEquipmentHistoryDTO) {
        log.debug("Request to save TBEquipmentHistory : {}", tBEquipmentHistoryDTO);

        TBEquipmentHistory tBEquipmentHistory = tBEquipmentHistoryMapper.toEntity(tBEquipmentHistoryDTO);
        tBEquipmentHistory = tBEquipmentHistoryRepository.save(tBEquipmentHistory);
        TBEquipmentHistoryDTO result = tBEquipmentHistoryMapper.toDto(tBEquipmentHistory);
        tBEquipmentHistorySearchRepository.save(tBEquipmentHistory);
        return result;
    }

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


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

    /**
     * Delete the tBEquipmentHistory by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBEquipmentHistory : {}", id);
        tBEquipmentHistoryRepository.deleteById(id);
        tBEquipmentHistorySearchRepository.deleteById(id);
    }

    /**
     * Search for the tBEquipmentHistory 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<TBEquipmentHistoryDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBEquipmentHistories for query {}", query);
        return tBEquipmentHistorySearchRepository.search(queryStringQuery(query), pageable)
            .map(tBEquipmentHistoryMapper::toDto);
    }
}
