package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBCropTypeService;
import vn.azteam.tpf.domain.TBCropType;
import vn.azteam.tpf.repository.TBCropTypeRepository;
import vn.azteam.tpf.repository.search.TBCropTypeSearchRepository;
import vn.azteam.tpf.service.dto.TBCropTypeDTO;
import vn.azteam.tpf.service.mapper.TBCropTypeMapper;
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 TBCropType.
 */
@Service
@Transactional
public class TBCropTypeServiceImpl implements TBCropTypeService {

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

    private final TBCropTypeRepository tBCropTypeRepository;

    private final TBCropTypeMapper tBCropTypeMapper;

    private final TBCropTypeSearchRepository tBCropTypeSearchRepository;

    public TBCropTypeServiceImpl(TBCropTypeRepository tBCropTypeRepository, TBCropTypeMapper tBCropTypeMapper, TBCropTypeSearchRepository tBCropTypeSearchRepository) {
        this.tBCropTypeRepository = tBCropTypeRepository;
        this.tBCropTypeMapper = tBCropTypeMapper;
        this.tBCropTypeSearchRepository = tBCropTypeSearchRepository;
    }

    /**
     * Save a tBCropType.
     *
     * @param tBCropTypeDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBCropTypeDTO save(TBCropTypeDTO tBCropTypeDTO) {
        log.debug("Request to save TBCropType : {}", tBCropTypeDTO);

        TBCropType tBCropType = tBCropTypeMapper.toEntity(tBCropTypeDTO);
        tBCropType = tBCropTypeRepository.save(tBCropType);
        TBCropTypeDTO result = tBCropTypeMapper.toDto(tBCropType);
        tBCropTypeSearchRepository.save(tBCropType);
        return result;
    }

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


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

    /**
     * Delete the tBCropType by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBCropType : {}", id);
        tBCropTypeRepository.deleteById(id);
        tBCropTypeSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBCropType 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<TBCropTypeDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBCropTypes for query {}", query);
        return tBCropTypeSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBCropTypeMapper::toDto);
    }
}
