package com.xydl.cac.service.impl;

import com.xydl.cac.entity.ThermalConfig;
import com.xydl.cac.repository.ThermalConfigRepository;
import com.xydl.cac.service.ThermalConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

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

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class ThermalConfigServiceImpl implements ThermalConfigService {

    @Resource
    ThermalConfigRepository repository;

    @Override
    public ThermalConfig getConfig() {
        ThermalConfig result = null;
        List<ThermalConfig> list = repository.findAll();
        if (!CollectionUtils.isEmpty(list)) {
            result = list.get(0);
        }
        return result;
    }

    @Override
    public void saveConfig(ThermalConfig model) {
        repository.deleteAll();
        model.setId(null);
        repository.save(model);
    }
}
