浏览代码

损失电量、停机时长

xushili 2 年之前
父节点
当前提交
3538a4c94e

+ 6 - 0
gyee-sample-impala/src/main/java/com/gyee/impala/common/feign/IAdapterService.java

@@ -54,6 +54,12 @@ public interface IAdapterService {
     @RequestLine("GET /ts/history/section?tagNames={tagNames}&ts={ts}")
     List<TsBooleanData> getHistorySectionDI(@Param(value = "tagNames") String tagNames, @Param(value = "ts") long ts);
 
+    /**
+     * 获取当前时刻的上一个最近的数据
+     * @param tagNames
+     * @param ts
+     * @return
+     */
     @Headers({"Content-Type: application/json", "Accept: application/json"})
     @RequestLine("GET /ts/history/section?tagNames={tagNames}&ts={ts}")
     Map<String,TsDoubleData> getHistorySection(@Param(value = "tagNames") String tagNames, @Param(value = "ts") long ts);

+ 16 - 0
gyee-sample-impala/src/main/java/com/gyee/impala/common/util/DateUtil.java

@@ -327,6 +327,22 @@ public class DateUtil extends DateUtils {
         return dateTime;
     }
 
+    public static Date str2DateTime(String time,String pattern) {
+
+        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
+        Date dateTime = null;
+        try {
+            dateTime = sdf.parse(time);
+        } catch (ParseException e) {
+            e.printStackTrace();
+
+        }
+        return dateTime;
+    }
+    public static Date str2DateTime(String time) {
+        return str2DateTime(time,DATE_TIME_PATTERN);
+    }
+
     public static double hoursDiff1(Date d1, Date d2) {
         return Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (double) (60 * 60 * 1000));
     }

+ 8 - 1
gyee-sample-impala/src/main/java/com/gyee/impala/mapper/slave/FaulthistoryMapper.java

@@ -2,7 +2,12 @@ package com.gyee.impala.mapper.slave;
 
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.gyee.impala.model.custom.diagnose.FaultInfo;
 import com.gyee.impala.model.slave.Faulthistory;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.Date;
+import java.util.List;
 
 /**
  * <p>
@@ -13,5 +18,7 @@ import com.gyee.impala.model.slave.Faulthistory;
  * @since 2022-03-01
  */
 public interface FaulthistoryMapper extends BaseMapper<Faulthistory> {
-
+    List<FaultInfo> queryFaultInfo(@Param("wtid") String windturbineid,
+                                   @Param("starttime") Date startTime,
+                                   @Param("endtime") Date endTime);
 }

+ 4 - 0
gyee-sample-impala/src/main/java/com/gyee/impala/model/master/Casefault.java

@@ -63,6 +63,10 @@ public class Casefault extends Model<Casefault> implements ToData<Object> {
 
     private String speed;
 
+    private Integer faultsecond;
+
+    private Float powerloos;
+
 
 
     /**

+ 4 - 0
gyee-sample-impala/src/main/java/com/gyee/impala/model/master/Casefaultalg.java

@@ -67,6 +67,10 @@ public class Casefaultalg extends Model<Casefaultalg> {
 
     private String speed;
 
+    private Integer faultsecond;
+
+    private Float powerloos;
+
 
     @Override
     protected Serializable pkVal() {

+ 199 - 0
gyee-sample-impala/src/main/java/com/gyee/impala/schdule/TaskCasefaultReplenish.java

@@ -0,0 +1,199 @@
+package com.gyee.impala.schdule;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.gyee.impala.common.feign.RemoteServiceBuilder;
+import com.gyee.impala.common.util.DateUtil;
+import com.gyee.impala.mapper.master.CasefaultMapper;
+import com.gyee.impala.mapper.master.CasefaultalgMapper;
+import com.gyee.impala.mapper.slave.FaulthistoryMapper;
+import com.gyee.impala.mapper.slave.Windturbinetestingpointai2Mapper;
+import com.gyee.impala.model.custom.TsDoubleData;
+import com.gyee.impala.model.custom.diagnose.FaultInfo;
+import com.gyee.impala.model.master.Casefault;
+import com.gyee.impala.model.master.Casefaultalg;
+import com.gyee.impala.model.slave.Windturbinetestingpointai2;
+import com.gyee.impala.service.master.CasefaultService;
+import com.gyee.impala.service.master.CasefaultalgService;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.time.DateUtils;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.text.ParseException;
+import java.util.*;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+@Component
+public class TaskCasefaultReplenish {
+    @Resource
+    private CasefaultalgMapper casefaultalgMapper;
+    @Resource
+    private CasefaultMapper casefaultMapper;
+    @Resource
+    private FaulthistoryMapper faulthistoryMapper;
+    @Resource
+    private Windturbinetestingpointai2Mapper windturbinetestingpointai2Mapper;
+    @Resource
+    private CasefaultalgService casefaultalgService;
+    @Resource
+    private CasefaultService casefaultService;
+    @Resource
+    private RemoteServiceBuilder remoteServiceBuilder;
+
+    @Scheduled(cron = "0 36 9 * * ?")
+    public void creatEndtime(){
+        Calendar instance = Calendar.getInstance();
+        instance.add(Calendar.DATE, -8);
+        //获取报警信息,用此信息计算故障解除时间
+        List<FaultInfo> faultInfos = faulthistoryMapper.queryFaultInfo(null, instance.getTime(), new Date());
+
+        List<Casefaultalg> casefaultalgList = new ArrayList<>();
+        List<Casefault> casefaultList = new ArrayList<>();
+
+        QueryWrapper<Casefaultalg> cfawrapper = new QueryWrapper<>();
+        cfawrapper.eq("endtime","").or().isNull("endtime").or().apply("starttime=endtime").orderByDesc("starttime").last("limit 5000");
+        //查找没有更新故障解除时间的记录,
+        List<Casefaultalg> casefaultalgs = casefaultalgMapper.selectList(cfawrapper);
+        //faultid、Casefaultalg
+        Map<Long, Casefaultalg> casefaultalgMap = casefaultalgs.stream().collect(Collectors.toMap(Casefaultalg::getFaultid, Function.identity(),(key1,key2)->key2));
+        List<String> cfaturbines = casefaultalgs.stream().map(Casefaultalg::getWindturbineid).distinct().collect(Collectors.toList());
+        for (String turbine : cfaturbines) {
+            List<FaultInfo> collect = faultInfos.stream().filter(fis -> fis.getWindturbineId().equals(turbine)).collect(Collectors.toList());
+            Long k = 0L;//报警faultid
+            //遍历当前风机的报警信息
+            for (FaultInfo faultInfo : collect) {
+                if(faultInfo.getMessageType()==3){
+                    if(!k.equals(0L)){
+                        Casefaultalg casefaultalg = new Casefaultalg();
+                        casefaultalg.setId(casefaultalgMap.get(k).getId());
+                        casefaultalg.setEndtime(faultInfo.getFaultTime());
+                        casefaultalgList.add(casefaultalg);
+                        k = 0L;
+                    }
+                }else {
+                    //用faultid查找样本库有没有这条报警信息
+                    Casefaultalg casefaultalg = casefaultalgMap.get(faultInfo.getId());
+                    if(casefaultalg!=null){
+                        k = casefaultalg.getFaultid();
+                    }else {
+                        k = 0L;
+                    }
+                }
+            }
+        }
+        //批量更新endtime
+        casefaultalgService.updateBatchById(casefaultalgList,300);
+
+
+
+        QueryWrapper<Casefault> cfwrapper = new QueryWrapper<>();
+        cfwrapper.eq("endtime","").or().isNull("endtime").or().apply("starttime=endtime").orderByDesc("starttime").last("limit 2000");
+        List<Casefault> casefaults = casefaultMapper.selectList(cfwrapper);
+        Map<Long, Casefault> casefaultMap = casefaults.stream().collect(Collectors.toMap(Casefault::getFaultid, Function.identity(),(key1,key2)->key2));
+        List<String> cfturbines = casefaults.stream().map(Casefault::getWindturbineid).distinct().collect(Collectors.toList());
+        for (String turbine : cfturbines) {
+            List<FaultInfo> collect = faultInfos.stream().filter(fis -> fis.getWindturbineId().equals(turbine)).collect(Collectors.toList());
+            Long k = 0L;
+            for (FaultInfo faultInfo : collect) {
+                if(faultInfo.getMessageType()==3){
+                    if(!k.equals(0L)){
+                        Casefault casefault = new Casefault();
+                        casefault.setId(casefaultMap.get(k).getId());
+                        casefault.setEndtime(faultInfo.getFaultTime());
+                        casefaultList.add(casefault);
+                        k = 0L;
+                    }
+                }else {
+                    Casefault casefault = casefaultMap.get(faultInfo.getId());
+                    if(casefault!=null){
+                        k = casefault.getFaultid();
+                    }else {
+                        k = 0L;
+                    }
+                }
+            }
+        }
+        casefaultService.updateBatchById(casefaultList,300);
+    }
+
+    @Scheduled(cron = "0 46 11 * * ?")
+    public void creatPowerloss(){
+        List<Casefaultalg> casefaultalgList = new ArrayList<>();
+        List<Casefault> casefaultList = new ArrayList<>();
+
+        QueryWrapper<Casefaultalg> cfawrapper = new QueryWrapper<>();
+        cfawrapper.isNull("faultsecond");
+        List<Casefaultalg> casefaultalgs = casefaultalgMapper.selectList(cfawrapper);
+
+        QueryWrapper<Casefault> cfwrapper = new QueryWrapper<>();
+        cfwrapper.isNull("faultsecond");
+        List<Casefault> casefaults = casefaultMapper.selectList(cfwrapper);
+
+        QueryWrapper<Windturbinetestingpointai2> wpwrapper = new QueryWrapper<>();
+        wpwrapper.eq("UNIFORMCODE", "RFDLLL").likeLeft("WINDPOWERSTATIONID", "FDC");
+        List<Windturbinetestingpointai2> windturbinetestingpointai2s = windturbinetestingpointai2Mapper.selectList(wpwrapper);
+        //风机、理论日发电量
+        Map<String, Windturbinetestingpointai2> collect = windturbinetestingpointai2s.stream().collect(Collectors.toMap(Windturbinetestingpointai2::getWindturbineid, Function.identity()));
+
+        for (Casefaultalg casefaultalg : casefaultalgs) {
+            String starttime = casefaultalg.getStarttime();
+            String endtime = casefaultalg.getEndtime();
+            if(StringUtils.isEmpty(starttime)||StringUtils.isEmpty(endtime)) return;
+
+            long startl = DateUtil.str2DateTime(starttime).getTime();
+            long endl = DateUtil.str2DateTime(endtime).getTime();
+            long l = endl - startl;
+
+            String pointAi = collect.get(casefaultalg.getWindturbineid()).getId();
+            Map<String,TsDoubleData> startLlgl = remoteServiceBuilder.adapter().getHistorySection(pointAi, startl);
+            try {
+                Thread.sleep(30);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+            Map<String,TsDoubleData> endLlgl = remoteServiceBuilder.adapter().getHistorySection(pointAi, endl);
+            double v = endLlgl.entrySet().iterator().next().getValue().getDoubleValue() - startLlgl.entrySet().iterator().next().getValue().getDoubleValue();
+
+            Casefaultalg cf = new Casefaultalg();
+            cf.setId(casefaultalg.getId());
+            cf.setFaultsecond((int) TimeUnit.MILLISECONDS.toSeconds(l));
+            cf.setPowerloos((float) v);
+            casefaultalgList.add(cf);
+        }
+        casefaultalgService.updateBatchById(casefaultalgList,300);
+
+
+        int k = 0;
+        for (Casefault casefault : casefaults) {
+            System.out.println(k);
+            k++;
+            String starttime = casefault.getStarttime();
+            String endtime = casefault.getEndtime();
+            if(StringUtils.isEmpty(starttime)||StringUtils.isEmpty(endtime)) continue;
+
+            long startl = DateUtil.str2DateTime(starttime).getTime();
+            long endl = DateUtil.str2DateTime(endtime).getTime();
+            long l = endl - startl;
+
+            String pointAi = collect.get(casefault.getWindturbineid()).getId();
+            Map<String,TsDoubleData> startLlgl = remoteServiceBuilder.adapter().getHistorySection(pointAi, startl);
+            try {
+                Thread.sleep(30);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+            Map<String,TsDoubleData> endLlgl = remoteServiceBuilder.adapter().getHistorySection(pointAi, endl);
+            double v = endLlgl.entrySet().iterator().next().getValue().getDoubleValue() - startLlgl.entrySet().iterator().next().getValue().getDoubleValue();
+
+            Casefault cf = new Casefault();
+            cf.setId(casefault.getId());
+            cf.setFaultsecond((int) TimeUnit.MILLISECONDS.toSeconds(l));
+            cf.setPowerloos((float) v);
+            casefaultList.add(cf);
+        }
+        casefaultService.updateBatchById(casefaultList, 300);
+    }
+}

+ 7 - 5
gyee-sample-impala/src/main/java/com/gyee/impala/service/impl/master/CasefaultalgServiceImpl.java

@@ -19,7 +19,9 @@ import com.gyee.impala.model.master.Casefaultalg;
 import com.gyee.impala.model.slave.Windturbinetestingpointai2;
 import com.gyee.impala.service.master.CasefaultService;
 import com.gyee.impala.service.master.CasefaultalgService;
+import org.apache.ibatis.session.SqlSession;
 import org.apache.kudu.client.*;
+import org.mybatis.spring.SqlSessionTemplate;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.stereotype.Service;
@@ -51,8 +53,10 @@ public class CasefaultalgServiceImpl extends ServiceImpl<CasefaultalgMapper, Cas
     @Autowired
     private RemoteServiceBuilder remoteServiceBuilder;
 
-    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
-
+    /**
+     * 获取所有风机风速测点
+     * @return
+     */
     @Cacheable("aisAI022")
     public Map<String, Windturbinetestingpointai2> getWindpointai2s() {
         Map<String, Object> wrapper = new HashMap<>();
@@ -79,7 +83,7 @@ public class CasefaultalgServiceImpl extends ServiceImpl<CasefaultalgMapper, Cas
         Map<String, Windturbinetestingpointai2> windpointai2s = getWindpointai2s();
         try{
             for (Casefaultalg casefaultalg : list) {
-                Map<String,TsDoubleData> historySection = remoteServiceBuilder.adapter().getHistorySection(windpointai2s.get(casefaultalg.getWindturbineid()).getId(), sdf.parse(casefaultalg.getStarttime()).getTime());
+                Map<String,TsDoubleData> historySection = remoteServiceBuilder.adapter().getHistorySection(windpointai2s.get(casefaultalg.getWindturbineid()).getId(), DateUtil.str2DateTime(casefaultalg.getStarttime()).getTime());
                 TsDoubleData value = historySection.entrySet().iterator().next().getValue();
                 casefaultalg.setSpeed(String.valueOf(value.getDoubleValue()));
             }
@@ -250,6 +254,4 @@ public class CasefaultalgServiceImpl extends ServiceImpl<CasefaultalgMapper, Cas
         // 关闭和kudu的会话
         kuduSession.close();
     }
-
-
 }

+ 1 - 0
gyee-sample-impala/src/main/java/com/gyee/impala/service/master/CasefaultalgService.java

@@ -77,4 +77,5 @@ public interface CasefaultalgService extends IService<Casefaultalg> {
     void faultAlgEditRemark(String id,String remark);
     //加入风速
     void insertBatchAddspeed(List<Casefaultalg> list);
+
 }

+ 18 - 4
gyee-sample-impala/src/main/resources/mapper/slave/FaulthistoryMapper.xml

@@ -16,9 +16,23 @@
         <result column="ALERTTEXTLAST" property="alerttextlast" />
     </resultMap>
 
-    <!-- 通用查询结果列 -->
-    <sql id="Base_Column_List">
-        ID, FAULTTIME, SNAPID, OPERATOR, CONFIRMTYPE, CONFIRMTIME, CONFIRMPERSON, MESSAGETYPE, ALERTSNAPID, ALERTTEXTLAST
-    </sql>
+    <select id="queryFaultInfo" resultType="com.gyee.impala.model.custom.diagnose.FaultInfo">
+        select
+            h.id,h.faultTime,h.messageType,h.snapID,h.confirmType,h.confirmTime,h.confirmPerson,
+            s.stationId,s.projectId,s.lineId,s.windturbineId,s.alertValue,s.rank,s.category1,s.category2,s.category3,
+            s.isOpened,s.lastUpdateTime,s.lastUpdatePerson,s.stationName,s.projectName,s.lineName,s.windturbineName,
+            s.alertText,s.modelId
+        from faulthistory h left join faultsnap s on h.snapid = s.id
+        <where>
+            CATEGORY2='GZ'
+            <if test="wtid !=null and wtid !=''">
+                and s.windturbineId=#{wtid}
+            </if>
+            <if test="starttime !=null and endtime !=null">
+                and h.faulttime between #{starttime} and #{endtime}
+            </if>
+        </where>
+        order by h.faultTime
+    </select>
 
 </mapper>

+ 34 - 4
gyee-sample-impala/src/test/java/com/gyee/impala/FilterFaultTest.java

@@ -1,15 +1,28 @@
 package com.gyee.impala;
 
+import com.gyee.impala.common.exception.CustomException;
 import com.gyee.impala.common.feign.RemoteServiceBuilder;
+import com.gyee.impala.common.result.ResultCode;
+import com.gyee.impala.common.util.DateUtil;
+import com.gyee.impala.mapper.slave.Windturbinetestingpointai2Mapper;
 import com.gyee.impala.model.custom.TsDoubleData;
+import com.gyee.impala.model.master.Casefaultalg;
+import com.gyee.impala.model.slave.Windturbinetestingpointai2;
+import org.apache.commons.lang3.time.DateUtils;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
 
+import javax.annotation.Resource;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
 
 
 @RunWith(SpringRunner.class)
@@ -18,11 +31,28 @@ public class FilterFaultTest {
 
     @Autowired
     private RemoteServiceBuilder remoteServiceBuilder;
+    @Resource
+    private Windturbinetestingpointai2Mapper windturbinetestingpointai2Mapper;
+
+    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
 
     @Test
-    public void golden(){
-        Map<String,TsDoubleData> historySection = remoteServiceBuilder.adapter().getHistorySection("HZJDJL.NX_GD_HZJG_DD_P1_L1_001_FXYG002", 1660893483000L);
-        TsDoubleData value = historySection.entrySet().iterator().next().getValue();
-        System.out.println(historySection);
+    public void golden() throws ParseException {
+        Map<String, Windturbinetestingpointai2> windpointai2s = getWindpointai2s();
+        for (Map.Entry<String, Windturbinetestingpointai2> stringWindturbinetestingpointai2Entry : windpointai2s.entrySet()) {
+            if(stringWindturbinetestingpointai2Entry.getValue().getWindturbineid().equals("SG01_103")){
+                long time = sdf.parse("2022-09-16 12:58:48").getTime();
+                Map<String,TsDoubleData> historySection = remoteServiceBuilder.adapter().getHistorySection(windpointai2s.get("SG01_103").getId(), DateUtil.str2DateTime("2022-09-16 12:58:48").getTime());
+                TsDoubleData value = historySection.entrySet().iterator().next().getValue();
+                String s = String.valueOf(value.getDoubleValue());
+            }
+        }
+    }
+
+    public Map<String, Windturbinetestingpointai2> getWindpointai2s() {
+        Map<String, Object> wrapper = new HashMap<>();
+        wrapper.put("uniformcode", "AI022");
+        List<Windturbinetestingpointai2> ai2s = windturbinetestingpointai2Mapper.selectByMap(wrapper);
+        return ai2s.stream().collect(Collectors.toMap(Windturbinetestingpointai2::getWindturbineid, Function.identity()));
     }
 }

+ 3 - 2
gyee-sample-impala/src/test/java/com/gyee/impala/KuduTest.java

@@ -2,11 +2,13 @@ package com.gyee.impala;
 
 import com.gyee.impala.common.config.datasource.KuduDataSourceConfig;
 import com.gyee.impala.common.util.SnowFlakeUtil;
+import com.gyee.impala.model.master.Casefaultalg;
 import com.gyee.impala.model.master.Windspeedforecastshortterm;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.kudu.client.*;
 import org.springframework.beans.factory.annotation.Autowired;
 
-import java.util.Date;
+import java.util.*;
 
 public class KuduTest {
 
@@ -61,6 +63,5 @@ public class KuduTest {
         kuduSession.close();
     }*/
     public static void main(String[] args) {
-
     }
 }