From ab7cb9a9753e4167a515bda98f8f6c7554ac5b62 Mon Sep 17 00:00:00 2001 From: zengweihai Date: Sat, 11 May 2024 15:18:14 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E9=97=A8=E6=9B=B4=E6=96=B0=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cxkg/task/HrmDepartmentTaskImpl.java | 250 ++++++++++++++++++ 1 file changed, 250 insertions(+) create mode 100644 shkd-cosmic-debug/src/main/java/shkd/cosmic/cxkg/task/HrmDepartmentTaskImpl.java diff --git a/shkd-cosmic-debug/src/main/java/shkd/cosmic/cxkg/task/HrmDepartmentTaskImpl.java b/shkd-cosmic-debug/src/main/java/shkd/cosmic/cxkg/task/HrmDepartmentTaskImpl.java new file mode 100644 index 0000000..d102fa8 --- /dev/null +++ b/shkd-cosmic-debug/src/main/java/shkd/cosmic/cxkg/task/HrmDepartmentTaskImpl.java @@ -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 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 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 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 addOrgList(List orgList,JSONObject data,Map 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 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 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 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); + // } +} \ No newline at end of file