package com.product.data.utli;
|
|
import com.product.admin.service.OrganizationCacheService;
|
import com.product.core.cache.DataPoolCacheImpl;
|
import com.product.core.dao.BaseDao;
|
import com.product.core.entity.DataTableEntity;
|
import com.product.core.entity.FieldSetEntity;
|
import com.product.data.config.CmnConst;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
|
public class OrgDataMap {
|
|
private Map<String, FieldSetEntity> companyMap = new HashMap<>();
|
|
private Map<String, FieldSetEntity> deptMap = new HashMap<>();
|
|
private Map<String, String> staffDeptMap = new HashMap<>();
|
|
private Map<String, String> staffCompanyMap = new HashMap<>();
|
|
private void readRecord(DataTableEntity dt, Map<String, FieldSetEntity> map) {
|
if (DataTableEntity.isEmpty(dt) || map == null) return;
|
for (int i = 0; i < dt.getRows(); i++) {
|
FieldSetEntity fs = dt.getFieldSetEntity(i);
|
String code = fs.getString(CmnConst.ORG_LEVEL_CODE);
|
map.put(code, fs.clones());
|
}
|
}
|
|
public OrgDataMap(BaseDao baseDao) {
|
//初始化组织机构表数据
|
DataPoolCacheImpl dataPoolCache = DataPoolCacheImpl.getInstance();
|
// DataTableEntity companyDt = dataPoolCache.getCacheData("所有公司信息");
|
DataTableEntity companyDt = OrganizationCacheService.getOrgData("1");
|
readRecord(companyDt, this.companyMap);
|
// DataTableEntity deptDt = dataPoolCache.getCacheData("公司-部门");
|
DataTableEntity deptDt = OrganizationCacheService.getOrgData("2");
|
readRecord(deptDt, this.deptMap);
|
DataTableEntity dt = baseDao.listTable("SELECT user_id,l.org_level_code dept_code,l1.org_level_code company_code FROM `product_sys_staffs` s join product_sys_org_levels l on s.dept_uuid=l.uuid \n" +
|
"join product_sys_org_levels l1 on s.org_level_uuid=l1.uuid ", new Object[]{});
|
if (!DataTableEntity.isEmpty(dt)) {
|
for (int i = 0; i < dt.getRows(); i++) {
|
this.staffDeptMap.put(dt.getString(i, "user_id"), dt.getString(i, "dept_code"));
|
this.staffCompanyMap.put(dt.getString(i, "user_id"), dt.getString(i, "company_code"));
|
}
|
}
|
}
|
|
/**
|
* 根据userid找部门code
|
*
|
* @param userId
|
* @return
|
*/
|
public String getDeptCodeByUser(String userId) {
|
return this.staffDeptMap.get(userId);
|
}
|
|
/**
|
* 根据userid找部门code
|
*
|
* @param userId
|
* @return
|
*/
|
public String getCompanyCodeByUser(String userId) {
|
return this.staffCompanyMap.get(userId);
|
}
|
|
/**
|
* 获取公司编码根据部门编码
|
*
|
* @param deptCode
|
* @return
|
*/
|
public String getCompanyCode(String deptCode) {
|
if (isExist(deptCode, this.deptMap)) {
|
FieldSetEntity fs = this.deptMap.get(deptCode);
|
String parentCode = fs.getString("org_level_code_parent");
|
String[] code = parentCode.split("-");
|
for (int i = 0; i < code.length; i++) {
|
parentCode = "";
|
for (int j = 0; j < code.length - i; j++) {
|
parentCode = (j > 0 ? parentCode + "-" : parentCode) + code[j];
|
}
|
if (isExist(parentCode, this.companyMap)) {
|
return this.companyMap.get(parentCode).getString(CmnConst.ORG_LEVEL_CODE);
|
}
|
}
|
}
|
return null;
|
}
|
|
|
/**
|
* 判断是否为部门
|
*
|
* @param code
|
* @return
|
*/
|
public boolean isDept(String code) {
|
return isExist(code, this.deptMap);
|
}
|
|
/**
|
* 判断是否为公司
|
*
|
* @param code
|
* @return
|
*/
|
public boolean isCompany(String code) {
|
return isExist(code, this.companyMap);
|
}
|
|
/**
|
* 是否存在
|
*
|
* @param code 键
|
* @param map 是否存在此map
|
* @return
|
*/
|
private boolean isExist(String code, Map<String, FieldSetEntity> map) {
|
return code != null ? map.get(code) != null : false;
|
}
|
|
|
}
|