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

@@ -8,6 +8,7 @@ import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.property.domain.vo.ResidentPersonImportVo;
import org.dromara.property.listener.ResidentPersonImportListener;
@@ -38,6 +39,7 @@ import org.springframework.web.multipart.MultipartFile;
* @author mocheng
* @since 2025-06-19
*/
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@@ -126,17 +128,17 @@ public class ResidentPersonController extends BaseController {
@SaCheckPermission("property:person:import")
@PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport, Long unitId) throws Exception {
ExcelResult<ResidentPersonImportVo> result = ExcelUtil.importExcel(file.getInputStream(), ResidentPersonImportVo.class, new ResidentPersonImportListener(updateSupport, unitId));
return R.ok(result.getAnalysis());
asyncImportExcel(StpUtil.getTokenValue(), file, updateSupport, unitId);
return R.ok("文件上传成功,请等待处理!");
}
@Async
public void asyncImportExcel(String tokenValue, MultipartFile file, Boolean updateSupport, Long unitId) {
try {
StpUtil.setTokenValueToStorage(tokenValue);
ExcelResult<ResidentPersonImportVo> result = ExcelUtil.importExcel(file.getInputStream(), ResidentPersonImportVo.class, new ResidentPersonImportListener(updateSupport, unitId));
ExcelUtil.importExcel(file.getInputStream(), ResidentPersonImportVo.class, new ResidentPersonImportListener(updateSupport, unitId));
} catch (Exception e) {
throw new RuntimeException(e);
log.info("处理导入入驻员工Excel文件时出错");
}
}
@@ -156,7 +158,17 @@ public class ResidentPersonController extends BaseController {
*/
@PostMapping(value = "/importFace", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public R<Void> importFace(@RequestPart("file") MultipartFile file, Long unitId) {
asyncImportFace(StpUtil.getTokenValue(), file, unitId);
return R.ok("文件上传成功,请等待处理!");
}
@Async
public void asyncImportFace(String tokenValue, MultipartFile file, Long unitId) {
try {
StpUtil.setTokenValueToStorage(tokenValue);
uploadFaceUtil.processFaceZip(file, unitId);
return R.ok();
} catch (Exception e) {
log.info("处理人脸压缩包时出错");
}
}
}

View File

@@ -126,30 +126,8 @@ public class ResidentPersonServiceImpl implements IResidentPersonService {
add.setAuthGroupId(ruVo.getAuthGroupId());
add.setAuthBegDate(ruVo.getAuthBegDate());
add.setAuthEndDate(ruVo.getAuthEndDate());
boolean flag = baseMapper.insert(add) > 0;
Assert.isTrue(flag, "员工入驻失败!");
// 存在图片时,才同步授权
if (flag && add.getImg() != null) {
log.info("开始写入授权记录, {}", bo.getUserName());
RemotePersonAuth personAuth = new RemotePersonAuth();
personAuth.setId(add.getId());
personAuth.setName(bo.getUserName());
personAuth.setSex(bo.getGender().intValue());
personAuth.setPhone(bo.getPhone());
personAuth.setEmail(bo.getEmail());
personAuth.setIdCardNumber(bo.getIdCard());
personAuth.setOssId(bo.getImg());
personAuth.setCarNumber(bo.getCarNumber());
// 使用公司权限组
personAuth.setAuthBegDate(ruVo.getAuthBegDate());
personAuth.setAuthEndDate(ruVo.getAuthEndDate());
personAuth.setAuthGroupId(ruVo.getAuthGroupId());
Boolean auth = remoteSisAuth.personAuth(personAuth);
Assert.isTrue(auth, "新增授权记录失败");
}
return flag;
}
@@ -180,22 +158,8 @@ public class ResidentPersonServiceImpl implements IResidentPersonService {
if (flag && e8Id != null) {
log.info("开始修改授权记录, {}", bo.getUserName());
RemotePersonAuth personAuth = new RemotePersonAuth();
personAuth.setId(update.getId());
personAuth.setName(update.getUserName());
personAuth.setSex(update.getGender().intValue());
personAuth.setPhone(update.getPhone());
personAuth.setEmail(update.getEmail());
personAuth.setIdCardNumber(update.getIdCard());
personAuth.setOssId(update.getImg());
personAuth.setCarNumber(update.getCarNumber());
personAuth.setE8Id(e8Id);
personAuth.setAuthGroupId(update.getAuthGroupId());
personAuth.setAuthBegDate(update.getAuthBegDate());
personAuth.setAuthEndDate(update.getAuthEndDate());
Boolean auth = remoteSisAuth.updatePersonAuth(personAuth);
// 先删除,定时任务增加
Boolean auth = remoteSisAuth.deletePersonAuth(List.of(update.getId()), List.of(e8Id));
Assert.isTrue(auth, "修改授权记录失败!");
}
@@ -256,7 +220,6 @@ public class ResidentPersonServiceImpl implements IResidentPersonService {
boolean auth = remoteSisAuth.deletePersonAuth(ids, e8Ids);
Assert.isTrue(auth, "删除授权记录失败!");
}
}
return baseMapper.deleteByIds(ids) > 0;
}

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,11 +360,15 @@ public class SisAuthRecordServiceImpl implements ISisAuthRecordService {
}
});
Collection<Long> imgIds = list.stream().map(SisPersonLibImgVo::getId).toList();
if (CollUtil.isNotEmpty(imgIds)){
flag = sisPersonLibImgService.deleteWithValidByIds(imgIds, false);
Assert.isTrue(flag, "删除人像库图片失败");
}
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,6 +175,9 @@ public class AuthSyncTask {
* @return Long
*/
private Long syncHuaweiBox(RemoteResidentPersonVo vo, byte[] imgByte) {
Long pId;
try {
log.info("开始写入华为平台");
AddHWPersonReq req = new AddHWPersonReq();
req.setIndex(CodePrefixConstants.PERSON_LIB_IMAGE_CODE_PREFIX + IdUtil.getSnowflakeNextIdStr());
@@ -170,9 +190,12 @@ public class AuthSyncTask {
pictures.add(Base64.getEncoder().encodeToString(imgByte));
req.setPictures(pictures);
Long pId = huaWeiBoxApi.addPerson(List.of(req));
pId = huaWeiBoxApi.addPerson(List.of(req));
Assert.notNull(pId, "调用华为盒子新增图片失败");
log.info("写入华为盒子完成pId={}", pId);
} catch (Exception e) {
return null;
}
return pId;
}
@@ -184,11 +207,16 @@ public class AuthSyncTask {
* @return Long
*/
private Long syncE8Plat(RemoteResidentPersonVo vo, byte[] imgByte) {
// 初始化步进器
int count = 0;
Long e8Id = null;
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();
@@ -196,9 +224,10 @@ public class AuthSyncTask {
req.setGender(vo.getGender() != 1L ? 0 : 1);
req.setIdentityType(0);
req.setIdentityNo(vo.getIdCard());
Long e8Id = e8PlatformApi.addCustomer(req).getId();
e8Id = e8PlatformApi.addCustomer(req).getId();
Assert.notNull(e8Id, "e8同步新建人员失败");
log.info("e8同步新建人员完成");
count++; // 新增人员完成步进器+1
log.info("e8平台开始发行凭证");
@@ -210,6 +239,7 @@ public class AuthSyncTask {
Long voucherId = e8PlatformApi.issueVoucher(voucherReq);
Assert.notNull(voucherId, "e8平台发行凭证失败");
log.info("e8平台发行凭证成功");
count++; // 发行凭证完成步进器+1
// 获取门禁
List<SisAuthGroupRefVo> refVos = sisAuthGroupRefService.queryListByGroupId(vo.getAuthGroupId());
@@ -236,8 +266,22 @@ public class AuthSyncTask {
Boolean flag = e8PlatformApi.addCustomerAuth(authReq);
Assert.isTrue(flag, "E8平台授权失败");
log.info("E8平台授权完成!");
count++; // 授权完成步进器+1
}
} 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;
}
}
/**

View File

@@ -69,6 +69,7 @@
<groupId>org.dromara</groupId>
<artifactId>ruoyi-api-system</artifactId>
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>sis-api</artifactId>
@@ -76,6 +77,12 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>property-api</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>
<build>

View File

@@ -0,0 +1,29 @@
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);
}
}