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 implements IJSONProvider{ private Employee currentEmployee; private MapList positions; public Employees() { positions = new MapList(); } 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 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 positions = employee.getPositions(); if (positions != null && positions.contains(positionId)) { return employee; } } return null; } public void setCurrentEmployee(Employee currentEmployee) { this.currentEmployee = currentEmployee; } public MapList getPositions() { return positions; } public void writeJSON(IJSONWriter writer) { writer.beginArray("employees"); for(Employee employee : this) { writer.beginObject(); employee.writeJSONBody(writer); writer.endObject(); } writer.endArray(); } }