1821349743@qq.com
2023-02-20 93dd9bc3f16b0f626761ec624f2dc78037568897
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
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;
    }
 
 
}