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


import com.gyee.wisdom.common.data.DataUtil;
import com.gyee.wisdom.common.data.alarm.Alertsnap;
import com.gyee.wisdom.common.data.alarm.AlertsnapNew;
import com.gyee.wisdom.dao.alarm.AlarmsnapNewDao;
import com.gyee.wisdom.dao.sql.alarm.Entity.AlertsnapEntity;
import com.gyee.wisdom.dao.sql.alarm.Entity.AlertsnapNewEntity;
import com.gyee.wisdom.dao.sql.alarm.repository.AlertsnapNewRepository;
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 JpaAlertsnapNewDao implements AlarmsnapNewDao{
    @Autowired
    private AlertsnapNewRepository alertsnapNewRepository;
    @Override
    public List<AlertsnapNew> findAll() {
        List<AlertsnapNew> listalertsnap=DataUtil.convertDataList(alertsnapNewRepository.findAll());
        return listalertsnap;
    }

    @Override
    public AlertsnapNew findById(String id) {
        AlertsnapNew alertsnap=alertsnapNewRepository.findById(id).get().toData();
        return alertsnap;
    }

    @Override
    public int countById(Long id) {
        return alertsnapNewRepository.countById(id);
    }
    @Override
    public void deletebyid(String id) {
        alertsnapNewRepository.deleteById(id);
    }


    @Override
    public AlertsnapNew save(AlertsnapNew alertsnap) {
       return alertsnapNewRepository.saveAndFlush(new AlertsnapNewEntity(alertsnap)).toData();
    }

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


    }

    @Override
    public AlertsnapNew findByWindturbineIdAndAlertValue(String windturbineid, int alertvalue) {
       AlertsnapNewEntity alertsnapEntity = alertsnapNewRepository.findByWindturbineIdAndAlertValue(windturbineid,alertvalue);
        if(alertsnapEntity!=null) {
            return alertsnapEntity.toData();
        }else {
            return null;
        }
    }

    @Override
    public AlertsnapNew findByStationIdAndAlertValue(String stationId, int alertvalue) {
        AlertsnapNewEntity alertsnapEntity = alertsnapNewRepository.findByStationIdAndAlertValue(stationId,alertvalue);
        if(alertsnapEntity!=null) {
            return alertsnapEntity.toData();
        }else {
            return null;
        }
    }


    @Override
    public AlertsnapNew findByTestingpointkey(String testingpointkey){
        AlertsnapNewEntity alertsnapEntity = alertsnapNewRepository.findByTestingpointKey(testingpointkey);
        return alertsnapEntity==null?null:alertsnapEntity.toData();
    }

}
