AsyncTask.java 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087
  1. package com.slibra.quartz.task;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import com.alibaba.fastjson2.JSONWriter;
  5. import com.google.protobuf.ByteString;
  6. import com.slibra.business.domain.*;
  7. import com.slibra.business.mapper.*;
  8. import com.slibra.business.req.*;
  9. import com.slibra.business.res.ShowValueCSBasic;
  10. import com.slibra.business.res.ShowValueJSBasic;
  11. import com.slibra.business.res.ShowValueSHBasic;
  12. import com.slibra.common.DecimalUtils;
  13. import com.slibra.common.config.BigModelConfig;
  14. import com.slibra.common.constant.MyConstants;
  15. import com.slibra.common.core.domain.DecisionReq;
  16. import com.slibra.common.core.domain.TXinyiDaily;
  17. import com.slibra.common.enums.BusinessEnum;
  18. import com.slibra.common.utils.DateUtils;
  19. import com.slibra.common.utils.LocalDateTimeUtil;
  20. import com.slibra.common.utils.StringUtils;
  21. import com.slibra.common.utils.format.WaterFormat;
  22. import com.slibra.common.utils.uuid.IdUtils;
  23. import com.slibra.quartz.business.JsCsFormatData;
  24. import inference.InferenceAPIsServiceGrpc;
  25. import inference.PredictionResponse;
  26. import inference.PredictionsRequest;
  27. import io.grpc.ManagedChannel;
  28. import io.grpc.ManagedChannelBuilder;
  29. import lombok.extern.slf4j.Slf4j;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.scheduling.annotation.Async;
  32. import org.springframework.stereotype.Component;
  33. import org.springframework.util.CollectionUtils;
  34. import java.io.IOException;
  35. import java.math.BigDecimal;
  36. import java.math.RoundingMode;
  37. import java.util.*;
  38. import static com.slibra.common.constant.MyConstants.*;
  39. import static com.slibra.common.enums.BusinessEnum.BigModelWarningTypeRemarkEnum.*;
  40. import static com.slibra.common.enums.BusinessEnum.WarningCategoryEnum.*;
  41. @Component
  42. @Slf4j
  43. public class AsyncTask {
  44. @Autowired
  45. private TXinyiIndustryMapper xinyiIndustryMapper;
  46. @Autowired
  47. private TXinyiNormConfigMapper xinyiNormConfigMapper;
  48. @Autowired
  49. private TXinyiWarningRecordMapper xinyiWarningRecordMapper;
  50. @Autowired
  51. private TXinyiChatRecordMapper xinyiChatRecordMapper;
  52. @Autowired
  53. private TXinyiDailyMapper xinyiDailyMapper;
  54. @Autowired
  55. private TXinyiCalculateMapper xinyiCalculateMapper;
  56. @Autowired
  57. private TXinyiRobotMapper xinyiRobotMapper;
  58. @Autowired
  59. private TXinyiForecastComparisonMapper xinyiForecastComparisonMapper;
  60. @Autowired
  61. private TXinyiBigTableHourMapper xinyiBigTableHourMapper;
  62. @Autowired
  63. private BigModelConfig bigModelConfig;
  64. @Autowired
  65. private JsCsFormatData jsCsFormatData;
  66. /**
  67. *
  68. * 异步处理告警任务
  69. * @param tXinyiIndustry
  70. */
  71. @Async("customizeExecutor")
  72. public void handleWarning(TXinyiIndustry tXinyiIndustry){
  73. //2024年6月18日10:45:20 额外计算一下预测的准确度
  74. try {
  75. this.updateForecastComparisonByIndustry(tXinyiIndustry);
  76. } catch (Exception e) {
  77. log.error("-------------------------------更新预测准确度的时候异常,异常信息为{}", e.getMessage());
  78. }
  79. log.info("进入了定时任务保存工业库数据并触发报警操作");
  80. //获取配置表
  81. List<TXinyiNormConfig> tXinyiNormConfigs = this.xinyiNormConfigMapper.selectTXinyiNormConfigList(null);
  82. if(CollectionUtils.isEmpty(tXinyiNormConfigs))
  83. throw new RuntimeException("未查询到配置信息");
  84. TXinyiNormConfig normConfig = tXinyiNormConfigs.get(0);
  85. //2024年6月7日15:26:44 把一些额外的计算的值,同时记录下来(xinyiCalculate对象在生化报警可能用的到)
  86. TXinyiCalculate xinyiCalculate = this.addCalculateByIndustry(tXinyiIndustry, normConfig);
  87. this.xinyiCalculateMapper.insertTXinyiCalculate(xinyiCalculate);
  88. //水质报警
  89. this.handleSZWarning(tXinyiIndustry, normConfig);
  90. //2024年5月28日14:14:26 下面是新增的 生化报警处理
  91. this.handleSHWarning(tXinyiIndustry, normConfig, xinyiCalculate);
  92. //2024年7月3日18:01:13 额外处理大表数据
  93. this.addBigTable(tXinyiIndustry, xinyiCalculate);
  94. }
  95. private void addBigTable(TXinyiIndustry tXinyiIndustry, TXinyiCalculate xinyiCalculate) {
  96. TXinyiBigTableHour xinyiBigTableHour = TXinyiBigTableHour.builder().build();
  97. //处理工业库的在线数据
  98. this.addIndustry2BigTable(xinyiBigTableHour, tXinyiIndustry);
  99. //处理计算的数据
  100. this.addCalculate2BigTable(xinyiBigTableHour, xinyiCalculate);
  101. //插入
  102. this.xinyiBigTableHourMapper.insertTXinyiBigTableHour(xinyiBigTableHour);
  103. }
  104. private void addIndustry2BigTable(TXinyiBigTableHour xinyiBigTableHour, TXinyiIndustry tXinyiIndustry) {
  105. //2024年7月5日18:13:43 处理时间和日期
  106. xinyiBigTableHour.setTestDate(tXinyiIndustry.getTestDate());
  107. xinyiBigTableHour.setTestHour(tXinyiIndustry.getTestHour());
  108. xinyiBigTableHour.setGyJsCod(tXinyiIndustry.getJsCod());
  109. xinyiBigTableHour.setGyJsPh(tXinyiIndustry.getJsPh());
  110. xinyiBigTableHour.setGyJsSs(tXinyiIndustry.getJsSs());
  111. xinyiBigTableHour.setGyJsTp(tXinyiIndustry.getJsTp());
  112. xinyiBigTableHour.setGyJsTn(tXinyiIndustry.getJsTn());
  113. xinyiBigTableHour.setGyJsNh3(tXinyiIndustry.getJsNh3());
  114. xinyiBigTableHour.setGyJsSwPh(tXinyiIndustry.getJsSwPh());
  115. xinyiBigTableHour.setGyJsBfyw(tXinyiIndustry.getJsBfyw());
  116. xinyiBigTableHour.setGyCsSlqc(tXinyiIndustry.getCsSlqc());
  117. xinyiBigTableHour.setGyCsCod(tXinyiIndustry.getCsCod());
  118. xinyiBigTableHour.setGyCsPh(tXinyiIndustry.getCsPh());
  119. xinyiBigTableHour.setGyCsSs(tXinyiIndustry.getCsSs());
  120. xinyiBigTableHour.setGyCsTn(tXinyiIndustry.getCsTn());
  121. xinyiBigTableHour.setGyCsTp(tXinyiIndustry.getCsTp());
  122. xinyiBigTableHour.setGyCsNh3(tXinyiIndustry.getCsNh3());
  123. xinyiBigTableHour.setGyOneHyzdDo(tXinyiIndustry.getOneHyzdDo());
  124. xinyiBigTableHour.setGyOneHymdDo(tXinyiIndustry.getOneHymdDo());
  125. xinyiBigTableHour.setGyTwoHyzdDo(tXinyiIndustry.getTwoHyzdDo());
  126. xinyiBigTableHour.setGyTwoHymdDo(tXinyiIndustry.getTwoHymdDo());
  127. xinyiBigTableHour.setGyOneMlss(tXinyiIndustry.getOneMlss());
  128. xinyiBigTableHour.setGyTwoMlss(tXinyiIndustry.getTwoMlss());
  129. xinyiBigTableHour.setGyJsTds(tXinyiIndustry.getJsTds());
  130. xinyiBigTableHour.setGyJsSlq(tXinyiIndustry.getJsSlq());
  131. xinyiBigTableHour.setGyNHlbOneGp(tXinyiIndustry.getNHlbOneGp());
  132. xinyiBigTableHour.setGyNHlbTwoGp(tXinyiIndustry.getNHlbTwoGp());
  133. xinyiBigTableHour.setGyNHlbThreeGp(tXinyiIndustry.getNHlbThreeGp());
  134. xinyiBigTableHour.setGyNHlbFourGp(tXinyiIndustry.getNHlbFourGp());
  135. xinyiBigTableHour.setGyNhlBFiveGp(tXinyiIndustry.getNhlBFiveGp());
  136. xinyiBigTableHour.setGyNHlbSixGp(tXinyiIndustry.getNHlbSixGp());
  137. xinyiBigTableHour.setGyWHlbOneGp(tXinyiIndustry.getWHlbOneGp());
  138. xinyiBigTableHour.setGyWHlbTwoGp(tXinyiIndustry.getWHlbTwoGp());
  139. xinyiBigTableHour.setGyWHlbThreeGp(tXinyiIndustry.getWHlbThreeGp());
  140. xinyiBigTableHour.setGyWHlbFourGp(tXinyiIndustry.getWHlbFourGp());
  141. xinyiBigTableHour.setGyWHlbFiveGp(tXinyiIndustry.getWHlbFiveGp());
  142. xinyiBigTableHour.setGyFjOne(tXinyiIndustry.getFjOne());
  143. xinyiBigTableHour.setGyFjTwo(tXinyiIndustry.getFjTwo());
  144. xinyiBigTableHour.setGyFjThree(tXinyiIndustry.getFjThree());
  145. xinyiBigTableHour.setGyFjFour(tXinyiIndustry.getFjFour());
  146. xinyiBigTableHour.setGyFjFive(tXinyiIndustry.getFjFive());
  147. xinyiBigTableHour.setGyFjSix(tXinyiIndustry.getFjSix());
  148. xinyiBigTableHour.setGyKqllOne(tXinyiIndustry.getKqllOne());
  149. xinyiBigTableHour.setGyKqllTwo(tXinyiIndustry.getKqllTwo());
  150. xinyiBigTableHour.setGyKqllThree(tXinyiIndustry.getKqllThree());
  151. xinyiBigTableHour.setGyKqllFour(tXinyiIndustry.getKqllFour());
  152. xinyiBigTableHour.setGyKqllFive(tXinyiIndustry.getKqllFive());
  153. xinyiBigTableHour.setGyKqllSix(tXinyiIndustry.getKqllSix());
  154. xinyiBigTableHour.setGySjtyjly(tXinyiIndustry.getSJTYJLY());
  155. xinyiBigTableHour.setGyCljyssll(tXinyiIndustry.getCLJYSSLL());
  156. xinyiBigTableHour.setGyHycRjyAll(tXinyiIndustry.getHycRjyAll());
  157. xinyiBigTableHour.setGyHycRjyZdAll(tXinyiIndustry.getHycRjyZdAll());
  158. xinyiBigTableHour.setGyHycWnndAll(tXinyiIndustry.getHycWnndAll());
  159. xinyiBigTableHour.setGyClP04Ycz(tXinyiIndustry.getCLP04YCZ());
  160. }
  161. private void addCalculate2BigTable(TXinyiBigTableHour xinyiBigTableHour, TXinyiCalculate xinyiCalculate) {
  162. xinyiBigTableHour.setJsJsTdb(xinyiCalculate.getJsTdb());
  163. xinyiBigTableHour.setJsJsTlb(xinyiCalculate.getJsTlb());
  164. xinyiBigTableHour.setJsJsBodBCod(xinyiCalculate.getJsBodBCod());
  165. xinyiBigTableHour.setJsYyqHrt(xinyiCalculate.getYyqHrt());
  166. xinyiBigTableHour.setJsQyqHrt(xinyiCalculate.getQyqHrt());
  167. xinyiBigTableHour.setJsHyqHrt(xinyiCalculate.getHyqHrt());
  168. xinyiBigTableHour.setJsHfxwnndzb(xinyiCalculate.getHFXWNNDZB());
  169. xinyiBigTableHour.setJsFM(xinyiCalculate.getFM());
  170. xinyiBigTableHour.setJsGsls(xinyiCalculate.getGSLS());
  171. xinyiBigTableHour.setJsGslsOne(xinyiCalculate.getGslsOne());
  172. xinyiBigTableHour.setJsGslsTwo(xinyiCalculate.getGslsTwo());
  173. xinyiBigTableHour.setJsXgsGsls(xinyiCalculate.getXgsGsls());
  174. xinyiBigTableHour.setJsXlcscBmfh(xinyiCalculate.getXlcscBmfh());
  175. xinyiBigTableHour.setJsXlcscHrt(xinyiCalculate.getXlcscHrt());
  176. xinyiBigTableHour.setJsCccdcBmfh(xinyiCalculate.getCccdcBmfh());
  177. xinyiBigTableHour.setJsCccdcHrt(xinyiCalculate.getCccdcHrt());
  178. xinyiBigTableHour.setJsEccBmfh(xinyiCalculate.getEccBmfh());
  179. xinyiBigTableHour.setJsEccHrt(xinyiCalculate.getEccHrt());
  180. xinyiBigTableHour.setJsEccGtfh(xinyiCalculate.getEccGtfh());
  181. xinyiBigTableHour.setJsClsnJcsj(xinyiCalculate.getClsnJcsj());
  182. xinyiBigTableHour.setJsXgcdcQsqSsls(xinyiCalculate.getXgcdcQsqSsls());
  183. xinyiBigTableHour.setJsWhlb(xinyiCalculate.getWHLB());
  184. xinyiBigTableHour.setJsNhlb(xinyiCalculate.getNHLB());
  185. xinyiBigTableHour.setJsWdscnl(xinyiCalculate.getWDSCNL());
  186. xinyiBigTableHour.setJsQsb(xinyiCalculate.getQSB());
  187. xinyiBigTableHour.setJsFcxsl(xinyiCalculate.getFCXSL());
  188. xinyiBigTableHour.setJsFcxslbl(xinyiCalculate.getFCXSLBL());
  189. //2024年7月11日15:57:20 外回流比采用新的公式计算
  190. xinyiBigTableHour.setJsWhlbRN(xinyiCalculate.getWhlRN());
  191. }
  192. /**
  193. * 额外计算一下预测的准确度
  194. * @param tXinyiIndustry
  195. */
  196. private void updateForecastComparisonByIndustry(TXinyiIndustry tXinyiIndustry) {
  197. BigDecimal csCod = tXinyiIndustry.getCsCod();
  198. BigDecimal csSs = tXinyiIndustry.getCsSs();
  199. BigDecimal csTn = tXinyiIndustry.getCsTn();
  200. BigDecimal csTp = tXinyiIndustry.getCsTp();
  201. BigDecimal csNh3 = tXinyiIndustry.getCsNh3();
  202. String testHour = tXinyiIndustry.getTestHour();
  203. //时间段1
  204. List<TXinyiForecastComparison> tXinyiForecastComparisons1 = this.xinyiForecastComparisonMapper.selectTXinyiForecastComparisonList(TXinyiForecastComparison.builder().forecastTimeOne(testHour).build());
  205. if(!CollectionUtils.isEmpty(tXinyiForecastComparisons1)){
  206. for (TXinyiForecastComparison tXinyiForecastComparison : tXinyiForecastComparisons1) {
  207. BigDecimal hsForecastOne = tXinyiForecastComparison.getHsForecastOne();
  208. BigDecimal yyForecastOne = tXinyiForecastComparison.getYyForecastOne();
  209. if(BusinessEnum.BigModelForecastEnum.COD.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())){
  210. tXinyiForecastComparison.setRealOne(csCod);
  211. if(!Objects.isNull(csCod) && csCod.compareTo(BigDecimal.ZERO) > 0){
  212. if(!Objects.isNull(hsForecastOne)){
  213. BigDecimal subtract = csCod.subtract(hsForecastOne);
  214. tXinyiForecastComparison.setHsErrorRateOne((subtract).divide(csCod, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  215. tXinyiForecastComparison.setHsOneSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  216. }
  217. if(!Objects.isNull(yyForecastOne)){
  218. BigDecimal subtract = csCod.subtract(yyForecastOne);
  219. tXinyiForecastComparison.setYyErrorRateOne((subtract).divide(csCod, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  220. tXinyiForecastComparison.setYyOneSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  221. }
  222. }
  223. }
  224. if(BusinessEnum.BigModelForecastEnum.SS.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())){
  225. tXinyiForecastComparison.setRealOne(csSs);
  226. if(!Objects.isNull(csSs) && csSs.compareTo(BigDecimal.ZERO) > 0){
  227. if(!Objects.isNull(hsForecastOne)){
  228. BigDecimal subtract = csSs.subtract(hsForecastOne);
  229. tXinyiForecastComparison.setHsErrorRateOne((subtract).divide(csSs, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  230. tXinyiForecastComparison.setHsOneSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  231. }
  232. if(!Objects.isNull(yyForecastOne)){
  233. BigDecimal subtract = csSs.subtract(yyForecastOne);
  234. tXinyiForecastComparison.setYyErrorRateOne((subtract).divide(csSs, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  235. tXinyiForecastComparison.setYyOneSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  236. }
  237. }
  238. }
  239. //2024年6月21日16:28:56 tn总氮的预测用xsy1和xsy2计算 并从化验库获取
  240. /*if(TN.equals(tXinyiForecastComparison.getCategory())){
  241. tXinyiForecastComparison.setRealOne(csTn);
  242. if(!Objects.isNull(csTn) && csTn.compareTo(BigDecimal.ZERO) > 0){
  243. if(!Objects.isNull(hsForecastOne))
  244. tXinyiForecastComparison.setHsErrorRateOne((csTn.subtract(hsForecastOne)).divide(csTn, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  245. if(!Objects.isNull(yyForecastOne))
  246. tXinyiForecastComparison.setYyErrorRateOne((csTn.subtract(yyForecastOne)).divide(csTn, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  247. }
  248. }*/
  249. //2024年6月20日11:16:55 出水总磷的真实值从化验室获取
  250. /*if(TP.equals(tXinyiForecastComparison.getCategory())){
  251. tXinyiForecastComparison.setRealOne(csTp);
  252. if(!Objects.isNull(csTp) && csTp.compareTo(BigDecimal.ZERO) > 0){
  253. tXinyiForecastComparison.setHsErrorRateOne((csTp.subtract(hsForecastOne)).divide(csTp, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  254. tXinyiForecastComparison.setYyErrorRateOne((csTp.subtract(yyForecastOne)).divide(csTp, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  255. }
  256. }*/
  257. if(BusinessEnum.BigModelForecastEnum.NH3.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())){
  258. tXinyiForecastComparison.setRealOne(csNh3);
  259. if(!Objects.isNull(csNh3) && csNh3.compareTo(BigDecimal.ZERO) > 0){
  260. if(!Objects.isNull(hsForecastOne)){
  261. BigDecimal subtract = csNh3.subtract(hsForecastOne);
  262. tXinyiForecastComparison.setHsErrorRateOne((subtract).divide(csNh3, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  263. tXinyiForecastComparison.setHsOneSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  264. }
  265. if(!Objects.isNull(yyForecastOne)){
  266. BigDecimal subtract = csNh3.subtract(yyForecastOne);
  267. tXinyiForecastComparison.setYyErrorRateOne((subtract).divide(csNh3, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  268. tXinyiForecastComparison.setYyOneSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  269. }
  270. }
  271. }
  272. this.xinyiForecastComparisonMapper.updateTXinyiForecastComparison(tXinyiForecastComparison);
  273. }
  274. }
  275. //时间段2
  276. List<TXinyiForecastComparison> tXinyiForecastComparisons2 = this.xinyiForecastComparisonMapper.selectTXinyiForecastComparisonList(TXinyiForecastComparison.builder().forecastTimeTwo(testHour).build());
  277. if(!CollectionUtils.isEmpty(tXinyiForecastComparisons2)){
  278. for (TXinyiForecastComparison tXinyiForecastComparison : tXinyiForecastComparisons2) {
  279. BigDecimal hsForecastTwo = tXinyiForecastComparison.getHsForecastTwo();
  280. BigDecimal yyForecastTwo = tXinyiForecastComparison.getYyForecastTwo();
  281. if(BusinessEnum.BigModelForecastEnum.COD.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())){
  282. tXinyiForecastComparison.setRealTwo(csCod);
  283. if(!Objects.isNull(csCod) && csCod.compareTo(BigDecimal.ZERO) > 0){
  284. if(!Objects.isNull(hsForecastTwo)){
  285. BigDecimal subtract = csCod.subtract(hsForecastTwo);
  286. tXinyiForecastComparison.setHsErrorRateTwo((subtract).divide(csCod, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  287. tXinyiForecastComparison.setHsTwoSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  288. }
  289. if(!Objects.isNull(yyForecastTwo)){
  290. BigDecimal subtract = csCod.subtract(yyForecastTwo);
  291. tXinyiForecastComparison.setYyErrorRateTwo((subtract).divide(csCod, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  292. tXinyiForecastComparison.setYyTwoSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  293. }
  294. }
  295. }
  296. if(BusinessEnum.BigModelForecastEnum.SS.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())){
  297. tXinyiForecastComparison.setRealTwo(csSs);
  298. if(!Objects.isNull(csSs) && csSs.compareTo(BigDecimal.ZERO) > 0){
  299. if(!Objects.isNull(hsForecastTwo)){
  300. BigDecimal subtract = csSs.subtract(hsForecastTwo);
  301. tXinyiForecastComparison.setHsErrorRateTwo((subtract).divide(csSs, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  302. tXinyiForecastComparison.setHsTwoSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  303. }
  304. if(!Objects.isNull(yyForecastTwo)){
  305. BigDecimal subtract = csSs.subtract(yyForecastTwo);
  306. tXinyiForecastComparison.setYyErrorRateTwo((subtract).divide(csSs, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  307. tXinyiForecastComparison.setYyTwoSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  308. }
  309. }
  310. }
  311. /*if(TN.equals(tXinyiForecastComparison.getCategory())){
  312. tXinyiForecastComparison.setRealTwo(csTn);
  313. if(!Objects.isNull(csTn) && csTn.compareTo(BigDecimal.ZERO) > 0){
  314. if(!Objects.isNull(hsForecastTwo))
  315. tXinyiForecastComparison.setHsErrorRateTwo((csTn.subtract(hsForecastTwo)).divide(csTn, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  316. if(!Objects.isNull(yyForecastTwo))
  317. tXinyiForecastComparison.setYyErrorRateTwo((csTn.subtract(yyForecastTwo)).divide(csTn, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  318. }
  319. }*/
  320. //2024年6月20日11:16:55 出水总磷的真实值从化验室获取
  321. /*if(TP.equals(tXinyiForecastComparison.getCategory())){
  322. tXinyiForecastComparison.setRealTwo(csTp);
  323. if(!Objects.isNull(csTp) && csTp.compareTo(BigDecimal.ZERO) > 0){
  324. tXinyiForecastComparison.setHsErrorRateTwo((csTp.subtract(hsForecastTwo)).divide(csTp, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  325. tXinyiForecastComparison.setYyErrorRateTwo((csTp.subtract(yyForecastTwo)).divide(csTp, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  326. }
  327. }*/
  328. if(BusinessEnum.BigModelForecastEnum.NH3.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())){
  329. tXinyiForecastComparison.setRealTwo(csNh3);
  330. if(!Objects.isNull(csNh3) && csNh3.compareTo(BigDecimal.ZERO) > 0){
  331. if(!Objects.isNull(hsForecastTwo)){
  332. BigDecimal subtract = csNh3.subtract(hsForecastTwo);
  333. tXinyiForecastComparison.setHsErrorRateTwo((subtract).divide(csNh3, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  334. tXinyiForecastComparison.setHsTwoSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  335. }
  336. if(!Objects.isNull(yyForecastTwo)){
  337. BigDecimal subtract = csNh3.subtract(yyForecastTwo);
  338. tXinyiForecastComparison.setYyErrorRateTwo((subtract).divide(csNh3, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  339. tXinyiForecastComparison.setYyTwoSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  340. }
  341. }
  342. }
  343. this.xinyiForecastComparisonMapper.updateTXinyiForecastComparison(tXinyiForecastComparison);
  344. }
  345. }
  346. //时间段3
  347. List<TXinyiForecastComparison> tXinyiForecastComparisons3 = this.xinyiForecastComparisonMapper.selectTXinyiForecastComparisonList(TXinyiForecastComparison.builder().forecastTimeThree(testHour).build());
  348. if(!CollectionUtils.isEmpty(tXinyiForecastComparisons3)){
  349. for (TXinyiForecastComparison tXinyiForecastComparison : tXinyiForecastComparisons3) {
  350. BigDecimal hsForecastThree = tXinyiForecastComparison.getHsForecastThree();
  351. BigDecimal yyForecastThree = tXinyiForecastComparison.getYyForecastThree();
  352. if(BusinessEnum.BigModelForecastEnum.COD.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())){
  353. tXinyiForecastComparison.setRealThree(csCod);
  354. if(!Objects.isNull(csCod) && csCod.compareTo(BigDecimal.ZERO) > 0){
  355. if(!Objects.isNull(hsForecastThree)){
  356. BigDecimal subtract = csCod.subtract(hsForecastThree);
  357. tXinyiForecastComparison.setHsErrorRateThree((subtract).divide(csCod, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  358. tXinyiForecastComparison.setHsThreeSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  359. }
  360. if(!Objects.isNull(yyForecastThree)){
  361. BigDecimal subtract = csCod.subtract(yyForecastThree);
  362. tXinyiForecastComparison.setYyErrorRateThree((subtract).divide(csCod, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  363. tXinyiForecastComparison.setYyThreeSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  364. }
  365. }
  366. }
  367. if(BusinessEnum.BigModelForecastEnum.SS.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())){
  368. tXinyiForecastComparison.setRealThree(csSs);
  369. if(!Objects.isNull(csSs) && csSs.compareTo(BigDecimal.ZERO) > 0){
  370. if(!Objects.isNull(hsForecastThree)){
  371. BigDecimal subtract = csSs.subtract(hsForecastThree);
  372. tXinyiForecastComparison.setHsErrorRateThree((subtract).divide(csSs, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  373. tXinyiForecastComparison.setHsThreeSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  374. }
  375. if(!Objects.isNull(yyForecastThree)){
  376. BigDecimal subtract = csSs.subtract(yyForecastThree);
  377. tXinyiForecastComparison.setYyErrorRateThree((subtract).divide(csSs, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  378. tXinyiForecastComparison.setYyThreeSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  379. }
  380. }
  381. }
  382. /*if(TN.equals(tXinyiForecastComparison.getCategory())){
  383. tXinyiForecastComparison.setRealThree(csTn);
  384. if(!Objects.isNull(csTn) && csTn.compareTo(BigDecimal.ZERO) > 0){
  385. if(!Objects.isNull(hsForecastThree))
  386. tXinyiForecastComparison.setHsErrorRateThree((csTn.subtract(hsForecastThree)).divide(csTn, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  387. if(!Objects.isNull(yyForecastThree))
  388. tXinyiForecastComparison.setYyErrorRateThree((csTn.subtract(yyForecastThree)).divide(csTn, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  389. }
  390. }*/
  391. //2024年6月20日11:16:55 出水总磷的真实值从化验室获取
  392. /*if(TP.equals(tXinyiForecastComparison.getCategory())){
  393. tXinyiForecastComparison.setRealThree(csTp);
  394. if(!Objects.isNull(csTp) && csTp.compareTo(BigDecimal.ZERO) > 0){
  395. tXinyiForecastComparison.setHsErrorRateThree((csTp.subtract(hsForecastThree)).divide(csTp, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  396. tXinyiForecastComparison.setYyErrorRateThree((csTp.subtract(yyForecastThree)).divide(csTp, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  397. }
  398. }*/
  399. if(BusinessEnum.BigModelForecastEnum.NH3.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())){
  400. tXinyiForecastComparison.setRealThree(csNh3);
  401. if(!Objects.isNull(csNh3) && csNh3.compareTo(BigDecimal.ZERO) > 0){
  402. if(!Objects.isNull(hsForecastThree)){
  403. BigDecimal subtract = csNh3.subtract(hsForecastThree);
  404. tXinyiForecastComparison.setHsErrorRateThree((subtract).divide(csNh3, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  405. tXinyiForecastComparison.setHsThreeSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  406. }
  407. if(!Objects.isNull(yyForecastThree)){
  408. BigDecimal subtract = csNh3.subtract(yyForecastThree);
  409. tXinyiForecastComparison.setYyErrorRateThree((subtract).divide(csNh3, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  410. tXinyiForecastComparison.setYyThreeSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  411. }
  412. }
  413. }
  414. this.xinyiForecastComparisonMapper.updateTXinyiForecastComparison(tXinyiForecastComparison);
  415. }
  416. }
  417. }
  418. /**
  419. * 额外计算一下预测的准确度
  420. * @param tXinyiRobot
  421. */
  422. public void updateForecastComparisonByRobot(TXinyiRobot tXinyiRobot) {
  423. BigDecimal no3Hlj1Jqr = tXinyiRobot.getNo3Hlj1Jqr();
  424. BigDecimal no3Hlj2Jqr = tXinyiRobot.getNo3Hlj2Jqr();
  425. String testHour = tXinyiRobot.getTestHour();
  426. BigDecimal tpRccJqr = tXinyiRobot.getTpRccJqr();
  427. BigDecimal csTp = null;
  428. // BigDecimal csTn = null;
  429. if(!Objects.isNull(tpRccJqr)){
  430. csTp = tpRccJqr.divide(ROBOT_HY_DIVIDE, NUMBER_SCALE_4, RoundingMode.HALF_UP);
  431. //2024年6月20日13:44:26 先不计算,结果会更准确
  432. //2024年6月21日14:23:14 就是要 除以0.8
  433. // csTp = tpRccJqr;
  434. }
  435. //2024年7月12日16:29:00 需要单独计算了 csTn不再合并,分别从两个池子获取。。。
  436. // if(!Objects.isNull(no3Hlj1Jqr) && !Objects.isNull(no3Hlj2Jqr))
  437. // csTn = (no3Hlj1Jqr.add(no3Hlj2Jqr)).divide(DECIMAL_2, NUMBER_SCALE_4, RoundingMode.HALF_UP).divide(ROBOT_HY_DIVIDE, NUMBER_SCALE_4, RoundingMode.HALF_UP);
  438. //时间段1
  439. List<TXinyiForecastComparison> tXinyiForecastComparisons1 = this.xinyiForecastComparisonMapper.selectTXinyiForecastComparisonList(TXinyiForecastComparison.builder().forecastTimeOne(testHour).build());
  440. if(!CollectionUtils.isEmpty(tXinyiForecastComparisons1)){
  441. for (TXinyiForecastComparison tXinyiForecastComparison : tXinyiForecastComparisons1) {
  442. BigDecimal hsForecastOne = tXinyiForecastComparison.getHsForecastOne();
  443. BigDecimal yyForecastOne = tXinyiForecastComparison.getYyForecastOne();
  444. /*if(XSY1.equals(tXinyiForecastComparison.getCategory())){
  445. tXinyiForecastComparison.setRealOne(no3Hlj1Jqr);
  446. if(!Objects.isNull(no3Hlj1Jqr) && no3Hlj1Jqr.compareTo(BigDecimal.ZERO) > 0){
  447. if(!Objects.isNull(hsForecastOne))
  448. tXinyiForecastComparison.setHsErrorRateOne((no3Hlj1Jqr.subtract(hsForecastOne)).divide(no3Hlj1Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  449. if(!Objects.isNull(yyForecastOne))
  450. tXinyiForecastComparison.setYyErrorRateOne((no3Hlj1Jqr.subtract(yyForecastOne)).divide(no3Hlj1Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  451. }
  452. }
  453. if(XSY2.equals(tXinyiForecastComparison.getCategory())){
  454. tXinyiForecastComparison.setRealOne(no3Hlj2Jqr);
  455. if(!Objects.isNull(no3Hlj2Jqr) && no3Hlj2Jqr.compareTo(BigDecimal.ZERO) > 0){
  456. if(!Objects.isNull(hsForecastOne))
  457. tXinyiForecastComparison.setHsErrorRateOne((no3Hlj2Jqr.subtract(hsForecastOne)).divide(no3Hlj2Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  458. if(!Objects.isNull(yyForecastOne))
  459. tXinyiForecastComparison.setYyErrorRateOne((no3Hlj2Jqr.subtract(yyForecastOne)).divide(no3Hlj2Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  460. }
  461. }*/
  462. //2024年6月21日17:04:01 总氮的真实值也从化验室获取
  463. //2024年7月12日10:31:29 tn又改成xsy1和xsy2了。。。
  464. if(BusinessEnum.BigModelForecastEnum.TN.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())
  465. || BusinessEnum.BigModelForecastEnum.XSY1.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())){
  466. tXinyiForecastComparison.setRealOne(no3Hlj1Jqr);
  467. if(!Objects.isNull(no3Hlj1Jqr) && no3Hlj1Jqr.compareTo(BigDecimal.ZERO) > 0){
  468. if(!Objects.isNull(hsForecastOne)){
  469. BigDecimal subtract = no3Hlj1Jqr.subtract(hsForecastOne);
  470. tXinyiForecastComparison.setHsErrorRateOne((subtract).divide(no3Hlj1Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  471. tXinyiForecastComparison.setHsOneSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  472. }
  473. if(!Objects.isNull(yyForecastOne)){
  474. BigDecimal subtract = no3Hlj1Jqr.subtract(yyForecastOne);
  475. tXinyiForecastComparison.setYyErrorRateOne((subtract).divide(no3Hlj1Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  476. tXinyiForecastComparison.setYyOneSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  477. }
  478. }
  479. }
  480. if(BusinessEnum.BigModelForecastEnum.TN.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())
  481. || BusinessEnum.BigModelForecastEnum.XSY2.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())){
  482. tXinyiForecastComparison.setRealOne(no3Hlj2Jqr);
  483. if(!Objects.isNull(no3Hlj2Jqr) && no3Hlj2Jqr.compareTo(BigDecimal.ZERO) > 0){
  484. if(!Objects.isNull(hsForecastOne)){
  485. BigDecimal subtract = no3Hlj2Jqr.subtract(hsForecastOne);
  486. tXinyiForecastComparison.setHsErrorRateOne((subtract).divide(no3Hlj2Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  487. tXinyiForecastComparison.setHsOneSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  488. }
  489. if(!Objects.isNull(yyForecastOne)){
  490. BigDecimal subtract = no3Hlj2Jqr.subtract(yyForecastOne);
  491. tXinyiForecastComparison.setYyErrorRateOne((subtract).divide(no3Hlj2Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  492. tXinyiForecastComparison.setYyOneSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  493. }
  494. }
  495. }
  496. //2024年6月20日11:14:42 总磷的真实值也从化验室获取
  497. //2024年7月11日17:08:51 tp改名字了。。。
  498. if(BusinessEnum.BigModelForecastEnum.TP.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())
  499. || BusinessEnum.BigModelForecastEnum.ZLSY.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())){
  500. tXinyiForecastComparison.setRealOne(csTp);
  501. if(!Objects.isNull(csTp) && csTp.compareTo(BigDecimal.ZERO) > 0){
  502. if(!Objects.isNull(hsForecastOne)){
  503. BigDecimal subtract = csTp.subtract(hsForecastOne);
  504. tXinyiForecastComparison.setHsErrorRateOne((subtract).divide(csTp, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  505. tXinyiForecastComparison.setHsOneSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  506. }
  507. if(!Objects.isNull(yyForecastOne)){
  508. BigDecimal subtract = csTp.subtract(yyForecastOne);
  509. tXinyiForecastComparison.setYyErrorRateOne((subtract).divide(csTp, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  510. tXinyiForecastComparison.setYyOneSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  511. }
  512. }
  513. }
  514. this.xinyiForecastComparisonMapper.updateTXinyiForecastComparison(tXinyiForecastComparison);
  515. }
  516. }
  517. //时间段2
  518. List<TXinyiForecastComparison> tXinyiForecastComparisons2 = this.xinyiForecastComparisonMapper.selectTXinyiForecastComparisonList(TXinyiForecastComparison.builder().forecastTimeTwo(testHour).build());
  519. if(!CollectionUtils.isEmpty(tXinyiForecastComparisons2)){
  520. for (TXinyiForecastComparison tXinyiForecastComparison : tXinyiForecastComparisons2) {
  521. BigDecimal hsForecastTwo = tXinyiForecastComparison.getHsForecastTwo();
  522. BigDecimal yyForecastTwo = tXinyiForecastComparison.getYyForecastTwo();
  523. /*if(XSY1.equals(tXinyiForecastComparison.getCategory())){
  524. tXinyiForecastComparison.setRealTwo(no3Hlj1Jqr);
  525. if(!Objects.isNull(no3Hlj1Jqr) && no3Hlj1Jqr.compareTo(BigDecimal.ZERO) > 0){
  526. if(!Objects.isNull(hsForecastTwo))
  527. tXinyiForecastComparison.setHsErrorRateTwo((no3Hlj1Jqr.subtract(hsForecastTwo)).divide(no3Hlj1Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  528. if(!Objects.isNull(yyForecastTwo))
  529. tXinyiForecastComparison.setYyErrorRateTwo((no3Hlj1Jqr.subtract(yyForecastTwo)).divide(no3Hlj1Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  530. }
  531. }
  532. if(XSY2.equals(tXinyiForecastComparison.getCategory())){
  533. tXinyiForecastComparison.setRealTwo(no3Hlj2Jqr);
  534. if(!Objects.isNull(no3Hlj2Jqr) && no3Hlj2Jqr.compareTo(BigDecimal.ZERO) > 0){
  535. if(!Objects.isNull(hsForecastTwo))
  536. tXinyiForecastComparison.setHsErrorRateTwo((no3Hlj2Jqr.subtract(hsForecastTwo)).divide(no3Hlj2Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  537. if(!Objects.isNull(yyForecastTwo))
  538. tXinyiForecastComparison.setYyErrorRateTwo((no3Hlj2Jqr.subtract(yyForecastTwo)).divide(no3Hlj2Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  539. }
  540. }*/
  541. //2024年6月21日17:04:01 总氮的真实值也从化验室获取
  542. //2024年7月12日10:31:29 tn又改成xsy1和xsy2了。。。
  543. if(BusinessEnum.BigModelForecastEnum.TN.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())
  544. || BusinessEnum.BigModelForecastEnum.XSY1.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())){
  545. tXinyiForecastComparison.setRealTwo(no3Hlj1Jqr);
  546. if(!Objects.isNull(no3Hlj1Jqr) && no3Hlj1Jqr.compareTo(BigDecimal.ZERO) > 0){
  547. if(!Objects.isNull(hsForecastTwo)){
  548. BigDecimal subtract = no3Hlj1Jqr.subtract(hsForecastTwo);
  549. tXinyiForecastComparison.setHsErrorRateTwo((subtract).divide(no3Hlj1Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  550. tXinyiForecastComparison.setHsTwoSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  551. }
  552. if(!Objects.isNull(yyForecastTwo)){
  553. BigDecimal subtract = no3Hlj1Jqr.subtract(yyForecastTwo);
  554. tXinyiForecastComparison.setYyErrorRateTwo((subtract).divide(no3Hlj1Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  555. tXinyiForecastComparison.setYyTwoSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  556. }
  557. }
  558. }
  559. if(BusinessEnum.BigModelForecastEnum.TN.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())
  560. || BusinessEnum.BigModelForecastEnum.XSY2.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())){
  561. tXinyiForecastComparison.setRealTwo(no3Hlj2Jqr);
  562. if(!Objects.isNull(no3Hlj2Jqr) && no3Hlj2Jqr.compareTo(BigDecimal.ZERO) > 0){
  563. if(!Objects.isNull(hsForecastTwo)){
  564. BigDecimal subtract = no3Hlj2Jqr.subtract(hsForecastTwo);
  565. tXinyiForecastComparison.setHsErrorRateTwo((subtract).divide(no3Hlj2Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  566. tXinyiForecastComparison.setHsTwoSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  567. }
  568. if(!Objects.isNull(yyForecastTwo)){
  569. BigDecimal subtract = no3Hlj2Jqr.subtract(yyForecastTwo);
  570. tXinyiForecastComparison.setYyErrorRateTwo((subtract).divide(no3Hlj2Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  571. tXinyiForecastComparison.setYyTwoSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  572. }
  573. }
  574. }
  575. //2024年6月20日11:14:42 总磷的真实值也从化验室获取
  576. //2024年7月11日17:08:51 tp改名字了。。。
  577. if(BusinessEnum.BigModelForecastEnum.TP.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())
  578. || BusinessEnum.BigModelForecastEnum.ZLSY.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())){
  579. tXinyiForecastComparison.setRealTwo(csTp);
  580. if(!Objects.isNull(csTp) && csTp.compareTo(BigDecimal.ZERO) > 0){
  581. if(!Objects.isNull(hsForecastTwo)){
  582. BigDecimal subtract = csTp.subtract(hsForecastTwo);
  583. tXinyiForecastComparison.setHsErrorRateTwo((subtract).divide(csTp, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  584. tXinyiForecastComparison.setHsTwoSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  585. }
  586. if(!Objects.isNull(yyForecastTwo)){
  587. BigDecimal subtract = csTp.subtract(yyForecastTwo);
  588. tXinyiForecastComparison.setYyErrorRateTwo((subtract).divide(csTp, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  589. tXinyiForecastComparison.setYyTwoSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  590. }
  591. }
  592. }
  593. this.xinyiForecastComparisonMapper.updateTXinyiForecastComparison(tXinyiForecastComparison);
  594. }
  595. }
  596. //时间段3
  597. List<TXinyiForecastComparison> tXinyiForecastComparisons3 = this.xinyiForecastComparisonMapper.selectTXinyiForecastComparisonList(TXinyiForecastComparison.builder().forecastTimeThree(testHour).build());
  598. if(!CollectionUtils.isEmpty(tXinyiForecastComparisons3)){
  599. for (TXinyiForecastComparison tXinyiForecastComparison : tXinyiForecastComparisons3) {
  600. BigDecimal hsForecastThree = tXinyiForecastComparison.getHsForecastThree();
  601. BigDecimal yyForecastThree = tXinyiForecastComparison.getYyForecastThree();
  602. /*if(XSY1.equals(tXinyiForecastComparison.getCategory())){
  603. tXinyiForecastComparison.setRealThree(no3Hlj1Jqr);
  604. if(!Objects.isNull(no3Hlj1Jqr) && no3Hlj1Jqr.compareTo(BigDecimal.ZERO) > 0){
  605. if(!Objects.isNull(hsForecastThree))
  606. tXinyiForecastComparison.setHsErrorRateThree((no3Hlj1Jqr.subtract(hsForecastThree)).divide(no3Hlj1Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  607. if(!Objects.isNull(yyForecastThree))
  608. tXinyiForecastComparison.setYyErrorRateThree((no3Hlj1Jqr.subtract(yyForecastThree)).divide(no3Hlj1Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  609. }
  610. }
  611. if(XSY2.equals(tXinyiForecastComparison.getCategory())){
  612. tXinyiForecastComparison.setRealThree(no3Hlj2Jqr);
  613. if(!Objects.isNull(no3Hlj2Jqr) && no3Hlj2Jqr.compareTo(BigDecimal.ZERO) > 0){
  614. if(!Objects.isNull(hsForecastThree))
  615. tXinyiForecastComparison.setHsErrorRateThree((no3Hlj2Jqr.subtract(hsForecastThree)).divide(no3Hlj2Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  616. if(!Objects.isNull(yyForecastThree))
  617. tXinyiForecastComparison.setYyErrorRateThree((no3Hlj2Jqr.subtract(yyForecastThree)).divide(no3Hlj2Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  618. }
  619. }*/
  620. //2024年6月21日17:04:01 总氮的真实值也从化验室获取
  621. //2024年7月12日10:31:29 tn又改成xsy1和xsy2了。。。
  622. if(BusinessEnum.BigModelForecastEnum.TN.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())
  623. || BusinessEnum.BigModelForecastEnum.XSY1.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())){
  624. tXinyiForecastComparison.setRealThree(no3Hlj1Jqr);
  625. if(!Objects.isNull(no3Hlj1Jqr) && no3Hlj1Jqr.compareTo(BigDecimal.ZERO) > 0){
  626. if(!Objects.isNull(hsForecastThree)){
  627. BigDecimal subtract = no3Hlj1Jqr.subtract(hsForecastThree);
  628. tXinyiForecastComparison.setHsErrorRateThree((subtract).divide(no3Hlj1Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  629. tXinyiForecastComparison.setHsThreeSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  630. }
  631. if(!Objects.isNull(yyForecastThree)){
  632. BigDecimal subtract = no3Hlj1Jqr.subtract(yyForecastThree);
  633. tXinyiForecastComparison.setYyErrorRateThree((subtract).divide(no3Hlj1Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  634. tXinyiForecastComparison.setYyThreeSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  635. }
  636. }
  637. }
  638. if(BusinessEnum.BigModelForecastEnum.TN.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())
  639. || BusinessEnum.BigModelForecastEnum.XSY2.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())){
  640. tXinyiForecastComparison.setRealThree(no3Hlj2Jqr);
  641. if(!Objects.isNull(no3Hlj2Jqr) && no3Hlj2Jqr.compareTo(BigDecimal.ZERO) > 0){
  642. if(!Objects.isNull(hsForecastThree)){
  643. BigDecimal subtract = no3Hlj2Jqr.subtract(hsForecastThree);
  644. tXinyiForecastComparison.setHsErrorRateThree((subtract).divide(no3Hlj2Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  645. tXinyiForecastComparison.setHsThreeSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  646. }
  647. if(!Objects.isNull(yyForecastThree)){
  648. BigDecimal subtract = no3Hlj2Jqr.subtract(yyForecastThree);
  649. tXinyiForecastComparison.setYyErrorRateThree((subtract).divide(no3Hlj2Jqr, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  650. tXinyiForecastComparison.setYyThreeSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  651. }
  652. }
  653. }
  654. //2024年6月20日11:14:42 总磷的真实值也从化验室获取
  655. //2024年7月11日17:08:51 tp改名字了。。。
  656. if(BusinessEnum.BigModelForecastEnum.TP.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())
  657. || BusinessEnum.BigModelForecastEnum.ZLSY.getCode().equalsIgnoreCase(tXinyiForecastComparison.getCategory())){
  658. tXinyiForecastComparison.setRealThree(csTp);
  659. if(!Objects.isNull(csTp) && csTp.compareTo(BigDecimal.ZERO) > 0){
  660. if(!Objects.isNull(hsForecastThree)){
  661. BigDecimal subtract = csTp.subtract(hsForecastThree);
  662. tXinyiForecastComparison.setHsErrorRateThree((subtract).divide(csTp, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  663. tXinyiForecastComparison.setHsThreeSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  664. }
  665. if(!Objects.isNull(yyForecastThree)){
  666. BigDecimal subtract = csTp.subtract(yyForecastThree);
  667. tXinyiForecastComparison.setYyErrorRateThree((subtract).divide(csTp, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  668. tXinyiForecastComparison.setYyThreeSubtract(DecimalUtils.getAbsAndScale(subtract, INT_2));
  669. }
  670. }
  671. }
  672. this.xinyiForecastComparisonMapper.updateTXinyiForecastComparison(tXinyiForecastComparison);
  673. }
  674. }
  675. }
  676. private TXinyiCalculate addCalculateByIndustry(TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
  677. //获取的一些配置信息
  678. BigDecimal nhlbqdsl = normConfig.getNHLBQDSL();
  679. BigDecimal nhlbdsjll = normConfig.getNHLBDSJLL();
  680. BigDecimal nhlbgzxl = normConfig.getNHLBGZXL();
  681. BigDecimal whlbqdsl = normConfig.getWHLBQDSL();
  682. BigDecimal whlbdsjll = normConfig.getWHLBDSJLL();
  683. BigDecimal whlbgzxl = normConfig.getWHLBGZXL();
  684. BigDecimal gfjgzts = normConfig.getGFJGZTS();
  685. BigDecimal gfjckll = normConfig.getGFJCKLL();
  686. BigDecimal fcxbsjll = normConfig.getFCXBSJLL();
  687. BigDecimal fclbsl = normConfig.getFCLBSL();
  688. BigDecimal fcxbgzxl = normConfig.getFCXBGZXL();
  689. //结算结果
  690. TXinyiCalculate tXinyiCalculate = new TXinyiCalculate();
  691. //时间相关
  692. tXinyiCalculate.setTestDate(tXinyiIndustry.getTestDate());
  693. tXinyiCalculate.setTestHour(tXinyiIndustry.getTestHour());
  694. tXinyiCalculate.setTestTime(tXinyiIndustry.getTestTime());
  695. //获取最新的一条日报信息
  696. TXinyiDaily tXinyiDaily = this.xinyiDailyMapper.selectNewestData();
  697. if(Objects.isNull(tXinyiDaily)){
  698. //理论不会出现
  699. log.error("没有日报数据");
  700. return null;
  701. }
  702. BigDecimal jsBod5 = tXinyiDaily.getJsBod5();
  703. BigDecimal csBod5 = tXinyiDaily.getCsBod5();
  704. BigDecimal whlR = tXinyiDaily.getWhlR();
  705. BigDecimal oneMlvss = tXinyiDaily.getShcHyOneMlvss();
  706. BigDecimal twoMlvss = tXinyiDaily.getShcHyTwoMlvss();
  707. BigDecimal avgMlvss = null;
  708. if(!Objects.isNull(oneMlvss) && !Objects.isNull(twoMlvss))
  709. avgMlvss = ((oneMlvss.add(twoMlvss)).divide(new BigDecimal(2),4, RoundingMode.HALF_UP));
  710. BigDecimal oneMlss = tXinyiDaily.getShcHyOneMlss();
  711. BigDecimal twoMlss = tXinyiDaily.getShcHyTwoMlss();
  712. BigDecimal avgMlss = null;
  713. if(!Objects.isNull(oneMlss) && !Objects.isNull(twoMlss))
  714. avgMlss = ((oneMlss.add(twoMlss)).divide(new BigDecimal(NUMBER_2),NUMBER_SCALE_4, RoundingMode.HALF_UP));
  715. //工业库的指标
  716. BigDecimal jsTn = tXinyiIndustry.getJsTn();
  717. BigDecimal jsTp = tXinyiIndustry.getJsTp();
  718. BigDecimal jsCod = tXinyiIndustry.getJsCod();
  719. BigDecimal jsSlq = tXinyiIndustry.getJsSlq();
  720. //计算
  721. if(!Objects.isNull(jsBod5)){
  722. if(!Objects.isNull(jsTn) && jsTn.compareTo(BigDecimal.ZERO) > 0)
  723. tXinyiCalculate.setJsTdb(jsBod5.divide(jsTn, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  724. if(!Objects.isNull(jsTp) && jsTp.compareTo(BigDecimal.ZERO) > 0)
  725. tXinyiCalculate.setJsTlb(jsBod5.divide(jsTp, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  726. if(!Objects.isNull(jsCod) && jsCod.compareTo(BigDecimal.ZERO) > 0)
  727. tXinyiCalculate.setJsBodBCod(jsBod5.divide(jsCod, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  728. if(!Objects.isNull(csBod5) && !Objects.isNull(avgMlss) && avgMlss.compareTo(BigDecimal.ZERO) > 0 && !Objects.isNull(jsSlq) && jsSlq.compareTo(BigDecimal.ZERO) > 0)
  729. tXinyiCalculate.setFM(new BigDecimal(ONE_DAY_HOURS).multiply((jsBod5.subtract(csBod5))).multiply(jsSlq).divide(avgMlss, NUMBER_SCALE_4, RoundingMode.HALF_UP).divide(new BigDecimal(F_M_LAST), NUMBER_SCALE_4, RoundingMode.HALF_UP));
  730. }
  731. if(!Objects.isNull(avgMlss) && avgMlss.compareTo(BigDecimal.ZERO) > 0 && !Objects.isNull(avgMlvss) && avgMlvss.compareTo(BigDecimal.ZERO) > 0)
  732. tXinyiCalculate.setHFXWNNDZB(avgMlvss.divide(avgMlss, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  733. if(!Objects.isNull(jsSlq) && jsSlq.compareTo(BigDecimal.ZERO) > 0){
  734. tXinyiCalculate.setYyqHrt(new BigDecimal(YYQ_TLSJ_FIRST).divide(jsSlq, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  735. tXinyiCalculate.setQyqHrt(new BigDecimal(QYQ_TLSJ_FIRST).divide(jsSlq, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  736. tXinyiCalculate.setHyqHrt(new BigDecimal(HYQ_TLSJ_FIRST).divide(jsSlq, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  737. // tXinyiCalculate.setGSLS(jsSlq.divide(new BigDecimal(NUMBER_3600), NUMBER_SCALE_4, RoundingMode.HALF_UP).multiply(new BigDecimal(NUMBER_4)).divide((new BigDecimal(Math.PI).multiply(new BigDecimal(NUMBER_0_0_0_3)).multiply(new BigDecimal(NUMBER_0_0_0_3)).multiply(new BigDecimal(NUMBER_4)).multiply(new BigDecimal(NUMBER_4)).multiply(new BigDecimal(NUMBER_97119))) , NUMBER_SCALE_4, RoundingMode.HALF_UP));
  738. tXinyiCalculate.setGSLS(jsSlq.divide(GSLS_LAST, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  739. tXinyiCalculate.setGslsOne(jsSlq.divide(new BigDecimal(NUMBER_3600), NUMBER_SCALE_4, RoundingMode.HALF_UP).multiply(new BigDecimal(Math.sqrt(Math.sin(Math.toRadians(DOUBLE_75))))).divide(GSLSONE_LAST, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  740. tXinyiCalculate.setGslsTwo(jsSlq.divide(new BigDecimal(NUMBER_3600), NUMBER_SCALE_4, RoundingMode.HALF_UP).multiply(new BigDecimal(Math.sqrt(Math.sin(Math.toRadians(DOUBLE_75))))).divide(GSLSTWO_LAST, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  741. tXinyiCalculate.setXgsGsls(jsSlq.divide(new BigDecimal(NUMBER_3600), NUMBER_SCALE_4, RoundingMode.HALF_UP).multiply(new BigDecimal(NUMBER_4)).divide((GSLS_XGS_LAST) , NUMBER_SCALE_4, RoundingMode.HALF_UP));
  742. tXinyiCalculate.setXlcscBmfh(jsSlq.divide(XLCSCBMFH_LAST, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  743. tXinyiCalculate.setXlcscHrt(XLCSCHRT_LAST.divide(jsSlq, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  744. tXinyiCalculate.setCccdcBmfh(jsSlq.divide(CCCDCBMFH_LAST, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  745. tXinyiCalculate.setCccdcHrt(CCCDCHRT_LAST.divide(jsSlq, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  746. tXinyiCalculate.setEccBmfh(jsSlq.divide(ECCBMFH_LAST, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  747. tXinyiCalculate.setEccHrt(ECCHRT_LAST.divide(jsSlq, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  748. //2024年7月1日17:16:16 计算逻辑调整
  749. tXinyiCalculate.setEccGtfh((new BigDecimal(1).add(whlR.divide(BigDecimal_100, NUMBER_SCALE_4, RoundingMode.HALF_UP))).multiply(jsSlq).multiply(new BigDecimal(NUMBER_24)).multiply(avgMlss).divide(ECCGTFH_LAST, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  750. tXinyiCalculate.setClsnJcsj(CLSNJCSJ_LAST.divide(jsSlq, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  751. tXinyiCalculate.setXgcdcQsqSsls(jsSlq.multiply(XGCDCQSQSSLS_LAST));
  752. //2024年6月29日15:18:01 增加几个新的计算指标
  753. if(!Objects.isNull(nhlbqdsl) && !Objects.isNull(nhlbdsjll) && !Objects.isNull(nhlbgzxl))
  754. tXinyiCalculate.setNHLB(nhlbqdsl.multiply(nhlbdsjll).multiply(nhlbgzxl).multiply(BigDecimal_100).divide(jsSlq, 4, RoundingMode.HALF_UP));
  755. if(!Objects.isNull(whlbqdsl) && !Objects.isNull(whlbdsjll) && !Objects.isNull(whlbgzxl))
  756. tXinyiCalculate.setWHLB(whlbqdsl.multiply(whlbdsjll).multiply(whlbgzxl).multiply(BigDecimal_100).divide(jsSlq, 4, RoundingMode.HALF_UP));
  757. if(!Objects.isNull(gfjgzts) && !Objects.isNull(gfjckll))
  758. tXinyiCalculate.setQSB(gfjgzts.multiply(gfjckll).divide(jsSlq, NUMBER_SCALE_4, RoundingMode.HALF_UP));
  759. if(!Objects.isNull(fcxbsjll) && !Objects.isNull(fclbsl) && !Objects.isNull(fcxbgzxl)){
  760. BigDecimal fcxsl = fcxbsjll.multiply(fclbsl).multiply(fcxbgzxl);
  761. tXinyiCalculate.setFCXSL(fcxsl);
  762. //2024年7月3日10:54:28 额外再增加一个字段
  763. tXinyiCalculate.setFCXSLBL(fcxsl.divide(jsSlq, NUMBER_SCALE_4, RoundingMode.HALF_UP).multiply(BigDecimal_100));
  764. }
  765. //2024年7月11日15:43:47 之前给的外回流比计算不准确,额外一个字段记录新的外回流比
  766. BigDecimal divide = jsSlq.divide(DECIMAL_2, NUMBER_SCALE_4, RoundingMode.HALF_UP);
  767. if(divide.compareTo(DECIMAL_1500) < 0)
  768. tXinyiCalculate.setWhlRN(DECIMAL_1200.divide(divide, NUMBER_SCALE_4, RoundingMode.HALF_UP).multiply(BigDecimal_100));
  769. else
  770. tXinyiCalculate.setWhlRN(DECIMAL_1620.divide(divide, NUMBER_SCALE_4, RoundingMode.HALF_UP).multiply(BigDecimal_100));
  771. }
  772. return tXinyiCalculate;
  773. }
  774. private void handleSHWarning(TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig, TXinyiCalculate xinyiCalculate) {
  775. //判断对应指标是否报警 然后调研大模型获取决策信息
  776. // BigDecimal jsSlq = tXinyiIndustry.getJsSlq();
  777. //内回流比报警
  778. // BigDecimal nhlbSjz = normConfig.getNhlbSjz();//400
  779. // BigDecimal nhlbnkSxz = normConfig.getNhlbnkSxz();//360
  780. BigDecimal nhlbnkXxz = normConfig.getNhlbnkXxz();//270
  781. // BigDecimal nhlbqdsl = normConfig.getNHLBQDSL();
  782. // BigDecimal nhlbdsjll = normConfig.getNHLBDSJLL();
  783. // BigDecimal nhlbgzxl = normConfig.getNHLBGZXL();
  784. /*BigDecimal nhlb = xinyiCalculate.getNHLB();
  785. if(!Objects.isNull(nhlb) && !Objects.isNull(nhlbnkXxz)){
  786. // BigDecimal divide = nhlbqdsl.multiply(nhlbdsjll).multiply(nhlbgzxl).divide(jsSlq, 4, RoundingMode.HALF_UP);
  787. handleXinYiWarningsSH(nhlbnkXxz, nhlb, BusinessEnum.WarningCategoryEnum.NHLB.getCode(), tXinyiIndustry, normConfig, true, BusinessEnum.BigModelUnitEnum.PERCENT.getCode());
  788. }*/
  789. //外回流比报警
  790. // BigDecimal whlbqdsl = normConfig.getWHLBQDSL();
  791. // BigDecimal whlbdsjll = normConfig.getWHLBDSJLL();
  792. // BigDecimal whlbgzxl = normConfig.getWHLBGZXL();
  793. BigDecimal whlbnkXxz = normConfig.getWhlbnkXxz();//75
  794. // BigDecimal whlbSjz = normConfig.getWhlbSjz();
  795. // BigDecimal whlb = xinyiCalculate.getWHLB();
  796. //2024年7月11日15:54:19 外回流比采用新的计算方式获取的值
  797. BigDecimal whlb = xinyiCalculate.getWhlRN();
  798. if(!Objects.isNull(whlb) && !Objects.isNull(whlbnkXxz)){
  799. handleXinYiWarningsSH(whlbnkXxz, whlb, BusinessEnum.WarningCategoryEnum.WHLB.getCode(), tXinyiIndustry, normConfig, true, BusinessEnum.BigModelUnitEnum.PERCENT.getCode());
  800. }
  801. //最新的一条日报数据
  802. /*List<TXinyiDaily> tXinyiDailies = this.xinyiDailyMapper.selectTXinyiDailyList(null);
  803. if(CollectionUtils.isEmpty(tXinyiDailies))
  804. return;//肯定不会出现这种情况 因为有很多历史数据了
  805. TXinyiDaily tXinyiDaily = tXinyiDailies.get(0);*/
  806. TXinyiDaily tXinyiDaily = this.xinyiDailyMapper.selectNewestData();
  807. if(Objects.isNull(tXinyiDaily))
  808. return;//肯定不会出现这种情况 因为有很多历史数据了 新水厂可能会有这个问题
  809. log.info("生化报警获取日报的最新的一条数据为{}", JSON.toJSONString(tXinyiDaily));
  810. //污泥浓度报警
  811. BigDecimal sjscgkz = null;
  812. //判断是否是夏季
  813. if(LocalDateTimeUtil.isSummer())
  814. sjscgkz = normConfig.getXjwnndXxz();
  815. else
  816. sjscgkz = normConfig.getDjwnndXxz();
  817. //#1
  818. BigDecimal oneMlss = tXinyiIndustry.getOneMlss();
  819. if(!Objects.isNull(oneMlss) && !Objects.isNull(sjscgkz)){
  820. handleXinYiWarningsSH(sjscgkz, oneMlss, BusinessEnum.WarningCategoryEnum.WNND_MLSS_1.getCode(), tXinyiIndustry, normConfig, true, BusinessEnum.BigModelUnitEnum.MG_L.getCode());
  821. }
  822. //#1
  823. BigDecimal twoMlss = tXinyiIndustry.getTwoMlss();
  824. if(!Objects.isNull(twoMlss) && !Objects.isNull(sjscgkz)){
  825. handleXinYiWarningsSH(sjscgkz, twoMlss, BusinessEnum.WarningCategoryEnum.WNND_MLSS_2.getCode(), tXinyiIndustry, normConfig, true, BusinessEnum.BigModelUnitEnum.MG_L.getCode());
  826. }
  827. //污泥负荷(需要从日报获取数据) 计算 + 部分数据从日报获取
  828. //污泥负荷=[Ls]=24*([BOD_in]-[BOD_off])*[Q_in]/[MLSS]/([V_hao]+[V_que]+[V_yan])/2 kgBOD/(kgMLSS·d)
  829. //2024年6月27日17:18:33 直接从计算结果拿到
  830. BigDecimal fm = xinyiCalculate.getFM();
  831. //2024年6月28日17:17:03 已经配置了新的
  832. BigDecimal wnfhnkXxz = normConfig.getWnfhnkXxz();
  833. if(!Objects.isNull(fm) && !Objects.isNull(wnfhnkXxz)){
  834. handleXinYiWarningsSH(wnfhnkXxz, fm, BusinessEnum.WarningCategoryEnum.WNFH.getCode(), tXinyiIndustry, normConfig, true, BusinessEnum.BigModelUnitEnum.KGBOD_KGMLSS_D.getCode());
  835. }
  836. //万吨水产泥率(todo 没有计算规则,只能从日报获取)
  837. //挥发性污泥浓度占比(MLVSS/MLSS)
  838. //特殊:小于
  839. BigDecimal hfxwnndzb = xinyiCalculate.getHFXWNNDZB();
  840. BigDecimal hfxwnndzbSjz = normConfig.getHfxwnndzbSjz();
  841. if(!Objects.isNull(hfxwnndzb) && !Objects.isNull(hfxwnndzbSjz)){
  842. handleXinYiWarningsSH(hfxwnndzbSjz, hfxwnndzb, BusinessEnum.WarningCategoryEnum.HFXWNNDZB.getCode(), tXinyiIndustry, normConfig, false, BusinessEnum.BigModelUnitEnum.NULL.getCode());
  843. }
  844. //进水碳氮比
  845. BigDecimal jsTdb = xinyiCalculate.getJsTdb();
  846. BigDecimal jstdbnkzXxz = normConfig.getJstdbnkzXxz();
  847. if(!Objects.isNull(jsTdb) && !Objects.isNull(jstdbnkzXxz)){
  848. handleXinYiWarningsSH(jstdbnkzXxz, jsTdb, BusinessEnum.WarningCategoryEnum.TDB.getCode(), tXinyiIndustry, normConfig, true, BusinessEnum.BigModelUnitEnum.NULL.getCode());
  849. }
  850. //进水碳磷比
  851. BigDecimal jsTlb = xinyiCalculate.getJsTlb();
  852. BigDecimal jstlbNkz = normConfig.getJstlbNkz();
  853. if(!Objects.isNull(jsTlb) && !Objects.isNull(jstlbNkz)){
  854. handleXinYiWarningsSH(jstlbNkz, jsTlb, BusinessEnum.WarningCategoryEnum.TLB.getCode(), tXinyiIndustry, normConfig, true, BusinessEnum.BigModelUnitEnum.NULL.getCode());
  855. }
  856. //进水BOD与COD比值(生化性)
  857. BigDecimal jsBodBCod = xinyiCalculate.getJsBodBCod();
  858. String jsbodycodbzGkz = normConfig.getJsbodycodbzGkz();
  859. if(!StringUtils.isBlank(jsbodycodbzGkz) && jsbodycodbzGkz.contains("-")){
  860. String[] split = jsbodycodbzGkz.split("-");
  861. sjscgkz = new BigDecimal(split[0]);
  862. if(!Objects.isNull(jsBodBCod)){
  863. handleXinYiWarningsSH(sjscgkz, jsBodBCod, BusinessEnum.WarningCategoryEnum.BODCODB.getCode(), tXinyiIndustry, normConfig, true, BusinessEnum.BigModelUnitEnum.NULL.getCode());
  864. }
  865. }
  866. //好氧区DO(一池) 2024年5月31日14:04:37 加数据,让他一直报警
  867. BigDecimal shcHyOneDo = tXinyiIndustry.getOneHymdDo();
  868. // BigDecimal shcHyOneDo = new BigDecimal("5");
  869. // BigDecimal hycrjysjzSxz = normConfig.getHycrjysjzSxz();
  870. BigDecimal hycrjyNkz = normConfig.getHycrjyNkz();
  871. if(!Objects.isNull(shcHyOneDo) && !Objects.isNull(hycrjyNkz)){
  872. handleXinYiWarningsSH(hycrjyNkz, shcHyOneDo, BusinessEnum.WarningCategoryEnum.HYQDO_ONE.getCode(), tXinyiIndustry, normConfig, true, BusinessEnum.BigModelUnitEnum.MG_L.getCode());
  873. }
  874. //好氧区DO(二池) 2024年5月31日14:09:36 溶解氧从日报获取 工业的不准
  875. //2024年6月27日17:00:31 都先从工业库获取,日报没有必要再报警了
  876. BigDecimal shcHyTwoDo = tXinyiIndustry.getTwoHymdDo();
  877. if(!Objects.isNull(shcHyTwoDo) && !Objects.isNull(hycrjyNkz)){
  878. handleXinYiWarningsSH(hycrjyNkz, shcHyTwoDo, BusinessEnum.WarningCategoryEnum.HYQDO_TWO.getCode(), tXinyiIndustry, normConfig, true, BusinessEnum.BigModelUnitEnum.MG_L.getCode());
  879. }
  880. //气水比
  881. BigDecimal qsb = xinyiCalculate.getQSB();
  882. BigDecimal shcqbNkz = normConfig.getShcqbNkz();
  883. if(!Objects.isNull(qsb) && !Objects.isNull(shcqbNkz))
  884. handleXinYiWarningsSH(shcqbNkz, qsb, BusinessEnum.WarningCategoryEnum.QSB.getCode(), tXinyiIndustry, normConfig, true, BusinessEnum.BigModelUnitEnum.M3_M3.getCode());
  885. //二沉池表面负荷
  886. BigDecimal eccBmfh = xinyiCalculate.getEccBmfh();
  887. BigDecimal eccbmfhznkzXxz = normConfig.getEccbmfhznkzXxz();
  888. if(!Objects.isNull(eccBmfh) && !Objects.isNull(eccbmfhznkzXxz)){
  889. handleXinYiWarningsSH(eccbmfhznkzXxz, eccBmfh, BusinessEnum.WarningCategoryEnum.ECC_BMFH.getCode(), tXinyiIndustry, normConfig, true, BusinessEnum.BigModelUnitEnum.M3_M2_H.getCode());
  890. }
  891. //二沉池固体负荷
  892. BigDecimal eccGtfh = xinyiCalculate.getEccGtfh();
  893. BigDecimal eccgtfhnkxxz = normConfig.getECCGTFHNKXXZ();
  894. if(!Objects.isNull(eccGtfh) && !Objects.isNull(eccgtfhnkxxz)){
  895. handleXinYiWarningsSH(eccgtfhnkxxz, eccGtfh, BusinessEnum.WarningCategoryEnum.ECC_GTFH.getCode(), tXinyiIndustry, normConfig, true, BusinessEnum.BigModelUnitEnum.KG_M2_D.getCode());
  896. }
  897. //反冲洗水量
  898. //2024年7月3日10:59:32 这里用反冲洗水量比例做计算
  899. BigDecimal fcxslBl = xinyiCalculate.getFCXSLBL();
  900. BigDecimal fcxslblsjzXxz = normConfig.getFcxslblsjzXxz();
  901. if(!Objects.isNull(fcxslBl) && !Objects.isNull(fcxslblsjzXxz))
  902. handleXinYiWarningsSH(fcxslblsjzXxz, fcxslBl, BusinessEnum.WarningCategoryEnum.FCXSL.getCode(), tXinyiIndustry, normConfig, true, BusinessEnum.BigModelUnitEnum.PERCENT.getCode());
  903. }
  904. /**
  905. * 处理信义生化报警的逻辑 统一处理
  906. * @param sjscgkz :实际生产管控值
  907. * @param currentVal
  908. * @param category
  909. * @param tXinyiIndustry
  910. * @param normConfig
  911. * @param normal 是否常规判断:默认都是超过;只有这个挥发性污泥浓度占比(VSS/SS)是小于才报警
  912. * 2024年6月27日15:39:15 调整:生化报警只取实际生产管控指标
  913. * 2024年7月11日10:44:11 新增单位unit 拼接完单位以后返回
  914. *
  915. */
  916. private void handleXinYiWarningsSH(BigDecimal sjscgkz, BigDecimal currentVal, String category, TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig, Boolean normal, String unit) {
  917. TXinyiWarningRecord tXinyiWarningRecord = new TXinyiWarningRecord();
  918. /*String category = BusinessEnum.WarningCategoryEnum.CS_AD.getCode();*/
  919. tXinyiWarningRecord.setStatus(0);
  920. tXinyiWarningRecord.setType(1);
  921. tXinyiWarningRecord.setCategory(category);
  922. tXinyiWarningRecord.setTime(DateUtils.getNowDate());
  923. tXinyiWarningRecord.setWarningVal(currentVal);
  924. tXinyiWarningRecord.setDesignVal(sjscgkz);
  925. tXinyiWarningRecord.setControlVal(sjscgkz);
  926. tXinyiWarningRecord.setCreateBy(WARNING_DEFAULT_CREATE);
  927. tXinyiWarningRecord.setCreateTime(DateUtils.getNowDate());
  928. tXinyiWarningRecord.setRemark(ONE_SH_WARNING.getCode());
  929. //2024年6月27日17:36:25 有一个判断是反正的
  930. if(normal){
  931. //2024年5月25日17:52:33 如果工业库获取不到数据,也触发报警,但是不调用决策接口
  932. if (Objects.isNull(currentVal) || currentVal.compareTo(BigDecimal.ZERO) == 0) {
  933. tXinyiWarningRecord.setReason(category + EXCEPTION_WARNING);
  934. tXinyiWarningRecord.setLevel(WARNING_LEVEL_NO_DATE);
  935. } else if (currentVal.compareTo(sjscgkz) > 0) {//一级
  936. tXinyiWarningRecord.setReason(category + CHAOBIAO_WARNING);
  937. tXinyiWarningRecord.setLevel(WARNING_LEVEL_ONE);
  938. } /*else if (currentVal.compareTo(bzz) >= 0 && currentVal.compareTo(multiply) <= 0) {//二级
  939. tXinyiWarningRecord.setReason(category + CHAOBIAO_WARNING);
  940. tXinyiWarningRecord.setLevel(WARNING_LEVEL_TWO);
  941. } else if (!Objects.isNull(gkz) && currentVal.compareTo(gkz) > 0) {
  942. tXinyiWarningRecord.setReason(category + CHAOGUANKONG_WARNING);
  943. tXinyiWarningRecord.setLevel(WARNING_LEVEL_THREE);
  944. }*/ else {
  945. tXinyiWarningRecord = null;//这种的无需处理
  946. }
  947. }else{
  948. //2024年5月25日17:52:33 如果工业库获取不到数据,也触发报警,但是不调用决策接口
  949. if (Objects.isNull(currentVal) || currentVal.compareTo(BigDecimal.ZERO) == 0) {
  950. tXinyiWarningRecord.setReason(category + EXCEPTION_WARNING);
  951. tXinyiWarningRecord.setLevel(WARNING_LEVEL_NO_DATE);
  952. } else if (currentVal.compareTo(sjscgkz) < 0) {//一级
  953. tXinyiWarningRecord.setReason(category + CHAOBIAO_WARNING);
  954. tXinyiWarningRecord.setLevel(WARNING_LEVEL_ONE);
  955. } /*else if (currentVal.compareTo(bzz) >= 0 && currentVal.compareTo(multiply) <= 0) {//二级
  956. tXinyiWarningRecord.setReason(category + CHAOBIAO_WARNING);
  957. tXinyiWarningRecord.setLevel(WARNING_LEVEL_TWO);
  958. } else if (!Objects.isNull(gkz) && currentVal.compareTo(gkz) > 0) {
  959. tXinyiWarningRecord.setReason(category + CHAOGUANKONG_WARNING);
  960. tXinyiWarningRecord.setLevel(WARNING_LEVEL_THREE);
  961. }*/ else {
  962. tXinyiWarningRecord = null;//这种的无需处理
  963. }
  964. }
  965. //当前状态正常 需要查询历史有无正在报警的数据,如果有,将报警状态改完2(系统自动关闭)
  966. List<TXinyiWarningRecord> tXinyiWarningRecords = this.xinyiWarningRecordMapper.selectTXinyiWarningRecordList(TXinyiWarningRecord.builder().delFlag(0).type(1).category(category).status(0).build());
  967. if(Objects.isNull(tXinyiWarningRecord)){//数据正常,无告警信息
  968. if(!CollectionUtils.isEmpty(tXinyiWarningRecords)){
  969. log.info( "{}:现在恢复正常,历史报警数据为{}", category,JSON.toJSONString(tXinyiWarningRecords));
  970. for (TXinyiWarningRecord xinyiWarningRecord : tXinyiWarningRecords) {
  971. xinyiWarningRecord.setStatus(2);
  972. Date nowDate = DateUtils.getNowDate();
  973. xinyiWarningRecord.setOffTime(nowDate);
  974. xinyiWarningRecord.setUpdateTime(nowDate);
  975. xinyiWarningRecord.setUpdateBy(WARNING_DEFAULT_CREATE);
  976. this.xinyiWarningRecordMapper.updateTXinyiWarningRecord(xinyiWarningRecord);
  977. }
  978. }
  979. }else{//有新的告警信息
  980. Date nowDate = DateUtils.getNowDate();
  981. if(CollectionUtils.isEmpty(tXinyiWarningRecords)){//之前没有告警记录
  982. //保存到数据库中
  983. //2024年7月15日11:06:16 因为只有一个告警记录,但是如果一直报警,现在报警时间取的是更新时间,兼容第一次报警处理
  984. tXinyiWarningRecord.setUpdateTime(nowDate);
  985. this.xinyiWarningRecordMapper.insertTXinyiWarningRecord(tXinyiWarningRecord);
  986. if(WARNING_LEVEL_NO_DATE.equals(tXinyiWarningRecord.getLevel())){
  987. //只保存一个普通的问答记录 不需要调用决策信息,但是实时数据还是要记录的
  988. this.addChatRecordByDeviceErr(tXinyiWarningRecord, tXinyiIndustry, normConfig);
  989. }else {
  990. //继续调用决策(普通问答)
  991. this.askBigModelForSHWarning(tXinyiWarningRecord, tXinyiIndustry, normConfig, unit);
  992. }
  993. }else{
  994. log.info("{}:之前已经有过告警记录了,且还是继续报警,无需重复添加报警,但是决策仍然要调用", category);
  995. for (TXinyiWarningRecord xinyiWarningRecord : tXinyiWarningRecords) {//理论上只有一个的
  996. if(WARNING_LEVEL_NO_DATE.equals(tXinyiWarningRecord.getLevel())){
  997. //只保存一个普通的问答记录 不需要调用决策信息,但是实时数据还是要记录的
  998. this.addChatRecordByDeviceErr(tXinyiWarningRecord, tXinyiIndustry, normConfig);
  999. }else {
  1000. //2024年7月5日10:45:24 逻辑调整:更新一直报警的那条记录的显示值
  1001. xinyiWarningRecord.setWarningVal(tXinyiWarningRecord.getWarningVal());
  1002. // xinyiWarningRecord.setDesignVal(tXinyiWarningRecord.getDesignVal());
  1003. // tXinyiWarningRecord.setControlVal(tXinyiWarningRecord.getControlVal());
  1004. xinyiWarningRecord.setTime(tXinyiWarningRecord.getTime());
  1005. xinyiWarningRecord.setUpdateTime(nowDate);
  1006. xinyiWarningRecord.setUpdateBy(WARNING_DEFAULT_CREATE);
  1007. //2024年7月15日11:07:33 报警的级别也要重新计算并更新
  1008. xinyiWarningRecord.setLevel(tXinyiWarningRecord.getLevel());
  1009. //更新数据库
  1010. this.xinyiWarningRecordMapper.updateTXinyiWarningRecord(xinyiWarningRecord);
  1011. //继续调用决策(普通问答)
  1012. this.askBigModelForSHWarning(xinyiWarningRecord, tXinyiIndustry, normConfig, unit);
  1013. }
  1014. }
  1015. }
  1016. }
  1017. }
  1018. private void handleSZWarning(TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
  1019. //出水相关
  1020. //出水COD报警
  1021. BigDecimal csCod = tXinyiIndustry.getCsCod();
  1022. BigDecimal cscodBzz = normConfig.getCscodBzz();
  1023. BigDecimal cscodGkz = normConfig.getCscodGkz();
  1024. if(!Objects.isNull(cscodBzz)){
  1025. handleXinYiWarningsCs(cscodBzz, csCod, cscodGkz, BusinessEnum.WarningCategoryEnum.CS_COD.getCode(), tXinyiIndustry, normConfig);
  1026. }
  1027. //出水总磷超标报警
  1028. BigDecimal csTp = tXinyiIndustry.getCsTp();
  1029. BigDecimal cszlBzz = normConfig.getCszlBzz();
  1030. BigDecimal cszlGkz = normConfig.getCszlGkz();
  1031. if(!Objects.isNull(cszlBzz)){
  1032. handleXinYiWarningsCs(cszlBzz, csTp, cszlGkz, BusinessEnum.WarningCategoryEnum.CS_ZL.getCode(), tXinyiIndustry, normConfig);
  1033. }
  1034. //出水总氮超标报警
  1035. BigDecimal csTn = tXinyiIndustry.getCsTn();
  1036. BigDecimal cszzBzz = normConfig.getCszzBzz();
  1037. BigDecimal cszzGkz = normConfig.getCszzGkz();
  1038. if(!Objects.isNull(cszzBzz)){
  1039. handleXinYiWarningsCs(cszzBzz, csTn, cszzGkz, BusinessEnum.WarningCategoryEnum.CS_ZD.getCode(), tXinyiIndustry, normConfig);
  1040. }
  1041. //出水氨氮超标报警
  1042. BigDecimal csNh3 = tXinyiIndustry.getCsNh3();
  1043. BigDecimal csadBzz = normConfig.getCsadBzz();
  1044. BigDecimal csadGkz = normConfig.getCsadGkz();
  1045. if(!Objects.isNull(csadBzz)){
  1046. handleXinYiWarningsCs(csadBzz, csNh3, csadGkz, BusinessEnum.WarningCategoryEnum.CS_AD.getCode(), tXinyiIndustry, normConfig);
  1047. }
  1048. //出水SS超标报警
  1049. BigDecimal csSS = tXinyiIndustry.getCsSs();
  1050. BigDecimal csSSBzz = normConfig.getCsssBzz();
  1051. BigDecimal csssGkz = normConfig.getCsssGkz();
  1052. if(!Objects.isNull(csSSBzz)){
  1053. handleXinYiWarningsCs(csSSBzz, csSS, csssGkz, BusinessEnum.WarningCategoryEnum.CS_SS.getCode(), tXinyiIndustry, normConfig);
  1054. }
  1055. //进水相关报警
  1056. //进水总磷超标报警
  1057. BigDecimal jsTp = tXinyiIndustry.getJsTp();
  1058. BigDecimal jszlSjz = normConfig.getJszlSjz();
  1059. if(!Objects.isNull(jszlSjz)){
  1060. handleXinYiWarningRecordJS(jszlSjz, jsTp, BusinessEnum.WarningCategoryEnum.JS_ZL.getCode(), tXinyiIndustry, normConfig);
  1061. }
  1062. //进水COD超标报警
  1063. BigDecimal jsCod = tXinyiIndustry.getJsCod();
  1064. BigDecimal jscodSjz = normConfig.getJscodSjz();
  1065. if(!Objects.isNull(jscodSjz)){
  1066. handleXinYiWarningRecordJS(jscodSjz, jsCod, BusinessEnum.WarningCategoryEnum.JS_COD.getCode(), tXinyiIndustry, normConfig);
  1067. }
  1068. //进水总氮超标报警
  1069. BigDecimal jsTn = tXinyiIndustry.getJsTn();
  1070. BigDecimal jszdSjz = normConfig.getJszdSjz();
  1071. if(!Objects.isNull(jszdSjz)){
  1072. handleXinYiWarningRecordJS(jszdSjz, jsTn, BusinessEnum.WarningCategoryEnum.JS_ZD.getCode(), tXinyiIndustry, normConfig);
  1073. }
  1074. //进水氨氮超标报警
  1075. BigDecimal jsNh3 = tXinyiIndustry.getJsNh3();
  1076. BigDecimal jsadSjz = normConfig.getJsadSjz();
  1077. if(!Objects.isNull(jsadSjz)){
  1078. handleXinYiWarningRecordJS(jsadSjz, jsNh3, BusinessEnum.WarningCategoryEnum.JS_AD.getCode(), tXinyiIndustry, normConfig);
  1079. }
  1080. //进水SS超标报警
  1081. BigDecimal jsSS = tXinyiIndustry.getJsSs();
  1082. BigDecimal jsSSSjz = normConfig.getJsssSjz();
  1083. if(!Objects.isNull(jsSSSjz)){
  1084. handleXinYiWarningRecordJS(jsSSSjz, jsSS, BusinessEnum.WarningCategoryEnum.JS_SS.getCode(), tXinyiIndustry, normConfig);
  1085. }
  1086. }
  1087. private void askBigModelForSHWarning(TXinyiWarningRecord xinyiWarningRecord, TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig, String unit) {
  1088. log.info("进入了后台接口调⽤⼤模型获取问答结果处理");
  1089. StringBuilder sb = new StringBuilder();
  1090. String sessionId = IdUtils.simpleUUID();
  1091. ChatReq chatReq = new ChatReq();
  1092. // String ipAddr = IpUtils.getIpAddr();//获取用户的ip地址 传给大模型
  1093. String ipAddr = "";//获取用户的ip地址 传给大模型 定时任务获取不到ip地址
  1094. int counts = 1;//默认是第一次
  1095. //这种问答 没有历史问答的概念 直接把问题扔进去就行 无需查询历史记录
  1096. List<String> historyDates = new ArrayList<>();
  1097. //构建问题(替换提示词中的占位符)
  1098. /*String shWarningPrompt = SH_WARNING_PROMPT;
  1099. shWarningPrompt =shWarningPrompt.replace("#{0}", xinyiWarningRecord.getReason());
  1100. shWarningPrompt =shWarningPrompt.replace("#{1}", String.valueOf(DecimalUtils.getAbsAndScale(xinyiWarningRecord.getDesignVal(), INT_2)));
  1101. shWarningPrompt =shWarningPrompt.replace("#{2}", String.valueOf(DecimalUtils.getAbsAndScale(xinyiWarningRecord.getControlVal(), INT_2)));
  1102. shWarningPrompt =shWarningPrompt.replace("#{3}", String.valueOf(DecimalUtils.getAbsAndScale(xinyiWarningRecord.getWarningVal(), INT_2)));*/
  1103. //2024年7月5日10:08:54 拼接优化
  1104. StringBuilder prompt = new StringBuilder(SH_WARNING_PROMPT_1);
  1105. prompt.append(xinyiWarningRecord.getReason());
  1106. prompt.append(SH_WARNING_PROMPT_2).append(DecimalUtils.getAbsAndScale(xinyiWarningRecord.getDesignVal(), INT_2));
  1107. prompt.append(SH_WARNING_PROMPT_3).append(DecimalUtils.getAbsAndScale(xinyiWarningRecord.getControlVal(), INT_2));
  1108. prompt.append(SH_WARNING_PROMPT_4).append(DecimalUtils.getAbsAndScale(xinyiWarningRecord.getWarningVal(), INT_2));
  1109. prompt.append(SH_WARNING_PROMPT_5);
  1110. historyDates.add(prompt.toString());
  1111. // 获取输出流
  1112. ManagedChannel channel = null;
  1113. try {
  1114. channel = ManagedChannelBuilder.forAddress(bigModelConfig.getIp(), bigModelConfig.getPort())
  1115. .usePlaintext()
  1116. .build();
  1117. InferenceAPIsServiceGrpc.InferenceAPIsServiceBlockingStub stub = InferenceAPIsServiceGrpc.newBlockingStub(channel);
  1118. // 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) + "}";
  1119. //2024年6月27日13:29:18 优化,不再使用拼接JSON字符串
  1120. String dataJson = buildBigModelReqForChat(sessionId, historyDates, ipAddr, false);
  1121. // log.info("请求大模型的问答参数为{}", dataJson);
  1122. PredictionsRequest request = PredictionsRequest.newBuilder()
  1123. .setModelName("slibra_bot")
  1124. .putInput("method", ByteString.copyFrom("infer_stream", "utf-8"))//推理
  1125. .putInput("data", ByteString.copyFrom(dataJson, "utf-8"))
  1126. .buildPartial();
  1127. Iterator<PredictionResponse> predictions = stub.streamPredictions(request);
  1128. //将结果记录到问答表
  1129. while (predictions.hasNext()) {
  1130. String responseStr = predictions.next().getPrediction().toStringUtf8();
  1131. // log.info("大模型问答返回的原始结果为{}", responseStr);
  1132. responseStr = JSON.parseObject(responseStr).getString("message");
  1133. if("complete".equals(responseStr)){
  1134. log.info("结尾语句并且是非JSON,无需处理");
  1135. }else{
  1136. sb.append(responseStr);
  1137. }
  1138. }
  1139. //将问答更新到数据库中
  1140. chatReq.setSessionId(sessionId);
  1141. chatReq.setType(1);//0问答 1决策 2本地 3仿真预测
  1142. chatReq.setModule(3);//0专家问答 1智能工单 2智能体助手 3告警 4简报
  1143. String showVal = this.buildShowValueSH(xinyiWarningRecord, tXinyiIndustry, normConfig, DateUtils.getNowDate(), unit);
  1144. chatReq.setShowVal(showVal);
  1145. chatReq.setQuestion(prompt.toString());
  1146. chatReq.setAnswer(sb.toString());
  1147. chatReq.setWarningId(String.valueOf(xinyiWarningRecord.getId()));
  1148. chatReq.setCounts(counts);//问答次数
  1149. chatReq.setUserId(WARNING_DEFAULT_CREATE);
  1150. chatReq.setCreateBy(WARNING_DEFAULT_CREATE);
  1151. chatReq.setCreateTime(DateUtils.getNowDate());
  1152. this.xinyiChatRecordMapper.insertTXinyiChatRecord(chatReq);
  1153. } catch (IOException e) {
  1154. throw new RuntimeException(e);
  1155. } finally {
  1156. // 关闭输出流
  1157. channel.shutdown();
  1158. }
  1159. }
  1160. private String buildBigModelReqForChat(String sessionId, List<String> historyDates, String ipAddr, Boolean strengthen) {
  1161. ChatRequest chatRequest = new ChatRequest();
  1162. chatRequest.setSessionId(sessionId);
  1163. chatRequest.setHistoryDia(historyDates);
  1164. //2024年7月5日13:24:10 temperature做区分
  1165. GenerateArgs generateArgs = new GenerateArgs();
  1166. generateArgs.setTemperature(bigModelConfig.getTemperature());
  1167. chatRequest.setGenerateArgs(generateArgs);
  1168. Map<String, Object> extra = new HashMap<>();
  1169. extra.put("ip_address", ipAddr);
  1170. chatRequest.setExtra(extra);
  1171. chatRequest.setStrengthen(strengthen);
  1172. //2024年7月3日16:21:28 明确该场景不用调工具
  1173. chatRequest.setTools(String.valueOf(false));
  1174. return JSON.toJSONString(chatRequest);
  1175. }
  1176. /**
  1177. * 调用大模型获取决策结果 并同时记录对应信息到聊天记录表中
  1178. *
  1179. * @param tXinyiWarningRecord
  1180. * @param tXinyiIndustry
  1181. * @param normConfig
  1182. * @param isSpecial 机器人化验库,需要把总磷和总氮的值特殊处理一下
  1183. * @param nowDate
  1184. */
  1185. private void handleDecision(TXinyiWarningRecord tXinyiWarningRecord, TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig, boolean isSpecial, Date nowDate) {
  1186. log.info("进入了调⽤大模型决策接口");
  1187. ChatReq chatReq = new ChatReq();
  1188. // StringBuilder sb = new StringBuilder();
  1189. //大模型结果 放入一个结合中
  1190. List<String> resultData = new ArrayList<>();
  1191. //决策和问答不一样 没有历史的概念 所以sessionId都是新的 次数都是1
  1192. String sessionId = IdUtils.simpleUUID();
  1193. // String feedback = chatReq.getFeedback();
  1194. //决策请求的业务参数
  1195. // List<HashMap<String, Object>> list = this.xinyiIndustryMapper.selectLast10RecordsForDecision();
  1196. //2024年5月21日15:23:07 这里不能用关联查询处理,日报要获取最新的一条而不是今日的数据。
  1197. List<DecisionReq> decisionReqs = this.xinyiIndustryMapper.selectLast10RecordsForDecisionOnlyIndustry();
  1198. if(!CollectionUtils.isEmpty(decisionReqs)){
  1199. //处理日报数据
  1200. TXinyiDaily daily = this.xinyiDailyMapper.selectNewestData();
  1201. for (DecisionReq decisionReq : decisionReqs) {
  1202. if(!Objects.isNull(daily)){
  1203. WaterFormat.getWaterDecimationData(decisionReq, daily);
  1204. // decisionReq.setT(daily.getJsSw());
  1205. // decisionReq.setSVI(daily.getWntjzsAll());
  1206. // decisionReq.setSV(daily.getWncjbAll());
  1207. // decisionReq.setMlvss(daily.getHfxwnndAll());
  1208. // //2024年7月2日17:20:12 mlss和DO_O也要从日报获取
  1209. // decisionReq.setMlss(daily.getHycWnndAll());
  1210. // decisionReq.setDoO(daily.getHycRjyAll());
  1211. // decisionReq.setDoAna(daily.getYycRjyAll());
  1212. // decisionReq.setDoQue(daily.getQycRjyAll());
  1213. // //2024年5月26日11:59:02 干污泥量数据做了同步
  1214. // decisionReq.setGwnl(daily.getGWNL());
  1215. // BigDecimal jsBod5 = daily.getJsBod5();
  1216. // decisionReq.setBodIn(jsBod5);
  1217. // BigDecimal tpIn = decisionReq.getTpIn();
  1218. // BigDecimal tnIn = decisionReq.getTnIn();
  1219. // if(!Objects.isNull(jsBod5)){
  1220. // if(!Objects.isNull(tpIn) && tpIn.compareTo(new BigDecimal("0")) >0){
  1221. // decisionReq.setC(jsBod5.divide(tpIn, 4, RoundingMode.HALF_UP));
  1222. // }
  1223. // if(!Objects.isNull(tnIn) && tnIn.compareTo(new BigDecimal("0")) >0){
  1224. // decisionReq.setB(jsBod5.divide(tnIn, 4, RoundingMode.HALF_UP));
  1225. // }
  1226. // }
  1227. // //2024年6月1日08:37:09 内回流加上
  1228. // decisionReq.setR(daily.getNhlR());
  1229. // decisionReq.setRR(daily.getWhlR());
  1230. }
  1231. //如果是机器人化验室的报警,需要把总磷和总氮,获取化验室对应的数据且计算
  1232. if(isSpecial){
  1233. List<TXinyiRobot> xinyiRobots = this.xinyiRobotMapper.selectTXinyiRobotList(TXinyiRobot.builder().testHour(tXinyiIndustry.getTestHour()).build());
  1234. if(CollectionUtils.isEmpty(xinyiRobots)){
  1235. log.error("{}获取化验室数据失败", tXinyiIndustry.getTestHour());
  1236. }else{
  1237. TXinyiRobot tXinyiRobot = xinyiRobots.get(0);
  1238. //总氮
  1239. BigDecimal no3Hlj1Jqr = tXinyiRobot.getNo3Hlj1Jqr();
  1240. BigDecimal no3Hlj2Jqr = tXinyiRobot.getNo3Hlj2Jqr();
  1241. if(!Objects.isNull(no3Hlj1Jqr) && !Objects.isNull(no3Hlj2Jqr)){
  1242. decisionReq.setTnOff((no3Hlj1Jqr.add(no3Hlj2Jqr)).divide((new BigDecimal("2").multiply(ROBOT_HY_DIVIDE)), INT_0, RoundingMode.HALF_UP));
  1243. }
  1244. //总磷
  1245. BigDecimal tpRccJqr = tXinyiRobot.getTpRccJqr();
  1246. if(!Objects.isNull(tpRccJqr)){
  1247. decisionReq.setTpOff(tpRccJqr.divide(ROBOT_HY_DIVIDE, INT_0, RoundingMode.HALF_UP));
  1248. }
  1249. }
  1250. }
  1251. //todo 字符串的调用getAbsAndScaleStr()
  1252. }
  1253. }
  1254. // String rows = JSON.toJSONString(decisionReqs, JSONWriter.Feature.WriteNulls);
  1255. // 获取输出流
  1256. ManagedChannel channel = null;
  1257. String dataJson = "";
  1258. try {
  1259. channel = ManagedChannelBuilder.forAddress(bigModelConfig.getIp(), bigModelConfig.getPort())
  1260. .usePlaintext()
  1261. .build();
  1262. InferenceAPIsServiceGrpc.InferenceAPIsServiceBlockingStub stub = InferenceAPIsServiceGrpc.newBlockingStub(channel);
  1263. // 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 + "}}";
  1264. //2024年6月24日17:59:17 优化,不再拼接JSON字符串
  1265. PolicyReq policyReq = new PolicyReq();
  1266. //2024年7月11日17:57:53 因为化验室的改了 但是 调用决策还得用原来的几个值
  1267. String category = tXinyiWarningRecord.getCategory();
  1268. if(ROBOT_XSY_1.getCode().equalsIgnoreCase(category)){
  1269. category = CS_ZD.getCode();
  1270. } else if (ROBOT_XSY_2.getCode().equalsIgnoreCase(category)) {
  1271. category = CS_ZD.getCode();
  1272. }else if (ROBOT_ECCZLSY.getCode().equalsIgnoreCase(category)) {
  1273. category = CS_ZL.getCode();
  1274. }
  1275. policyReq.setNorm(category);
  1276. HashMap<String, Object> hashMap = new HashMap<>();
  1277. policyReq.setFeedback(hashMap);//不能传null
  1278. policyReq.setSimulate(hashMap);//不能传null
  1279. policyReq.setSessionId(sessionId);
  1280. //2024年7月5日13:24:10 temperature做区分
  1281. GenerateArgs generateArgs = new GenerateArgs();
  1282. generateArgs.setTemperature(bigModelConfig.getTemperature());
  1283. policyReq.setGenerateArgs(generateArgs);
  1284. HashMap<String, Object> map = new HashMap<>();
  1285. map.put("rows", decisionReqs);
  1286. //2024年6月25日14:16:05 增加报警是管控值报警还是标准值报警
  1287. if(WARNING_LEVEL_ONE.equals(tXinyiWarningRecord.getLevel()) || WARNING_LEVEL_TWO.equals(tXinyiWarningRecord.getLevel()))
  1288. map.put("source", "bzz");
  1289. else
  1290. map.put("source", "gkz");
  1291. policyReq.setExtra(map);
  1292. dataJson = JSON.toJSONString(policyReq, JSONWriter.Feature.WriteNulls);
  1293. // log.info("请求大模型的决策的参数为{}", dataJson);
  1294. PredictionsRequest request = PredictionsRequest.newBuilder()
  1295. .setModelName("slibra_bot")
  1296. .putInput("method", ByteString.copyFrom("decision_stream", "utf-8"))//推理
  1297. .putInput("data", ByteString.copyFrom(dataJson, "utf-8"))
  1298. .buildPartial();
  1299. Iterator<PredictionResponse> predictions = stub.streamPredictions(request);
  1300. while (predictions.hasNext()) {
  1301. String responseStr = predictions.next().getPrediction().toStringUtf8();
  1302. // log.info("决策流式返回的结果是{}", responseStr);
  1303. //2024年5月25日16:37:16 按照大模型返回的类型解析数据
  1304. String biz = JSON.parseObject(responseStr).getString("biz");
  1305. if(BusinessEnum.BigModelBizEnum.OK.getCode().equals(biz)){
  1306. log.info("结尾语句并且是非JSON,无需处理");
  1307. //结束语句也流式输出,但是并不记录下来 2024年5月24日11:15:23 也不返回前端
  1308. /*outputStream.write(responseStr.getBytes());
  1309. outputStream.flush();*/
  1310. }else if(BusinessEnum.BigModelBizEnum.DECISION_DEBUGGER.getCode().equals(biz)){
  1311. log.info("中间过程,目前只打印日志,不记录数据,也不返回给前端,返回数据为{}", responseStr);
  1312. //结束语句也流式输出,但是并不记录下来 2024年5月24日11:15:23 也不返回前端
  1313. /*outputStream.write(responseStr.getBytes());
  1314. outputStream.flush();*/
  1315. }else{//其他 要么错误 要么alert 要么出的报告
  1316. // sb.append(responseStr);
  1317. resultData.add(responseStr);
  1318. }
  1319. }
  1320. } catch (Exception e) {
  1321. // throw new RuntimeException(e);
  1322. log.error("定时任务处理告警调用决策异常,异常信息为{}", JSON.toJSONString(e));
  1323. resultData.add("{\"biz\":\"ERROR\",\"message\":\"大模型分析数据异常,请稍后再试\"}");
  1324. } finally {
  1325. // log.info("决策最终要保存的数据是{}", JSON.toJSONString(resultData));
  1326. //保存聊天记录
  1327. //将问答更新到数据库中
  1328. chatReq.setSessionId(sessionId);
  1329. chatReq.setType(1);//0问答 1决策
  1330. chatReq.setModule(3);
  1331. /*String userId = SecurityUtils.getUserId().toString();
  1332. String username = SecurityUtils.getUsername();*/
  1333. chatReq.setUserId(WARNING_DEFAULT_CREATE);
  1334. String showVal = this.buildShowValue(tXinyiWarningRecord, tXinyiIndustry, normConfig, nowDate);
  1335. chatReq.setShowVal(showVal);//前端展示的数据和提问的数据不一致
  1336. chatReq.setQuestion(dataJson);
  1337. chatReq.setAnswer(JSON.toJSONString(resultData));
  1338. chatReq.setWarningId(String.valueOf(tXinyiWarningRecord.getId()));
  1339. chatReq.setCounts(1);//问答次数
  1340. chatReq.setCreateBy(WARNING_DEFAULT_CREATE);
  1341. chatReq.setCreateTime(DateUtils.getNowDate());
  1342. this.xinyiChatRecordMapper.insertTXinyiChatRecord(chatReq);
  1343. // 关闭输出流
  1344. if(!Objects.isNull(channel))
  1345. channel.shutdown();
  1346. }
  1347. }
  1348. /**
  1349. * 2024年7月11日10:32:49 生化报警的展示额外处理(因为涉及到很多的单位)
  1350. *
  1351. * @param tXinyiWarningRecord
  1352. * @param tXinyiIndustry
  1353. * @param normConfig
  1354. * @param nowDate
  1355. * @param unit
  1356. * @return
  1357. */
  1358. private String buildShowValueSH(TXinyiWarningRecord tXinyiWarningRecord, TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig, Date nowDate, String unit) {
  1359. JSONObject result = new JSONObject();
  1360. // JSONObject basic = new JSONObject();
  1361. Integer status = tXinyiWarningRecord.getStatus();
  1362. Date warningTime = tXinyiWarningRecord.getTime();
  1363. String remark = tXinyiWarningRecord.getRemark();
  1364. int count = DateUtils.differentHoursByMillisecond(warningTime, DateUtils.getNowDate()) + 1;
  1365. ShowValueSHBasic showValueSHBasic = new ShowValueSHBasic();
  1366. /*if("0".equals(remark)){//水质报警
  1367. showValueSHBasic.setH(ZAIXIANYIBIAO);
  1368. showValueSHBasic.setF(tXinyiWarningRecord.getLevel());
  1369. }else if("1".equals(remark)){//生化报警
  1370. showValueSHBasic.setH(ZAIXIANYIBIAO);
  1371. }else if("2".equals(remark)){//预测报警
  1372. showValueSHBasic.setH(YVCE);
  1373. // showValueCSBasic.setF(tXinyiWarningRecord.getLevel());
  1374. }else {//机器人化验室报警(特殊的 水质报警)
  1375. showValueSHBasic.setH(LIANXUJIANCE);
  1376. showValueSHBasic.setF(tXinyiWarningRecord.getLevel());
  1377. }*/
  1378. //生化报警固定了
  1379. showValueSHBasic.setF(tXinyiWarningRecord.getLevel());
  1380. showValueSHBasic.setH("计算");
  1381. //通用的
  1382. // showValueBasic.setA(tXinyiWarningRecord.getReason());
  1383. showValueSHBasic.setB(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM ,warningTime));
  1384. showValueSHBasic.setC(DecimalUtils.getAbsAndScale(tXinyiWarningRecord.getWarningVal(), INT_2) + unit);
  1385. showValueSHBasic.setD(DecimalUtils.getAbsAndScale(tXinyiWarningRecord.getDesignVal(), INT_2) + unit);
  1386. showValueSHBasic.setG(Math.min(count, MAX_COUNT));
  1387. if(tXinyiWarningRecord.getType() != 2)
  1388. showValueSHBasic.setI(status == 0 ? "报警中" : status == 1 ? "用户关闭" : status == 2 ? "系统关闭" : "应急处理中");
  1389. else
  1390. showValueSHBasic.setI(status == 0 ? "预警中" : "已完成");
  1391. result.put("basic", showValueSHBasic);
  1392. JSONObject jsData = jsCsFormatData.getJsonObject(tXinyiIndustry, normConfig);//进水数据
  1393. result.put("jsData", jsData);
  1394. JSONObject csData = jsCsFormatData.getCsonObject(tXinyiIndustry, normConfig);//出水数据
  1395. result.put("csData", csData);
  1396. // return JSON.toJSONString(result, JSONWriter.Feature.WriteNulls);
  1397. return JSON.toJSONString(result);
  1398. }
  1399. private String buildShowValue(TXinyiWarningRecord tXinyiWarningRecord, TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig, Date nowDate) {
  1400. JSONObject result = new JSONObject();
  1401. // JSONObject basic = new JSONObject();
  1402. Integer status = tXinyiWarningRecord.getStatus();
  1403. Date warningTime = tXinyiWarningRecord.getTime();
  1404. String remark = tXinyiWarningRecord.getRemark();
  1405. int count = DateUtils.differentHoursByMillisecond(warningTime, DateUtils.getNowDate()) + 1;
  1406. String category = tXinyiWarningRecord.getCategory();
  1407. /*basic.put("title", tXinyiWarningRecord.getReason());
  1408. basic.put("报警时间", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM ,warningTime));
  1409. basic.put("报警值", tXinyiWarningRecord.getWarningVal());
  1410. basic.put("标准值", tXinyiWarningRecord.getDesignVal());
  1411. basic.put("管控值", tXinyiWarningRecord.getControlVal());
  1412. basic.put("报警次数", Math.min(count, MAX_COUNT));
  1413. if(tXinyiWarningRecord.getType() != 2)
  1414. basic.put("状态", status == 0 ? "报警中" : status == 1 ? "用户关闭" : status == 2 ? "系统关闭" : "应急处理中");
  1415. else
  1416. basic.put("状态", status == 0 ? "预警中" : "已完成");
  1417. //2024年5月27日14:04:22 额外返回2个字段 [管控值 和 告警级别] 返回的json没有数据是因为value没有值
  1418. // basic.put("管控值", tXinyiWarningRecord.getControlVal());
  1419. basic.put("告警级别", tXinyiWarningRecord.getLevel());*/
  1420. //2024年6月25日16:00:18 进出水展示的不一样
  1421. //2024年7月15日10:57:31 因为新增了化验室的3个指标,也是出水数据
  1422. if(category.contains(CHUSHUI)
  1423. || ROBOT_ECCZLSY.getCode().equalsIgnoreCase(category)
  1424. || ROBOT_XSY_2.getCode().equalsIgnoreCase(category)
  1425. || ROBOT_XSY_1.getCode().equalsIgnoreCase(category)){//出水的展示
  1426. ShowValueCSBasic showValueCSBasic = new ShowValueCSBasic();
  1427. if(ZERO_SZ_WARNING.getCode().equals(remark)){//水质报警
  1428. showValueCSBasic.setH(ZAIXIANYIBIAO);
  1429. showValueCSBasic.setF(tXinyiWarningRecord.getLevel());
  1430. }else if(ONE_SH_WARNING.getCode().equals(remark)){//生化报警
  1431. showValueCSBasic.setH(ZAIXIANYIBIAO);
  1432. }else if(BusinessEnum.BigModelWarningTypeRemarkEnum.TWO_YC_WARNING.getCode().equals(remark)){//预测报警
  1433. showValueCSBasic.setH(YVCE);
  1434. // showValueCSBasic.setF(tXinyiWarningRecord.getLevel());
  1435. }else {//机器人化验室报警(特殊的水质报警)
  1436. showValueCSBasic.setH(LIANXUJIANCE);
  1437. showValueCSBasic.setF(tXinyiWarningRecord.getLevel());
  1438. }
  1439. //通用的
  1440. // showValueBasic.setA(tXinyiWarningRecord.getReason());
  1441. // showValueCSBasic.setB(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM ,warningTime));
  1442. showValueCSBasic.setB(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM ,nowDate));
  1443. showValueCSBasic.setC(DecimalUtils.getAbsAndScale(tXinyiWarningRecord.getWarningVal(), INT_2));
  1444. showValueCSBasic.setD(DecimalUtils.getAbsAndScale(tXinyiWarningRecord.getControlVal(), INT_2));
  1445. showValueCSBasic.setE(DecimalUtils.getAbsAndScale(tXinyiWarningRecord.getDesignVal(), INT_2));
  1446. showValueCSBasic.setG(Math.min(count, MAX_COUNT));
  1447. if(tXinyiWarningRecord.getType() != 2)
  1448. showValueCSBasic.setI(status == 0 ? "报警中" : status == 1 ? "用户关闭" : status == 2 ? "系统关闭" : "应急处理中");
  1449. else
  1450. showValueCSBasic.setI(status == 0 ? "预警中" : "已完成");
  1451. result.put("basic", showValueCSBasic);
  1452. }else{//进水的展示
  1453. ShowValueJSBasic showValueJSBasic = new ShowValueJSBasic();
  1454. if(ZERO_SZ_WARNING.getCode().equals(remark)){//水质报警
  1455. showValueJSBasic.setH(ZAIXIANYIBIAO);
  1456. showValueJSBasic.setF(tXinyiWarningRecord.getLevel());
  1457. }else if(ONE_SH_WARNING.getCode().equals(remark)){//生化报警
  1458. showValueJSBasic.setH(ZAIXIANYIBIAO);
  1459. }else if(BusinessEnum.BigModelWarningTypeRemarkEnum.TWO_YC_WARNING.getCode().equals(remark)){//预测报警
  1460. showValueJSBasic.setH(YVCE);
  1461. // showValueCSBasic.setF(tXinyiWarningRecord.getLevel());
  1462. }else {//机器人化验室报警(特殊的 水质报警)
  1463. showValueJSBasic.setH(LIANXUJIANCE);
  1464. showValueJSBasic.setF(tXinyiWarningRecord.getLevel());
  1465. }
  1466. //通用的
  1467. // showValueBasic.setA(tXinyiWarningRecord.getReason());
  1468. showValueJSBasic.setB(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM ,warningTime));
  1469. showValueJSBasic.setC(DecimalUtils.getAbsAndScale(tXinyiWarningRecord.getWarningVal(), INT_2));
  1470. showValueJSBasic.setE(DecimalUtils.getAbsAndScale(tXinyiWarningRecord.getDesignVal(), INT_2));
  1471. showValueJSBasic.setG(Math.min(count, MAX_COUNT));
  1472. if(tXinyiWarningRecord.getType() != 2)
  1473. showValueJSBasic.setI(status == 0 ? "报警中" : status == 1 ? "用户关闭" : status == 2 ? "系统关闭" : "应急处理中");
  1474. else
  1475. showValueJSBasic.setI(status == 0 ? "预警中" : "已完成");
  1476. result.put("basic", showValueJSBasic);
  1477. }
  1478. JSONObject jsData = jsCsFormatData.getJsonObject(tXinyiIndustry, normConfig);//进水数据
  1479. result.put("jsData", jsData);
  1480. JSONObject csData = jsCsFormatData.getCsonObject(tXinyiIndustry, normConfig);//出水数据
  1481. result.put("csData", csData);
  1482. // return JSON.toJSONString(result, JSONWriter.Feature.WriteNulls);
  1483. return JSON.toJSONString(result);
  1484. }
  1485. private static JSONObject getJsonObject(TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
  1486. JSONObject jsData = new JSONObject();
  1487. HashMap<Object, Object> temp1 = new HashMap<>();
  1488. BigDecimal jsSlq = tXinyiIndustry.getJsSlq();
  1489. temp1.put("value", DecimalUtils.getAbsAndScale(jsSlq, INT_2));
  1490. temp1.put("exceed", jsSlq.compareTo(normConfig.getJsslSjz()) >0);
  1491. jsData.put("流量", temp1);
  1492. HashMap<Object, Object> temp2 = new HashMap<>();
  1493. BigDecimal jsCod = tXinyiIndustry.getJsCod();
  1494. temp2.put("value", DecimalUtils.getAbsAndScale(jsCod, INT_2));
  1495. temp2.put("exceed", jsCod.compareTo(normConfig.getJscodSjz()) > 0);
  1496. jsData.put("COD", temp2);
  1497. HashMap<Object, Object> temp3 = new HashMap<>();
  1498. BigDecimal jsNh3 = tXinyiIndustry.getJsNh3();
  1499. temp3.put("value", DecimalUtils.getAbsAndScale(jsNh3, INT_2));
  1500. temp3.put("exceed", jsNh3.compareTo(normConfig.getJsadSjz()) > 0);
  1501. jsData.put("NH3-N", temp3);
  1502. HashMap<Object, Object> temp4 = new HashMap<>();
  1503. BigDecimal jsTp = tXinyiIndustry.getJsTp();
  1504. temp4.put("value", DecimalUtils.getAbsAndScale(jsTp, INT_2));
  1505. temp4.put("exceed", jsTp.compareTo(normConfig.getJszlSjz()) > 0);
  1506. jsData.put("TP", temp4);
  1507. HashMap<Object, Object> temp5 = new HashMap<>();
  1508. BigDecimal jsSs = tXinyiIndustry.getJsSs();
  1509. temp5.put("value", DecimalUtils.getAbsAndScale(jsSs, INT_2));
  1510. temp5.put("exceed", jsSs.compareTo(normConfig.getJsssSjz()) > 0);
  1511. jsData.put("SS", temp5);
  1512. HashMap<Object, Object> temp6 = new HashMap<>();
  1513. BigDecimal jsTn = tXinyiIndustry.getJsTn();
  1514. temp6.put("value", DecimalUtils.getAbsAndScale(jsTn, INT_2));
  1515. temp6.put("exceed", jsTn.compareTo(normConfig.getJszdSjz()) > 0);
  1516. jsData.put("TN", temp6);
  1517. return jsData;
  1518. }
  1519. private static JSONObject getCsonObject(TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
  1520. JSONObject csData = new JSONObject();
  1521. HashMap<Object, Object> temp1 = new HashMap<>();
  1522. BigDecimal csSlq = tXinyiIndustry.getCsSlqc();
  1523. temp1.put("value", DecimalUtils.getAbsAndScale(csSlq, INT_2));
  1524. temp1.put("exceed", false);//出水水量没有管控值
  1525. csData.put("流量", temp1);
  1526. HashMap<Object, Object> temp2 = new HashMap<>();
  1527. BigDecimal csCod = tXinyiIndustry.getCsCod();
  1528. temp2.put("value", DecimalUtils.getAbsAndScale(csCod, INT_2));
  1529. temp2.put("exceed", csCod.compareTo(normConfig.getCscodGkz()) > 0);
  1530. csData.put("COD", temp2);
  1531. HashMap<Object, Object> temp3 = new HashMap<>();
  1532. BigDecimal csNh3 = tXinyiIndustry.getCsNh3();
  1533. temp3.put("value", DecimalUtils.getAbsAndScale(csNh3, INT_2));
  1534. temp3.put("exceed", csNh3.compareTo(normConfig.getCsadGkz()) > 0);
  1535. csData.put("NH3-N", temp3);
  1536. HashMap<Object, Object> temp4 = new HashMap<>();
  1537. BigDecimal csTp = tXinyiIndustry.getCsTp();
  1538. temp4.put("value", DecimalUtils.getAbsAndScale(csTp, INT_2));
  1539. temp4.put("exceed", csTp.compareTo(normConfig.getCszlGkz()) > 0);
  1540. csData.put("TP", temp4);
  1541. HashMap<Object, Object> temp5 = new HashMap<>();
  1542. BigDecimal csSs = tXinyiIndustry.getCsSs();
  1543. temp5.put("value", DecimalUtils.getAbsAndScale(csSs, INT_2));
  1544. temp5.put("exceed", csSs.compareTo(normConfig.getCsssGkz()) > 0);
  1545. csData.put("SS", temp5);
  1546. HashMap<Object, Object> temp6 = new HashMap<>();
  1547. BigDecimal csTn = tXinyiIndustry.getCsTn();
  1548. temp6.put("value", DecimalUtils.getAbsAndScale(csTn, INT_2));
  1549. temp6.put("exceed", csTn.compareTo(normConfig.getCszzGkz()) > 0);
  1550. csData.put("TN", temp6);
  1551. return csData;
  1552. }
  1553. /**
  1554. * 通过输入的值 生成对应类型的报警对象(出水)
  1555. *
  1556. * @param csBzz
  1557. * @param currentVal
  1558. * @param csGkz
  1559. * @param category
  1560. * @param tXinyiIndustry
  1561. * @param normConfig
  1562. * @return
  1563. */
  1564. private void handleXinYiWarningsCs(BigDecimal csBzz, BigDecimal currentVal, BigDecimal csGkz, String category, TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
  1565. BigDecimal multiply = csBzz.multiply(new BigDecimal(MyConstants.SCALE_VALUE));
  1566. TXinyiWarningRecord tXinyiWarningRecord = new TXinyiWarningRecord();
  1567. /*String category = BusinessEnum.WarningCategoryEnum.CS_AD.getCode();*/
  1568. tXinyiWarningRecord.setStatus(0);
  1569. tXinyiWarningRecord.setType(0);
  1570. tXinyiWarningRecord.setCategory(category);
  1571. tXinyiWarningRecord.setTime(DateUtils.getNowDate());
  1572. tXinyiWarningRecord.setWarningVal(currentVal);
  1573. tXinyiWarningRecord.setDesignVal(csBzz);
  1574. tXinyiWarningRecord.setControlVal(csGkz);
  1575. tXinyiWarningRecord.setCreateBy(WARNING_DEFAULT_CREATE);
  1576. tXinyiWarningRecord.setCreateTime(DateUtils.getNowDate());
  1577. tXinyiWarningRecord.setRemark(ZERO_SZ_WARNING.getCode());
  1578. //2024年5月25日17:52:33 如果工业库获取不到数据,也触发报警,但是不调用决策接口
  1579. if(Objects.isNull(currentVal) || currentVal.compareTo(BigDecimal.ZERO) == 0){
  1580. tXinyiWarningRecord.setReason(category + EXCEPTION_WARNING);
  1581. tXinyiWarningRecord.setLevel(WARNING_LEVEL_NO_DATE);
  1582. }else if(currentVal.compareTo(multiply) > 0){//一级
  1583. tXinyiWarningRecord.setReason(category + CHAOBIAO_WARNING);
  1584. tXinyiWarningRecord.setLevel(WARNING_LEVEL_ONE);
  1585. }else if(currentVal.compareTo(csBzz) >= 0 && currentVal.compareTo(multiply) <= 0){//二级
  1586. tXinyiWarningRecord.setReason(category + CHAOBIAO_WARNING);
  1587. tXinyiWarningRecord.setLevel(WARNING_LEVEL_TWO);
  1588. }else if(!Objects.isNull(csGkz) && currentVal.compareTo(csGkz) > 0){
  1589. tXinyiWarningRecord.setReason(category + CHAOGUANKONG_WARNING);
  1590. tXinyiWarningRecord.setLevel(WARNING_LEVEL_THREE);
  1591. }else{
  1592. tXinyiWarningRecord = null;//这种的无需处理
  1593. }
  1594. //当前状态正常 需要查询历史有无正在报警的数据,如果有,将报警状态改完2(系统自动关闭)
  1595. List<TXinyiWarningRecord> tXinyiWarningRecords = this.xinyiWarningRecordMapper.selectTXinyiWarningRecordList(TXinyiWarningRecord.builder().delFlag(0).type(0).category(category).warningStatus(0).build());
  1596. if(Objects.isNull(tXinyiWarningRecord)){//数据正常,无告警信息
  1597. if(!CollectionUtils.isEmpty(tXinyiWarningRecords)){
  1598. log.info( "{}:现在恢复正常,历史报警数据为{}", category,JSON.toJSONString(tXinyiWarningRecords));
  1599. for (TXinyiWarningRecord xinyiWarningRecord : tXinyiWarningRecords) {
  1600. xinyiWarningRecord.setStatus(2);
  1601. Date nowDate = DateUtils.getNowDate();
  1602. xinyiWarningRecord.setOffTime(nowDate);
  1603. xinyiWarningRecord.setUpdateTime(nowDate);
  1604. xinyiWarningRecord.setUpdateBy(WARNING_DEFAULT_CREATE);
  1605. this.xinyiWarningRecordMapper.updateTXinyiWarningRecord(xinyiWarningRecord);
  1606. }
  1607. }
  1608. }else{//有告警信息
  1609. Date nowDate = DateUtils.getNowDate();
  1610. if(CollectionUtils.isEmpty(tXinyiWarningRecords)){//之前没有告警记录
  1611. //保存到数据库中
  1612. //2024年7月15日11:06:16 因为只有一个告警记录,但是如果一直报警,现在报警时间取的是更新时间,兼容第一次报警处理
  1613. tXinyiWarningRecord.setUpdateTime(nowDate);
  1614. this.xinyiWarningRecordMapper.insertTXinyiWarningRecord(tXinyiWarningRecord);
  1615. if(WARNING_LEVEL_NO_DATE.equals(tXinyiWarningRecord.getLevel())){
  1616. //只保存一个普通的问答记录 不需要调用决策信息,但是实时数据还是要记录的
  1617. this.addChatRecordByDeviceErr(tXinyiWarningRecord, tXinyiIndustry, normConfig);
  1618. }else {
  1619. //继续调用决策
  1620. this.handleDecision(tXinyiWarningRecord, tXinyiIndustry, normConfig, false, nowDate);
  1621. }
  1622. }else{
  1623. log.info("{}:之前已经有过告警记录了,且还是继续报警,无需重复添加报警,但是决策仍然要调用", category);
  1624. for (TXinyiWarningRecord xinyiWarningRecord : tXinyiWarningRecords) {//理论上只有一个的
  1625. if(WARNING_LEVEL_NO_DATE.equals(tXinyiWarningRecord.getLevel())){
  1626. //只保存一个普通的问答记录 不需要调用决策信息,但是实时数据还是要记录的
  1627. this.addChatRecordByDeviceErr(tXinyiWarningRecord, tXinyiIndustry, normConfig);
  1628. }else {
  1629. //2024年7月5日10:45:24 逻辑调整:更新一直报警的那条记录的显示值
  1630. xinyiWarningRecord.setWarningVal(tXinyiWarningRecord.getWarningVal());
  1631. // xinyiWarningRecord.setDesignVal(tXinyiWarningRecord.getDesignVal());
  1632. // tXinyiWarningRecord.setControlVal(tXinyiWarningRecord.getControlVal());
  1633. xinyiWarningRecord.setTime(tXinyiWarningRecord.getTime());
  1634. xinyiWarningRecord.setUpdateTime(nowDate);
  1635. xinyiWarningRecord.setUpdateBy(WARNING_DEFAULT_CREATE);
  1636. //2024年7月15日11:07:33 报警的级别也要重新计算并更新
  1637. xinyiWarningRecord.setLevel(tXinyiWarningRecord.getLevel());
  1638. //更新数据库
  1639. this.xinyiWarningRecordMapper.updateTXinyiWarningRecord(xinyiWarningRecord);
  1640. //继续调用决策
  1641. this.handleDecision(xinyiWarningRecord, tXinyiIndustry, normConfig, false, nowDate);
  1642. }
  1643. }
  1644. }
  1645. }
  1646. }
  1647. /**
  1648. * 通过输入的值 生成对应类型的报警对象(进水)
  1649. *
  1650. * @param jsBzz
  1651. * @param currentVal
  1652. * @param category
  1653. * @param tXinyiIndustry
  1654. * @param normConfig
  1655. * @return
  1656. */
  1657. private void handleXinYiWarningRecordJS(BigDecimal jsBzz, BigDecimal currentVal, String category, TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
  1658. BigDecimal multiply = jsBzz.multiply(new BigDecimal(MyConstants.SCALE_VALUE));
  1659. TXinyiWarningRecord tXinyiWarningRecord = new TXinyiWarningRecord();
  1660. /*String category = BusinessEnum.WarningCategoryEnum.CS_AD.getCode();*/
  1661. tXinyiWarningRecord.setStatus(0);
  1662. tXinyiWarningRecord.setType(0);
  1663. tXinyiWarningRecord.setCategory(category);
  1664. tXinyiWarningRecord.setTime(DateUtils.getNowDate());
  1665. tXinyiWarningRecord.setWarningVal(currentVal);
  1666. tXinyiWarningRecord.setDesignVal(jsBzz);
  1667. // tXinyiWarningRecord.setControlVal(csGkz);
  1668. tXinyiWarningRecord.setCreateBy(WARNING_DEFAULT_CREATE);
  1669. tXinyiWarningRecord.setCreateTime(DateUtils.getNowDate());
  1670. tXinyiWarningRecord.setRemark(ZERO_SZ_WARNING.getCode());
  1671. //2024年5月25日17:52:33 如果工业库获取不到数据,也触发报警,但是不调用决策接口
  1672. if(Objects.isNull(currentVal) || currentVal.compareTo(BigDecimal.ZERO) == 0){
  1673. tXinyiWarningRecord.setReason(category + EXCEPTION_WARNING);
  1674. tXinyiWarningRecord.setLevel(WARNING_LEVEL_NO_DATE);
  1675. }else if(currentVal.compareTo(multiply) > 0){//一级
  1676. tXinyiWarningRecord.setReason(category + CHAOBIAO_WARNING);
  1677. tXinyiWarningRecord.setLevel(WARNING_LEVEL_ONE);
  1678. }else if(currentVal.compareTo(jsBzz) >= 0 && currentVal.compareTo(multiply) <= 0){//二级
  1679. tXinyiWarningRecord.setReason(category + CHAOBIAO_WARNING);
  1680. tXinyiWarningRecord.setLevel(WARNING_LEVEL_TWO);
  1681. }else{
  1682. tXinyiWarningRecord = null;//这种的无需处理
  1683. }
  1684. /*else if(!Objects.isNull(csGkz) && currentVal.compareTo(csGkz) > 0){
  1685. tXinyiWarningRecord.setReason(category + CHAOGUANKONG_WARNING);
  1686. tXinyiWarningRecord.setLevel(WARNING_LEVEL_THREE);
  1687. }*/
  1688. //当前状态正常 需要查询历史有无正在报警的数据,如果有,将报警状态改完2(系统自动关闭)
  1689. List<TXinyiWarningRecord> tXinyiWarningRecords = this.xinyiWarningRecordMapper.selectTXinyiWarningRecordList(TXinyiWarningRecord.builder().delFlag(0).type(0).category(category).status(0).build());
  1690. if(Objects.isNull(tXinyiWarningRecord)){//数据正常,无告警信息
  1691. if(!CollectionUtils.isEmpty(tXinyiWarningRecords)){
  1692. log.info( "{}:现在恢复正常,历史报警数据为{}", category,JSON.toJSONString(tXinyiWarningRecords));
  1693. for (TXinyiWarningRecord xinyiWarningRecord : tXinyiWarningRecords) {
  1694. xinyiWarningRecord.setStatus(2);
  1695. Date nowDate = DateUtils.getNowDate();
  1696. xinyiWarningRecord.setOffTime(nowDate);
  1697. xinyiWarningRecord.setUpdateTime(nowDate);
  1698. xinyiWarningRecord.setUpdateBy(WARNING_DEFAULT_CREATE);
  1699. this.xinyiWarningRecordMapper.updateTXinyiWarningRecord(xinyiWarningRecord);
  1700. }
  1701. }
  1702. }else{//有告警信息
  1703. Date nowDate = DateUtils.getNowDate();
  1704. if(CollectionUtils.isEmpty(tXinyiWarningRecords)){//之前没有告警记录
  1705. //保存到数据库中
  1706. //2024年7月15日11:06:16 因为只有一个告警记录,但是如果一直报警,现在报警时间取的是更新时间,兼容第一次报警处理
  1707. tXinyiWarningRecord.setUpdateTime(nowDate);
  1708. this.xinyiWarningRecordMapper.insertTXinyiWarningRecord(tXinyiWarningRecord);
  1709. if(WARNING_LEVEL_NO_DATE.equals(tXinyiWarningRecord.getLevel())){
  1710. //只保存一个普通的问答记录 不需要调用决策信息,但是实时数据还是要记录的
  1711. this.addChatRecordByDeviceErr(tXinyiWarningRecord, tXinyiIndustry, normConfig);
  1712. }else {
  1713. //继续调用决策
  1714. this.handleDecision(tXinyiWarningRecord, tXinyiIndustry, normConfig, false, nowDate);
  1715. }
  1716. }else{
  1717. log.info("{}:之前已经有过告警记录了,且还是继续报警,无需重复添加报警,但是决策仍然要调用", category);
  1718. for (TXinyiWarningRecord xinyiWarningRecord : tXinyiWarningRecords) {//理论上只有一个的
  1719. if(WARNING_LEVEL_NO_DATE.equals(tXinyiWarningRecord.getLevel())){
  1720. //只保存一个普通的问答记录 不需要调用决策信息,但是实时数据还是要记录的
  1721. this.addChatRecordByDeviceErr(tXinyiWarningRecord, tXinyiIndustry, normConfig);
  1722. }else {
  1723. //2024年7月5日10:45:24 逻辑调整:更新一直报警的那条记录的显示值
  1724. xinyiWarningRecord.setWarningVal(tXinyiWarningRecord.getWarningVal());
  1725. // xinyiWarningRecord.setDesignVal(tXinyiWarningRecord.getDesignVal());
  1726. // tXinyiWarningRecord.setControlVal(tXinyiWarningRecord.getControlVal());
  1727. xinyiWarningRecord.setTime(tXinyiWarningRecord.getTime());
  1728. xinyiWarningRecord.setUpdateTime(nowDate);
  1729. xinyiWarningRecord.setUpdateBy(WARNING_DEFAULT_CREATE);
  1730. //2024年7月15日11:07:33 报警的级别也要重新计算并更新
  1731. xinyiWarningRecord.setLevel(tXinyiWarningRecord.getLevel());
  1732. //更新数据库
  1733. this.xinyiWarningRecordMapper.updateTXinyiWarningRecord(xinyiWarningRecord);
  1734. //继续调用决策
  1735. this.handleDecision(xinyiWarningRecord, tXinyiIndustry, normConfig, false, nowDate);
  1736. }
  1737. }
  1738. }
  1739. }
  1740. }
  1741. private void addChatRecordByDeviceErr(TXinyiWarningRecord tXinyiWarningRecord, TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
  1742. ChatReq chatReq = new ChatReq();
  1743. //保存聊天记录
  1744. //将问答更新到数据库中
  1745. chatReq.setSessionId(IdUtils.simpleUUID());
  1746. chatReq.setType(1);//0问答 1决策 2本地 3仿真预测
  1747. chatReq.setModule(3);
  1748. /*String userId = SecurityUtils.getUserId().toString();
  1749. String username = SecurityUtils.getUsername();*/
  1750. chatReq.setUserId(WARNING_DEFAULT_CREATE);
  1751. String showVal = this.buildShowValue(tXinyiWarningRecord, tXinyiIndustry, normConfig, DateUtils.getNowDate());
  1752. chatReq.setShowVal(showVal);//前端展示的数据和提问的数据不一致
  1753. chatReq.setQuestion(WARNING_DEFAULT_QUESTION);//本地问题
  1754. chatReq.setAnswer(tXinyiWarningRecord.getReason() + ",请检查设备是否正常运行");
  1755. chatReq.setWarningId(String.valueOf(tXinyiWarningRecord.getId()));
  1756. chatReq.setCounts(1);//问答次数
  1757. chatReq.setCreateBy(WARNING_DEFAULT_CREATE);
  1758. chatReq.setCreateTime(DateUtils.getNowDate());
  1759. this.xinyiChatRecordMapper.insertTXinyiChatRecord(chatReq);
  1760. }
  1761. public static void main(String[] args) {
  1762. System.out.println(CCCDCHRT_LAST);
  1763. System.out.println(GSLS_LAST);
  1764. System.out.println(BigDecimal.valueOf(1).subtract(null));
  1765. }
  1766. /**
  1767. * 处理机器人化验数据报警
  1768. * @param uniqueList
  1769. */
  1770. public void handleRobotWarning(List<TXinyiRobot> uniqueList) {
  1771. log.info("进入了定时任务判断机器人化验库是否超标及后续逻辑");
  1772. //查询配置
  1773. List<TXinyiNormConfig> tXinyiNormConfigs = this.xinyiNormConfigMapper.selectTXinyiNormConfigList(null);
  1774. if(CollectionUtils.isEmpty(tXinyiNormConfigs))
  1775. throw new RuntimeException("未查询到配置信息");
  1776. TXinyiNormConfig normConfig = tXinyiNormConfigs.get(0);
  1777. //查询最新的一条工业库的数据(showvalue展示使用)
  1778. List<TXinyiIndustry> tXinyiIndustries = this.xinyiIndustryMapper.selectNIndustry(1);
  1779. TXinyiIndustry tXinyiIndustry = CollectionUtils.isEmpty(tXinyiIndustries) ? new TXinyiIndustry() : tXinyiIndustries.get(0);
  1780. //处理数据
  1781. for (TXinyiRobot tXinyiRobot : uniqueList) {
  1782. //2024年7月11日17:27:32 化验室报警项原有总氮和总氮改为 1#好氧硝酸盐、2#好氧硝酸盐、二沉池正磷酸盐
  1783. //处理总氮和总磷报警
  1784. //总氮
  1785. /*BigDecimal no3Hlj1Jqr = tXinyiRobot.getNo3Hlj1Jqr();
  1786. BigDecimal no3Hlj2Jqr = tXinyiRobot.getNo3Hlj2Jqr();
  1787. if(!Objects.isNull(no3Hlj1Jqr) && !Objects.isNull(no3Hlj2Jqr)){
  1788. BigDecimal csTn = (no3Hlj1Jqr.add(no3Hlj2Jqr)).divide((new BigDecimal("2").multiply(ROBOT_HY_DIVIDE)), NUMBER_SCALE_4, RoundingMode.HALF_UP);
  1789. BigDecimal cszzBzz = normConfig.getCszzBzz();
  1790. BigDecimal cszzGkz = normConfig.getCszzGkz();
  1791. if(!Objects.isNull(cszzBzz)){
  1792. handleXinYiWarningsRobot(cszzBzz, csTn, cszzGkz, BusinessEnum.WarningCategoryEnum.CS_ZD.getCode(), tXinyiIndustry, normConfig);
  1793. }
  1794. }*/
  1795. //#1好氧硝酸盐
  1796. BigDecimal no3Hlj1Jqr = tXinyiRobot.getNo3Hlj1Jqr();
  1797. if(!Objects.isNull(no3Hlj1Jqr)){
  1798. BigDecimal cszzBzz = normConfig.getCszzBzz();
  1799. BigDecimal cszzGkz = normConfig.getCszzGkz();
  1800. if(!Objects.isNull(cszzBzz)){
  1801. handleXinYiWarningsRobot(cszzBzz, no3Hlj1Jqr, cszzGkz, ROBOT_XSY_1.getCode(), tXinyiIndustry, normConfig);
  1802. }
  1803. }
  1804. //#2好氧硝酸盐
  1805. BigDecimal no3Hlj2Jqr = tXinyiRobot.getNo3Hlj2Jqr();
  1806. if(!Objects.isNull(no3Hlj2Jqr)){
  1807. BigDecimal cszzBzz = normConfig.getCszzBzz();
  1808. BigDecimal cszzGkz = normConfig.getCszzGkz();
  1809. if(!Objects.isNull(cszzBzz)){
  1810. handleXinYiWarningsRobot(cszzBzz, no3Hlj2Jqr, cszzGkz, ROBOT_XSY_2.getCode(), tXinyiIndustry, normConfig);
  1811. }
  1812. }
  1813. //总磷
  1814. BigDecimal tpRccJqr = tXinyiRobot.getTpRccJqr();
  1815. if(!Objects.isNull(tpRccJqr)){
  1816. BigDecimal csTp = tpRccJqr.divide(ROBOT_HY_DIVIDE, NUMBER_SCALE_4, RoundingMode.HALF_UP);
  1817. BigDecimal cszlBzz = normConfig.getCszlBzz();
  1818. BigDecimal cszlGkz = normConfig.getCszlGkz();
  1819. if(!Objects.isNull(cszlBzz)){
  1820. handleXinYiWarningsRobot(cszlBzz, csTp, cszlGkz, ROBOT_ECCZLSY.getCode(), tXinyiIndustry, normConfig);
  1821. }
  1822. }
  1823. }
  1824. }
  1825. /**
  1826. * 通过输入的值 生成对应类型的报警对象(出水)
  1827. *
  1828. * @param csBzz
  1829. * @param currentVal
  1830. * @param csGkz
  1831. * @param category
  1832. * @param tXinyiIndustry
  1833. * @param normConfig
  1834. * @return
  1835. */
  1836. private void handleXinYiWarningsRobot(BigDecimal csBzz, BigDecimal currentVal, BigDecimal csGkz, String category, TXinyiIndustry tXinyiIndustry, TXinyiNormConfig normConfig) {
  1837. BigDecimal multiply = csBzz.multiply(new BigDecimal(MyConstants.SCALE_VALUE));
  1838. TXinyiWarningRecord tXinyiWarningRecord = new TXinyiWarningRecord();
  1839. tXinyiWarningRecord.setStatus(0);
  1840. tXinyiWarningRecord.setType(0);
  1841. tXinyiWarningRecord.setCategory(category);
  1842. tXinyiWarningRecord.setTime(DateUtils.getNowDate());
  1843. tXinyiWarningRecord.setWarningVal(currentVal);
  1844. tXinyiWarningRecord.setDesignVal(csBzz);
  1845. tXinyiWarningRecord.setControlVal(csGkz);
  1846. tXinyiWarningRecord.setCreateBy(WARNING_DEFAULT_CREATE);
  1847. tXinyiWarningRecord.setCreateTime(DateUtils.getNowDate());
  1848. tXinyiWarningRecord.setRemark(THREE_HYS_WARNING.getCode());//化验室
  1849. //2024年5月25日17:52:33 如果工业库获取不到数据,也触发报警,但是不调用决策接口
  1850. if(Objects.isNull(currentVal)){
  1851. tXinyiWarningRecord.setReason(category + EXCEPTION_WARNING);
  1852. tXinyiWarningRecord.setLevel(WARNING_LEVEL_NO_DATE);
  1853. }else if(currentVal.compareTo(multiply) > 0){//一级
  1854. tXinyiWarningRecord.setReason(category + CHAOBIAO_WARNING);
  1855. tXinyiWarningRecord.setLevel(WARNING_LEVEL_ONE);
  1856. }else if(currentVal.compareTo(csBzz) >= 0 && currentVal.compareTo(multiply) <= 0){//二级
  1857. tXinyiWarningRecord.setReason(category + CHAOBIAO_WARNING);
  1858. tXinyiWarningRecord.setLevel(WARNING_LEVEL_TWO);
  1859. }else if(!Objects.isNull(csGkz) && currentVal.compareTo(csGkz) > 0){
  1860. tXinyiWarningRecord.setReason(category + CHAOGUANKONG_WARNING);
  1861. tXinyiWarningRecord.setLevel(WARNING_LEVEL_THREE);
  1862. }else{
  1863. tXinyiWarningRecord = null;//这种的无需处理
  1864. }
  1865. //当前状态正常 需要查询历史有无正在报警的数据,如果有,将报警状态改完2(系统自动关闭)
  1866. List<TXinyiWarningRecord> tXinyiWarningRecords = this.xinyiWarningRecordMapper.selectTXinyiWarningRecordList(TXinyiWarningRecord.builder().delFlag(0).type(0).category(category).warningStatus(0).build());
  1867. if(Objects.isNull(tXinyiWarningRecord)){//数据正常,无告警信息
  1868. if(!CollectionUtils.isEmpty(tXinyiWarningRecords)){
  1869. log.info( "{}:现在恢复正常,历史报警数据为{}", category,JSON.toJSONString(tXinyiWarningRecords));
  1870. for (TXinyiWarningRecord xinyiWarningRecord : tXinyiWarningRecords) {
  1871. xinyiWarningRecord.setStatus(2);
  1872. Date nowDate = DateUtils.getNowDate();
  1873. xinyiWarningRecord.setOffTime(nowDate);
  1874. xinyiWarningRecord.setUpdateTime(nowDate);
  1875. xinyiWarningRecord.setUpdateBy(WARNING_DEFAULT_CREATE);
  1876. this.xinyiWarningRecordMapper.updateTXinyiWarningRecord(xinyiWarningRecord);
  1877. }
  1878. }
  1879. }else{//有告警信息
  1880. Date nowDate = DateUtils.getNowDate();
  1881. if(CollectionUtils.isEmpty(tXinyiWarningRecords)){//之前没有告警记录
  1882. //保存到数据库中
  1883. //2024年7月15日11:06:16 因为只有一个告警记录,但是如果一直报警,现在报警时间取的是更新时间,兼容第一次报警处理
  1884. tXinyiWarningRecord.setUpdateTime(nowDate);
  1885. this.xinyiWarningRecordMapper.insertTXinyiWarningRecord(tXinyiWarningRecord);
  1886. if(WARNING_LEVEL_NO_DATE.equals(tXinyiWarningRecord.getLevel())){
  1887. //只保存一个普通的问答记录 不需要调用决策信息,但是实时数据还是要记录的
  1888. this.addChatRecordByDeviceErr(tXinyiWarningRecord, tXinyiIndustry, normConfig);
  1889. }else {
  1890. //继续调用决策
  1891. this.handleDecision(tXinyiWarningRecord, tXinyiIndustry, normConfig, true, nowDate);
  1892. }
  1893. }else{
  1894. log.info("{}:之前已经有过告警记录了,且还是继续报警,无需重复添加报警,但是决策仍然要调用", category);
  1895. for (TXinyiWarningRecord xinyiWarningRecord : tXinyiWarningRecords) {//理论上只有一个的
  1896. if(WARNING_LEVEL_NO_DATE.equals(tXinyiWarningRecord.getLevel())){
  1897. //只保存一个普通的问答记录 不需要调用决策信息,但是实时数据还是要记录的
  1898. this.addChatRecordByDeviceErr(tXinyiWarningRecord, tXinyiIndustry, normConfig);
  1899. }else {
  1900. //2024年7月5日10:45:24 逻辑调整:更新一直报警的那条记录的显示值
  1901. xinyiWarningRecord.setWarningVal(tXinyiWarningRecord.getWarningVal());
  1902. // xinyiWarningRecord.setDesignVal(tXinyiWarningRecord.getDesignVal());
  1903. // tXinyiWarningRecord.setControlVal(tXinyiWarningRecord.getControlVal());
  1904. xinyiWarningRecord.setTime(tXinyiWarningRecord.getTime());
  1905. xinyiWarningRecord.setUpdateTime(nowDate);
  1906. xinyiWarningRecord.setUpdateBy(WARNING_DEFAULT_CREATE);
  1907. //2024年7月15日11:07:33 报警的级别也要重新计算并更新
  1908. xinyiWarningRecord.setLevel(tXinyiWarningRecord.getLevel());
  1909. //更新数据库
  1910. this.xinyiWarningRecordMapper.updateTXinyiWarningRecord(xinyiWarningRecord);
  1911. //继续调用决策
  1912. this.handleDecision(xinyiWarningRecord, tXinyiIndustry, normConfig, true, nowDate);
  1913. }
  1914. }
  1915. }
  1916. }
  1917. }
  1918. /**
  1919. *
  1920. * 大宽表的数据处理
  1921. * @param tXinyiRobot
  1922. */
  1923. public void updateBigTableHourByRobot(TXinyiRobot tXinyiRobot) {
  1924. List<TXinyiBigTableHour> tXinyiBigTableHours = this.xinyiBigTableHourMapper.selectTXinyiBigTableHourList(TXinyiBigTableHour.builder().testHour(tXinyiRobot.getTestHour()).build());
  1925. if(!CollectionUtils.isEmpty(tXinyiBigTableHours)){
  1926. for (TXinyiBigTableHour tXinyiBigTableHour : tXinyiBigTableHours) {
  1927. this.updateBigTableByRobot(tXinyiBigTableHour, tXinyiRobot);
  1928. }
  1929. }
  1930. }
  1931. private void updateBigTableByRobot(TXinyiBigTableHour tXinyiBigTableHour, TXinyiRobot tXinyiRobot) {
  1932. tXinyiBigTableHour.setHyCodYb(tXinyiRobot.getCodYb());
  1933. tXinyiBigTableHour.setHyCodJqr(tXinyiRobot.getCodJqr());
  1934. tXinyiBigTableHour.setHyCodYz(tXinyiRobot.getCodYz());
  1935. tXinyiBigTableHour.setHyTpHl1Jqr(tXinyiRobot.getTpHl1Jqr());
  1936. tXinyiBigTableHour.setHyTpHl2Jqr(tXinyiRobot.getTpHl2Jqr());
  1937. tXinyiBigTableHour.setHyTpRccJqr(tXinyiRobot.getTpRccJqr());
  1938. tXinyiBigTableHour.setHyTp1Yz(tXinyiRobot.getTp1Yz());
  1939. tXinyiBigTableHour.setHyTp2Yz(tXinyiRobot.getTp2Yz());
  1940. tXinyiBigTableHour.setHyTpRccYz(tXinyiRobot.getTpRccYz());
  1941. tXinyiBigTableHour.setHyNh31Jqr(tXinyiRobot.getNh31Jqr());
  1942. tXinyiBigTableHour.setHyNh32Jqr(tXinyiRobot.getNh32Jqr());
  1943. tXinyiBigTableHour.setHyNh31Yz(tXinyiRobot.getNh31Yz());
  1944. tXinyiBigTableHour.setHyNo3Hlc1Yz(tXinyiRobot.getNo3Hlc1Yz());
  1945. tXinyiBigTableHour.setHyNh32Yz(tXinyiRobot.getNh32Yz());
  1946. tXinyiBigTableHour.setHyNo3Hlc2Yz(tXinyiRobot.getNo3Hlc2Yz());
  1947. tXinyiBigTableHour.setHyNo3Hlj1Jqr(tXinyiRobot.getNo3Hlj1Jqr());
  1948. tXinyiBigTableHour.setHyNo3Hlj2Jqr(tXinyiRobot.getNo3Hlj2Jqr());
  1949. tXinyiBigTableHour.setHyTyll(tXinyiRobot.getTYLL());
  1950. tXinyiBigTableHour.setHyNo3Qyc1Jqr(tXinyiRobot.getNo3Qyc1Jqr());
  1951. tXinyiBigTableHour.setHyNo3Qyc2Jqr(tXinyiRobot.getNo3Qyc2Jqr());
  1952. tXinyiBigTableHour.setHyNo3Qyc1Yz(tXinyiRobot.getNo3Qyc1Yz());
  1953. tXinyiBigTableHour.setHyNo3Qyc2Yz(tXinyiRobot.getNo3Qyc2Yz());
  1954. tXinyiBigTableHour.setHyJsll(tXinyiRobot.getJSLL());
  1955. tXinyiBigTableHour.setHyHycxsyAll(tXinyiRobot.getHycxsyAll());
  1956. tXinyiBigTableHour.setHyQyanAll(tXinyiRobot.getQyanAll());
  1957. tXinyiBigTableHour.setHyQyckxsyAll(tXinyiRobot.getQyckxsyAll());
  1958. tXinyiBigTableHour.setHyHyzlsyAll(tXinyiRobot.getHyzlsyAll());
  1959. //更新数据库
  1960. this.xinyiBigTableHourMapper.updateTXinyiBigTableHour(tXinyiBigTableHour);
  1961. }
  1962. }