From 5ed5b1f5723a8b3862a48f006fe348176077bb9b Mon Sep 17 00:00:00 2001 From: PoffyZhang <99775271@qq.com> Date: Tue, 13 Jun 2023 11:48:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AF=8F=E6=97=A5=E6=9B=B4=E6=96=B0=E5=91=98?= =?UTF-8?q?=E5=B7=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ding/controller/DingInfoPullController.java | 5 ++ .../pmapi/ding/task/EmployeeBatchGetTask.java | 81 +++++++++++++++++++--- .../scheduler/task/BatchUpdateEmployeeTask.java | 57 +++++++++++++++ pmapi/src/main/resources/application.yml | 2 +- 4 files changed, 136 insertions(+), 9 deletions(-) create mode 100644 pmapi/src/main/java/com/ningdatech/pmapi/scheduler/task/BatchUpdateEmployeeTask.java diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/ding/controller/DingInfoPullController.java b/pmapi/src/main/java/com/ningdatech/pmapi/ding/controller/DingInfoPullController.java index 5e028c9..a164519 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/ding/controller/DingInfoPullController.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/ding/controller/DingInfoPullController.java @@ -43,4 +43,9 @@ public class DingInfoPullController { public void employeeBatchGetByRegionCode(@PathVariable String regionCode) { employeeBatchGetTask.batchGetEmployeeTaskByRegionCode(regionCode); } + + @GetMapping("/employeeByOrdCode/{orgCode}") + public void employeeBatchGetByOrdCode(@PathVariable String orgCode) { + employeeBatchGetTask.batchGetEmployeeTaskByOrdCode(orgCode); + } } diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/ding/task/EmployeeBatchGetTask.java b/pmapi/src/main/java/com/ningdatech/pmapi/ding/task/EmployeeBatchGetTask.java index bca3948..6ac9084 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/ding/task/EmployeeBatchGetTask.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/ding/task/EmployeeBatchGetTask.java @@ -5,7 +5,9 @@ import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.google.common.collect.Lists; +import com.ningdatech.basic.function.VUtils; import com.ningdatech.basic.model.GenericResult; +import com.ningdatech.pmapi.common.constant.BizConst; import com.ningdatech.pmapi.organization.model.entity.DingEmployeeInfo; import com.ningdatech.pmapi.organization.model.entity.DingOrganization; import com.ningdatech.pmapi.organization.service.IDingEmployeeInfoService; @@ -335,13 +337,76 @@ public class EmployeeBatchGetTask { } } -// if (saveList.size() <= GROUP_SIZE) { -// iDingEmployeeInfoService.saveBatch(saveList); -// } else { -// List> split = Lists.partition(saveList, GROUP_SIZE); -// for (List segment : split) { -// iDingEmployeeInfoService.saveBatch(segment); -// } -// } + public void batchGetEmployeeTaskByOrdCode(String orgCode) { + DingOrganization org = iDingOrganizationService.getOne(Wrappers.lambdaQuery(DingOrganization.class) + .eq(DingOrganization::getOrganizationCode, orgCode) + .last(BizConst.LIMIT_1)); + VUtils.isTrue(Objects.isNull(org)).throwMessage("单位不存在"); + log.info("所属单位 {} 要更新员工信息了",org.getOrganizationName()); + + List allOrganizationEmployeePositionList = new ArrayList<>(); + String organizationCode = orgCode; + PageOrganizationEmployeePositionsQuery query = new PageOrganizationEmployeePositionsQuery(); + query.setEmployeeStatus("A"); + query.setOrganizationCode(organizationCode); + query.setReturnTotalSize(true); + query.setTenantId(zwddIntegrationProperties.getTenantId()); + int pageNo = 1; + query.setPageNo(pageNo); + query.setPageSize(PAGE_SIZE); + + // 查询组织下 用户信息 + GenericResult> firstPageGenericResult = zwddClient.pageOrganizationEmployeePositions(query); + Page data = firstPageGenericResult.getData(); + if (Objects.isNull(data)){ + log.info("响应为空:{}",organizationCode); + return; + } + if (CollUtil.isNotEmpty(data.getData())) { + allOrganizationEmployeePositionList.addAll(data.getData()); + } + Long totalSize = data.getTotalSize(); + + log.info("dingOrganization :{}", JSON.toJSONString(org)); + + log.info("totalSize :{},{}", totalSize,org.getOrganizationName()); + if (totalSize > PAGE_SIZE) { + if (totalSize > MAX_SIZE) { + //超过1万 按1万的处理 + totalSize = MAX_SIZE.longValue(); + } + + int restPageNo = totalSize % PAGE_SIZE > 0 ? 1 : 0; + int maxPageNo = (int) Math.ceil(totalSize / PAGE_SIZE) + restPageNo; + for (pageNo = 2; pageNo <= maxPageNo; pageNo++) { + query.setPageNo(pageNo); + GenericResult> pageGenericResult = zwddClient.pageOrganizationEmployeePositions(query); +// log.info("pageGenericResult :{}", JSON.toJSONString(pageGenericResult)); + if(pageGenericResult.isSuccess()){ + allOrganizationEmployeePositionList.addAll(pageGenericResult.getData().getData()); + }else{ + log.error(pageGenericResult.getMsg()); + } + } + } + + // 批量查询 成员的accountId + List dingEmployeeInfoSaveRecordList = new ArrayList<>(); + if (allOrganizationEmployeePositionList.size() <= GROUP_SIZE) { + log.info("assemblerAccountId :{}", allOrganizationEmployeePositionList.size()); + assemblerAccountId(allOrganizationEmployeePositionList, dingEmployeeInfoSaveRecordList); + } else { + log.info("assemblerAccountId :{}", allOrganizationEmployeePositionList.size()); + List> split = Lists.partition(allOrganizationEmployeePositionList, GROUP_SIZE); + for (List segment : split) { + assemblerAccountId(segment, dingEmployeeInfoSaveRecordList); + } + } + // 批量保存用户信息 + saveBatch(dingEmployeeInfoSaveRecordList); + + log.info("员工同步完成"); + + } } diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/scheduler/task/BatchUpdateEmployeeTask.java b/pmapi/src/main/java/com/ningdatech/pmapi/scheduler/task/BatchUpdateEmployeeTask.java new file mode 100644 index 0000000..0aa9954 --- /dev/null +++ b/pmapi/src/main/java/com/ningdatech/pmapi/scheduler/task/BatchUpdateEmployeeTask.java @@ -0,0 +1,57 @@ +package com.ningdatech.pmapi.scheduler.task; + +import cn.hutool.core.date.StopWatch; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.ningdatech.pmapi.ding.task.EmployeeBatchGetTask; +import com.ningdatech.pmapi.organization.model.entity.DingOrganization; +import com.ningdatech.pmapi.organization.service.IDingOrganizationService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.List; + +/** + * @author liuxinxin + * @date 2023/1/12 上午9:53 + * 批量获取/更新部门信息任务 + */ +@Component +@Slf4j +@RequiredArgsConstructor +public class BatchUpdateEmployeeTask { + + @Value("${hostname}") + private String HOST_NAME; + + private final EmployeeBatchGetTask employeeBatchGetTask; + private final IDingOrganizationService organizationService; + + @Scheduled(cron = "0 45 1 * * ?") + public void doTask() throws UnknownHostException { + if (!HOST_NAME.equals(InetAddress.getLocalHost().getHostName())) { + return; + } + + log.info("=========== 每日更新 浙政钉员工信息 (根据现有单位) ======== 任务开始"); + StopWatch stopWatch = new StopWatch(); + stopWatch.start(); + // 1.查出浙政钉 所有单位 + List units = organizationService.list(Wrappers.lambdaQuery(DingOrganization.class) + .eq(DingOrganization::getTypeCode, "GOV_UNIT")); + + log.info("需要更新的 单位数量为 {}",units.size()); + + for(DingOrganization organization : units){ + log.info("当前单位是 {}",organization.getOrganizationName()); + employeeBatchGetTask.batchGetEmployeeTaskByOrdCode(organization.getOrganizationCode()); + } + + stopWatch.stop(); + log.info("=========== 每日更新 浙政钉员工信息 (根据现有单位) ======== 任务结束 {}s", stopWatch.getTotalTimeSeconds()); + } + +} diff --git a/pmapi/src/main/resources/application.yml b/pmapi/src/main/resources/application.yml index 3d7808a..9f96606 100644 --- a/pmapi/src/main/resources/application.yml +++ b/pmapi/src/main/resources/application.yml @@ -1,3 +1,3 @@ spring: profiles: - active: dev + active: prod