package vn.azteam.tpf.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vn.azteam.tpf.domain.TBCode;
import vn.azteam.tpf.repository.TBCodeRepository;
import vn.azteam.tpf.repository.search.TBCodeSearchRepository;
import vn.azteam.tpf.service.TBCodeService;
import vn.azteam.tpf.service.dto.TBCodeDTO;
import vn.azteam.tpf.service.mapper.TBCodeMapper;
import vn.azteam.tpf.web.rest.errors.BadRequestAlertException;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing TBCode.
 */


@Service
@Transactional
public class TBCodeServiceImpl implements TBCodeService {

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

    private final TBCodeRepository tbCodeRepository;

    private final TBCodeMapper tBCodeMapper;

    private final TBCodeSearchRepository tBCodeSearchRepository;

    public TBCodeServiceImpl(TBCodeRepository tbCodeRepository,
                             TBCodeMapper tBCodeMapper, TBCodeSearchRepository tBCodeSearchRepository) {
        this.tbCodeRepository = tbCodeRepository;
        this.tBCodeMapper = tBCodeMapper;
        this.tBCodeSearchRepository = tBCodeSearchRepository;
    }


    @Override
    public TBCodeDTO save(TBCodeDTO tBCodeDTO) {
        log.debug("Request to save TBCodeDTO : {}", tBCodeDTO);

        TBCode tbCode = tBCodeMapper.toEntity(tBCodeDTO);
        tbCode = tbCodeRepository.save(tbCode);
        TBCodeDTO result = tBCodeMapper.toDto(tbCode);
        tBCodeSearchRepository.save(tbCode);
        return result;
    }

    @Override
    public List<TBCodeDTO> saveAll(List<TBCodeDTO> tBCodeDTOs) {
        List<TBCode> tbCodes = new ArrayList<>();
        for (TBCodeDTO tbCodeDTO : tBCodeDTOs) {
            TBCode tbCode = tBCodeMapper.toEntity(tbCodeDTO);
            tbCodes.add(tbCode);
        }
        tbCodes = tbCodeRepository.saveAll(tbCodes);
        return tbCodes.stream().map(tBCodeMapper::toDto).collect(Collectors.toList());
    }

    @Override
    @Transactional(readOnly = true)
    public Optional<TBCodeDTO> findOne(Long id) {
        log.debug("Request to get TBCode : {}", id);
        return tbCodeRepository.findOneWithEagerRelationships(id)
            .map(tBCodeMapper::toDto);
    }

    @Override
    @Transactional(readOnly = true)
    public Optional<TBCodeDTO> updateStatusTBCode(TBCodeDTO tbCodeDTO) {
        log.debug("Request to updateTBCode TBCode id: {}", tbCodeDTO.getId());
        Optional<TBCode> tbCodeOptional = tbCodeRepository.findById(tbCodeDTO.getId());
        if (!tbCodeOptional.isPresent()) {
            throw new BadRequestAlertException("Not found TBCode", "",
                tbCodeDTO.getId().toString());
        }

        TBCode tbCode = tBCodeMapper.toEntity(tbCodeDTO);
        tbCode = tbCodeRepository.save(tbCode);
        return Optional.ofNullable(tBCodeMapper.toDto(tbCode));
    }
}
