部门更新接口

This commit is contained in:
zengweihai 2024-05-11 15:18:14 +08:00
parent b9ce6f6dfd
commit ab7cb9a975
1 changed files with 250 additions and 0 deletions

View File

@ -0,0 +1,250 @@
package shkd.cosmic.cxkg.task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.exception.KDException;
import kd.bos.org.model.OrgDutyView;
import kd.bos.org.model.OrgParam;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.schedule.executor.AbstractTask;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.bos.servicehelper.org.OrgUnitServiceHelper;
import kd.bos.servicehelper.org.OrgViewType;
import kd.sdk.plugin.Plugin;
import shkd.cosmic.cxkg.task.DTO.Query;
import shkd.cosmic.cxkg.task.DTO.QueryDTO;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
/**
* 后台任务插件
*/
public class HrmDepartmentTaskImpl extends AbstractTask implements Plugin {
private static Logger logger = LoggerFactory.getLogger(HrmDepartmentTaskImpl.class);
private static int PAGESIZE = 20;
private int CURPAGE = 1;
static boolean flag = false; //判断上级部门而设置默认部门的情况是否存在
/**
* 预先取部分数据进行测试构建组织单元
* @param requestContext
* @param map
* @throws KDException
*/
@Override
public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
String oaUrl = "10.157.226.9:8088";//暂时指定接口地址
// String oaUrl = System.getProperty("OaUrl");//:10.157.226.9:8088
//初始化入参
Object isFull = map.get("isfull");//判断是否全量
//构造请求体
Query query = new Query();
QueryDTO queryDTO = new QueryDTO();
queryDTO.setCurpage(CURPAGE);
queryDTO.setPagesize(PAGESIZE);
query.setQueryDTO(queryDTO);
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());
}
DynamicObjectCollection queryOrges = QueryServiceHelper.query("bos_adminorg", "id,number,shkd_oaid", null);//查询星瀚系统部门集合
Map<Object,Long> orgIds = new HashMap();//存放系统中已经存在的用户id集合
for (DynamicObject queryOrg : queryOrges) {
orgIds.put(queryOrg.getString("shkd_oaid"),queryOrg.getLong("id"));//存放已经存在的部门的id以及oaid
}
JSONObject data = this.getOADepartment(query, oaUrl);
if (data != null){
List<OrgParam> orgList = new ArrayList<>();//存放
BigDecimal totalSize = data.getBigDecimal("totalSize");//获取查询到部门总数
BigDecimal size = new BigDecimal(PAGESIZE);//设置页大小为20
BigDecimal pageNumber = totalSize.divide(size, 0, RoundingMode.UP);//计算需要分页数量
//查询成功取第一轮数据添加入参数集合
this.addOrgList(orgList, data, orgIds);
//判断分页数量是否大于1是则进行遍历后续数据
if (pageNumber.compareTo(new BigDecimal(1)) > 0){//当页数为1时不需要再次查询接口直接去同步数据
for (int i = 1; i < pageNumber.intValue(); i++){//查询成功取第一轮数据同步
//修改CURPAGE当前页数
CURPAGE = i + 1;
JSONObject dataNext = this.getOADepartment(query, oaUrl);//获取查询的数据
this.addOrgList(orgList, dataNext, orgIds);
}
}
OrgUnitServiceHelper.addOrUpdate(orgList);//新增或更新全量数据
for (OrgParam orgParam : orgList) {
boolean success = orgParam.isSuccess();
if (!success){
String name = orgParam.getName();
String msg = orgParam.getMsg();
logger.info(name+"数据同步失败:"+msg);//返回失败结果
}
}
if(flag){//重新生成构造集合
orgList.clear();
orgIds.clear();
queryOrges = QueryServiceHelper.query("bos_adminorg", "id,number,shkd_oaid", null);//查询星瀚系统部门集合
for (DynamicObject queryOrg : queryOrges) {
orgIds.put(queryOrg.getString("shkd_oaid"),queryOrg.getLong("id"));//存放已经存在的部门的id以及oaid
}
for (int i = 1; i < pageNumber.intValue(); i++){//查询成功取第一轮数据同步
JSONObject dataNext = this.getOADepartment(query, oaUrl);//获取查询的数据
this.addOrgList(orgList, dataNext, orgIds);
}
OrgUnitServiceHelper.addOrUpdate(orgList);//新增或更新全量数据
for (OrgParam orgParam : orgList) {
boolean success = orgParam.isSuccess();
if (!success){
String name = orgParam.getName();
String msg = orgParam.getMsg();
logger.info(name+"数据同步失败:"+msg);//返回失败结果
}
}
}
}
}
/**
* 调用oa接口返回数据
* @param query
* @param oaUrl
* @return
*/
private JSONObject getOADepartment(Query query,String oaUrl){
String requestBody = JSON.toJSONString(query);
oaUrl = "http://"+oaUrl+"/api/hrm/resful/getHrmdepartmentWithPage";
//返回参数
String json = HttpRequest.post(oaUrl).body(requestBody).execute().body();
JSONObject jsonObject = JSONObject.parseObject(json);//查询oa接口返回数据
String code = jsonObject.getString("code");//第一次执行查询判断接口调用是否成功否则不执行后续逻辑
if (!Objects.equals("1",code)){//当code不为1时调用失败
logger.info("调用接口失败"+jsonObject.getString("msg"));
return null;
}
JSONObject data = jsonObject.getJSONObject("data");
return data ;
}
/**
*填充组织构造集合
* @param orgList
* @param data
* @param orgIds
* @return
*/
private List<OrgParam> addOrgList(List<OrgParam> orgList,JSONObject data,Map<Object,Long> orgIds){
JSONArray dataList = data.getJSONArray("dataList");
for (int i = 0; i < dataList.size(); i++){
JSONObject item = dataList.getJSONObject(i);//单个部门数据
OrgParam orgParam = builderOrg(item, orgIds);
if(orgParam != null){
orgList.add(orgParam);
}
}
return orgList;
}
/**
* 构建组织参数类
* @param item
* @param orgIds
* @return
*/
private OrgParam builderOrg(JSONObject item , Map<Object,Long> orgIds) {
OrgParam orgParam = new OrgParam();
String id = item.getString("id");
if(id == null || id == ""){//当id不存在时该数据不合法方法结束
return null;//结束
}
//设置自定义id(设置新增组织id)及id(为空则新增其他则更新)
Long depId = orgIds.get(id);
if (depId != null){
orgParam.setId(depId);
orgParam.setCustomOrgId(depId);
}
// 设置组织属性
orgParam.setNumber(item.getString("departmentcode"));
orgParam.setName(item.getString("departmentname"));
orgParam.setSimpleName(item.getString("departmentmark"));
orgParam.setDuty(OrgViewType.Admin);//设置组织职能
//二开属性设置
Map<String, Object> proMap = new HashMap<>();
proMap.put("shkd_oaid",id);
orgParam.setPropertyMap(proMap);
//上级部门设置
String supDepId = item.getString("supdepid");//获取oa数据的上级部门id
if (supDepId == null || supDepId.equals("0")){
TreeMap<String, OrgDutyView> multiViewMap = new TreeMap<>();
OrgDutyView dutyView = new OrgDutyView();
multiViewMap.put(OrgViewType.Admin, dutyView);
multiViewMap.put(OrgViewType.OrgUnit, dutyView);
orgParam.setMultiViewMap(multiViewMap);
}else {
Long pk = orgIds.get(supDepId);//获取上级部门在星瀚的id
if (pk != null){
orgParam.setParentId(pk);
}else{
orgParam.setParentId(100000);
flag = true; //修改标志为true
}
}
return orgParam;
}
// //数据服务接口修改数据
// OperateOption option = OperateOption.create();
// JSONObject item = dataList.getJSONObject(i);
// String id = item.getString("id");//oa部门id
// String departmentCode = item.getString("departmentcode");//部门编码
// String departmentName = item.getString("departmentname");// 全称
// String departmentMark = item.getString("departmentmark");// 简称
// String canceled = item.getString("canceled");// 封存标志1 封存其他为未封存 对应星瀚使用状态
// String supDepId = item.getString("supdepid");// 上级部门oaid
// Date created = item.getDate("created");// 创建时间戳
// Date modified = item.getDate("modified");// 修改时间戳
// //String subCompanyId1 = item.getString("subcompanyid1");// 分部id
// //String showOrder = item.getString("showorder");//排序
//
// QFilter qFilter = new QFilter("number", QCP.equals,departmentCode);
// DynamicObject bos_adminorg = BusinessDataServiceHelper.loadSingle("bos_adminorg", qFilter.toArray());
// if (bos_adminorg != null){//查找到晨曦公司修改其数据
// bos_adminorg.set("shkd_oaid",id);
// bos_adminorg.set("number",departmentCode);
// bos_adminorg.set("name",departmentName);
// bos_adminorg.set("simplename",departmentMark);
// bos_adminorg.set("enable",canceled.equals("1") ? "0":"1");
// bos_adminorg.set("createtime",created);
// bos_adminorg.set("modifytime",modified);
// OperationServiceHelper.executeOperate("save", "bos_adminorg", new DynamicObject[]{bos_adminorg}, option);
// }
}