package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBEquipmentTypeService;
import vn.azteam.tpf.domain.TBEquipmentType;
import vn.azteam.tpf.repository.TBEquipmentTypeRepository;
import vn.azteam.tpf.repository.search.TBEquipmentTypeSearchRepository;
import vn.azteam.tpf.service.dto.TBEquipmentTypeDTO;
import vn.azteam.tpf.service.mapper.TBEquipmentTypeMapper;
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 TBEquipmentType.
 */
@Service
@Transactional
public class TBEquipmentTypeServiceImpl implements TBEquipmentTypeService {

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

    private final TBEquipmentTypeRepository tBEquipmentTypeRepository;

    private final TBEquipmentTypeMapper tBEquipmentTypeMapper;

    private final TBEquipmentTypeSearchRepository tBEquipmentTypeSearchRepository;

    public TBEquipmentTypeServiceImpl(TBEquipmentTypeRepository tBEquipmentTypeRepository, TBEquipmentTypeMapper tBEquipmentTypeMapper, TBEquipmentTypeSearchRepository tBEquipmentTypeSearchRepository) {
        this.tBEquipmentTypeRepository = tBEquipmentTypeRepository;
        this.tBEquipmentTypeMapper = tBEquipmentTypeMapper;
        this.tBEquipmentTypeSearchRepository = tBEquipmentTypeSearchRepository;
    }

    /**
     * Save a tBEquipmentType.
     *
     * @param tBEquipmentTypeDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBEquipmentTypeDTO save(TBEquipmentTypeDTO tBEquipmentTypeDTO) {
        log.debug("Request to save TBEquipmentType : {}", tBEquipmentTypeDTO);

        TBEquipmentType tBEquipmentType = tBEquipmentTypeMapper.toEntity(tBEquipmentTypeDTO);
        tBEquipmentType = tBEquipmentTypeRepository.save(tBEquipmentType);
        TBEquipmentTypeDTO result = tBEquipmentTypeMapper.toDto(tBEquipmentType);
        tBEquipmentTypeSearchRepository.save(tBEquipmentType);
        return result;
    }

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


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

    /**
     * Delete the tBEquipmentType by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBEquipmentType : {}", id);
        tBEquipmentTypeRepository.deleteById(id);
        tBEquipmentTypeSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBEquipmentType 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<TBEquipmentTypeDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBEquipmentTypes for query {}", query);
        return tBEquipmentTypeSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBEquipmentTypeMapper::toDto);
    }
}
