FrontServiceImpl.java 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. package com.slibra.business.service.impl;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import com.slibra.business.domain.*;
  5. import com.slibra.business.mapper.*;
  6. import com.slibra.business.req.ChatReq;
  7. import com.slibra.business.req.WorkOrderReq;
  8. import com.slibra.business.res.WorkOrderRes;
  9. import com.slibra.business.res.XinyiDailyFeeSimple;
  10. import com.slibra.business.res.XinyiIndustrySimple;
  11. import com.slibra.business.service.IFrontService;
  12. import com.slibra.common.DecimalUtils;
  13. import com.slibra.common.core.domain.TXinyiDaily;
  14. import com.slibra.common.core.redis.RedisCache;
  15. import com.slibra.common.exception.ServiceException;
  16. import com.slibra.common.utils.DateUtils;
  17. import com.slibra.common.utils.SecurityUtils;
  18. import com.slibra.common.utils.StringUtils;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.springframework.beans.BeanUtils;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.data.redis.connection.stream.RecordId;
  23. import org.springframework.stereotype.Service;
  24. import org.springframework.util.CollectionUtils;
  25. import java.math.BigDecimal;
  26. import java.math.RoundingMode;
  27. import java.time.LocalDate;
  28. import java.time.LocalDateTime;
  29. import java.time.format.DateTimeFormatter;
  30. import java.util.*;
  31. import static com.slibra.common.constant.MyConstants.*;
  32. import static com.slibra.common.enums.BusinessEnum.BigModelBizEnum.DECISION_REPORT;
  33. import static com.slibra.common.utils.DateUtils.getAllDatesBetween;
  34. @Service
  35. @Slf4j
  36. public class FrontServiceImpl implements IFrontService {
  37. @Autowired
  38. private TXinyiIndustryMapper xinyiIndustryMapper;
  39. @Autowired
  40. private TXinyiDailyMapper xinyiDailyMapper;
  41. @Autowired
  42. private TXinyiRobotMapper xinyiRobotMapper;
  43. @Autowired
  44. private TXinyiNormConfigMapper tXinyiNormConfigMapper;
  45. @Autowired
  46. private TXinyiWarningRecordMapper xinyiWarningRecordMapper;
  47. @Autowired
  48. private TXinyiRecommendQaMapper xinyiRecommendQaMapper;
  49. @Autowired
  50. private TXinyiChatRecordMapper xinyiChatRecordMapper;
  51. @Autowired
  52. private RedisCache redisCache;
  53. @Override
  54. public XinyiIndustrySimple countInfo() {
  55. //查询基本的工业库数据
  56. XinyiIndustrySimple xinyiIndustrySimple = new XinyiIndustrySimple();
  57. LocalDateTime nowTime = LocalDateTime.now();
  58. //调用接口查询指标
  59. String[] queryTags = {"信义污水厂JS_COD_Value", "信义污水厂升级出水COD", "信义污水厂JS_AD_Value", "信义污水厂升级出水氨氮", "信义污水厂JS_ZA_Value", "信义污水厂升级出水TN", "信义污水厂JS_ZL_Value", "信义污水厂升级出水TP", "信义污水厂JS_SS_Value", "信义污水厂升级出水SS", "信义污水厂JS_PH_Value", "信义污水厂升级出水PH", "信义污水厂FT101_Value", "信义污水厂出水瞬时流量"};
  60. xinyiIndustrySimple = this.getRealTimeData(queryTags, nowTime);
  61. //处理日报数据
  62. this.addDailyData(xinyiIndustrySimple, nowTime);
  63. //处理配置信息
  64. this.addConfigData(xinyiIndustrySimple);
  65. //计算
  66. this.calculateData(xinyiIndustrySimple, nowTime);
  67. //当前的小事数
  68. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.YYYYMMDDHH_CHINESE);
  69. xinyiIndustrySimple.setTestHour(formatter.format(nowTime) + ":00");//只要小时 但是要后面的:00
  70. return xinyiIndustrySimple;
  71. }
  72. @Override
  73. public List<String> warningCount() {
  74. ArrayList<String> result = new ArrayList<>();
  75. //获取告警的统计数量
  76. int count = this.xinyiWarningRecordMapper.getWarningCountByType(1);
  77. if(count > 0)//2024年5月26日19:01:39 有告警记录再返回
  78. result.add(WARNING_TEMPLATE.replace("#", String.valueOf(count)));
  79. //获取报警警的统计数量
  80. count = this.xinyiWarningRecordMapper.getWarningCountByType(2);
  81. if(count > 0)//2024年5月26日19:01:39 有告警记录再返回
  82. result.add(FORECAST_WARNING_TEMPLATE.replace("#", String.valueOf(count)));
  83. return result;
  84. }
  85. @Override
  86. public List<TXinyiWarningRecord> homeWarningList() {
  87. return this.xinyiWarningRecordMapper.homeWarningList();
  88. }
  89. @Override
  90. public List<TXinyiRecommendQa> recommendQAList(Integer type) {
  91. return this.xinyiRecommendQaMapper.selectTXinyiRecommendQaList(TXinyiRecommendQa.builder().type(type).build());
  92. }
  93. /**
  94. * 2024年6月23日17:30:07 因为只展示一条数据,所以判断是不是最后一条数据是report类型的,如果是就返回; 如果不是,获取是定时任务生成的最近的一条数据
  95. * 2024年6月23日20:13:34 因为决策和预测走的同一个接口,所以把type去掉,否则type为1获取不到预测数据
  96. * @param warningId
  97. * @return
  98. */
  99. @Override
  100. public TXinyiChatRecord qaDetailByWarningId(String warningId) {
  101. //2024年5月28日19:10:21 只获取决策的 因为决策和仿真预测是一个接口,所以通过类型区分
  102. List<TXinyiChatRecord> tXinyiChatRecords = this.xinyiChatRecordMapper.selectTXinyiChatRecordList(TXinyiChatRecord.builder().warningId(warningId).type(1).build());
  103. if(CollectionUtils.isEmpty(tXinyiChatRecords))
  104. return null;
  105. TXinyiChatRecord tXinyiChatRecord = tXinyiChatRecords.get(0);
  106. //todo 表增肌一个字段,记录类型,否则查询速度会慢
  107. if(tXinyiChatRecord.getAnswer().contains(DECISION_REPORT.getCode()))
  108. return tXinyiChatRecord;
  109. else{
  110. tXinyiChatRecords = this.xinyiChatRecordMapper.selectTXinyiChatRecordList(TXinyiChatRecord.builder().warningId(warningId).type(1).userId(WARNING_DEFAULT_CREATE).build());
  111. return CollectionUtils.isEmpty(tXinyiChatRecords) ? null : tXinyiChatRecords.get(0);
  112. }
  113. }
  114. @Override
  115. public String isSatisfiedAnswer(ChatReq chatReq) {
  116. Long id = chatReq.getId();
  117. Integer isSatisfied = chatReq.getIsSatisfied();
  118. if(Objects.isNull(id) || Objects.isNull(isSatisfied))
  119. throw new ServiceException("请输入正确的参数");
  120. TXinyiChatRecord tXinyiChatRecord = this.xinyiChatRecordMapper.selectTXinyiChatRecordById(id);
  121. if(!Objects.isNull(tXinyiChatRecord)){
  122. tXinyiChatRecord.setIsSatisfied(isSatisfied);
  123. tXinyiChatRecord.setUpdateBy(SecurityUtils.getUsername());
  124. tXinyiChatRecord.setUpdateTime(DateUtils.getNowDate());
  125. this.xinyiChatRecordMapper.updateTXinyiChatRecord(tXinyiChatRecord);
  126. }
  127. return "操作成功";
  128. }
  129. @Override
  130. public String handleWarningByMan(TXinyiWarningRecord warningRecordReq) {
  131. Long id = warningRecordReq.getId();
  132. if(Objects.isNull(id))
  133. throw new ServiceException("请输入告警记录id");
  134. TXinyiWarningRecord tXinyiWarningRecord = this.xinyiWarningRecordMapper.selectTXinyiWarningRecordById(id);
  135. if(Objects.isNull(tXinyiWarningRecord))
  136. throw new ServiceException("请输入正确的告警记录id");
  137. warningRecordReq.setUpdateBy(SecurityUtils.getUsername());
  138. warningRecordReq.setUpdateTime(DateUtils.getNowDate());
  139. xinyiWarningRecordMapper.updateTXinyiWarningRecord(warningRecordReq);
  140. return "操作成功";
  141. }
  142. @Override
  143. public TXinyiChatRecord getLeastShortReport() {
  144. List<TXinyiChatRecord> tXinyiChatRecords = this.xinyiChatRecordMapper.selectTXinyiChatRecordList(TXinyiChatRecord.builder().module(4).build());
  145. return CollectionUtils.isEmpty(tXinyiChatRecords) ? null : tXinyiChatRecords.get(0);
  146. }
  147. @Override
  148. public List<TXinyiChatRecord> forecastList(String warningId) {
  149. return this.xinyiChatRecordMapper.selectTXinyiChatRecordList(TXinyiChatRecord.builder().warningId(warningId).type(3).build());
  150. }
  151. @Override
  152. public XinyiDailyFeeSimple recentlyOneFeeInfo() {
  153. //获取最新的一条日报
  154. TXinyiDaily tXinyiDaily = this.xinyiDailyMapper.selectNewestData();
  155. XinyiDailyFeeSimple xinyiDailyFeeSimple = new XinyiDailyFeeSimple();
  156. if(!Objects.isNull(tXinyiDaily))
  157. BeanUtils.copyProperties(tXinyiDaily, xinyiDailyFeeSimple);
  158. return xinyiDailyFeeSimple;
  159. }
  160. /**
  161. * 自定义工单接口 获取数据接口
  162. * @param workOrderReq
  163. * @return
  164. *
  165. * 2024年6月23日17:12:12 小数处理,所有的小数均保留两位小数处理
  166. */
  167. @Override
  168. public List<WorkOrderRes> customWorkOrder(WorkOrderReq workOrderReq) {
  169. checkCustomWorkOrder(workOrderReq);
  170. //两个日期之间所有的时间-天
  171. List<String> allDatesBetween = getAllDatesBetween(LocalDate.parse(workOrderReq.getTimeBegin()), LocalDate.parse(workOrderReq.getTimeEnd()));
  172. //返回结果
  173. List<WorkOrderRes> result = new ArrayList<>(allDatesBetween.size());
  174. for (String date : allDatesBetween) {
  175. //查询机器人数据
  176. WorkOrderRes workOrderRes = new WorkOrderRes();
  177. workOrderRes.setTime(date);
  178. TXinyiRobot xinyiRobot = this.xinyiRobotMapper.selectRebotAvgByDate(date);
  179. if(!Objects.isNull(xinyiRobot))
  180. this.buildRobotData(xinyiRobot, workOrderRes, workOrderReq);
  181. //判断数据来源(进出水情况)
  182. if(workOrderReq.getWhichWay() == 0){// 0:日报 1:工业库(在线仪表)
  183. List<TXinyiDaily> tXinyiDailies = this.xinyiDailyMapper.selectTXinyiDailyList(TXinyiDaily.builder().testDate(date).build());
  184. if(!CollectionUtils.isEmpty(tXinyiDailies)){
  185. TXinyiDaily tXinyiDaily = tXinyiDailies.get(0);
  186. this.buildDailyData(tXinyiDaily, workOrderRes, workOrderReq);
  187. }
  188. }else {
  189. TXinyiIndustry industry = this.xinyiIndustryMapper.selectIndustryAvgByDate(date);
  190. if(!Objects.isNull(industry))
  191. this.buildIndustryData(industry, workOrderRes, workOrderReq);
  192. }
  193. result.add(workOrderRes);
  194. }
  195. return result;
  196. }
  197. @Override
  198. public JSONObject customWorkOrderHandle(WorkOrderReq workOrderReq) {
  199. List<WorkOrderRes> workOrderRes = this.customWorkOrder(workOrderReq);
  200. JSONObject jsonObject = new JSONObject();
  201. List<JSONObject> jsGroup = new ArrayList<>();
  202. List<JSONObject> csGroup = new ArrayList<>();
  203. List<JSONObject> hyGroup = new ArrayList<>();
  204. //处理成前端响应的结构
  205. if(!CollectionUtils.isEmpty(workOrderRes)){
  206. for (WorkOrderRes workOrderRe : workOrderRes) {
  207. //处理进水
  208. if(workOrderReq.getJsSlq() || workOrderReq.getJsCod() || workOrderReq.getJsTn()
  209. || workOrderReq.getJsTp() || workOrderReq.getJsNh3() || workOrderReq.getJsSs()){
  210. JSONObject jsonObjectJS = new JSONObject();
  211. jsonObjectJS.put("time", workOrderRe.getTime());
  212. if(workOrderReq.getJsSlq())
  213. jsonObjectJS.put("jsSlq", workOrderRe.getJsSlq());
  214. if(workOrderReq.getJsCod())
  215. jsonObjectJS.put("jsCod", workOrderRe.getJsCod());
  216. if(workOrderReq.getJsTn())
  217. jsonObjectJS.put("jsTn", workOrderRe.getJsTn());
  218. if(workOrderReq.getJsTp())
  219. jsonObjectJS.put("jsTp", workOrderRe.getJsTp());
  220. if(workOrderReq.getJsNh3())
  221. jsonObjectJS.put("jsNh3", workOrderRe.getJsNh3());
  222. if(workOrderReq.getJsSs())
  223. jsonObjectJS.put("jsSs", workOrderRe.getJsSs());
  224. jsGroup.add(jsonObjectJS);
  225. }
  226. //处理出水
  227. if(workOrderReq.getCsSlqc() || workOrderReq.getCsCod() || workOrderReq.getCsTn()
  228. || workOrderReq.getCsTp() || workOrderReq.getCsNh3() || workOrderReq.getCsSs()){
  229. JSONObject jsonObjectCS = new JSONObject();
  230. jsonObjectCS.put("time", workOrderRe.getTime());
  231. if(workOrderReq.getCsSlqc())
  232. jsonObjectCS.put("csSlqc", workOrderRe.getCsSlqc());
  233. if(workOrderReq.getCsCod())
  234. jsonObjectCS.put("csCod", workOrderRe.getCsCod());
  235. if(workOrderReq.getCsTn())
  236. jsonObjectCS.put("csTn", workOrderRe.getCsTn());
  237. if(workOrderReq.getCsTp())
  238. jsonObjectCS.put("csTp", workOrderRe.getCsTp());
  239. if(workOrderReq.getCsNh3())
  240. jsonObjectCS.put("csNh3", workOrderRe.getCsNh3());
  241. if(workOrderReq.getCsSs())
  242. jsonObjectCS.put("csSs", workOrderRe.getCsSs());
  243. csGroup.add(jsonObjectCS);
  244. }
  245. //处理化验
  246. if(workOrderReq.getNo3Hlj1Jqr() || workOrderReq.getNo3Hlj2Jqr() || workOrderReq.getNh31Jqr()
  247. || workOrderReq.getNh32Jqr() || workOrderReq.getNo3Qyc1Jqr() || workOrderReq.getNo3Qyc2Jqr() || workOrderReq.getTpRccJqr()){
  248. JSONObject jsonObjectHY = new JSONObject();
  249. jsonObjectHY.put("time", workOrderRe.getTime());
  250. if(workOrderReq.getNo3Hlj1Jqr())
  251. jsonObjectHY.put("no3Hlj1Jqr", workOrderRe.getNo3Hlj1Jqr());
  252. if(workOrderReq.getNo3Hlj2Jqr())
  253. jsonObjectHY.put("no3Hlj2Jqr", workOrderRe.getNo3Hlj2Jqr());
  254. if(workOrderReq.getNh31Jqr())
  255. jsonObjectHY.put("nh31Jqr", workOrderRe.getNh31Jqr());
  256. if(workOrderReq.getNh32Jqr())
  257. jsonObjectHY.put("nh32Jqr", workOrderRe.getNh32Jqr());
  258. if(workOrderReq.getNo3Qyc1Jqr())
  259. jsonObjectHY.put("no3Qyc1Jqr", workOrderRe.getNo3Qyc1Jqr());
  260. if(workOrderReq.getNo3Qyc2Jqr())
  261. jsonObjectHY.put("no3Qyc2Jqr", workOrderRe.getNo3Qyc2Jqr());
  262. if(workOrderReq.getTpRccJqr())
  263. jsonObjectHY.put("tpRccJqr", workOrderRe.getTpRccJqr());
  264. hyGroup.add(jsonObjectHY);
  265. }
  266. }
  267. }
  268. jsonObject.put("jsGroup", jsGroup);
  269. jsonObject.put("csGroup", csGroup);
  270. jsonObject.put("hyGroup", hyGroup);
  271. return jsonObject;
  272. }
  273. @Override
  274. public JSONObject customWorkOrderHandleByData(WorkOrderReq workOrderReq, List<WorkOrderRes> workOrderRes) {
  275. JSONObject jsonObject = new JSONObject();
  276. List<JSONObject> jsGroup = new ArrayList<>();
  277. List<JSONObject> csGroup = new ArrayList<>();
  278. List<JSONObject> hyGroup = new ArrayList<>();
  279. //处理成前端响应的结构
  280. if(!CollectionUtils.isEmpty(workOrderRes)){
  281. for (WorkOrderRes workOrderRe : workOrderRes) {
  282. //处理进水
  283. if(workOrderReq.getJsSlq() || workOrderReq.getJsCod() || workOrderReq.getJsTn()
  284. || workOrderReq.getJsTp() || workOrderReq.getJsNh3() || workOrderReq.getJsSs()){
  285. JSONObject jsonObjectJS = new JSONObject();
  286. jsonObjectJS.put("time", workOrderRe.getTime());
  287. if(workOrderReq.getJsSlq())
  288. jsonObjectJS.put("jsSlq", workOrderRe.getJsSlq());
  289. if(workOrderReq.getJsCod())
  290. jsonObjectJS.put("jsCod", workOrderRe.getJsCod());
  291. if(workOrderReq.getJsTn())
  292. jsonObjectJS.put("jsTn", workOrderRe.getJsTn());
  293. if(workOrderReq.getJsTp())
  294. jsonObjectJS.put("jsTp", workOrderRe.getJsTp());
  295. if(workOrderReq.getJsNh3())
  296. jsonObjectJS.put("jsNh3", workOrderRe.getJsNh3());
  297. if(workOrderReq.getJsSs())
  298. jsonObjectJS.put("jsSs", workOrderRe.getJsSs());
  299. jsGroup.add(jsonObjectJS);
  300. }
  301. //处理出水
  302. if(workOrderReq.getCsSlqc() || workOrderReq.getCsCod() || workOrderReq.getCsTn()
  303. || workOrderReq.getCsTp() || workOrderReq.getCsNh3() || workOrderReq.getCsSs()){
  304. JSONObject jsonObjectCS = new JSONObject();
  305. jsonObjectCS.put("time", workOrderRe.getTime());
  306. if(workOrderReq.getCsSlqc())
  307. jsonObjectCS.put("csSlqc", workOrderRe.getCsSlqc());
  308. if(workOrderReq.getCsCod())
  309. jsonObjectCS.put("csCod", workOrderRe.getCsCod());
  310. if(workOrderReq.getCsTn())
  311. jsonObjectCS.put("csTn", workOrderRe.getCsTn());
  312. if(workOrderReq.getCsTp())
  313. jsonObjectCS.put("csTp", workOrderRe.getCsTp());
  314. if(workOrderReq.getCsNh3())
  315. jsonObjectCS.put("csNh3", workOrderRe.getCsNh3());
  316. if(workOrderReq.getCsSs())
  317. jsonObjectCS.put("csSs", workOrderRe.getCsSs());
  318. csGroup.add(jsonObjectCS);
  319. }
  320. //处理化验
  321. if(workOrderReq.getNo3Hlj1Jqr() || workOrderReq.getNo3Hlj2Jqr() || workOrderReq.getNh31Jqr()
  322. || workOrderReq.getNh32Jqr() || workOrderReq.getNo3Qyc1Jqr() || workOrderReq.getNo3Qyc2Jqr() || workOrderReq.getTpRccJqr()){
  323. JSONObject jsonObjectHY = new JSONObject();
  324. jsonObjectHY.put("time", workOrderRe.getTime());
  325. if(workOrderReq.getNo3Hlj1Jqr())
  326. jsonObjectHY.put("no3Hlj1Jqr", workOrderRe.getNo3Hlj1Jqr());
  327. if(workOrderReq.getNo3Hlj2Jqr())
  328. jsonObjectHY.put("no3Hlj2Jqr", workOrderRe.getNo3Hlj2Jqr());
  329. if(workOrderReq.getNh31Jqr())
  330. jsonObjectHY.put("nh31Jqr", workOrderRe.getNh31Jqr());
  331. if(workOrderReq.getNh32Jqr())
  332. jsonObjectHY.put("nh32Jqr", workOrderRe.getNh32Jqr());
  333. if(workOrderReq.getNo3Qyc1Jqr())
  334. jsonObjectHY.put("no3Qyc1Jqr", workOrderRe.getNo3Qyc1Jqr());
  335. if(workOrderReq.getNo3Qyc2Jqr())
  336. jsonObjectHY.put("no3Qyc2Jqr", workOrderRe.getNo3Qyc2Jqr());
  337. if(workOrderReq.getTpRccJqr())
  338. jsonObjectHY.put("tpRccJqr", workOrderRe.getTpRccJqr());
  339. hyGroup.add(jsonObjectHY);
  340. }
  341. }
  342. }
  343. jsonObject.put("jsGroup", jsGroup);
  344. jsonObject.put("csGroup", csGroup);
  345. jsonObject.put("hyGroup", hyGroup);
  346. //2024年6月25日13:45:48 新增逻辑 区分是在线仪表的还是日报的,方便前端展示单位
  347. jsonObject.put("whichWay", workOrderReq.getWhichWay());
  348. return jsonObject;
  349. }
  350. @Override
  351. public String stopChat(String sessionId) {
  352. HashMap<String, Object> map = new HashMap<>();
  353. map.put(SESSION_ID, sessionId);
  354. RecordId stopGenStream = redisCache.streamXadd(STOP_GEN_STREAM, map);
  355. log.info("返回结果是{}", JSON.toJSONString(stopGenStream));
  356. return "操作成功";
  357. }
  358. private void buildIndustryData(TXinyiIndustry industry, WorkOrderRes workOrderRes, WorkOrderReq workOrderReq) {
  359. if(workOrderReq.getJsSlq())
  360. workOrderRes.setJsSlq(DecimalUtils.getAbsAndScale(industry.getJsSlq(), 2));
  361. if(workOrderReq.getJsCod())
  362. workOrderRes.setJsCod(DecimalUtils.getAbsAndScale(industry.getJsCod(), 2));
  363. if(workOrderReq.getJsTn())
  364. workOrderRes.setJsTn(DecimalUtils.getAbsAndScale(industry.getJsTn(), 2));
  365. if(workOrderReq.getJsTp())
  366. workOrderRes.setJsTp(DecimalUtils.getAbsAndScale(industry.getJsTp(), 2));
  367. if(workOrderReq.getJsNh3())
  368. workOrderRes.setJsNh3(DecimalUtils.getAbsAndScale(industry.getJsNh3(), 2));
  369. if(workOrderReq.getJsSs())
  370. workOrderRes.setJsSs(DecimalUtils.getAbsAndScale(industry.getJsSs(), 2));
  371. if(workOrderReq.getCsSlqc())
  372. workOrderRes.setCsSlqc(DecimalUtils.getAbsAndScale(industry.getCsSlqc(), 2));
  373. if(workOrderReq.getCsCod())
  374. workOrderRes.setCsCod(DecimalUtils.getAbsAndScale(industry.getCsCod(), 2));
  375. if(workOrderReq.getCsTn())
  376. workOrderRes.setCsTn(DecimalUtils.getAbsAndScale(industry.getCsTn(), 2));
  377. if(workOrderReq.getCsTp())
  378. workOrderRes.setCsTp(DecimalUtils.getAbsAndScale(industry.getCsTp(), 2));
  379. if(workOrderReq.getCsNh3())
  380. workOrderRes.setCsNh3(DecimalUtils.getAbsAndScale(industry.getCsNh3(), 2));
  381. if(workOrderReq.getCsSs())
  382. workOrderRes.setCsSs(DecimalUtils.getAbsAndScale(industry.getCsSs(), 2));
  383. }
  384. private void buildDailyData(TXinyiDaily tXinyiDaily, WorkOrderRes workOrderRes, WorkOrderReq workOrderReq) {
  385. if(workOrderReq.getJsSlq())
  386. workOrderRes.setJsSlq(DecimalUtils.getAbsAndScale(tXinyiDaily.getJSL(), 2));
  387. if(workOrderReq.getJsCod())
  388. workOrderRes.setJsCod(DecimalUtils.getAbsAndScale(tXinyiDaily.getJsCod(), 2));
  389. if(workOrderReq.getJsTn())
  390. workOrderRes.setJsTn(DecimalUtils.getAbsAndScale(tXinyiDaily.getJsTn(), 2));
  391. if(workOrderReq.getJsTp())
  392. workOrderRes.setJsTp(DecimalUtils.getAbsAndScale(tXinyiDaily.getJsTp(), 2));
  393. if(workOrderReq.getJsNh3())
  394. workOrderRes.setJsNh3(DecimalUtils.getAbsAndScale(tXinyiDaily.getJsNh3(), 2));
  395. if(workOrderReq.getJsSs())
  396. workOrderRes.setJsSs(DecimalUtils.getAbsAndScale(tXinyiDaily.getJsSs(), 2));
  397. if(workOrderReq.getCsSlqc())
  398. workOrderRes.setCsSlqc(DecimalUtils.getAbsAndScale(tXinyiDaily.getCSL(), 2));
  399. if(workOrderReq.getCsCod())
  400. workOrderRes.setCsCod(DecimalUtils.getAbsAndScale(tXinyiDaily.getCsCod(), 2));
  401. if(workOrderReq.getCsTn())
  402. workOrderRes.setCsTn(DecimalUtils.getAbsAndScale(tXinyiDaily.getCsTn(), 2));
  403. if(workOrderReq.getCsTp())
  404. workOrderRes.setCsTp(DecimalUtils.getAbsAndScale(tXinyiDaily.getCsTp(), 2));
  405. if(workOrderReq.getCsNh3())
  406. workOrderRes.setCsNh3(DecimalUtils.getAbsAndScale(tXinyiDaily.getCsNh3(), 2));
  407. if(workOrderReq.getCsSs())
  408. workOrderRes.setCsSs(DecimalUtils.getAbsAndScale(tXinyiDaily.getCsSs(), 2));
  409. }
  410. private void buildRobotData(TXinyiRobot xinyiRobot, WorkOrderRes workOrderRes, WorkOrderReq workOrderReq) {
  411. if(workOrderReq.getNo3Hlj1Jqr())
  412. workOrderRes.setNo3Hlj1Jqr(DecimalUtils.getAbsAndScale(xinyiRobot.getNo3Hlj1Jqr(), 2));
  413. if(workOrderReq.getNo3Hlj2Jqr())
  414. workOrderRes.setNo3Hlj2Jqr(DecimalUtils.getAbsAndScale(xinyiRobot.getNo3Hlj2Jqr(), 2));
  415. if(workOrderReq.getNh31Jqr())
  416. workOrderRes.setNh31Jqr(DecimalUtils.getAbsAndScale(xinyiRobot.getNh31Jqr(), 2));
  417. if(workOrderReq.getNh32Jqr())
  418. workOrderRes.setNh32Jqr(DecimalUtils.getAbsAndScale(xinyiRobot.getNh32Jqr(), 2));
  419. if(workOrderReq.getNo3Qyc1Jqr())
  420. workOrderRes.setNo3Qyc1Jqr(DecimalUtils.getAbsAndScale(xinyiRobot.getNo3Qyc1Jqr(), 2));
  421. if(workOrderReq.getNo3Qyc2Jqr())
  422. workOrderRes.setNo3Qyc2Jqr(DecimalUtils.getAbsAndScale(xinyiRobot.getNo3Qyc2Jqr(), 2));
  423. if(workOrderReq.getTpRccJqr())
  424. workOrderRes.setTpRccJqr(DecimalUtils.getAbsAndScale(xinyiRobot.getTpRccJqr(), 2));
  425. }
  426. private void checkCustomWorkOrder(WorkOrderReq workOrderReq) {
  427. if(StringUtils.isBlank(workOrderReq.getTimeBegin()) || StringUtils.isBlank(workOrderReq.getTimeEnd()))
  428. throw new ServiceException("输入的参数不合法");
  429. }
  430. /***
  431. * 将工业库数据计算出结果
  432. * @param xinyiIndustrySimple
  433. * @param nowTime
  434. * @return
  435. */
  436. private XinyiIndustrySimple calculateData(XinyiIndustrySimple xinyiIndustrySimple, LocalDateTime nowTime) {
  437. LocalDateTime yesterdaySameTime = nowTime.plusDays(-1);
  438. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.YYYYMMDDHH_TS);
  439. String testTime = formatter.format(yesterdaySameTime);
  440. //处理昨日同时期的进水 获取环比
  441. List<TXinyiIndustry> tXinyiIndustries = this.xinyiIndustryMapper.selectTXinyiIndustryList(TXinyiIndustry.builder().testTime(testTime).build());
  442. if(!CollectionUtils.isEmpty(tXinyiIndustries)){
  443. TXinyiIndustry industry = tXinyiIndustries.get(0);
  444. BigDecimal jsSlqYesterday = industry.getJsSlq();
  445. BigDecimal csSlqcYesterday = industry.getCsSlqc();
  446. BigDecimal jsSlqToday = xinyiIndustrySimple.getJsSlq();
  447. BigDecimal csSlqcToday = xinyiIndustrySimple.getCsSlqc();
  448. if(!Objects.isNull(jsSlqYesterday) && !Objects.isNull(jsSlqToday))
  449. xinyiIndustrySimple.setSsJsHb((jsSlqToday.subtract(jsSlqYesterday)).divide(jsSlqYesterday, 4, RoundingMode.HALF_UP));
  450. if(!Objects.isNull(csSlqcYesterday) && !Objects.isNull(csSlqcToday))
  451. xinyiIndustrySimple.setSsJsHb((csSlqcToday.subtract(csSlqcYesterday)).divide(csSlqcYesterday, 4, RoundingMode.HALF_UP));
  452. }
  453. //通过sql查询上周的平均值和环比情况
  454. DateTimeFormatter formatterDate = DateTimeFormatter.ofPattern(DateUtils.YYYYMMDD_TS);
  455. //获取上周的统计进出水品均值
  456. String begin = formatterDate.format(nowTime.plusDays(-7));
  457. String end = formatterDate.format(nowTime.plusDays(-1));
  458. HashMap<String, BigDecimal> lastWeek = this.xinyiDailyMapper.selectAvgWater(begin, end);
  459. if(!Objects.isNull(lastWeek)){
  460. BigDecimal lastJSL = lastWeek.get("JSL");
  461. BigDecimal lastCSL = lastWeek.get("CSL");
  462. //获取上上周的统计进出水品均值
  463. begin = formatterDate.format(nowTime.plusDays(-14));
  464. end = formatterDate.format(nowTime.plusDays(-8));
  465. HashMap<String, BigDecimal> beforeLastWeek = this.xinyiDailyMapper.selectAvgWater(begin, end);
  466. if(!Objects.isNull(beforeLastWeek)){
  467. BigDecimal beforeLastJSL = beforeLastWeek.get("JSL");
  468. BigDecimal beforeLastCSL = beforeLastWeek.get("CSL");
  469. if(!Objects.isNull(lastJSL)){
  470. xinyiIndustrySimple.setSzZJSAvg(lastJSL);
  471. if(!Objects.isNull(beforeLastJSL))
  472. xinyiIndustrySimple.setSzZJSHb((lastJSL.subtract(beforeLastJSL)).divide(beforeLastJSL, 4, RoundingMode.HALF_UP));
  473. }
  474. if(!Objects.isNull(lastCSL)){
  475. xinyiIndustrySimple.setSzZCSAvg(lastCSL);
  476. if(!Objects.isNull(beforeLastCSL))
  477. xinyiIndustrySimple.setSzZCSHb((lastCSL.subtract(beforeLastCSL)).divide(beforeLastCSL, 4, RoundingMode.HALF_UP));
  478. }
  479. }
  480. }
  481. return xinyiIndustrySimple;
  482. }
  483. private XinyiIndustrySimple addConfigData(XinyiIndustrySimple xinyiIndustrySimple) {
  484. //获取配置表
  485. List<TXinyiNormConfig> tXinyiNormConfigs = this.tXinyiNormConfigMapper.selectTXinyiNormConfigList(null);
  486. if(!CollectionUtils.isEmpty(tXinyiNormConfigs)){
  487. TXinyiNormConfig normConfig = tXinyiNormConfigs.get(0);
  488. BeanUtils.copyProperties(normConfig, xinyiIndustrySimple);
  489. }
  490. return xinyiIndustrySimple;
  491. }
  492. private XinyiIndustrySimple addDailyData(XinyiIndustrySimple xinyiIndustrySimple, LocalDateTime nowDate) {
  493. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.YYYYMMDD_TS);
  494. String nowDateStr = nowDate.format(formatter);
  495. String yesterdayStr = nowDate.plusDays(-1).format(formatter);//昨天
  496. String beforeYesterdayStr = nowDate.plusDays(-2).format(formatter);//前天
  497. List<TXinyiDaily> tXinyiDailies = this.xinyiDailyMapper.selectTXinyiDailyList(TXinyiDaily.builder().testDate(nowDateStr).build());
  498. if(!CollectionUtils.isEmpty(tXinyiDailies)){
  499. TXinyiDaily tXinyiDaily = tXinyiDailies.get(0);
  500. BeanUtils.copyProperties(tXinyiDaily, xinyiIndustrySimple);
  501. }
  502. //昨日数据
  503. List<TXinyiDaily> tXinyiDailiesYesterday = this.xinyiDailyMapper.selectTXinyiDailyList(TXinyiDaily.builder().testDate(yesterdayStr).build());
  504. if(!CollectionUtils.isEmpty(tXinyiDailiesYesterday)){
  505. TXinyiDaily tXinyiDailyYesterday = tXinyiDailiesYesterday.get(0);
  506. BigDecimal zrJsl = tXinyiDailyYesterday.getJSL();
  507. xinyiIndustrySimple.setZrZJSL(zrJsl);
  508. BigDecimal zrCsl = tXinyiDailyYesterday.getCSL();
  509. xinyiIndustrySimple.setZrZCSL(zrCsl);
  510. //有可能今日获取不到日报数据,相关信息从昨天获取
  511. if(CollectionUtils.isEmpty(tXinyiDailies))
  512. BeanUtils.copyProperties(tXinyiDailyYesterday, xinyiIndustrySimple);
  513. //获取前日数据 并计算环比
  514. List<TXinyiDaily> tXinyiDailiesBeforeYesterday = this.xinyiDailyMapper.selectTXinyiDailyList(TXinyiDaily.builder().testDate(beforeYesterdayStr).build());
  515. if(!CollectionUtils.isEmpty(tXinyiDailiesBeforeYesterday)){
  516. TXinyiDaily tXinyiDailyBeforeYesterday = tXinyiDailiesBeforeYesterday.get(0);
  517. BigDecimal qrZJSL = tXinyiDailyBeforeYesterday.getJSL();
  518. if(!Objects.isNull(qrZJSL) && !Objects.isNull(zrJsl))
  519. xinyiIndustrySimple.setZrZJsHb((zrJsl.subtract(qrZJSL)).divide(qrZJSL, 4, RoundingMode.HALF_UP));
  520. BigDecimal qrZCSL = tXinyiDailyBeforeYesterday.getCSL();
  521. if(!Objects.isNull(qrZCSL) && !Objects.isNull(zrCsl))
  522. xinyiIndustrySimple.setZrZCsHb((zrCsl.subtract(qrZCSL)).divide(qrZCSL, 4, RoundingMode.HALF_UP));
  523. }
  524. }
  525. return xinyiIndustrySimple;
  526. }
  527. private XinyiIndustrySimple getRealTimeData(String[] queryTags, LocalDateTime nowTime) {
  528. /*AtomicReference<XinyiIndustrySimple> result = new AtomicReference<>();
  529. *//*String time = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH, DateUtils.getNowDate());
  530. time = time + ":00:00";*//*
  531. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.YYYY_MM_DD_HH_MM_SS);
  532. //获取当前的时间
  533. LocalDateTime beginTime = nowTime.plusMinutes(-1);
  534. //调用工业库接口
  535. String url = "http://10.0.0.27:4568/api/v1/khquerydata";
  536. HashMap<String, Object> req = new HashMap<>();
  537. req.put("tagNames", queryTags);
  538. req.put("startTime", formatter.format(beginTime));
  539. req.put("endTime", formatter.format(nowTime));
  540. req.put("recordNumbers", 100000);
  541. String body = HttpRequest.post(url).header("Authorization", "c2E6c2E=").header("clientName", "hongshan").body(JSON.toJSONString(req)).execute().body();
  542. // System.out.println("body = " + body);
  543. log.info("实时数据返回的结果是{}", body);
  544. //2024年5月31日13:43:24 新增解析内容,判断是否正常返回
  545. String errorCode = (String) JSON.parseObject(body).get("errorCode");
  546. if("-1".equals(errorCode)){
  547. log.info("工业库数据返回错误");
  548. return new XinyiIndustrySimple();
  549. }
  550. List<HashMap<String, String>> list = new ArrayList<>();
  551. //行转列数据处理
  552. for (String queryTag : queryTags) {
  553. JSONArray array = JSON.parseObject(body).getJSONArray(queryTag);
  554. //特殊数据处理一
  555. if(Objects.isNull(array) || array.isEmpty()){
  556. System.out.println(queryTag + "查询到了空的数据,跳过本次循环");
  557. continue;
  558. }
  559. int size = array.size();
  560. //特殊数据处理二
  561. if("0".equals(array.get(1) + "")){
  562. System.out.println(queryTag + "查询到了数据,但是数据集合只有一条,且都是0");
  563. continue;
  564. }
  565. //存放的数据集
  566. //利用map去重
  567. HashMap<String, String> map = new LinkedHashMap<>();
  568. for (int i = 2; i < size; i++) {
  569. // System.out.println(i + "" + array.get(i));
  570. JSONArray oneRecord = JSON.parseArray(JSON.toJSONString(array.get(i)));
  571. //处理为空或者为0的数据
  572. Object timeStampValue = oneRecord.get(2);
  573. if(Objects.isNull(timeStampValue) || "0".equals(timeStampValue + ""))
  574. continue;
  575. BigDecimal value = Objects.isNull(oneRecord.get(0)) ? null : new BigDecimal(oneRecord.get(0) + "");
  576. long timestamp = (long) timeStampValue;
  577. String format = DateUtil.format(new Date(timestamp), DateUtils.YYYYMMDDHH_TS);
  578. map.put(format, queryTag + "-" + value);
  579. }
  580. list.add(map);
  581. }
  582. Set<String> recordTimeSet = new HashSet<>();
  583. Map<String, XinyiIndustrySimple> recordMap = new HashMap<>();
  584. for (int i = 0; i < list.size(); i++) {
  585. HashMap<String, String> map = list.get(i);
  586. int finalJ = i;
  587. map.forEach((k, v) ->{
  588. XinyiIndustrySimple xinyiIndustrySimple = null;
  589. if(!recordTimeSet.contains(k)){//第一次
  590. xinyiIndustrySimple = new XinyiIndustrySimple();
  591. recordTimeSet.add(k);
  592. recordMap.put(k, xinyiIndustrySimple);
  593. }else{
  594. xinyiIndustrySimple = recordMap.get(k);
  595. }
  596. xinyiIndustrySimple.setTestTime(k);
  597. //解析值
  598. String[] split = v.split("-");
  599. String type = split[0];
  600. BigDecimal value = new BigDecimal(split[1]);
  601. if ("信义污水厂JS_COD_Value".equals(type)) {
  602. xinyiIndustrySimple.setJsCod(value);
  603. } else if ("信义污水厂JS_PH_Value".equals(type)) {
  604. xinyiIndustrySimple.setJsPh(value);
  605. } else if ("信义污水厂JS_SS_Value".equals(type)) {
  606. xinyiIndustrySimple.setJsSs(value);
  607. } else if ("信义污水厂JS_ZL_Value".equals(type)) {
  608. xinyiIndustrySimple.setJsTp(value);
  609. } else if ("信义污水厂JS_ZA_Value".equals(type)) {
  610. xinyiIndustrySimple.setJsTn(value);
  611. } else if ("信义污水厂JS_AD_Value".equals(type)) {
  612. xinyiIndustrySimple.setJsNh3(value);
  613. } else if ("信义污水厂出水瞬时流量".equals(type)) {
  614. xinyiIndustrySimple.setCsSlqc(value);
  615. } else if ("信义污水厂升级出水COD".equals(type)) {
  616. xinyiIndustrySimple.setCsCod(value);
  617. } else if ("信义污水厂升级出水PH".equals(type)) {
  618. xinyiIndustrySimple.setCsPh(value);
  619. } else if ("信义污水厂升级出水SS".equals(type)) {
  620. xinyiIndustrySimple.setCsSs(value);
  621. } else if ("信义污水厂升级出水TN".equals(type)) {
  622. xinyiIndustrySimple.setCsTn(value);
  623. } else if ("信义污水厂升级出水TP".equals(type)) {
  624. xinyiIndustrySimple.setCsTp(value);
  625. } else if ("信义污水厂升级出水氨氮".equals(type)) {
  626. xinyiIndustrySimple.setCsNh3(value);
  627. } else if ("信义污水厂FT101_Value".equals(type)) {
  628. xinyiIndustrySimple.setJsSlq(value);
  629. }
  630. //只有最后一次才执行数据库添加
  631. if(finalJ == list.size()-1){
  632. //赋值完毕返回数据
  633. result.set(xinyiIndustrySimple);
  634. }
  635. });
  636. }
  637. XinyiIndustrySimple xinyiIndustrySimple = result.get();
  638. return Objects.isNull(xinyiIndustrySimple) ? new XinyiIndustrySimple() : xinyiIndustrySimple;*/
  639. //2024年6月11日16:16:17 逻辑调整,获取最新的那条工业库的数据
  640. TXinyiIndustry tXinyiIndustry = this.xinyiIndustryMapper.selectTXinyiIndustryNewest();
  641. XinyiIndustrySimple xinyiIndustrySimple = new XinyiIndustrySimple();
  642. if(!Objects.isNull(tXinyiIndustry))
  643. BeanUtils.copyProperties(tXinyiIndustry, xinyiIndustrySimple);
  644. //获取化验室的最新的一条数据
  645. TXinyiRobot tXinyiRobot = this.xinyiRobotMapper.selectNewest();
  646. if(!Objects.isNull(tXinyiRobot))
  647. BeanUtils.copyProperties(tXinyiRobot, xinyiIndustrySimple);
  648. return xinyiIndustrySimple;
  649. }
  650. public static void main(String[] args) {
  651. String s = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH, DateUtils.getNowDate());
  652. s = s + ":00:00";
  653. System.out.println("s = " + s);
  654. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  655. LocalDateTime endTime = LocalDateTime.now();
  656. LocalDateTime begin = endTime.plusMinutes(-1);
  657. System.out.println(formatter.format(endTime));
  658. System.out.println(formatter.format(begin));
  659. LocalDate parse = LocalDate.parse("2024-02-26");
  660. System.out.println("parse = " + parse);
  661. }
  662. }