P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package foundation.user;
 
import java.util.List;
import java.util.Set;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import foundation.capacity.Actor;
import foundation.capacity.ActorBucket;
import foundation.capacity.ActorTarget;
import foundation.capacity.ActorType;
import foundation.capacity.ActorsRuntime;
import foundation.capacity.sight.Sight;
import foundation.dao.Filter;
import foundation.data.entity.Entity;
import foundation.data.entity.EntitySet;
import foundation.data.object.DataObject;
import foundation.data.object.EntitySaver;
import foundation.json.IJSONProvider;
import foundation.json.IJSONWriter;
import foundation.json.JSONReader;
import foundation.org.Employee;
import foundation.org.Employees;
import foundation.org.Org;
import foundation.org.OrgAccount;
import foundation.org.Position;
import foundation.token.IOnlineUser;
import foundation.token.IUser;
import foundation.util.CaseInsensitiveSet;
import foundation.util.MapList;
import foundation.variant.provider.DataEvent;
import foundation.variant.provider.VariantProviderType;
 
public class User extends IUser implements IJSONProvider {
    
    protected static Logger logger;
    private static Set<String> adminSet;
    private String id;
    private String name;
    private String password;
    private boolean passNeedChange;
    private String remark;
    private String employeeId;
    private String orgId;
    private Org org;
    private Employees employees;
    private MapList<String, Position> positions;
    private MapList<String, ActorTarget> actors;
    private ActorsRuntime actorsRuntime;
    private boolean built;
    
    
    static {
        logger = LogManager.getLogger(User.class); 
        adminSet = new CaseInsensitiveSet();
        adminSet.add("admin");
    }
    
    public User() {
        actors = new MapList<String, ActorTarget>();
        actorsRuntime = new ActorsRuntime();
        passNeedChange = false;
        built = false;
    }
    
    public static User getInstance(String userId) {
        UserBucket userBucket = UserBucket.getInstance();
        User user = userBucket.getOrLoad(userId);
        
        return user;
    }
    
    public void load(Entity entity) {
        id = entity.getString("id");
        name = entity.getString("name");
        password = entity.getString("password");
        passNeedChange = entity.getBoolean("pass_need_change", true);
        remark = entity.getString("remark");
        orgId = entity.getString("org_id");
    }
 
    public void loadHierarchyAndCapacitys() {
        try {
            actorsRuntime.clear();
            
            //1. 加载公司、员工、岗位
            loadHierarchys();
            
            //2. 获取角色列表,创建Runtime
            collectActors();
            
            built = true;
        }
        catch (Exception e) {
            e.printStackTrace();
        }        
    }
    
    private void loadHierarchys() throws Exception {
        //1. 加载用户关联的公司
        org = Org.getInstance(orgId);
        
        //2. 加载用户关联的员工(包含员工关联的岗位)
        employees = Employees.getInstance(id);
 
        positions = employees.getPositions();
    }
 
    private void collectActors() throws Exception {
        //1. 获取员工自己的权限
        doCollectActors(ActorType.User, this, id);
        
        //2. 加载员工的角色
        for(Employee employee : employees) {
            doCollectActors(ActorType.Employee, employee, employee.getId());
        }
        
        //3. 加载岗位权限
        if (positions != null) {
            for (Position position: positions) {
                doCollectActors(ActorType.Position, position, position.getId());
            }
        }
        
        //4. 加载授予给公司的角色(以后这里要去掉,先给公司重的管理员,然后再分配)
        if (org != null) {
            List<OrgAccount> orgAccounts = org.getOrgAccounts();
            
            if (orgAccounts != null) {
                for (OrgAccount orgAccount: orgAccounts) {
                    doCollectActors(ActorType.OrgAccount, orgAccount, orgAccount.getId());
                }
            }
        }
    }
 
    private void doCollectActors(ActorType type, Object sender, String targetId) throws Exception {
        //1. 获取 Actor 配置
        DataObject dataObject = DataObject.getInstance("sys_right_actor_target");
        Filter filter = new Filter("target_id", targetId);
        EntitySet entitySet = dataObject.getTableEntitySet(filter);
        
        if (entitySet.isEmpty()) {
            return;
        }
        
        //2. 加载 Actor
        ActorBucket actorBucket = ActorBucket.getInstance();
        
        for (Entity entity: entitySet) {
            ActorTarget target = new ActorTarget();
            target.load(entity);
            
            String actorId = entity.getString("actor_id");
            Actor actor = actorBucket.get(actorId);
            
            if (actor == null) {
                logger.error("actor not exists: {}", actorId);
                continue;
            }
            
            target.setActor(actor);
            
            if (ActorType.Position == type) {
                Position position = (Position)sender;
                target.setPosition(position);
            }
            else if (ActorType.OrgAccount == type) {
                target.loadAccount();
            }
            
            actors.add(target.getId(), target);
            actorsRuntime.loadOne(target);
        }
    }
 
    public String getPassword() {
        return password;
    }
 
    public synchronized void chagnePassword(String newPass) throws Exception {
        DataObject dataObject = DataObject.getInstance("sys_user");
        
        EntitySaver saver = dataObject.createEntitySaver(id);
        saver.set("password", newPass);
        saver.set("pass_need_change", "F");
        saver.update();                
        
        password = newPass;
        passNeedChange = false;
    }
 
    public void checkBuild() {
        if (built) {
            return;
        }
        
        synchronized (this) {
            if (built) {
                return;
            }
            
            loadHierarchyAndCapacitys();
        }
    }
    
    public boolean isOrgFrozen() {
        if (org == null) {
            return false;
        }
        
        return org.isFrozen();
    }
    
    public boolean isAdmin() {
        return adminSet.contains(name);
    }
    
    public String getId() {
        return id;
    }
 
    public String getName() {
        return name;
    }
 
    public String getRemark() {
        return remark;
    }
 
    @Override
    public String getEmployeeId() {
        Employee employee = getCurrentEmployee();
        
        if (employee != null) {
            return employee.getId();
        }
        
        return null;
    }
    
    @Override
    public String getEmployeeName() {
        Employee employee = getCurrentEmployee();
        if (employee != null) {
            return employee.getName();
        }
        return name;
    }
 
    public String getOrgId() {
        return orgId;
    }
    
    public Org getOrg() {
        return org;
    }
 
    public Sight getCurrentSight(String onlineCode) throws Exception {
        //1. 如果没有计算角色,先计算角色(如果计算不会执行)
        checkBuild();
        
        //2.
        ActorTarget actorTarget = actorsRuntime.getCurrent(onlineCode);
        
        if (actorTarget == null) {
            return null;
        }
        
        return actorTarget.getSightRuntime();
    }
 
    public ActorTarget getCurrentActor(String onlineCode) throws Exception {
        //1. 如果没有计算角色,先计算角色(如果计算不会执行)
        checkBuild();
        
        //2.
        return actorsRuntime.getCurrent(onlineCode);
    }
    
    public ActorTarget changeCurrentActor(String onlineCode, String actorId) throws Exception {
        //1. 如果没有计算角色,先计算角色(如果计算不会执行)
        checkBuild();
        
        //2.
        return actorsRuntime.changeCurrent(onlineCode, actorId);
    }
 
    public Position getPosition(String onlineCode) throws Exception {
        //1. 如果没有计算角色,先计算角色(如果计算不会执行)
        checkBuild();
        
        //2.        
        ActorTarget actorRuntime = actorsRuntime.getCurrent(onlineCode);
        return actorRuntime.getPosition();
    }
    
    public ActorTarget getCapacity(String actorId) {
        return actorsRuntime.get(actorId);
    }
    
    public void saveCapacitys(JSONReader capacitys) {
        actorsRuntime.saveChanges(capacitys);
    }
 
    public ActorsRuntime getActorsRuntime() {
        return actorsRuntime;
    }
 
    @Override
    public boolean isAnymous() {
        return false;
    }
    
    @Override
    public String getProviderName() {
        return ProviderName;
    }
 
    @Override
    public VariantProviderType getProviderType() {
        return VariantProviderType.Transiant;
    }
    
    @Override
    public Object getVariantValue(DataEvent dataEvent, String variantName) {
        if ("user.id".equalsIgnoreCase(variantName)) {
            return id;
        }
        else if ("user.name".equalsIgnoreCase(variantName)) {
            return name;
        }
        else if ("user.employee_id".equalsIgnoreCase(variantName)) {
            Employee employee = getCurrentEmployee();
            
            if (employee != null) {
                return employee.getId();
            }
            
            return id;
        }
        else if ("user.employee_code".equalsIgnoreCase(variantName)) {
            Employee employee = getCurrentEmployee();
            
            if (employee != null) {
                return employee.getCode();
            }
            
            return null;
        }
        else if ("user.employee_name".equalsIgnoreCase(variantName)) {
            Employee employee = getCurrentEmployee();
            
            if (employee != null) {
                return employee.getName();
            }
            
            return name;
        }
        else if ("user.position_id".equalsIgnoreCase(variantName)) {
            Position position = getCurrentPosition();
            
            if (position != null) {
                return position.getId();
            }
            
            return null;
        }
        else if ("user.position_level_field".equalsIgnoreCase(variantName)) {
            Position position = getCurrentPosition();
            
            if (position != null) {
                return position.getLevelField();
            }
            
            return null;
        }
        
        return null;
    }
    
    public Position getCurrentPosition() {
        IOnlineUser onlineUser = IOnlineUser.getInstance();
        ActorTarget actor = actorsRuntime.getCurrent(onlineUser.getCode());
        
        if (actor == null) {
            return null;
        }
        
        Position position = actor.getPosition();
        return position;
    }
 
    public Employee getCurrentEmployee() {
        IOnlineUser onlineUser = IOnlineUser.getInstance();
        ActorTarget actor = actorsRuntime.getCurrent(onlineUser.getCode());
        
        if (actor == null) {
            return null;
        }
        
        Position position = actor.getPosition();
        if (employees.isEmpty()) {
            return null;
        }
        Employee employee = employees.getEmployeeByPositionId(position.getId());
        return employee;
    }
    
    public static EntitySet calculateUser(String actorTypeCode, String actorId, String id) throws Exception {
        ActorType actorType = ActorType.parse(actorTypeCode);
        DataObject dataObject;
        
        if (ActorType.User == actorType) {
            dataObject = DataObject.getInstance("actor_target_user");
            EntitySet entitySet = dataObject.getBrowseEntitySet(new Filter("sys_user.id", id));
            return entitySet;
        }
        else if (ActorType.OrgAccount == actorType) {
            dataObject = DataObject.getInstance("md_org_account_user");
            EntitySet entitySet = dataObject.getBrowseEntitySet(new Filter("md_org_account.id", id));
            return entitySet;
            
        }
        else if (ActorType.Position == actorType) {
            String positionId = id;
            
            if ("Actor-Sales-Leader".equalsIgnoreCase(actorId)) {
                dataObject = DataObject.getInstance("md_position_hierarchy");
                Entity entity = dataObject.getTableEntity(new Filter("position_id", id));
                positionId = entity.getString("level2");
            }
            else if ("Actor-Sales-Direction".equalsIgnoreCase(actorId)) {
                dataObject = DataObject.getInstance("md_position_hierarchy");
                Entity entity = dataObject.getTableEntity(new Filter("position_id", id));
                positionId = entity.getString("level1");
            }
 
            dataObject = DataObject.getInstance("md_position_user");
            EntitySet positionSet = dataObject.getBrowseEntitySet(new Filter("md_position_employee.position_id", positionId));
            return positionSet;
        }
        else if(ActorType.Employee == actorType) {
            // 运营, 商务, 总经理, 财务.....
            dataObject = DataObject.getInstance("actor_target_position");
            EntitySet entitySet = dataObject.getBrowseEntitySet(new Filter("sys_right_actor_target.actor_id", actorId));
            return entitySet;
        }
        
        return null;
    }
 
    @Override
    public void writeJSON(IJSONWriter writer) {
        writer.beginObject();
        writeJSONBody(writer); 
        writer.endObject();
    }
 
    protected void writeJSONBody(IJSONWriter writer) {
        //1. body
        writer.write("id", id);
        writer.write("name", name);
        writer.write("pass_need_change", passNeedChange);
        
        //2. employee
        if (employees != null) {
            employees.writeJSON(writer);
        }
        
        //3. organization
        if (org != null) {
            org.writeJSON(writer);
        }        
        
        //4. capacity
        writer.beginArray("actors");
        
        for (ActorTarget actor: actorsRuntime) {
            actor.writeJSON(writer);
        }
        
        writer.endArray();
    }
    
}