refactor(sis): 定时授权任务通过snail-job触发
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run

This commit is contained in:
2025-08-02 15:50:40 +08:00
parent c366a30488
commit f7cdf4bc09
9 changed files with 550 additions and 409 deletions

View File

@@ -83,6 +83,12 @@
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>ruoyi-api-resource</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>
<build>

View File

@@ -1,29 +0,0 @@
package org.dromara.job.snailjob.sis;
import com.aizuda.snailjob.client.job.core.annotation.JobExecutor;
import com.aizuda.snailjob.client.job.core.dto.JobArgs;
import com.aizuda.snailjob.client.model.ExecuteResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.property.api.RemoteResidentPersonService;
import org.dromara.property.api.domain.vo.RemoteResidentPersonVo;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author lsm
* @apiNote AuthSyncTask
* @since 2025/8/1
*/
@Component
@JobExecutor(name = "authSyncTask")
public class AuthSyncTask {
@DubboReference
private RemoteResidentPersonService remoteResidentPersonService;
public ExecuteResult jobExecute(JobArgs jobArgs) throws InterruptedException {
List<RemoteResidentPersonVo> unAuthPerson = remoteResidentPersonService.queryUnAuthPerson();
return ExecuteResult.success(unAuthPerson);
}
}

View File

@@ -0,0 +1,243 @@
package org.dromara.job.snailjob.sis;
import cn.dev33.satoken.context.mock.SaTokenContextMockUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import com.aizuda.snailjob.client.job.core.annotation.JobExecutor;
import com.aizuda.snailjob.client.job.core.dto.JobArgs;
import com.aizuda.snailjob.client.model.ExecuteResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.property.api.RemoteResidentPersonService;
import org.dromara.property.api.domain.vo.RemoteResidentPersonVo;
import org.dromara.resource.api.RemoteFileService;
import org.dromara.sis.api.RemoteSisAuthService;
import org.dromara.sis.api.domain.RemotePersonAuth;
import org.springframework.stereotype.Component;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
/**
* @author lsm
* @apiNote SyncGrantAuthTask
* @since 2025/8/1
*/
@Slf4j
@Component
@JobExecutor(name = "syncGrantAuthTask")
public class SyncGrantAuthTask {
@DubboReference
private RemoteFileService remoteFileService;
@DubboReference
private RemoteSisAuthService remoteSisAuthService;
@DubboReference
private RemoteResidentPersonService remoteResidentPersonService;
public ExecuteResult jobExecute(JobArgs jobArgs) throws InterruptedException {
AtomicReference<List<RemoteResidentPersonVo>> unAuthPersonRef = new AtomicReference<>(new ArrayList<>());
AtomicReference<byte[]> imgByteRef = new AtomicReference<>(new byte[0]);
// 需要先设置模拟上下文
SaTokenContextMockUtil.setMockContext(() -> {
// 模拟登录
StpUtil.login(1);
unAuthPersonRef.set(remoteResidentPersonService.queryUnAuthPerson());
List<RemoteResidentPersonVo> unAuthPerson = unAuthPersonRef.get();
try {
if (CollUtil.isNotEmpty(unAuthPerson)) {
for (RemoteResidentPersonVo person : unAuthPerson) {
log.info("开始定时授权:{}----{}", person.getName(), person.getId());
// 判断是否存在授权
Boolean auth = remoteSisAuthService.queryPersonAuth(person.getAuthGroupId(), person.getId());
if (!auth) {
log.info("无授权记录:{}----{}", person.getName(), person.getId());
// 补录授权记录、人像信息
Boolean flag = syncAuthRecord(person);
if (!flag) {
log.info("补录授权记录、人像信息失败:{}----{}", person.getName(), person.getId());
continue;
}
// 下载人像
byte[] imgByte;
try {
imgByteRef.set(remoteFileService.downloadToByteArray(Long.parseLong(person.getOssId())));
imgByte = imgByteRef.get();
} catch (Exception e) {
log.info("下载图片失败:{}----{}", person.getName(), person.getId());
remoteSisAuthService.deletePersonAuth(List.of(person.getId()), new ArrayList<>());
continue;
}
// 计算图片MD5
String nowMd5 = calculateMD5(imgByte);
// 通过MD5查询相同图片
Long remoteId = remoteSisAuthService.queryImgIdByImgMd5(nowMd5);
Long huaweiId;
if (remoteId == null) {
// 当前本地人像信息不存在相同照片,直接上传华为盒子
huaweiId = syncHuaweiBox(person, imgByte);
} else {
huaweiId = remoteId;
}
if (huaweiId == null) {
log.info("华为盒子人像上传失败:{}-----{}", person.getName(), person.getId());
remoteSisAuthService.deletePersonAuth(List.of(person.getId()), new ArrayList<>());
continue;
}
Boolean update = false;
try {
update = remoteSisAuthService.updateImgByPersonId(person.getId(), huaweiId, nowMd5);
} catch (Exception e) {
remoteSisAuthService.deletePersonAuth(List.of(person.getId()), new ArrayList<>());
}
if (!update) {
log.info("更新人像信息失败:{}-----{}", person.getName(), person.getId());
remoteSisAuthService.deletePersonAuth(List.of(person.getId()), new ArrayList<>());
continue;
}
// 同步人像到E8平台
Long e8Id = syncE8Plat(person, imgByte);
if (e8Id == null) {
log.info("同步人像到E8平台失败{}-----{}", person.getName(), person.getId());
remoteSisAuthService.deletePersonAuth(List.of(person.getId()), new ArrayList<>());
continue;
}
// 更新入驻员工E8平台id
remoteResidentPersonService.updateE8Id(person.getId(), e8Id);
} else {
// 存在授权记录,为了避免重复授权,删除授权记录
remoteSisAuthService.deletePersonAuth(List.of(person.getId()), new ArrayList<>());
}
}
} else {
log.info("无待授权人员");
}
} catch (Exception e) {
log.info("同步授权异常");
throw new RuntimeException(e);
}
});
return ExecuteResult.success();
}
/**
* 补录授权记录
*
* @param person bean
*/
private Boolean syncAuthRecord(RemoteResidentPersonVo person) {
log.info("开始补录授权记录、人像信息:{}----{}", person.getName(), person.getId());
RemotePersonAuth personAuth = getRemotePersonAuth(person);
Boolean flag;
try {
flag = remoteSisAuthService.personAuth(personAuth);
if (flag) {
log.info("补录授权记录、人像信息完成:{}----{}", person.getName(), person.getId());
}
} catch (Exception e) {
return false;
}
return flag;
}
/**
* 实体类转换
*/
private RemotePersonAuth getRemotePersonAuth(RemoteResidentPersonVo person) {
RemotePersonAuth personAuth = new RemotePersonAuth();
personAuth.setId(person.getId());
personAuth.setOssId(person.getOssId());
personAuth.setName(person.getName());
personAuth.setPhone(person.getPhone());
personAuth.setSex(person.getGender().intValue());
personAuth.setIdCardNumber(person.getIdCard());
personAuth.setAuthGroupId(person.getAuthGroupId());
personAuth.setAuthBegDate(person.getAuthBegDate());
personAuth.setAuthEndDate(person.getAuthEndDate());
return personAuth;
}
/**
* 同步华为盒子
*
* @param vo bean
* @param imgByte 图片字节
* @return Long
*/
private Long syncHuaweiBox(RemoteResidentPersonVo vo, byte[] imgByte) {
log.info("开始写入华为盒子");
RemotePersonAuth personAuth = getRemotePersonAuth(vo);
Long pId = remoteSisAuthService.syncHuaweiBox(personAuth, imgByte);
if (pId != null) {
log.info("写入华为盒子完成pId={}", pId);
}
return pId;
}
/**
* 同步E8平台
*
* @param vo bean
* @param imgByte 图片字节
* @return Long
*/
private Long syncE8Plat(RemoteResidentPersonVo vo, byte[] imgByte) {
log.info("开始写入E8平台");
RemotePersonAuth personAuth = getRemotePersonAuth(vo);
Long e8Id = remoteSisAuthService.syncE8Plat(personAuth, imgByte);
if (e8Id != null) {
log.info("写入E8平台完成e8Id={}", e8Id);
}
return e8Id;
}
/**
* 直接计算字节数组的MD5值
*
* @param data 图片的字节数组
* @return 32位小写MD5字符串
*/
public String calculateMD5(byte[] data) throws Exception {
if (data == null) {
throw new IllegalArgumentException("输入数据不能为null");
}
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(data);
return bytesToHex(md.digest());
}
/**
* 字节数组转十六进制字符串
*
* @param bytes 字节数组
* @return 32位十六进制字符串
*/
private String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
// %02x 表示两位小写十六进制不足两位补0
sb.append(String.format("%02x", b));
}
return sb.toString();
}
}