123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077 |
- package com.slibra.quartz.task;
- import cn.hutool.core.date.DateUtil;
- import cn.hutool.http.HttpRequest;
- import com.alibaba.fastjson2.JSON;
- import com.alibaba.fastjson2.JSONArray;
- import com.alibaba.fastjson2.JSONObject;
- import com.alibaba.fastjson2.JSONWriter;
- import com.google.protobuf.ByteString;
- import com.slibra.business.domain.*;
- import com.slibra.business.mapper.*;
- import com.slibra.business.req.ChatReq;
- import com.slibra.business.req.DecisionReq;
- import com.slibra.common.constant.MyConstants;
- import com.slibra.common.enums.BusinessEnum;
- import com.slibra.common.enums.DataSourceType;
- import com.slibra.common.utils.DateUtils;
- import com.slibra.common.utils.SecurityUtils;
- import com.slibra.common.utils.uuid.IdUtils;
- import com.slibra.framework.datasource.DynamicDataSourceContextHolder;
- import inference.InferenceAPIsServiceGrpc;
- import inference.PredictionResponse;
- import inference.PredictionsRequest;
- import io.grpc.ManagedChannel;
- import io.grpc.ManagedChannelBuilder;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import com.slibra.common.utils.StringUtils;
- import org.springframework.util.CollectionUtils;
- import java.io.IOException;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
- import java.util.*;
- import static com.slibra.common.constant.MyConstants.*;
- /**
- * 定时任务调度测试
- *
- *
- */
- @Component("ryTask")
- @Slf4j
- public class RyTask
- {
- public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
- {
- System.out.println(StringUtils.format("执行多参方法: 字符串类型{},布尔类型{},长整型{},浮点型{},整形{}", s, b, l, d, i));
- }
- public void ryParams(String params)
- {
- System.out.println("执行有参方法:" + params);
- }
- public void ryNoParams()
- {
- System.out.println("执行无参方法");
- }
- //----------------------------------------------下面是新增的方法----------------------------------------------
- @Autowired
- private TXinyiIndustryMapper xinyiIndustryMapper;
- @Autowired
- private TXinyiRobotMapper xinyiRobotMapper;
- @Autowired
- private TXinyiNormConfigMapper xinyiNormConfigMapper;
- @Autowired
- private TXinyiWarningRecordMapper xinyiWarningRecordMapper;
- @Autowired
- private TXinyiChatRecordMapper xinyiChatRecordMapper;
- @Autowired
- private TXinyiDailyMapper xinyiDailyMapper;
- // public final static StopWatch watch = new StopWatch("task");
- public static final String[] queryTags = {"信义污水厂JS_COD_Value","信义污水厂JS_PH_Value","信义污水厂JS_SS_Value","信义污水厂JS_ZL_Value","信义污水厂JS_ZA_Value","信义污水厂JS_AD_Value","信义污水厂JS_T_Value","信义污水厂进水泵房液位","信义污水厂出水瞬时流量","信义污水厂升级出水COD","信义污水厂升级出水PH","信义污水厂升级出水SS","信义污水厂升级出水TN","信义污水厂升级出水TP","信义污水厂升级出水氨氮","信义污水厂AIT202_Value","信义污水厂AIT203_Value","信义污水厂AIT207_Value","信义污水厂AIT206_Value","信义污水厂AIT209_Value","信义污水厂AIT210_Value","信义污水厂进水TDS","信义污水厂FT101_Value","信义污水厂SWCHHYHLB1_R_Value","信义污水厂SWCHHYHLB2_R_Value","信义污水厂SWCHHYHLB3_R_Value","信义污水厂SWCHHYHLB4_R_Value","信义污水厂SWCHHYHLB5_R_Value","信义污水厂SWCHHYHLB6_R_Value","信义污水厂SWCWNHLB1_R_Value","信义污水厂SWCWNHLB2_R_Value","信义污水厂SWCWNHLB3_R_Value","信义污水厂SWCWNHLB4_R_Value","信义污水厂SWCWNHLB5_R_Value","信义污水厂GFJ1_R_Value","信义污水厂GFJ2_R_Value","信义污水厂GFJ3_R_Value","信义污水厂GFJ4_R_Value","信义污水厂GFJ5_R_Value","信义污水厂GFJ6_R_Value","信义污水厂GFJ1_KQLL_Value","信义污水厂GFJ2_KQLL_Value","信义污水厂GFJ3_KQLL_Value","信义污水厂GFJ4_KQLL_Value","信义污水厂GFJ5_KQLL_Value","信义污水厂GFJ6_KQLL_Value","信义污水厂实际碳源加药量","信义污水厂除磷加药瞬时流量", "信义污水厂_除磷P04预测值_"};
- /**
- * 定时从工业库获取数据
- *
- * 2024年4月17日17:44:15 调整逻辑:考虑到因断电等情况导致服务断电,所以不再同步最近一小时,而是同步从上次成功的最后一条数据开始。
- */
- public void getIndustryData(){
- log.info("进入了定时同步工业库数据的任务");
- //耗时工具
- // watch.start("parseJob");
- // 给定时间段的起始时间和结束时间
- LocalDateTime endTime = LocalDateTime.now();
- // LocalDateTime startTime = endTime.plusMinutes(-60);
- //获取上次最后一条同步的数据的日期到 分钟维度
- String lastDateHour = this.xinyiIndustryMapper.getLastMinute();
- log.info("获取上次同步工业库的最后一条记录的时间是{}", lastDateHour);
- lastDateHour = lastDateHour + ":00";
- //开始时间
- LocalDateTime startTime = LocalDateTime.parse(lastDateHour.replaceAll("/", "-").replace(" ", "T"));
- startTime = startTime.plusMinutes(60L);//加一分钟 从上次最后一条记录的下一分钟开始
- /*LocalDateTime startTime = LocalDateTime.parse("2024-02-26T00:00:00");
- LocalDateTime endTime = LocalDateTime.parse("2024-02-27T00:00:00");*/
- // 每个小时的时间格式
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
- // 循环按小时分割
- LocalDateTime currentHour = startTime;
- //最终获取的数据
- Map<String, TXinyiIndustry> needMap = new LinkedHashMap<>();
- while (currentHour.isBefore(endTime)) {
- String begin = currentHour.format(formatter);
- String end = currentHour.plusMinutes(5).format(formatter);
- // 输出当前小时的起始时间和结束时间
- System.out.println("起始时间:" + begin);
- System.out.println("结束时间:" + end);
- // 当前小时加一小时,作为下一个小时的起始时间
- currentHour = currentHour.plusMinutes(5);
- //每个小时查询一次数据
- String url = "http://10.0.0.27:4568/api/v1/khquerydata";
- HashMap<String, Object> req = new HashMap<>();
- req.put("tagNames", queryTags);
- req.put("startTime", begin);
- req.put("endTime", end);
- req.put("recordNumbers", 100000);
- String body = HttpRequest.post(url).header("Authorization", "c2E6c2E=").header("clientName", "hongshan").body(JSON.toJSONString(req)).execute().body();
- // System.out.println("body = " + body);
- List<HashMap<String, String>> list = new ArrayList<>();
- //行转列数据处理
- for (String queryTag : queryTags) {
- JSONArray array = JSON.parseObject(body).getJSONArray(queryTag);
- //特殊数据处理一
- if(Objects.isNull(array) || array.isEmpty()){
- System.out.println(queryTag + "查询到了空的数据,跳过本次循环");
- continue;
- }
- int size = array.size();
- //特殊数据处理二
- if("0".equals(array.get(1) + "")){
- System.out.println(queryTag + "查询到了数据,但是数据集合只有一条,且都是0");
- continue;
- }
- //结合至少62个数据才满足条件(有可能获取不到)
- /*if(size < 62){
- System.out.println(queryTag + "查询到了不符合条件的数据,跳过本次循环");
- continue;
- }*/
- //存放的数据集
- //利用map去重
- HashMap<String, String> map = new LinkedHashMap<>();
- for (int i = 2; i < size; i++) {
- // System.out.println(i + "" + array.get(i));
- JSONArray oneRecord = JSON.parseArray(JSON.toJSONString(array.get(i)));
- //处理为空或者为0的数据
- Object timeStampValue = oneRecord.get(2);
- if(Objects.isNull(timeStampValue) || "0".equals(timeStampValue + ""))
- continue;
- BigDecimal value = Objects.isNull(oneRecord.get(0)) ? null : new BigDecimal(oneRecord.get(0) + "");
- long timestamp = (long) timeStampValue;
- String format = DateUtil.format(new Date(timestamp), DateUtils.YYYYMMDDHH_TS);
- map.put(format, queryTag + "-" + value);
- }
- list.add(map);
- }
- Set<String> recordTimeSet = new HashSet<>();
- Map<String, TXinyiIndustry> recordMap = new HashMap<>();
- for (int i = 0; i < list.size(); i++) {
- HashMap<String, String> map = list.get(i);
- int finalJ = i;
- map.forEach((k, v) ->{
- TXinyiIndustry industry = null;
- if(!recordTimeSet.contains(k)){//第一次
- industry = new TXinyiIndustry();
- recordTimeSet.add(k);
- recordMap.put(k, industry);
- }else{
- industry = recordMap.get(k);
- }
- industry.setTestTime(k + ":00");
- //2024年4月15日11:19:52 额外增加2个字段
- industry.setTestDate(k.substring(0,10));
- industry.setTestHour(k.substring(0,13));
- //解析值
- String[] split = v.split("-");
- String type = split[0];
- BigDecimal value = new BigDecimal(split[1]);
- if ("信义污水厂JS_COD_Value".equals(type)) {
- industry.setJsCod(value);
- } else if ("信义污水厂JS_PH_Value".equals(type)) {
- industry.setJsPh(value);
- } else if ("信义污水厂JS_SS_Value".equals(type)) {
- industry.setJsSs(value);
- } else if ("信义污水厂JS_ZL_Value".equals(type)) {
- industry.setJsTp(value);
- } else if ("信义污水厂JS_ZA_Value".equals(type)) {
- industry.setJsTn(value);
- } else if ("信义污水厂JS_AD_Value".equals(type)) {
- industry.setJsNh3(value);
- } else if ("信义污水厂JS_T_Value".equals(type)) {
- industry.setJsSwPh(value);
- } else if ("信义污水厂进水泵房液位".equals(type)) {
- industry.setJsBfyw(value);
- } else if ("信义污水厂出水瞬时流量".equals(type)) {
- industry.setCsSlqc(value);
- } else if ("信义污水厂升级出水COD".equals(type)) {
- industry.setCsCod(value);
- } else if ("信义污水厂升级出水PH".equals(type)) {
- industry.setCsPh(value);
- } else if ("信义污水厂升级出水SS".equals(type)) {
- industry.setCsSs(value);
- } else if ("信义污水厂升级出水TN".equals(type)) {
- industry.setCsTn(value);
- } else if ("信义污水厂升级出水TP".equals(type)) {
- industry.setCsTp(value);
- } else if ("信义污水厂升级出水氨氮".equals(type)) {
- industry.setCsNh3(value);
- } else if ("信义污水厂AIT202_Value".equals(type)) {
- industry.setOneHyzdDo(value);
- } else if ("信义污水厂AIT203_Value".equals(type)) {
- industry.setOneHymdDo(value);
- } else if ("信义污水厂AIT207_Value".equals(type)) {
- industry.setTwoHyzdDo(value);
- } else if ("信义污水厂AIT206_Value".equals(type)) {
- industry.setTwoHymdDo(value);
- } else if ("信义污水厂AIT209_Value".equals(type)) {
- industry.setOneMlss(value);
- } else if ("信义污水厂AIT210_Value".equals(type)) {
- industry.setTwoMlss(value);
- } else if ("信义污水厂进水TDS".equals(type)) {
- industry.setJsTds(value);
- } else if ("信义污水厂FT101_Value".equals(type)) {
- industry.setJsSlq(value);
- } else if ("信义污水厂SWCHHYHLB1_R_Value".equals(type)) {
- industry.setNHlbOneGp(value);
- } else if ("信义污水厂SWCHHYHLB2_R_Value".equals(type)) {
- industry.setNHlbTwoGp(value);
- } else if ("信义污水厂SWCHHYHLB3_R_Value".equals(type)) {
- industry.setNHlbThreeGp(value);
- } else if ("信义污水厂SWCHHYHLB4_R_Value".equals(type)) {
- industry.setNHlbFourGp(value);
- } else if ("信义污水厂SWCHHYHLB5_R_Value".equals(type)) {
- industry.setNhlBFiveGp(value);
- } else if ("信义污水厂SWCHHYHLB6_R_Value".equals(type)) {
- industry.setNHlbSixGp(value);
- } else if ("信义污水厂SWCWNHLB1_R_Value".equals(type)) {
- industry.setWHlbOneGp(value);
- } else if ("信义污水厂SWCWNHLB2_R_Value".equals(type)) {
- industry.setWHlbTwoGp(value);
- } else if ("信义污水厂SWCWNHLB3_R_Value".equals(type)) {
- industry.setWHlbThreeGp(value);
- } else if ("信义污水厂SWCWNHLB4_R_Value".equals(type)) {
- industry.setWHlbFourGp(value);
- } else if ("信义污水厂SWCWNHLB5_R_Value".equals(type)) {
- industry.setWHlbFiveGp(value);
- } else if ("信义污水厂GFJ1_R_Value".equals(type)) {
- industry.setFjOne(value);
- } else if ("信义污水厂GFJ2_R_Value".equals(type)) {
- industry.setFjTwo(value);
- } else if ("信义污水厂GFJ3_R_Value".equals(type)) {
- industry.setFjThree(value);
- } else if ("信义污水厂GFJ4_R_Value".equals(type)) {
- industry.setFjFour(value);
- } else if ("信义污水厂GFJ5_R_Value".equals(type)) {
- industry.setFjFive(value);
- } else if ("信义污水厂GFJ6_R_Value".equals(type)) {
- industry.setFjSix(value);
- } else if ("信义污水厂GFJ1_KQLL_Value".equals(type)) {
- industry.setKqllOne(value);
- } else if ("信义污水厂GFJ2_KQLL_Value".equals(type)) {
- industry.setKqllTwo(value);
- } else if ("信义污水厂GFJ3_KQLL_Value".equals(type)) {
- industry.setKqllThree(value);
- } else if ("信义污水厂GFJ4_KQLL_Value".equals(type)) {
- industry.setKqllFour(value);
- } else if ("信义污水厂GFJ5_KQLL_Value".equals(type)) {
- industry.setKqllFive(value);
- } else if ("信义污水厂GFJ6_KQLL_Value".equals(type)) {
- industry.setKqllSix(value);
- }else if ("信义污水厂实际碳源加药量".equals(type)) {
- industry.setSJTYJLY(value);
- }else if ("信义污水厂除磷加药瞬时流量".equals(type)) {
- industry.setCLJYSSLL(value);
- } else if ("信义污水厂_除磷P04预测值_".equals(type)) {
- industry.setCLP04YCZ(value);
- }
- //只有最后一次才执行数据库添加
- if(finalJ == list.size()-1){
- needMap.put(industry.getTestHour(), industry);
- }
- });
- }
- }
- //保存数据 触发告警 决策 问答记录等等
- needMap.forEach((k, industry) ->{
- //2024年4月22日15:45:24 额外保存两个字段 数组
- List<BigDecimal> extraList = new ArrayList<>();
- extraList.add(industry.getOneHymdDo());
- extraList.add(industry.getTwoHymdDo());
- industry.setHycRjyAll(JSON.toJSONString(extraList));
- extraList.clear();
- extraList.add(industry.getOneHyzdDo());
- extraList.add(industry.getTwoHyzdDo());
- industry.setHycRjyZdAll(JSON.toJSONString(extraList));
- extraList.clear();
- extraList.add(industry.getOneMlss());
- extraList.add(industry.getTwoMlss());
- industry.setHycWnndAll(JSON.toJSONString(extraList));
- //插入数据库
- xinyiIndustryMapper.insertTXinyiIndustry(industry);
- //判断是否触发告警、接触告警、保存决策等等
- this.handleWarning(industry);
- });
- //执行完成 测试执行时间
- //计时结束
- // watch.stop();
- // System.out.println(watch.getLastTaskName() + " 执行耗时:" + watch.getLastTaskTimeMillis() + " ms");
- }
- private void handleWarning(TXinyiIndustry tXinyiIndustry) {
- log.info("进入了定时任务保存工业库数据并触发报警操作");
- //获取配置表
- List<TXinyiNormConfig> tXinyiNormConfigs = this.xinyiNormConfigMapper.selectTXinyiNormConfigList(null);
- if(CollectionUtils.isEmpty(tXinyiNormConfigs))
- throw new RuntimeException("未查询到配置信息");
- TXinyiNormConfig normConfig = tXinyiNormConfigs.get(0);
- //出水相关
- //出水COD报警
- BigDecimal csCod = tXinyiIndustry.getCsCod();
- BigDecimal cscodBzz = normConfig.getCscodBzz();
- BigDecimal cscodGkz = normConfig.getCscodGkz();
- if(!Objects.isNull(csCod) && !Objects.isNull(cscodBzz)){
- handleXinYiWarningsCs(cscodBzz, csCod, cscodGkz, BusinessEnum.WarningCategoryEnum.CS_COD.getCode(), tXinyiIndustry, normConfig);
- }
- //出水总磷超标报警
- BigDecimal csTp = tXinyiIndustry.getCsTp();
- BigDecimal cszlBzz = normConfig.getCszlBzz();
- BigDecimal cszlGkz = normConfig.getCszlGkz();
- if(!Objects.isNull(csTp) && !Objects.isNull(cszlBzz)){
- handleXinYiWarningsCs(cszlBzz, csTp, cszlGkz, BusinessEnum.WarningCategoryEnum.CS_ZL.getCode(), tXinyiIndustry, normConfig);
- }
- //出水总氮超标报警
- BigDecimal csTn = tXinyiIndustry.getCsTn();
- BigDecimal cszzBzz = normConfig.getCszzBzz();
- BigDecimal cszzGkz = normConfig.getCszzGkz();
- if(!Objects.isNull(csTn) && !Objects.isNull(cszzBzz)){
- handleXinYiWarningsCs(cszzBzz, csTn, cszzGkz, BusinessEnum.WarningCategoryEnum.CS_ZD.getCode(), tXinyiIndustry, normConfig);
- }
- //出水氨氮超标报警
- BigDecimal csNh3 = tXinyiIndustry.getCsNh3();
- BigDecimal csadBzz = normConfig.getCsadBzz();
- BigDecimal csadGkz = normConfig.getCsadGkz();
- if(!Objects.isNull(csNh3) && !Objects.isNull(csadBzz)){
- handleXinYiWarningsCs(csadBzz, csNh3, csadGkz, BusinessEnum.WarningCategoryEnum.CS_AD.getCode(), tXinyiIndustry, normConfig);
- }
- //出水SS超标报警
- BigDecimal csSS = tXinyiIndustry.getCsSs();
- BigDecimal csSSBzz = normConfig.getCsssBzz();
- BigDecimal csssGkz = normConfig.getCsssGkz();
- if(!Objects.isNull(csSS) && !Objects.isNull(csSSBzz)){
- handleXinYiWarningsCs(csSSBzz, csSS, csssGkz, BusinessEnum.WarningCategoryEnum.CS_SS.getCode(), tXinyiIndustry, normConfig);
- }
- //进水相关报警
- //进水总磷超标报警
- BigDecimal jsTp = tXinyiIndustry.getJsTp();
- BigDecimal jszlSjz = normConfig.getJszlSjz();
- if(!Objects.isNull(jsTp) && !Objects.isNull(jszlSjz)){
- handleXinYiWarningRecordJS(jszlSjz, jsTp, BusinessEnum.WarningCategoryEnum.JS_ZL.getCode(), tXinyiIndustry, normConfig);
- }
- //进水COD超标报警
- BigDecimal jsCod = tXinyiIndustry.getJsCod();
- BigDecimal jscodSjz = normConfig.getJscodSjz();
- if(!Objects.isNull(jsCod) && !Objects.isNull(jscodSjz)){
- handleXinYiWarningRecordJS(jscodSjz, jsCod, BusinessEnum.WarningCategoryEnum.JS_COD.getCode(), tXinyiIndustry, normConfig);
- }
- //进水总氮超标报警
- BigDecimal jsTn = tXinyiIndustry.getJsTn();
- BigDecimal jszdSjz = normConfig.getJszdSjz();
- if(!Objects.isNull(jsTn) && !Objects.isNull(jszdSjz)){
- handleXinYiWarningRecordJS(jszdSjz, jsTn, BusinessEnum.WarningCategoryEnum.JS_ZD.getCode(), tXinyiIndustry, normConfig);
- }
- //进水氨氮超标报警
- BigDecimal jsNh3 = tXinyiIndustry.getJsNh3();
- BigDecimal jsadSjz = normConfig.getJsadSjz();
- if(!Objects.isNull(jsNh3) && !Objects.isNull(jsadSjz)){
- handleXinYiWarningRecordJS(jsadSjz, jsNh3, BusinessEnum.WarningCategoryEnum.JS_AD.getCode(), tXinyiIndustry, normConfig);
- }
- //进水SS超标报警
- BigDecimal jsSS = tXinyiIndustry.getJsSs();
- BigDecimal jsSSSjz = normConfig.getJsssSjz();
- if(!Objects.isNull(jsSS) && !Objects.isNull(jsSSSjz)){
- handleXinYiWarningRecordJS(jsSSSjz, jsSS, BusinessEnum.WarningCategoryEnum.JS_SS.getCode(), tXinyiIndustry, normConfig);
- }
-
- }
- /**
- * 调用大模型获取决策结果 并同时记录对应信息到聊天记录表中
- *
- * @param tXinyiWarningRecord
- * @param tXinyiIndustry
- * @param normConfig
- */
- private void handleDecision(TXinyiWarningRecord tXinyiWarningRecord, TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
- log.info("进入了调⽤大模型决策接口");
- ChatReq chatReq = new ChatReq();
- // StringBuilder sb = new StringBuilder();
- //大模型结果 放入一个结合中
- List<String> resultData = new ArrayList<>();
- //决策和问答不一样 没有历史的概念 所以sessionId都是新的 次数都是1
- String sessionId = IdUtils.simpleUUID();
- String feedback = chatReq.getFeedback();
- //决策请求的业务参数
- // List<HashMap<String, Object>> list = this.xinyiIndustryMapper.selectLast10RecordsForDecision();
- //2024年5月21日15:23:07 这里不能用关联查询处理,日报要获取最新的一条而不是今日的数据。
- List<DecisionReq> decisionReqs = this.xinyiIndustryMapper.selectLast10RecordsForDecisionOnlyIndustry();
- if(!CollectionUtils.isEmpty(decisionReqs)){
- for (DecisionReq decisionReq : decisionReqs) {
- //处理日报数据
- TXinyiDaily daily = this.xinyiDailyMapper.selectNewestData();
- if(!Objects.isNull(daily)){
- decisionReq.setT(daily.getJsSw());
- decisionReq.setSVI(daily.getWntjzsAll());
- decisionReq.setSV(daily.getWncjbAll());
- decisionReq.setMlvss(daily.getHfxwnndAll());
- decisionReq.setDoAna(daily.getYycRjyAll());
- decisionReq.setDoQue(daily.getQycRjyAll());
- BigDecimal jsBod5 = daily.getJsBod5();
- decisionReq.setBodIn(jsBod5);
- BigDecimal tpIn = decisionReq.getTpIn();
- BigDecimal tnIn = decisionReq.getTnIn();
- if(!Objects.isNull(jsBod5)){
- if(!Objects.isNull(tpIn) && tpIn.compareTo(new BigDecimal("0")) >0){
- decisionReq.setC(jsBod5.divide(tpIn, 4, RoundingMode.HALF_UP));
- }
- if(!Objects.isNull(tnIn) && tnIn.compareTo(new BigDecimal("0")) >0){
- decisionReq.setB(jsBod5.divide(tnIn, 4, RoundingMode.HALF_UP));
- }
- }
- }
- }
- }
- String rows = JSON.toJSONString(decisionReqs, JSONWriter.Feature.WriteNulls);
- // 获取输出流
- ManagedChannel channel = null;
- String dataJson = "";
- try {
- channel = ManagedChannelBuilder.forAddress("10.0.0.24", 17070)
- .usePlaintext()
- .build();
- InferenceAPIsServiceGrpc.InferenceAPIsServiceBlockingStub stub = InferenceAPIsServiceGrpc.newBlockingStub(channel);
- dataJson = "{\"bot_id\":\"b00001\",\"exp_id\":\"721\",\"norm\":\"" + tXinyiWarningRecord.getCategory() + "\",\"feedback\":" + feedback + ",\"session_id\":" + "\"" + sessionId + "\"" + ",\"generate_args\":{\"max_new_tokens\":1024,\"max_length\":4096,\"num_beams\":1,\"do_sample\":true,\"top_p\":0.7,\"temperature\":0.95},\"extra\":{\"rows\":" + rows + "}}";
- log.info("请求大模型的决策的参数为{}", dataJson);
- PredictionsRequest request = PredictionsRequest.newBuilder()
- .setModelName("slibra_bot")
- .putInput("method", ByteString.copyFrom("decision_stream", "utf-8"))//推理
- .putInput("data", ByteString.copyFrom(dataJson, "utf-8"))
- .buildPartial();
- Iterator<PredictionResponse> predictions = stub.streamPredictions(request);
- while (predictions.hasNext()) {
- String responseStr = predictions.next().getPrediction().toStringUtf8();
- log.info("决策流式返回的结果是{}", responseStr);
- responseStr = JSON.parseObject(responseStr).getString("message");
- if("complete".equals(responseStr)){
- System.out.println("结尾语句并且是非JSON,无需处理");
- }else{
- // sb.append(responseStr);
- resultData.add(responseStr);
- }
- // todo 类型判断处理
- }
- } catch (Exception e) {
- // throw new RuntimeException(e);
- log.error("处理大模型推理异常,异常信息为{}", JSON.toJSONString(e));
- } finally {
- System.out.println(JSON.toJSONString(resultData));
- //保存聊天记录
- //将问答更新到数据库中
- chatReq.setSessionId(sessionId);
- chatReq.setType(0);//0问答 1决策
- chatReq.setModule(3);
- /*String userId = SecurityUtils.getUserId().toString();
- String username = SecurityUtils.getUsername();*/
- chatReq.setUserId(WARNING_DEFAULT_CREATE);
- String showVal = this.buildShowValue(tXinyiWarningRecord, tXinyiIndustry, normConfig);
- chatReq.setShowVal(showVal);//前端展示的数据和提问的数据不一致
- chatReq.setQuestion(dataJson);
- chatReq.setAnswer(JSON.toJSONString(resultData));
- chatReq.setWarningId(String.valueOf(tXinyiWarningRecord.getId()));
- chatReq.setCounts(1);//问答次数
- chatReq.setCreateBy(WARNING_DEFAULT_CREATE);
- chatReq.setCreateTime(DateUtils.getNowDate());
- this.xinyiChatRecordMapper.insertTXinyiChatRecord(chatReq);
- // 关闭输出流
- if(!Objects.isNull(channel))
- channel.shutdown();
- }
- }
- private String buildShowValue(TXinyiWarningRecord tXinyiWarningRecord, TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
- JSONObject result = new JSONObject();
- JSONObject basic = new JSONObject();
- Integer status = tXinyiWarningRecord.getStatus();
- Date warningTime = tXinyiWarningRecord.getTime();
- int count = DateUtils.differentHoursByMillisecond(warningTime, DateUtils.getNowDate()) + 1;
- basic.put(tXinyiWarningRecord.getReason(), tXinyiWarningRecord.getReason());
- basic.put("报警时间", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM ,warningTime));
- basic.put("报警值", tXinyiWarningRecord.getWarningVal());
- basic.put("标准值", tXinyiWarningRecord.getDesignVal());
- basic.put("管控值", tXinyiWarningRecord.getControlVal());
- basic.put("报警次数", Math.min(count, maxCount));
- if(tXinyiWarningRecord.getType() != 2)
- basic.put("状态", status == 0 ? "告警中" : status == 1 ? "用户关闭" : status == 2 ? "系统自动关闭" : "用户转为应急处理中");
- else
- basic.put("状态", status == 0 ? "预警中" : "已完成");
- result.put("basic", basic);
- JSONObject jsData = getJsonObject(tXinyiIndustry, normConfig);//进水数据
- result.put("jsData", jsData);
- JSONObject csData = getCsonObject(tXinyiIndustry, normConfig);//出水数据
- result.put("csData", csData);
- return JSON.toJSONString(result);
- }
- private static JSONObject getJsonObject(TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
- JSONObject jsData = new JSONObject();
- HashMap<Object, Object> temp1 = new HashMap<>();
- BigDecimal jsSlq = tXinyiIndustry.getJsSlq();
- temp1.put("value", jsSlq);
- temp1.put("exceed", jsSlq.compareTo(normConfig.getJsslSjz()) >0);
- jsData.put("流量", temp1);
- HashMap<Object, Object> temp2 = new HashMap<>();
- BigDecimal jsCod = tXinyiIndustry.getJsCod();
- temp2.put("value", jsCod);
- temp2.put("exceed", jsCod.compareTo(normConfig.getJscodSjz()) > 0);
- jsData.put("COD", temp2);
- HashMap<Object, Object> temp3 = new HashMap<>();
- BigDecimal jsNh3 = tXinyiIndustry.getJsNh3();
- temp3.put("value", jsNh3);
- temp3.put("exceed", jsNh3.compareTo(normConfig.getJsadSjz()) > 0);
- jsData.put("NH3-N", temp3);
- HashMap<Object, Object> temp4 = new HashMap<>();
- BigDecimal jsTp = tXinyiIndustry.getJsTp();
- temp4.put("value", jsTp);
- temp4.put("exceed", jsTp.compareTo(normConfig.getJszlSjz()) > 0);
- jsData.put("TP", temp4);
- HashMap<Object, Object> temp5 = new HashMap<>();
- BigDecimal jsSs = tXinyiIndustry.getJsSs();
- temp5.put("value", jsSs);
- temp5.put("exceed", jsSs.compareTo(normConfig.getJsssSjz()) > 0);
- jsData.put("SS", temp5);
- HashMap<Object, Object> temp6 = new HashMap<>();
- BigDecimal jsTn = tXinyiIndustry.getJsTn();
- temp6.put("value", jsTn);
- temp6.put("exceed", jsTn.compareTo(normConfig.getJszdSjz()) > 0);
- jsData.put("TN", temp6);
- return jsData;
- }
- private static JSONObject getCsonObject(TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
- JSONObject csData = new JSONObject();
- HashMap<Object, Object> temp1 = new HashMap<>();
- BigDecimal csSlq = tXinyiIndustry.getCsSlqc();
- temp1.put("value", csSlq);
- temp1.put("exceed", false);//出水水量没有管控值
- csData.put("流量", temp1);
- HashMap<Object, Object> temp2 = new HashMap<>();
- BigDecimal csCod = tXinyiIndustry.getCsCod();
- temp2.put("value", csCod);
- temp2.put("exceed", csCod.compareTo(normConfig.getCscodGkz()) > 0);
- csData.put("COD", temp2);
- HashMap<Object, Object> temp3 = new HashMap<>();
- BigDecimal csNh3 = tXinyiIndustry.getCsNh3();
- temp3.put("value", csNh3);
- temp3.put("exceed", csNh3.compareTo(normConfig.getCsadGkz()) > 0);
- csData.put("NH3-N", temp3);
- HashMap<Object, Object> temp4 = new HashMap<>();
- BigDecimal csTp = tXinyiIndustry.getCsTp();
- temp4.put("value", csTp);
- temp4.put("exceed", csTp.compareTo(normConfig.getCszlGkz()) > 0);
- csData.put("TP", temp4);
- HashMap<Object, Object> temp5 = new HashMap<>();
- BigDecimal csSs = tXinyiIndustry.getCsSs();
- temp5.put("value", csSs);
- temp5.put("exceed", csSs.compareTo(normConfig.getCsssGkz()) > 0);
- csData.put("SS", temp5);
- HashMap<Object, Object> temp6 = new HashMap<>();
- BigDecimal csTn = tXinyiIndustry.getCsTn();
- temp6.put("value", csTn);
- temp6.put("exceed", csTn.compareTo(normConfig.getCszzGkz()) > 0);
- csData.put("TN", temp6);
- return csData;
- }
- /**
- * 通过输入的值 生成对应类型的报警对象(出水)
- *
- * @param csBzz
- * @param currentVal
- * @param csGkz
- * @param category
- * @param tXinyiIndustry
- * @param normConfig
- * @return
- */
- private void handleXinYiWarningsCs(BigDecimal csBzz, BigDecimal currentVal, BigDecimal csGkz, String category, TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
- BigDecimal multiply = csBzz.multiply(new BigDecimal(MyConstants.SCALE_VALUE));
- TXinyiWarningRecord tXinyiWarningRecord = new TXinyiWarningRecord();
- /*String category = BusinessEnum.WarningCategoryEnum.CS_AD.getCode();*/
- tXinyiWarningRecord.setStatus(0);
- tXinyiWarningRecord.setType(0);
- tXinyiWarningRecord.setCategory(category);
- tXinyiWarningRecord.setTime(DateUtils.getNowDate());
- tXinyiWarningRecord.setWarningVal(currentVal);
- tXinyiWarningRecord.setDesignVal(csBzz);
- tXinyiWarningRecord.setControlVal(csGkz);
- tXinyiWarningRecord.setCreateBy(WARNING_DEFAULT_CREATE);
- tXinyiWarningRecord.setCreateTime(DateUtils.getNowDate());
- if(currentVal.compareTo(multiply) > 0){//一级
- tXinyiWarningRecord.setReason(category + CHAOBIAO_WARNING);
- tXinyiWarningRecord.setLevel(WARNING_LEVEL_ONE);
- }else if(currentVal.compareTo(csBzz) >= 0 && currentVal.compareTo(multiply) <= 0){//二级
- tXinyiWarningRecord.setReason(category + CHAOBIAO_WARNING);
- tXinyiWarningRecord.setLevel(WARNING_LEVEL_TWO);
- }else if(!Objects.isNull(csGkz) && currentVal.compareTo(csGkz) > 0){
- tXinyiWarningRecord.setReason(category + CHAOGUANKONG_WARNING);
- tXinyiWarningRecord.setLevel(WARNING_LEVEL_THREE);
- }else{
- tXinyiWarningRecord = null;//这种的无需处理
- }
- //当前状态正常 需要查询历史有无正在报警的数据,如果有,将报警状态改完2(系统自动关闭)
- List<TXinyiWarningRecord> tXinyiWarningRecords = this.xinyiWarningRecordMapper.selectTXinyiWarningRecordList(TXinyiWarningRecord.builder().delFlag(0).type(0).category(category).status(0).build());
- if(Objects.isNull(tXinyiWarningRecord)){//数据正常,无告警信息
- if(!CollectionUtils.isEmpty(tXinyiWarningRecords)){
- log.info( "{}:现在恢复正常,历史报警数据为{}", category,JSON.toJSONString(tXinyiWarningRecords));
- for (TXinyiWarningRecord xinyiWarningRecord : tXinyiWarningRecords) {
- xinyiWarningRecord.setStatus(2);
- Date nowDate = DateUtils.getNowDate();
- xinyiWarningRecord.setOffTime(nowDate);
- xinyiWarningRecord.setUpdateTime(nowDate);
- xinyiWarningRecord.setUpdateBy(WARNING_DEFAULT_CREATE);
- this.xinyiWarningRecordMapper.updateTXinyiWarningRecord(xinyiWarningRecord);
- }
- }
- }else{//有告警信息
- if(CollectionUtils.isEmpty(tXinyiWarningRecords)){//之前没有告警记录
- //保存到数据库中
- this.xinyiWarningRecordMapper.insertTXinyiWarningRecord(tXinyiWarningRecord);
- //继续调用决策
- this.handleDecision(tXinyiWarningRecord, tXinyiIndustry, normConfig);
- }else{
- log.info("{}:之前已经有过告警记录了,且还是继续报警,无需重复添加报警,但是决策仍然要调用", category);
- for (TXinyiWarningRecord xinyiWarningRecord : tXinyiWarningRecords) {//理论上只有一个的
- //继续调用决策
- this.handleDecision(xinyiWarningRecord, tXinyiIndustry, normConfig);
- }
- }
- }
- }
- /**
- * 通过输入的值 生成对应类型的报警对象(进水)
- *
- * @param jsBzz
- * @param currentVal
- * @param category
- * @param tXinyiIndustry
- * @param normConfig
- * @return
- */
- private void handleXinYiWarningRecordJS(BigDecimal jsBzz, BigDecimal currentVal, String category, TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
- BigDecimal multiply = jsBzz.multiply(new BigDecimal(MyConstants.SCALE_VALUE));
- TXinyiWarningRecord tXinyiWarningRecord = new TXinyiWarningRecord();
- /*String category = BusinessEnum.WarningCategoryEnum.CS_AD.getCode();*/
- tXinyiWarningRecord.setStatus(0);
- tXinyiWarningRecord.setType(0);
- tXinyiWarningRecord.setCategory(category);
- tXinyiWarningRecord.setTime(DateUtils.getNowDate());
- tXinyiWarningRecord.setWarningVal(currentVal);
- tXinyiWarningRecord.setDesignVal(jsBzz);
- // tXinyiWarningRecord.setControlVal(csGkz);
- tXinyiWarningRecord.setCreateBy(WARNING_DEFAULT_CREATE);
- tXinyiWarningRecord.setCreateTime(DateUtils.getNowDate());
- if(currentVal.compareTo(multiply) > 0){//一级
- tXinyiWarningRecord.setReason(category + CHAOBIAO_WARNING);
- tXinyiWarningRecord.setLevel(WARNING_LEVEL_ONE);
- }else if(currentVal.compareTo(jsBzz) >= 0 && currentVal.compareTo(multiply) <= 0){//二级
- tXinyiWarningRecord.setReason(category + CHAOBIAO_WARNING);
- tXinyiWarningRecord.setLevel(WARNING_LEVEL_TWO);
- }else{
- tXinyiWarningRecord = null;//这种的无需处理
- }
- /*else if(!Objects.isNull(csGkz) && currentVal.compareTo(csGkz) > 0){
- tXinyiWarningRecord.setReason(category + CHAOGUANKONG_WARNING);
- tXinyiWarningRecord.setLevel(WARNING_LEVEL_THREE);
- }*/
- //当前状态正常 需要查询历史有无正在报警的数据,如果有,将报警状态改完2(系统自动关闭)
- List<TXinyiWarningRecord> tXinyiWarningRecords = this.xinyiWarningRecordMapper.selectTXinyiWarningRecordList(TXinyiWarningRecord.builder().delFlag(0).type(0).category(category).status(0).build());
- if(Objects.isNull(tXinyiWarningRecord)){//数据正常,无告警信息
- if(!CollectionUtils.isEmpty(tXinyiWarningRecords)){
- log.info( "{}:现在恢复正常,历史报警数据为{}", category,JSON.toJSONString(tXinyiWarningRecords));
- for (TXinyiWarningRecord xinyiWarningRecord : tXinyiWarningRecords) {
- xinyiWarningRecord.setStatus(2);
- Date nowDate = DateUtils.getNowDate();
- xinyiWarningRecord.setOffTime(nowDate);
- xinyiWarningRecord.setUpdateTime(nowDate);
- xinyiWarningRecord.setUpdateBy(WARNING_DEFAULT_CREATE);
- this.xinyiWarningRecordMapper.updateTXinyiWarningRecord(xinyiWarningRecord);
- }
- }
- }else{//有告警信息
- if(CollectionUtils.isEmpty(tXinyiWarningRecords)){//之前没有告警记录
- //保存到数据库中
- this.xinyiWarningRecordMapper.insertTXinyiWarningRecord(tXinyiWarningRecord);
- //继续调用决策
- this.handleDecision(tXinyiWarningRecord, tXinyiIndustry, normConfig);
- }else{
- log.info("{}:之前已经有过告警记录了,且还是继续报警,无需重复添加报警,但是决策仍然要调用", category);
- for (TXinyiWarningRecord xinyiWarningRecord : tXinyiWarningRecords) {//理论上只有一个的
- //继续调用决策
- this.handleDecision(xinyiWarningRecord, tXinyiIndustry, normConfig);
- }
- }
- }
- }
- /**
- * 定时从sqlserver获取数据
- */
- public void sqlserverData(){
- log.info("进入了定时同步SqlServer的任务");
- //主库获取上次最新的同步日期
- String lastTime = this.xinyiRobotMapper.selectLastTime();
- log.info("上次同步的日期是{}", lastTime);
- //从
- DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
- List<TXinyiRobot> tXinyiRobots = xinyiRobotMapper.selectTXinyiRobotListByTime(lastTime);
- DynamicDataSourceContextHolder.clearDataSourceType();
- // System.out.println(JSON.toJSONString(tXinyiRobots));
- // System.out.println("-------------");
- //主
- if(!CollectionUtils.isEmpty(tXinyiRobots)){
- for (TXinyiRobot tXinyiRobot : tXinyiRobots) {
- String date = handleDate(tXinyiRobot.getVDate().replaceAll(" ", ""));//有空格
- String time = handleDate(tXinyiRobot.getVTime().replaceAll(" ", ""));//有空格
- tXinyiRobot.setVDate(date);
- tXinyiRobot.setVTime(time);
- tXinyiRobot.setVDateTime(date + " " + time);
- //处理给前端展示的字段
- tXinyiRobot.setTestDate(date);//日期
- tXinyiRobot.setTestHour(date + " " + time.substring(0, 2));//小时
- tXinyiRobot.setTestTime(date + " " + time.substring(0, 5));//分钟
- tXinyiRobot.setCreatedTime(new Date());
- this.xinyiRobotMapper.insertTXinyiRobot(tXinyiRobot);
- }
- }
- }
- public static String handleDate(String str){
- StringBuilder sb = new StringBuilder();
- if(str.contains(" ")){//包含空格 就是年月日时分秒了
- String[] split = str.split(" ");
- addBeforeZero(sb, split[0], "/");
- sb.append(" ");
- addBeforeZero(sb, split[1], ":");
- }else{
- if(str.contains("/")){//年月日
- addBeforeZero(sb, str, "/");
- }else if(str.contains(":")){//时分秒
- addBeforeZero(sb, str, ":");
- }else {
- sb.append(str);
- }
- }
- return sb.toString();
- }
- public static StringBuilder addBeforeZero(StringBuilder sb, String str, String tag){
- String[] split = str.split(tag);
- int length = split.length;
- for (int i = 0; i < length; i++) {
- String value = split[i];
- Integer intValue = Integer.parseInt(value);
- if(intValue < 10 && value.length() == 1){////防止有正确的情况 额外再补充字符串
- sb.append(0).append(value);
- }else{
- sb.append(value);
- }
- if(i < length-1){
- sb.append(tag);
- }
- }
- return sb;
- }
- public static void main(String[] args) {
- /*LocalDateTime endTime = LocalDateTime.now();
- System.out.println("endTime = " + endTime);
- endTime = endTime.plusMinutes(60);
- System.out.println("endTime = " + endTime);*/
- /*String str = "2024/04/15 09:55";
- System.out.println(str);
- System.out.println(str.substring(0,10));
- System.out.println(str.substring(0,13));*/
- /*String str = "2024/04/18 08:00";
- str = str + ":00";
- System.out.println(str);
- LocalDateTime startTime = LocalDateTime.parse(str.replaceAll("/", "-").replace(" ", "T"));
- System.out.println(startTime.plusMinutes(1L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));*/
- ArrayList<Integer> objects = new ArrayList<>();
- objects.add(1);
- objects.add(2);
- objects.add(3);
- System.out.println(objects);
- objects.clear();
- System.out.println(objects);
- // test();
- Date date = new Date();
- System.out.println(DateUtils.differentHoursByMillisecond(date, date));
- HashMap<Object, Object> map = new HashMap<>();
- map.put("a", null);
- map.put("b", "ab");
- map.put("c", "");
- map.put("d", '1');
- System.out.println(JSON.toJSONString(map, JSONWriter.Feature.WriteNulls));
- TXinyiIndustry tXinyiIndustry = new TXinyiIndustry();
- System.out.println(JSON.toJSONString(tXinyiIndustry, JSONWriter.Feature.WriteNulls));
- }
- //测试工业库 没小时保存一条记录是否可行
- public static void test() {
- // 每个小时的时间格式
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
- LocalDateTime startTime = LocalDateTime.parse("2024-05-18T00:00:00");
- LocalDateTime endTime = LocalDateTime.now();
- // 循环按小时分割
- LocalDateTime currentHour = startTime;
- //最终获取的数据
- Map<String, TXinyiIndustry> needMap = new LinkedHashMap<>();
- while (currentHour.isBefore(endTime)) {
- String begin = currentHour.format(formatter);
- String end = currentHour.plusMinutes(5).format(formatter);
- // 输出当前小时的起始时间和结束时间
- System.out.println("起始时间:" + begin);
- System.out.println("结束时间:" + end);
- // 当前小时加一小时,作为下一个小时的起始时间
- currentHour = currentHour.plusMinutes(5);
- //每个小时查询一次数据
- String url = "http://10.0.0.27:4568/api/v1/khquerydata";
- HashMap<String, Object> req = new HashMap<>();
- req.put("tagNames", queryTags);
- req.put("startTime", begin);
- req.put("endTime", end);
- req.put("recordNumbers", 100000);
- String body = HttpRequest.post(url).header("Authorization", "c2E6c2E=").header("clientName", "hongshan").body(JSON.toJSONString(req)).execute().body();
- // System.out.println("body = " + body);
- List<HashMap<String, String>> list = new ArrayList<>();
- //行转列数据处理
- for (String queryTag : queryTags) {
- JSONArray array = JSON.parseObject(body).getJSONArray(queryTag);
- //特殊数据处理一
- if(Objects.isNull(array) || array.isEmpty()){
- System.out.println(queryTag + "查询到了空的数据,跳过本次循环");
- continue;
- }
- int size = array.size();
- //特殊数据处理二
- if("0".equals(array.get(1) + "")){
- System.out.println(queryTag + "查询到了数据,但是数据集合只有一条,且都是0");
- continue;
- }
- //结合至少62个数据才满足条件(有可能获取不到)
- /*if(size < 62){
- System.out.println(queryTag + "查询到了不符合条件的数据,跳过本次循环");
- continue;
- }*/
- //存放的数据集
- //利用map去重
- HashMap<String, String> map = new LinkedHashMap<>();
- for (int i = 2; i < size; i++) {
- // System.out.println(i + "" + array.get(i));
- JSONArray oneRecord = JSON.parseArray(JSON.toJSONString(array.get(i)));
- //处理为空或者为0的数据
- Object timeStampValue = oneRecord.get(2);
- if(Objects.isNull(timeStampValue) || "0".equals(timeStampValue + ""))
- continue;
- BigDecimal value = Objects.isNull(oneRecord.get(0)) ? null : new BigDecimal(oneRecord.get(0) + "");
- long timestamp = (long) timeStampValue;
- String format = DateUtil.format(new Date(timestamp), DateUtils.YYYYMMDDHH_TS);
- map.put(format, queryTag + "-" + value);
- }
- list.add(map);
- }
- Set<String> recordTimeSet = new HashSet<>();
- Map<String, TXinyiIndustry> recordMap = new HashMap<>();
- for (int i = 0; i < list.size(); i++) {
- HashMap<String, String> map = list.get(i);
- int finalJ = i;
- map.forEach((k, v) ->{
- TXinyiIndustry industry = null;
- if(!recordTimeSet.contains(k)){//第一次
- industry = new TXinyiIndustry();
- recordTimeSet.add(k);
- recordMap.put(k, industry);
- }else{
- industry = recordMap.get(k);
- }
- industry.setTestTime(k + ":00");
- //2024年4月15日11:19:52 额外增加2个字段
- industry.setTestDate(k.substring(0,10));
- industry.setTestHour(k.substring(0,13));
- //解析值
- String[] split = v.split("-");
- String type = split[0];
- BigDecimal value = new BigDecimal(split[1]);
- if ("信义污水厂JS_COD_Value".equals(type)) {
- industry.setJsCod(value);
- } else if ("信义污水厂JS_PH_Value".equals(type)) {
- industry.setJsPh(value);
- } else if ("信义污水厂JS_SS_Value".equals(type)) {
- industry.setJsSs(value);
- } else if ("信义污水厂JS_ZL_Value".equals(type)) {
- industry.setJsTp(value);
- } else if ("信义污水厂JS_ZA_Value".equals(type)) {
- industry.setJsTn(value);
- } else if ("信义污水厂JS_AD_Value".equals(type)) {
- industry.setJsNh3(value);
- } else if ("信义污水厂JS_T_Value".equals(type)) {
- industry.setJsSwPh(value);
- } else if ("信义污水厂进水泵房液位".equals(type)) {
- industry.setJsBfyw(value);
- } else if ("信义污水厂出水瞬时流量".equals(type)) {
- industry.setCsSlqc(value);
- } else if ("信义污水厂升级出水COD".equals(type)) {
- industry.setCsCod(value);
- } else if ("信义污水厂升级出水PH".equals(type)) {
- industry.setCsPh(value);
- } else if ("信义污水厂升级出水SS".equals(type)) {
- industry.setCsSs(value);
- } else if ("信义污水厂升级出水TN".equals(type)) {
- industry.setCsTn(value);
- } else if ("信义污水厂升级出水TP".equals(type)) {
- industry.setCsTp(value);
- } else if ("信义污水厂升级出水氨氮".equals(type)) {
- industry.setCsNh3(value);
- } else if ("信义污水厂AIT202_Value".equals(type)) {
- industry.setOneHyzdDo(value);
- } else if ("信义污水厂AIT203_Value".equals(type)) {
- industry.setOneHymdDo(value);
- } else if ("信义污水厂AIT207_Value".equals(type)) {
- industry.setTwoHyzdDo(value);
- } else if ("信义污水厂AIT206_Value".equals(type)) {
- industry.setTwoHymdDo(value);
- } else if ("信义污水厂AIT209_Value".equals(type)) {
- industry.setOneMlss(value);
- } else if ("信义污水厂AIT210_Value".equals(type)) {
- industry.setTwoMlss(value);
- } else if ("信义污水厂进水TDS".equals(type)) {
- industry.setJsTds(value);
- } else if ("信义污水厂FT101_Value".equals(type)) {
- industry.setJsSlq(value);
- } else if ("信义污水厂SWCHHYHLB1_R_Value".equals(type)) {
- industry.setNHlbOneGp(value);
- } else if ("信义污水厂SWCHHYHLB2_R_Value".equals(type)) {
- industry.setNHlbTwoGp(value);
- } else if ("信义污水厂SWCHHYHLB3_R_Value".equals(type)) {
- industry.setNHlbThreeGp(value);
- } else if ("信义污水厂SWCHHYHLB4_R_Value".equals(type)) {
- industry.setNHlbFourGp(value);
- } else if ("信义污水厂SWCHHYHLB5_R_Value".equals(type)) {
- industry.setNhlBFiveGp(value);
- } else if ("信义污水厂SWCHHYHLB6_R_Value".equals(type)) {
- industry.setNHlbSixGp(value);
- } else if ("信义污水厂SWCWNHLB1_R_Value".equals(type)) {
- industry.setWHlbOneGp(value);
- } else if ("信义污水厂SWCWNHLB2_R_Value".equals(type)) {
- industry.setWHlbTwoGp(value);
- } else if ("信义污水厂SWCWNHLB3_R_Value".equals(type)) {
- industry.setWHlbThreeGp(value);
- } else if ("信义污水厂SWCWNHLB4_R_Value".equals(type)) {
- industry.setWHlbFourGp(value);
- } else if ("信义污水厂SWCWNHLB5_R_Value".equals(type)) {
- industry.setWHlbFiveGp(value);
- } else if ("信义污水厂GFJ1_R_Value".equals(type)) {
- industry.setFjOne(value);
- } else if ("信义污水厂GFJ2_R_Value".equals(type)) {
- industry.setFjTwo(value);
- } else if ("信义污水厂GFJ3_R_Value".equals(type)) {
- industry.setFjThree(value);
- } else if ("信义污水厂GFJ4_R_Value".equals(type)) {
- industry.setFjFour(value);
- } else if ("信义污水厂GFJ5_R_Value".equals(type)) {
- industry.setFjFive(value);
- } else if ("信义污水厂GFJ6_R_Value".equals(type)) {
- industry.setFjSix(value);
- } else if ("信义污水厂GFJ1_KQLL_Value".equals(type)) {
- industry.setKqllOne(value);
- } else if ("信义污水厂GFJ2_KQLL_Value".equals(type)) {
- industry.setKqllTwo(value);
- } else if ("信义污水厂GFJ3_KQLL_Value".equals(type)) {
- industry.setKqllThree(value);
- } else if ("信义污水厂GFJ4_KQLL_Value".equals(type)) {
- industry.setKqllFour(value);
- } else if ("信义污水厂GFJ5_KQLL_Value".equals(type)) {
- industry.setKqllFive(value);
- } else if ("信义污水厂GFJ6_KQLL_Value".equals(type)) {
- industry.setKqllSix(value);
- }else if ("信义污水厂实际碳源加药量".equals(type)) {
- industry.setSJTYJLY(value);
- }else if ("信义污水厂除磷加药瞬时流量".equals(type)) {
- industry.setCLJYSSLL(value);
- } else if ("信义污水厂_除磷P04预测值_".equals(type)) {
- industry.setCLP04YCZ(value);
- }
- //只有最后一次才执行数据库添加
- if(finalJ == list.size()-1){
- needMap.put(industry.getTestHour(), industry);
- }
- });
- }
- }
- //保存数据 触发告警 决策 问答记录等等
- needMap.forEach((k, industry) ->{
- System.out.println(JSON.toJSONString(industry));
- });
- }
- }
|