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 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");
|
}
|
|
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 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();
|
}
|
|
}
|