package com.nokia.alarm.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nokia.alarm.entity.ResultMap;
import com.nokia.alarm.mapper.ResultMapDao;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class ResultMapService {

    private final ResultMapDao resultMapDao;

    public ResultMapService(ResultMapDao resultMapDao) {
        this.resultMapDao = resultMapDao;
    }

    public boolean isExist(ResultMap resultMap) {
        int count = resultMapDao.findByUniqueId(resultMap);
        return count != 0;
    }

    @Transactional
    public void updateOrInsertData(List<ResultMap> results) {
        if (results != null) {
            for (ResultMap result : results) {
                if (isExist(result)) {
                    log.debug("更新数据 {}", result.getUniqueId());
                    resultMapDao.updateData(result);
                } else {
                    resultMapDao.insertData(result);
                }
            }
        }
    }

    @Transactional
    public void insertData(List<ResultMap> results) {
        for (ResultMap result : results) {
            resultMapDao.insertData(result);
        }
    }
}
