refactor(sis): 定时任务逻辑

This commit is contained in:
2025-07-26 17:53:57 +08:00
parent d070a52487
commit 127336af6d
18 changed files with 293 additions and 125 deletions

View File

@@ -1,14 +1,39 @@
package org.dromara.sis.config.timer;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.common.core.constant.CodePrefixConstants;
import org.dromara.common.tenant.helper.TenantHelper;
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.domain.RemotePersonAuth;
import org.dromara.sis.domain.bo.SisPersonLibImgBo;
import org.dromara.sis.domain.vo.*;
import org.dromara.sis.sdk.e8.E8PlatformApi;
import org.dromara.sis.sdk.e8.domain.accessControl.req.CustomerAuthAddReq;
import org.dromara.sis.sdk.e8.domain.custom.req.CustomAddReq;
import org.dromara.sis.sdk.e8.domain.voucher.req.IssueVoucherReq;
import org.dromara.sis.sdk.huawei.HuaWeiBoxApi;
import org.dromara.sis.sdk.huawei.domain.AddHWPersonReq;
import org.dromara.sis.service.ISisAccessControlService;
import org.dromara.sis.service.ISisAuthGroupRefService;
import org.dromara.sis.service.ISisAuthRecordService;
import org.dromara.sis.service.ISisPersonLibImgService;
import org.dromara.sis.service.impl.SisPersonLibImgServiceImpl;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
import java.util.List;
/**
@@ -19,22 +44,159 @@ import java.util.List;
@Slf4j
@Configuration
@EnableScheduling
@RequiredArgsConstructor
public class AuthTimer {
@DubboReference
private RemoteFileService remoteFileService;
@DubboReference
private RemoteResidentPersonService remoteResidentPersonService;
private final HuaWeiBoxApi huaWeiBoxApi;
private final E8PlatformApi e8PlatformApi;
private final ISisAuthRecordService sisAuthRecordService;
private final ISisPersonLibImgService sisPersonLibImgService;
private final ISisAuthGroupRefService sisAuthGroupRefService;
private final ISisAccessControlService sisAccessControlService;
/**
* 每两分钟执行一次
*/
@Scheduled(cron = "0 */2 * * * ?")
public void autoAuth(){
List<RemoteResidentPersonVo> unAuthPerson = remoteResidentPersonService.queryUnAuthPerson();
if (CollUtil.isNotEmpty(unAuthPerson)){
for(RemoteResidentPersonVo person : unAuthPerson){
log.info("开始授权:{}", person.getId());
}
@Scheduled(cron = "0 */1 * * * ?")
public void autoAuth() {
// TenantHelper.setDynamic("000000", true);
List<RemoteResidentPersonVo> unAuthPerson = remoteResidentPersonService.queryUnAuthPerson();
// if (CollUtil.isNotEmpty(unAuthPerson)) {
//
// try {
// for (RemoteResidentPersonVo person : unAuthPerson) {
// log.info("开始定时授权:{}", person.getId());
//
// // 判断是否已存在授权
// SisAuthRecordVo authRecord = sisAuthRecordService.queryByGroupIdAndPersonId(person.getAuthGroupId(), person.getId());
// if (ObjectUtil.isEmpty(authRecord)) {
// // 无授权记录时,补录
// this.syncAuthRecord(person);
// }
//
// // 读取人像
// byte[] imgByte = remoteFileService.downloadToByteArray(Long.parseLong(person.getOssId()));
// if (imgByte == null) continue;
//
// // 写入华为盒子
// Long huaweiId = syncHuaweiBox(person, imgByte);
// if (huaweiId == null) continue;
//
// // 更新人像信息huaweiBoxId
// Boolean update = sisPersonLibImgService.updateByPersonId(person.getId(), huaweiId);
// if (!update) continue;
//
// // 同步E8平台
// Long e8Id = syncE8Plat(person, imgByte);
// if (e8Id == null) continue;
//
// // 更新入驻员工E8平台id
// remoteResidentPersonService.updateE8Id(person.getId(), e8Id);
//
// }
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
//
// }
}
private void syncAuthRecord(RemoteResidentPersonVo person) {
log.info("开始补录授权记录、人像信息");
RemotePersonAuth personAuth = new RemotePersonAuth();
personAuth.setId(person.getId());
personAuth.setOssId(person.getOssId());
personAuth.setName(person.getName());
personAuth.setSex(person.getGender().intValue());
personAuth.setIdCardNumber(person.getIdCard());
personAuth.setAuthGroupId(person.getAuthGroupId());
personAuth.setAuthBegDate(person.getAuthBegDate());
personAuth.setAuthEndDate(person.getAuthEndDate());
sisAuthRecordService.insertByPerson(personAuth);
log.info("补录授权记录、人像信息完成");
}
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 = huaWeiBoxApi.addPerson(List.of(req));
Assert.notNull(pId, "调用华为盒子新增图片失败");
log.info("写入华为盒子完成pId={}", pId);
return pId;
}
private Long syncE8Plat(RemoteResidentPersonVo vo, byte[] imgByte) throws Exception {
log.info("e8平台上传照片");
String e8ImgUrl = e8PlatformApi.uploadFace(imgByte);
Assert.notNull(e8ImgUrl, "图片上传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());
Long e8Id = e8PlatformApi.addCustomer(req).getId();
Assert.notNull(e8Id, "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平台发行凭证成功");
// 获取门禁
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平台授权完成!");
}
return e8Id;
}
}

View File

@@ -11,7 +11,7 @@ import java.io.Serial;
* 权限组⇄设备关联对象 sis_auth_group_ref
*
* @author lsm
* @date 2025-07-23
* @sincex 2025-07-23
*/
@Data
@EqualsAndHashCode(callSuper = true)
@@ -30,7 +30,7 @@ public class SisAuthGroupRef extends TenantEntity {
/**
* 权限组id
*/
private Long groupId;
private Long authGroupId;
/**
* 设备id门禁/电梯)

View File

@@ -13,7 +13,7 @@ import jakarta.validation.constraints.*;
* 权限组⇄设备关联业务对象 sis_auth_group_ref
*
* @author lsm
* @date 2025-07-23
* @since 2025-07-23
*/
@Data
@EqualsAndHashCode(callSuper = true)
@@ -30,7 +30,7 @@ public class SisAuthGroupRefBo extends BaseEntity {
* 权限组id
*/
@NotNull(message = "权限组id不能为空", groups = { AddGroup.class, EditGroup.class })
private Long groupId;
private Long authGroupId;
/**
* 设备id门禁/电梯)

View File

@@ -10,7 +10,6 @@ import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
@@ -18,7 +17,7 @@ import java.util.Date;
* 权限组⇄设备关联视图对象 sis_auth_group_ref
*
* @author lsm
* @date 2025-07-23
* @since 2025-07-23
*/
@Data
@ExcelIgnoreUnannotated
@@ -38,7 +37,7 @@ public class SisAuthGroupRefVo implements Serializable {
* 权限组id
*/
@ExcelProperty(value = "权限组id")
private Long groupId;
private Long authGroupId;
/**
* 设备id门禁/电梯)

View File

@@ -1,5 +1,6 @@
package org.dromara.sis.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.dromara.sis.domain.SisPersonLibImg;
import org.dromara.sis.domain.vo.SisPersonLibImgVo;
import org.dromara.common.mybatis.core.mapper.BaseMapperPlus;
@@ -8,8 +9,9 @@ import org.dromara.common.mybatis.core.mapper.BaseMapperPlus;
* 人像信息Mapper接口
*
* @author mocheng
* @date 2025-06-24
* @since 2025-06-24
*/
@Mapper
public interface SisPersonLibImgMapper extends BaseMapperPlus<SisPersonLibImg, SisPersonLibImgVo> {
}

View File

@@ -96,4 +96,13 @@ public interface ISisAuthRecordService {
* @param personId 人脸比对ID
*/
// List<SisAuthRecordVo> checkAuth(Long personId);
/**
* 根据权限组ID人员id返回授权记录
*
* @param groupId 权限组ID
* @param personId 人员id
* @return SisAuthRecordVo
*/
SisAuthRecordVo queryByGroupIdAndPersonId(Long groupId, Long personId);
}

View File

@@ -73,4 +73,13 @@ public interface ISisPersonLibImgService {
* @return 列表
*/
List<SisPersonLibImgVo> queryListByIds(Collection<Long> ids);
/**
* 根据入驻员工id更新huaweiBoxId
*
* @param personId 入驻员工id
* @param huaweiBoxId 华为盒子图像id
* @return Boolean
*/
Boolean updateByPersonId(Long personId, Long huaweiBoxId);
}

View File

@@ -74,7 +74,7 @@ public class SisAuthGroupRefServiceImpl implements ISisAuthGroupRefService {
Map<String, Object> params = bo.getParams();
LambdaQueryWrapper<SisAuthGroupRef> lqw = Wrappers.lambdaQuery();
lqw.orderByAsc(SisAuthGroupRef::getId);
lqw.eq(bo.getGroupId() != null, SisAuthGroupRef::getGroupId, bo.getGroupId());
lqw.eq(bo.getAuthGroupId() != null, SisAuthGroupRef::getAuthGroupId, bo.getAuthGroupId());
lqw.eq(bo.getDeviceId() != null, SisAuthGroupRef::getDeviceId, bo.getDeviceId());
lqw.eq(bo.getDeviceType() != null, SisAuthGroupRef::getDeviceType, bo.getDeviceType());
return lqw;
@@ -140,7 +140,7 @@ public class SisAuthGroupRefServiceImpl implements ISisAuthGroupRefService {
@Override
public List<SisAuthGroupRefVo> queryListByGroupId(Long groupId){
LambdaQueryWrapper<SisAuthGroupRef> lqw = Wrappers.lambdaQuery();
lqw.eq(SisAuthGroupRef::getGroupId, groupId);
lqw.eq(SisAuthGroupRef::getAuthGroupId, groupId);
return baseMapper.selectVoList(lqw);
}
}

View File

@@ -123,7 +123,7 @@ public class SisAuthGroupServiceImpl implements ISisAuthGroupService {
boolean check;
for(Long ac : bo.getAcIds()){
SisAuthGroupRefBo groupRef = new SisAuthGroupRefBo();
groupRef.setGroupId(add.getId());
groupRef.setAuthGroupId(add.getId());
groupRef.setDeviceType(1L);
groupRef.setDeviceId(ac);
check = sisAuthGroupRefService.insertByBo(groupRef);
@@ -134,7 +134,7 @@ public class SisAuthGroupServiceImpl implements ISisAuthGroupService {
log.info("开始写入授权组电梯eleIds{}", bo.getEleIds());
for (SisElevatorInfoVo ele : eleVoList) {
SisAuthGroupRefBo groupRef = new SisAuthGroupRefBo();
groupRef.setGroupId(add.getId());
groupRef.setAuthGroupId(add.getId());
groupRef.setDeviceType(2L);
groupRef.setDeviceId(ele.getElevatorId());
check = sisAuthGroupRefService.insertByBo(groupRef);

View File

@@ -1,11 +1,7 @@
package org.dromara.sis.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.IdUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.common.core.constant.CodePrefixConstants;
import org.dromara.common.core.domain.TreeNode;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
@@ -17,20 +13,12 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.property.api.RemoteFloorService;
import org.dromara.property.api.domain.vo.RemoteFloorVo;
import org.dromara.resource.api.RemoteFileService;
import org.dromara.sis.api.domain.RemotePersonAuth;
import org.dromara.sis.domain.bo.SisAccessControlBo;
import org.dromara.sis.domain.bo.SisElevatorInfoBo;
import org.dromara.sis.domain.bo.SisPersonLibImgBo;
import org.dromara.sis.domain.vo.SisAccessControlVo;
import org.dromara.sis.domain.vo.SisAuthGroupRefVo;
import org.dromara.sis.domain.vo.SisElevatorInfoVo;
import org.dromara.sis.sdk.e8.E8PlatformApi;
import org.dromara.sis.sdk.e8.domain.accessControl.req.CustomerAuthAddReq;
import org.dromara.sis.sdk.e8.domain.custom.req.CustomAddReq;
import org.dromara.sis.sdk.e8.domain.voucher.req.IssueVoucherReq;
import org.dromara.sis.sdk.huawei.HuaWeiBoxApi;
import org.dromara.sis.sdk.huawei.domain.AddHWPersonReq;
import org.dromara.sis.service.*;
import org.springframework.stereotype.Service;
import org.dromara.sis.domain.bo.SisAuthRecordBo;
@@ -52,18 +40,11 @@ import java.util.*;
@Service
public class SisAuthRecordServiceImpl implements ISisAuthRecordService {
private final HuaWeiBoxApi huaWeiBoxApi;
private final E8PlatformApi e8PlatformApi;
private final SisAuthRecordMapper baseMapper;
private final ISisElevatorInfoService elevatorInfoService;
private final ISisAuthGroupRefService sisAuthGroupRefService;
private final ISisPersonLibImgService sisPersonLibImgService;
private final ISisAccessControlService sisAccessControlService;
@DubboReference
private RemoteFileService remoteFileService;
@DubboReference
private RemoteFloorService remoteFloorService;
@@ -145,96 +126,16 @@ public class SisAuthRecordServiceImpl implements ISisAuthRecordService {
private void syncPersonImg(RemotePersonAuth bo){
log.info("开始写入安防人像信息");
SisPersonLibImgBo personLibImg = new SisPersonLibImgBo();
personLibImg.setImgOssId(12346L);
personLibImg.setImgOssId(Long.parseLong(bo.getOssId()));
personLibImg.setImgName(bo.getName());
personLibImg.setSex(bo.getSex());
personLibImg.setEmail(bo.getEmail());
personLibImg.setTel(bo.getPhone());
personLibImg.setCertificateType(bo.getCardType());
personLibImg.setCertificateType(1);
personLibImg.setCertificateNo(bo.getIdCardNumber());
personLibImg.setResidentPersonId(bo.getId());
sisPersonLibImgService.insertByBo(personLibImg);
log.info("写入安防人像信息完成");
}
private Long syncHuaweiBox(RemotePersonAuth bo, byte[] imgByte) {
log.info("开始写入华为平台");
AddHWPersonReq req = new AddHWPersonReq();
req.setIndex(CodePrefixConstants.PERSON_LIB_IMAGE_CODE_PREFIX + IdUtil.getSnowflakeNextIdStr());
req.setName(bo.getName());
req.setGender(bo.getSex() == 1 ? "0" : bo.getSex() == 2 ? "1" : "-1");
req.setCredentialType("0");
req.setCredentialNumber(bo.getIdCardNumber());
ArrayList<String> pictures = new ArrayList<>();
pictures.add(Base64.getEncoder().encodeToString(imgByte));
req.setPictures(pictures);
Long pId = huaWeiBoxApi.addPerson(List.of(req));
Assert.notNull(pId, "调用华为盒子新增图片失败");
log.info("写入华为盒子完成pId={}", pId);
return pId;
}
private Long syncE8Plat(RemotePersonAuth bo, byte[] imgByte) throws Exception {
log.info("e8平台上传照片");
String e8ImgUrl = e8PlatformApi.uploadFace(imgByte);
Assert.notNull(e8ImgUrl, "图片上传E8平台失败");
log.info("e8平台上传照片完成");
log.info("e8同步新建人员");
CustomAddReq req = new CustomAddReq();
req.setName(bo.getName());
req.setGender(bo.getSex());
req.setMobile(bo.getPhone());
req.setEmail(bo.getEmail());
req.setIdentityType(0);
req.setIdentityNo(bo.getIdCardNumber());
Long e8Id = e8PlatformApi.addCustomer(req).getId();
Assert.notNull(e8Id, "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平台发行凭证成功");
// 获取门禁
List<SisAuthGroupRefVo> refVos = sisAuthGroupRefService.queryListByGroupId(bo.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(bo.getAuthBegDate(), "yyyy-MM-dd HH:mm:ss"));
authReq.setEndTime(DateUtil.format(bo.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平台授权完成!");
}
return e8Id;
}
/**
* 修改授权记录
*
@@ -348,4 +249,18 @@ public class SisAuthRecordServiceImpl implements ISisAuthRecordService {
root.setChildren(List.of(accessNode, elevatorNode));
return List.of(root);
}
/**
* 根据权限组ID人员id返回授权记录
*
* @param groupId 权限组ID
* @param personId 人员id
* @return SisAuthRecordVo
*/
public SisAuthRecordVo queryByGroupIdAndPersonId(Long groupId, Long personId){
LambdaQueryWrapper<SisAuthRecord> lqw = Wrappers.lambdaQuery();
lqw.eq(SisAuthRecord::getGroupId, groupId)
.eq(SisAuthRecord::getTargetId, personId);
return baseMapper.selectVoOne(lqw);
}
}

View File

@@ -45,7 +45,7 @@ public class SisPersonLibImgServiceImpl implements ISisPersonLibImgService {
private final ISisLibImgRefService sisLibImgRefService;
@DubboReference
private final RemoteFileService remoteFileService;
private RemoteFileService remoteFileService;
/**
* 查询人像信息
@@ -178,4 +178,20 @@ public class SisPersonLibImgServiceImpl implements ISisPersonLibImgService {
public List<SisPersonLibImgVo> queryListByIds(Collection<Long> Ids) {
return baseMapper.selectVoByIds(Ids);
}
/**
* 根据入驻员工id更新huaweiBoxId
*
* @param personId 入驻员工id
* @param huaweiBoxId 华为盒子图像id
* @return Boolean
*/
@Override
public Boolean updateByPersonId(Long personId, Long huaweiBoxId){
LambdaQueryWrapper<SisPersonLibImg> lqw = new LambdaQueryWrapper<>();
lqw.eq(SisPersonLibImg::getResidentPersonId, personId);
SisPersonLibImg update = new SisPersonLibImg();
update.setRemoteImgId(huaweiBoxId);
return baseMapper.update(update, lqw) > 0;
}
}