package com.gyee.wisdom.dao.sql.alarm;


import com.gyee.wisdom.common.data.alarm.FaultHistory;
import com.gyee.wisdom.dao.alarm.FaultHistoryDao;
import com.gyee.wisdom.dao.sql.alarm.Entity.FaultHistoryEntity;
import com.gyee.wisdom.dao.sql.alarm.repository.FaultHistoryRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class JpaFaultHistoryDao implements FaultHistoryDao {

    @Autowired
    private FaultHistoryRepository historyRepository;



    @Override
    public void save(FaultHistory faultHistory) {
        historyRepository.saveAndFlush(new FaultHistoryEntity(faultHistory));
    }

    @Override
    public void save(List<FaultHistory> faultHistorys) {
        ArrayList<FaultHistoryEntity> entities = new ArrayList<>();
        for(FaultHistory his : faultHistorys) {
            FaultHistoryEntity et = new FaultHistoryEntity(his);
            entities.add(et);
        }
        try {
            historyRepository.saveAll(entities);
            historyRepository.flush();
        }catch (Exception e){
            log.error("--------保存报警历史信息error--------{}",e.getMessage());
        }

    }

}
