a
554325746@qq.com
2020-01-15 956063ff14bc75e3a2a97c7bcaa06b9edc84ad24
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
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.model.Sync;
import com.basic.security.utils.Constants;
import com.basic.security.utils.ExceptionUtil;
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<ModelAdapter> findPersonIdentityListByPersonId(String personId) {
        List<ModelAdapter> 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<ModelAdapter> list = findList("select * from " + PersonIdentity.tableName + " where person_id='" + personId + "'");
        for (ModelAdapter k : list) {
            try {
                deletePhysically(k);
            } catch (Exception e) {
                ExceptionUtil.printException(e);
            }
        }
    }
 
    public static void deletePersonIdentity(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) {
            ExceptionUtil.printException(e);
        }
    }
 
    public static void save(String personId, String identityId) {
        ModelAdapter personIdentity = findOne("select * from " + PersonIdentity.tableName + " where "
                + PersonIdentity.person_id + "='" + personId + "'"
                + " and " + PersonIdentity.identity_id + "='" + identityId + "'"
        );
        if (personIdentity == null) {
            personIdentity = new ModelAdapter();
            personIdentity.setString(PersonIdentity.id, personId + "" + identityId);
            personIdentity.setString("person_id", personId);
            personIdentity.setString("identity_id", identityId);
            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<ModelAdapter> list = findList("select * from user where person_id='" + personId + "'");
        for (ModelAdapter d : list) {
            deletePhysically(d);
        }
    }
 
    public static Map<String, ModelAdapter> findIdentityMapByPersonId(String personId) {
        Map<String, ModelAdapter> ruleMap = new HashMap();
        List<ModelAdapter> personPersonIdentity = findPersonIdentityListByPersonId(personId);
        for (ModelAdapter personIdentity : personPersonIdentity) {
            String identityId = personIdentity.getString("identity_id");
            ruleMap.put(identityId, findById("identity", identityId));
        }
        return ruleMap;
    }
 
    public static List<String> findIdentityNameByPersonId(String personId) {
        Map<String, ModelAdapter> checkedIdentityMapByPerson = PersonIdentityManager.findIdentityMapByPersonId(personId);
        List<String> identityNameList = new ArrayList<>();
        for (ModelAdapter identityModelAdapter : checkedIdentityMapByPerson.values()) {
            if (identityModelAdapter != null && !TextUtils.isEmpty(identityModelAdapter.getString("name"))) {
                identityNameList.add(identityModelAdapter.getString("name"));
            }
        }
        return identityNameList;
    }
 
    public static void savePersonIdentity(ModelAdapter person_identity) {
        BaseManager.save(person_identity);
    }
 
    public static void reSavePersonIdentities(String personId, String identitiesStr) {
        String[] toIdentityArray = identitiesStr.split(",");
        List<String> dbIdentityList = IdentityManager.findIdentityNameListByPersonId(personId);
        Map<String, ModelAdapter> allIdentityMap = IdentityManager.findAllIdentitiesMap();
        List<String> newIdentityList = new ArrayList<>();
        List<String> 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);
                deletePersonIdentity(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);
    }
 
    public static void initDefaultPersonIdentities() {
        ModelAdapter admin_person_identity = BaseManager.findById("person_identity", "admin_person_identity");
        if (admin_person_identity == null) {
            admin_person_identity = new ModelAdapter("admin_person_identity")
                    .setString("table", "person_identity")
                    .setString("identity_id", IdentityManager.getAdminIdentity().getId())
                    .setString("auto_init", "1")
                    .setString("person_id", PersonManager.getAdminPerson().getId())
                    .setString("device_id", DeviceManager.getDeviceId())
                    .setString("company_id", CompanyManager.getCompanyId());
            admin_person_identity.setString(Sync.needSync, Constants.FALSE);
            PersonIdentityManager.savePersonIdentity(admin_person_identity);
        }
    }
}