1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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.totalCountOfOnLine = result[0] ;
        vo.totalCountOfOffLine = totalCountOfIntake - vo.totalCountOfOnLine; ;
        vo.totalCountOfOpenValve = this.dao.totalCountOfOpenValve(dtAtXHourBefore) ;
        vo.totalCountOfCloseValve = this.dao.totalCountOfCloseValve(dtAtXHourBefore) ;
        vo.totalCountOfUnknownValve = totalCountOfIntake - vo.totalCountOfOpenValve - vo.totalCountOfCloseValve ;
        vo.totalCountOfAlarm = this.dao.totalCountOfAlarm(dtAtXHourBefore) ;
        vo.totalCountOfNoAlarm = this.dao.totalCountOfNoAlarm(dtAtXHourBefore) ;
        vo.totalCountOfUnknownAlarm = totalCountOfIntake - vo.totalCountOfAlarm - vo.totalCountOfNoAlarm ;
        return vo ;
    }
 
    /**
     * 监测信息统计
     * @return
     */
    public VoMonitorInfo monitorInfo(Date startDt){
        VoMonitorInfo vo = new VoMonitorInfo() ;
        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 ;
    }
}