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

import com.gyee.wisdom.common.data.DataUtil;
import com.gyee.wisdom.common.data.alarm.FaultSnap;
import com.gyee.wisdom.dao.alarm.FaultSnapDao;
import com.gyee.wisdom.dao.sql.alarm.Entity.FaultHistoryEntity;
import com.gyee.wisdom.dao.sql.alarm.Entity.FaultSnapEntity;
import com.gyee.wisdom.dao.sql.alarm.repository.FaultHistoryRepository;
import com.gyee.wisdom.dao.sql.alarm.repository.FaultSnapRepository;
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 JpaFaultSnapDao implements FaultSnapDao{
    @Autowired
    private FaultSnapRepository faultSnapRepository;

    @Autowired
    private FaultHistoryRepository faultHistoryRepository;

    @Override
    public FaultSnap save(FaultSnap alertsnap) {
       return faultSnapRepository.saveAndFlush(new FaultSnapEntity(alertsnap)).toData();
    }

    @Override
    public void save(List<FaultSnap> alertsnaps) {
        ArrayList<FaultSnapEntity> entities = new ArrayList<>();
        for(FaultSnap his : alertsnaps) {
            FaultSnapEntity et = new FaultSnapEntity(his);
            entities.add(et);
        }
        try{
            List<FaultSnapEntity> results = faultSnapRepository.saveAll(entities);
            faultSnapRepository.flush();
            ArrayList<FaultHistoryEntity> hisEntities = new ArrayList<>();
            for(FaultSnapEntity snapEntity : results) {
                FaultHistoryEntity hisE = createFaultHistoryEntity(snapEntity);
                hisEntities.add(hisE);
            }
            faultHistoryRepository.saveAll(hisEntities);
            faultHistoryRepository.flush();

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

    }

    public static FaultHistoryEntity createFaultHistoryEntity(FaultSnapEntity snap) {
        FaultHistoryEntity his = new FaultHistoryEntity();

        his.setSnapId(snap.getId());
        his.setFaultTime(snap.getLastUpdateTime());
        his.setOperator(snap.getLastUpdatePerson());
        his.setConfirmType(0);

        if (snap.getIsOpened() != 0 )
            his.setMessageType(1);
        else
            his.setMessageType(3);

        return his;
    }

    @Override
    public FaultSnap findByWindturbineIdAndAlertValue(String windturbineid, int alertvalue) {
       FaultSnapEntity alertsnapEntity = faultSnapRepository.findByWindturbineIdAndAlertValue(windturbineid,alertvalue);
        if(alertsnapEntity!=null) {
            return alertsnapEntity.toData();
        }else {
            return null;
        }
    }

    @Override
    public FaultSnap findByStationIdAndAlertValue(String stationId, int alertvalue) {
        FaultSnapEntity alertsnapEntity = faultSnapRepository.findByStationIdAndAlertValue(stationId,alertvalue);
        if(alertsnapEntity!=null) {
            return alertsnapEntity.toData();
        }else {
            return null;
        }
    }

    @Override
    public FaultSnap findByTestingpointkey(String testingpointkey){
        FaultSnapEntity alertsnapEntity = faultSnapRepository.findByTestingpointKey(testingpointkey);
        return alertsnapEntity==null?null:alertsnapEntity.toData();
    }

    @Override
    public List<FaultSnap> findAllFaultWindturbineId(){
        List<FaultSnap> resultList=new ArrayList<>();
      List<FaultSnapEntity> entityList=  faultSnapRepository.searchAllFaultWindturbineId();
       for (int i=0;i<entityList.size();i++){
           resultList.add(entityList.get(i).toData())  ;
       }
       return  resultList;
    }

}
