员工更新接口

This commit is contained in:
zengweihai 2024-05-15 12:47:29 +08:00
parent 9187c6d535
commit 37169f571b
1 changed files with 140 additions and 56 deletions

View File

@ -8,20 +8,24 @@ import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject; import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection; import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.exception.KDException; import kd.bos.exception.KDException;
import kd.bos.org.model.OrgParam;
import kd.bos.permission.model.UserParam; import kd.bos.permission.model.UserParam;
import kd.bos.schedule.executor.AbstractTask; import kd.bos.schedule.executor.AbstractTask;
import kd.bos.service.ServiceFactory; import kd.bos.service.ServiceFactory;
import kd.bos.servicehelper.QueryServiceHelper; import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.org.OrgUnitServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper; import kd.bos.servicehelper.user.UserServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper; import kd.bos.servicehelper.operation.SaveServiceHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import shkd.cosmic.cxkg.task.DTO.Query; import shkd.cosmic.cxkg.task.DTO.Query;
import shkd.cosmic.cxkg.task.DTO.QueryDTO; import shkd.cosmic.cxkg.task.DTO.QueryDTO;
import java.util.ArrayList; import java.io.UnsupportedEncodingException;
import java.util.HashMap; import java.math.BigDecimal;
import java.util.List; import java.math.RoundingMode;
import java.util.Map; import java.util.*;
/** /**
* *
@ -29,86 +33,166 @@ import java.util.Map;
*/ */
public class UserTaskImpl extends AbstractTask { public class UserTaskImpl extends AbstractTask {
private static Logger logger = LoggerFactory.getLogger(HrmDepartmentTaskImpl.class);
private static final int PAGESIZE = 20; private static final int PAGESIZE = 20;
private int CURPAGE = 1; private int CURPAGE = 1;
private DynamicObjectCollection queryOrges; private DynamicObjectCollection queryOrges;
static boolean flag = false; //判断当前用户集合是否存在需要设置的用户还没存入系统的情况
@Override @Override
public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException { public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
DynamicObjectCollection queryOrges = QueryServiceHelper.query("bos_adminorg",
"id,number,shkd_oaid", null);//查询星瀚系统部门集合
Map<Object, String> orgIds = new HashMap<Object, String>();//存放系统中已经存在的用户id集合
for (DynamicObject queryOrg : queryOrges) {
orgIds.put(queryOrg.getString("number"), (queryOrg.getLong("id") + ""));//存放已经存在的部门的id以及oaid
}
//bos_base_user_syn_weaver2kd //bos_base_user_syn_weaver2kd
//构造请求体
QueryDTO queryDTO = new QueryDTO(); QueryDTO queryDTO = new QueryDTO();
queryDTO.setCurpage(4); queryDTO.setPagesize(PAGESIZE);
queryDTO.setPagesize(1); queryDTO.setCurpage(CURPAGE);
Query query = new Query(); Query query = new Query();
query.setQueryDTO(queryDTO); query.setQueryDTO(queryDTO);
String requestBody = JSON.toJSONString(query);
String jsonResponse = HttpRequest.post("http://" + "10.157.226.9:8088" + "/api/hrm/resful/getHrmUserInfoWithPage")
.body(requestBody).timeout(20000) //20s 超时
.execute().body();
JSONObject resultJson = JSONObject.parseObject(jsonResponse);
JSONArray dataList = resultJson.getJSONObject("data").getJSONArray("dataList");
List<UserParam> paramList = new ArrayList<>(); Object isFull = map.get("isfull");//判断是否全量
UserParam user = null; if("0".equals(isFull)){//判断是否全量不是则设置时间戳参数
Calendar calendar = Calendar.getInstance();// 获取当前日期
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Long time = calendar.getTimeInMillis();
queryDTO.setCreated(time.toString());
}
JSONObject data = this.getOAUserData(query);
if(data != null){
//查询当前星瀚人员id集合
DynamicObjectCollection queryUsers = QueryServiceHelper.query("bos_user",
"id,number,shkd_oaid", null);//查询星瀚系统部门集合
Map<Object, Long> userIds = new HashMap<Object, Long>();//存放系统中已经存在的用户id集合
for (DynamicObject queryUser : queryUsers) {
userIds.put(queryUser.getString("number"), queryUser.getLong("id"));//存放已经存在的人员的id以及number
}
List<UserParam> paramList = new ArrayList<>();//创建存放人员类的集合
JSONArray dataList = data.getJSONArray("dataList");//oa人员数据集合
BigDecimal totalSize = data.getBigDecimal("totalSize");//获取查询到人员总数
BigDecimal size = new BigDecimal(PAGESIZE);//设置页大小为20
BigDecimal pageNumber = totalSize.divide(size, 0, RoundingMode.UP);//计算需要分页数量
//查询成功取第一轮数据添加入参数集合
this.buildUserParam(paramList,dataList,userIds);
//判断分页数量是否大于1是则进行遍历后续数据
if (pageNumber.compareTo(new BigDecimal(1)) > 0) {//当页数为1时不需要再次查询接口直接去同步数据
for (int i = 1; i < pageNumber.intValue(); i++){//查询成功取第一轮数据同步
//修改CURPAGE当前页数
CURPAGE = i + 1;
queryDTO.setCurpage(CURPAGE);
JSONObject dataNext = this.getOAUserData(query);//获取查询的数据
this.buildUserParam(paramList,dataNext.getJSONArray("dataList"),userIds);
}
}
UserServiceHelper.addOrUpdate(paramList);
int resultNumber = 0;
for (UserParam userParam : paramList) {
boolean success = userParam.isSuccess();
if (!success){
String name = userParam.getDataMap().get("name").toString();
String msg = userParam.getMsg();
logger.info(name+"数据同步失败:"+msg);//返回失败结果
resultNumber++;
String str = ""+resultNumber+""+name+"数据同步失败:"+msg;
System.out.println(str);
}
}
if(flag){//重新生成构造集合
paramList.clear();
userIds.clear();
queryUsers = QueryServiceHelper.query("bos_user",
"id,number,shkd_oaid", null);//查询星瀚系统部门集合
userIds = new HashMap<Object, Long>();//存放系统中已经存在的用户id集合
for (DynamicObject queryUser : queryUsers) {
userIds.put(queryUser.getString("number"), queryUser.getLong("id"));//存放已经存在的人员的id以及number
}
for (int i = 1; i < pageNumber.intValue(); i++){//查询成功取第一轮数据同步
CURPAGE = i ;
queryDTO.setCurpage(CURPAGE);
JSONObject dataNext = this.getOAUserData(query);//获取查询的数据
this.buildUserParam(paramList,dataNext.getJSONArray("dataList"),userIds);
}
UserServiceHelper.addOrUpdate(paramList);//新增或更新全量数据
for (UserParam userParam : paramList) {
boolean success = userParam.isSuccess();
if (!success){
String name = userParam.getDataMap().get("name").toString();
String msg = userParam.getMsg();
logger.info(name+"数据同步失败:"+msg);//返回失败结果
}
}
}
}
}
private JSONObject getOAUserData(Query query){
String requestBody = JSON.toJSONString(query);
//返回参数
String json = HttpRequest.post("http://" + "10.157.226.9:8088" + "/api/hrm/resful/getHrmUserInfoWithPage").body(requestBody).execute().body();
JSONObject resultJson = JSONObject.parseObject(json);;//查询oa接口返回人员数据
String code = resultJson.getString("code");//第一次执行查询判断接口调用是否成功否则不执行后续逻辑
if (!Objects.equals("1",code)){//当code不为1时调用失败
logger.info("调用接口失败"+resultJson.getString("msg"));
return null;
}
JSONObject data = resultJson.getJSONObject("data");
return data ;
}
private void buildUserParam(List<UserParam> paramList,JSONArray dataList,Map<Object,Long> userIds) {
for (int i = 0; i < dataList.size(); i++) { for (int i = 0; i < dataList.size(); i++) {
UserParam user = new UserParam();
JSONObject userData = dataList.getJSONObject(i); JSONObject userData = dataList.getJSONObject(i);
System.out.println(i + "-----------" + userData); Long workid = userIds.get(userData.getString("workcode"));
user = new UserParam(); if (workid != null){
// user.setCustomUserId(123456780L); user.setId(workid);
user.setCustomUserId(workid);
}
Map<String, Object> dataMap = new HashMap<>(); Map<String, Object> dataMap = new HashMap<>();
dataMap.put("number", userData.getString("workcode")); //工号 dataMap.put("number", userData.getString("workcode")); //工号
dataMap.put("name", userData.getString("lastname")); // 姓名 dataMap.put("name", userData.getString("lastname")); // 姓名
dataMap.put("username", userData.getString("loginid")); //用户名 dataMap.put("username", userData.getString("loginid")); //用户名
dataMap.put("usertype", "1"); dataMap.put("usertype", "1");//类型
dataMap.put("phone", userData.getString("mobile")); dataMap.put("phone", userData.getString("mobile"));//移动电话
dataMap.put("email", userData.getString("email")); dataMap.put("email", userData.getString("email"));//邮箱
// dataMap.put("idcard", userData.getString("certificatenum")); //身份证 // dataMap.put("idcard", userData.getString("certificatenum")); //身份证
dataMap.put("birthday", userData.getString("birthday")); dataMap.put("birthday", userData.getDate("birthday"));//生日
dataMap.put("gender", userData.getString("sex")); //int dataMap.put("gender", "" .equals(userData.getString("sex")) ? "1" : "2"); //性别
// dataMap.put("picturefield", ""); //头像 dataMap.put("shkd_oaid",userData.getString("id"));
// 职位分录 // 职位分录
List<Map<String, Object>> posList = new ArrayList<>(); List<Map<String, Object>> posList = new ArrayList<>();
Map<String, Object> entryentity = new HashMap<>(); Map<String, Object> entryentity = new HashMap<>();
// 设置部门ID // 通过编码设置部门
entryentity.put("dpt", orgIds.get(userData.getString("departmentcode"))); // 部门 Map<String, Object> dptNumMap = new HashMap<>();
dptNumMap.put("number", userData.getString("departmentcode"));
entryentity.put("dpt",dptNumMap ); // 部门
entryentity.put("position", userData.getString("jobactivityname")); // 职位 entryentity.put("position", userData.getString("jobactivityname")); // 职位
//entryentity.put("isincharge", false); //负责人 entryentity.put("isincharge", false); //负责人
//entryentity.put("ispartjob", false); //兼职 entryentity.put("ispartjob", false); //兼职
entryentity.put("superior", userData.getString("managerid")); // 直接上级 String managerId_oa = userData.getString("managerid");//获取上级人员id(oa)
Long managerId = userIds.get(managerId_oa);//直接上级id
if (managerId != null){
entryentity.put("superior", managerId); // 赋值直接上级
}else {
if (!"".equals(managerId_oa)){//当上级人员idoa存在且未在星瀚系统检索成功时修改参数flag,接口将重新构造人员类更新数据
flag = true;
}
}
entryentity.put("seq", 1); entryentity.put("seq", 1);
posList.add(entryentity); posList.add(entryentity);
dataMap.put("entryentity", posList); dataMap.put("entryentity", posList);
user.setDataMap(dataMap); user.setDataMap(dataMap);
user.setCustomUserId(userData.getInteger("id"));
// user.setId(293);
paramList.add(user); paramList.add(user);
} }
}
// 业务调用可以参照接口名以下举例说明微服务调用方式
// IUserService userService = (IUserService) ServiceFactory.getService(IUserService.class);
//kd.bos.servicehelper.user.UserServiceHelper.addOrUpdate(List userList)
// userService.addOrUpdate(paramList);
UserServiceHelper.addOrUpdate(paramList);
// SaveServiceHelper.save();
List<UserParam> paramList2 = new ArrayList<>();
// 判断执行结果
for (UserParam result : paramList) {
if (!result.isSuccess()) {
//
System.out.println(result.getId() + "-----------" + result.getMsg());
paramList2.add(result);
}
}
}
} }