RyTask.java 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010
  1. package com.slibra.quartz.task;
  2. import cn.hutool.core.date.DateUtil;
  3. import cn.hutool.http.HttpRequest;
  4. import com.alibaba.fastjson2.JSON;
  5. import com.alibaba.fastjson2.JSONArray;
  6. import com.alibaba.fastjson2.JSONObject;
  7. import com.alibaba.fastjson2.JSONWriter;
  8. import com.google.protobuf.ByteString;
  9. import com.slibra.business.domain.*;
  10. import com.slibra.business.mapper.*;
  11. import com.slibra.business.req.ChatReq;
  12. import com.slibra.business.req.DecisionReq;
  13. import com.slibra.business.res.ChartBasic;
  14. import com.slibra.business.res.ShowValueBasic;
  15. import com.slibra.common.constant.MyConstants;
  16. import com.slibra.common.enums.BusinessEnum;
  17. import com.slibra.common.enums.DataSourceType;
  18. import com.slibra.common.utils.DateUtils;
  19. import com.slibra.common.utils.uuid.IdUtils;
  20. import com.slibra.framework.datasource.DynamicDataSourceContextHolder;
  21. import inference.InferenceAPIsServiceGrpc;
  22. import inference.PredictionResponse;
  23. import inference.PredictionsRequest;
  24. import io.grpc.ManagedChannel;
  25. import io.grpc.ManagedChannelBuilder;
  26. import lombok.extern.slf4j.Slf4j;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.stereotype.Component;
  29. import com.slibra.common.utils.StringUtils;
  30. import org.springframework.util.CollectionUtils;
  31. import java.io.IOException;
  32. import java.math.BigDecimal;
  33. import java.math.RoundingMode;
  34. import java.time.LocalDateTime;
  35. import java.time.format.DateTimeFormatter;
  36. import java.util.*;
  37. import static com.slibra.common.constant.MyConstants.*;
  38. /**
  39. * 定时任务调度测试
  40. *
  41. *
  42. */
  43. @Component("ryTask")
  44. @Slf4j
  45. public class RyTask
  46. {
  47. public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
  48. {
  49. System.out.println(StringUtils.format("执行多参方法: 字符串类型{},布尔类型{},长整型{},浮点型{},整形{}", s, b, l, d, i));
  50. }
  51. public void ryParams(String params)
  52. {
  53. System.out.println("执行有参方法:" + params);
  54. }
  55. public void ryNoParams()
  56. {
  57. System.out.println("执行无参方法");
  58. }
  59. //----------------------------------------------下面是新增的方法----------------------------------------------
  60. @Autowired
  61. private TXinyiIndustryMapper xinyiIndustryMapper;
  62. @Autowired
  63. private TXinyiRobotMapper xinyiRobotMapper;
  64. @Autowired
  65. private TXinyiNormConfigMapper xinyiNormConfigMapper;
  66. @Autowired
  67. private TXinyiWarningRecordMapper xinyiWarningRecordMapper;
  68. @Autowired
  69. private TXinyiChatRecordMapper xinyiChatRecordMapper;
  70. @Autowired
  71. private TXinyiDailyMapper xinyiDailyMapper;
  72. @Autowired
  73. private AsyncTask asyncTask;
  74. // public final static StopWatch watch = new StopWatch("task");
  75. 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预测值_"};
  76. public static final String[] predictorArr = {"出水COD", "出水SS", "出水总氮", "出水总磷", "出水氨氮", "xsy1", "xsy2"};
  77. public static final String[] predictorArrStr = {"出水COD", "出水SS", "出水总氮", "出水总磷", "出水氨氮", "硝酸盐#1", "硝酸盐#2"};
  78. /**
  79. * 定时从工业库获取数据
  80. *
  81. * 2024年4月17日17:44:15 调整逻辑:考虑到因断电等情况导致服务断电,所以不再同步最近一小时,而是同步从上次成功的最后一条数据开始。
  82. */
  83. public void getIndustryData(){
  84. log.info("进入了定时同步工业库数据的任务");
  85. //耗时工具
  86. // watch.start("parseJob");
  87. // 给定时间段的起始时间和结束时间
  88. LocalDateTime endTime = LocalDateTime.now();
  89. // LocalDateTime startTime = endTime.plusMinutes(-60);
  90. //获取上次最后一条同步的数据的日期到 分钟维度
  91. String lastDateHour = this.xinyiIndustryMapper.getLastMinute();
  92. log.info("获取上次同步工业库的最后一条记录的时间是{}", lastDateHour);
  93. lastDateHour = lastDateHour + ":00";
  94. //开始时间
  95. LocalDateTime startTime = LocalDateTime.parse(lastDateHour.replaceAll("/", "-").replace(" ", "T"));
  96. startTime = startTime.plusMinutes(60L);//加一分钟 从上次最后一条记录的下一分钟开始
  97. /*LocalDateTime startTime = LocalDateTime.parse("2024-02-26T00:00:00");
  98. LocalDateTime endTime = LocalDateTime.parse("2024-02-27T00:00:00");*/
  99. // 每个小时的时间格式
  100. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  101. // 循环按小时分割
  102. LocalDateTime currentHour = startTime;
  103. //最终获取的数据
  104. Map<String, TXinyiIndustry> needMap = new LinkedHashMap<>();
  105. while (currentHour.isBefore(endTime)) {
  106. String begin = currentHour.format(formatter);
  107. String end = currentHour.plusMinutes(5).format(formatter);
  108. // 输出当前小时的起始时间和结束时间
  109. System.out.println("起始时间:" + begin);
  110. System.out.println("结束时间:" + end);
  111. // 当前小时加一小时,作为下一个小时的起始时间
  112. currentHour = currentHour.plusMinutes(5);
  113. //每个小时查询一次数据
  114. String url = "http://10.0.0.27:4568/api/v1/khquerydata";
  115. HashMap<String, Object> req = new HashMap<>();
  116. req.put("tagNames", queryTags);
  117. req.put("startTime", begin);
  118. req.put("endTime", end);
  119. req.put("recordNumbers", 100000);
  120. String body = HttpRequest.post(url).header("Authorization", "c2E6c2E=").header("clientName", "hongshan").body(JSON.toJSONString(req)).execute().body();
  121. // System.out.println("body = " + body);
  122. List<HashMap<String, String>> list = new ArrayList<>();
  123. //行转列数据处理
  124. for (String queryTag : queryTags) {
  125. JSONArray array = JSON.parseObject(body).getJSONArray(queryTag);
  126. //特殊数据处理一
  127. if(Objects.isNull(array) || array.isEmpty()){
  128. System.out.println(queryTag + "查询到了空的数据,跳过本次循环");
  129. continue;
  130. }
  131. int size = array.size();
  132. //特殊数据处理二
  133. if("0".equals(array.get(1) + "")){
  134. System.out.println(queryTag + "查询到了数据,但是数据集合只有一条,且都是0");
  135. continue;
  136. }
  137. //结合至少62个数据才满足条件(有可能获取不到)
  138. /*if(size < 62){
  139. System.out.println(queryTag + "查询到了不符合条件的数据,跳过本次循环");
  140. continue;
  141. }*/
  142. //存放的数据集
  143. //利用map去重
  144. HashMap<String, String> map = new LinkedHashMap<>();
  145. for (int i = 2; i < size; i++) {
  146. // System.out.println(i + "" + array.get(i));
  147. JSONArray oneRecord = JSON.parseArray(JSON.toJSONString(array.get(i)));
  148. //处理为空或者为0的数据
  149. Object timeStampValue = oneRecord.get(2);
  150. if(Objects.isNull(timeStampValue) || "0".equals(timeStampValue + ""))
  151. continue;
  152. BigDecimal value = Objects.isNull(oneRecord.get(0)) ? null : new BigDecimal(oneRecord.get(0) + "");
  153. long timestamp = (long) timeStampValue;
  154. String format = DateUtil.format(new Date(timestamp), DateUtils.YYYYMMDDHH_TS);
  155. map.put(format, queryTag + "-" + value);
  156. }
  157. list.add(map);
  158. }
  159. Set<String> recordTimeSet = new HashSet<>();
  160. Map<String, TXinyiIndustry> recordMap = new HashMap<>();
  161. for (int i = 0; i < list.size(); i++) {
  162. HashMap<String, String> map = list.get(i);
  163. int finalJ = i;
  164. map.forEach((k, v) ->{
  165. TXinyiIndustry industry = null;
  166. if(!recordTimeSet.contains(k)){//第一次
  167. industry = new TXinyiIndustry();
  168. recordTimeSet.add(k);
  169. recordMap.put(k, industry);
  170. }else{
  171. industry = recordMap.get(k);
  172. }
  173. industry.setTestTime(k + ":00");
  174. //2024年4月15日11:19:52 额外增加2个字段
  175. industry.setTestDate(k.substring(0,10));
  176. industry.setTestHour(k.substring(0,13));
  177. //解析值
  178. String[] split = v.split("-");
  179. String type = split[0];
  180. BigDecimal value = new BigDecimal(split[1]);
  181. if ("信义污水厂JS_COD_Value".equals(type)) {
  182. industry.setJsCod(value);
  183. } else if ("信义污水厂JS_PH_Value".equals(type)) {
  184. industry.setJsPh(value);
  185. } else if ("信义污水厂JS_SS_Value".equals(type)) {
  186. // log.info("************----------真实的进水SS的值为{}\n时间为{}", value, k);
  187. industry.setJsSs(value);
  188. } else if ("信义污水厂JS_ZL_Value".equals(type)) {
  189. industry.setJsTp(value);
  190. } else if ("信义污水厂JS_ZA_Value".equals(type)) {
  191. industry.setJsTn(value);
  192. } else if ("信义污水厂JS_AD_Value".equals(type)) {
  193. industry.setJsNh3(value);
  194. } else if ("信义污水厂JS_T_Value".equals(type)) {
  195. industry.setJsSwPh(value);
  196. } else if ("信义污水厂进水泵房液位".equals(type)) {
  197. industry.setJsBfyw(value);
  198. } else if ("信义污水厂出水瞬时流量".equals(type)) {
  199. industry.setCsSlqc(value);
  200. } else if ("信义污水厂升级出水COD".equals(type)) {
  201. industry.setCsCod(value);
  202. } else if ("信义污水厂升级出水PH".equals(type)) {
  203. industry.setCsPh(value);
  204. } else if ("信义污水厂升级出水SS".equals(type)) {
  205. industry.setCsSs(value);
  206. } else if ("信义污水厂升级出水TN".equals(type)) {
  207. // log.info("************----------真实的出水总氮的值为{}\n时间为{}", value, k);
  208. // industry.setCsTn(new BigDecimal(17));
  209. industry.setCsTn(value);
  210. } else if ("信义污水厂升级出水TP".equals(type)) {
  211. industry.setCsTp(value);
  212. } else if ("信义污水厂升级出水氨氮".equals(type)) {
  213. industry.setCsNh3(value);
  214. } else if ("信义污水厂AIT202_Value".equals(type)) {
  215. industry.setOneHyzdDo(value);
  216. } else if ("信义污水厂AIT203_Value".equals(type)) {
  217. industry.setOneHymdDo(value);
  218. } else if ("信义污水厂AIT207_Value".equals(type)) {
  219. industry.setTwoHyzdDo(value);
  220. } else if ("信义污水厂AIT206_Value".equals(type)) {
  221. industry.setTwoHymdDo(value);
  222. } else if ("信义污水厂AIT209_Value".equals(type)) {
  223. industry.setOneMlss(value);
  224. } else if ("信义污水厂AIT210_Value".equals(type)) {
  225. industry.setTwoMlss(value);
  226. } else if ("信义污水厂进水TDS".equals(type)) {
  227. industry.setJsTds(value);
  228. } else if ("信义污水厂FT101_Value".equals(type)) {
  229. industry.setJsSlq(value);
  230. } else if ("信义污水厂SWCHHYHLB1_R_Value".equals(type)) {
  231. industry.setNHlbOneGp(value);
  232. } else if ("信义污水厂SWCHHYHLB2_R_Value".equals(type)) {
  233. industry.setNHlbTwoGp(value);
  234. } else if ("信义污水厂SWCHHYHLB3_R_Value".equals(type)) {
  235. industry.setNHlbThreeGp(value);
  236. } else if ("信义污水厂SWCHHYHLB4_R_Value".equals(type)) {
  237. industry.setNHlbFourGp(value);
  238. } else if ("信义污水厂SWCHHYHLB5_R_Value".equals(type)) {
  239. industry.setNhlBFiveGp(value);
  240. } else if ("信义污水厂SWCHHYHLB6_R_Value".equals(type)) {
  241. industry.setNHlbSixGp(value);
  242. } else if ("信义污水厂SWCWNHLB1_R_Value".equals(type)) {
  243. industry.setWHlbOneGp(value);
  244. } else if ("信义污水厂SWCWNHLB2_R_Value".equals(type)) {
  245. industry.setWHlbTwoGp(value);
  246. } else if ("信义污水厂SWCWNHLB3_R_Value".equals(type)) {
  247. industry.setWHlbThreeGp(value);
  248. } else if ("信义污水厂SWCWNHLB4_R_Value".equals(type)) {
  249. industry.setWHlbFourGp(value);
  250. } else if ("信义污水厂SWCWNHLB5_R_Value".equals(type)) {
  251. industry.setWHlbFiveGp(value);
  252. } else if ("信义污水厂GFJ1_R_Value".equals(type)) {
  253. industry.setFjOne(value);
  254. } else if ("信义污水厂GFJ2_R_Value".equals(type)) {
  255. industry.setFjTwo(value);
  256. } else if ("信义污水厂GFJ3_R_Value".equals(type)) {
  257. industry.setFjThree(value);
  258. } else if ("信义污水厂GFJ4_R_Value".equals(type)) {
  259. industry.setFjFour(value);
  260. } else if ("信义污水厂GFJ5_R_Value".equals(type)) {
  261. industry.setFjFive(value);
  262. } else if ("信义污水厂GFJ6_R_Value".equals(type)) {
  263. industry.setFjSix(value);
  264. } else if ("信义污水厂GFJ1_KQLL_Value".equals(type)) {
  265. industry.setKqllOne(value);
  266. } else if ("信义污水厂GFJ2_KQLL_Value".equals(type)) {
  267. industry.setKqllTwo(value);
  268. } else if ("信义污水厂GFJ3_KQLL_Value".equals(type)) {
  269. industry.setKqllThree(value);
  270. } else if ("信义污水厂GFJ4_KQLL_Value".equals(type)) {
  271. industry.setKqllFour(value);
  272. } else if ("信义污水厂GFJ5_KQLL_Value".equals(type)) {
  273. industry.setKqllFive(value);
  274. } else if ("信义污水厂GFJ6_KQLL_Value".equals(type)) {
  275. industry.setKqllSix(value);
  276. }else if ("信义污水厂实际碳源加药量".equals(type)) {
  277. industry.setSJTYJLY(value);
  278. }else if ("信义污水厂除磷加药瞬时流量".equals(type)) {
  279. industry.setCLJYSSLL(value);
  280. } else if ("信义污水厂_除磷P04预测值_".equals(type)) {
  281. industry.setCLP04YCZ(value);
  282. }
  283. //只有最后一次才执行数据库添加
  284. if(finalJ == list.size()-1){
  285. needMap.put(industry.getTestHour(), industry);
  286. }
  287. });
  288. }
  289. }
  290. //保存数据 触发告警 决策 问答记录等等
  291. needMap.forEach((k, industry) ->{
  292. //2024年4月22日15:45:24 额外保存两个字段 数组
  293. List<BigDecimal> extraList = new ArrayList<>();
  294. extraList.add(industry.getOneHymdDo());
  295. extraList.add(industry.getTwoHymdDo());
  296. industry.setHycRjyAll(JSON.toJSONString(extraList));
  297. extraList.clear();
  298. extraList.add(industry.getOneHyzdDo());
  299. extraList.add(industry.getTwoHyzdDo());
  300. industry.setHycRjyZdAll(JSON.toJSONString(extraList));
  301. extraList.clear();
  302. extraList.add(industry.getOneMlss());
  303. extraList.add(industry.getTwoMlss());
  304. industry.setHycWnndAll(JSON.toJSONString(extraList));
  305. //插入数据库
  306. xinyiIndustryMapper.insertTXinyiIndustry(industry);
  307. //判断是否触发告警、接触告警、保存决策等等
  308. // this.handleWarning(industry);
  309. //2024年5月30日18:29:20 改为异步处理
  310. asyncTask.handleWarning(industry);
  311. });
  312. //执行完成 测试执行时间
  313. //计时结束
  314. // watch.stop();
  315. // System.out.println(watch.getLastTaskName() + " 执行耗时:" + watch.getLastTaskTimeMillis() + " ms");
  316. }
  317. private void handleWarning(TXinyiIndustry tXinyiIndustry) {
  318. log.info("进入了定时任务保存工业库数据并触发报警操作");
  319. // tXinyiIndustry.setCsTn(new BigDecimal(17));
  320. //获取配置表
  321. List<TXinyiNormConfig> tXinyiNormConfigs = this.xinyiNormConfigMapper.selectTXinyiNormConfigList(null);
  322. if(CollectionUtils.isEmpty(tXinyiNormConfigs))
  323. throw new RuntimeException("未查询到配置信息");
  324. TXinyiNormConfig normConfig = tXinyiNormConfigs.get(0);
  325. //水质报警
  326. this.handleSZWarning(tXinyiIndustry, normConfig);
  327. //2024年5月28日14:14:26 下面是新增的 生化报警处理
  328. this.handleSHWarning(tXinyiIndustry, normConfig);
  329. }
  330. private void handleSHWarning(TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
  331. //判断对应指标是否报警 然后调研大模型获取决策信息
  332. BigDecimal jsSlq = tXinyiIndustry.getJsSlq();
  333. //内回流比报警
  334. BigDecimal nhlbSjz = normConfig.getNhlbSjz();//400
  335. BigDecimal nhlbnkSxz = normConfig.getNhlbnkSxz();//360
  336. BigDecimal nhlbqdsl = normConfig.getNHLBQDSL();
  337. BigDecimal nhlbdsjll = normConfig.getNHLBDSJLL();
  338. BigDecimal nhlbgzxl = normConfig.getNHLBGZXL();
  339. if(!Objects.isNull(jsSlq) && !Objects.isNull(nhlbnkSxz) && !Objects.isNull(nhlbqdsl)
  340. && !Objects.isNull(nhlbdsjll) && !Objects.isNull(nhlbgzxl)){
  341. BigDecimal divide = nhlbqdsl.multiply(nhlbdsjll).multiply(nhlbgzxl).divide(jsSlq, 4, RoundingMode.HALF_UP);
  342. if(!Objects.isNull(nhlbSjz)){
  343. handleXinYiWarningsSH(nhlbSjz, divide, nhlbnkSxz, BusinessEnum.WarningCategoryEnum.NHLB.getCode(), tXinyiIndustry, normConfig);
  344. }
  345. }
  346. //外回流比报警
  347. BigDecimal whlbqdsl = normConfig.getWHLBQDSL();
  348. BigDecimal whlbdsjll = normConfig.getWHLBDSJLL();
  349. BigDecimal whlbgzxl = normConfig.getWHLBGZXL();
  350. BigDecimal whlbnkXxz = normConfig.getWhlbnkXxz();
  351. BigDecimal whlbSjz = normConfig.getWhlbSjz();
  352. if(!Objects.isNull(whlbqdsl) && !Objects.isNull(whlbdsjll) && !Objects.isNull(whlbgzxl)){
  353. BigDecimal divide = whlbqdsl.multiply(whlbdsjll).multiply(whlbgzxl).divide(jsSlq, 4, RoundingMode.HALF_UP);
  354. if(!Objects.isNull(whlbSjz)){
  355. handleXinYiWarningsSH(whlbSjz, divide, whlbnkXxz, BusinessEnum.WarningCategoryEnum.WHLB.getCode(), tXinyiIndustry, normConfig);
  356. }
  357. }
  358. //最新的一条日报数据
  359. List<TXinyiDaily> tXinyiDailies = this.xinyiDailyMapper.selectTXinyiDailyList(null);
  360. if(CollectionUtils.isEmpty(tXinyiDailies))
  361. return;//肯定不会出现这种情况 因为有很多历史数据了
  362. TXinyiDaily tXinyiDaily = tXinyiDailies.get(0);
  363. log.info("生化报警获取日报的最新的一条数据为{}", JSON.toJSONString(tXinyiDaily));
  364. BigDecimal jsBod5 = tXinyiDaily.getJsBod5();
  365. //污泥负荷(需要从日报获取数据) 计算 + 部分数据从日报获取
  366. //污泥负荷=[Ls]=24*([BOD_in]-[BOD_off])*[Q_in]/[MLSS]/([V_hao]+[V_que]+[V_yan])/2 kgBOD/(kgMLSS·d)
  367. //todo 后面再加 计算太复杂
  368. //碳氮比(需要从日报获取数据)
  369. //进水碳氮比=[b]=[BOD_in]/[TN_in]
  370. BigDecimal jsTn = tXinyiIndustry.getJsTn();
  371. if(!Objects.isNull(jsBod5) && !Objects.isNull(jsTn)){
  372. BigDecimal divide = jsBod5.divide(jsTn, 4, RoundingMode.HALF_UP);
  373. BigDecimal jstdbnkzSxz = normConfig.getJstdbnkzSxz();
  374. BigDecimal cstdbSjz = normConfig.getCstdbSjz();
  375. if(!Objects.isNull(whlbSjz)){
  376. handleXinYiWarningsSH(cstdbSjz, divide, jstdbnkzSxz, BusinessEnum.WarningCategoryEnum.TDB.getCode(), tXinyiIndustry, normConfig);
  377. }
  378. }
  379. //碳磷比(需要从日报获取数据)
  380. //进水碳磷比=[c]=[BOD_in]/[TP_in]
  381. BigDecimal jsTp = tXinyiIndustry.getJsTp();
  382. BigDecimal jstlbSjz = normConfig.getJstlbSjz();
  383. if(!Objects.isNull(jsBod5) && !Objects.isNull(jsTp)){
  384. BigDecimal divide = jsBod5.divide(jsTp, 4, RoundingMode.HALF_UP);
  385. BigDecimal jstlbNkz = normConfig.getJstlbNkz();
  386. if(!Objects.isNull(jstlbSjz)){
  387. handleXinYiWarningsSH(jstlbSjz, divide, jstlbNkz, BusinessEnum.WarningCategoryEnum.TLB.getCode(), tXinyiIndustry, normConfig);
  388. }
  389. }
  390. //BOD比COD(需要从日报获取数据)
  391. //进水BOD与COD比值数据=[d]=[BOD_in]/[COD_in]
  392. BigDecimal jsCod = tXinyiIndustry.getJsCod();
  393. if(!Objects.isNull(jsBod5) && !Objects.isNull(jsCod)){
  394. BigDecimal jsbodycodbzSzj = normConfig.getJsbodycodbzSzj();
  395. String jsbodycodbzGkz = normConfig.getJsbodycodbzGkz();
  396. if(StringUtils.isNotBlank(jsbodycodbzGkz) && jsbodycodbzGkz.contains("-")){
  397. String[] split = jsbodycodbzGkz.split("-");
  398. BigDecimal gkz = new BigDecimal(split[1]);
  399. BigDecimal divide = jsBod5.divide(jsCod, 4, RoundingMode.HALF_UP);
  400. handleXinYiWarningsSH(gkz, divide, jsbodycodbzSzj, BusinessEnum.WarningCategoryEnum.BODCODB.getCode(), tXinyiIndustry, normConfig);
  401. }
  402. }
  403. //好氧区DO(一池)
  404. BigDecimal shcHyOneDo = tXinyiIndustry.getOneHymdDo();
  405. BigDecimal hycrjysjzSxz = normConfig.getHycrjysjzSxz();
  406. BigDecimal hycrjyNkz = normConfig.getHycrjyNkz();
  407. if(!Objects.isNull(shcHyOneDo) && !Objects.isNull(hycrjysjzSxz)){
  408. handleXinYiWarningsSH(hycrjysjzSxz, shcHyOneDo, hycrjyNkz, BusinessEnum.WarningCategoryEnum.HYQDO_ONE.getCode(), tXinyiIndustry, normConfig);
  409. }
  410. //好氧区DO(二池)
  411. BigDecimal shcHyTwoDo = tXinyiIndustry.getTwoHymdDo();
  412. if(!Objects.isNull(shcHyTwoDo) && !Objects.isNull(hycrjysjzSxz)){
  413. handleXinYiWarningsSH(hycrjysjzSxz, shcHyTwoDo, hycrjyNkz, BusinessEnum.WarningCategoryEnum.HYQDO_TWO.getCode(), tXinyiIndustry, normConfig);
  414. }
  415. //气水比(需要从日报获取数据)
  416. BigDecimal gfjgzts = normConfig.getGFJGZTS();
  417. BigDecimal gfjckll = normConfig.getGFJCKLL();
  418. BigDecimal shcqbSjz = normConfig.getShcqbSjz();
  419. if(!Objects.isNull(gfjgzts) && !Objects.isNull(gfjckll) && !Objects.isNull(shcqbSjz) && !Objects.isNull(jsSlq)){
  420. BigDecimal qsb = gfjgzts.multiply(gfjckll).divide(jsSlq, 4, RoundingMode.HALF_UP);
  421. BigDecimal shcqbNkz = normConfig.getShcqbNkz();
  422. handleXinYiWarningsSH(shcqbSjz, qsb, shcqbNkz, BusinessEnum.WarningCategoryEnum.WHLB.getCode(), tXinyiIndustry, normConfig);
  423. }
  424. }
  425. /**
  426. * 处理信义生化报警的逻辑 统一处理
  427. * @param bzz
  428. * @param currentVal
  429. * @param gkz
  430. * @param category
  431. * @param tXinyiIndustry
  432. * @param normConfig
  433. */
  434. private void handleXinYiWarningsSH(BigDecimal bzz, BigDecimal currentVal, BigDecimal gkz, String category, TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
  435. BigDecimal multiply = bzz.multiply(new BigDecimal(MyConstants.SCALE_VALUE));
  436. TXinyiWarningRecord tXinyiWarningRecord = new TXinyiWarningRecord();
  437. /*String category = BusinessEnum.WarningCategoryEnum.CS_AD.getCode();*/
  438. tXinyiWarningRecord.setStatus(0);
  439. tXinyiWarningRecord.setType(1);
  440. tXinyiWarningRecord.setCategory(category);
  441. tXinyiWarningRecord.setTime(DateUtils.getNowDate());
  442. tXinyiWarningRecord.setWarningVal(currentVal);
  443. tXinyiWarningRecord.setDesignVal(bzz);
  444. tXinyiWarningRecord.setControlVal(gkz);
  445. tXinyiWarningRecord.setCreateBy(WARNING_DEFAULT_CREATE);
  446. tXinyiWarningRecord.setCreateTime(DateUtils.getNowDate());
  447. //2024年5月25日17:52:33 如果工业库获取不到数据,也触发报警,但是不调用决策接口
  448. if (Objects.isNull(currentVal)) {
  449. tXinyiWarningRecord.setReason(category + EXCEPTION_WARNING);
  450. tXinyiWarningRecord.setLevel(WARNING_LEVEL_NO_DATE);
  451. } else if (currentVal.compareTo(multiply) > 0) {//一级
  452. tXinyiWarningRecord.setReason(category + CHAOBIAO_WARNING);
  453. tXinyiWarningRecord.setLevel(WARNING_LEVEL_ONE);
  454. } else if (currentVal.compareTo(bzz) >= 0 && currentVal.compareTo(multiply) <= 0) {//二级
  455. tXinyiWarningRecord.setReason(category + CHAOBIAO_WARNING);
  456. tXinyiWarningRecord.setLevel(WARNING_LEVEL_TWO);
  457. } else if (!Objects.isNull(gkz) && currentVal.compareTo(gkz) > 0) {
  458. tXinyiWarningRecord.setReason(category + CHAOGUANKONG_WARNING);
  459. tXinyiWarningRecord.setLevel(WARNING_LEVEL_THREE);
  460. } else {
  461. tXinyiWarningRecord = null;//这种的无需处理
  462. }
  463. //当前状态正常 需要查询历史有无正在报警的数据,如果有,将报警状态改完2(系统自动关闭)
  464. List<TXinyiWarningRecord> tXinyiWarningRecords = this.xinyiWarningRecordMapper.selectTXinyiWarningRecordList(TXinyiWarningRecord.builder().delFlag(0).type(1).category(category).status(0).build());
  465. if(Objects.isNull(tXinyiWarningRecord)){//数据正常,无告警信息
  466. if(!CollectionUtils.isEmpty(tXinyiWarningRecords)){
  467. log.info( "{}:现在恢复正常,历史报警数据为{}", category,JSON.toJSONString(tXinyiWarningRecords));
  468. for (TXinyiWarningRecord xinyiWarningRecord : tXinyiWarningRecords) {
  469. xinyiWarningRecord.setStatus(2);
  470. Date nowDate = DateUtils.getNowDate();
  471. xinyiWarningRecord.setOffTime(nowDate);
  472. xinyiWarningRecord.setUpdateTime(nowDate);
  473. xinyiWarningRecord.setUpdateBy(WARNING_DEFAULT_CREATE);
  474. this.xinyiWarningRecordMapper.updateTXinyiWarningRecord(xinyiWarningRecord);
  475. }
  476. }
  477. }else{//有新的告警信息
  478. if(CollectionUtils.isEmpty(tXinyiWarningRecords)){//之前没有告警记录
  479. //保存到数据库中
  480. this.xinyiWarningRecordMapper.insertTXinyiWarningRecord(tXinyiWarningRecord);
  481. if(WARNING_LEVEL_NO_DATE.equals(tXinyiWarningRecord.getLevel())){
  482. //只保存一个普通的问答记录 不需要调用决策信息,但是实时数据还是要记录的
  483. this.addChatRecordByWarning(tXinyiWarningRecord, tXinyiIndustry, normConfig);
  484. }else {
  485. //继续调用决策(普通问答)
  486. this.askBigModelForSHWarning(tXinyiWarningRecord, tXinyiIndustry, normConfig);
  487. }
  488. }else{
  489. log.info("{}:之前已经有过告警记录了,且还是继续报警,无需重复添加报警,但是决策仍然要调用", category);
  490. for (TXinyiWarningRecord xinyiWarningRecord : tXinyiWarningRecords) {//理论上只有一个的
  491. if(WARNING_LEVEL_NO_DATE.equals(tXinyiWarningRecord.getLevel())){
  492. //只保存一个普通的问答记录 不需要调用决策信息,但是实时数据还是要记录的
  493. this.addChatRecordByWarning(tXinyiWarningRecord, tXinyiIndustry, normConfig);
  494. }else {
  495. //继续调用决策(普通问答)
  496. this.askBigModelForSHWarning(xinyiWarningRecord, tXinyiIndustry, normConfig);
  497. }
  498. }
  499. }
  500. }
  501. }
  502. private void askBigModelForSHWarning(TXinyiWarningRecord xinyiWarningRecord, TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
  503. log.info("进入了后台接口调⽤⼤模型获取问答结果处理");
  504. StringBuilder sb = new StringBuilder();
  505. String sessionId = IdUtils.simpleUUID();
  506. ChatReq chatReq = new ChatReq();
  507. // String ipAddr = IpUtils.getIpAddr();//获取用户的ip地址 传给大模型
  508. String ipAddr = "";//获取用户的ip地址 传给大模型 定时任务获取不到ip地址
  509. int counts = 1;//默认是第一次
  510. //这种问答 没有历史问答的概念 直接把问题扔进去就行 无需查询历史记录
  511. List<String> historyDates = new ArrayList<>();
  512. //构建问题(替换提示词中的占位符)
  513. String shWarningPrompt = SH_WARNING_PROMPT;
  514. shWarningPrompt =shWarningPrompt.replace("#{0}", xinyiWarningRecord.getReason());
  515. shWarningPrompt =shWarningPrompt.replace("#{1}", String.valueOf(xinyiWarningRecord.getDesignVal()));
  516. shWarningPrompt =shWarningPrompt.replace("#{2}", String.valueOf(xinyiWarningRecord.getControlVal()));
  517. shWarningPrompt =shWarningPrompt.replace("#{3}", String.valueOf(xinyiWarningRecord.getWarningVal()));
  518. historyDates.add(shWarningPrompt);
  519. // 获取输出流
  520. ManagedChannel channel = null;
  521. try {
  522. channel = ManagedChannelBuilder.forAddress("10.0.0.24", 17070)
  523. .usePlaintext()
  524. .build();
  525. InferenceAPIsServiceGrpc.InferenceAPIsServiceBlockingStub stub = InferenceAPIsServiceGrpc.newBlockingStub(channel);
  526. String dataJson = "{\"bot_id\":\"721\",\"exp_id\":\"721\",\"session_id\":\"" + sessionId + "\",\"use_rag\":\"true\",\"prompt\":\"你是⼀个资深⽔务领域专家,能回答各种⽔务相关问题\",\"history_dia\":" + JSON.toJSONString(historyDates) + ",\"generate_args\":{\"max_new_tokens\":2048,\"max_length\":4096,\"num_beams\":1,\"do_sample\":true,\"top_p\":0.7,\"temperature\":0.95},\"extra\":{ \"ip_address\": \"" + ipAddr + "\" },\"strengthen\":" + (true) + "}";
  527. log.info("请求大模型的问答参数为{}", dataJson);
  528. PredictionsRequest request = PredictionsRequest.newBuilder()
  529. .setModelName("slibra_bot")
  530. .putInput("method", ByteString.copyFrom("infer_stream", "utf-8"))//推理
  531. .putInput("data", ByteString.copyFrom(dataJson, "utf-8"))
  532. .buildPartial();
  533. Iterator<PredictionResponse> predictions = stub.streamPredictions(request);
  534. //将结果记录到问答表
  535. while (predictions.hasNext()) {
  536. String responseStr = predictions.next().getPrediction().toStringUtf8();
  537. log.info("大模型问答返回的原始结果为{}", responseStr);
  538. responseStr = JSON.parseObject(responseStr).getString("message");
  539. if("complete".equals(responseStr)){
  540. log.info("结尾语句并且是非JSON,无需处理");
  541. }else{
  542. sb.append(responseStr);
  543. }
  544. }
  545. //将问答更新到数据库中
  546. chatReq.setSessionId(sessionId);
  547. chatReq.setType(1);//0问答 1决策 2本地 3仿真预测
  548. chatReq.setModule(3);//0专家问答 1智能工单 2智能体助手 3告警 4简报
  549. String showVal = this.buildShowValue(xinyiWarningRecord, tXinyiIndustry, normConfig);
  550. chatReq.setShowVal(showVal);
  551. chatReq.setQuestion(shWarningPrompt);
  552. chatReq.setAnswer(sb.toString());
  553. chatReq.setWarningId(String.valueOf(xinyiWarningRecord.getId()));
  554. chatReq.setCounts(counts);//问答次数
  555. chatReq.setUserId(WARNING_DEFAULT_CREATE);
  556. chatReq.setCreateBy(WARNING_DEFAULT_CREATE);
  557. chatReq.setCreateTime(DateUtils.getNowDate());
  558. this.xinyiChatRecordMapper.insertTXinyiChatRecord(chatReq);
  559. } catch (IOException e) {
  560. throw new RuntimeException(e);
  561. } finally {
  562. // 关闭输出流
  563. channel.shutdown();
  564. }
  565. }
  566. private void handleSZWarning(TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
  567. //出水相关
  568. //出水COD报警
  569. BigDecimal csCod = tXinyiIndustry.getCsCod();
  570. BigDecimal cscodBzz = normConfig.getCscodBzz();
  571. BigDecimal cscodGkz = normConfig.getCscodGkz();
  572. if(!Objects.isNull(cscodBzz)){
  573. handleXinYiWarningsCs(cscodBzz, csCod, cscodGkz, BusinessEnum.WarningCategoryEnum.CS_COD.getCode(), tXinyiIndustry, normConfig);
  574. }
  575. //出水总磷超标报警
  576. BigDecimal csTp = tXinyiIndustry.getCsTp();
  577. BigDecimal cszlBzz = normConfig.getCszlBzz();
  578. BigDecimal cszlGkz = normConfig.getCszlGkz();
  579. if(!Objects.isNull(cszlBzz)){
  580. handleXinYiWarningsCs(cszlBzz, csTp, cszlGkz, BusinessEnum.WarningCategoryEnum.CS_ZL.getCode(), tXinyiIndustry, normConfig);
  581. }
  582. //出水总氮超标报警
  583. BigDecimal csTn = tXinyiIndustry.getCsTn();
  584. BigDecimal cszzBzz = normConfig.getCszzBzz();
  585. BigDecimal cszzGkz = normConfig.getCszzGkz();
  586. if(!Objects.isNull(cszzBzz)){
  587. handleXinYiWarningsCs(cszzBzz, csTn, cszzGkz, BusinessEnum.WarningCategoryEnum.CS_ZD.getCode(), tXinyiIndustry, normConfig);
  588. }
  589. //出水氨氮超标报警
  590. BigDecimal csNh3 = tXinyiIndustry.getCsNh3();
  591. BigDecimal csadBzz = normConfig.getCsadBzz();
  592. BigDecimal csadGkz = normConfig.getCsadGkz();
  593. if(!Objects.isNull(csadBzz)){
  594. handleXinYiWarningsCs(csadBzz, csNh3, csadGkz, BusinessEnum.WarningCategoryEnum.CS_AD.getCode(), tXinyiIndustry, normConfig);
  595. }
  596. //出水SS超标报警
  597. BigDecimal csSS = tXinyiIndustry.getCsSs();
  598. BigDecimal csSSBzz = normConfig.getCsssBzz();
  599. BigDecimal csssGkz = normConfig.getCsssGkz();
  600. if(!Objects.isNull(csSSBzz)){
  601. handleXinYiWarningsCs(csSSBzz, csSS, csssGkz, BusinessEnum.WarningCategoryEnum.CS_SS.getCode(), tXinyiIndustry, normConfig);
  602. }
  603. //进水相关报警
  604. //进水总磷超标报警
  605. BigDecimal jsTp = tXinyiIndustry.getJsTp();
  606. BigDecimal jszlSjz = normConfig.getJszlSjz();
  607. if(!Objects.isNull(jszlSjz)){
  608. handleXinYiWarningRecordJS(jszlSjz, jsTp, BusinessEnum.WarningCategoryEnum.JS_ZL.getCode(), tXinyiIndustry, normConfig);
  609. }
  610. //进水COD超标报警
  611. BigDecimal jsCod = tXinyiIndustry.getJsCod();
  612. BigDecimal jscodSjz = normConfig.getJscodSjz();
  613. if(!Objects.isNull(jscodSjz)){
  614. handleXinYiWarningRecordJS(jscodSjz, jsCod, BusinessEnum.WarningCategoryEnum.JS_COD.getCode(), tXinyiIndustry, normConfig);
  615. }
  616. //进水总氮超标报警
  617. BigDecimal jsTn = tXinyiIndustry.getJsTn();
  618. BigDecimal jszdSjz = normConfig.getJszdSjz();
  619. if(!Objects.isNull(jszdSjz)){
  620. handleXinYiWarningRecordJS(jszdSjz, jsTn, BusinessEnum.WarningCategoryEnum.JS_ZD.getCode(), tXinyiIndustry, normConfig);
  621. }
  622. //进水氨氮超标报警
  623. BigDecimal jsNh3 = tXinyiIndustry.getJsNh3();
  624. BigDecimal jsadSjz = normConfig.getJsadSjz();
  625. if(!Objects.isNull(jsadSjz)){
  626. handleXinYiWarningRecordJS(jsadSjz, jsNh3, BusinessEnum.WarningCategoryEnum.JS_AD.getCode(), tXinyiIndustry, normConfig);
  627. }
  628. //进水SS超标报警
  629. BigDecimal jsSS = tXinyiIndustry.getJsSs();
  630. BigDecimal jsSSSjz = normConfig.getJsssSjz();
  631. if(!Objects.isNull(jsSSSjz)){
  632. handleXinYiWarningRecordJS(jsSSSjz, jsSS, BusinessEnum.WarningCategoryEnum.JS_SS.getCode(), tXinyiIndustry, normConfig);
  633. }
  634. }
  635. /**
  636. * 调用大模型获取决策结果 并同时记录对应信息到聊天记录表中
  637. *
  638. * @param tXinyiWarningRecord
  639. * @param tXinyiIndustry
  640. * @param normConfig
  641. */
  642. private void handleDecision(TXinyiWarningRecord tXinyiWarningRecord, TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
  643. log.info("进入了调⽤大模型决策接口");
  644. ChatReq chatReq = new ChatReq();
  645. // StringBuilder sb = new StringBuilder();
  646. //大模型结果 放入一个结合中
  647. List<String> resultData = new ArrayList<>();
  648. //决策和问答不一样 没有历史的概念 所以sessionId都是新的 次数都是1
  649. String sessionId = IdUtils.simpleUUID();
  650. String feedback = chatReq.getFeedback();
  651. //决策请求的业务参数
  652. // List<HashMap<String, Object>> list = this.xinyiIndustryMapper.selectLast10RecordsForDecision();
  653. //2024年5月21日15:23:07 这里不能用关联查询处理,日报要获取最新的一条而不是今日的数据。
  654. List<DecisionReq> decisionReqs = this.xinyiIndustryMapper.selectLast10RecordsForDecisionOnlyIndustry();
  655. if(!CollectionUtils.isEmpty(decisionReqs)){
  656. for (DecisionReq decisionReq : decisionReqs) {
  657. //处理日报数据
  658. TXinyiDaily daily = this.xinyiDailyMapper.selectNewestData();
  659. if(!Objects.isNull(daily)){
  660. decisionReq.setT(daily.getJsSw());
  661. decisionReq.setSVI(daily.getWntjzsAll());
  662. decisionReq.setSV(daily.getWncjbAll());
  663. decisionReq.setMlvss(daily.getHfxwnndAll());
  664. decisionReq.setDoAna(daily.getYycRjyAll());
  665. decisionReq.setDoQue(daily.getQycRjyAll());
  666. //2024年5月26日11:59:02 干污泥量数据做了同步
  667. decisionReq.setGwnl(daily.getGWNL());
  668. BigDecimal jsBod5 = daily.getJsBod5();
  669. decisionReq.setBodIn(jsBod5);
  670. BigDecimal tpIn = decisionReq.getTpIn();
  671. BigDecimal tnIn = decisionReq.getTnIn();
  672. if(!Objects.isNull(jsBod5)){
  673. if(!Objects.isNull(tpIn) && tpIn.compareTo(new BigDecimal("0")) >0){
  674. decisionReq.setC(jsBod5.divide(tpIn, 4, RoundingMode.HALF_UP));
  675. }
  676. if(!Objects.isNull(tnIn) && tnIn.compareTo(new BigDecimal("0")) >0){
  677. decisionReq.setB(jsBod5.divide(tnIn, 4, RoundingMode.HALF_UP));
  678. }
  679. }
  680. }
  681. }
  682. }
  683. String rows = JSON.toJSONString(decisionReqs, JSONWriter.Feature.WriteNulls);
  684. // 获取输出流
  685. ManagedChannel channel = null;
  686. String dataJson = "";
  687. try {
  688. channel = ManagedChannelBuilder.forAddress("10.0.0.24", 17070)
  689. .usePlaintext()
  690. .build();
  691. InferenceAPIsServiceGrpc.InferenceAPIsServiceBlockingStub stub = InferenceAPIsServiceGrpc.newBlockingStub(channel);
  692. 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 + "}}";
  693. log.info("请求大模型的决策的参数为{}", dataJson);
  694. PredictionsRequest request = PredictionsRequest.newBuilder()
  695. .setModelName("slibra_bot")
  696. .putInput("method", ByteString.copyFrom("decision_stream", "utf-8"))//推理
  697. .putInput("data", ByteString.copyFrom(dataJson, "utf-8"))
  698. .buildPartial();
  699. Iterator<PredictionResponse> predictions = stub.streamPredictions(request);
  700. while (predictions.hasNext()) {
  701. String responseStr = predictions.next().getPrediction().toStringUtf8();
  702. log.info("决策流式返回的结果是{}", responseStr);
  703. //2024年5月25日16:37:16 按照大模型返回的类型解析数据
  704. String biz = JSON.parseObject(responseStr).getString("biz");
  705. if(BusinessEnum.BigModelBizEnum.OK.getCode().equals(biz)){
  706. log.info("结尾语句并且是非JSON,无需处理");
  707. //结束语句也流式输出,但是并不记录下来 2024年5月24日11:15:23 也不返回前端
  708. /*outputStream.write(responseStr.getBytes());
  709. outputStream.flush();*/
  710. }else if(BusinessEnum.BigModelBizEnum.DECISION_DEBUGGER.getCode().equals(biz)){
  711. log.info("中间过程,目前只打印日志,不记录数据,也不返回给前端,返回数据为{}", responseStr);
  712. //结束语句也流式输出,但是并不记录下来 2024年5月24日11:15:23 也不返回前端
  713. /*outputStream.write(responseStr.getBytes());
  714. outputStream.flush();*/
  715. }else{//其他 要么错误 要么alert 要么出的报告
  716. // sb.append(responseStr);
  717. resultData.add(responseStr);
  718. }
  719. }
  720. } catch (Exception e) {
  721. // throw new RuntimeException(e);
  722. log.error("定时任务处理告警调用决策异常,异常信息为{}", JSON.toJSONString(e));
  723. resultData.add("{\"biz\":\"ERROR\",\"message\":\"大模型分析数据异常,请稍后再试\"}");
  724. } finally {
  725. log.info("决策最终要保存的数据是{}", JSON.toJSONString(resultData));
  726. //保存聊天记录
  727. //将问答更新到数据库中
  728. chatReq.setSessionId(sessionId);
  729. chatReq.setType(1);//0问答 1决策
  730. chatReq.setModule(3);
  731. /*String userId = SecurityUtils.getUserId().toString();
  732. String username = SecurityUtils.getUsername();*/
  733. chatReq.setUserId(WARNING_DEFAULT_CREATE);
  734. String showVal = this.buildShowValue(tXinyiWarningRecord, tXinyiIndustry, normConfig);
  735. chatReq.setShowVal(showVal);//前端展示的数据和提问的数据不一致
  736. chatReq.setQuestion(dataJson);
  737. chatReq.setAnswer(JSON.toJSONString(resultData));
  738. chatReq.setWarningId(String.valueOf(tXinyiWarningRecord.getId()));
  739. chatReq.setCounts(1);//问答次数
  740. chatReq.setCreateBy(WARNING_DEFAULT_CREATE);
  741. chatReq.setCreateTime(DateUtils.getNowDate());
  742. this.xinyiChatRecordMapper.insertTXinyiChatRecord(chatReq);
  743. // 关闭输出流
  744. if(!Objects.isNull(channel))
  745. channel.shutdown();
  746. }
  747. }
  748. private String buildShowValue(TXinyiWarningRecord tXinyiWarningRecord, TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
  749. JSONObject result = new JSONObject();
  750. JSONObject basic = new JSONObject();
  751. Integer status = tXinyiWarningRecord.getStatus();
  752. Date warningTime = tXinyiWarningRecord.getTime();
  753. int count = DateUtils.differentHoursByMillisecond(warningTime, DateUtils.getNowDate()) + 1;
  754. basic.put("title", tXinyiWarningRecord.getReason());
  755. basic.put("报警时间", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM ,warningTime));
  756. basic.put("报警值", tXinyiWarningRecord.getWarningVal());
  757. basic.put("标准值", tXinyiWarningRecord.getDesignVal());
  758. basic.put("管控值", tXinyiWarningRecord.getControlVal());
  759. basic.put("报警次数", Math.min(count, MAX_COUNT));
  760. if(tXinyiWarningRecord.getType() != 2)
  761. basic.put("状态", status == 0 ? "报警中" : status == 1 ? "用户关闭" : status == 2 ? "系统关闭" : "应急处理中");
  762. else
  763. basic.put("状态", status == 0 ? "预警中" : "已完成");
  764. //2024年5月27日14:04:22 额外返回2个字段 [管控值 和 告警级别] 返回的json没有数据是因为value没有值
  765. // basic.put("管控值", tXinyiWarningRecord.getControlVal());
  766. basic.put("告警级别", tXinyiWarningRecord.getLevel());
  767. result.put("basic", basic);
  768. JSONObject jsData = getJsonObject(tXinyiIndustry, normConfig);//进水数据
  769. result.put("jsData", jsData);
  770. JSONObject csData = getCsonObject(tXinyiIndustry, normConfig);//出水数据
  771. result.put("csData", csData);
  772. return JSON.toJSONString(result, JSONWriter.Feature.WriteNulls);
  773. }
  774. /**
  775. * 带图形的构建展示数据
  776. * @param tXinyiWarningRecord
  777. * @param tXinyiIndustry
  778. * @param normConfig
  779. * @return
  780. */
  781. private String buildShowValueWithCharts(TXinyiWarningRecord tXinyiWarningRecord, TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig, List<ChartBasic> chartsDataList) {
  782. JSONObject result = new JSONObject();
  783. // JSONObject basic = new JSONObject();
  784. Integer status = tXinyiWarningRecord.getStatus();
  785. Date warningTime = tXinyiWarningRecord.getTime();
  786. String remark = tXinyiWarningRecord.getRemark();
  787. int count = DateUtils.differentHoursByMillisecond(warningTime, DateUtils.getNowDate()) + 1;
  788. ShowValueBasic showValueBasic = new ShowValueBasic();
  789. if("0".equals(remark)){//水质报警
  790. showValueBasic.setH("在线仪表");
  791. showValueBasic.setF(tXinyiWarningRecord.getLevel());
  792. }else if("1".equals(remark)){//生化报警
  793. showValueBasic.setH("在线仪表");
  794. }else if("2".equals(remark)){//预测报错
  795. showValueBasic.setH("预测");
  796. showValueBasic.setF(tXinyiWarningRecord.getLevel());
  797. }else {//机器人化验室报警
  798. showValueBasic.setH("化验室");
  799. showValueBasic.setF(tXinyiWarningRecord.getLevel());
  800. }
  801. //通用的
  802. // showValueBasic.setA(tXinyiWarningRecord.getReason());
  803. showValueBasic.setB(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM ,warningTime));
  804. showValueBasic.setC(tXinyiWarningRecord.getWarningVal());
  805. showValueBasic.setD(tXinyiWarningRecord.getControlVal());
  806. showValueBasic.setE(tXinyiWarningRecord.getDesignVal());
  807. showValueBasic.setG(Math.min(count, MAX_COUNT));
  808. if(tXinyiWarningRecord.getType() != 2)
  809. showValueBasic.setI(status == 0 ? "报警中" : status == 1 ? "用户关闭" : status == 2 ? "系统关闭" : "应急处理中");
  810. else
  811. showValueBasic.setI(status == 0 ? "预警中" : "已完成");
  812. result.put("basic", showValueBasic);
  813. JSONObject jsData = getJsonObject(tXinyiIndustry, normConfig);//进水数据
  814. result.put("jsData", jsData);
  815. JSONObject csData = getCsonObject(tXinyiIndustry, normConfig);//出水数据
  816. result.put("csData", csData);
  817. // JSONObject chartsData = getChartsCsonObject(tXinyiIndustry, normConfig);//图表数据 因为展示不同,所以要传过来或者判断
  818. result.put("chartsData", chartsDataList);
  819. return JSON.toJSONString(result, JSONWriter.Feature.WriteNulls);
  820. }
  821. @Deprecated
  822. private JSONObject getChartsCsonObject(TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
  823. JSONObject jsData = new JSONObject();
  824. //处理图表数据
  825. return jsData;
  826. }
  827. private static JSONObject getJsonObject(TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
  828. JSONObject jsData = new JSONObject();
  829. HashMap<Object, Object> temp1 = new HashMap<>();
  830. BigDecimal jsSlq = tXinyiIndustry.getJsSlq();
  831. temp1.put("value", jsSlq);
  832. temp1.put("exceed", jsSlq.compareTo(normConfig.getJsslSjz()) >0);
  833. jsData.put("流量", temp1);
  834. HashMap<Object, Object> temp2 = new HashMap<>();
  835. BigDecimal jsCod = tXinyiIndustry.getJsCod();
  836. temp2.put("value", jsCod);
  837. temp2.put("exceed", jsCod.compareTo(normConfig.getJscodSjz()) > 0);
  838. jsData.put("COD", temp2);
  839. HashMap<Object, Object> temp3 = new HashMap<>();
  840. BigDecimal jsNh3 = tXinyiIndustry.getJsNh3();
  841. temp3.put("value", jsNh3);
  842. temp3.put("exceed", jsNh3.compareTo(normConfig.getJsadSjz()) > 0);
  843. jsData.put("NH3-N", temp3);
  844. HashMap<Object, Object> temp4 = new HashMap<>();
  845. BigDecimal jsTp = tXinyiIndustry.getJsTp();
  846. temp4.put("value", jsTp);
  847. temp4.put("exceed", jsTp.compareTo(normConfig.getJszlSjz()) > 0);
  848. jsData.put("TP", temp4);
  849. HashMap<Object, Object> temp5 = new HashMap<>();
  850. BigDecimal jsSs = tXinyiIndustry.getJsSs();
  851. temp5.put("value", jsSs);
  852. temp5.put("exceed", jsSs.compareTo(normConfig.getJsssSjz()) > 0);
  853. jsData.put("SS", temp5);
  854. HashMap<Object, Object> temp6 = new HashMap<>();
  855. BigDecimal jsTn = tXinyiIndustry.getJsTn();
  856. temp6.put("value", jsTn);
  857. temp6.put("exceed", jsTn.compareTo(normConfig.getJszdSjz()) > 0);
  858. jsData.put("TN", temp6);
  859. return jsData;
  860. }
  861. private static JSONObject getCsonObject(TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
  862. JSONObject csData = new JSONObject();
  863. HashMap<Object, Object> temp1 = new HashMap<>();
  864. BigDecimal csSlq = tXinyiIndustry.getCsSlqc();
  865. temp1.put("value", csSlq);
  866. temp1.put("exceed", false);//出水水量没有管控值
  867. csData.put("流量", temp1);
  868. HashMap<Object, Object> temp2 = new HashMap<>();
  869. BigDecimal csCod = tXinyiIndustry.getCsCod();
  870. temp2.put("value", csCod);
  871. temp2.put("exceed", csCod.compareTo(normConfig.getCscodGkz()) > 0);
  872. csData.put("COD", temp2);
  873. HashMap<Object, Object> temp3 = new HashMap<>();
  874. BigDecimal csNh3 = tXinyiIndustry.getCsNh3();
  875. temp3.put("value", csNh3);
  876. temp3.put("exceed", csNh3.compareTo(normConfig.getCsadGkz()) > 0);
  877. csData.put("NH3-N", temp3);
  878. HashMap<Object, Object> temp4 = new HashMap<>();
  879. BigDecimal csTp = tXinyiIndustry.getCsTp();
  880. temp4.put("value", csTp);
  881. temp4.put("exceed", csTp.compareTo(normConfig.getCszlGkz()) > 0);
  882. csData.put("TP", temp4);
  883. HashMap<Object, Object> temp5 = new HashMap<>();
  884. BigDecimal csSs = tXinyiIndustry.getCsSs();
  885. temp5.put("value", csSs);
  886. temp5.put("exceed", csSs.compareTo(normConfig.getCsssGkz()) > 0);
  887. csData.put("SS", temp5);
  888. HashMap<Object, Object> temp6 = new HashMap<>();
  889. BigDecimal csTn = tXinyiIndustry.getCsTn();
  890. temp6.put("value", csTn);
  891. temp6.put("exceed", csTn.compareTo(normConfig.getCszzGkz()) > 0);
  892. csData.put("TN", temp6);
  893. return csData;
  894. }
  895. /**
  896. * 通过输入的值 生成对应类型的报警对象(出水)
  897. *
  898. * @param csBzz
  899. * @param currentVal
  900. * @param csGkz
  901. * @param category
  902. * @param tXinyiIndustry
  903. * @param normConfig
  904. * @return
  905. */
  906. private void handleXinYiWarningsCs(BigDecimal csBzz, BigDecimal currentVal, BigDecimal csGkz, String category, TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
  907. BigDecimal multiply = csBzz.multiply(new BigDecimal(MyConstants.SCALE_VALUE));
  908. TXinyiWarningRecord tXinyiWarningRecord = new TXinyiWarningRecord();
  909. /*String category = BusinessEnum.WarningCategoryEnum.CS_AD.getCode();*/
  910. tXinyiWarningRecord.setStatus(0);
  911. tXinyiWarningRecord.setType(0);
  912. tXinyiWarningRecord.setCategory(category);
  913. tXinyiWarningRecord.setTime(DateUtils.getNowDate());
  914. tXinyiWarningRecord.setWarningVal(currentVal);
  915. tXinyiWarningRecord.setDesignVal(csBzz);
  916. tXinyiWarningRecord.setControlVal(csGkz);
  917. tXinyiWarningRecord.setCreateBy(WARNING_DEFAULT_CREATE);
  918. tXinyiWarningRecord.setCreateTime(DateUtils.getNowDate());
  919. //2024年5月25日17:52:33 如果工业库获取不到数据,也触发报警,但是不调用决策接口
  920. if(Objects.isNull(currentVal)){
  921. tXinyiWarningRecord.setReason(category + EXCEPTION_WARNING);
  922. tXinyiWarningRecord.setLevel(WARNING_LEVEL_NO_DATE);
  923. }else if(currentVal.compareTo(multiply) > 0){//一级
  924. tXinyiWarningRecord.setReason(category + CHAOBIAO_WARNING);
  925. tXinyiWarningRecord.setLevel(WARNING_LEVEL_ONE);
  926. }else if(currentVal.compareTo(csBzz) >= 0 && currentVal.compareTo(multiply) <= 0){//二级
  927. tXinyiWarningRecord.setReason(category + CHAOBIAO_WARNING);
  928. tXinyiWarningRecord.setLevel(WARNING_LEVEL_TWO);
  929. }else if(!Objects.isNull(csGkz) && currentVal.compareTo(csGkz) > 0){
  930. tXinyiWarningRecord.setReason(category + CHAOGUANKONG_WARNING);
  931. tXinyiWarningRecord.setLevel(WARNING_LEVEL_THREE);
  932. }else{
  933. tXinyiWarningRecord = null;//这种的无需处理
  934. }
  935. //当前状态正常 需要查询历史有无正在报警的数据,如果有,将报警状态改完2(系统自动关闭)
  936. List<TXinyiWarningRecord> tXinyiWarningRecords = this.xinyiWarningRecordMapper.selectTXinyiWarningRecordList(TXinyiWarningRecord.builder().delFlag(0).type(0).category(category).warningStatus(0).build());
  937. if(Objects.isNull(tXinyiWarningRecord)){//数据正常,无告警信息
  938. if(!CollectionUtils.isEmpty(tXinyiWarningRecords)){
  939. log.info( "{}:现在恢复正常,历史报警数据为{}", category,JSON.toJSONString(tXinyiWarningRecords));
  940. for (TXinyiWarningRecord xinyiWarningRecord : tXinyiWarningRecords) {
  941. xinyiWarningRecord.setStatus(2);
  942. Date nowDate = DateUtils.getNowDate();
  943. xinyiWarningRecord.setOffTime(nowDate);
  944. xinyiWarningRecord.setUpdateTime(nowDate);
  945. xinyiWarningRecord.setUpdateBy(WARNING_DEFAULT_CREATE);
  946. this.xinyiWarningRecordMapper.updateTXinyiWarningRecord(xinyiWarningRecord);
  947. }
  948. }
  949. }else{//有告警信息
  950. if(CollectionUtils.isEmpty(tXinyiWarningRecords)){//之前没有告警记录
  951. //保存到数据库中
  952. this.xinyiWarningRecordMapper.insertTXinyiWarningRecord(tXinyiWarningRecord);
  953. if(WARNING_LEVEL_NO_DATE.equals(tXinyiWarningRecord.getLevel())){
  954. //只保存一个普通的问答记录 不需要调用决策信息,但是实时数据还是要记录的
  955. this.addChatRecordByWarning(tXinyiWarningRecord, tXinyiIndustry, normConfig);
  956. }else {
  957. //继续调用决策
  958. this.handleDecision(tXinyiWarningRecord, tXinyiIndustry, normConfig);
  959. }
  960. }else{
  961. log.info("{}:之前已经有过告警记录了,且还是继续报警,无需重复添加报警,但是决策仍然要调用", category);
  962. for (TXinyiWarningRecord xinyiWarningRecord : tXinyiWarningRecords) {//理论上只有一个的
  963. if(WARNING_LEVEL_NO_DATE.equals(tXinyiWarningRecord.getLevel())){
  964. //只保存一个普通的问答记录 不需要调用决策信息,但是实时数据还是要记录的
  965. this.addChatRecordByWarning(tXinyiWarningRecord, tXinyiIndustry, normConfig);
  966. }else {
  967. //继续调用决策
  968. this.handleDecision(xinyiWarningRecord, tXinyiIndustry, normConfig);
  969. }
  970. }
  971. }
  972. }
  973. }
  974. /**
  975. * 通过输入的值 生成对应类型的报警对象(进水)
  976. *
  977. * @param jsBzz
  978. * @param currentVal
  979. * @param category
  980. * @param tXinyiIndustry
  981. * @param normConfig
  982. * @return
  983. */
  984. private void handleXinYiWarningRecordJS(BigDecimal jsBzz, BigDecimal currentVal, String category, TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
  985. BigDecimal multiply = jsBzz.multiply(new BigDecimal(MyConstants.SCALE_VALUE));
  986. TXinyiWarningRecord tXinyiWarningRecord = new TXinyiWarningRecord();
  987. /*String category = BusinessEnum.WarningCategoryEnum.CS_AD.getCode();*/
  988. tXinyiWarningRecord.setStatus(0);
  989. tXinyiWarningRecord.setType(0);
  990. tXinyiWarningRecord.setCategory(category);
  991. tXinyiWarningRecord.setTime(DateUtils.getNowDate());
  992. tXinyiWarningRecord.setWarningVal(currentVal);
  993. tXinyiWarningRecord.setDesignVal(jsBzz);
  994. // tXinyiWarningRecord.setControlVal(csGkz);
  995. tXinyiWarningRecord.setCreateBy(WARNING_DEFAULT_CREATE);
  996. tXinyiWarningRecord.setCreateTime(DateUtils.getNowDate());
  997. //2024年5月25日17:52:33 如果工业库获取不到数据,也触发报警,但是不调用决策接口
  998. if(Objects.isNull(currentVal)){
  999. tXinyiWarningRecord.setReason(category + EXCEPTION_WARNING);
  1000. tXinyiWarningRecord.setLevel(WARNING_LEVEL_NO_DATE);
  1001. }else if(currentVal.compareTo(multiply) > 0){//一级
  1002. tXinyiWarningRecord.setReason(category + CHAOBIAO_WARNING);
  1003. tXinyiWarningRecord.setLevel(WARNING_LEVEL_ONE);
  1004. }else if(currentVal.compareTo(jsBzz) >= 0 && currentVal.compareTo(multiply) <= 0){//二级
  1005. tXinyiWarningRecord.setReason(category + CHAOBIAO_WARNING);
  1006. tXinyiWarningRecord.setLevel(WARNING_LEVEL_TWO);
  1007. }else{
  1008. tXinyiWarningRecord = null;//这种的无需处理
  1009. }
  1010. /*else if(!Objects.isNull(csGkz) && currentVal.compareTo(csGkz) > 0){
  1011. tXinyiWarningRecord.setReason(category + CHAOGUANKONG_WARNING);
  1012. tXinyiWarningRecord.setLevel(WARNING_LEVEL_THREE);
  1013. }*/
  1014. //当前状态正常 需要查询历史有无正在报警的数据,如果有,将报警状态改完2(系统自动关闭)
  1015. List<TXinyiWarningRecord> tXinyiWarningRecords = this.xinyiWarningRecordMapper.selectTXinyiWarningRecordList(TXinyiWarningRecord.builder().delFlag(0).type(0).category(category).status(0).build());
  1016. if(Objects.isNull(tXinyiWarningRecord)){//数据正常,无告警信息
  1017. if(!CollectionUtils.isEmpty(tXinyiWarningRecords)){
  1018. log.info( "{}:现在恢复正常,历史报警数据为{}", category,JSON.toJSONString(tXinyiWarningRecords));
  1019. for (TXinyiWarningRecord xinyiWarningRecord : tXinyiWarningRecords) {
  1020. xinyiWarningRecord.setStatus(2);
  1021. Date nowDate = DateUtils.getNowDate();
  1022. xinyiWarningRecord.setOffTime(nowDate);
  1023. xinyiWarningRecord.setUpdateTime(nowDate);
  1024. xinyiWarningRecord.setUpdateBy(WARNING_DEFAULT_CREATE);
  1025. this.xinyiWarningRecordMapper.updateTXinyiWarningRecord(xinyiWarningRecord);
  1026. }
  1027. }
  1028. }else{//有告警信息
  1029. if(CollectionUtils.isEmpty(tXinyiWarningRecords)){//之前没有告警记录
  1030. //保存到数据库中
  1031. this.xinyiWarningRecordMapper.insertTXinyiWarningRecord(tXinyiWarningRecord);
  1032. if(WARNING_LEVEL_NO_DATE.equals(tXinyiWarningRecord.getLevel())){
  1033. //只保存一个普通的问答记录 不需要调用决策信息,但是实时数据还是要记录的
  1034. this.addChatRecordByWarning(tXinyiWarningRecord, tXinyiIndustry, normConfig);
  1035. }else {
  1036. //继续调用决策
  1037. this.handleDecision(tXinyiWarningRecord, tXinyiIndustry, normConfig);
  1038. }
  1039. }else{
  1040. log.info("{}:之前已经有过告警记录了,且还是继续报警,无需重复添加报警,但是决策仍然要调用", category);
  1041. for (TXinyiWarningRecord xinyiWarningRecord : tXinyiWarningRecords) {//理论上只有一个的
  1042. if(WARNING_LEVEL_NO_DATE.equals(tXinyiWarningRecord.getLevel())){
  1043. //只保存一个普通的问答记录 不需要调用决策信息,但是实时数据还是要记录的
  1044. this.addChatRecordByWarning(tXinyiWarningRecord, tXinyiIndustry, normConfig);
  1045. }else {
  1046. //继续调用决策
  1047. this.handleDecision(xinyiWarningRecord, tXinyiIndustry, normConfig);
  1048. }
  1049. }
  1050. }
  1051. }
  1052. }
  1053. private void addChatRecordByWarning(TXinyiWarningRecord tXinyiWarningRecord, TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
  1054. ChatReq chatReq = new ChatReq();
  1055. //保存聊天记录
  1056. //将问答更新到数据库中
  1057. chatReq.setSessionId(IdUtils.simpleUUID());
  1058. chatReq.setType(1);//0问答 1决策 2本地 3仿真预测
  1059. chatReq.setModule(3);
  1060. /*String userId = SecurityUtils.getUserId().toString();
  1061. String username = SecurityUtils.getUsername();*/
  1062. chatReq.setUserId(WARNING_DEFAULT_CREATE);
  1063. String showVal = this.buildShowValue(tXinyiWarningRecord, tXinyiIndustry, normConfig);
  1064. chatReq.setShowVal(showVal);//前端展示的数据和提问的数据不一致
  1065. chatReq.setQuestion(WARNING_DEFAULT_QUESTION);//本地问题
  1066. chatReq.setAnswer(tXinyiWarningRecord.getReason() + ",请检查设备是否正常运行");
  1067. chatReq.setWarningId(String.valueOf(tXinyiWarningRecord.getId()));
  1068. chatReq.setCounts(1);//问答次数
  1069. chatReq.setCreateBy(WARNING_DEFAULT_CREATE);
  1070. chatReq.setCreateTime(DateUtils.getNowDate());
  1071. this.xinyiChatRecordMapper.insertTXinyiChatRecord(chatReq);
  1072. }
  1073. /**
  1074. * 定时从sqlserver获取数据
  1075. */
  1076. public void sqlserverData(){
  1077. log.info("进入了定时同步SqlServer的任务");
  1078. //主库获取上次最新的同步日期
  1079. String lastTime = this.xinyiRobotMapper.selectLastTime();
  1080. log.info("上次同步的日期是{}", lastTime);
  1081. //从
  1082. DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
  1083. List<TXinyiRobot> tXinyiRobots = xinyiRobotMapper.selectTXinyiRobotListByTime(lastTime);
  1084. DynamicDataSourceContextHolder.clearDataSourceType();
  1085. // System.out.println(JSON.toJSONString(tXinyiRobots));
  1086. // System.out.println("-------------");
  1087. //主
  1088. if(!CollectionUtils.isEmpty(tXinyiRobots)){
  1089. for (TXinyiRobot tXinyiRobot : tXinyiRobots) {
  1090. String date = handleDate(tXinyiRobot.getVDate().replaceAll(" ", ""));//有空格
  1091. String time = handleDate(tXinyiRobot.getVTime().replaceAll(" ", ""));//有空格
  1092. tXinyiRobot.setVDate(date);
  1093. tXinyiRobot.setVTime(time);
  1094. tXinyiRobot.setVDateTime(date + " " + time);
  1095. //处理给前端展示的字段
  1096. tXinyiRobot.setTestDate(date);//日期
  1097. tXinyiRobot.setTestHour(date + " " + time.substring(0, 2));//小时
  1098. tXinyiRobot.setTestTime(date + " " + time.substring(0, 5));//分钟
  1099. tXinyiRobot.setCreatedTime(new Date());
  1100. //2024年5月29日10:33:32 额外处理几个新增的字段 多个池子数据合并一个
  1101. List<BigDecimal> extraList = new ArrayList<>();
  1102. extraList.add(tXinyiRobot.getNo3Hlj1Jqr());
  1103. extraList.add(tXinyiRobot.getNo3Hlj2Jqr());
  1104. tXinyiRobot.setHycxsyAll(JSON.toJSONString(extraList));
  1105. extraList.clear();
  1106. extraList.add(tXinyiRobot.getNh31Jqr());
  1107. extraList.add(tXinyiRobot.getNh32Jqr());
  1108. tXinyiRobot.setQyanAll(JSON.toJSONString(extraList));
  1109. extraList.clear();
  1110. extraList.add(tXinyiRobot.getNo3Qyc1Jqr());
  1111. extraList.add(tXinyiRobot.getNo3Qyc2Jqr());
  1112. tXinyiRobot.setQyckxsyAll(JSON.toJSONString(extraList));
  1113. extraList.clear();
  1114. extraList.add(tXinyiRobot.getTpHl1Jqr());
  1115. extraList.add(tXinyiRobot.getTpHl2Jqr());
  1116. tXinyiRobot.setHyzlsyAll(JSON.toJSONString(extraList));
  1117. this.xinyiRobotMapper.insertTXinyiRobot(tXinyiRobot);
  1118. }
  1119. }
  1120. }
  1121. /**
  1122. *
  1123. * 定时生成每日简报数据
  1124. *
  1125. */
  1126. public void generageShortReport(){
  1127. log.info("进入了定时生成每日简报数据");
  1128. List<TXinyiDaily> dailyTwoRecords = this.xinyiDailyMapper.selectNRecords(2);
  1129. //正常不会有这种问题 因为日报有很多条
  1130. if(CollectionUtils.isEmpty(dailyTwoRecords) || dailyTwoRecords.size() < 2){
  1131. log.error("进入了定时生成每日简报数据 获取最新的2条数据不足,终止");
  1132. return;
  1133. }
  1134. //暂时不考虑因为没有填写日报 导致生成重复数据的问题(后续需要的话再添加)
  1135. //处理数据 并 拼装
  1136. String queryData = buildShortReportQueryData(dailyTwoRecords);
  1137. log.info("定时生成简报,组装好的请求大模型的参数为:{}", queryData);
  1138. if(StringUtils.isBlank(queryData)){
  1139. log.error("无法拼装请求数据!!!!!!");
  1140. return;
  1141. }
  1142. String showVal = formateDateStr(dailyTwoRecords.get(0).getTestDate()) + JIAN_BAO_END;
  1143. //调用模型 并保存结果
  1144. this.askBigModel(queryData, showVal);
  1145. log.info("定时生成简报任务结束~~~~~~~~~~~~~~");
  1146. }
  1147. /**
  1148. *
  1149. * 每小时发一一次预测
  1150. *
  1151. */
  1152. public void predictor(){
  1153. //获取最新的6条工业科数据
  1154. List<TXinyiIndustry> tXinyiIndustries6 = this.xinyiIndustryMapper.selectNIndustry(6);
  1155. List<TXinyiRobot> tXinyiRobots6 = this.xinyiRobotMapper.selectNRobot(6);
  1156. for (int i = 0; i < predictorArr.length; i++) {
  1157. String result = testPredictor(predictorArr[i]);
  1158. JSONObject jsonObject = JSON.parseObject(result);
  1159. String task = jsonObject.getString("task");
  1160. String hour = jsonObject.getString("hour");
  1161. String pred = jsonObject.getString("pred");
  1162. if(StringUtils.isNotBlank(pred) && pred.contains(",")){
  1163. String[] split = pred.split(",");
  1164. if(split.length != 3){
  1165. log.error("预测数据返回结果为{},长度不是3,无法正常解析", result);
  1166. //因为部分预测还不支持 所以不需要
  1167. // handleXinYiWarningsYC(cscodBzz, split, cscodGkz, BusinessEnum.WarningCategoryEnum.CS_COD_YC.getCode(), hour, normConfig);
  1168. return;
  1169. }
  1170. //解析数据 处理报警 调研prompt 保存等
  1171. this.handlePredictorWarning(split, hour, task, tXinyiIndustries6, tXinyiRobots6);
  1172. }else {
  1173. log.error("预测数据返回结果为{},无法正常解析", result);
  1174. }
  1175. }
  1176. }
  1177. private void handlePredictorWarning(String[] split, String hour, String task, List<TXinyiIndustry> tXinyiIndustries6, List<TXinyiRobot> tXinyiRobots6) {
  1178. //获取配置表
  1179. List<TXinyiNormConfig> tXinyiNormConfigs = this.xinyiNormConfigMapper.selectTXinyiNormConfigList(null);
  1180. if(CollectionUtils.isEmpty(tXinyiNormConfigs)) {
  1181. log.error( "未查询到配置信息");
  1182. return;
  1183. }
  1184. TXinyiNormConfig normConfig = tXinyiNormConfigs.get(0);
  1185. //获取最新的工业库的数据
  1186. TXinyiIndustry tXinyiIndustry = this.xinyiIndustryMapper.selectTXinyiIndustryNewest();
  1187. List<ChartBasic> chartsDataList = new ArrayList<>(9);
  1188. if("cod".equals(task)){
  1189. BigDecimal cscodBzz = normConfig.getCscodBzz();
  1190. BigDecimal cscodGkz = normConfig.getCscodGkz();
  1191. //处理展示数据
  1192. for (int i = 0; i < tXinyiIndustries6.size(); i++) {
  1193. TXinyiIndustry tXinyiIndustryTemp = tXinyiIndustries6.get(5 - i);
  1194. ChartBasic chartBasic = new ChartBasic();
  1195. chartBasic.setTime(tXinyiIndustryTemp.getTestHour().substring(11));
  1196. chartBasic.setVal(tXinyiIndustryTemp.getCsCod());
  1197. chartsDataList.add(chartBasic);
  1198. }
  1199. String[] hours = hour.split(",");
  1200. for (int i = 0; i < split.length; i++) {
  1201. String val = split[i];
  1202. ChartBasic chartBasic = new ChartBasic();
  1203. chartBasic.setTime(hours[i]);
  1204. chartBasic.setVal(new BigDecimal(val));
  1205. chartsDataList.add(chartBasic);
  1206. }
  1207. handleXinYiWarningsYC(cscodBzz, split, cscodGkz, BusinessEnum.WarningCategoryEnum.CS_COD_YC.getCode(), hour, normConfig,tXinyiIndustry.getCsCod(), tXinyiIndustry, chartsDataList);
  1208. }else if("ss".equals(task)){
  1209. BigDecimal csSSBzz = normConfig.getCsssBzz();
  1210. BigDecimal csssGkz = normConfig.getCsssGkz();
  1211. //处理展示数据
  1212. for (int i = 0; i < tXinyiIndustries6.size(); i++) {
  1213. TXinyiIndustry tXinyiIndustryTemp = tXinyiIndustries6.get(5 - i);
  1214. ChartBasic chartBasic = new ChartBasic();
  1215. chartBasic.setTime(tXinyiIndustryTemp.getTestHour().substring(11));
  1216. chartBasic.setVal(tXinyiIndustryTemp.getCsSs());
  1217. chartsDataList.add(chartBasic);
  1218. }
  1219. String[] hours = hour.split(",");
  1220. for (int i = 0; i < split.length; i++) {
  1221. String val = split[i];
  1222. ChartBasic chartBasic = new ChartBasic();
  1223. chartBasic.setTime(hours[i]);
  1224. chartBasic.setVal(new BigDecimal(val));
  1225. chartsDataList.add(chartBasic);
  1226. }
  1227. handleXinYiWarningsYC(csSSBzz, split, csssGkz, BusinessEnum.WarningCategoryEnum.CS_SS_YC.getCode(), hour, normConfig,tXinyiIndustry.getCsCod(), tXinyiIndustry, chartsDataList);
  1228. }else if("tn".equals(task)){
  1229. BigDecimal cszzBzz = normConfig.getCszzBzz();
  1230. BigDecimal cszzGkz = normConfig.getCszzGkz();
  1231. //处理展示数据
  1232. for (int i = 0; i < tXinyiIndustries6.size(); i++) {
  1233. TXinyiIndustry tXinyiIndustryTemp = tXinyiIndustries6.get(5 - i);
  1234. ChartBasic chartBasic = new ChartBasic();
  1235. chartBasic.setTime(tXinyiIndustryTemp.getTestHour().substring(11));
  1236. chartBasic.setVal(tXinyiIndustryTemp.getCsTn());
  1237. chartsDataList.add(chartBasic);
  1238. }
  1239. String[] hours = hour.split(",");
  1240. for (int i = 0; i < split.length; i++) {
  1241. String val = split[i];
  1242. ChartBasic chartBasic = new ChartBasic();
  1243. chartBasic.setTime(hours[i]);
  1244. chartBasic.setVal(new BigDecimal(val));
  1245. chartsDataList.add(chartBasic);
  1246. }
  1247. handleXinYiWarningsYC(cszzBzz, split, cszzGkz, BusinessEnum.WarningCategoryEnum.CS_TN_YC.getCode(), hour, normConfig,tXinyiIndustry.getCsCod(), tXinyiIndustry, chartsDataList);
  1248. }else if("tp".equals(task)){
  1249. BigDecimal cszlBzz = normConfig.getCszlBzz();
  1250. BigDecimal cszlGkz = normConfig.getCszlGkz();
  1251. //处理展示数据
  1252. for (int i = 0; i < tXinyiIndustries6.size(); i++) {
  1253. TXinyiIndustry tXinyiIndustryTemp = tXinyiIndustries6.get(5 - i);
  1254. ChartBasic chartBasic = new ChartBasic();
  1255. chartBasic.setTime(tXinyiIndustryTemp.getTestHour().substring(11));
  1256. chartBasic.setVal(tXinyiIndustryTemp.getCsTp());
  1257. chartsDataList.add(chartBasic);
  1258. }
  1259. String[] hours = hour.split(",");
  1260. for (int i = 0; i < split.length; i++) {
  1261. String val = split[i];
  1262. ChartBasic chartBasic = new ChartBasic();
  1263. chartBasic.setTime(hours[i]);
  1264. chartBasic.setVal(new BigDecimal(val));
  1265. chartsDataList.add(chartBasic);
  1266. }
  1267. handleXinYiWarningsYC(cszlBzz, split, cszlGkz, BusinessEnum.WarningCategoryEnum.CS_TP_YC.getCode(), hour, normConfig,tXinyiIndustry.getCsCod(), tXinyiIndustry, chartsDataList);
  1268. }else if("nh3".equals(task)){
  1269. BigDecimal csadBzz = normConfig.getCsadBzz();
  1270. BigDecimal csadGkz = normConfig.getCsadGkz();
  1271. //处理展示数据
  1272. for (int i = 0; i < tXinyiIndustries6.size(); i++) {
  1273. TXinyiIndustry tXinyiIndustryTemp = tXinyiIndustries6.get(5 - i);
  1274. ChartBasic chartBasic = new ChartBasic();
  1275. chartBasic.setTime(tXinyiIndustryTemp.getTestHour().substring(11));
  1276. chartBasic.setVal(tXinyiIndustryTemp.getCsNh3());
  1277. chartsDataList.add(chartBasic);
  1278. }
  1279. String[] hours = hour.split(",");
  1280. for (int i = 0; i < split.length; i++) {
  1281. String val = split[i];
  1282. ChartBasic chartBasic = new ChartBasic();
  1283. chartBasic.setTime(hours[i]);
  1284. chartBasic.setVal(new BigDecimal(val));
  1285. chartsDataList.add(chartBasic);
  1286. }
  1287. handleXinYiWarningsYC(csadBzz, split, csadGkz, BusinessEnum.WarningCategoryEnum.CS_AD_YC.getCode(), hour, normConfig,tXinyiIndustry.getCsCod(), tXinyiIndustry, chartsDataList);
  1288. }else if("xsy1".equals(task)){
  1289. BigDecimal cscodBzz = new BigDecimal(XIAOSUANYAN_BZK);
  1290. BigDecimal cscodGkz = new BigDecimal(XIAOSUANYAN_GKZ);
  1291. //处理展示数据
  1292. for (int i = 0; i < tXinyiRobots6.size(); i++) {
  1293. TXinyiRobot tXinyiRobotTemp = tXinyiRobots6.get(5 - i);
  1294. ChartBasic chartBasic = new ChartBasic();
  1295. chartBasic.setTime(tXinyiRobotTemp.getTestHour().substring(11));
  1296. chartBasic.setVal(tXinyiRobotTemp.getNo3Hlj1Jqr());
  1297. chartsDataList.add(chartBasic);
  1298. }
  1299. String[] hours = hour.split(",");
  1300. for (int i = 0; i < split.length; i++) {
  1301. String val = split[i];
  1302. ChartBasic chartBasic = new ChartBasic();
  1303. chartBasic.setTime(hours[i]);
  1304. chartBasic.setVal(new BigDecimal(val));
  1305. chartsDataList.add(chartBasic);
  1306. }
  1307. handleXinYiWarningsYC(cscodBzz, split, cscodGkz, BusinessEnum.WarningCategoryEnum.CS_XSY_1_YC.getCode(), hour, normConfig,tXinyiIndustry.getCsCod(), tXinyiIndustry, chartsDataList);
  1308. }else if("xsy2".equals(task)){
  1309. BigDecimal cscodBzz = new BigDecimal(XIAOSUANYAN_BZK);
  1310. BigDecimal cscodGkz = new BigDecimal(XIAOSUANYAN_GKZ);
  1311. //处理展示数据
  1312. for (int i = 0; i < tXinyiRobots6.size(); i++) {
  1313. TXinyiRobot tXinyiRobotTemp = tXinyiRobots6.get(5 - i);
  1314. ChartBasic chartBasic = new ChartBasic();
  1315. chartBasic.setTime(tXinyiRobotTemp.getTestHour().substring(11));
  1316. chartBasic.setVal(tXinyiRobotTemp.getNo3Hlj2Jqr());
  1317. chartsDataList.add(chartBasic);
  1318. }
  1319. String[] hours = hour.split(",");
  1320. for (int i = 0; i < split.length; i++) {
  1321. String val = split[i];
  1322. ChartBasic chartBasic = new ChartBasic();
  1323. chartBasic.setTime(hours[i]);
  1324. chartBasic.setVal(new BigDecimal(val));
  1325. chartsDataList.add(chartBasic);
  1326. }
  1327. handleXinYiWarningsYC(cscodBzz, split, cscodGkz, BusinessEnum.WarningCategoryEnum.CS_XSY_2_YC.getCode(), hour, normConfig,tXinyiIndustry.getCsCod(), tXinyiIndustry, chartsDataList);
  1328. }else {
  1329. log.error("暂未支持的类型{}", task);
  1330. }
  1331. }
  1332. private void handleXinYiWarningsYC(BigDecimal csBzz, String[] split, BigDecimal csGkz, String category, String hour, TXinyiNormConfig normConfig, BigDecimal currentVal, TXinyiIndustry tXinyiIndustry, List<ChartBasic> chartsDataList) {
  1333. BigDecimal multiply = csBzz.multiply(new BigDecimal(MyConstants.SCALE_VALUE));
  1334. TXinyiWarningRecord tXinyiWarningRecord = null;
  1335. for (String forecast : split) {
  1336. BigDecimal forecastVal = new BigDecimal(forecast);
  1337. tXinyiWarningRecord = new TXinyiWarningRecord();
  1338. /*String category = BusinessEnum.WarningCategoryEnum.CS_AD.getCode();*/
  1339. tXinyiWarningRecord.setStatus(0);
  1340. tXinyiWarningRecord.setType(2);
  1341. tXinyiWarningRecord.setCategory(category);
  1342. tXinyiWarningRecord.setTime(DateUtils.getNowDate());
  1343. tXinyiWarningRecord.setWarningVal(currentVal);//当前值
  1344. tXinyiWarningRecord.setForecastVal(forecastVal);
  1345. tXinyiWarningRecord.setDesignVal(csBzz);
  1346. tXinyiWarningRecord.setControlVal(csGkz);
  1347. tXinyiWarningRecord.setCreateBy(WARNING_DEFAULT_CREATE);
  1348. tXinyiWarningRecord.setCreateTime(DateUtils.getNowDate());
  1349. tXinyiWarningRecord.setRemark("0");
  1350. //2024年5月25日17:52:33 如果工业库获取不到数据,也触发报警,但是不调用决策接口
  1351. if(Objects.isNull(forecastVal)){
  1352. tXinyiWarningRecord.setReason(category + EXCEPTION_WARNING_YC);
  1353. tXinyiWarningRecord.setLevel(WARNING_LEVEL_NO_DATE);
  1354. }else if(forecastVal.compareTo(multiply) > 0){//一级
  1355. tXinyiWarningRecord.setReason(category + CHAOBIAO_WARNING_YC);
  1356. tXinyiWarningRecord.setLevel(WARNING_LEVEL_ONE);
  1357. }else if(forecastVal.compareTo(csBzz) >= 0 && forecastVal.compareTo(multiply) <= 0){//二级
  1358. tXinyiWarningRecord.setReason(category + CHAOBIAO_WARNING_YC);
  1359. tXinyiWarningRecord.setLevel(WARNING_LEVEL_TWO);
  1360. }else if(!Objects.isNull(csGkz) && forecastVal.compareTo(csGkz) > 0){
  1361. tXinyiWarningRecord.setReason(category + CHAOGUANKONG_WARNING_YC);
  1362. tXinyiWarningRecord.setLevel(WARNING_LEVEL_THREE);
  1363. }else{
  1364. tXinyiWarningRecord = null;//这种的无需处理
  1365. }
  1366. if(!Objects.isNull(tXinyiWarningRecord)){
  1367. log.info("预测报警中出现了超标的情况,循环可以退出了");
  1368. break;
  1369. }
  1370. }
  1371. //当前状态正常 需要查询历史有无正在报警的数据,如果有,将报警状态改完2(系统自动关闭)
  1372. List<TXinyiWarningRecord> tXinyiWarningRecords = this.xinyiWarningRecordMapper.selectTXinyiWarningRecordList(TXinyiWarningRecord.builder().delFlag(0).type(2).category(category).warningStatus(0).build());
  1373. if(Objects.isNull(tXinyiWarningRecord)){//数据正常,无告警信息
  1374. if(!CollectionUtils.isEmpty(tXinyiWarningRecords)){
  1375. log.info( "{}:现在恢复正常,历史报警数据为{}", category,JSON.toJSONString(tXinyiWarningRecords));
  1376. for (TXinyiWarningRecord xinyiWarningRecord : tXinyiWarningRecords) {
  1377. xinyiWarningRecord.setStatus(2);
  1378. Date nowDate = DateUtils.getNowDate();
  1379. xinyiWarningRecord.setOffTime(nowDate);
  1380. xinyiWarningRecord.setUpdateTime(nowDate);
  1381. xinyiWarningRecord.setUpdateBy(WARNING_DEFAULT_CREATE);
  1382. this.xinyiWarningRecordMapper.updateTXinyiWarningRecord(xinyiWarningRecord);
  1383. }
  1384. }
  1385. }else{//有告警信息
  1386. if(CollectionUtils.isEmpty(tXinyiWarningRecords)){//之前没有告警记录
  1387. //保存到数据库中
  1388. this.xinyiWarningRecordMapper.insertTXinyiWarningRecord(tXinyiWarningRecord);
  1389. if(WARNING_LEVEL_NO_DATE.equals(tXinyiWarningRecord.getLevel())){
  1390. //只保存一个普通的问答记录 不需要调用决策信息,但是实时数据还是要记录的
  1391. this.addChatRecordByWarning(tXinyiWarningRecord, tXinyiIndustry, normConfig);
  1392. }else {
  1393. //继续调用大模型prompt
  1394. this.askBigModelForYC(tXinyiWarningRecord, tXinyiIndustry, normConfig, chartsDataList);
  1395. }
  1396. }else{
  1397. log.info("{}:之前已经有过告警记录了,且还是继续报警,无需重复添加报警,但是决策仍然要调用", category);
  1398. for (TXinyiWarningRecord xinyiWarningRecord : tXinyiWarningRecords) {//理论上只有一个的
  1399. if(WARNING_LEVEL_NO_DATE.equals(tXinyiWarningRecord.getLevel())){
  1400. //只保存一个普通的问答记录 不需要调用决策信息,但是实时数据还是要记录的
  1401. this.addChatRecordByWarning(tXinyiWarningRecord, tXinyiIndustry, normConfig);
  1402. }else {
  1403. //继续调用决策
  1404. this.askBigModelForYC(xinyiWarningRecord, tXinyiIndustry, normConfig, chartsDataList);
  1405. }
  1406. }
  1407. }
  1408. }
  1409. }
  1410. private void askBigModelForYC(TXinyiWarningRecord xinyiWarningRecord, TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig, List<ChartBasic> chartsDataList) {
  1411. log.info("预测进入了后台接口调⽤⼤模型获取问答结果处理(预测)");
  1412. StringBuilder sb = new StringBuilder();
  1413. String sessionId = IdUtils.simpleUUID();
  1414. ChatReq chatReq = new ChatReq();
  1415. // String ipAddr = IpUtils.getIpAddr();//获取用户的ip地址 传给大模型
  1416. String ipAddr = "";//获取用户的ip地址 传给大模型 定时任务获取不到ip地址
  1417. int counts = 1;//默认是第一次
  1418. //这种问答 没有历史问答的概念 直接把问题扔进去就行 无需查询历史记录
  1419. List<String> historyDates = new ArrayList<>();
  1420. //构建问题(替换提示词中的占位符)
  1421. String shWarningPrompt = YC_WARNING_PROMPT;
  1422. shWarningPrompt =shWarningPrompt.replace("#{0}", xinyiWarningRecord.getReason());
  1423. shWarningPrompt =shWarningPrompt.replace("#{1}", String.valueOf(xinyiWarningRecord.getDesignVal()));
  1424. shWarningPrompt =shWarningPrompt.replace("#{2}", String.valueOf(xinyiWarningRecord.getWarningVal()));
  1425. shWarningPrompt =shWarningPrompt.replace("#{3}", String.valueOf(xinyiWarningRecord.getForecastVal()));
  1426. historyDates.add(shWarningPrompt);
  1427. // 获取输出流
  1428. ManagedChannel channel = null;
  1429. try {
  1430. channel = ManagedChannelBuilder.forAddress("10.0.0.24", 17070)
  1431. .usePlaintext()
  1432. .build();
  1433. InferenceAPIsServiceGrpc.InferenceAPIsServiceBlockingStub stub = InferenceAPIsServiceGrpc.newBlockingStub(channel);
  1434. String dataJson = "{\"bot_id\":\"721\",\"exp_id\":\"721\",\"session_id\":\"" + sessionId + "\",\"use_rag\":\"true\",\"prompt\":\"你是⼀个资深⽔务领域专家,能回答各种⽔务相关问题\",\"history_dia\":" + JSON.toJSONString(historyDates) + ",\"generate_args\":{\"max_new_tokens\":2048,\"max_length\":4096,\"num_beams\":1,\"do_sample\":true,\"top_p\":0.7,\"temperature\":0.95},\"extra\":{ \"ip_address\": \"" + ipAddr + "\" },\"strengthen\":" + (true) + "}";
  1435. log.info("请求大模型的问答参数为{}", dataJson);
  1436. PredictionsRequest request = PredictionsRequest.newBuilder()
  1437. .setModelName("slibra_bot")
  1438. .putInput("method", ByteString.copyFrom("infer_stream", "utf-8"))//推理
  1439. .putInput("data", ByteString.copyFrom(dataJson, "utf-8"))
  1440. .buildPartial();
  1441. Iterator<PredictionResponse> predictions = stub.streamPredictions(request);
  1442. //将结果记录到问答表
  1443. while (predictions.hasNext()) {
  1444. String responseStr = predictions.next().getPrediction().toStringUtf8();
  1445. log.info("大模型问答返回的原始结果为{}", responseStr);
  1446. responseStr = JSON.parseObject(responseStr).getString("message");
  1447. if("complete".equals(responseStr)){
  1448. log.info("结尾语句并且是非JSON,无需处理");
  1449. }else{
  1450. sb.append(responseStr);
  1451. }
  1452. }
  1453. //将问答更新到数据库中
  1454. chatReq.setSessionId(sessionId);
  1455. chatReq.setType(3);//0问答 1决策 2本地 3仿真预测
  1456. chatReq.setModule(3);//0专家问答 1智能工单 2智能体助手 3告警 4简报
  1457. String showVal = this.buildShowValueWithCharts(xinyiWarningRecord, tXinyiIndustry, normConfig, chartsDataList);
  1458. chatReq.setShowVal(showVal);
  1459. chatReq.setQuestion(shWarningPrompt);
  1460. chatReq.setAnswer(sb.toString());
  1461. chatReq.setWarningId(String.valueOf(xinyiWarningRecord.getId()));
  1462. chatReq.setCounts(counts);//问答次数
  1463. chatReq.setUserId(WARNING_DEFAULT_CREATE);
  1464. chatReq.setCreateBy(WARNING_DEFAULT_CREATE);
  1465. chatReq.setCreateTime(DateUtils.getNowDate());
  1466. this.xinyiChatRecordMapper.insertTXinyiChatRecord(chatReq);
  1467. } catch (IOException e) {
  1468. throw new RuntimeException(e);
  1469. } finally {
  1470. // 关闭输出流
  1471. channel.shutdown();
  1472. }
  1473. }
  1474. public static String testPredictor(String type){
  1475. // 获取输出流
  1476. ManagedChannel channel = null;
  1477. try {
  1478. channel = ManagedChannelBuilder.forAddress("10.0.0.24", 17070)
  1479. .usePlaintext()
  1480. .build();
  1481. InferenceAPIsServiceGrpc.InferenceAPIsServiceBlockingStub stub = InferenceAPIsServiceGrpc.newBlockingStub(channel);
  1482. String dataJson = "{\"bot_id\":\"b00001\",\"exp_id\":\"721\",\"norm\":\"" + type + "\",\"session_id\":\" " + IdUtils.simpleUUID() + " \",\"extra\":{}}";
  1483. log.info("请求大模型的预测的参数为{}", dataJson);
  1484. PredictionsRequest request = PredictionsRequest.newBuilder()
  1485. .setModelName("slibra_bot")
  1486. .putInput("method", ByteString.copyFrom("predictor", "utf-8"))//推理
  1487. .putInput("data", ByteString.copyFrom(dataJson, "utf-8"))
  1488. .buildPartial();
  1489. Iterator<PredictionResponse> predictions = stub.streamPredictions(request);
  1490. //将结果记录到问答表
  1491. String responseStr = predictions.next().getPrediction().toStringUtf8();
  1492. log.info("大模型的预测的返回结果为{}", responseStr);
  1493. // return JSON.parseObject(responseStr).getString("pred");
  1494. return responseStr;
  1495. } catch (IOException e) {
  1496. throw new RuntimeException(e);
  1497. } finally {
  1498. assert channel != null;
  1499. channel.shutdown();
  1500. }
  1501. }
  1502. /**
  1503. *
  1504. * 2022/01/01 转成2022年01月01日 数据
  1505. * @param testDate
  1506. * @return
  1507. */
  1508. private String formateDateStr(String testDate) {
  1509. if(StringUtils.isBlank(testDate))
  1510. return "";
  1511. if(!testDate.contains("/"))
  1512. return testDate;
  1513. String[] split = testDate.split("/");
  1514. return split[0] + "年" + split[1] + "月" + split[2] + "日";
  1515. }
  1516. private void askBigModel(String question, String showVal) {
  1517. log.info("进入了后台接口调⽤⼤模型获取问答结果处理");
  1518. StringBuilder sb = new StringBuilder();
  1519. String sessionId = IdUtils.simpleUUID();
  1520. ChatReq chatReq = new ChatReq();
  1521. // String ipAddr = IpUtils.getIpAddr();//获取用户的ip地址 传给大模型
  1522. String ipAddr = "";//获取用户的ip地址 传给大模型 定时任务获取不到ip地址
  1523. int counts = 1;//默认是第一次
  1524. //这种问答 没有历史问答的概念 直接把问题扔进去就行 无需查询历史记录
  1525. List<String> historyDates = new ArrayList<>();
  1526. historyDates.add(question);
  1527. // 获取输出流
  1528. ManagedChannel channel = null;
  1529. try {
  1530. channel = ManagedChannelBuilder.forAddress("10.0.0.24", 17070)
  1531. .usePlaintext()
  1532. .build();
  1533. InferenceAPIsServiceGrpc.InferenceAPIsServiceBlockingStub stub = InferenceAPIsServiceGrpc.newBlockingStub(channel);
  1534. String dataJson = "{\"bot_id\":\"721\",\"exp_id\":\"721\",\"session_id\":\"" + sessionId + "\",\"use_rag\":\"true\",\"prompt\":\"你是⼀个资深⽔务领域专家,能回答各种⽔务相关问题\",\"history_dia\":" + JSON.toJSONString(historyDates) + ",\"generate_args\":{\"max_new_tokens\":2048,\"max_length\":4096,\"num_beams\":1,\"do_sample\":true,\"top_p\":0.7,\"temperature\":0.95},\"extra\":{ \"ip_address\": \"" + ipAddr + "\" },\"strengthen\":" + (true) + "}";
  1535. log.info("请求大模型的问答参数为{}", dataJson);
  1536. PredictionsRequest request = PredictionsRequest.newBuilder()
  1537. .setModelName("slibra_bot")
  1538. .putInput("method", ByteString.copyFrom("infer_stream", "utf-8"))//推理
  1539. .putInput("data", ByteString.copyFrom(dataJson, "utf-8"))
  1540. .buildPartial();
  1541. Iterator<PredictionResponse> predictions = stub.streamPredictions(request);
  1542. //将结果记录到问答表
  1543. while (predictions.hasNext()) {
  1544. String responseStr = predictions.next().getPrediction().toStringUtf8();
  1545. log.info("大模型问答返回的原始结果为{}", responseStr);
  1546. responseStr = JSON.parseObject(responseStr).getString("message");
  1547. if("complete".equals(responseStr)){
  1548. System.out.println("结尾语句并且是非JSON,无需处理");
  1549. //结束语句也流式输出,但是并不记录下来 2024年5月24日11:15:23 也不返回前端
  1550. /*outputStream.write(responseStr.getBytes());
  1551. outputStream.flush();*/
  1552. }else{
  1553. sb.append(responseStr);
  1554. }
  1555. }
  1556. //将问答更新到数据库中
  1557. chatReq.setSessionId(sessionId);
  1558. chatReq.setType(2);//0问答 1决策 2本地 3仿真预测
  1559. chatReq.setModule(4);//0专家问答 1智能工单 2智能体助手 3告警 4简报
  1560. chatReq.setShowVal(showVal);
  1561. chatReq.setQuestion(question);
  1562. chatReq.setAnswer(sb.toString());
  1563. chatReq.setCounts(counts);//问答次数
  1564. chatReq.setUserId(WARNING_DEFAULT_CREATE);
  1565. chatReq.setCreateBy(WARNING_DEFAULT_CREATE);
  1566. chatReq.setCreateTime(DateUtils.getNowDate());
  1567. this.xinyiChatRecordMapper.insertTXinyiChatRecord(chatReq);
  1568. } catch (IOException e) {
  1569. throw new RuntimeException(e);
  1570. } finally {
  1571. // 关闭输出流
  1572. channel.shutdown();
  1573. }
  1574. }
  1575. private String buildShortReportQueryData(List<TXinyiDaily> dailyTwoRecords) {
  1576. //查询配置信息
  1577. List<TXinyiNormConfig> tXinyiNormConfigs = this.xinyiNormConfigMapper.selectTXinyiNormConfigList(null);
  1578. if(CollectionUtils.isEmpty(tXinyiNormConfigs))
  1579. return null;
  1580. TXinyiNormConfig normConfig = tXinyiNormConfigs.get(0);
  1581. //获取数据
  1582. TXinyiDaily yesterdayData = dailyTwoRecords.get(0);
  1583. TXinyiDaily beforeYesterdayData = dailyTwoRecords.get(1);
  1584. String originStr = JIAN_BAO_PROMPT;
  1585. String yesterdayStr = yesterdayData.getTestDate().substring(5).replace("/", "月") + "日";
  1586. String beforeYesterdayStr = beforeYesterdayData.getTestDate().substring(5).replace("/", "月") + "日";
  1587. originStr = originStr.replace("#{0}", yesterdayStr);
  1588. originStr = originStr.replace("#{1}", beforeYesterdayStr);
  1589. originStr = originStr.replace("#{2}", String.valueOf(yesterdayData.getJsCod()));
  1590. originStr = originStr.replace("#{3}", String.valueOf(yesterdayData.getJsTn()));
  1591. originStr = originStr.replace("#{4}", String.valueOf(yesterdayData.getJsTp()));
  1592. originStr = originStr.replace("#{5}", String.valueOf(yesterdayData.getJsNh3()));
  1593. originStr = originStr.replace("#{6}", String.valueOf(yesterdayData.getJsSs()));
  1594. originStr = originStr.replace("#{7}", String.valueOf(yesterdayData.getJSL()));
  1595. originStr = originStr.replace("#{8}", String.valueOf(yesterdayData.getCsCod()));
  1596. originStr = originStr.replace("#{9}", String.valueOf(yesterdayData.getCsTn()));
  1597. originStr = originStr.replace("#{10}", String.valueOf(yesterdayData.getCsTp()));
  1598. originStr = originStr.replace("#{11}", String.valueOf(yesterdayData.getCsNh3()));
  1599. originStr = originStr.replace("#{12}", String.valueOf(yesterdayData.getCsSs()));
  1600. originStr = originStr.replace("#{13}", String.valueOf(yesterdayData.getCSL()));
  1601. originStr = originStr.replace("#{14}", String.valueOf(beforeYesterdayData.getJsCod()));
  1602. originStr = originStr.replace("#{15}", String.valueOf(beforeYesterdayData.getJsTn()));
  1603. originStr = originStr.replace("#{16}", String.valueOf(beforeYesterdayData.getJsTp()));
  1604. originStr = originStr.replace("#{17}", String.valueOf(beforeYesterdayData.getJsNh3()));
  1605. originStr = originStr.replace("#{18}", String.valueOf(beforeYesterdayData.getJsSs()));
  1606. originStr = originStr.replace("#{19}", String.valueOf(beforeYesterdayData.getJSL()));
  1607. originStr = originStr.replace("#{20}", String.valueOf(beforeYesterdayData.getCsCod()));
  1608. originStr = originStr.replace("#{21}", String.valueOf(beforeYesterdayData.getCsTn()));
  1609. originStr = originStr.replace("#{22}", String.valueOf(beforeYesterdayData.getCsTp()));
  1610. originStr = originStr.replace("#{23}", String.valueOf(beforeYesterdayData.getCsNh3()));
  1611. originStr = originStr.replace("#{24}", String.valueOf(beforeYesterdayData.getCsSs()));
  1612. originStr = originStr.replace("#{25}", String.valueOf(beforeYesterdayData.getCSL()));
  1613. originStr = originStr.replace("#{26}", String.valueOf(normConfig.getJscodSjz()));
  1614. originStr = originStr.replace("#{27}", String.valueOf(normConfig.getJszdSjz()));
  1615. originStr = originStr.replace("#{28}", String.valueOf(normConfig.getJszlSjz()));
  1616. originStr = originStr.replace("#{29}", String.valueOf(normConfig.getJsadSjz()));
  1617. originStr = originStr.replace("#{30}", String.valueOf(normConfig.getJsssSjz()));
  1618. originStr = originStr.replace("#{31}", String.valueOf(normConfig.getCscodBzz()));
  1619. originStr = originStr.replace("#{32}", String.valueOf(normConfig.getCszzBzz()));
  1620. originStr = originStr.replace("#{33}", String.valueOf(normConfig.getCszlBzz()));
  1621. originStr = originStr.replace("#{34}", String.valueOf(normConfig.getCsadBzz()));
  1622. originStr = originStr.replace("#{35}", String.valueOf(normConfig.getCsssBzz()));
  1623. return originStr;
  1624. }
  1625. public static String handleDate(String str){
  1626. StringBuilder sb = new StringBuilder();
  1627. if(str.contains(" ")){//包含空格 就是年月日时分秒了
  1628. String[] split = str.split(" ");
  1629. addBeforeZero(sb, split[0], "/");
  1630. sb.append(" ");
  1631. addBeforeZero(sb, split[1], ":");
  1632. }else{
  1633. if(str.contains("/")){//年月日
  1634. addBeforeZero(sb, str, "/");
  1635. }else if(str.contains(":")){//时分秒
  1636. addBeforeZero(sb, str, ":");
  1637. }else {
  1638. sb.append(str);
  1639. }
  1640. }
  1641. return sb.toString();
  1642. }
  1643. public static StringBuilder addBeforeZero(StringBuilder sb, String str, String tag){
  1644. String[] split = str.split(tag);
  1645. int length = split.length;
  1646. for (int i = 0; i < length; i++) {
  1647. String value = split[i];
  1648. Integer intValue = Integer.parseInt(value);
  1649. if(intValue < 10 && value.length() == 1){////防止有正确的情况 额外再补充字符串
  1650. sb.append(0).append(value);
  1651. }else{
  1652. sb.append(value);
  1653. }
  1654. if(i < length-1){
  1655. sb.append(tag);
  1656. }
  1657. }
  1658. return sb;
  1659. }
  1660. public static void main(String[] args) {
  1661. /*LocalDateTime endTime = LocalDateTime.now();
  1662. System.out.println("endTime = " + endTime);
  1663. endTime = endTime.plusMinutes(60);
  1664. System.out.println("endTime = " + endTime);*/
  1665. /*String str = "2024/04/15 09:55";
  1666. System.out.println(str);
  1667. System.out.println(str.substring(0,10));
  1668. System.out.println(str.substring(0,13));*/
  1669. /*String str = "2024/04/18 08:00";
  1670. str = str + ":00";
  1671. System.out.println(str);
  1672. LocalDateTime startTime = LocalDateTime.parse(str.replaceAll("/", "-").replace(" ", "T"));
  1673. System.out.println(startTime.plusMinutes(1L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));*/
  1674. /*ArrayList<Integer> objects = new ArrayList<>();
  1675. objects.add(1);
  1676. objects.add(2);
  1677. objects.add(3);
  1678. System.out.println(objects);
  1679. objects.clear();
  1680. System.out.println(objects);
  1681. // test();
  1682. Date date = new Date();
  1683. System.out.println(DateUtils.differentHoursByMillisecond(date, date));
  1684. HashMap<Object, Object> map = new HashMap<>();
  1685. map.put("a", null);
  1686. map.put("b", "ab");
  1687. map.put("c", "");
  1688. map.put("d", '1');
  1689. System.out.println(JSON.toJSONString(map, JSONWriter.Feature.WriteNulls));
  1690. TXinyiIndustry tXinyiIndustry = new TXinyiIndustry();
  1691. System.out.println(JSON.toJSONString(tXinyiIndustry, JSONWriter.Feature.WriteNulls));*/
  1692. String s = "2022/01/01".substring(5).replace("/", "月") + "日";
  1693. System.out.println("s = " + s);
  1694. // System.out.println(new BigDecimal("1").compareTo(null));//空指针 要判断
  1695. System.out.println("2022/01/01 10".substring(11));
  1696. }
  1697. //测试工业库 没小时保存一条记录是否可行
  1698. public static void test() {
  1699. // 每个小时的时间格式
  1700. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  1701. LocalDateTime startTime = LocalDateTime.parse("2024-05-18T00:00:00");
  1702. LocalDateTime endTime = LocalDateTime.now();
  1703. // 循环按小时分割
  1704. LocalDateTime currentHour = startTime;
  1705. //最终获取的数据
  1706. Map<String, TXinyiIndustry> needMap = new LinkedHashMap<>();
  1707. while (currentHour.isBefore(endTime)) {
  1708. String begin = currentHour.format(formatter);
  1709. String end = currentHour.plusMinutes(5).format(formatter);
  1710. // 输出当前小时的起始时间和结束时间
  1711. System.out.println("起始时间:" + begin);
  1712. System.out.println("结束时间:" + end);
  1713. // 当前小时加一小时,作为下一个小时的起始时间
  1714. currentHour = currentHour.plusMinutes(5);
  1715. //每个小时查询一次数据
  1716. String url = "http://10.0.0.27:4568/api/v1/khquerydata";
  1717. HashMap<String, Object> req = new HashMap<>();
  1718. req.put("tagNames", queryTags);
  1719. req.put("startTime", begin);
  1720. req.put("endTime", end);
  1721. req.put("recordNumbers", 100000);
  1722. String body = HttpRequest.post(url).header("Authorization", "c2E6c2E=").header("clientName", "hongshan").body(JSON.toJSONString(req)).execute().body();
  1723. // System.out.println("body = " + body);
  1724. List<HashMap<String, String>> list = new ArrayList<>();
  1725. //行转列数据处理
  1726. for (String queryTag : queryTags) {
  1727. JSONArray array = JSON.parseObject(body).getJSONArray(queryTag);
  1728. //特殊数据处理一
  1729. if(Objects.isNull(array) || array.isEmpty()){
  1730. System.out.println(queryTag + "查询到了空的数据,跳过本次循环");
  1731. continue;
  1732. }
  1733. int size = array.size();
  1734. //特殊数据处理二
  1735. if("0".equals(array.get(1) + "")){
  1736. System.out.println(queryTag + "查询到了数据,但是数据集合只有一条,且都是0");
  1737. continue;
  1738. }
  1739. //结合至少62个数据才满足条件(有可能获取不到)
  1740. /*if(size < 62){
  1741. System.out.println(queryTag + "查询到了不符合条件的数据,跳过本次循环");
  1742. continue;
  1743. }*/
  1744. //存放的数据集
  1745. //利用map去重
  1746. HashMap<String, String> map = new LinkedHashMap<>();
  1747. for (int i = 2; i < size; i++) {
  1748. // System.out.println(i + "" + array.get(i));
  1749. JSONArray oneRecord = JSON.parseArray(JSON.toJSONString(array.get(i)));
  1750. //处理为空或者为0的数据
  1751. Object timeStampValue = oneRecord.get(2);
  1752. if(Objects.isNull(timeStampValue) || "0".equals(timeStampValue + ""))
  1753. continue;
  1754. BigDecimal value = Objects.isNull(oneRecord.get(0)) ? null : new BigDecimal(oneRecord.get(0) + "");
  1755. long timestamp = (long) timeStampValue;
  1756. String format = DateUtil.format(new Date(timestamp), DateUtils.YYYYMMDDHH_TS);
  1757. map.put(format, queryTag + "-" + value);
  1758. }
  1759. list.add(map);
  1760. }
  1761. Set<String> recordTimeSet = new HashSet<>();
  1762. Map<String, TXinyiIndustry> recordMap = new HashMap<>();
  1763. for (int i = 0; i < list.size(); i++) {
  1764. HashMap<String, String> map = list.get(i);
  1765. int finalJ = i;
  1766. map.forEach((k, v) ->{
  1767. TXinyiIndustry industry = null;
  1768. if(!recordTimeSet.contains(k)){//第一次
  1769. industry = new TXinyiIndustry();
  1770. recordTimeSet.add(k);
  1771. recordMap.put(k, industry);
  1772. }else{
  1773. industry = recordMap.get(k);
  1774. }
  1775. industry.setTestTime(k + ":00");
  1776. //2024年4月15日11:19:52 额外增加2个字段
  1777. industry.setTestDate(k.substring(0,10));
  1778. industry.setTestHour(k.substring(0,13));
  1779. //解析值
  1780. String[] split = v.split("-");
  1781. String type = split[0];
  1782. BigDecimal value = new BigDecimal(split[1]);
  1783. if ("信义污水厂JS_COD_Value".equals(type)) {
  1784. industry.setJsCod(value);
  1785. } else if ("信义污水厂JS_PH_Value".equals(type)) {
  1786. industry.setJsPh(value);
  1787. } else if ("信义污水厂JS_SS_Value".equals(type)) {
  1788. industry.setJsSs(value);
  1789. } else if ("信义污水厂JS_ZL_Value".equals(type)) {
  1790. industry.setJsTp(value);
  1791. } else if ("信义污水厂JS_ZA_Value".equals(type)) {
  1792. industry.setJsTn(value);
  1793. } else if ("信义污水厂JS_AD_Value".equals(type)) {
  1794. industry.setJsNh3(value);
  1795. } else if ("信义污水厂JS_T_Value".equals(type)) {
  1796. industry.setJsSwPh(value);
  1797. } else if ("信义污水厂进水泵房液位".equals(type)) {
  1798. industry.setJsBfyw(value);
  1799. } else if ("信义污水厂出水瞬时流量".equals(type)) {
  1800. industry.setCsSlqc(value);
  1801. } else if ("信义污水厂升级出水COD".equals(type)) {
  1802. industry.setCsCod(value);
  1803. } else if ("信义污水厂升级出水PH".equals(type)) {
  1804. industry.setCsPh(value);
  1805. } else if ("信义污水厂升级出水SS".equals(type)) {
  1806. industry.setCsSs(value);
  1807. } else if ("信义污水厂升级出水TN".equals(type)) {
  1808. industry.setCsTn(value);
  1809. } else if ("信义污水厂升级出水TP".equals(type)) {
  1810. industry.setCsTp(value);
  1811. } else if ("信义污水厂升级出水氨氮".equals(type)) {
  1812. industry.setCsNh3(value);
  1813. } else if ("信义污水厂AIT202_Value".equals(type)) {
  1814. industry.setOneHyzdDo(value);
  1815. } else if ("信义污水厂AIT203_Value".equals(type)) {
  1816. industry.setOneHymdDo(value);
  1817. } else if ("信义污水厂AIT207_Value".equals(type)) {
  1818. industry.setTwoHyzdDo(value);
  1819. } else if ("信义污水厂AIT206_Value".equals(type)) {
  1820. industry.setTwoHymdDo(value);
  1821. } else if ("信义污水厂AIT209_Value".equals(type)) {
  1822. industry.setOneMlss(value);
  1823. } else if ("信义污水厂AIT210_Value".equals(type)) {
  1824. industry.setTwoMlss(value);
  1825. } else if ("信义污水厂进水TDS".equals(type)) {
  1826. industry.setJsTds(value);
  1827. } else if ("信义污水厂FT101_Value".equals(type)) {
  1828. industry.setJsSlq(value);
  1829. } else if ("信义污水厂SWCHHYHLB1_R_Value".equals(type)) {
  1830. industry.setNHlbOneGp(value);
  1831. } else if ("信义污水厂SWCHHYHLB2_R_Value".equals(type)) {
  1832. industry.setNHlbTwoGp(value);
  1833. } else if ("信义污水厂SWCHHYHLB3_R_Value".equals(type)) {
  1834. industry.setNHlbThreeGp(value);
  1835. } else if ("信义污水厂SWCHHYHLB4_R_Value".equals(type)) {
  1836. industry.setNHlbFourGp(value);
  1837. } else if ("信义污水厂SWCHHYHLB5_R_Value".equals(type)) {
  1838. industry.setNhlBFiveGp(value);
  1839. } else if ("信义污水厂SWCHHYHLB6_R_Value".equals(type)) {
  1840. industry.setNHlbSixGp(value);
  1841. } else if ("信义污水厂SWCWNHLB1_R_Value".equals(type)) {
  1842. industry.setWHlbOneGp(value);
  1843. } else if ("信义污水厂SWCWNHLB2_R_Value".equals(type)) {
  1844. industry.setWHlbTwoGp(value);
  1845. } else if ("信义污水厂SWCWNHLB3_R_Value".equals(type)) {
  1846. industry.setWHlbThreeGp(value);
  1847. } else if ("信义污水厂SWCWNHLB4_R_Value".equals(type)) {
  1848. industry.setWHlbFourGp(value);
  1849. } else if ("信义污水厂SWCWNHLB5_R_Value".equals(type)) {
  1850. industry.setWHlbFiveGp(value);
  1851. } else if ("信义污水厂GFJ1_R_Value".equals(type)) {
  1852. industry.setFjOne(value);
  1853. } else if ("信义污水厂GFJ2_R_Value".equals(type)) {
  1854. industry.setFjTwo(value);
  1855. } else if ("信义污水厂GFJ3_R_Value".equals(type)) {
  1856. industry.setFjThree(value);
  1857. } else if ("信义污水厂GFJ4_R_Value".equals(type)) {
  1858. industry.setFjFour(value);
  1859. } else if ("信义污水厂GFJ5_R_Value".equals(type)) {
  1860. industry.setFjFive(value);
  1861. } else if ("信义污水厂GFJ6_R_Value".equals(type)) {
  1862. industry.setFjSix(value);
  1863. } else if ("信义污水厂GFJ1_KQLL_Value".equals(type)) {
  1864. industry.setKqllOne(value);
  1865. } else if ("信义污水厂GFJ2_KQLL_Value".equals(type)) {
  1866. industry.setKqllTwo(value);
  1867. } else if ("信义污水厂GFJ3_KQLL_Value".equals(type)) {
  1868. industry.setKqllThree(value);
  1869. } else if ("信义污水厂GFJ4_KQLL_Value".equals(type)) {
  1870. industry.setKqllFour(value);
  1871. } else if ("信义污水厂GFJ5_KQLL_Value".equals(type)) {
  1872. industry.setKqllFive(value);
  1873. } else if ("信义污水厂GFJ6_KQLL_Value".equals(type)) {
  1874. industry.setKqllSix(value);
  1875. }else if ("信义污水厂实际碳源加药量".equals(type)) {
  1876. industry.setSJTYJLY(value);
  1877. }else if ("信义污水厂除磷加药瞬时流量".equals(type)) {
  1878. industry.setCLJYSSLL(value);
  1879. } else if ("信义污水厂_除磷P04预测值_".equals(type)) {
  1880. industry.setCLP04YCZ(value);
  1881. }
  1882. //只有最后一次才执行数据库添加
  1883. if(finalJ == list.size()-1){
  1884. needMap.put(industry.getTestHour(), industry);
  1885. }
  1886. });
  1887. }
  1888. }
  1889. //保存数据 触发告警 决策 问答记录等等
  1890. needMap.forEach((k, industry) ->{
  1891. System.out.println(JSON.toJSONString(industry));
  1892. });
  1893. }
  1894. }