许鹏程
2023-05-25 213cc37cbf0b2515a4de56cc1e01813211bad183
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package com.product.org.admin.service;
 
 
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.alibaba.druid.util.StringUtils;
import com.product.admin.service.CodeService;
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.permission.PermissionService;
import com.product.core.service.support.AbstractBaseService;
import com.product.core.service.support.QueryFilterService;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.core.transfer.Transactional;
import com.product.org.admin.config.CmnCode;
import com.product.org.admin.config.CmnConst;
import com.product.org.admin.config.SystemCode;
import com.product.org.admin.service.idel.IDeptChangeService;
import com.product.util.BaseUtil;
 
/**
 * 
 * Copyright LX-BASE
 * 
 * @Title: LX-BASE-
 * @Project: DeptChangesService
 * @Date: 2020-06-04 20:07
 * @Author: 杜洪波
 * @Description: 部门变更
 */
@Component("deptChange")
public class DeptChangeService extends AbstractBaseService implements IDeptChangeService{
 
    @Autowired
    private BaseDao baseDao;
    
    @Autowired
    CodeService codeService;
 
    @Autowired
    PermissionService permissionService;
    
    @Autowired
    DeptChangeService deptChangeService;
    
    @Autowired
    public QueryFilterService queryFilterService;
    
    /**
     * 部门变更列表
     * 
     * @param fse
     * @return
     * @throws BaseException
     */
    public DataTableEntity listDeptChange(FieldSetEntity fse) throws BaseException {
        //郑盟   2020年12月22日   下午18:34
        String dataFilter=permissionService.getDataFilter("org_level_uuid,new_org_level_uuid") ;
        String queryFilter="";
        if (!BaseUtil.dataTableIsEmpty(fse.getSubDataTable("systemSeniorQueryString"))) {
            if (!StringUtils.isEmpty(dataFilter)) {
                queryFilter =" and"+queryFilterService.getQueryFilter(fse);
            }else {
                queryFilter =queryFilterService.getQueryFilter(fse);
            }
        } 
        return baseDao.listTable("product_sys_org_levels_change",dataFilter+queryFilter ,
                new Object[] {},new Object[] {}, null,fse.getInteger("pagesize"), fse.getInteger("cpage"));
    }
 
    /**
     * 部门变更详情
     * 
     * @param uuid
     * @return
     * @throws BaseException
     */
    public FieldSetEntity findDeptChange(String uuid) throws BaseException {
        return baseDao.getFieldSetEntity("product_sys_org_levels_change", uuid, false);
    }
 
    /**
     * 部门变更新增验证
     * 
     * @param deptUUid
     * @return
     * @throws BaseException
     */
    public String deptChangeAdd(FieldSetEntity fseChangeInfo) throws BaseException {
        DataTableEntity dtDeptChange = baseDao.listTable(CmnConst.PRODUCT_SYS_ORG_LEVELS_CHANGE, "origin_dept_uuid=? and (result_status is null or result_status=1)", new Object[] { fseChangeInfo.getString("origin_dept_uuid") });
        if (dtDeptChange != null && dtDeptChange.getRows() > 0) {
            throw new BaseException(SystemCode.SYSTEM_CURRENT_DEPT_CHANGE_EXIST.getValue(), SystemCode.SYSTEM_CURRENT_DEPT_CHANGE_EXIST.getText(), this.getClass(), "deptChangeAddValitation");
        }
        
        //设置创建人和创建时间
        fseChangeInfo.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());
        fseChangeInfo.setValue(CmnConst.CREATED_UTC_DATETIME, new Date());
        fseChangeInfo.setValue("result_status", 0);
        
        boolean isMatch=true;
        
        //获取部门变更类型
        String deptChangeType = fseChangeInfo.getString("change_status_uuid");
        
        IDeptChangeService service =(IDeptChangeService)getProxyInstance(deptChangeService);
        if ("2".equals(deptChangeType)) {    //合并部门
            isMatch = service.deptMerge(fseChangeInfo);
        } else if ("3".equals(deptChangeType)) {    //迁移部门
            isMatch = service.deptMove(fseChangeInfo);
        }
        
        if (isMatch) {
            //新增记录
            return baseDao.add(fseChangeInfo);
        }else {
            throw new BaseException(CmnCode.DEPT_CHANGE_ERROR.getValue(), CmnCode.DEPT_CHANGE_ERROR.getText());
        }
    }
    
    
    /**
     *     部门合并
     *  1:变更后代部门全称  
     *  2:变更后代部门组织机构编码  
     *  3:变更原部门岗位至合并部门  
     *  4:变更原部门员工至合并部门  
     *  5:重新生成相关部门下员工的员工编码
     *     6.将原部门标记为被合并
     *     7.将变更标识为1
     * @param fse
     * @return
     * @throws BaseException
     */
    @Transactional
    public boolean deptMerge(FieldSetEntity fse) throws BaseException {
        
        boolean flag = true;
        
        String originOrgUuid = fse.getString(CmnConst.ORG_LEVEL_UUID);        //合并部门所属公司UUID
        String mergeOrgUuid = fse.getString("new_org_level_uuid");    //保留部门所属公司UUID
        String originDeptUuid = fse.getString(CmnConst.ORIGIN_DEPT_UUID);    //获取原部门UUID
        String mergeDeptUuid = fse.getString("new_dept_uuid");    //获取保留部门UUID
        
        //获取原部门信息
        FieldSetEntity fseOriginDept = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS, originDeptUuid, false);
        //获取保留部门信息
        FieldSetEntity fseMergeDept = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS, mergeDeptUuid, false);
        if (fseMergeDept == null || fseOriginDept==null) {
            throw new BaseException(SystemCode.SYSTEM_DEPT_MERGE_NO_EXIST.getValue(), SystemCode.SYSTEM_DEPT_MERGE_NO_EXIST.getText());
        }
 
        String originDeptCode = fseOriginDept.getString(CmnConst.ORG_LEVEL_CODE);    //获取原部门编码
        String mergeDeptCode = fseMergeDept.getString(CmnConst.ORG_LEVEL_CODE);        //获取保留部门编码
        
        //获取子一代部门信息
        DataTableEntity dtDepts=baseDao.listTable(CmnConst.PRODUCT_SYS_ORG_LEVELS, "org_level_code_parent =?", new Object[]{originDeptCode});
        
        //判断是否包含子一代部门变化
        if (!BaseUtil.dataTableIsEmpty(dtDepts)) {    
            Object[] uuids = dtDepts.getUuids();
            if (uuids != null) {
                
                //生成的第一个编码,后续编码需要根据该编码生成
                String finalCode="";
                
                //遍历子部门,变更上下级编码
                for (int i = 0; i < dtDepts.getRows(); i++) {
                    FieldSetEntity fseSubDept=dtDepts.getFieldSetEntity(i);
                    // 获取子部门合并前的编码
                    String originDeptSubCode = fseSubDept.getString(CmnConst.ORG_LEVEL_CODE);
                    // 创建子部门新编码
                    String newDeptSubCode="";
                    if(i==0) {    //生成code管理器编码
                        newDeptSubCode = codeService.createCode(CmnConst.PRODUCT_SYS_ORG_LEVELS, CmnConst.ORG_LEVEL_CODE, mergeDeptCode);
                        finalCode=newDeptSubCode;
                    }else {    //自动生成后续编码
                        newDeptSubCode = numCodeparse(finalCode,i);
                    }
                    
                    fseSubDept.setValue(CmnConst.ORG_LEVEL_CODE, newDeptSubCode);
                    fseSubDept.setValue(CmnConst.ORG_LEVEL_CODE_PARENT, mergeDeptCode);
                    baseDao.update(fseSubDept);
                    
                    //变更该部门后代部门 编码与上级编码
                    StringBuilder sqlSubDeptChangeCode=new StringBuilder();
                    sqlSubDeptChangeCode.append(" update product_sys_org_levels ");
                    sqlSubDeptChangeCode.append(" set org_level_code=concat(replace(substring(org_level_code,1,locate(?,org_level_code)+length(?)),?,?),substring(org_level_code,locate(?,org_level_code)+length(?)+1)), ");
                    sqlSubDeptChangeCode.append(" org_level_code_parent=concat(replace(substring(org_level_code_parent,1,locate(?,org_level_code_parent)+length(?)),?,?),substring(org_level_code_parent,locate(?,org_level_code_parent)+length(?)+1)) ");
                    sqlSubDeptChangeCode.append(" where org_level_code like ? ");
                    baseDao.executeUpdate(sqlSubDeptChangeCode.toString(), new Object[] {originDeptSubCode,originDeptSubCode,originDeptSubCode,newDeptSubCode,originDeptSubCode,originDeptSubCode,
                            originDeptSubCode,originDeptSubCode,originDeptSubCode,newDeptSubCode,originDeptSubCode,originDeptSubCode,originDeptSubCode+"-%"});
                    
                    //变更该部门及后代部门所有员工 编码与上级领导编码
                    StringBuilder sqlStaffChangeCode=new StringBuilder();
                    sqlStaffChangeCode.append(" update product_sys_staffs ");
                    sqlStaffChangeCode.append(" set tricode=concat(replace(substring(tricode,1,locate(?,tricode)+length(?)),?,?),substring(tricode,locate(?,tricode)+length(?)+1)), ");
                    sqlStaffChangeCode.append(" direct_leader_code=concat(replace(substring(direct_leader_code,1,locate(?,direct_leader_code)+length(?)),?,?),substring(direct_leader_code,locate(?,direct_leader_code)+length(?)+1)) ");
                    sqlStaffChangeCode.append(" where tricode like ? ");
                    
                    flag=baseDao.executeUpdate(sqlStaffChangeCode.toString(), new Object[] {originDeptSubCode,originDeptSubCode,originDeptSubCode,newDeptSubCode,originDeptSubCode,originDeptSubCode,
                            originDeptSubCode,originDeptSubCode,originDeptSubCode,newDeptSubCode,originDeptSubCode,originDeptSubCode,originDeptSubCode+"-%"});
 
                }
            }
            
        }
        
        
        //获取被合并部门的员工信息
        DataTableEntity dtStaffs=baseDao.listTable(CmnConst.PRODUCT_SYS_STAFFS, "dept_uuid = ?", new Object[]{originDeptUuid});
        if (!BaseUtil.dataTableIsEmpty(dtStaffs)) {
            
            //员工编码变更存储对象
            Map<String, String> changeStaffCode=new HashMap<>();
            
            //生成一个固定编码
            String finalCode="";
            
            //遍历变化的员工
            for (int i = 0; i < dtStaffs.getRows(); i++) {
                FieldSetEntity fseStaff=dtStaffs.getFieldSetEntity(i);
                fseStaff.remove("show_name");
                fseStaff.remove("family_name");
                fseStaff.remove("given_name");
                fseStaff.remove("preferred_name");
                
                String newStaffTricode="";
                if(i==0) {
                    newStaffTricode = codeService.createCode(CmnConst.PRODUCT_SYS_STAFFS, CmnConst.TRICODE, mergeDeptCode);
                    finalCode=newStaffTricode;
                }else {
                    newStaffTricode = numCodeparse(finalCode,i);
                }
                
                String originStaffTricode=fseStaff.getString(CmnConst.TRICODE);
                
                fseStaff.setValue(CmnConst.TRICODE, newStaffTricode);
                
                changeStaffCode.put(originStaffTricode, newStaffTricode);
            }
            baseDao.update(dtStaffs);
            
            //修改影响到的其余下属员工
            for ( Map.Entry<String, String> entries : changeStaffCode.entrySet() ){
                String originStaffTriCode=entries.getKey();    //原部门编码
                String newStaffTriCode=entries.getValue();    //新部门编码
                    
                StringBuilder sqlStaffChangeCode=new StringBuilder();
                sqlStaffChangeCode.append(" update product_sys_staffs set ");
                sqlStaffChangeCode.append(" direct_leader_code=concat(replace(substring(direct_leader_code,1,locate(?,direct_leader_code)+length(?)),?,?),substring(direct_leader_code,locate(?,direct_leader_code)+length(?)+1)) ");
                sqlStaffChangeCode.append(" where direct_leader_code like ? ");
                    
                flag=baseDao.executeUpdate(sqlStaffChangeCode.toString(), new Object[] {originStaffTriCode,originStaffTriCode,originStaffTriCode,newStaffTriCode,originStaffTriCode,originStaffTriCode,originStaffTriCode+"%"});
            }
        }
        
        
 
        //变更合并部门的岗位所属部门至保留部门
        if (!originOrgUuid.equals(mergeOrgUuid)) {
            baseDao.executeUpdate("update product_sys_job_posts set org_level_uuid=?,dept_uuid=? where dept_uuid=?", new Object[] {mergeOrgUuid,mergeDeptUuid,originOrgUuid});
        }
        baseDao.executeUpdate("update product_sys_job_posts set dept_uuid=? where dept_uuid=?", new Object[] {mergeDeptUuid,originDeptUuid});
        
        //变更合并部门的人员所属部门至保留部门
        if (!originOrgUuid.equals(mergeOrgUuid)) {
            baseDao.executeUpdate("update product_sys_staffs set org_level_uuid=?,dept_uuid=? where dept_uuid=?", new Object[] {mergeOrgUuid,mergeDeptUuid,originOrgUuid});
        }
        baseDao.executeUpdate("update product_sys_staffs set dept_uuid=? where dept_uuid=?", new Object[] {mergeDeptUuid,originDeptUuid});
        
        //变更当前部门状态为被合并
        if (flag == true) {
            fseOriginDept.setValue("org_level_status", 2);
            flag = baseDao.update(fseOriginDept);
        }
        fse.setValue(CmnConst.RESULT_STATUS, 1);
        
        return flag;
    }
    
    /**
     *     手动处理编码  (循环产生编码拓展)
     * @param finalCode
     * @param i
     * @return
     */
    public String numCodeparse(String finalCode,int i) {
        
        //获取编码前缀和后缀
        String prefix=finalCode.substring(0, finalCode.lastIndexOf("-"));
        String suffix=finalCode.substring(finalCode.lastIndexOf("-")+1, finalCode.length());
        
        //通过后缀生成新编码
        int newSuffixNum=Integer.parseInt(suffix);
        newSuffixNum+=i;
        
        String newCode="";
        //生成新的编码
        if (newSuffixNum<10) {
            newCode=prefix+"-00"+newSuffixNum;
        }else if (newSuffixNum>9 && newSuffixNum<100) {
            newCode=prefix+"-0"+newSuffixNum;
        }else {
            newCode=prefix+"-"+newSuffixNum;
        }
        return newCode;
    }
 
    /**
     *     部门迁移
     *     1.变更原部门及后代部门国际化全称
     *     2.判断是否跨公司迁移(变更岗位和员工所属公司UUID)
     *     3.变更原部门及后代部门的组织机构编码及父编码
     *     4.变更原部门及后代部门下的员工编码
     *     5.将变更标识为1
     * @param fse
     * @param originDeptCode 原部门编码
     * @param originDeptUuid 原部门uuid
     * @return
     * @throws BaseException
     */
    @Transactional
    public boolean deptMove(FieldSetEntity fse) throws BaseException {
        boolean flag = true;
        
        String originDeptUuid=fse.getString(CmnConst.ORIGIN_DEPT_UUID);    //获取原部门UUID
        String moveDeptUuid = fse.getString("new_dept_uuid");        //获取迁入部门uuid
        String originOrgUuid=fse.getString(CmnConst.ORG_LEVEL_UUID);        //原部门所属公司
        String moveOrgUuid=fse.getString("new_org_level_uuid");    //迁入部门所属公司
        if (StringUtils.isEmpty(moveDeptUuid)) {    //杜洪波 updateTime 2020-12-17 09:48:00
            moveDeptUuid=moveOrgUuid;
        }
        
        //获取原部门国际化信息
        FieldSetEntity fseOriginDept=baseDao.listInternationDataTable(baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS, originDeptUuid, false),null);
        
        //获取迁入部门国际化信息
        FieldSetEntity fseMoveDept = baseDao.listInternationDataTable(baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ORG_LEVELS, moveDeptUuid, false),null);
        if (fseMoveDept == null || fseOriginDept==null) {
            return false;
        }
 
        String originDeptCode = fseOriginDept.getString(CmnConst.ORG_LEVEL_CODE);    //获取原部门编码
        String originDeptParentCode = fseOriginDept.getString(CmnConst.ORG_LEVEL_CODE_PARENT);    //获取原部门父级编码
        String moveDeptCode = fseMoveDept.getString(CmnConst.ORG_LEVEL_CODE);    //获取迁入部门编码
        
        //获取原部门上级国际化信息
        FieldSetEntity fseOriginDeptParent = baseDao.listInternationDataTable(baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_ORG_LEVELS, "org_level_code=?", new Object[] {originDeptParentCode}, false),null);
        if (fseOriginDeptParent==null) {
            return false;
        }
        
                
        //获取所有后代部门信息
        DataTableEntity dtDepts=baseDao.listTable(CmnConst.PRODUCT_SYS_ORG_LEVELS, "org_level_code like'"+originDeptCode+"%'", new Object[]{});
        if (dtDepts!=null && dtDepts.getRows()>0) {
            Object[] uuids = dtDepts.getUuids();
            if (uuids != null) {
                
                //判断迁移前后公司是否修改,如已修改,则变更员工所属公司,岗位所属公司
                if (!originOrgUuid.equals(moveOrgUuid)) {
                    baseDao.executeUpdate("update product_sys_staffs set org_level_uuid=? where " +  BaseUtil.buildQuestionMarkFilter(CmnConst.DATA_UUID, uuids.length, true), new Object[] {moveOrgUuid,Arrays.asList(uuids)});
                    baseDao.executeUpdate("update product_sys_job_posts set org_level_uuid=? where " +  BaseUtil.buildQuestionMarkFilter(CmnConst.DATA_UUID, uuids.length, true), new Object[] {moveOrgUuid,Arrays.asList(uuids)});
                }
            }
        }
        
        //生成新部门编码
        String newMoveCode = codeService.createCode(CmnConst.PRODUCT_SYS_ORG_LEVELS, CmnConst.ORG_LEVEL_CODE, moveDeptCode);
        fseOriginDept.setValue(CmnConst.ORG_LEVEL_CODE, newMoveCode);
        fseOriginDept.setValue(CmnConst.ORG_LEVEL_CODE_PARENT, moveDeptCode);
        baseDao.update(fseOriginDept);
        
        //变更迁移部门及后代部门组织机构编码及父编码
        StringBuilder sqlSubDeptChangeCode=new StringBuilder();
        sqlSubDeptChangeCode.append(" update product_sys_org_levels ");
        sqlSubDeptChangeCode.append(" set org_level_code=concat(replace(substring(org_level_code,1,locate(?,org_level_code)+length(?)),?,?),substring(org_level_code,locate(?,org_level_code)+length(?)+1)), ");
        sqlSubDeptChangeCode.append(" org_level_code_parent=concat(replace(substring(org_level_code_parent,1,locate(?,org_level_code_parent)+length(?)),?,?),substring(org_level_code_parent,locate(?,org_level_code_parent)+length(?)+1)) ");
        sqlSubDeptChangeCode.append(" where org_level_code like ? ");
        
        baseDao.executeUpdate(sqlSubDeptChangeCode.toString(), new Object[] {originDeptCode,originDeptCode,originDeptCode,newMoveCode,originDeptCode,originDeptCode,
                originDeptCode,originDeptCode,originDeptCode,newMoveCode,originDeptCode,originDeptCode,originDeptCode+"-%"});
        
        //变更迁移部门及后代部门的员工编码和上级领导编码
        StringBuilder sqlStaffChangeCode=new StringBuilder();
        sqlStaffChangeCode.append(" update product_sys_staffs ");
        sqlStaffChangeCode.append(" set tricode=concat(replace(substring(tricode,1,locate(?,tricode)+length(?)),?,?),substring(tricode,locate(?,tricode)+length(?)+1)), ");
        sqlStaffChangeCode.append(" direct_leader_code=concat(replace(substring(direct_leader_code,1,locate(?,direct_leader_code)+length(?)),?,?),substring(direct_leader_code,locate(?,direct_leader_code)+length(?)+1)) ");
        sqlStaffChangeCode.append(" where tricode like ? ");
        
        flag=baseDao.executeUpdate(sqlStaffChangeCode.toString(), new Object[] {originDeptCode,originDeptCode,originDeptCode,newMoveCode,originDeptCode,originDeptCode,originDeptCode,originDeptCode,originDeptCode,newMoveCode,originDeptCode,originDeptCode,originDeptCode+"%"});
        
        if (flag) {
            fse.setValue(CmnConst.RESULT_STATUS, 1);
        }
        
        return baseDao.update(fse);
    }        
}