package com.dy.pipIrrRemote.rtuUpgrage; 
 | 
  
 | 
import com.dy.common.softUpgrade.state.UpgradeRtu; 
 | 
import com.dy.common.webUtil.QueryResultVo; 
 | 
import com.dy.pipIrrGlobal.voUg.VoUgRtuResult4Failure; 
 | 
import com.dy.pipIrrGlobal.voUg.VoUgRtuResult4Success; 
 | 
import com.dy.pipIrrGlobal.voUg.VoWatch; 
 | 
import lombok.extern.slf4j.Slf4j; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.stereotype.Service; 
 | 
  
 | 
import java.util.ArrayList; 
 | 
import java.util.List; 
 | 
  
 | 
/** 
 | 
 * @Author liurunyu 
 | 
 * @Date 2024/11/22 14:40 
 | 
 * @Description 
 | 
 */ 
 | 
@Slf4j 
 | 
@Service 
 | 
public class RtuUpgradeResSv { 
 | 
    @Autowired 
 | 
    private RtuUpgradeSv sv ; 
 | 
  
 | 
    public QueryResultVo<VoWatch> curUpgradeState(QueryVo qvo){ 
 | 
        QueryResultVo<VoWatch> rsVo = new QueryResultVo<>(); 
 | 
        VoWatch vo = new VoWatch() ; 
 | 
        List<UpgradeRtu> listFiltered = null ; 
 | 
        if(RtuUpgradeStateReceiverCtrl.cache != null && RtuUpgradeStateReceiverCtrl.cache.ugTaskId != null){ 
 | 
            vo.upgrade = this.sv.selectTaskDetail(RtuUpgradeStateReceiverCtrl.cache.ugTaskId); 
 | 
            vo.overall = RtuUpgradeStateReceiverCtrl.cache.ugOverallState ; 
 | 
            vo.rtus = new ArrayList<>() ; 
 | 
            if(RtuUpgradeStateReceiverCtrl.cache.ugRtuStateList != null && RtuUpgradeStateReceiverCtrl.cache.ugRtuStateList.size() > 0) { 
 | 
                listFiltered = filterByQuery(qvo) ; 
 | 
                if (listFiltered.size() > 0) { 
 | 
                    if(qvo.pageCurr < 1){ 
 | 
                        qvo.pageCurr = 1 ; 
 | 
                    } 
 | 
                    int start = (qvo.pageCurr - 1) * qvo.pageSize ; 
 | 
                    if(start >= listFiltered.size()){ 
 | 
                        if(listFiltered.size()%qvo.pageSize > 0){ 
 | 
                            start = listFiltered.size() - listFiltered.size()%qvo.pageSize ; 
 | 
                        }else{ 
 | 
                            start = listFiltered.size() - qvo.pageSize ; 
 | 
                        } 
 | 
                    } 
 | 
                    for(int i = start; i < (start + qvo.pageSize) && i < listFiltered.size(); i++){ 
 | 
                        UpgradeRtu ugRtu = listFiltered.get(i) ; 
 | 
                        VoWatch.VoWatchRtu rtu = new VoWatch.VoWatchRtu() ; 
 | 
                        rtu.fromCache(ugRtu) ; 
 | 
                        vo.rtus.add(rtu) ; 
 | 
                    } 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
  
 | 
        rsVo.obj = vo ; 
 | 
        rsVo.pageSize = qvo.pageSize ; 
 | 
        rsVo.pageCurr = qvo.pageCurr ; 
 | 
        rsVo.calculateAndSet((long)(listFiltered==null?0:listFiltered.size()), null); 
 | 
        return rsVo ; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 过滤结果 
 | 
     * @param qvo 查询参数 
 | 
     * @return 集合 
 | 
     */ 
 | 
    private List<UpgradeRtu> filterByQuery(QueryVo qvo){ 
 | 
        if (qvo.status != null || qvo.result != null) { 
 | 
            Integer qvoStatus = qvo.status ; 
 | 
            Integer qvoResult = qvo.result ; 
 | 
            String qvoRtuAddr = qvo.rtuAddr ; 
 | 
            return RtuUpgradeStateReceiverCtrl.cache.ugRtuStateList.stream().filter(rtu -> { 
 | 
                boolean ok = false; 
 | 
                if (qvoStatus != null) { 
 | 
                    if (qvoStatus == 1) { 
 | 
                        ok = rtu.state == UpgradeRtu.STATE_RUNNING; 
 | 
                    } else if (qvoStatus == 0) { 
 | 
                        ok = rtu.isOver; 
 | 
                    } 
 | 
                } 
 | 
                if (qvoResult != null) { 
 | 
                    if (qvoResult == 1) { 
 | 
                        ok = rtu.state == UpgradeRtu.STATE_SUCCESS; 
 | 
                    } else if (qvoResult == 0) { 
 | 
                        ok = rtu.state == UpgradeRtu.STATE_OFFLINE 
 | 
                                || rtu.state == UpgradeRtu.STATE_FAILONE 
 | 
                                || rtu.state == UpgradeRtu.STATE_FAIL 
 | 
                                || rtu.state == UpgradeRtu.STATE_FAILOFFLINE 
 | 
                                || rtu.state == UpgradeRtu.STATE_FAILOPEN; 
 | 
                    } 
 | 
                } 
 | 
                if(qvoRtuAddr != null && !qvoRtuAddr.trim().equals("")){ 
 | 
                    ok = rtu.rtuAddr.equals(qvoRtuAddr); 
 | 
                } 
 | 
                return ok; 
 | 
            }).toList() ; 
 | 
        }else{ 
 | 
            return RtuUpgradeStateReceiverCtrl.cache.ugRtuStateList ; 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 过滤出当前长级任务中升级失败的RTU 
 | 
     * @return 集合 
 | 
     */ 
 | 
    public List<VoUgRtuResult4Failure> exportUgFail(){ 
 | 
        List<VoUgRtuResult4Failure> list = new ArrayList<>() ; 
 | 
        if(RtuUpgradeStateReceiverCtrl.cache != null && RtuUpgradeStateReceiverCtrl.cache.ugTaskId != null){ 
 | 
            if(RtuUpgradeStateReceiverCtrl.cache.ugRtuStateList != null && RtuUpgradeStateReceiverCtrl.cache.ugRtuStateList.size() > 0) { 
 | 
                RtuUpgradeStateReceiverCtrl.cache.ugRtuStateList.forEach(rtu -> { 
 | 
                    if(rtu.isOver){ 
 | 
                        if(rtu.state == UpgradeRtu.STATE_OFFLINE 
 | 
                                || rtu.state == UpgradeRtu.STATE_UNSTART 
 | 
                                || rtu.state == UpgradeRtu.STATE_FAILOFFLINE){ 
 | 
                            VoUgRtuResult4Failure vo = new VoUgRtuResult4Failure() ; 
 | 
                            vo.rtuAddr = rtu.rtuAddr ; 
 | 
                            vo.result = "离线" ; 
 | 
                            list.add(vo); 
 | 
                        }else if(rtu.state == UpgradeRtu.STATE_FAILOPEN){ 
 | 
                            VoUgRtuResult4Failure vo = new VoUgRtuResult4Failure() ; 
 | 
                            vo.rtuAddr = rtu.rtuAddr ; 
 | 
                            vo.result = "阀开" ; 
 | 
                            list.add(vo); 
 | 
                        }else if(rtu.state == UpgradeRtu.STATE_FAILONE){ 
 | 
                            VoUgRtuResult4Failure vo = new VoUgRtuResult4Failure() ; 
 | 
                            vo.rtuAddr = rtu.rtuAddr ; 
 | 
                            vo.result = "一包死" ; 
 | 
                            list.add(vo); 
 | 
                        }else if(rtu.state == UpgradeRtu.STATE_FAIL){ 
 | 
                            VoUgRtuResult4Failure vo = new VoUgRtuResult4Failure() ; 
 | 
                            vo.rtuAddr = rtu.rtuAddr ; 
 | 
                            vo.result = "多包死" ; 
 | 
                            list.add(vo); 
 | 
                        } 
 | 
                    } 
 | 
                }); 
 | 
            } 
 | 
        } 
 | 
        return list ; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 过滤出当前长级任务中升级成功的RTU 
 | 
     * @return 集合 
 | 
     */ 
 | 
    public List<VoUgRtuResult4Success> exportUgSuccess(){ 
 | 
        List<VoUgRtuResult4Success> list = new ArrayList<>() ; 
 | 
        if(RtuUpgradeStateReceiverCtrl.cache != null && RtuUpgradeStateReceiverCtrl.cache.ugTaskId != null){ 
 | 
            if(RtuUpgradeStateReceiverCtrl.cache.ugRtuStateList != null && RtuUpgradeStateReceiverCtrl.cache.ugRtuStateList.size() > 0) { 
 | 
                RtuUpgradeStateReceiverCtrl.cache.ugRtuStateList.forEach(rtu -> { 
 | 
                    if(rtu.isOver){ 
 | 
                        if(rtu.state == UpgradeRtu.STATE_SUCCESS){ 
 | 
                            VoUgRtuResult4Success vo = new VoUgRtuResult4Success() ; 
 | 
                            vo.rtuAddr = rtu.rtuAddr ; 
 | 
                            vo.result = "升级成功" ; 
 | 
                            list.add(vo); 
 | 
                        } 
 | 
                    } 
 | 
                }); 
 | 
            } 
 | 
        } 
 | 
        return list ; 
 | 
    } 
 | 
} 
 |