AsyncTask.java 147 KB

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