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.Alertsnap;
import com.gyee.wisdom.dao.alarm.AlarmsnapDao;
import com.gyee.wisdom.dao.sql.alarm.Entity.AlerthistoryEntity;
import com.gyee.wisdom.dao.sql.alarm.Entity.AlertsnapEntity;
import com.gyee.wisdom.dao.sql.alarm.repository.AlerthistoryRepository;
import com.gyee.wisdom.dao.sql.alarm.repository.AlertsnapRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
@Slf4j
public class JpaAlertsnapDao implements AlarmsnapDao{
    @Autowired
    private AlertsnapRepository alertsnapRepository;

    @Autowired
    private AlerthistoryRepository alerthistoryRepository;

    @Override
    public List<Alertsnap> findAll() {
        List<Alertsnap> listalertsnap=DataUtil.convertDataList(alertsnapRepository.findAll());
        return listalertsnap;
    }

    @Override
    public Alertsnap findById(Long id) {
        Alertsnap alertsnap=alertsnapRepository.findById(id).get().toData();
        return alertsnap;
    }

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


    @Override
    public Alertsnap save(Alertsnap alertsnap) {
       return alertsnapRepository.saveAndFlush(new AlertsnapEntity(alertsnap)).toData();
    }

    @Override
    public void save(List<Alertsnap> alertsnaps) {
        ArrayList<AlertsnapEntity> entities = new ArrayList<>();
        for(Alertsnap his : alertsnaps) {
            AlertsnapEntity et = new AlertsnapEntity(his);
            entities.add(et);
        }
        try{
            List<AlertsnapEntity> results = alertsnapRepository.saveAll(entities);

            ArrayList<AlerthistoryEntity> hisEntities = new ArrayList<>();
            for(AlertsnapEntity snapEntity : results) {
                AlerthistoryEntity hisE = createAlerthistoryEntity(snapEntity);
                hisEntities.add(hisE);
            }
            alerthistoryRepository.saveAll(hisEntities);

            alertsnapRepository.flush();
            alerthistoryRepository.flush();
        }catch (Exception e){
            log.error("--------保存报警信息error--------{}",e.getMessage());
        }
    }

    public static AlerthistoryEntity createAlerthistoryEntity(AlertsnapEntity snap) {
        AlerthistoryEntity his = new AlerthistoryEntity();

        his.setSnapId(snap.getId());
        his.setAlertTime(snap.getLastUpdateTime());
        his.setOperator(snap.getLastUpdatePerson());
        his.setDataInfo(snap.getDataInfo());
        his.setIsConfirmed(snap.getIsConfirmed());
        if (snap.getIsConfirmed() != 0)
            his.setMessageType(2); //确认报警
        else if (snap.getIsOpened() != 0)
            his.setMessageType(1); //触发报警
        else {
            his.setMessageType(3);  //解除报警
            //his.setAlertTime(snap.getLastCloseTime()!= null ? snap.getLastCloseTime() : new Date());
            his.setOperator(snap.getLastClosePerson());
        }

        return his;
    }


//    @Override
//    public Alertsnap findByprojectId(Long id) {
//        return alertsnapRepository.findAllById(id).toData();
//    }

    @Override
    public Alertsnap findByWindturbineIdAndAlertValue(String windturbineid, int alertvalue) {
       AlertsnapEntity alertsnapEntity = alertsnapRepository.findByWindturbineIdAndAlertValue(windturbineid,alertvalue);
        if(alertsnapEntity!=null) {
            return alertsnapEntity.toData();
        }else {
            return null;
        }
    }

    @Override
    public Alertsnap findByStationIdAndAlertValue(String stationId, int alertvalue) {
        AlertsnapEntity alertsnapEntity = alertsnapRepository.findByStationIdAndAlertValue(stationId,alertvalue);
        if(alertsnapEntity!=null) {
            return alertsnapEntity.toData();
        }else {
            return null;
        }
    }


    @Override
    public Alertsnap findByTestingpointkey(String testingpointkey){
        AlertsnapEntity alertsnapEntity = alertsnapRepository.findByTestingpointKey(testingpointkey);
        return alertsnapEntity==null?null:alertsnapEntity.toData();
    }

}
