xuxiuxi
2017-08-01 e09e9f8a34cbc99a33dfa9ef1792b0025575c3a8
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package cn.com.basic.face.util;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import cn.com.basic.face.discern.common.CommonVariables;
import cn.com.basic.face.discern.query.item.SurveillanceQueryItem;
import cn.com.basic.face.fragment.SurveillanceFragment;
 
public class SurveillanceQueryItemUtil {
 
    private static SurveillanceQueryItemUtil instance = new SurveillanceQueryItemUtil();
 
    public static SurveillanceQueryItemUtil getInstance() {
        return instance;
    }
 
    //所有监控图片
    public static List<SurveillanceQueryItem> allItems = new ArrayList<>();
    //访客选项卡的分类(未注册/人员类型、来访状态、标签状态)
    //考勤选项卡(考勤人员类型:教师、学生、员工,来访状态:签到、签退)
    //第一行显示未注册,第二行显示来访
    public static List<SurveillanceQueryItem> visitorTwoRows = new ArrayList<>();
    //第一行显示签到,第二行显示签退
    public static List<SurveillanceQueryItem> attendanceTwoRows = new ArrayList<>();
    //未注册的访客列表, 选择时用
    public static List<SurveillanceQueryItem> allUnregisterVisitorTwoRowsForSelect = new ArrayList();
    //未注册的考勤列表, 选择时用
    public static List<SurveillanceQueryItem> allUnregisterAttenderTwoRowsForSelect = new ArrayList();
 
    public void addItems(List<SurveillanceQueryItem> returnItems, List<SurveillanceQueryItem> requestItems, String cameraInOrOut) {
        for ( int i = 0; i < returnItems.size(); i++) {
            SurveillanceQueryItem returnItem = returnItems.get(i);
            SurveillanceQueryItem requestItem = requestItems.get(i);
            addItem(returnItem, requestItem, cameraInOrOut);
        }
        removeTimeoutItemAndShow();
        limitItemsToN();
    }
 
    public void addItem(SurveillanceQueryItem returnItem, SurveillanceQueryItem requestItem, String cameraInOrOut) {
        if (!CommonVariables.FACE_SERVER) {
            RtspFaceNative.setFaceLabel(Integer.parseInt(cameraInOrOut), requestItem.getStTrackId(), returnItem.getVisitorType()==null?"":returnItem.getVisitorType().substring(0,
                    returnItem.getVisitorType().length() > 3 ? 3 : returnItem.getVisitorType().length()
                    ));
        }
        returnItem.setCreateTime(new Date());
        returnItem.setStTrackId(requestItem.getStTrackId());
        returnItem.setBitmap(requestItem.getBitmap());
        returnItem.setNativeImg(requestItem.getNativeImg());
        returnItem.setWidth(requestItem.getWidth());
        returnItem.setHeight(requestItem.getHeight());
        allItems.add(0, returnItem);
    }
 
    private void limitItemsToN() {
        int max = 25;
        int unRegisters = 0;
        int checkIns = 0;
        int attenderEnter = 0;
        int attenderExit = 0;
        List<SurveillanceQueryItem> itemsToRemove = new ArrayList<>();
        for (int i = 0; i < allItems.size(); i++) {
            SurveillanceQueryItem item = allItems.get(i);
            int type = item.getRegisterOrCheckIn();
            if (type == CommonVariables.Surveillance.VISIT_REGISTER_ITEM_TYPE) {
                unRegisters++;
                if (unRegisters > max) {
                    itemsToRemove.add(item);
                }
            }
            if (type == CommonVariables.Surveillance.VISIT_CHECK_IN_ITEM_TYPE) {
                checkIns++;
                if (checkIns > max) {
                    itemsToRemove.add(item);
                }
            }
            if (type == CommonVariables.Surveillance.VISIT_ATTENDANCE_IN) {
                attenderEnter++;
                if (attenderEnter > max) {
                    itemsToRemove.add(item);
                }
            }
            if (type == CommonVariables.Surveillance.VISIT_ATTENDANCE_OUT) {
                attenderExit++;
                if (attenderExit > max) {
                    itemsToRemove.add(item);
                }
            }
        }
        allItems.removeAll(itemsToRemove);
    }
 
    public List getVisitorTwoRows() {
        visitorTwoRows.clear();
        for (SurveillanceQueryItem item : allItems) {
            int type = item.getRegisterOrCheckIn();
            if (type == CommonVariables.Surveillance.VISIT_REGISTER_ITEM_TYPE
                    || type == CommonVariables.Surveillance.VISIT_CHECK_IN_ITEM_TYPE
                    ) {
                visitorTwoRows.add(item);
            }
        }
        VisitorUtil.removeDuplicateCheckIn();
        VisitorUtil.removeDuplicateUnregister();
        VisitorUtil.sortByRow();
        return visitorTwoRows;
    }
 
    public List getAttendanceTwoRows() {
        attendanceTwoRows.clear();
        for (SurveillanceQueryItem item : allItems) {
            if (item.getRegisterOrCheckIn() ==  CommonVariables.Surveillance.VISIT_ATTENDANCE_IN ||
                    item.getRegisterOrCheckIn() ==  CommonVariables.Surveillance.VISIT_ATTENDANCE_OUT
                    ) {
                attendanceTwoRows.add(item);
            }
        }
        AttendentUtil.removeDuplicateAttendanceIn();
        AttendentUtil.removeDuplicateAttendanceOut();
        AttendentUtil.sortByRow();
        return attendanceTwoRows;
    }
 
    public List getAllUnregisterVisitorTwoRowsForSelect(long selectedId) {
        allUnregisterVisitorTwoRowsForSelect.clear();
        List<SurveillanceQueryItem> registerList = new ArrayList();
        int countPerRow = 9;
        for (SurveillanceQueryItem surveillanceQueryItem : allItems) {
            if (surveillanceQueryItem.getRegisterOrCheckIn() == CommonVariables.Surveillance.VISIT_REGISTER_ITEM_TYPE) {
                if (registerList.size() > countPerRow*2) {
                    break;
                }
                if (surveillanceQueryItem.getIdForSelect() == selectedId) {
                    surveillanceQueryItem.setSurveillancePhotoSelected(true);
                } else {
                    surveillanceQueryItem.setSurveillancePhotoSelected(false);
                }
                registerList.add(surveillanceQueryItem);
            }
        }
 
        SurveillanceQueryItem item;
        List items = new ArrayList();
        int m = 0, n = 0;
        for (int i = 0; i < countPerRow*2; i++) {
            if (i % 2 == 0) {
                if (m < registerList.size()) {
                    item = registerList.get(m);
                    m++;
                } else {
                    item = VisitorUtil.getBlankItem();
                }
            } else {
                if (n < registerList.size() - countPerRow) {
                    item = registerList.get(countPerRow+n);
                    n++;
                } else {
                    item = VisitorUtil.getBlankItem();
                }
            }
            items.add(item);
        }
        allUnregisterVisitorTwoRowsForSelect.addAll(items);
        return allUnregisterVisitorTwoRowsForSelect;
    }
 
    public List getAllUnregisterAttenderTwoRowsForSelect(long selectedId) {
        allUnregisterAttenderTwoRowsForSelect.clear();
        List<SurveillanceQueryItem> registerList = new ArrayList();
        int countPerRow = 9;
        for (SurveillanceQueryItem surveillanceQueryItem : allItems) {
            if (surveillanceQueryItem.getRegisterOrCheckIn() == CommonVariables.Surveillance.VISIT_REGISTER_ITEM_TYPE) {
                if (registerList.size() > countPerRow*2) {
                    break;
                }
                if (surveillanceQueryItem.getIdForSelect() == selectedId) {
                    surveillanceQueryItem.setSurveillancePhotoSelected(true);
                } else {
                    surveillanceQueryItem.setSurveillancePhotoSelected(false);
                }
                registerList.add(surveillanceQueryItem);
            }
        }
 
        SurveillanceQueryItem item;
        List items = new ArrayList();
        int m = 0, n = 0;
        for (int i = 0; i < countPerRow*2; i++) {
            if (i % 2 == 0) {
                if (m < registerList.size()) {
                    item = registerList.get(m);
                    m++;
                } else {
                    item = VisitorUtil.getBlankItem();
                }
            } else {
                if (n < registerList.size() - countPerRow) {
                    item = registerList.get(countPerRow+n);
                    n++;
                } else {
                    item = VisitorUtil.getBlankItem();
                }
            }
            items.add(item);
        }
        allUnregisterAttenderTwoRowsForSelect.addAll(items);
        return allUnregisterAttenderTwoRowsForSelect;
    }
 
    public void removeTimeoutItemAndShow() {
        List itemsToRemove = new ArrayList();
        for (SurveillanceQueryItem item : allItems) {
            if (item.getRegisterOrCheckIn() == CommonVariables.Surveillance.VISIT_REGISTER_ITEM_TYPE) {
                Date now = new Date();
                Date createTime = item.getCreateTime();
                double minutes = (now.getTime() - createTime.getTime())*1.0 / 1000 / 60;
                if (minutes >= 5) {
                    itemsToRemove.add(item);
                }
            }
        }
        allItems.removeAll(itemsToRemove);
        SurveillanceFragment.getInstance().get_fragment_supervisory_bottom_visitor_list_view().show(SurveillanceQueryItemUtil.getInstance().getVisitorTwoRows());
        SurveillanceFragment.getInstance().get_fragment_supervisory_bottom_attendance_list_view().show(SurveillanceQueryItemUtil.getInstance().getAttendanceTwoRows());
    }
 
    static class VisitorUtil {
 
        public static void removeDuplicateCheckIn() {
            List itemsToRemove = new ArrayList();
            Set<String> registerIds = new HashSet<>();
            for (SurveillanceQueryItem item : visitorTwoRows) {
                if (item.getRegisterOrCheckIn() == Constant.Surveillance.VISIT_CHECK_IN_ITEM_TYPE) {
                    if (registerIds.contains(item.getRegisterId()+"_"+item.getInOrOutCamera())) {
                        itemsToRemove.add(item);
                    } else {
                        registerIds.add(item.getRegisterId()+"_"+item.getInOrOutCamera());
                    }
                }
            }
            visitorTwoRows.removeAll(itemsToRemove);
        }
 
        public static void removeDuplicateUnregister() {
            List<SurveillanceQueryItem> registerList = new ArrayList<SurveillanceQueryItem>();
            List itemsToRemove = new ArrayList();
            for (SurveillanceQueryItem item : visitorTwoRows) {
                if (item.getRegisterOrCheckIn() == Constant.Surveillance.VISIT_REGISTER_ITEM_TYPE) {
                    int trackId = item.getStTrackId();
                    boolean trackIdExist = false;
                    for (SurveillanceQueryItem registerItem : registerList) {
                        if (registerItem.getStTrackId() == trackId) {
                            itemsToRemove.add(item);
                        }
                    }
                    if (!trackIdExist) {
                        registerList.add(item);
                    }
                }
            }
            visitorTwoRows.removeAll(itemsToRemove);
        }
 
        public static void sortByRow() {
            List<SurveillanceQueryItem> registerList = new ArrayList<SurveillanceQueryItem>();
            List<SurveillanceQueryItem> checkInList = new ArrayList<SurveillanceQueryItem>();
            int i1 = 0, j = 0;
            int maxRegister = 50;
            int maxCheckIn = 50;
            for (SurveillanceQueryItem item : visitorTwoRows) {
                if (item.getRegisterOrCheckIn() == Constant.Surveillance.VISIT_REGISTER_ITEM_TYPE) {
                    if (i1 < maxRegister) {
                        registerList.add(item);
                        i1++;
                    }
                }
                if (item.getRegisterOrCheckIn() == Constant.Surveillance.VISIT_CHECK_IN_ITEM_TYPE) {
                    if (j < maxCheckIn) {
                        checkInList.add(item);
                        j++;
                    }
                }
            }
 
            int max = registerList.size() > checkInList.size() ? registerList.size() : checkInList.size();
 
            List tempList = new ArrayList();
 
            int m = 0, n = 0;
            for (int i = 0; i < max * 2; i++) {
                SurveillanceQueryItem item;
                if (i % 2 == 0) {
                    if (m < registerList.size()) {
                        item = registerList.get(m);
                    } else {
                        item = getBlankItem();
                    }
                    m++;
                } else {
                    if (n < checkInList.size()) {
                        item = checkInList.get(n);
                    } else {
                        item = getBlankItem();
                    }
                    n++;
                }
                tempList.add(item);
            }
            visitorTwoRows.clear();
            visitorTwoRows.addAll(tempList);
        }
 
        public static SurveillanceQueryItem getBlankItem() {
            SurveillanceQueryItem blankItem = new SurveillanceQueryItem();
            blankItem.setRegisterOrCheckIn(Constant.Surveillance.VISIT_BLANK_ITEM_TYPE);
            return blankItem;
        }
 
    }
 
    static class AttendentUtil {
 
        public static void removeDuplicateAttendanceIn() {
            Map<String, String> registerIdMap = new HashMap<>();
            List itemsToRemove = new ArrayList();
            for (SurveillanceQueryItem item : attendanceTwoRows) {
                if (item.getRegisterOrCheckIn() == CommonVariables.Surveillance.VISIT_ATTENDANCE_IN) {
                    String registerId = item.getRegisterId();
                    if (registerIdMap.containsKey(item.getRegisterId())) {
                        itemsToRemove.add(item);
                    } else {
                        registerIdMap.put(registerId, registerId);
                    }
                }
            }
            attendanceTwoRows.removeAll(itemsToRemove);
        }
 
        public static void removeDuplicateAttendanceOut() {
            Map<String, String> registerIdMap = new HashMap<>();
            List itemsToRemove = new ArrayList();
            for (SurveillanceQueryItem item : attendanceTwoRows) {
                if (item.getRegisterOrCheckIn() == CommonVariables.Surveillance.VISIT_ATTENDANCE_OUT) {
                    String registerId = item.getRegisterId();
                    if (registerIdMap.containsKey(item.getRegisterId())) {
                        itemsToRemove.add(item);
                    } else {
                        registerIdMap.put(registerId, registerId);
                    }
                }
            }
            attendanceTwoRows.removeAll(itemsToRemove);
        }
 
        public static void sortByRow() {
            List<SurveillanceQueryItem> attenderInList = new ArrayList<SurveillanceQueryItem>();
            List<SurveillanceQueryItem> attenderOutList = new ArrayList<SurveillanceQueryItem>();
            int i1 = 0, j = 0;
            int maxAttenderIn = 50;
            int maxAttenderOut = 50;
            for (SurveillanceQueryItem item : attendanceTwoRows) {
                if (item.getRegisterOrCheckIn() == CommonVariables.Surveillance.VISIT_ATTENDANCE_IN) {
                    if (i1 < maxAttenderIn) {
                        attenderInList.add(item);
                        i1++;
                    }
                }
                if (item.getRegisterOrCheckIn() == CommonVariables.Surveillance.VISIT_ATTENDANCE_OUT) {
                    if (j < maxAttenderOut) {
                        attenderOutList.add(item);
                        j++;
                    }
                }
            }
 
            int max = attenderInList.size() > attenderOutList.size() ? attenderInList.size() : attenderOutList.size();
 
            List tempList = new ArrayList();
 
            int m = 0, n = 0;
            for (int i = 0; i < max * 2; i++) {
                SurveillanceQueryItem item;
                if (i % 2 == 0) {
                    if (m < attenderInList.size()) {
                        item = attenderInList.get(m);
                    } else {
                        item = getBlankItem();
                    }
                    m++;
                } else {
                    if (n < attenderOutList.size()) {
                        item = attenderOutList.get(n);
                    } else {
                        item = getBlankItem();
                    }
                    n++;
                }
                tempList.add(item);
            }
            attendanceTwoRows.clear();
            attendanceTwoRows.addAll(tempList);
        }
 
        public static SurveillanceQueryItem getBlankItem() {
            SurveillanceQueryItem blankItem = new SurveillanceQueryItem();
            blankItem.setRegisterOrCheckIn(CommonVariables.Surveillance.VISIT_ATTENDANCE_BLANK);
            return blankItem;
        }
    }
 
}