黄潞潞
2024-06-07 482f807361c9bc0dce2db949a29c755cf858548b
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
package foundation.org;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
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.persist.NamedSQL;
import foundation.persist.SQLRunner;
import foundation.util.MapList;
import foundation.util.Util;
 
public class Employee implements IJSONProvider {
 
    protected static Logger logger;
    private String id;
    private String code;
    private String name;
    private String phone;
    private String email;
    private Org org;
    private MapList<String, Position> positions;
    
    static {
        logger = LogManager.getLogger(Employee.class); 
    }
    
    private Employee() {
        positions = new MapList<String, Position>();
    }
 
    public static Employee getInstance(String employeeId) throws Exception {
        if (Util.isEmpty(employeeId)) {
            return null;
        }
        
        //1. 加载员工本身
        DataObject dataObject = DataObject.getInstance("md_employee");
        Entity entity = dataObject.getTableEntity(employeeId);
        
        if (entity == null) {
            logger.error("员工不存在:{}", employeeId);
            return null;
        }
        
        Employee employee = new Employee();
        employee.load(entity);
        
        //2. 加载员工关联的岗位
        NamedSQL namedSQL = NamedSQL.getInstance("getEmployeePositions");
        namedSQL.setParam("employeeId", employeeId);
        EntitySet entitySet = SQLRunner.getEntitySet(namedSQL);
        
        for (Entity ponsitionEntity: entitySet) {
            Position position = new Position();
            position.load(ponsitionEntity);
            employee.loadOnePosition(position.getId(), position);
        }
        
        return employee;
    }
    
    public void load(Entity entity) throws Exception {
        id = entity.getString("id");
        code = entity.getString("code");
        name = entity.getString("name");
        phone = entity.getString("phone");
        email = entity.getString("email");
    }
 
    private void loadOnePosition(String positionId, Position position) {
        if (positions.contains(positionId)) {
            return;
        }
        
        positions.add(positionId, position);
    }
    
    public String getId() {
        return id;
    }
 
    public String getCode() {
        return code;
    }
 
    public String getName() {
        return name;
    }
    
    public String getPhone() {
        return phone;
    }
 
    public String getEmail() {
        return email;
    }
 
    public void setOrg(Org org) {
        this.org = org;
    }
    
    public Org getOrg() {
        return org;
    }
 
    public MapList<String, Position> getPositions() {
        return positions;
    }
 
    @Override
    public void writeJSON(IJSONWriter writer) {
        writer.beginObject("employee");
        writeJSONBody(writer);
        writer.endObject();
    }
 
    public void writeJSONBody(IJSONWriter writer) {
        //1. body
        writer.write("id", id);
        writer.write("code", code);
        writer.write("name", name);
        
        //2. positions
        writer.beginArray("positions");
        
        for (Position position: positions) {
            position.writeJSON(writer);
        }
        
        writer.endArray();
    }
 
}