hefeixia
2021-02-18 5b8c95c760840f09910730943b21391e47187315
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
package chat.user;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
import chat.medeasy.HttpClientUtil;
import chat.module.ModuleLoader;
import chat.module.entity.Group;
import chat.module.entity.Member;
import chat.module.entity.MessageContainer;
import chat.module.entity.PrivateFriend;
import chat.redis.RedisUtil;
import frame.object.data.DataObject;
import frame.object.data.Entity;
import frame.persist.NamedSQL;
 
public class UserStore {
 
    private static Map<String, User> idUserMap;
    private static Map<String, User> phoneUserMap;
    private static Map<String, User> extraUserMap;
    private static User cachedUser;
    private static String cachedUserId;    
    
    static {
        init();
    }
    
    private static void init() {
        idUserMap = new ConcurrentHashMap<String, User>();
        phoneUserMap = new ConcurrentHashMap<String, User>();
        extraUserMap = new ConcurrentHashMap<String, User>();
    }
    
    public static void loadOne(Entity entity) {
        String id = entity.getString("id");
        
        User user = new User(id);
        user.load(entity);
        
        addOne(user);
    }
    
    public static void addOne(User user) {
        if (user == null) {
            return;
        }
        
        String id = user.getId();
        idUserMap.put(id, user);
        
        String phone = user.getMobile();
        if (phone != null) {
            phoneUserMap.put(phone, user);
        }
        
        String extra = user.getExtra();
        if (extra != null) {
            extraUserMap.put(extra, user);
        }
    }
 
    public static User getOrCreate(User user) throws Exception {
        String userId = user.getId();
        String phone = user.getMobile();
        //String bizid = user.getBizid();
        String flag = "";
        
        User userResult = null;
        
        //1. get user by id
        if (userId != null) {
            //1.1 get from cache
            userResult = getById(userId);
            
            //1.2 get from database
            if (userResult == null) {
                userResult = loadOneUserById(userId);
            }
        }
        //2. get user by phone
        else if (phone != null) {
            //2.1 get from cache
            userResult = getByPhone(phone);
            
            //2.2 get from database
            if (userResult == null) {
                userResult = loadOneUserByPhone(phone);
            }
        }
        
        //3. create new user
        if (userResult == null) {
            userResult = user;
            
            userResult.setDefaultValues();
            userResult = createOneUser(userResult);
            
            //调用医视无忧接口,同步创建账号 需等接口调通  希望用调用修改
            Map<String, String> heads = new HashMap<String, String>();
            heads.put("Content-Type", "application/json");
            String json = "{\"doctorMobile\":\""+phone+"\",\"certificateStatus\": 0,\"sourceType\": 2}";
            HttpClientUtil.execHttpClient(json, "http://api-test.medeasy123.com/doctor/api/save", heads);
            
            if (!userResult.getRoletype().equals("")) {
                Map<String, String> userMap = user.getRoleUserId(userResult.getMobile(), userResult.getRoletype());
                userResult.setExtra(userMap.get("id"));
                userResult.setName(userMap.get("name"));
                userResult.setDisplayName(userMap.get("name"));
            }
            
            //修改extra
            int resultRecord = UserStore.modifyUser(userResult, "id", null);
             
            if (user.getRoletype().equals("1")) {
                 flag = "doctor:id:" + user.getExtra();
            } else if (user.getRoletype().equals("2")) {
                 flag = "client:id:" + user.getExtra();
            }
            String jsonStr = "{\"id\": \""+user.getExtra()+"\", \"name\" : \""+user.getName()+"\", \"portrait\": \""+user.getPortrait()+"\", \"roletype\": \""+user.getRoletype()+"\",\"mobile\": \""+user.getMobile()+"\"}";
            RedisUtil.createRedisKey(flag, jsonStr);
            
            UserStore.addOne(user);
        }
        
        return userResult;
    }
    
    public static User getCachedById(String userId) {
        if (userId == null) {
            return null;
        }
        
        if (userId.equals(cachedUserId)) {
            return cachedUser;
        }
        
        cachedUserId = userId;
        cachedUser = idUserMap.get(cachedUserId);
        
        return cachedUser;
    }    
 
    public static User getById(String userId) {
        if (userId == null) {
            return null;
        }
        
        return idUserMap.get(userId);
    }
    
    public static User getByExtra(String extra) {
        if (extra == null) {
            return null;
        }
        
        return extraUserMap.get(extra);
    }
    
    private static User getByPhone(String phone) {
        if (phone == null) {
            return null;
        }
        
        return phoneUserMap.get(phone);
    }
    
    private static User loadOneUserById(String userId) throws Exception {
        if (userId == null) {
            return null;
        }
        
        NamedSQL namedSQL = NamedSQL.getInstance("getOneUserById");
        namedSQL.setParam("id", userId);
        Entity entity = namedSQL.getEntity();
        
        if (entity == null) {
            return null;
        }
        
        User user = new User(userId);
        user.load(entity);
        
        return user;
    }
    
    private static User loadOneUserByPhone(String phone) throws Exception {
        if (phone == null) {
            return null;
        }
        
        NamedSQL namedSQL = NamedSQL.getInstance("getOneUserByPhone");
        namedSQL.setParam("phone", phone);
        Entity entity = namedSQL.getEntity();
        
        if (entity == null) {
            return null;
        }
        
        User user = new User(null);
        user.load(entity);
        
        return user;
    }
    
    public static User createOneUser(User user)  {
        String flag = "";
        MessageContainer messageContainer;
        try {
             DataObject dataObject = DataObject.getInstance("usr");
            
             Entity entity = dataObject.newEntity();
             user.pushTo(entity);
             dataObject.insertToDataBase(entity);
             
             UserStore.addOne(user);
             
             //1.创建chatspace 单聊
             PrivateFriend privateGroup = new PrivateFriend("医生助理", user);
             privateGroup.createPrivateFriend();
             ModuleLoader.getPrivateFriendBucket().addOne(privateGroup.getId(), privateGroup);
             
             //2.创建member
             List<String> userList = new ArrayList<String>();
             userList.add(user.getId());
             userList.add("user004");
             messageContainer = ModuleLoader.getPrivateFriendBucket().getOne(privateGroup.getId());
             messageContainer.addInitMembers(privateGroup.getId(), userList);
             
             //3?.需要发一条消息暂时需不需要
             
             //4.创建frienship
             user.createInitFrieldShip(user);
 
             return user;            
        } catch(Exception e) {
             return null;
        }
    }
    
    public static int modifyUser(User user, String pointColumn) {
        return modifyUser(user, pointColumn, null);
    }
    
    /**
     * 
     * @param userId
     * @return
     */
    public static int modifyUser(User user, String pointColumn, Map<String, String> excludeColumn) {
        if (user == null) {
            return -2;
        }
        
        try {
             DataObject dataObject = DataObject.getInstance("usr");
             Entity entity = dataObject.newEntity();
             user.pushToModify(entity);
             int result = dataObject.updateToDateBaseWill(entity, pointColumn, excludeColumn);
             
             return result;
        } catch (Exception e) {
            return 0;
        }
    }
 
    public static boolean contains(String userId) {
        if (userId == null) {
            return false;
        }
        
        return idUserMap.containsKey(userId);
    }
 
    public static Map<String, User> getIdUserMap() {
        return idUserMap;
    }
    
}