refactor(property): 优化人员导入和人脸上传功能

This commit is contained in:
zcxlsm 2025-07-30 19:58:22 +08:00
parent f0beb7be5e
commit 79f98b5387
6 changed files with 87 additions and 66 deletions

View File

@ -37,7 +37,7 @@ public class ResidentPersonBo extends BaseEntity {
/**
* 联系电话
*/
@NotBlank(message = "联系电话不能为空", groups = {AddGroup.class, EditGroup.class})
// @NotBlank(message = "联系电话不能为空", groups = {AddGroup.class, EditGroup.class})
private String phone;
/**
* 人员类型
@ -53,7 +53,7 @@ public class ResidentPersonBo extends BaseEntity {
/**
* 证件号
*/
@NotBlank(message = "证件号不能为空", groups = {AddGroup.class, EditGroup.class})
// @NotBlank(message = "证件号不能为空", groups = {AddGroup.class, EditGroup.class})
private String idCard;
/**

View File

@ -57,33 +57,31 @@ public class ResidentPersonImportListener extends AnalysisEventListener<Resident
@Override
public void invoke(ResidentPersonImportVo personVo, AnalysisContext context) {
ResidentUnitVo unitVo = residentUnitService.queryById(unitId);
List<ResidentPersonVo> list = new ArrayList<>();
ResidentPersonVo person = new ResidentPersonVo();
// 判断证件号是否为空
if (StringUtils.isEmpty(personVo.getIdCard())) {
failureNum++;
failureMsg.append("<br/>").append(failureNum).append("、账号 ").append(personVo.getUserName()).append(" 证件号不能为空!");
} else {
ResidentPersonBo personBo = new ResidentPersonBo();
personBo.setUnitId(unitId);
personBo.setIdCard(personVo.getIdCard());
list = residentPersonService.queryList(personBo);
person = residentPersonService.queryByUnitIdAndName(unitId, person.getUserName());
}
try {
if (list.isEmpty()) { // 判断当前单位是否已存在该用户
if (person == null) { // 判断当前单位是否已存在该用户
ResidentPersonBo bo = BeanUtil.toBean(personVo, ResidentPersonBo.class);
ValidatorUtils.validate(bo);
bo.setState(1L);
bo.setUnitId(unitId);
bo.setTime(new Date());
bo.setUnitName(unitVo.getName());
bo.setUnitName(unitVo.getName().trim());
bo.setAuthGroupId(unitVo.getAuthGroupId());
bo.setAuthBegDate(unitVo.getAuthBegDate());
bo.setAuthEndDate(unitVo.getAuthEndDate());
bo.setUserName(personVo.getUserName().replaceAll("[\\s\u3000]", ""));
residentPersonService.insertByBo(bo);
successNum++;
successMsg.append("<br/>").append(successNum).append("、账号 ").append(bo.getUserName()).append(" 导入成功");
} else if (isUpdateSupport) {
Long id = list.get(0).getUserId();
Long id = person.getUserId();
ResidentPersonBo bo = BeanUtil.toBean(personVo, ResidentPersonBo.class);
bo.setId(id);
ValidatorUtils.validate(bo);
@ -93,7 +91,7 @@ public class ResidentPersonImportListener extends AnalysisEventListener<Resident
successMsg.append("<br/>").append(successNum).append("、账号 ").append(bo.getUserName()).append(" 更新成功");
} else {
failureNum++;
failureMsg.append("<br/>").append(failureNum).append("、账号 ").append(list.get(0).getUserName()).append(" 已存在");
failureMsg.append("<br/>").append(failureNum).append("、账号 ").append(personVo.getUserName()).append(" 已存在");
}
} catch (Exception e) {

View File

@ -69,7 +69,7 @@ public interface IResidentPersonService {
/**
* 获取单位人员数量
*
* @param unitId id
* @param unitId id
* @return Long
*/
Long queryPersonCount(Long unitId);
@ -80,4 +80,12 @@ public interface IResidentPersonService {
* @return List<ResidentPersonVo>
*/
List<ResidentPersonVo> queryUnAuthPerson();
/**
* 通过单位和姓名查询人员信息
*
* @param unitId 单位id
* @param name 姓名
*/
ResidentPersonVo queryByUnitIdAndName(Long unitId, String name);
}

View File

@ -169,16 +169,16 @@ public class ResidentPersonServiceImpl implements IResidentPersonService {
if (update.getAuthGroupId() != null && update.getAuthEndDate() != null && update.getImg() != null) {
ResidentPersonVo vo = queryById(update.getId());
Long e8Id = vo.getEEightId();
flag = baseMapper.updateById(update) > 0;
baseMapper.updateById(update);
// 显式移除e8id
LambdaUpdateWrapper<ResidentPerson> lqw = new LambdaUpdateWrapper<>();
lqw.eq(ResidentPerson::getId, update.getId())
.set(ResidentPerson::getEEightId, null);
baseMapper.update(lqw);
flag = baseMapper.update(lqw) > 0;
if (flag) {
if (flag && e8Id != null) {
log.info("开始修改授权记录, {}", bo.getUserName());
RemotePersonAuth personAuth = new RemotePersonAuth();
personAuth.setId(update.getId());
@ -224,7 +224,7 @@ public class ResidentPersonServiceImpl implements IResidentPersonService {
private void validEntityBeforeSave(ResidentPerson entity) {
//TODO 做一些数据校验,如唯一约束
LambdaQueryWrapper<ResidentPerson> lqw = Wrappers.lambdaQuery();
lqw.eq(ResidentPerson::getIdCard, entity.getIdCard())
lqw.eq(ResidentPerson::getIdCard, entity.getUserName())
.eq(ResidentPerson::getUnitId, entity.getUnitId());
boolean exists = baseMapper.exists(lqw);
Assert.isTrue(!exists, "当前单位,{}已入驻!", entity.getUserName());
@ -252,8 +252,11 @@ public class ResidentPersonServiceImpl implements IResidentPersonService {
.map(ResidentPersonVo::getEEightId)
.filter(Objects::nonNull)
.toList();
boolean auth = remoteSisAuth.deletePersonAuth(ids, e8Ids);
Assert.isTrue(auth, "删除授权记录失败!");
if (!e8Ids.isEmpty()) {
boolean auth = remoteSisAuth.deletePersonAuth(ids, e8Ids);
Assert.isTrue(auth, "删除授权记录失败!");
}
}
return baseMapper.deleteByIds(ids) > 0;
}
@ -284,4 +287,19 @@ public class ResidentPersonServiceImpl implements IResidentPersonService {
.isNull(ResidentPerson::getEEightId);
return baseMapper.selectVoList(lqw);
}
/**
* 通过单位和姓名查询人员信息
*
* @param unitId 单位id
* @param name 姓名
*/
@Override
public ResidentPersonVo queryByUnitIdAndName(Long unitId, String name) {
LambdaQueryWrapper<ResidentPerson> lqw = Wrappers.lambdaQuery();
lqw.eq(ResidentPerson::getUnitId, unitId)
.eq(ResidentPerson::getUserName, name);
List<ResidentPersonVo> list = baseMapper.selectVoList(lqw);
return list.isEmpty() ? null : list.get(0);
}
}

View File

@ -14,9 +14,8 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
@ -39,7 +38,7 @@ public class UploadFaceUtil {
// 安全配置参数实际项目中可以从配置文件读取
private static final int MAX_TOTAL_SIZE = 50 * 1024 * 1024; // 50MB 最大解压总大小
private static final int MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB 最大单个文件大小
private static final int MAX_FILE_COUNT = 30; // 最大文件数量
private static final int MAX_FILE_COUNT = 50; // 最大文件数量
private static final Map<String, String> CONTENT_TYPE_MAP = new HashMap<>();
static {
@ -59,7 +58,7 @@ public class UploadFaceUtil {
// 重置统计信息
resetStats();
try (ZipInputStream zis = new ZipInputStream(zipFile.getInputStream())) {
try (ZipInputStream zis = new ZipInputStream(zipFile.getInputStream(), StandardCharsets.UTF_8)) {
ZipEntry entry;
byte[] buffer = new byte[8192]; // 8KB缓冲区
long totalExtractedSize = 0;
@ -126,19 +125,16 @@ public class UploadFaceUtil {
bao.write(buffer, 0, len);
}
ResidentPersonBo bo = new ResidentPersonBo();
bo.setUnitId(unitId);
bo.setUserName(name);
List<ResidentPersonVo> personVos = residentPersonService.queryList(bo);
ResidentPersonVo personVo = residentPersonService.queryByUnitIdAndName(unitId, name);
// 判断当前姓名是否存在入驻单位
if (personVos.isEmpty()) continue;
if (personVo == null) continue;
byte[] imageData = bao.toByteArray();
RemoteFile remoteFile = remoteFileService.upload(name, name, contentType, imageData);
RemoteFile remoteFile = remoteFileService.upload(name, entry.getName(), contentType, imageData);
personVos.get(0).setImg(remoteFile.getOssId().toString());
ResidentPersonBo updateBo = BeanUtil.toBean(personVos.get(0), ResidentPersonBo.class);
personVo.setImg(remoteFile.getOssId().toString());
ResidentPersonBo updateBo = BeanUtil.toBean(personVo, ResidentPersonBo.class);
residentPersonService.updateByBo(updateBo);
totalFiles++;

View File

@ -61,7 +61,7 @@ public class AuthSyncTask {
/**
* 每两分钟执行一次
*/
@Scheduled(cron = "0 */2 * * * ?")
@Scheduled(cron = "0 */5 * * * ?")
public void autoAuth() {
AtomicReference<List<RemoteResidentPersonVo>> unAuthPersonRef = new AtomicReference<>(new ArrayList<>());
AtomicReference<byte[]> imgByteRef = new AtomicReference<>(new byte[0]);
@ -81,43 +81,44 @@ public class AuthSyncTask {
// 判断是否已存在授权
SisAuthRecordVo authRecord = sisAuthRecordService.queryByGroupIdAndPersonId(person.getAuthGroupId(), person.getId());
if (ObjectUtil.isEmpty(authRecord)) {
// 无授权记录时补录
log.info("无授权记录:{}", person.getId());
this.syncAuthRecord(person);
imgByteRef.set(remoteFileService.downloadToByteArray(Long.parseLong(person.getOssId())));
// 读取人像
byte[] imgByte = imgByteRef.get();
if (imgByte == null) continue;
// String nowMd5 = this.calculateMD5(imgByte);
// SisPersonLibImgVo imgVo = sisPersonLibImgService.queryByImgMd5(nowMd5);
//
// Long huaweiId;
// Boolean update;
// if (ObjectUtil.isEmpty(imgVo)) {
// // 写入华为盒子
// huaweiId = syncHuaweiBox(person, imgByte);
// } else {
// if (imgVo.getRemoteImgId() == null) {
// huaweiId = syncHuaweiBox(person, imgByte);
// } else {
// huaweiId = imgVo.getRemoteImgId();
// }
// }
// if (huaweiId == null) continue;
//
// // 更新人像信息huaweiBoxId
// update = sisPersonLibImgService.updateByPersonId(person.getId(), huaweiId, nowMd5);
// if (!update) continue;
// 同步E8平台
Long e8Id = syncE8Plat(person, imgByte);
if (e8Id == null) continue;
// 更新入驻员工E8平台id
remoteResidentPersonService.updateE8Id(person.getId(), e8Id);
}else{
log.info("已存在授权记录:{}", person.getId());
}
imgByteRef.set(remoteFileService.downloadToByteArray(Long.parseLong(person.getOssId())));
// 读取人像
byte[] imgByte = imgByteRef.get();
if (imgByte == null) continue;
String nowMd5 = this.calculateMD5(imgByte);
SisPersonLibImgVo imgVo = sisPersonLibImgService.queryByImgMd5(nowMd5);
Long huaweiId;
Boolean update;
if (ObjectUtil.isEmpty(imgVo)) {
// 写入华为盒子
huaweiId = syncHuaweiBox(person, imgByte);
} else {
if (imgVo.getRemoteImgId() == null) {
huaweiId = syncHuaweiBox(person, imgByte);
} else {
huaweiId = imgVo.getRemoteImgId();
}
}
if (huaweiId == null) continue;
// 更新人像信息huaweiBoxId
update = sisPersonLibImgService.updateByPersonId(person.getId(), huaweiId, nowMd5);
if (!update) continue;
// 同步E8平台
Long e8Id = syncE8Plat(person, imgByte);
if (e8Id == null) continue;
// 更新入驻员工E8平台id
remoteResidentPersonService.updateE8Id(person.getId(), e8Id);
}
} else {
log.info("无待授权人员");