refactor(property): 优化授权逻辑
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled

This commit is contained in:
2025-08-01 14:45:28 +08:00
parent 389ba82659
commit c366a30488
6 changed files with 177 additions and 118 deletions

View File

@@ -1,5 +1,6 @@
package org.dromara.sis.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.common.core.domain.TreeNode;
@@ -351,7 +352,6 @@ public class SisAuthRecordServiceImpl implements ISisAuthRecordService {
Assert.isTrue(flag, "删除授权记录失败");
if (flag) {
List<SisPersonLibImgVo> list = new ArrayList<>();
ids.forEach(id -> {
SisPersonLibImgVo imgVo = sisPersonLibImgService.queryByPersonId(id);
@@ -360,10 +360,14 @@ public class SisAuthRecordServiceImpl implements ISisAuthRecordService {
}
});
Collection<Long> imgIds = list.stream().map(SisPersonLibImgVo::getId).toList();
flag = sisPersonLibImgService.deleteWithValidByIds(imgIds, false);
Assert.isTrue(flag, "删除人像库图片失败");
if (CollUtil.isNotEmpty(imgIds)){
flag = sisPersonLibImgService.deleteWithValidByIds(imgIds, false);
Assert.isTrue(flag, "删除人像库图片失败");
}
e8Ids.forEach(e8PlatformApi::deleteCustomer);
if (CollUtil.isNotEmpty(e8Ids)){
e8Ids.forEach(e8PlatformApi::deleteCustomer);
}
}
return flag;
}

View File

@@ -61,7 +61,7 @@ public class AuthSyncTask {
/**
* 每两分钟执行一次
*/
@Scheduled(cron = "0 */5 * * * ?")
// @Scheduled(cron = "0 */5 * * * ?")
public void autoAuth() {
AtomicReference<List<RemoteResidentPersonVo>> unAuthPersonRef = new AtomicReference<>(new ArrayList<>());
AtomicReference<byte[]> imgByteRef = new AtomicReference<>(new byte[0]);
@@ -76,18 +76,22 @@ public class AuthSyncTask {
try {
if (CollUtil.isNotEmpty(unAuthPerson)) {
for (RemoteResidentPersonVo person : unAuthPerson) {
log.info("开始定时授权:{}", person.getId());
log.info("开始定时授权:{}----{}", person.getName(), person.getId());
// 判断是否已存在授权
SisAuthRecordVo authRecord = sisAuthRecordService.queryByGroupIdAndPersonId(person.getAuthGroupId(), person.getId());
if (ObjectUtil.isEmpty(authRecord)) {
log.info("无授权记录:{}", person.getId());
log.info("无授权记录:{}----{}", person.getName(), person.getId());
this.syncAuthRecord(person);
imgByteRef.set(remoteFileService.downloadToByteArray(Long.parseLong(person.getOssId())));
// 读取人像
byte[] imgByte = imgByteRef.get();
if (imgByte == null) continue;
if (imgByte == null) {
log.info("下载图片失败:{}-----{}", person.getName(), person.getId());
sisAuthRecordService.deleteByPersonIds(List.of(person.getId()), new ArrayList<>());
continue;
}
// String nowMd5 = this.calculateMD5(imgByte);
// SisPersonLibImgVo imgVo = sisPersonLibImgService.queryByImgMd5(nowMd5);
@@ -104,20 +108,33 @@ public class AuthSyncTask {
// huaweiId = imgVo.getRemoteImgId();
// }
// }
// if (huaweiId == null) continue;
//
// if (huaweiId == null){
// log.info("华为盒子人像上传失败:{}-----{}", person.getName(), person.getId());
// sisAuthRecordService.deleteByPersonIds(List.of(person.getId()), new ArrayList<>());
// continue;
// }
//
// // 更新人像信息huaweiBoxId
// update = sisPersonLibImgService.updateByPersonId(person.getId(), huaweiId, nowMd5);
// if (!update) continue;
// if (!update) {
// log.info("更新人像信息失败:{}-----{}", person.getName(), person.getId());
// sisAuthRecordService.deleteByPersonIds(List.of(person.getId()), new ArrayList<>());
// continue;
// }
// 同步E8平台
Long e8Id = syncE8Plat(person, imgByte);
if (e8Id == null) continue;
if (e8Id == null) {
log.info("E8平台授权失败{}-----{}", person.getName(), person.getId());
sisAuthRecordService.deleteByPersonIds(List.of(person.getId()), new ArrayList<>());
continue;
}
// 更新入驻员工E8平台id
remoteResidentPersonService.updateE8Id(person.getId(), e8Id);
}else{
log.info("已存在授权记录:{}", person.getId());
} else {
log.info("已存在授权记录:{}-----{}", person.getName(), person.getId());
}
}
} else {
@@ -158,21 +175,27 @@ public class AuthSyncTask {
* @return Long
*/
private Long syncHuaweiBox(RemoteResidentPersonVo vo, byte[] imgByte) {
log.info("开始写入华为平台");
AddHWPersonReq req = new AddHWPersonReq();
req.setIndex(CodePrefixConstants.PERSON_LIB_IMAGE_CODE_PREFIX + IdUtil.getSnowflakeNextIdStr());
req.setName(vo.getName());
req.setGender(vo.getGender() == 1L ? "0" : vo.getGender() == 2L ? "1" : "-1");
req.setCredentialType("0");
req.setCredentialNumber(vo.getIdCard());
ArrayList<String> pictures = new ArrayList<>();
pictures.add(Base64.getEncoder().encodeToString(imgByte));
req.setPictures(pictures);
Long pId;
try {
log.info("开始写入华为平台");
AddHWPersonReq req = new AddHWPersonReq();
req.setIndex(CodePrefixConstants.PERSON_LIB_IMAGE_CODE_PREFIX + IdUtil.getSnowflakeNextIdStr());
req.setName(vo.getName());
req.setGender(vo.getGender() == 1L ? "0" : vo.getGender() == 2L ? "1" : "-1");
req.setCredentialType("0");
req.setCredentialNumber(vo.getIdCard());
Long pId = huaWeiBoxApi.addPerson(List.of(req));
Assert.notNull(pId, "调用华为盒子新增图片失败");
log.info("写入华为盒子完成pId={}", pId);
ArrayList<String> pictures = new ArrayList<>();
pictures.add(Base64.getEncoder().encodeToString(imgByte));
req.setPictures(pictures);
pId = huaWeiBoxApi.addPerson(List.of(req));
Assert.notNull(pId, "调用华为盒子新增图片失败");
log.info("写入华为盒子完成pId={}", pId);
} catch (Exception e) {
return null;
}
return pId;
}
@@ -184,60 +207,81 @@ public class AuthSyncTask {
* @return Long
*/
private Long syncE8Plat(RemoteResidentPersonVo vo, byte[] imgByte) {
// 初始化步进器
int count = 0;
Long e8Id = null;
log.info("e8平台上传照片");
String e8ImgUrl = e8PlatformApi.uploadFace(imgByte);
Assert.notNull(e8ImgUrl, "图片上传E8平台失败");
log.info("e8平台上传照片完成");
try {
log.info("e8平台上传照片");
String e8ImgUrl = e8PlatformApi.uploadFace(imgByte);
Assert.notNull(e8ImgUrl, "图片上传E8平台失败");
log.info("e8平台上传照片完成");
count++; // 图片上传完成步进器+1
log.info("e8同步新建人员");
CustomAddReq req = new CustomAddReq();
req.setName(vo.getName());
req.setGender(vo.getGender() != 1L ? 0 : 1);
req.setIdentityType(0);
req.setIdentityNo(vo.getIdCard());
Long e8Id = e8PlatformApi.addCustomer(req).getId();
Assert.notNull(e8Id, "e8同步新建人员失败");
log.info("e8同步新建人员完成");
log.info("e8同步新建人员");
CustomAddReq req = new CustomAddReq();
req.setName(vo.getName());
req.setGender(vo.getGender() != 1L ? 0 : 1);
req.setIdentityType(0);
req.setIdentityNo(vo.getIdCard());
e8Id = e8PlatformApi.addCustomer(req).getId();
Assert.notNull(e8Id, "e8同步新建人员失败");
log.info("e8同步新建人员完成");
count++; // 新增人员完成步进器+1
log.info("e8平台开始发行凭证");
IssueVoucherReq voucherReq = new IssueVoucherReq();
voucherReq.setVoucherType(70);
voucherReq.setPersonID(e8Id);
voucherReq.setTxtData(e8ImgUrl);
voucherReq.setCardType(34);
Long voucherId = e8PlatformApi.issueVoucher(voucherReq);
Assert.notNull(voucherId, "e8平台发行凭证失败");
log.info("e8平台发行凭证成功");
log.info("e8平台开始发行凭证");
IssueVoucherReq voucherReq = new IssueVoucherReq();
voucherReq.setVoucherType(70);
voucherReq.setPersonID(e8Id);
voucherReq.setTxtData(e8ImgUrl);
voucherReq.setCardType(34);
Long voucherId = e8PlatformApi.issueVoucher(voucherReq);
Assert.notNull(voucherId, "e8平台发行凭证失败");
log.info("e8平台发行凭证成功");
count++; // 发行凭证完成步进器+1
// 获取门禁
List<SisAuthGroupRefVo> refVos = sisAuthGroupRefService.queryListByGroupId(vo.getAuthGroupId());
Collection<Long> deviceIds = refVos.stream().filter(ref -> ref.getDeviceType() == 1).map(SisAuthGroupRefVo::getDeviceId).toList();
if (CollUtil.isNotEmpty(deviceIds)) {
// 初始化赋值
CustomerAuthAddReq authReq = new CustomerAuthAddReq();
authReq.setPersonIds(List.of(e8Id));
authReq.setStartTime(DateUtil.format(vo.getAuthBegDate(), "yyyy-MM-dd HH:mm:ss"));
authReq.setEndTime(DateUtil.format(vo.getAuthEndDate(), "yyyy-MM-dd HH:mm:ss"));
List<CustomerAuthAddReq.AuthGroupData> list = new ArrayList<>();
SisAccessControlVo accessControlVo;
for (Long deviceId : deviceIds) {
accessControlVo = sisAccessControlService.queryById(deviceId);
CustomerAuthAddReq.AuthGroupData authData = new CustomerAuthAddReq.AuthGroupData();
authData.setId(Long.parseLong(accessControlVo.getOutDoorCode()));
authData.setType(0);
authData.setGatewayType(1);
list.add(authData);
// 获取门禁
List<SisAuthGroupRefVo> refVos = sisAuthGroupRefService.queryListByGroupId(vo.getAuthGroupId());
Collection<Long> deviceIds = refVos.stream().filter(ref -> ref.getDeviceType() == 1).map(SisAuthGroupRefVo::getDeviceId).toList();
if (CollUtil.isNotEmpty(deviceIds)) {
// 初始化赋值
CustomerAuthAddReq authReq = new CustomerAuthAddReq();
authReq.setPersonIds(List.of(e8Id));
authReq.setStartTime(DateUtil.format(vo.getAuthBegDate(), "yyyy-MM-dd HH:mm:ss"));
authReq.setEndTime(DateUtil.format(vo.getAuthEndDate(), "yyyy-MM-dd HH:mm:ss"));
List<CustomerAuthAddReq.AuthGroupData> list = new ArrayList<>();
SisAccessControlVo accessControlVo;
for (Long deviceId : deviceIds) {
accessControlVo = sisAccessControlService.queryById(deviceId);
CustomerAuthAddReq.AuthGroupData authData = new CustomerAuthAddReq.AuthGroupData();
authData.setId(Long.parseLong(accessControlVo.getOutDoorCode()));
authData.setType(0);
authData.setGatewayType(1);
list.add(authData);
}
authReq.setAuthData(list);
log.info("e8平台开始授权");
Boolean flag = e8PlatformApi.addCustomerAuth(authReq);
Assert.isTrue(flag, "E8平台授权失败");
log.info("E8平台授权完成!");
count++; // 授权完成步进器+1
}
authReq.setAuthData(list);
log.info("e8平台开始授权");
Boolean flag = e8PlatformApi.addCustomerAuth(authReq);
Assert.isTrue(flag, "E8平台授权失败");
log.info("E8平台授权完成!");
} catch (Exception e) {
return null;
}
if (count == 4) {
// 授权完成返回e8平台人员Id
return e8Id;
} else if (count >= 2 && count < 4) {
// 人员新建完成,授权失败,删除人员
e8PlatformApi.deleteCustomer(e8Id);
return null;
} else {
return null;
}
return e8Id;
}
/**