package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBCustomerHasCropTypeService;
import vn.azteam.tpf.domain.TBCustomerHasCropType;
import vn.azteam.tpf.repository.TBCustomerHasCropTypeRepository;
import vn.azteam.tpf.repository.search.TBCustomerHasCropTypeSearchRepository;
import vn.azteam.tpf.service.dto.TBCustomerHasCropTypeDTO;
import vn.azteam.tpf.service.mapper.TBCustomerHasCropTypeMapper;
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 TBCustomerHasCropType.
 */
@Service
@Transactional
public class TBCustomerHasCropTypeServiceImpl implements TBCustomerHasCropTypeService {

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

    private final TBCustomerHasCropTypeRepository tBCustomerHasCropTypeRepository;

    private final TBCustomerHasCropTypeMapper tBCustomerHasCropTypeMapper;

    private final TBCustomerHasCropTypeSearchRepository tBCustomerHasCropTypeSearchRepository;

    public TBCustomerHasCropTypeServiceImpl(TBCustomerHasCropTypeRepository tBCustomerHasCropTypeRepository, TBCustomerHasCropTypeMapper tBCustomerHasCropTypeMapper, TBCustomerHasCropTypeSearchRepository tBCustomerHasCropTypeSearchRepository) {
        this.tBCustomerHasCropTypeRepository = tBCustomerHasCropTypeRepository;
        this.tBCustomerHasCropTypeMapper = tBCustomerHasCropTypeMapper;
        this.tBCustomerHasCropTypeSearchRepository = tBCustomerHasCropTypeSearchRepository;
    }

    /**
     * Save a tBCustomerHasCropType.
     *
     * @param tBCustomerHasCropTypeDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBCustomerHasCropTypeDTO save(TBCustomerHasCropTypeDTO tBCustomerHasCropTypeDTO) {
        log.debug("Request to save TBCustomerHasCropType : {}", tBCustomerHasCropTypeDTO);

        TBCustomerHasCropType tBCustomerHasCropType = tBCustomerHasCropTypeMapper.toEntity(tBCustomerHasCropTypeDTO);
        tBCustomerHasCropType = tBCustomerHasCropTypeRepository.save(tBCustomerHasCropType);
        TBCustomerHasCropTypeDTO result = tBCustomerHasCropTypeMapper.toDto(tBCustomerHasCropType);
        tBCustomerHasCropTypeSearchRepository.save(tBCustomerHasCropType);
        return result;
    }

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


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

    /**
     * Delete the tBCustomerHasCropType by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBCustomerHasCropType : {}", id);
        tBCustomerHasCropTypeRepository.deleteById(id);
        tBCustomerHasCropTypeSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBCustomerHasCropType 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<TBCustomerHasCropTypeDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBCustomerHasCropTypes for query {}", query);
        return tBCustomerHasCropTypeSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBCustomerHasCropTypeMapper::toDto);
    }
}
