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


import com.gyee.wisdom.common.data.DataUtil;
import com.gyee.wisdom.common.data.alarm.Alerthistory;
import com.gyee.wisdom.dao.alarm.AlarmhistoryDao;
import com.gyee.wisdom.dao.sql.alarm.Entity.AlerthistoryEntity;
import com.gyee.wisdom.dao.sql.alarm.repository.AlerthistoryRepository;
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 JpaAlerthistoryDao implements AlarmhistoryDao {
    @Autowired
    private AlerthistoryRepository alerthistoryRepository;
    @Override
    public List<Alerthistory> findAll() {
        List<Alerthistory> listalertsnap=DataUtil.convertDataList(alerthistoryRepository.findAll());
        return listalertsnap;
    }

    @Override
    public Alerthistory findById(Long id) {
        Alerthistory alerthistory=alerthistoryRepository.findById(id).get().toData();
        return alerthistory;
    }

    @Override
    public void deletebyid(Long id) {
        alerthistoryRepository.deleteById(id);
    }


    @Override
    public void save(Alerthistory alerthistory) {
        alerthistoryRepository.saveAndFlush(new AlerthistoryEntity(alerthistory));
    }

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

    }

//    @Override
//    public Alerthistory findByprojectId(String id) {
//        return alerthistoryRepository.findAllById(id).toData();
//    }


}
