whyclxw
2025-05-15 96510a549bfb313920bf297b28089c4cf57f0146
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package com.whyc.service;
 
import com.whyc.dto.LockAlmHisDto;
import com.whyc.dto.LockHisDto;
import com.whyc.mapper.CallBack;
import com.whyc.pojo.plus_lock_alarm.LockAlarmHis;
import com.whyc.pojo.plus_lock_his.LockHis;
import com.whyc.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
 
@Service
public class SubTableService {
    @Autowired
    private MybatisSqlExecuteService sqlExecuteService;
    //锁的历史记录总数
    public int getLockHisCount(LockHisDto dto) {
        String sql="SELECT  count(*) as number FROM plus_lock_his."+ dto.getRecordYear()+" history " +
                " where history.lock_id="+ dto.getLockId() ;
        if(dto.getStationName1()!=null){
            sql+=" and  tb_station_inf.station_name1  like '%"+dto.getStationName1()+"%' ";
        }
        if(dto.getStationName2()!=null){
            sql+=" and  tb_station_inf.station_name2  like '%"+dto.getStationName2()+"%' ";
        }
        if(dto.getStationName3()!=null){
            sql+=" and  tb_station_inf.station_name3  like '%"+dto.getStationName3()+"%' ";
        }
        if(dto.getStationName4()!=null){
            sql+=" and  tb_station_inf.station_name4  like '%"+dto.getStationName4()+"%' ";
        }
        if(dto.getLockName()!=null){
            sql+=" and  tb_lock_inf.lock_name  like '%"+dto.getLockName()+"%' ";
        }
        if(dto.getStartTime()!=null){
            sql+=" and record_time  >='"+ ThreadLocalUtil.format(dto.getStartTime(),1)+"' ";
        }
        if(dto.getEndTime()!=null){
            sql+=" and record_time  <='"+ThreadLocalUtil.format(dto.getEndTime(),1)+"' ";
        }
        if(dto.getUid()>100){
            sql+="   and tb_lock_inf.lock_id in(" +
                    "select distinct lock_id from plus_user.tb_baojigroup_lock,plus_user.tb_baojigroup_usr " +
                    " where tb_baojigroup_lock.baoji_id=tb_baojigroup_usr.baoji_id " +
                    " and tb_baojigroup_usr.uid= " +dto.getUid()+
                    " )";
        }else{
            sql+="   and tb_lock_inf.lock_id in(" +
                    "select distinct lock_id from plus_user.tb_baojigroup_lock,plus_user.tb_baojigroup_usr " +
                    " where tb_baojigroup_lock.baoji_id=tb_baojigroup_usr.baoji_id " +
                    " )";
        }
        List list = sqlExecuteService.executeQuery_call(sql, new CallBack() {
            @Override
            public List getResults(ResultSet rs) throws SQLException {
                LinkedList<Object> temp = new LinkedList<>();
                try {
                    while (rs.next())
                        temp.add(rs.getInt("number"));
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                return temp;
            }
        });
        int num =0;
        if(list!=null){
            num= (int) list.get(0);
        }
        return num;
    }
    //锁的历史记录
    public List<LockHis> getLockHisList(LockHisDto dto) {
        String sql="SELECT * FROM plus_lock_his."+ dto.getRecordYear()+" history " +
                " where history.lock_id="+  dto.getLockId() ;
        if(dto.getStationName1()!=null){
            sql+=" and  tb_station_inf.station_name1  like '%"+dto.getStationName1()+"%' ";
        }
        if(dto.getStationName2()!=null){
            sql+=" and  tb_station_inf.station_name2  like '%"+dto.getStationName2()+"%' ";
        }
        if(dto.getStationName3()!=null){
            sql+=" and  tb_station_inf.station_name3  like '%"+dto.getStationName3()+"%' ";
        }
        if(dto.getStationName4()!=null){
            sql+=" and  tb_station_inf.station_name4  like '%"+dto.getStationName4()+"%' ";
        }
        if(dto.getLockName()!=null){
            sql+=" and  tb_lock_inf.lock_name  like '%"+dto.getLockName()+"%' ";
        }
        if(dto.getStartTime()!=null){
            sql+=" and record_time  >='"+ ThreadLocalUtil.format(dto.getStartTime(),1)+"' ";
        }
        if(dto.getEndTime()!=null){
            sql+=" and record_time  <='"+ThreadLocalUtil.format(dto.getEndTime(),1)+"' ";
        }
        if(dto.getUid()>100){
            sql+="   and tb_lock_inf.lock_id in(" +
                    "select distinct lock_id from plus_user.tb_baojigroup_lock,plus_user.tb_baojigroup_usr " +
                    " where tb_baojigroup_lock.baoji_id=tb_baojigroup_usr.baoji_id " +
                    " and tb_baojigroup_usr.uid= " +dto.getUid()+
                    " )";
        }else{
            sql+="   and tb_lock_inf.lock_id in(" +
                    "select distinct lock_id from plus_user.tb_baojigroup_lock,plus_user.tb_baojigroup_usr " +
                    " where tb_baojigroup_lock.baoji_id=tb_baojigroup_usr.baoji_id " +
                    " )";
        }
        sql+="  ORDER BY record_time asc  limit "+ dto.getLimitStart()+","+ dto.getLimitEnd()+" ";
        List<LockHis> list=sqlExecuteService.executeQuery_call(sql, new CallBack() {
            @Override
            public List getResults(ResultSet rs) throws SQLException {
                List list=new ArrayList();
                while (rs.next()){
                    LockHis data=new LockHis();
                    data.setNum(rs.getInt("num"));
                    data.setLockId(rs.getInt("lock_id"));
                    data.setRecordTime(rs.getTimestamp("record_time"));
                    data.setLockState(rs.getInt("lock_state"));
                    data.setUnlockType(rs.getInt("unlock_type"));
                    data.setUnlockId(rs.getString("unlock_id"));
                    list.add(data);
                }
                return list;
            }
        });
        return list;
    }
 
    //锁的历史记录
    public List<LockHis> getLockHisWithReal(LockHis his) {
        String sql="SELECT * FROM plus_lock_his."+ his.getRecordYear()+" history " +
                " where history.lock_id="+  his.getLockId() ;
        if(his.getStartTime()!=null){
            sql+=" and record_time  >='"+ ThreadLocalUtil.format(his.getStartTime(),1)+"' ";
        }
        if(his.getEndTime()!=null){
            sql+=" and record_time  <='"+ThreadLocalUtil.format(his.getEndTime(),1)+"' ";
        }
        sql+="  ORDER BY record_time asc ";
        List<LockHis> list=sqlExecuteService.executeQuery_call(sql, new CallBack() {
            @Override
            public List getResults(ResultSet rs) throws SQLException {
                List list=new ArrayList();
                while (rs.next()){
                    LockHis data=new LockHis();
                    data.setNum(rs.getInt("num"));
                    data.setLockId(rs.getInt("lock_id"));
                    data.setRecordTime(rs.getTimestamp("record_time"));
                    data.setLockState(rs.getInt("lock_state"));
                    data.setUnlockType(rs.getInt("unlock_type"));
                    data.setUnlockId(rs.getString("unlock_id"));
                    list.add(data);
                }
                return list;
            }
        });
        return list;
    }
    //锁的告警历史记录总数
    public int getLockAlmHisCount(LockAlmHisDto dto) {
        String sql="SELECT  count(*) as number FROM plus_lock_alarm."+ dto.getRecordYear()+" history" +
                ",plus_inf.tb_lock_inf,plus_inf.tb_station_inf " +
                " where  history.lock_id=tb_lock_inf.lock_id " +
                "    and tb_lock_inf.station_id=tb_station_inf.station_id  ";
        if(dto.getStationName1()!=null){
            sql+=" and  tb_station_inf.station_name1  like '%"+dto.getStationName1()+"%' ";
        }
        if(dto.getStationName2()!=null){
            sql+=" and  tb_station_inf.station_name2  like '%"+dto.getStationName2()+"%' ";
        }
        if(dto.getStationName3()!=null){
            sql+=" and  tb_station_inf.station_name3  like '%"+dto.getStationName3()+"%' ";
        }
        if(dto.getStationName4()!=null){
            sql+=" and  tb_station_inf.station_name4  like '%"+dto.getStationName4()+"%' ";
        }
        if(dto.getLockName()!=null){
            sql+=" and  tb_lock_inf.lock_name  like '%"+dto.getLockName()+"%' ";
        }
        if(dto.getStartTime()!=null){
            sql+=" and alm_start_time  >='"+ ThreadLocalUtil.format(dto.getStartTime(),1)+"' ";
        }
        if(dto.getEndTime()!=null){
            sql+=" and alm_start_time  <='"+ThreadLocalUtil.format(dto.getEndTime(),1)+"' ";
        }
        if(dto.getAlmIdList()!=null&&dto.getAlmIdList().size()>0){
            List almIdList=dto.getAlmIdList();
            sql+=" and  history.alm_id in (";
            for (int i=0;i<almIdList.size();i++) {
                if(i==almIdList.size()-1){
                    sql=sql+almIdList.get(i)+") ";
                }else{
                    sql=sql+almIdList.get(i)+",";
                }
            }
        }
        if(dto.getUid()>100){
            sql+="   and tb_lock_inf.lock_id in(" +
                    "select distinct lock_id from plus_user.tb_baojigroup_lock,plus_user.tb_baojigroup_usr " +
                    " where tb_baojigroup_lock.baoji_id=tb_baojigroup_usr.baoji_id " +
                    " and tb_baojigroup_usr.uid= " +dto.getUid()+
                    " )";
        }else{
            sql+="   and tb_lock_inf.lock_id in(" +
                    "select distinct lock_id from plus_user.tb_baojigroup_lock,plus_user.tb_baojigroup_usr " +
                    " where tb_baojigroup_lock.baoji_id=tb_baojigroup_usr.baoji_id " +
                    " )";
        }
 
        List list = sqlExecuteService.executeQuery_call(sql, new CallBack() {
            @Override
            public List getResults(ResultSet rs) throws SQLException {
                LinkedList<Object> temp = new LinkedList<>();
                try {
                    while (rs.next())
                        temp.add(rs.getInt("number"));
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                return temp;
            }
        });
        int num =0;
        if(list!=null){
            num= (int) list.get(0);
        }
        return num;
    }
    //锁的告警历史记录
    public List<LockAlarmHis> getLockAlmHisList(LockAlmHisDto dto) {
        String sql="SELECT  history.* FROM plus_lock_alarm."+ dto.getRecordYear()+" history" +
                ",plus_inf.tb_lock_inf,plus_inf.tb_station_inf " +
                " where  history.lock_id=tb_lock_inf.lock_id " +
                "    and tb_lock_inf.station_id=tb_station_inf.station_id  ";
        if(dto.getStationName1()!=null){
            sql+=" and  tb_station_inf.station_name1  like '%"+dto.getStationName1()+"%' ";
        }
        if(dto.getStationName2()!=null){
            sql+=" and  tb_station_inf.station_name2  like '%"+dto.getStationName2()+"%' ";
        }
        if(dto.getStationName3()!=null){
            sql+=" and  tb_station_inf.station_name3  like '%"+dto.getStationName3()+"%' ";
        }
        if(dto.getStationName4()!=null){
            sql+=" and  tb_station_inf.station_name4  like '%"+dto.getStationName4()+"%' ";
        }
        if(dto.getLockName()!=null){
            sql+=" and  tb_lock_inf.lock_name  like '%"+dto.getLockName()+"%' ";
        }
        if(dto.getStartTime()!=null){
            sql+=" and alm_start_time  >='"+ ThreadLocalUtil.format(dto.getStartTime(),1)+"' ";
        }
        if(dto.getEndTime()!=null){
            sql+=" and alm_start_time  <='"+ThreadLocalUtil.format(dto.getEndTime(),1)+"' ";
        }
        if(dto.getAlmIdList()!=null&&dto.getAlmIdList().size()>0){
            List almIdList=dto.getAlmIdList();
            sql+=" and  history.alm_id in (";
            for (int i=0;i<almIdList.size();i++) {
                if(i==almIdList.size()-1){
                    sql=sql+almIdList.get(i)+") ";
                }else{
                    sql=sql+almIdList.get(i)+",";
                }
            }
        }
        if(dto.getUid()>100){
            sql+="   and tb_lock_inf.lock_id in(" +
                    "select distinct lock_id from plus_user.tb_baojigroup_lock,plus_user.tb_baojigroup_usr " +
                    " where tb_baojigroup_lock.baoji_id=tb_baojigroup_usr.baoji_id " +
                    " and tb_baojigroup_usr.uid= " +dto.getUid()+
                    " )";
        }else{
            sql+="   and tb_lock_inf.lock_id in(" +
                    "select distinct lock_id from plus_user.tb_baojigroup_lock,plus_user.tb_baojigroup_usr " +
                    " where tb_baojigroup_lock.baoji_id=tb_baojigroup_usr.baoji_id " +
                    " )";
        }
        sql+="  ORDER BY history.alm_start_time asc  limit "+ dto.getLimitStart()+","+ dto.getLimitEnd()+" ";
        List<LockAlarmHis> list=sqlExecuteService.executeQuery_call(sql, new CallBack() {
            @Override
            public List getResults(ResultSet rs) throws SQLException {
                List list=new ArrayList();
                while (rs.next()){
                    LockAlarmHis data=new LockAlarmHis();
                    data.setNum(rs.getInt("num"));
                    data.setLockId(rs.getInt("lock_id"));
                    data.setAlmId(rs.getInt("alm_id"));
                    data.setAlmSource(rs.getInt("alm_source"));
                    data.setAlmStartTime(rs.getTimestamp("alm_start_time"));
                    data.setAlmConfirmedTime(rs.getTimestamp("alm_confirmed_time"));
                    data.setAlmEndTime(rs.getTimestamp("alm_end_time"));
                    data.setAlmIsConfirmed(rs.getInt("alm_is_confirmed"));
                    list.add(data);
                }
                return list;
            }
        });
        return list;
    }
 
 
}