a
554325746@qq.com
2020-01-02 e52632329ef8342a2f692bf58184fc54db3d2b4c
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
package com.basic.security.manager.impl.cblite;
 
import android.text.TextUtils;
 
import com.basic.security.manager.PersonIdentityManager;
import com.basic.security.manager.impl.sqlite.SlBusinessPersonManager;
import com.basic.security.model.ModelAdapter;
import com.basic.security.utils.Constants;
import com.couchbase.lite.Expression;
 
import java.util.ArrayList;
import java.util.List;
 
public class BusinessPersonManager extends BaseManager {
 
    // 把人员和业务表关联
    public static void savePersonToBusiness(String personId, String businessId, String passModel) {
        if (Constants.useCouchbase) {
            Expression expression = Expression.property("table").equalTo(Expression.string("business_person"))
                    .and(Expression.property("business_id").equalTo(Expression.string(businessId))
                            .and(Expression.property("person_id").equalTo(Expression.string(personId))));
            ModelAdapter businessPersonDocument = findOne(expression);
            if (businessPersonDocument == null) {
                businessPersonDocument = new ModelAdapter();
            }
            businessPersonDocument.setString("person_id", personId);
            businessPersonDocument.setString("business_id", businessId);
            businessPersonDocument.setString("pass_model", passModel);
//        businessPersonDocument.setString("contact_person",);    联系人,暂无
            businessPersonDocument.setString("table", "business_person");
            save(businessPersonDocument);
        } else {
            SlBusinessPersonManager.savePersonToBusiness(personId, businessId, passModel);
        }
    }
 
    // 获取的此业务表关联的所有人
    public static List<ModelAdapter> getPersonFromBusiness(String businessId) {
        if (Constants.useCouchbase) {
            Expression expression = Expression.property("table").equalTo(Expression.string("business_person"))
                    .and(Expression.property("business_id").equalTo(Expression.string(businessId)));
 
            List<ModelAdapter> businessPersonListDocument = findList(expression); // 业务表中所有人
            List<ModelAdapter> personList = new ArrayList<>(); // 根据业务表中的所有人的人员ID获取到 数据库中的人员
            if (businessPersonListDocument.size() != 0) {
                for (ModelAdapter businessPerson : businessPersonListDocument) {
                    ModelAdapter personDocument = findById(businessPerson.getString("person_id"));
                    if (personDocument != null) {
                        if (!personList.contains(personDocument)) {
                            personList.add(personDocument);
                        } else {
                            delete(businessPerson);
                        }
                    }
                }
                return personList;
            }
            return new ArrayList<>();
        } else {
            return SlBusinessPersonManager.getPersonFromBusiness(businessId);
        }
    }
 
    // 根据业务表Id,人员id获取人员的通行规则
    public static String getPersonInBusinessPassModel(String businessId, String personId) {
        if (Constants.useCouchbase) {
            Expression expression = Expression.property("table").equalTo(Expression.string("business_person"))
                    .and(Expression.property("business_id").equalTo(Expression.string(businessId))
                            .and(Expression.property("person_id").equalTo(Expression.string(personId))));
            ModelAdapter businessPerson = findOne(expression);
 
            return businessPerson.getString("pass_model") == null ? "有效时间内通行" : businessPerson.getString("pass_model");
        } else {
            return SlBusinessPersonManager.getPersonInBusinessPassModel(businessId, personId);
        }
    }
 
    // 把人从业务标准表中删除
    public static void deleteBusinessPerson(String personId, String businessId) {
        if (Constants.useCouchbase) {
            try {
                Expression expression = Expression.property("table").equalTo(Expression.string("business_person"))
                        .and(Expression.property("business_id").equalTo(Expression.string(businessId)))
                        .and(Expression.property("person_id").equalTo(Expression.string(personId)));
                List<ModelAdapter> personList = findList(expression);
                for (ModelAdapter person : personList) {
                    delete(person);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            SlBusinessPersonManager.deleteBusinessPerson(personId, businessId);
        }
    }
 
    public static List<ModelAdapter> getPersonFromMessage(List<ModelAdapter> checkedAllPerson, String searchText) {
        if (Constants.useCouchbase) {
            if (checkedAllPerson.size() != 0) {
                List<ModelAdapter> personFromMessage = new ArrayList<>();
                for (ModelAdapter person : checkedAllPerson) {
                    String person_name = person.getString("name");
                    String person_phone = person.getString("phone");
                    String person_id_card = person.getString("id_card_number");
                    if (searchText.equals(person_id_card) || searchText.equals(person_name) || searchText.equals(person_phone)) {
                        if (person != null) {
                            personFromMessage.add(person);
                        }
                    }
                }
                return personFromMessage;
            }
 
            return new ArrayList<>();
        } else {
            return SlBusinessPersonManager.getPersonFromMessage(checkedAllPerson, searchText);
        }
    }
 
    // 有效地业务表
    public static List<ModelAdapter> getValidBusinessRuleList() {
        if (Constants.useCouchbase) {
            List<ModelAdapter> validBusinessRuleList = new ArrayList<>();
            List<ModelAdapter> businessRuleList = BusinessListManager.getBusinessList(); // 获取所有的业务表
            for (ModelAdapter businessRule : businessRuleList) {
                if ("1".equals(businessRule.getString("only_allow"))) { // 选中仅允许
                    String only_allow_start = businessRule.getString("only_allow_start");
                    String only_allow_end = businessRule.getString("only_allow_end");
                    long start_time = Long.parseLong(only_allow_start);
                    long end_time = Long.parseLong(only_allow_end);
                    if (System.currentTimeMillis() > start_time && System.currentTimeMillis() < end_time) {
                        validBusinessRuleList.add(businessRule);
                    }
                } else if ("1".equals(businessRule.getString("choose_from_time_rule"))) { // 选择的时间段
                    if (TimeRuleManager.timeRuleIsValid(businessRule.getString("checked_time_rule_id"))) {
                        validBusinessRuleList.add(businessRule);
                    }
                }
            }
            return validBusinessRuleList;
        } else {
            return SlBusinessPersonManager.getValidBusinessRuleList();
        }
    }
 
 
    // 根据人员id在有效的业务表中获取人员所在的业务表
    public static List<ModelAdapter> getPersonInValidBusinessList(String personId) {
        if (Constants.useCouchbase) {
            List<ModelAdapter> personInValidBusinessList = new ArrayList<>();
            List<ModelAdapter> validBusinessRuleList = getValidBusinessRuleList();
            for (ModelAdapter validBusinessRule : validBusinessRuleList) {
                Expression expression = Expression.property("table").equalTo(Expression.string("business_person"))
                        .and(Expression.property("business_id").equalTo(Expression.string(validBusinessRule.getId())))
                        .and(Expression.property("person_id").equalTo(Expression.string(personId)));
                ModelAdapter personDocument = findOne(expression);
                if (personDocument != null) {
                    System.out.println(personDocument.getString("pass_model"));
                }
 
                if (personDocument != null && "有效时间内通行".equals(personDocument.getString("pass_model"))) {
                    personInValidBusinessList.add(validBusinessRule);
                }
            }
 
            return personInValidBusinessList;
        } else {
            return SlBusinessPersonManager.getPersonInValidBusinessList(personId);
        }
    }
 
    // 根据业务表中提前提示时间,获取到未到时间的业务表
    public static List<ModelAdapter> getValidBusinessRuleListButTimeNotReach() {
        if (Constants.useCouchbase) {
            List<ModelAdapter> validBusinessRuleButNotReachList = new ArrayList<>();
            List<ModelAdapter> businessList = BusinessListManager.getBusinessList(); // 获取所有的业务表
            for (ModelAdapter businessRule : businessList) {
                long noReachTime = BusinessDetailManager.getNotReachRemindTime(businessRule.getId()); // 获取业务表中开始前提示的时间  分钟
                if ("1".equals(businessRule.getString("only_allow"))) { // 选中仅允许
                    String only_allow_start = businessRule.getString("only_allow_start");
                    String only_allow_end = businessRule.getString("only_allow_end");
                    long start_time = Long.parseLong(only_allow_start);
                    long end_time = Long.parseLong(only_allow_end);
                    if (System.currentTimeMillis() + noReachTime * 60 * 1000 > start_time && System.currentTimeMillis() < end_time) {
                        validBusinessRuleButNotReachList.add(businessRule);
                    }
                } else if ("1".equals(businessRule.getString("choose_from_time_rule"))) { // 选择的时间段
                    if (TimeRuleManager.timeRuleIsValid(businessRule.getString("checked_time_rule_id"), noReachTime * 60 * 1000)) {
                        validBusinessRuleButNotReachList.add(businessRule);
                    }
                }
            }
            return validBusinessRuleButNotReachList;
        } else {
            return SlBusinessPersonManager.getValidBusinessRuleListButTimeNotReach();
        }
    }
 
    // 根据人员ID,获取到人的未到时间的业务表
    public static List<ModelAdapter> getValidBusinessRuleListButTimeNotReach(String personId) {
        if (Constants.useCouchbase) {
            List<ModelAdapter> personInValidBusinessRuleButNotReachList = new ArrayList<>();
            List<ModelAdapter> validBusinessRuleListButTimeNotReach = getValidBusinessRuleListButTimeNotReach();
            for (ModelAdapter notReachBusinessRule : validBusinessRuleListButTimeNotReach) {
                Expression expression = Expression.property("table").equalTo(Expression.string("business_person"))
                        .and(Expression.property("business_id").equalTo(Expression.string(notReachBusinessRule.getId())))
                        .and(Expression.property("person_id").equalTo(Expression.string(personId)));
                ModelAdapter personDocument = findOne(expression);
                if (personDocument != null && "有效时间内通行".equals(personDocument.getString("pass_model"))) {
                    personInValidBusinessRuleButNotReachList.add(notReachBusinessRule);
                }
            }
            return personInValidBusinessRuleButNotReachList;
        } else {
            return SlBusinessPersonManager.getValidBusinessRuleListButTimeNotReach(personId);
        }
    }
 
 
    public static String getBusinessRuleDoorAccessConfirmMessage(ModelAdapter document, ModelAdapter currentPerson) {
        if (Constants.useCouchbase) {
            String message = "";
            if (BusinessDetailManager.getConfirmPassShowIdentity(document.getId())) {
                List<String> identityNameList = PersonIdentityManager.findIdentityNameByPersonId(currentPerson.getId());
                if (identityNameList.size() != 0) {
                    message = identityNameList.get(0);
                }
            }
            if (BusinessDetailManager.getConfirmPassShowName(document.getId())) {
                message = message + "  " + currentPerson.getString("name");
            }
            String confirmPassHint = BusinessDetailManager.getConfirmPassHint(document.getId());
            if (!TextUtils.isEmpty(confirmPassHint)) {
                message = message + "  " + confirmPassHint;
            }
            if (TextUtils.isEmpty(message)) {
                return "业务表:确认通行";
            }
            return message;
        } else {
            return SlBusinessPersonManager.getBusinessRuleDoorAccessConfirmMessage(document, currentPerson);
        }
    }
 
    public static String getBusinessRuleDoorAccessAllowMessage(ModelAdapter document, ModelAdapter currentPerson) {
        if (Constants.useCouchbase) {
            String message = "";
            if (BusinessDetailManager.getAccessShowTableName(document.getId())) {
                if (!TextUtils.isEmpty(document.getString("business_name"))) {
                    message = document.getString("business_name");
                }
            }
            if (BusinessDetailManager.getAccessShowIdentity(document.getId())) {
                List<String> identityNameList = PersonIdentityManager.findIdentityNameByPersonId(currentPerson.getId());
                if (identityNameList.size() != 0) {
                    message = message + "  " + identityNameList.get(0);
                }
            }
            if (BusinessDetailManager.getAccessShowName(document.getId())) {
                message = message + "  " + currentPerson.getString("name");
            }
 
            String accessHint = BusinessDetailManager.getAccessHint(document.getId());
            if (!TextUtils.isEmpty(accessHint)) {
                message = message + "  " + accessHint;
            }
            if (TextUtils.isEmpty(message)) {
                return "业务表:允许通行";
            }
            return message;
        } else {
            return SlBusinessPersonManager.getBusinessRuleDoorAccessAllowMessage(document, currentPerson);
        }
    }
 
    public static String getBusinessDoorAccessTimeNotReachMessage(ModelAdapter currentValidBusinessRuleListButTimeNotReach, ModelAdapter currentPerson) {
        if (Constants.useCouchbase) {
            String notReachHint = BusinessDetailManager.getNotReachHint(currentValidBusinessRuleListButTimeNotReach.getId());
            if (!TextUtils.isEmpty(notReachHint)) {
                return notReachHint;
            }
            return "业务表:时间未到";
        } else {
            return SlBusinessPersonManager.getBusinessDoorAccessTimeNotReachMessage(currentValidBusinessRuleListButTimeNotReach, currentPerson);
        }
    }
 
}