package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBControlTypeService;
import vn.azteam.tpf.domain.TBControlType;
import vn.azteam.tpf.repository.TBControlTypeRepository;
import vn.azteam.tpf.repository.search.TBControlTypeSearchRepository;
import vn.azteam.tpf.service.dto.TBControlTypeDTO;
import vn.azteam.tpf.service.mapper.TBControlTypeMapper;
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 TBControlType.
 */
@Service
@Transactional
public class TBControlTypeServiceImpl implements TBControlTypeService {

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

    private final TBControlTypeRepository tBControlTypeRepository;

    private final TBControlTypeMapper tBControlTypeMapper;

    private final TBControlTypeSearchRepository tBControlTypeSearchRepository;

    public TBControlTypeServiceImpl(TBControlTypeRepository tBControlTypeRepository, TBControlTypeMapper tBControlTypeMapper, TBControlTypeSearchRepository tBControlTypeSearchRepository) {
        this.tBControlTypeRepository = tBControlTypeRepository;
        this.tBControlTypeMapper = tBControlTypeMapper;
        this.tBControlTypeSearchRepository = tBControlTypeSearchRepository;
    }

    /**
     * Save a tBControlType.
     *
     * @param tBControlTypeDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBControlTypeDTO save(TBControlTypeDTO tBControlTypeDTO) {
        log.debug("Request to save TBControlType : {}", tBControlTypeDTO);

        TBControlType tBControlType = tBControlTypeMapper.toEntity(tBControlTypeDTO);
        tBControlType = tBControlTypeRepository.save(tBControlType);
        TBControlTypeDTO result = tBControlTypeMapper.toDto(tBControlType);
        tBControlTypeSearchRepository.save(tBControlType);
        return result;
    }

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


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

    /**
     * Delete the tBControlType by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBControlType : {}", id);
        tBControlTypeRepository.deleteById(id);
        tBControlTypeSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBControlType 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<TBControlTypeDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBControlTypes for query {}", query);
        return tBControlTypeSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBControlTypeMapper::toDto);
    }
}
