Browse Source

昆仑人员信息同步

guozheng 3 months ago
parent
commit
e4e740dc36

+ 0 - 19
src/main/java/com/finogeeks/swan/hxaccountsync/client/StaffClient.java

@@ -193,24 +193,5 @@ public class StaffClient {
         return responseEntity.getBody();
     }
 
-    public AddJobRes addJobList(String superStaff, AddJobReq addReq) {
-        return null;
-    }
 
-    public List<StaffGroup> addGroupList(List<AddGroupReq> addGroupReqs, String operator) {
-        String url = baseUrl + BASE_URI_PREFIX + "/groupLisst" + "?jwt=" + jwt;
-        HttpHeaders headers = new HttpHeaders();
-        headers.add("X-Consumer-Custom-ID", operator);
-        headers.add("X-Consumer-Merchant-ID", mid);
-        Map<String, Object> body = DataConverterUtil.beanToMap(addGroupReqs);
-        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(body, headers);
-        ResponseEntity<StaffGroup[]> responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, StaffGroup[].class);
-        if (!responseEntity.getStatusCode().is2xxSuccessful()) {
-            throw new ServiceException(responseEntity.getStatusCode(), "FC_ERROR", "新增分组错误");
-        }
-        return Arrays.asList(responseEntity.getBody());
-    }
-
-    public void deleteGroupList(List<String> deletegroupIdList) {
-    }
 }

+ 15 - 0
src/main/java/com/finogeeks/swan/hxaccountsync/domain/KlDept.java

@@ -0,0 +1,15 @@
+package com.finogeeks.swan.hxaccountsync.domain;
+
+import lombok.Data;
+
+/**
+ * @author Guo Zheng
+ * @date 2020/8/16 2:19 下午
+ */
+@Data
+public class KlDept {
+    private String groupName;
+    private String DeptCode;
+    private String id;
+    private String parentId;
+}

+ 8 - 0
src/main/java/com/finogeeks/swan/hxaccountsync/domain/KlJob.java

@@ -0,0 +1,8 @@
+package com.finogeeks.swan.hxaccountsync.domain;
+
+/**
+ * @author Guo Zheng
+ * @date 2020/8/16 2:19 下午
+ */
+public class KlJob {
+}

+ 55 - 0
src/main/java/com/finogeeks/swan/hxaccountsync/domain/KlUser.java

@@ -0,0 +1,55 @@
+package com.finogeeks.swan.hxaccountsync.domain;
+
+import lombok.Data;
+
+/**
+ * @author Guo Zheng
+ * @date 2020/8/16 2:18 下午
+ */
+@Data
+public class KlUser {
+    //    工号(employeeNumber)	字符串	必填
+    private String employeeNumber;
+    //    手机号(phone)	字符串	必填
+    private String phone;
+
+    //    帐号(account)	字符串	必填
+    private String account;
+
+    //    密码(password)	字符串	必填
+    private String password;
+
+    //    姓名(name)	字符串	必填
+    private String name;
+
+    //    性别(sex)	字符串	必填
+    private String sex;
+
+    //    所属部门(groups)	字符串	必填
+    private String groups;
+
+    //    渠道(agentCate)	字符串	必填
+    private String agentCate;
+
+    //    机构编码(organId)	字符串	当渠道为1、8时必填
+    private String organId;
+
+    //    中介机构编(agencyId)	字符串	当渠道为6Z、8Z时必填
+    private String agencyId;
+
+    //    职位(position)	字符串	选填
+    private String position;
+
+    //    邮箱(email)	字符串	选填
+    private String email;
+
+    //    座机(hotline)	字符串	选填
+    private String hotline;
+
+    //    所属角色(roles)	字符串	选填
+    private String roles;
+    //    入职时间(createTime)
+    private String createTime;
+    //员工id
+    private String staffId;
+}

+ 3 - 19
src/main/java/com/finogeeks/swan/hxaccountsync/entity/AddGroupReq.java

@@ -6,7 +6,6 @@ import lombok.Data;
 import lombok.NoArgsConstructor;
 
 import java.util.List;
-import java.util.Objects;
 
 /**
  * @author roshan
@@ -18,22 +17,7 @@ public class AddGroupReq {
     private String name;
     private String parentId;
     private String hotline;
-    private List<String> parentIds;
-//    private Location location;
-    private String groupId;
-    private String excelHelper;
-    private int depth;
-
-    @Override
-    public boolean equals(Object o) {
-        if (this == o) return true;
-        if (o == null || getClass() != o.getClass()) return false;
-        AddGroupReq that = (AddGroupReq) o;
-        return Objects.equals(excelHelper, that.excelHelper);
-    }
-
-    @Override
-    public int hashCode() {
-        return Objects.hash(excelHelper);
-    }
+    private List<String> staffs;
+    private Location location;
+    private String groupNumber;
 }

+ 29 - 29
src/main/java/com/finogeeks/swan/hxaccountsync/entity/StaffDto.java

@@ -1,5 +1,7 @@
 package com.finogeeks.swan.hxaccountsync.entity;
 
+import com.finogeeks.swan.hxaccountsync.domain.KlDept;
+import com.finogeeks.swan.hxaccountsync.domain.KlUser;
 import lombok.Data;
 import org.bson.BSONObject;
 import org.bson.conversions.Bson;
@@ -9,6 +11,7 @@ import org.springframework.util.StringUtils;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
+import java.util.UUID;
 
 /**
  * @author Guo Zheng
@@ -58,6 +61,10 @@ public class StaffDto {
     private String roles;
     //    入职时间(createTime)
     private String createTime;
+    //员工id
+    private String staffId;
+    //部门id
+    private String groupId;
 
     @Override
     public boolean equals(Object o) {
@@ -86,37 +93,30 @@ public class StaffDto {
         return Objects.hash(employeeNumber, phone, account, password, name, sex, groups, agentCate, organId, agencyId, position, email, hotline, roles, createTime);
     }
 
-    public AddStaffReq toStaffReq() {
-        AddStaffReq addReq = new AddStaffReq();
-        addReq.setAccount(account);
-        addReq.setName(name);
-        addReq.setEmail(email);
-        addReq.setPhone(phone);
-        addReq.setEmployeeNo(employeeNumber);
-        addReq.setShowGroup(true);
-        addReq.setShowJob(true);
-        addReq.setZoneStatus(1);
-        addReq.setCompany(new Company("","昆仑健康保险",""));
-        return addReq;
-    }
 
-    public List<AddGroupReq> toGroupReq() {
-        String[] split = StringUtils.split(groups, "/");
-        List<AddGroupReq> addGroupReqList = new ArrayList<>();
-
-        int i=1;
-        String excelHelper = split[0];
-        for(String groupName :split){
-            AddGroupReq addGroupReq = new AddGroupReq(groupName, null, hotline, null, null,excelHelper,i);
-            addGroupReqList.add(addGroupReq);
-            if(split.length>i){
-                excelHelper+="/"+split[1];
-            }
-            i++;
-        }
-        return addGroupReqList;
-    }
     public AddJobReq toJobReq(String staff) {
         return new AddJobReq(position,staff);
     }
+
+    public KlUser toKlUser() {
+        return  null;
+    }
+
+    public List<KlDept> toklDept() {
+        String[] split = StringUtils.split(groups, ",");
+        String parentId = null;
+        List<KlDept> deptList=new ArrayList<>();
+        for(String groupName:split){
+            KlDept klDept = new KlDept();
+            klDept.setGroupName(groupName);
+            String id = UUID.randomUUID().toString();
+            klDept.setId(id);
+            klDept.setDeptCode(groups);
+            klDept.setParentId(parentId);
+            parentId=id;
+            deptList.add(klDept);
+        }
+        this.groupId=parentId;
+        return deptList;
+    }
 }

+ 268 - 303
src/main/java/com/finogeeks/swan/hxaccountsync/services/AccountJob.java

@@ -2,10 +2,7 @@ package com.finogeeks.swan.hxaccountsync.services;
 
 import com.finogeeks.swan.hxaccountsync.client.ContactClient;
 import com.finogeeks.swan.hxaccountsync.client.StaffClient;
-import com.finogeeks.swan.hxaccountsync.domain.DeptEntity;
-import com.finogeeks.swan.hxaccountsync.domain.JobEntity;
-import com.finogeeks.swan.hxaccountsync.domain.JobUserLinkEntity;
-import com.finogeeks.swan.hxaccountsync.domain.UserEntity;
+import com.finogeeks.swan.hxaccountsync.domain.*;
 import com.finogeeks.swan.hxaccountsync.entity.*;
 import com.finogeeks.swan.hxaccountsync.repositories.DeptRepository;
 import com.finogeeks.swan.hxaccountsync.repositories.JobRepository;
@@ -16,12 +13,11 @@ import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.beanutils.BeanUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.bson.types.ObjectId;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
+import org.springframework.util.StringUtils;
 
 import java.lang.reflect.InvocationTargetException;
 import java.util.*;
@@ -37,13 +33,8 @@ public class AccountJob {
             "系统运营人员", "总部各部门人员", "营业部总经理", "营业部投顾团队管理人员", "营业部营销团队管理人员",
             "营业部合规人员", "投顾角色"};
 
-    private final UserRepository userRepository;
-    private final DeptRepository deptRepository;
-    private final JobRepository jobRepository;
-    private final JobUserLinkRepository jobUserLinkRepository;
     private final StaffClient staffClient;
     private final ContactClient contactClient;
-
     /**
      * SWAN 分组groupNumber-groupId Map
      */
@@ -62,12 +53,12 @@ public class AccountJob {
     /**
      * OA 部门ID-Entity Map
      */
-    private final Map<String, List<AddGroupReq>> constDeptMap = Maps.newHashMap();
+    private final Map<String, KlDept> constDeptMap = Maps.newHashMap();
 
     /**
      * OA 员工account-Entity Map
      */
-    private final Map<String, AddStaffReq> constUserAccMap = Maps.newHashMap();
+    private final Map<String, StaffDto> constUserAccMap = Maps.newHashMap();
 
     /**
      * OA 需要建立虚拟节点的组织
@@ -82,8 +73,7 @@ public class AccountJob {
     /**
      * OA 岗位ID-Entity Map
      */
-    private final Set <String> constJobIdSet = Sets.newHashSet();
-
+    private final Map<String, String> constJobIdMap = Maps.newHashMap();
 
     @Value("${staff.domain}")
     private String domain;
@@ -91,103 +81,35 @@ public class AccountJob {
     @Value("${staff.super}")
     private String superStaff;
 
+
     @Autowired
-    public AccountJob(UserRepository userRepository,
-                      DeptRepository deptRepository,
-                      JobRepository jobRepository,
-                      JobUserLinkRepository jobUserLinkRepository,
-                      StaffClient staffClient,
-                      ContactClient contactClient) {
-        this.userRepository = userRepository;
-        this.deptRepository = deptRepository;
-        this.jobRepository = jobRepository;
-        this.jobUserLinkRepository = jobUserLinkRepository;
+    public AccountJob(StaffClient staffClient, ContactClient contactClient) {
         this.staffClient = staffClient;
         this.contactClient = contactClient;
     }
 
     @Async
-    public void runJob() throws Exception {
+    public void runJob() {
         log.info("start job");
         dataPrepare();
         log.info("dataPrepare end");
-        syncJob();
+        syncJobAndRole();
         log.info("syncJobAndRole end");
-//        syncVirtualDepts(true);
+        syncVirtualDepts(true);
         log.info("createVirtualDepts end");
-        syncDept();
+        syncDept(false);
         log.info("createDept end");
         syncStaff();
         log.info("syncStaff end");
-
-//        syncVirtualDepts(false);
+        syncDept(true);
+        log.info("syncDept end");
+        syncVirtualDepts(false);
         log.info("syncVirtualDepts end");
         dataClean();
         log.info("dataClean end");
         log.info("job end");
     }
 
-    private void syncDept() throws Exception {
-        //需要新增的组织 constGroupNumberIdMap(现有的组织)中不存在的组织
-        Set<AddGroupReq> addGroupSet = new HashSet<>();
-        for(Map.Entry<String,List<AddGroupReq>> entry: constDeptMap.entrySet()){
-            if(!constGroupNumberIdMap.containsKey(entry.getKey())){
-                addGroupSet.addAll(entry.getValue());
-            }
-        }
-        //添加父节点id
-        reformParentNode(addGroupSet);
-        List<AddGroupReq> addGroupReqs = new ArrayList<>(addGroupSet);
-        List<StaffGroup> staffGroup = staffClient.addGroupList(addGroupReqs, superStaff);
-        //需要软删除的组织
-        List<String> deletegroupIdList = new ArrayList<>();
-        for(Map.Entry<String,String> entry: constGroupNumberIdMap.entrySet()){
-            if(!constDeptMap.containsKey(entry.getKey())){
-                deletegroupIdList.add(entry.getValue());
-            }
-        }
-        staffClient.deleteGroupList(deletegroupIdList);
-    }
-
-    /**
-     * 重构树形结构 parentId
-     * @param addGroupSet
-     */
-    private void reformParentNode(Set<AddGroupReq> addGroupSet) throws Exception {
-        Map<String,AddGroupReq> parentMap=null;
-        for (int depth=1;depth<=6;depth++){
-            Map<String,AddGroupReq> childMap = new HashMap<>();
-            if(depth==1){
-                for(AddGroupReq req:addGroupSet){
-                    if(req.getDepth()==depth){
-                        req.setGroupId(new ObjectId().toString());
-                        childMap.put(req.getExcelHelper(),req);
-                    }
-                }
-            }else {
-                for(AddGroupReq req:addGroupSet){
-                    if(req.getDepth()==depth){
-                        String parentGroup = StringUtils.substringBeforeLast(req.getExcelHelper(), "/");
-                        AddGroupReq parentNode = parentMap.get(parentGroup);
-                        if(parentNode==null){
-                            throw new Exception("parentGroup is error");
-                        }
-                        req.setParentId(parentNode.getGroupId());
-                        List<String> parentIds = parentNode.getParentIds();
-                        if(parentIds!=null){
-                            parentIds.add(parentGroup);
-                            req.setParentIds(parentIds);
-                        }else{
-                            req.setParentIds(Arrays.asList(parentGroup));
-                        }
-                        childMap.put(req.getExcelHelper(),req);
-                    }
-                }
-            }
-            parentMap=childMap;
-        }
-    }
-
     private void dataClean() {
         constJobNameMap.clear();
         constGroupNumberIdMap.clear();
@@ -196,7 +118,7 @@ public class AccountJob {
         constUserAccMap.clear();
         constVirtualSet.clear();
         constUserIdJobIdMap.clear();
-        constJobIdSet.clear();
+        constJobIdMap.clear();
     }
 
     private void dataPrepare() {
@@ -208,7 +130,7 @@ public class AccountJob {
         List<StaffGroup> groups = staffClient.getAllGroup(superStaff);
         for (StaffGroup group : groups) {
             if (!StringUtils.isEmpty(group.getGroupId()) && !StringUtils.isEmpty(group.getGroupNumber())) {
-                constGroupNumberIdMap.put(group.getExcelHelper(), group.getGroupId());
+                constGroupNumberIdMap.put(group.getGroupNumber(), group.getGroupId());
             }
         }
 
@@ -229,119 +151,165 @@ public class AccountJob {
         }
 
         // 记录有员工的部门ID
+        Set<String> constStaffExistSet = Sets.newHashSet();
 
+        //开始获取昆仑的信息
         List<StaffDto> staffList = contactClient.getStaffList();
-        for(StaffDto staffdto:staffList){
-            //获取 员工信息List 装入 constUserAccMap
-            AddStaffReq staffReq = staffdto.toStaffReq();
-            constUserAccMap.put(staffdto.getAccount(),staffReq);
-            //获取group list 装入 constDeptMap
-            //前提是没有重复的部门名称
-            String group = staffdto.getGroups();
-            if(!constDeptMap.containsKey(group)){
-                constDeptMap.put(staffdto.getGroups(),staffdto.toGroupReq());
+
+        // 获取昆仑员工
+         for (StaffDto entity : staffList) {
+            if (entity != null && entity.getStaffId() != null
+                    && !StringUtils.isEmpty(entity.getAccount())) {
+                String oaAccount = entity.getAccount();
+                if (!constUserAccMap.containsKey(oaAccount)) {
+//                    // 避免oa账户重复,有人在职,有人离职的情况
+//                    if (entity.getXstatus() == 1) {
+//                        constUserAccMap.put(oaAccount, entity);
+//                    }
+                } else {
+                    constUserAccMap.put(oaAccount, entity);
+                }
+                // 记录有员工的集合
+                constStaffExistSet.add(entity.getGroups());
+            }
+            //获取工作信息
+             if (entity != null && !StringUtils.isEmpty(entity.getPosition())) {
+                 constJobIdMap.put(entity.getPosition(), entity.getPosition());
+             }
+        }
+
+        // 获取昆仑的的组织
+        Iterable<KlDept> deptAll = staff2KlDept(staffList);
+        for (KlDept klDept : deptAll) {
+            if (klDept != null && !StringUtils.isEmpty(klDept.getId())
+                    && !StringUtils.isEmpty(klDept.getGroupName())) {
+                String parentId = klDept.getParentId();
+                // 存在父级节点
+                if (!StringUtils.isEmpty(parentId)) {
+                    // 父级节点下有人
+                    if (constStaffExistSet.contains(parentId)) {
+                        constVirtualSet.add(parentId);
+                    }
+                }
+                constDeptMap.put(klDept.getId(), klDept);
             }
-            //获取job 去重
-            constJobIdSet.add(staffdto.getPosition());
         }
+        //获取员工和职位的关系
+//        Iterable<JobUserLinkEntity> userLinks = jobUserLinkRepository.findAll();
+//        for (JobUserLinkEntity link : userLinks) {
+//            if (link != null && !StringUtils.isEmpty(link.getUserid())
+//                    && !StringUtils.isEmpty(link.getJobid()) && link.getMajorjob() == 1) {
+//                constUserIdJobIdMap.put(link.getUserid(), link);
+//            }
+//        }
+
+    }
 
+    private Iterable<KlDept> staff2KlDept(List<StaffDto> staffList) {
+        List<KlDept> klDepts = new ArrayList<>();
+        for (StaffDto dto :staffList){
+            klDepts.addAll(dto.toklDept());
+        }
+        return klDepts;
     }
 
+
+
     /**
      * 岗位和权限角色的对应关系
      *
-     * @param job string 岗位名称
+     * @param
      * @return string 权限角色名称
      */
-    private String roleNameConvert(String job) {
-        String role;
-        switch (job) {
-            case "营业部总经理":
-            case "营业部副总经理(主持工作)":
-                role = "营业部总经理";
-                break;
-            case "投顾总监助理":
-            case "投顾总监":
-                role = "营业部投顾团队管理人员";
-                break;
-            case "市场总监":
-                role = "营业部营销团队管理人员";
-                break;
-            case "合规专员":
-                role = "营业部合规人员";
-                break;
-            case "投资顾问":
-            case "投资顾问助理":
-            case "服务经理":
-            case "财富顾问(新设营业部)":
-            case "财富顾问(非新设营业部)":
-                role = "投顾角色";
-                break;
-            default:
-                // 其它岗位默认没有权限角色
-                role = "";
-        }
-        return role;
-    }
-
-//    public void syncVirtualDepts(boolean withoutParent) {
-//        // 建立虚拟的直属部门
-//        for (String deptId : constVirtualSet) {
-//            AddGroupReq dept = constDeptMap.get(deptId);
-//            if (dept != null) {
-//                try {
-//                    DeptEntity virtualDept = (DeptEntity) BeanUtils.cloneBean(dept);
-//                    String virtualDeptName = convertDeptName(dept.getTitle());
-//                    String virtualDeptCode = convertDeptCode(dept.getDepcode());
-//                    virtualDept.setDepcode(virtualDeptCode);
-//                    // 没有父级的虚拟部门, 只新建不更新
-//                    boolean update = !withoutParent;
-//                    if (withoutParent) {
-//                        // 先建立没有父级的虚拟部门
-//                        virtualDept.setParentid(null);
-//                    } else {
-//                        virtualDept.setParentid(deptId);
-//                    }
-//                    virtualDept.setTitle(virtualDeptName);
-//                    createOrUpdateGroup(virtualDept, update);
-//                } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | InstantiationException e) {
-//                    log.error(e.getMessage());
-//                }
-//            }
+//    private String roleNameConvert(String job) {
+//        String role;
+//        switch (job) {
+//            case "营业部总经理":
+//            case "营业部副总经理(主持工作)":
+//                role = "营业部总经理";
+//                break;
+//            case "投顾总监助理":
+//            case "投顾总监":
+//                role = "营业部投顾团队管理人员";
+//                break;
+//            case "市场总监":
+//                role = "营业部营销团队管理人员";
+//                break;
+//            case "合规专员":
+//                role = "营业部合规人员";
+//                break;
+//            case "投资顾问":
+//            case "投资顾问助理":
+//            case "服务经理":
+//            case "财富顾问(新设营业部)":
+//            case "财富顾问(非新设营业部)":
+//                role = "投顾角色";
+//                break;
+//            default:
+//                // 其它岗位默认没有权限角色
+//                role = "";
 //        }
+//        return role;
 //    }
 
-//    public void syncDept(boolean update) {
-//        log.debug("dept size:" + constDeptMap.size());
-//        if (update) {
-//            for (DeptEntity dept : constDeptMap.values()) {
-//                createOrUpdateGroup(dept, true);
-//            }
-//        } else {
-//            Set<String> deptNameSet = Sets.newHashSet();
-//            for (DeptEntity dept : constDeptMap.values()) {
-//                // 部门名称重复
-//                if (deptNameSet.contains(dept.getTitle())) {
-//                    if (!StringUtils.isEmpty(dept.getParentid())) {
-//                        try {
-//                            DeptEntity newDept = (DeptEntity) BeanUtils.cloneBean(dept);
-//                            DeptEntity parentNode = constDeptMap.get(dept.getParentid());
-//                            String newDeptName = parentNode.getTitle() + "-" + dept.getTitle();
-//                            newDept.setTitle(newDeptName);
-//                            deptNameSet.add(newDeptName);
-//                            createOrUpdateGroup(newDept, false);
-//                            constDeptMap.put(newDept.getId(), newDept);
-//                        } catch (IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException e) {
-//                            e.printStackTrace();
-//                        }
-//                    }
-//                } else {
-//                    deptNameSet.add(dept.getTitle());
-//                    createOrUpdateGroup(dept, false);
-//                }
-//            }
-//        }
-//    }
+    public void syncVirtualDepts(boolean withoutParent) {
+        // 建立虚拟的直属部门
+        for (String deptId : constVirtualSet) {
+            KlDept dept = constDeptMap.get(deptId);
+            if (dept != null) {
+                try {
+                    KlDept virtualDept = (KlDept) BeanUtils.cloneBean(dept);
+                    String virtualDeptName = convertDeptName(dept.getGroupName());
+                    String virtualDeptCode = convertDeptCode(dept.getDeptCode());
+                    virtualDept.setDeptCode(virtualDeptCode);
+                    // 没有父级的虚拟部门, 只新建不更新
+                    boolean update = !withoutParent;
+                    if (withoutParent) {
+                        // 先建立没有父级的虚拟部门
+                        virtualDept.setParentId(null);
+                    } else {
+                        virtualDept.setParentId(deptId);
+                    }
+                    virtualDept.setGroupName(virtualDeptName);
+                    createOrUpdateGroup(virtualDept, update);
+                } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | InstantiationException e) {
+                    log.error(e.getMessage());
+                }
+            }
+        }
+    }
+
+    public void syncDept(boolean update) {
+        log.debug("dept size:" + constDeptMap.size());
+        if (update) {
+            for (KlDept dept : constDeptMap.values()) {
+                createOrUpdateGroup(dept, true);
+            }
+        } else {
+            Set<String> deptNameSet = Sets.newHashSet();
+            for (KlDept dept : constDeptMap.values()) {
+                // 部门名称重复
+                if (deptNameSet.contains(dept.getGroupName())) {
+                    if (!StringUtils.isEmpty(dept.getParentId())) {
+                        try {
+                            KlDept newDept = (KlDept) BeanUtils.cloneBean(dept);
+                            KlDept parentNode = constDeptMap.get(dept.getParentId());
+                            String newDeptName = parentNode.getGroupName() + "-" + dept.getGroupName();
+                            newDept.setGroupName(newDeptName);
+                            deptNameSet.add(newDeptName);
+                            createOrUpdateGroup(newDept, false);
+                            constDeptMap.put(newDept.getId(), newDept);
+                        } catch (IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException e) {
+                            e.printStackTrace();
+                        }
+                    }
+                } else {
+                    deptNameSet.add(dept.getGroupName());
+                    createOrUpdateGroup(dept, false);
+                }
+            }
+        }
+    }
 
     private String convertDeptCode(String deptCode) {
         return deptCode + "-" + "Direct";
@@ -351,35 +319,36 @@ public class AccountJob {
         return deptName + "(直属)";
     }
 
-//    private void createOrUpdateGroup(DeptEntity dept, boolean update) {
-//        if (dept != null) {
-//            String deptName = dept.getTitle();
-//            String groupNumber = dept.getDepcode();
-//            try {
-//                // 新的分组
-//                if (!constGroupNumberIdMap.containsKey(groupNumber)) {
-//                    AddGroupReq addReq = new AddGroupReq();
-//                    addReq.setName(deptName);
-//                     if (!StringUtils.isEmpty(dept.getParentid())) {
-//                        DeptEntity parentDept = constDeptMap.get(dept.getParentid());
-//                        String parentGroupId = constGroupNumberIdMap.get(parentDept.getDepcode());
-//                        if (!StringUtils.isEmpty(parentGroupId)) {
-//                            addReq.setParentId(parentGroupId);
-//                        }
-//                    }
-//
-//                    try {
-//                        StaffGroup addRes = staffClient.addGroup(superStaff, addReq);
-//                        constGroupNumberIdMap.put(groupNumber, addRes.getGroupId());
-//                    } catch (Exception e) {
-//                        log.error("create " + deptName + "," + groupNumber + "," + e.getMessage());
-//                    }
-//                } else if (update) {
-//                    // 更新
-//                    String groupId = constGroupNumberIdMap.get(groupNumber);
-//                    UpdateGroupReq updateReq = new UpdateGroupReq();
-//                    updateReq.setGroupNumber(groupNumber);
-//                    String title = dept.getTitle();
+    private void createOrUpdateGroup(KlDept dept, boolean update) {
+        if (dept != null) {
+            String deptName = dept.getGroupName();
+            String groupNumber = dept.getDeptCode();
+            try {
+                // 新的分组
+                if (!constGroupNumberIdMap.containsKey(groupNumber)) {
+                    AddGroupReq addReq = new AddGroupReq();
+                    addReq.setName(deptName);
+//                    addReq.setGroupNumber(groupNumber);
+                    if (!StringUtils.isEmpty(dept.getParentId())) {
+                        KlDept parentDept = constDeptMap.get(dept.getParentId());
+                        String parentGroupId = constGroupNumberIdMap.get(parentDept.getDeptCode());
+                        if (!StringUtils.isEmpty(parentGroupId)) {
+                            addReq.setParentId(parentGroupId);
+                        }
+                    }
+
+                    try {
+                        StaffGroup addRes = staffClient.addGroup(superStaff, addReq);
+                        constGroupNumberIdMap.put(groupNumber, addRes.getGroupId());
+                    } catch (Exception e) {
+                        log.error("create " + deptName + "," + groupNumber + "," + e.getMessage());
+                    }
+                } else if (update) {
+                    // 更新
+                    String groupId = constGroupNumberIdMap.get(groupNumber);
+                    UpdateGroupReq updateReq = new UpdateGroupReq();
+                    updateReq.setGroupNumber(groupNumber);
+                    String title = dept.getGroupName();
 //                    if (dept.getTitle().contains("-")) {
 //                        Optional<DeptEntity> optional = deptRepository.findById(dept.getId());
 //                        if (optional.isPresent()) {
@@ -389,35 +358,35 @@ public class AccountJob {
 //                            }
 //                        }
 //                    }
-//                    updateReq.setName(title);
-//                    if (!StringUtils.isEmpty(dept.getParentid())) {
-//                        DeptEntity parentDept = constDeptMap.get(dept.getParentid());
-//                        String parentGroupId = constGroupNumberIdMap.get(parentDept.getDepcode());
-//                        if (!StringUtils.isEmpty(parentGroupId)) {
-//                            updateReq.setParentId(parentGroupId);
-//                        }
-//                    }
-//                    try {
-//                        staffClient.updateGroup(superStaff, groupId, updateReq);
-//                    } catch (Exception e) {
-//                        log.error("update " + deptName + "," + groupNumber + "," + e.getMessage());
-//                    }
-//                }
-//            } catch (Exception e) {
-//                log.error(e.getMessage());
-//            }
-//        }
-//    }
+                    updateReq.setName(title);
+                    if (!StringUtils.isEmpty(dept.getParentId())) {
+                        KlDept parentDept = constDeptMap.get(dept.getParentId());
+                        String parentGroupId = constGroupNumberIdMap.get(parentDept.getDeptCode());
+                        if (!StringUtils.isEmpty(parentGroupId)) {
+                            updateReq.setParentId(parentGroupId);
+                        }
+                    }
+                    try {
+                        staffClient.updateGroup(superStaff, groupId, updateReq);
+                    } catch (Exception e) {
+                        log.error("update " + deptName + "," + groupNumber + "," + e.getMessage());
+                    }
+                }
+            } catch (Exception e) {
+                log.error(e.getMessage());
+            }
+        }
+    }
 
-    public void syncJob() {
-        log.debug("jobEntityList size:" + constJobIdSet.size());
-        for (String jobName : constJobIdSet) {
+    public void syncJobAndRole() {
+        log.debug("jobEntityList size:" + constJobIdMap.size());
+        for (String jobName : constJobIdMap.values()) {
             try {
                 if (!constJobNameMap.containsKey(jobName)) {
                     AddJobReq addReq = new AddJobReq();
                     addReq.setName(jobName);
                     addReq.setUpdateUser(superStaff);
-                    AddJobRes addRes = staffClient.addJobList(superStaff, addReq);
+                    AddJobRes addRes = staffClient.addJob(superStaff, addReq);
                     constJobNameMap.put(jobName, addRes.getJobId());
                 }
             } catch (Exception e) {
@@ -425,12 +394,13 @@ public class AccountJob {
             }
         }
         try {
-            for (String roleName : DEFAULT_ROLES) {
-                if (!constRoleNameMap.containsKey(roleName)) {
-                    AddPermissionReq req = new AddPermissionReq(roleName);
-                    staffClient.addPermission(superStaff, req);
-                }
-            }
+            //暂时不用同步角色
+//            for (String roleName : DEFAULT_ROLES) {
+//                if (!constRoleNameMap.containsKey(roleName)) {
+//                    AddPermissionReq req = new AddPermissionReq(roleName);
+//                    staffClient.addPermission(superStaff, req);
+//                }
+//            }
         } catch (Exception e) {
             log.error(e.getMessage());
         }
@@ -440,19 +410,19 @@ public class AccountJob {
         log.debug("valid users total:" + constUserAccMap.size());
         List<String> deActiveList = Lists.newArrayList();
         Company company = new Company("", "华西证券", "95584");
-        for (AddStaffReq entity : constUserAccMap.values()) {
+        for (StaffDto entity : constUserAccMap.values()) {
             // 获取部门信息
-            DeptEntity deptEntity = constDeptMap.get(entity.getDepid());
+            KlDept deptEntity = constDeptMap.get(entity.getGroups());
             if (deptEntity == null) {
                 // 部门不存在
-                log.error(entity.getDepid() + ",部门ID不存在");
+                log.error(entity.getGroups() + ",部门ID不存在");
                 continue;
             }
             String groupId;
             // 虚拟直属部门
-            if (constVirtualSet.contains(entity.getDepid())) {
-                String virtualDeptName = convertDeptName(deptEntity.getTitle());
-                String virtualDeptCode = convertDeptCode(deptEntity.getDepcode());
+            if (constVirtualSet.contains(entity.getGroupId())) {
+                String virtualDeptName = convertDeptName(deptEntity.getGroupName());
+                String virtualDeptCode = convertDeptCode(deptEntity.getDeptCode());
                 if (!constGroupNumberIdMap.containsKey(virtualDeptCode)) {
                     // 虚拟直属部门名称错误
                     log.error(virtualDeptName + "," + virtualDeptCode+ ",虚拟直属部门CODE错误");
@@ -460,14 +430,14 @@ public class AccountJob {
                 }
                 groupId = constGroupNumberIdMap.get(virtualDeptCode);
             } else {
-                if (!constGroupNumberIdMap.containsKey(deptEntity.getDepcode())) {
+                if (!constGroupNumberIdMap.containsKey(deptEntity.getDeptCode())) {
                     // 部门ID错误
-                    log.error(deptEntity.getTitle()+ "," + deptEntity.getDepcode() + ",部门CODE错误");
+                    log.error(deptEntity.getGroupName()+ "," + deptEntity.getDeptCode() + ",部门CODE错误");
                     continue;
                 }
-                groupId = constGroupNumberIdMap.get(deptEntity.getDepcode());
+                groupId = constGroupNumberIdMap.get(deptEntity.getDeptCode());
             }
-            String fcid = "@staff_" + entity.getOaaccount() + ":" + domain;
+            String fcid = entity.getStaffId();
             StaffVO staff = null;
             try {
                 staff = staffClient.getStaffById(fcid);
@@ -475,33 +445,30 @@ public class AccountJob {
                 log.error(e.getMessage());
             }
             if (staff == null) {
-                if (entity.getXstatus() == 1) {
+//                if (entity.getXstatus() == 1) {
                     // 在职的新员工
                     AddStaffReq addReq = new AddStaffReq();
-                    addReq.setAccount(entity.getOaaccount());
+                    addReq.setAccount(entity.getAccount());
                     addReq.setName(entity.getName());
-                    addReq.setEmail(entity.getOamail());
-                    addReq.setPhone(entity.getMobile());
-                    addReq.setEmployeeNo(entity.getBadge());
+                    addReq.setEmail(entity.getEmail());
+                    addReq.setPhone(entity.getPhone());
+//                    addReq.setEmployeeNo(entity.getBadge());
                     addReq.setGroup(groupId);
                     addReq.setShowGroup(true);
                     addReq.setShowJob(true);
                     addReq.setZoneStatus(1);
                     addReq.setCompany(company);
-                    JobEntity jobEntity = null;
-                    JobUserLinkEntity jobUserLinkEntity = constUserIdJobIdMap.get(entity.getId());
-                    if (jobUserLinkEntity != null) {
-                        jobEntity = constJobIdMap.get(jobUserLinkEntity.getJobid());
-                    }
-                    if (jobEntity != null && constJobNameMap.containsKey(jobEntity.getTitle())) {
-                        String jobId = constJobNameMap.get(jobEntity.getTitle());
-                        String roleName = roleNameConvert(jobEntity.getTitle());
-                        if (!StringUtils.isEmpty(roleName) && constRoleNameMap.containsKey(roleName)) {
-                            String roleId = constRoleNameMap.get(roleName);
-                            addReq.setPermissionRoles(Lists.newArrayList(roleId));
-                        } else {
-                            addReq.setPermissionRoles(Lists.newArrayList());
-                        }
+                    String jobName = null;
+
+                    if (entity != null && constJobNameMap.containsKey(entity.getPosition())) {
+                        String jobId = constJobNameMap.get(entity.getPosition());
+//                        String roleName = roleNameConvert(jobEntity.getTitle());
+//                        if (!StringUtils.isEmpty(roleName) && constRoleNameMap.containsKey(roleName)) {
+//                            String roleId = constRoleNameMap.get(roleName);
+//                            addReq.setPermissionRoles(Lists.newArrayList(roleId));
+//                        } else {
+//                            addReq.setPermissionRoles(Lists.newArrayList());
+//                        }
                         addReq.setJobs(Lists.newArrayList(jobId));
                         try {
                             staffClient.addStaff(superStaff, addReq);
@@ -509,27 +476,24 @@ public class AccountJob {
                             log.error(e.getMessage());
                         }
                     }
-                }
+//                }
             } else {
                 // 老员工
-                if (entity.getXstatus() == 1) {
+//                if (entity.getXstatus() == 1) {
                     // 在职
                     UpdateStaffReq req = new UpdateStaffReq();
-                    req.setEmail(entity.getOamail());
-                    req.setEmployeeNo(entity.getBadge());
+                    req.setEmail(entity.getEmail());
+//                    req.setEmployeeNo(entity.getBadge());
                     req.setGroup(groupId);
                     req.setShowGroup(true);
                     req.setShowJob(true);
                     req.setCompany(company);
                     req.setName(entity.getName());
-                    req.setAccount(entity.getOaaccount());
+                    req.setAccount(entity.getAccount());
                     JobEntity jobEntity = null;
-                    JobUserLinkEntity jobUserLinkEntity = constUserIdJobIdMap.get(entity.getId());
-                    if (jobUserLinkEntity != null) {
-                        jobEntity = constJobIdMap.get(jobUserLinkEntity.getJobid());
-                    }
-                    if (jobEntity != null && constJobNameMap.containsKey(jobEntity.getTitle())) {
-                        String jobId = constJobNameMap.get(jobEntity.getTitle());
+
+                    if (jobEntity != null && constJobNameMap.containsKey(entity.getPosition())) {
+                        String jobId = constJobNameMap.get(entity.getPosition());
                         req.setJobs(Lists.newArrayList(jobId));
                         try {
                             staffClient.updateStaff(superStaff, fcid, req);
@@ -537,20 +501,21 @@ public class AccountJob {
                             log.error(e.getMessage());
                         }
                     }
-                } else {
-                    // 离职
-                    deActiveList.add(fcid);
-                }
-            }
-        }
-        log.info("离职的员工数量:" + deActiveList.size());
-        if (!deActiveList.isEmpty()) {
-            log.info("离职的员工:" + Arrays.toString(deActiveList.toArray()));
-            try {
-                staffClient.deActiveStaff(superStaff, new DeActiveStaffReq(deActiveList));
-            } catch (Exception e) {
-                log.error(e.getMessage());
+//                }
+//                else {
+//                    // 离职
+//                    deActiveList.add(fcid);
+//                }
             }
         }
+//        log.info("离职的员工数量:" + deActiveList.size());
+//        if (!deActiveList.isEmpty()) {
+//            log.info("离职的员工:" + Arrays.toString(deActiveList.toArray()));
+//            try {
+//                staffClient.deActiveStaff(superStaff, new DeActiveStaffReq(deActiveList));
+//            } catch (Exception e) {
+//                log.error(e.getMessage());
+//            }
+//        }
     }
 }