package com.dy.pipIrrStatistics.largeScreen; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.dy.common.mw.protocol.Command; import com.dy.common.mw.protocol.CommandBackParam; import com.dy.common.util.DateTime; import com.dy.common.webUtil.BaseResponse; import com.dy.pipIrrGlobal.daoLargeScreen.Ls4StatisticsMapper; import com.dy.pipIrrGlobal.rtuMw.CodeLocal; import com.dy.pipIrrGlobal.rtuMw.Web2RtuMw; import com.dy.pipIrrGlobal.voLargeScreen.VoBaseInfo; import com.dy.pipIrrGlobal.voLargeScreen.VoCurrentInfo; import com.dy.pipIrrGlobal.voLargeScreen.VoMonitorInfo; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import java.util.Date; /** * @Author: liurunyu * @Date: 2025/2/6 10:33 * @Description */ @Slf4j @Service public class Ls4StatisticsSv extends Web2RtuMw { private Environment env; private RestTemplate restTemplate; private Ls4StatisticsMapper dao ; @Autowired private void setEnvironment(Environment env){ this.env = env; } @Autowired private void setRestTemplate(RestTemplate restTemplate){ this.restTemplate = restTemplate; } @Autowired private void setDao(Ls4StatisticsMapper dao){ this.dao = dao; } /** * 基本信息统计 * @return */ public VoBaseInfo baseInfo(){ VoBaseInfo vo = new VoBaseInfo() ; vo.totalCountOfTown = this.dao.totalCountOfDistrict(3) ; vo.totalCountOfVillage = this.dao.totalCountOfDistrict(4) ; vo.totalCountOfBlock = this.dao.totalCountOfBlock() ; vo.totalCountOfDivide = this.dao.totalCountOfDivide() ; vo.totalCountOfIntake = this.dao.totalCountOfIntake() ; vo.totalCountOfIntakeWithController = this.dao.totalCountOfIntakeWithController() ; vo.totalCountOfIntakeWithoutController = this.dao.totalCountOfIntakeWithoutController() ; vo.totalCountOfController = this.dao.totalCountOfController() ; vo.totalCountOfControllerTramp = this.dao.totalCountOfControllerTramp() ; return vo ; } /** * 当前信息统计 * @return */ public VoCurrentInfo currentInfo() throws Exception{ Integer totalCountOfIntake = this.dao.totalCountOfIntake() ; Date dtAtXHourBefore = DateTime.dateFrom_yyyy_MM_dd_HH_mm_ss(DateTime.lastXHour_yyyy_MM_dd_HH_mm_ss(24)) ;//X个小时前 VoCurrentInfo vo = new VoCurrentInfo() ; Integer[] result = this.queryMwOnAndOffLine() ; vo.cTotalCountOfOnLine = result[0] ; vo.cTotalCountOfOffLine = totalCountOfIntake - vo.cTotalCountOfOnLine; vo.cTotalCountOfOpenValve = this.dao.totalCountOfOpenValve(dtAtXHourBefore) ; vo.cTotalCountOfCloseValve = this.dao.totalCountOfCloseValve(dtAtXHourBefore) ; vo.cTotalCountOfUnknownValve = totalCountOfIntake - vo.cTotalCountOfOpenValve - vo.cTotalCountOfCloseValve; vo.cTotalCountOfAlarm = this.dao.totalCountOfAlarm(dtAtXHourBefore) ; vo.cTotalCountOfNoAlarm = this.dao.totalCountOfNoAlarm(dtAtXHourBefore) ; vo.cTotalCountOfUnknownAlarm = totalCountOfIntake - vo.cTotalCountOfAlarm - vo.cTotalCountOfNoAlarm; return vo ; } /** * 监测信息统计 * @return */ public VoMonitorInfo monitorInfo(Date fromDt){ VoMonitorInfo vo = new VoMonitorInfo() ; vo.mTotalCountOfReport = this.dao.mTotalCountOfReport(fromDt) ; vo.mTotalCountOfNoReport = this.dao.mTotalCountOfNoReport(fromDt) ; vo.mTotalCountOfNeverReport = this.dao.mTotalCountOfNeverReport() ; vo.mTotalCountOfOpenValve = this.dao.mTotalCountOfOpenValve(fromDt) ; vo.mTotalCountOfCloseValve = this.dao.mTotalCountOfCloseValve(fromDt) ; vo.mTotalCountOfNeverOpenValve = this.dao.mTotalCountOfNeverOpenValve() ; vo.mTotalCountOfAlarm = this.dao.mTotalCountOfAlarm(fromDt) ; vo.mTotalCountOfNoAlarm = this.dao.mTotalCountOfNoAlarm(fromDt) ; vo.mTotalCountOfNeverAlarm = this.dao.mTotalCountOfNeverAlarm() ; return vo ; } /** * 查询取水口在线和离线统计 * @return */ private Integer[] queryMwOnAndOffLine() { Integer[] result = new Integer[]{0,0} ; //向通信中间件发关命令,查询RTU在线统计情况 Command com = this.createInnerCommand(CodeLocal.onLineStatistics); String rqUrl = this.get2MwRequestUrl(this.env, ContextComSend) ; BaseResponse res = this.sendPostRequest2Mw(restTemplate, rqUrl, com) ; if(res != null){ if(res.isSuccess()){ Command reCom = JSON.parseObject(res.getContent() == null ? null : JSON.toJSONString(res.getContent()), Command.class) ; CommandBackParam bakParam = JSON.parseObject((reCom== null || reCom.param == null) ? null : JSON.toJSONString(reCom.param), CommandBackParam.class) ; if(bakParam != null){ if(bakParam.getSuccess().booleanValue() && reCom.getAttachment() != null){ //通信中间件成功返回命令结果 JSONObject comRes = (JSONObject) JSON.toJSON(reCom.getAttachment()); result[0] = comRes.getInteger("onLineNum") ; result[1] = comRes.getInteger("offLineNum") ; } }else{ log.error("通信中间件返回内部命令结果中不包含CommandBackParam类型参数"); } }else{ log.error("通信中间件返回内部命令执行失败" + (res.getMsg() == null? "" : ("," + res.getMsg()))) ; } }else{ log.error("通信中间件返回内部命令结果为null"); } return result ; } }