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
package foundation.org;
 
import foundation.dao.Filter;
import foundation.data.entity.Entity;
import foundation.data.entity.EntitySet;
import foundation.data.object.DataObject;
import foundation.json.IJSONProvider;
import foundation.json.IJSONWriter;
import foundation.util.MapList;
import foundation.util.Util;
 
public class Employees extends MapList<String, Employee> implements IJSONProvider{
    private Employee currentEmployee;
    private MapList<String, Position> positions; 
 
    public Employees() {
        positions = new MapList<String, Position>();
    }
 
    public static Employees getInstance(String userId) throws Exception {
        Employees employees = new Employees();
        DataObject dataObject = DataObject.getInstance("sys_user_employee");
        EntitySet employeeSet = dataObject.getTableEntitySet(new Filter("user_id", userId));
        
        for (Entity entity: employeeSet) {
            Employee employee = Employee.getInstance(entity.getString("employee_id"));
 
            // 加载员工关联的岗位
            if (employee != null) {
                MapList<String, Position> employeePositions = employee.getPositions();
                for (Position position : employeePositions) {
                    employees.loadOnePosition(position.getId(), position);
                }
            }
            
            employees.add(employee.getId(), employee);
        }
        
        if(employees.size() > 0 ) {
            employees.setCurrentEmployee(employees.get(0));
        }
        
        return employees;
    }
    
    private void loadOnePosition(String positionId, Position position) {
        positions.add(positionId, position);
    }
 
    public String getId() {
        return currentEmployee.getId();
    }
    
    public String getName(String employeeId) {
        if (Util.isEmpty(employeeId)) {
            currentEmployee.getName();
        }
        Employee employee = this.get("employeeId");
        return employee.getName();
    }
 
    public Employee getEmployeeByPositionId(String positionId) {
        for(Employee employee : this) {
            MapList<String, Position> positions = employee.getPositions();
            
            if (positions != null && positions.contains(positionId)) {
                return employee;
            }
        }
        return null;
    }
    
    public void setCurrentEmployee(Employee currentEmployee) {
        this.currentEmployee = currentEmployee;
    }
 
    public MapList<String, Position> getPositions() {
        return positions;
    }
 
    public void writeJSON(IJSONWriter writer) {
        writer.beginArray("employees");
        
        for(Employee employee : this) {
            writer.beginObject();
            employee.writeJSONBody(writer);
            writer.endObject();
        }
        
        writer.endArray();
    }
 
 
}