package com.xydl.cac.service.impl;

import com.xydl.cac.entity.ThermalConfig;
import com.xydl.cac.model.ThermalConfigModel;
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 ThermalConfigModel getConfig() {
        ThermalConfigModel model = new ThermalConfigModel();
        List<ThermalConfig> list = repository.findAll();
        if (!CollectionUtils.isEmpty(list)) {
            model.setTypeId(list.get(0).getTypeId());
            model.setList(list);
        }
        return model;
    }

    @Override
    public void saveConfig(ThermalConfigModel model) {
        repository.deleteAll();
        for (ThermalConfig item : model.getList()) {
            item.setId(null);
            item.setTypeId(model.getTypeId());
            repository.save(item);
        }
    }
}
