package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBProductUnitService;
import vn.azteam.tpf.domain.TBProductUnit;
import vn.azteam.tpf.repository.TBProductUnitRepository;
import vn.azteam.tpf.repository.search.TBProductUnitSearchRepository;
import vn.azteam.tpf.service.dto.TBProductUnitDTO;
import vn.azteam.tpf.service.mapper.TBProductUnitMapper;
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 TBProductUnit.
 */
@Service
@Transactional
public class TBProductUnitServiceImpl implements TBProductUnitService {

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

    private final TBProductUnitRepository tBProductUnitRepository;

    private final TBProductUnitMapper tBProductUnitMapper;

    private final TBProductUnitSearchRepository tBProductUnitSearchRepository;

    public TBProductUnitServiceImpl(TBProductUnitRepository tBProductUnitRepository, TBProductUnitMapper tBProductUnitMapper, TBProductUnitSearchRepository tBProductUnitSearchRepository) {
        this.tBProductUnitRepository = tBProductUnitRepository;
        this.tBProductUnitMapper = tBProductUnitMapper;
        this.tBProductUnitSearchRepository = tBProductUnitSearchRepository;
    }

    /**
     * Save a tBProductUnit.
     *
     * @param tBProductUnitDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBProductUnitDTO save(TBProductUnitDTO tBProductUnitDTO) {
        log.debug("Request to save TBProductUnit : {}", tBProductUnitDTO);

        TBProductUnit tBProductUnit = tBProductUnitMapper.toEntity(tBProductUnitDTO);
        tBProductUnit = tBProductUnitRepository.save(tBProductUnit);
        TBProductUnitDTO result = tBProductUnitMapper.toDto(tBProductUnit);
        tBProductUnitSearchRepository.save(tBProductUnit);
        return result;
    }

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


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

    /**
     * Delete the tBProductUnit by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBProductUnit : {}", id);
        tBProductUnitRepository.deleteById(id);
        tBProductUnitSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBProductUnit 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<TBProductUnitDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBProductUnits for query {}", query);
        return tBProductUnitSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBProductUnitMapper::toDto);
    }
}
