package com.xydl.cac.service.impl;

import com.xydl.cac.entity.RemoteConfig;
import com.xydl.cac.entity.Zsb;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.repository.RemoteConfigRepository;
import com.xydl.cac.service.RemoteConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class RemoteConfigServiceImpl implements RemoteConfigService {

    @Resource
    RemoteConfigRepository repository;

    @Override
    public List<RemoteConfig> listAll() {
        List<RemoteConfig> list = repository.findAll();
        for (RemoteConfig server : list) {
            server.toList();
        }
        return list;
    }

    @Override
    public RemoteConfig get(Integer id) throws BusinessException {
        Optional<RemoteConfig> optional = repository.findById(id);
        if (!optional.isPresent()) {
            throw new BusinessException("未找到该记录");
        }
        return optional.get();
    }

    @Override
    public RemoteConfig add(RemoteConfig item) throws Exception {
        item.setId(null);
        item.fromList();
        return repository.save(item);
    }

    @Override
    public void update(RemoteConfig item) throws Exception {
        item.fromList();
        repository.save(item);
    }

    @Override
    public void delete(Integer id) {
        repository.deleteById(id);
    }
}
