zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package com.changhong.epc.rely.api.tool;
 
import java.util.ArrayList;
import java.util.List;
 
import com.changhong.epc.rely.api.bean.Organization;
import com.changhong.epc.rely.api.bean.Position;
import com.changhong.epc.rely.api.bean.User;
import com.changhong.epc.rely.api.bean.WFParticipantEntity;
import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
 
/**
 * 格式化组织机构数据工具
 * @author liush
 *
 */
public class OrganizationFormat {
    
    /**
     * 组织机构接口返回格式
     * 整理成
     * 普元参与者格式
     * @param organization
     * @param participantList
     */
    public static void format(List<Organization> organizationList, List<WFParticipantEntity> participantList){
        if(ObjectUtil.empty(organizationList) || participantList == null) { return; }
        organizationList.stream().forEach(org->{
            WFParticipantEntity orgP = getOrg(org);
            /* 添加部门 */
            setChildOrg(orgP, org.getChildren());
            /* 添加角色 */
            setChildJob(orgP, org.getPositions());
            /* 添加员工 */
            participantList.add(orgP);
        });
    }
    
    /**
     * 获取组织机构下所有用户
     * @param organizationList
     * @param users
     */
    public static void getAllUsers(List<Organization> organizationList, List<WFParticipantEntity> users){
        if(ObjectUtil.empty(organizationList) || users == null) { return; }
        organizationList.stream().forEach(org->{
            /* 添加部门 */
            getAllUsers(org.getChildren(), users);
            //获取所有用户
            setChildUser(users, org.getUsers());
            
        });
    }
    
    /**
     * 获取组织机构下所有机构
     * @param organizationList
     * @param orgs
     */
    public static void getAllOrgs(List<Organization> organizationList, List<WFParticipantEntity> orgs) {
        if(ObjectUtil.empty(organizationList) || orgs == null) { return; }
        organizationList.stream().forEach(org->{
            getAllOrgs(org.getChildren(), orgs);
            setChildOrg(orgs, org.getChildren());
        });
    }
    
    
    /**
     * 添加子角色
     * @param orgP
     * @param positions
     */
    public static void setChildJob(WFParticipantEntity orgP, List<Position> positions) {
        if(ObjectUtil.empty(positions)) { return; }
        positions.stream().forEach(job->{
            WFParticipantEntity jobP = getJob(job,orgP);
            /* 添加员工 */
            setChildUser(jobP, job.getUsers());
            /* 添加到机构中 */
            getChildParticipant(orgP).add(jobP);
        });
    }
 
    /**
     * 添加子员工
     * @param orgP
     * @param users
     */
    public static void setChildUser(WFParticipantEntity orgP, List<User> users) {
        if(ObjectUtil.empty(users)) { return; }
        /* 添加到机构中 */
        users.stream().forEach(user->getChildParticipant(orgP).add(getUser(user,orgP)));
    }
    
    public static void setChildUser(List<WFParticipantEntity> users, List<User> users2) {
        if(ObjectUtil.empty(users2)) { return; }
        /* 添加到机构中 */
        users2.stream().forEach(user->users.add(getUser(user, null)));
    }
    
    /**
     * 添加子部门
     * @param orgP
     * @param organizationList
     */
    public static void setChildOrg(WFParticipantEntity orgP, List<Organization> organizationList){
        List<WFParticipantEntity> childOrgP = new ArrayList<>();
        format(organizationList, childOrgP);
        /* 添加到机构中 */
        getChildParticipant(orgP).addAll(childOrgP);
    }
    
    public static void setChildOrg(List<WFParticipantEntity> orgs, List<Organization> organizationList){
        if(ObjectUtil.empty(organizationList)) { return; }
        /* 添加到机构中 */
        organizationList.stream().forEach(org->orgs.add(getOrg(org)));
    }
    
    /**
     * 获得组织机构参与者
     * @param organization
     * @return
     */
    public static WFParticipantEntity getOrg(Organization organization){
        return new WFParticipantEntity(organization.getId(), "org",organization.getId(), organization.getName(),organization.getParentId(), organization.getCode(), null);
    }
    
    
    public static WFParticipantEntity getOrg(Organization organization, WFParticipantEntity orgP){
        if(ObjectUtil.empty(orgP)){
            return new WFParticipantEntity(organization.getId(), "org",organization.getId(), organization.getName(),organization.getParentId(), organization.getCode(), null);
        }
        return new WFParticipantEntity(organization.getId(), "org", organization.getId(), organization.getName(), organization.getParentId(), organization.getCode(), orgP.getParentCode());
    }
    
    /**
     * 获得员工参与者
     * @param user
     * @return
     */
    public static WFParticipantEntity getUser(User user, WFParticipantEntity orgP){
        if(ObjectUtil.empty(orgP)){
            return new WFParticipantEntity(user.getId(), "emp", user.getId(), user.getUserName(), user.getEmail(), user.getPhone(), user.getOpenId(), null);
        }
        return new WFParticipantEntity(user.getId(), "emp", user.getId(), user.getUserName(), user.getEmail(), user.getPhone(), user.getOpenId(), orgP.getId());
    }
    
    /**
     * 获得角色参与者
     * @param position
     * @return
     */
    public static WFParticipantEntity getJob(Position position, WFParticipantEntity orgP){
        return new WFParticipantEntity(position.getId(), "role", position.getId(), position.getName(),orgP.getId(), orgP.getCode(), null);
    }
    
    /**
     * 获得子参与者
     * @param participant
     * @return
     */
    private static List<WFParticipantEntity> getChildParticipant(WFParticipantEntity participant){
        if(participant.getChildren() == null){
            participant.setChildren(new ArrayList<>());
        }
        return participant.getChildren();
    }
    
    
    /**
     * 获取父机构code)
     * @param organizationList
     * @param orgs
     */
    public static void getParentCode(List<Organization> organizationList, List<WFParticipantEntity> orgs) {
        if(ObjectUtil.empty(organizationList) || orgs == null) { return; }
        organizationList.stream().forEach(org->{
            getParentCode(org.getChildren(), orgs);
            orgs.stream().forEach(o->{
                getParentCodeById(o, org);
            });
        });
    }
    
    /**
     * 获取父机构code(依据当前的parentId)
     * @param participant
     * @param org
     */
    private static void getParentCodeById(WFParticipantEntity participant, Organization org){
        if(participant.getParentId().equals(org.getId())){
            participant.setParentCode(org.getCode());
        } else {
            return;
        }
    }
    
    
}