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 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 businessPersonListDocument = findList(expression); // 业务表中所有人 List 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 personList = findList(expression); for (ModelAdapter person : personList) { delete(person); } } catch (Exception e) { e.printStackTrace(); } } else { SlBusinessPersonManager.deleteBusinessPerson(personId, businessId); } } public static List getPersonFromMessage(List checkedAllPerson, String searchText) { if (Constants.useCouchbase) { if (checkedAllPerson.size() != 0) { List 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 getValidBusinessRuleList() { if (Constants.useCouchbase) { List validBusinessRuleList = new ArrayList<>(); List 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 getPersonInValidBusinessList(String personId) { if (Constants.useCouchbase) { List personInValidBusinessList = new ArrayList<>(); List 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 getValidBusinessRuleListButTimeNotReach() { if (Constants.useCouchbase) { List validBusinessRuleButNotReachList = new ArrayList<>(); List 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 getValidBusinessRuleListButTimeNotReach(String personId) { if (Constants.useCouchbase) { List personInValidBusinessRuleButNotReachList = new ArrayList<>(); List 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 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 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); } } }