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.common.data.alarm.AlerthistoryNew;
import com.gyee.wisdom.dao.alarm.AlarmhistoryNewDao;
import com.gyee.wisdom.dao.sql.alarm.Entity.AlerthistoryEntity;
import com.gyee.wisdom.dao.sql.alarm.Entity.AlerthistoryNewEntity;
import com.gyee.wisdom.dao.sql.alarm.repository.AlerthistoryNewRepository;
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 JpaAlerthistoryNewDao implements AlarmhistoryNewDao {
    @Autowired
    private AlerthistoryNewRepository alerthistoryNewRepository;
    @Override
    public List<AlerthistoryNew> findAll() {
        List<AlerthistoryNew> listalertsnap=DataUtil.convertDataList(alerthistoryNewRepository.findAll());
        return listalertsnap;
    }

    @Override
    public AlerthistoryNew findById(String id) {
        AlerthistoryNew alerthistory=alerthistoryNewRepository.findById(id).get().toData();
        return alerthistory;
    }

    @Override
    public void deletebyid(String id) {
        alerthistoryNewRepository.deleteById(id);
    }


    @Override
    public void save(AlerthistoryNew alerthistory) {
        alerthistoryNewRepository.saveAndFlush(new AlerthistoryNewEntity(alerthistory));
    }

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

    }

    @Override
    public AlerthistoryNew findByprojectId(String id) {
        return alerthistoryNewRepository.findAllById(id).toData();
    }


}
