代码优化 EmpUtils 增加 getCompanyCodes、getCompanyParentCodess 方法

This commit is contained in:
thinkgem
2025-02-07 17:11:33 +08:00
parent 477c5dcfbf
commit b1b29715b8

View File

@@ -77,8 +77,7 @@ public class EmpUtils {
*/ */
public static Employee getByUserCode(String userCode){ public static Employee getByUserCode(String userCode){
User user = UserUtils.get(userCode); User user = UserUtils.get(userCode);
Employee employee = get(user); return get(user);
return employee;
} }
/** /**
@@ -87,8 +86,7 @@ public class EmpUtils {
*/ */
public static Employee getByLoginCode(String loginCode){ public static Employee getByLoginCode(String loginCode){
User user = UserUtils.getByLoginCode(loginCode); User user = UserUtils.getByLoginCode(loginCode);
Employee employee = get(user); return get(user);
return employee;
} }
/** /**
@@ -145,8 +143,7 @@ public class EmpUtils {
* @author ThinkGem * @author ThinkGem
*/ */
public static List<Office> getOfficeAllList(){ public static List<Office> getOfficeAllList(){
@SuppressWarnings("unchecked") List<Office> officeList = CorpUtils.getCache(CACHE_OFFICE_ALL_LIST);
List<Office> officeList = (List<Office>)CorpUtils.getCache(CACHE_OFFICE_ALL_LIST);
if (officeList == null){ if (officeList == null){
Office where = new Office(); Office where = new Office();
where.setStatus(Office.STATUS_NORMAL); where.setStatus(Office.STATUS_NORMAL);
@@ -166,7 +163,7 @@ public class EmpUtils {
getEmployeeOfficeList().forEach(e -> { getEmployeeOfficeList().forEach(e -> {
list.add(e.getOfficeCode()); list.add(e.getOfficeCode());
}); });
return list.toArray(new String[list.size()]); return list.toArray(new String[0]);
} }
/** /**
@@ -175,25 +172,25 @@ public class EmpUtils {
*/ */
public static String[] getOfficeCodesAndChildren(){ public static String[] getOfficeCodesAndChildren(){
Set<String> list = SetUtils.newLinkedHashSet(); Set<String> list = SetUtils.newLinkedHashSet();
Set<String> parentCodess = SetUtils.newHashSet(); Set<String> parentCodesSet = SetUtils.newHashSet();
Office currentOffice = getOffice(); Office currentOffice = getOffice();
list.add(currentOffice.getOfficeCode()); list.add(currentOffice.getOfficeCode());
parentCodess.add(currentOffice.getParentCodes() + currentOffice.getOfficeCode() + ","); parentCodesSet.add(currentOffice.getParentCodes() + currentOffice.getOfficeCode() + ",");
// 添加附属机构 // 添加附属机构
getEmployeeOfficeList().forEach(e -> { getEmployeeOfficeList().forEach(e -> {
list.add(e.getOfficeCode()); list.add(e.getOfficeCode());
parentCodess.add(e.getParentCodes() + e.getOfficeCode() + ","); parentCodesSet.add(e.getParentCodes() + e.getOfficeCode() + ",");
}); });
// 查找并添加子机构 // 查找并添加子机构
getOfficeAllList().forEach(e -> { getOfficeAllList().forEach(e -> {
for (String parentCodes : parentCodess) { for (String parentCodes : parentCodesSet) {
if (e.getParentCodes().startsWith(parentCodes)) { if (e.getParentCodes().startsWith(parentCodes)) {
list.add(e.getOfficeCode()); list.add(e.getOfficeCode());
break; break;
} }
} }
}); });
return list.toArray(new String[list.size()]); return list.toArray(new String[0]);
} }
/** /**
@@ -213,16 +210,18 @@ public class EmpUtils {
} }
getEmployeeOfficeList().forEach(e -> { getEmployeeOfficeList().forEach(e -> {
Office office2 = getOffice(e.getOfficeCode()); Office office2 = getOffice(e.getOfficeCode());
if (type.equals(office2.getOfficeType())){ if (office2 != null){
list.add(office2.getOfficeCode()); if (type.equals(office2.getOfficeType())){
}else{ list.add(office2.getOfficeCode());
Office parent2 = office2.getParentByType(type); } else {
if (parent2 != null){ Office parent2 = office2.getParentByType(type);
list.add(parent2.getOfficeCode()); if (parent2 != null){
list.add(parent2.getOfficeCode());
}
} }
} }
}); });
return list.toArray(new String[list.size()]); return list.toArray(new String[0]);
} }
/** /**
@@ -235,7 +234,7 @@ public class EmpUtils {
getEmployeeOfficeList().forEach(e -> { getEmployeeOfficeList().forEach(e -> {
list.add(e.getParentCodes()); list.add(e.getParentCodes());
}); });
return list.toArray(new String[list.size()]); return list.toArray(new String[0]);
} }
/** /**
@@ -290,13 +289,22 @@ public class EmpUtils {
return getEmployee().getCompany(); return getEmployee().getCompany();
} }
/**
* 获取当前员工所有公司编码(数据权限用)
* @author ThinkGem
*/
public static String[] getCompanyCodes(){
List<String> list = ListUtils.newArrayList();
list.add(getCompany().getCompanyCode());
return list.toArray(new String[0]);
}
/** /**
* 获取当前员工所有的公司 * 获取当前员工所有的公司
* @author ThinkGem * @author ThinkGem
*/ */
public static List<Company> getCompanyAllList(){ public static List<Company> getCompanyAllList(){
@SuppressWarnings("unchecked") List<Company> companyList = CorpUtils.getCache(CACHE_COMPANY_ALL_LIST);
List<Company> companyList = (List<Company>)CorpUtils.getCache(CACHE_COMPANY_ALL_LIST);
if (companyList == null){ if (companyList == null){
Company where = new Company(); Company where = new Company();
where.setStatus(Office.STATUS_NORMAL); where.setStatus(Office.STATUS_NORMAL);
@@ -312,20 +320,30 @@ public class EmpUtils {
*/ */
public static String[] getCompanyCodesAndChildren(){ public static String[] getCompanyCodesAndChildren(){
Set<String> list = SetUtils.newLinkedHashSet(); Set<String> list = SetUtils.newLinkedHashSet();
Set<String> parentCodess = SetUtils.newHashSet(); Set<String> parentCodesSet = SetUtils.newHashSet();
Company currentCompany = getCompany(); Company currentCompany = getCompany();
list.add(currentCompany.getCompanyCode()); list.add(currentCompany.getCompanyCode());
parentCodess.add(currentCompany.getParentCodes() + currentCompany.getCompanyCode() + ","); parentCodesSet.add(currentCompany.getParentCodes() + currentCompany.getCompanyCode() + ",");
// 查找并添加子公司 // 查找并添加子公司
getCompanyAllList().forEach(e -> { getCompanyAllList().forEach(e -> {
for (String parentCodes : parentCodess) { for (String parentCodes : parentCodesSet) {
if (e.getParentCodes().startsWith(parentCodes)) { if (e.getParentCodes().startsWith(parentCodes)) {
list.add(e.getCompanyCode()); list.add(e.getCompanyCode());
break; break;
} }
} }
}); });
return list.toArray(new String[list.size()]); return list.toArray(new String[0]);
}
/**
* 获取当前员工所有上级公司编码(数据权限用)
* @author ThinkGem
*/
public static String[] getCompanyParentCodess(){
List<String> list = ListUtils.newArrayList();
list.add(getCompany().getParentCodes());
return list.toArray(new String[0]);
} }
/** /**