xuxiuxi
2017-04-25 d4445e538a4fe99f21b4030f96852a51ce4fca8a
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
package cn.com.basic.face.service;
 
import android.graphics.Bitmap;
import android.util.Base64;
 
import org.xutils.http.RequestParams;
import org.xutils.x;
 
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
import cn.com.basic.face.base.BaseApplication;
import cn.com.basic.face.discern.common.BaseCommonCallBack;
import cn.com.basic.face.discern.common.CommonVariables;
import cn.com.basic.face.discern.query.item.SurveillanceQueryItem;
import cn.com.basic.face.fragment.SurveillanceFragment;
import cn.com.basic.face.util.AppApi;
import cn.com.basic.face.util.Constant;
 
public class SurveillanceMng {
 
    private static SurveillanceMng instance = new SurveillanceMng();
 
    public static SurveillanceMng getInstance() {
        return instance;
    }
 
    public void findSurveillanceBottomList(final boolean isVisit) {
        if (!BaseApplication.deviceAvailable) {
            return;
        }
//        RequestParams params = new RequestParams(AppApi.BASE_URL +AppApi.Query.SURVEILLANCE_QUERY);
//        params.addBodyParameter(CommonVariables.Page.PAGE_NUM, "1");
//        params.addBodyParameter(CommonVariables.Page.PAGE_SIZE, "10");
//        x.http().post(params, new BaseCommonCallBack() {
//            public void success() {
//                List<SurveillanceQueryItem> list = getList(SurveillanceQueryItem.class);
//                if (isVisit) {
//                    int i = 0;
//                    for (SurveillanceQueryItem item : list) {
//                        if (i % 5 == 0) {
//                            item.setRegisterOrCheckIn(CommonVariables.Surveillance.VISIT_REGISTER_ITEM_TYPE);
//                        } else {
//                            item.setRegisterOrCheckIn(CommonVariables.Surveillance.VISIT_CHECK_IN_ITEM_TYPE);
//                        }
//                        i++;
//                    }
//
//                    List<SurveillanceQueryItem> doubleList = new ArrayList<SurveillanceQueryItem>();
//                    fillBlank(list, doubleList);
//
//                    SurveillanceFragment.getInstance().get_fragment_supervisory_bottom_visit_list_view().show(doubleList, true);
//                } else {
//                    SurveillanceFragment.getInstance().get_fragment_supervisory_bottom_attendance_list_view().show(list, true);
//                }
//            }
//        });
    }
 
    public void addBitmap(final List<Bitmap> bitmapList, byte[] byteArray, int len, String cameraInOrOut) {
        if (!BaseApplication.deviceAvailable) {
            return;
        }
        //final int cameraInOrOut = Integer.parseInt(CommonVariables.Camera.IN);
        RequestParams params = new RequestParams(AppApi.BASE_URL +AppApi.SURVEILLANCE_UPLOAD);
        params.addBodyParameter("base64Photo", Base64.encodeToString(byteArray, 0, len, Base64.DEFAULT));
        params.addBodyParameter("deviceCompanyId", BaseApplication.getInstance().getDevice().getDeviceCompanyId());
        params.addBodyParameter("deviceId", BaseApplication.getInstance().getDevice().getDeviceId());
        params.addBodyParameter("InOrOutCamera", cameraInOrOut+"");
        x.http().post(params, new BaseCommonCallBack() {
            public void success() {
                List<SurveillanceQueryItem> items = getList(SurveillanceQueryItem.class);
                List prevList = SurveillanceFragment.getInstance().get_fragment_supervisory_bottom_visit_list_view().getPrevList();
 
                List newList = new ArrayList();
 
                for (int i = 0; items != null && i < items.size(); i++) {
                    SurveillanceQueryItem item = items.get(i);
                    int type = item.getRegisterOrCheckIn();
                    if (type == CommonVariables.Surveillance.VISIT_REGISTER_ITEM_TYPE
                            || type == CommonVariables.Surveillance.VISIT_CHECK_IN_ITEM_TYPE
                            ) {
                        newList.add(0, item);
                        if (i < bitmapList.size()) {
                            item.setBitmap(bitmapList.get(i));
                        }
                    }
                }
 
                if (newList.size() > 0) {
 
                    List<SurveillanceQueryItem> doubleList = new ArrayList<SurveillanceQueryItem>();
 
                    prevList.addAll(0, newList);
 
                    fillBlank(prevList, doubleList);
 
                    SurveillanceFragment.getInstance().get_fragment_supervisory_bottom_visit_list_view().show(doubleList, true);
                }
 
            }
        });
    }
 
    private void fillBlank(List<SurveillanceQueryItem> list, List<SurveillanceQueryItem> doubleList) {
        doubleList.clear();
        List<SurveillanceQueryItem> registerList = new ArrayList<SurveillanceQueryItem>();
        List<SurveillanceQueryItem> checkInList = new ArrayList<SurveillanceQueryItem>();
        removeDuplicate(list);
        int maxRegister = 20;
        int i1 = 0, j = 0;
        int maxCheckIn = 20;
        for (SurveillanceQueryItem item : list) {
            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();
 
        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++;
            }
            doubleList.add(item);
        }
 
    }
 
    private void removeDuplicate(List<SurveillanceQueryItem> list) {
        List itemsToRemove = new ArrayList();
        Set<String> registerIds = new HashSet<>();
        for (SurveillanceQueryItem item : list) {
            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());
                }
            }
        }
        list.removeAll(itemsToRemove);
    }
 
    private SurveillanceQueryItem getBlankItem() {
        SurveillanceQueryItem blankItem = new SurveillanceQueryItem();
        blankItem.setRegisterOrCheckIn(Constant.Surveillance.VISIT_BLANK_ITEM_TYPE);
        return blankItem;
    }
 
}