package com.basic.security.manager; import android.text.TextUtils; import com.basic.security.model.Identity; import com.basic.security.model.ModelAdapter; import com.basic.security.model.PersonIdentity; import com.basic.security.utils.SocketUtil; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; //人员身份表 public class PersonIdentityManager extends BaseManager { public static List findPersonIdentityListByPersonId(String personId) { List personIdentityListByPerson = findList("select * from person_identity where person_id='" + personId + "' and del_flag ='0'"); return personIdentityListByPerson; } public static void deletePersonIdentityByPersonId(String personId) { deleteList(findPersonIdentityListByPersonId(personId)); List list = BaseManager.findList("select * from user where person_id='" + personId + "'"); for (ModelAdapter k : list) { try { deletePhysically(k); } catch (Exception e) { e.printStackTrace(); } } } public static void delete(String personId, String identity_id) { try { ModelAdapter personIdentity = findOne("select * from person_identity where person_id='" + personId + "' and identity_id='" + identity_id + "' and del_flag ='0'"); if (personIdentity != null) { SocketUtil.rpcCallDeletePersonIdentity(personIdentity); deletePhysically(personIdentity); } } catch (Exception e) { e.printStackTrace(); } } public static void save(String personId, String ruleId) { ModelAdapter personIdentity = findOne("select * from " + PersonIdentity.tableName + " where " + PersonIdentity.person_id + "='" + personId + "'" + " and " + PersonIdentity.identity_id + "='" + ruleId + "'" ); if (personIdentity == null) { personIdentity = new ModelAdapter(); personIdentity.setString(PersonIdentity.id, personId + "" + ruleId); personIdentity.setString("person_id", personId); personIdentity.setString("identity_id", ruleId); personIdentity.setString("del_flag", "0"); personIdentity.setString("table", "person_identity"); personIdentity.setString("device_id", DeviceManager.getDeviceId()); personIdentity.setString("company_id", CompanyManager.getCompanyId()); save(personIdentity); SocketUtil.rpcCallSavePersonIdentity(personIdentity); } } public static void saveAdminIdentity(String personId) { } public static void delAdminIdentity(String personId) { List list = findList("select * from user where person_id='" + personId + "'"); for (ModelAdapter d : list) { deletePhysically(d); } } public static Map findIdentityMapByPersonId(String personId) { Map ruleMap = new HashMap(); List personPersonIdentity = findPersonIdentityListByPersonId(personId); for (ModelAdapter personIdentity : personPersonIdentity) { String ruleId = personIdentity.getString("identity_id"); ruleMap.put(ruleId, findById("identity", ruleId)); } return ruleMap; } public static List findIdentityNameByPersonId(String personId) { Map checkedIdentityMapByPerson = PersonIdentityManager.findIdentityMapByPersonId(personId); List ruleNameList = new ArrayList<>(); for (ModelAdapter identityModelAdapter : checkedIdentityMapByPerson.values()) { if (identityModelAdapter != null && !TextUtils.isEmpty(identityModelAdapter.getString("name"))) { ruleNameList.add(identityModelAdapter.getString("name")); } } return ruleNameList; } public static void savePersonIdentity(ModelAdapter person_identity) { BaseManager.save(person_identity); } public static void reSavePersonIdentities(String personId, String identitiesStr) { String[] toIdentityArray = identitiesStr.split(","); List dbIdentityList = IdentityManager.findIdentityNameListByPersonId(personId); Map allIdentityMap = IdentityManager.findAllIdentitiesMap(); List newIdentityList = new ArrayList<>(); List removePersonIdentityList = new ArrayList<>(); for (String toIdentity : toIdentityArray) { if (dbIdentityList.contains(toIdentity)) { dbIdentityList.remove(toIdentity); } else { newIdentityList.add(toIdentity); } } removePersonIdentityList.addAll(dbIdentityList); for (String removePersonIdentity : removePersonIdentityList) { if (allIdentityMap.containsKey(removePersonIdentity)) { String identityId = allIdentityMap.get(removePersonIdentity).getString(Identity.id); delete(personId, identityId); } } for (String newIdentityName : newIdentityList) { if (allIdentityMap.containsKey(newIdentityName)) { PersonIdentityManager.savePersonIdentity(personId, allIdentityMap.get(newIdentityName).getString(Identity.id)); } } } public static void savePersonIdentity(String personId, String identityId) { ModelAdapter personIdentity = new ModelAdapter(personId + "" + identityId) .setString("table", "person_identity") .setString("identity_id", identityId) .setString("auto_init", "0") .setString("person_id", personId) .setString("device_id", DeviceManager.getDeviceId()) .setString("company_id", CompanyManager.getCompanyId()); save(personIdentity); } }