From f9a340c77c232e4536ea510ddd7bdf01b1d8dc7f Mon Sep 17 00:00:00 2001 From: daiyongfei <974332738@qq.com> Date: Sat, 21 Jun 2025 13:48:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E8=AE=BE=E5=A4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/domain/DeviceAssignments.java | 73 ++++---- .../modules/system/domain/DeviceType.java | 3 +- .../modules/system/domain/dto/CustomerVo.java | 5 + .../system/enums/DeviceActiveStatusEnum.java | 44 +++++ .../modules/system/rest/DeviceController.java | 8 - .../system/rest/DeviceTypeController.java | 1 + .../service/impl/DeviceServiceImpl.java | 161 +++++++++++------- .../service/impl/DeviceTypeServiceImpl.java | 1 + .../modules/utils/AdminCheckUtil.java | 40 +++++ .../resources/mapper/system/DeviceMapper.xml | 5 +- 10 files changed, 227 insertions(+), 114 deletions(-) create mode 100644 fys-system/src/main/java/com/fuyuanshen/modules/system/enums/DeviceActiveStatusEnum.java create mode 100644 fys-system/src/main/java/com/fuyuanshen/modules/utils/AdminCheckUtil.java diff --git a/fys-system/src/main/java/com/fuyuanshen/modules/system/domain/DeviceAssignments.java b/fys-system/src/main/java/com/fuyuanshen/modules/system/domain/DeviceAssignments.java index af60852..fb31970 100644 --- a/fys-system/src/main/java/com/fuyuanshen/modules/system/domain/DeviceAssignments.java +++ b/fys-system/src/main/java/com/fuyuanshen/modules/system/domain/DeviceAssignments.java @@ -8,6 +8,8 @@ import lombok.Data; import java.util.Date; /** + * 设备分配记录表 + * * @TableName device_assignments */ @TableName(value = "device_assignments") @@ -35,6 +37,20 @@ public class DeviceAssignments { */ private Long toCustomerId; + /** + * 分配者 + * assigner_name + */ + private Long assignerId; + private String assignerName; + + /** + * 接收者 + * assignee_name + */ + private Long assigneeId; + private String assigneeName; + /** * 分配时间 */ @@ -47,48 +63,23 @@ public class DeviceAssignments { */ private Integer deviceTypeGranted; - @Override - public boolean equals(Object that) { - if (this == that) { - return true; - } - if (that == null) { - return false; - } - if (getClass() != that.getClass()) { - return false; - } - DeviceAssignments other = (DeviceAssignments) that; - return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId())) && (this.getDeviceId() == null ? other.getDeviceId() == null : this.getDeviceId().equals(other.getDeviceId())) && (this.getFromCustomerId() == null ? other.getFromCustomerId() == null : this.getFromCustomerId().equals(other.getFromCustomerId())) && (this.getToCustomerId() == null ? other.getToCustomerId() == null : this.getToCustomerId().equals(other.getToCustomerId())) && (this.getAssignedAt() == null ? other.getAssignedAt() == null : this.getAssignedAt().equals(other.getAssignedAt())) && (this.getDeviceTypeGranted() == null ? other.getDeviceTypeGranted() == null : this.getDeviceTypeGranted().equals(other.getDeviceTypeGranted())); - } + /** + * 0 否 + * 1 是 + * 是否直接分配(用于父级显示) + */ + private Integer direct; - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((getId() == null) ? 0 : getId().hashCode()); - result = prime * result + ((getDeviceId() == null) ? 0 : getDeviceId().hashCode()); - result = prime * result + ((getFromCustomerId() == null) ? 0 : getFromCustomerId().hashCode()); - result = prime * result + ((getToCustomerId() == null) ? 0 : getToCustomerId().hashCode()); - result = prime * result + ((getAssignedAt() == null) ? 0 : getAssignedAt().hashCode()); - result = prime * result + ((getDeviceTypeGranted() == null) ? 0 : getDeviceTypeGranted().hashCode()); - return result; - } + /** + * 0 否 + * 1 是 + * 设备是否有效 + */ + private Integer active; - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append(getClass().getSimpleName()); - sb.append(" ["); - sb.append("Hash = ").append(hashCode()); - sb.append(", id=").append(id); - sb.append(", deviceId=").append(deviceId); - sb.append(", fromCustomerId=").append(fromCustomerId); - sb.append(", toCustomerId=").append(toCustomerId); - sb.append(", assignedAt=").append(assignedAt); - sb.append(", deviceTypeGranted=").append(deviceTypeGranted); - sb.append("]"); - return sb.toString(); - } + /** + * 分配等级(用于失效) + */ + private String lever; } \ No newline at end of file diff --git a/fys-system/src/main/java/com/fuyuanshen/modules/system/domain/DeviceType.java b/fys-system/src/main/java/com/fuyuanshen/modules/system/domain/DeviceType.java index 7da3af0..12f32ee 100644 --- a/fys-system/src/main/java/com/fuyuanshen/modules/system/domain/DeviceType.java +++ b/fys-system/src/main/java/com/fuyuanshen/modules/system/domain/DeviceType.java @@ -32,7 +32,8 @@ public class DeviceType extends BaseEntity implements Serializable { private Long customerId; @ApiModelProperty(value = "创建该类型的客户") - private String ownerCustomerId; + private Long ownerCustomerId; + /** * 租户ID */ diff --git a/fys-system/src/main/java/com/fuyuanshen/modules/system/domain/dto/CustomerVo.java b/fys-system/src/main/java/com/fuyuanshen/modules/system/domain/dto/CustomerVo.java index e802a4c..2ea2e70 100644 --- a/fys-system/src/main/java/com/fuyuanshen/modules/system/domain/dto/CustomerVo.java +++ b/fys-system/src/main/java/com/fuyuanshen/modules/system/domain/dto/CustomerVo.java @@ -2,7 +2,9 @@ package com.fuyuanshen.modules.system.domain.dto; import io.swagger.annotations.ApiModelProperty; import lombok.Data; +import org.springframework.validation.annotation.Validated; +import javax.validation.constraints.NotNull; import java.util.List; /** @@ -11,12 +13,15 @@ import java.util.List; * @Date: 2025/5/28 **/ @Data +@Validated public class CustomerVo { @ApiModelProperty(value = "客户ID") + @NotNull(message = "客户ID不能为空") private Long customerId; @ApiModelProperty(value = "设备ID") + @NotNull(message = "设备ID不能为空") private List deviceIds; } diff --git a/fys-system/src/main/java/com/fuyuanshen/modules/system/enums/DeviceActiveStatusEnum.java b/fys-system/src/main/java/com/fuyuanshen/modules/system/enums/DeviceActiveStatusEnum.java new file mode 100644 index 0000000..ce32fa9 --- /dev/null +++ b/fys-system/src/main/java/com/fuyuanshen/modules/system/enums/DeviceActiveStatusEnum.java @@ -0,0 +1,44 @@ +package com.fuyuanshen.modules.system.enums; + +/** + * 设备有效性状态枚举 + * + * @author: 默苍璃 + * @date: 2025-06-2113:26 + */ +public enum DeviceActiveStatusEnum { + + INACTIVE(0, "无效"), ACTIVE(1, "有效"); + + private final Integer code; + private final String description; + + DeviceActiveStatusEnum(Integer code, String description) { + this.code = code; + this.description = description; + } + + public Integer getCode() { + return code; + } + + public String getDescription() { + return description; + } + + /** + * 根据 code 获取描述 + * + * @param code 状态码 + * @return 描述信息 + */ + public static String getDescriptionByCode(Integer code) { + for (DeviceActiveStatusEnum status : values()) { + if (status.getCode().equals(code)) { + return status.getDescription(); + } + } + return null; + } +} + diff --git a/fys-system/src/main/java/com/fuyuanshen/modules/system/rest/DeviceController.java b/fys-system/src/main/java/com/fuyuanshen/modules/system/rest/DeviceController.java index 40b75a8..18f84fe 100644 --- a/fys-system/src/main/java/com/fuyuanshen/modules/system/rest/DeviceController.java +++ b/fys-system/src/main/java/com/fuyuanshen/modules/system/rest/DeviceController.java @@ -216,28 +216,20 @@ public class DeviceController { try { User currentUser = userMapper.findByUsername(SecurityUtils.getCurrentUsername()); DeviceImportParams params = DeviceImportParams.builder().ip(ip).deviceService(deviceService).tenantId(currentUser.getTenantId()).file(file).filePath(filePath).deviceMapper(deviceMapper).deviceTypeMapper(deviceTypeMapper).userId(currentUser.getId()).build(); - // 创建监听器 UploadDeviceDataListener listener = new UploadDeviceDataListener(params); - // 读取Excel EasyExcel.read(file.getInputStream(), DeviceExcelImportDTO.class, listener).sheet().doRead(); - // 获取导入结果 result = listener.getImportResult(); - // 设置响应消息 String message = String.format("成功导入 %d 条数据,失败 %d 条", result.getSuccessCount(), result.getFailureCount()); - // 返回带有正确泛型的响应 return ResponseVO.success(message, result); - } catch (Exception e) { log.error("导入设备数据出错: {}", e.getMessage(), e); - // 在异常情况下,设置默认结果 String errorMessage = String.format("导入失败: %s。成功 %d 条,失败 %d 条", e.getMessage(), result.getSuccessCount(), result.getFailureCount()); - // 使用新方法确保类型正确 return ResponseVO.fail(errorMessage, result); } diff --git a/fys-system/src/main/java/com/fuyuanshen/modules/system/rest/DeviceTypeController.java b/fys-system/src/main/java/com/fuyuanshen/modules/system/rest/DeviceTypeController.java index 09e7177..303f1ee 100644 --- a/fys-system/src/main/java/com/fuyuanshen/modules/system/rest/DeviceTypeController.java +++ b/fys-system/src/main/java/com/fuyuanshen/modules/system/rest/DeviceTypeController.java @@ -81,4 +81,5 @@ public class DeviceTypeController { return ResponseVO.success(deviceTypeService.getById(id)); } + } diff --git a/fys-system/src/main/java/com/fuyuanshen/modules/system/service/impl/DeviceServiceImpl.java b/fys-system/src/main/java/com/fuyuanshen/modules/system/service/impl/DeviceServiceImpl.java index 7066121..2d20d10 100644 --- a/fys-system/src/main/java/com/fuyuanshen/modules/system/service/impl/DeviceServiceImpl.java +++ b/fys-system/src/main/java/com/fuyuanshen/modules/system/service/impl/DeviceServiceImpl.java @@ -18,6 +18,7 @@ import com.fuyuanshen.modules.system.domain.dto.CustomerVo; import com.fuyuanshen.modules.system.domain.dto.DeviceForm; import com.fuyuanshen.modules.system.domain.dto.DeviceQueryCriteria; import com.fuyuanshen.modules.system.enums.BindingStatusEnum; +import com.fuyuanshen.modules.system.enums.DeviceActiveStatusEnum; import com.fuyuanshen.modules.system.enums.DeviceAuthorizationStatus; import com.fuyuanshen.modules.system.enums.DeviceStatusEnum; import com.fuyuanshen.modules.system.mapper.DeviceMapper; @@ -29,6 +30,7 @@ import com.fuyuanshen.modules.system.service.DeviceAssignmentsService; import com.fuyuanshen.modules.system.service.DeviceService; import com.fuyuanshen.modules.system.service.DeviceTypeGrantsService; import com.fuyuanshen.modules.system.service.UserService; +import com.fuyuanshen.modules.utils.AdminCheckUtil; import com.fuyuanshen.modules.utils.NanoId; import com.fuyuanshen.utils.*; import com.fuyuanshen.utils.enums.NanoIdLengthEnum; @@ -56,6 +58,12 @@ import java.util.stream.Collectors; @RequiredArgsConstructor public class DeviceServiceImpl extends ServiceImpl implements DeviceService { + /** + * 初级分配等级 + */ + public static final int LEVEL_PRIMARY = 1; + + @Value("${file.device.pic}") private String filePath; @Value("${file.device.ip}") @@ -99,7 +107,7 @@ public class DeviceServiceImpl extends ServiceImpl impleme // 只能看到自己的创建的设备,以及被分配的设备。 if (onlineuser.getTenantId() != null && !onlineuser.getTenantId().equals(UserConstants.SUPER_ADMIN_ID)) { criteria.setTenantId(onlineuser.getTenantId()); - // criteria.setCurrentOwnerId(onlineuser.getId()); + criteria.setCurrentOwnerId(onlineuser.getId()); } IPage devices = deviceMapper.findAll(criteria, page); @@ -183,11 +191,24 @@ public class DeviceServiceImpl extends ServiceImpl impleme device.setDeviceStatus(DeviceStatusEnum.NORMAL.getCode()); // SnowflakeGenerator snowflakeGenerator = new SnowflakeGenerator(); // device.setId(snowflakeGenerator.next()); - device.setCurrentOwnerId(currentUser.getId()); device.setOriginalOwnerId(currentUser.getId()); deviceMapper.insert(device); + + // 新增设备类型记录 + DeviceAssignments assignments = new DeviceAssignments(); + assignments.setDeviceId(device.getId()); + assignments.setAssignedAt(new Date()); + // 分配者 + assignments.setAssignerId(currentUser.getId()); + // 接收者 + assignments.setAssigneeId(currentUser.getId()); + assignments.setActive(DeviceActiveStatusEnum.ACTIVE.getCode()); + String lever = currentUser.getId() + ":"; + assignments.setLever(lever); + deviceAssignmentsService.save(assignments); + } @@ -315,88 +336,104 @@ public class DeviceServiceImpl extends ServiceImpl impleme @Transactional(rollbackFor = Exception.class) public void assignCustomer(CustomerVo customerVo) { - // 防止管理员误操作 + // 获取当前登录用户 User currentUser = userService.findById(SecurityUtils.getCurrentUserId()); - if (currentUser.getTenantId().equals(UserConstants.SUPER_ADMIN_ID)) { - throw new BadRequestException(ExceptionMessages.ADMIN_OPERATION_NOT_ALLOWED); - } - + // 防止管理员误操作 + AdminCheckUtil.checkIfSuperAdmin(currentUser); + // 获取分配用户信息 User user = userService.findById(customerVo.getCustomerId()); + // 获取分配设备信息 List devices = deviceMapper.selectBatchIds(customerVo.getDeviceIds()); - Timestamp timestamp = new Timestamp(System.currentTimeMillis()); + + // Timestamp timestamp = new Timestamp(System.currentTimeMillis()); for (Device device : devices) { device.setCustomerId(user.getId()); device.setCustomerName(user.getNickName()); - device.setUpdateTime(timestamp); + // device.setUpdateTime(timestamp); } this.updateBatchById(devices); // 批量更新设备状态 List deviceTypeGrants = new ArrayList<>(); for (Device device : devices) { - // 获取当前用户的所有祖先用户 - List ancestorsById = userMapper.findAncestorsById(currentUser.getId()); - Set excludedTenantIds = new HashSet<>(); - if (CollectionUtil.isNotEmpty(ancestorsById)) { - // 提取所有需要排除的 tenant_id - excludedTenantIds = ancestorsById.stream().map(User::getTenantId).distinct().collect(Collectors.toSet()); - } - excludedTenantIds.add(currentUser.getTenantId()); - // 构建查询条件:device_mac 相同,并且 tenant_id 不在 excludedTenantIds 列表中 - Wrapper wrapper = new QueryWrapper().eq("device_mac", device.getDeviceMac()).notIn("tenant_id", excludedTenantIds); - // 构建要更新的数据 - Device updateDevice = new Device(); - updateDevice.setDeviceStatus(0); - updateDevice.setUpdateTime(new Timestamp(System.currentTimeMillis())); + // 如果设备已分配给需要分配的客户,则跳过 + if (device.getCustomerId() != null && device.getCustomerId().equals(customerVo.getCustomerId())) { + continue; + } + + + // // 获取当前用户的所有祖先用户 + // List ancestorsById = userMapper.findAncestorsById(currentUser.getId()); + // Set excludedTenantIds = new HashSet<>(); + // if (CollectionUtil.isNotEmpty(ancestorsById)) { + // // 提取所有需要排除的 tenant_id + // excludedTenantIds = ancestorsById.stream().map(User::getTenantId).distinct().collect(Collectors.toSet()); + // } + // excludedTenantIds.add(currentUser.getTenantId()); + // // 构建查询条件:device_mac 相同,并且 tenant_id 不在 excludedTenantIds 列表中 + // Wrapper wrapper = new QueryWrapper().eq("device_mac", device.getDeviceMac()).notIn("tenant_id", excludedTenantIds); + // + // // 构建要更新的数据 + // Device updateDevice = new Device(); + // updateDevice.setDeviceStatus(0); + // updateDevice.setUpdateTime(new Timestamp(System.currentTimeMillis())); // 根据条件批量更新 - deviceMapper.update(updateDevice, wrapper); + // deviceMapper.update(updateDevice, wrapper); - // 自定义16位id - Long generatedId = NanoId.generate(NanoIdLengthEnum.HIGH_CONCURRENCY.getLength()); - // -- 授权设备类型给客户 - // QueryWrapper deviceTypeGrantsQueryWrapper = new QueryWrapper<>(); - // Long count = deviceTypeGrantsMapper.selectCount(deviceTypeGrantsQueryWrapper); - DeviceTypeGrants deviceTypeGrant = new DeviceTypeGrants(); - deviceTypeGrant.setGrantedAt(new Date()); - // 设备类型 - deviceTypeGrant.setDeviceTypeId(device.getDeviceType()); - deviceTypeGrant.setAssignmentId(generatedId); - // 被授权的客户 - deviceTypeGrant.setCustomerId(customerVo.getCustomerId()); - // 授权方客户 - deviceTypeGrant.setGrantorCustomerId(currentUser.getId()); - deviceTypeGrants.add(deviceTypeGrant); + // 创建并保存设备类型授权记录 + createAndSaveDeviceTypeGrants(device, currentUser, customerVo, deviceTypeGrants); } deviceTypeGrantsService.saveBatch(deviceTypeGrants); - // 批量分配 - Set deviceTypes = new HashSet<>(); - for (Device device : devices) { - device.setId(null); - device.setCustomerName(null); - device.setCustomerId(null); - device.setTenantId(user.getTenantId()); - device.setCreateTime(timestamp); - device.setCreateBy(currentUser.getUsername()); - device.setUpdateTime(timestamp); + // + // // 批量分配 + // Set deviceTypes = new HashSet<>(); + // for (Device device : devices) { + // device.setId(null); + // device.setCustomerName(null); + // device.setCustomerId(null); + // device.setTenantId(user.getTenantId()); + // device.setCreateTime(timestamp); + // device.setCreateBy(currentUser.getUsername()); + // device.setUpdateTime(timestamp); + // + // // 查询设备类型 + // DeviceType deviceType = deviceTypeMapper.selectById(device.getDeviceType()); + // // SnowflakeGenerator snowflakeGenerator = new SnowflakeGenerator(); + // Long id = NanoId.generate(NanoIdLengthEnum.HIGH_CONCURRENCY.getLength()); + // deviceType.setId(id); + // deviceType.setCreateTime(timestamp); + // deviceType.setCreateBy(currentUser.getUsername()); + // deviceType.setCustomerId(customerVo.getCustomerId()); + // deviceTypes.add(deviceType); + // + // device.setDeviceType(deviceType.getId()); + // } + // this.saveBatch(devices); + // deviceTypeService.saveBatch(deviceTypes); - // 查询设备类型 - DeviceType deviceType = deviceTypeMapper.selectById(device.getDeviceType()); - // SnowflakeGenerator snowflakeGenerator = new SnowflakeGenerator(); - Long id = NanoId.generate(NanoIdLengthEnum.HIGH_CONCURRENCY.getLength()); - deviceType.setId(id); - deviceType.setCreateTime(timestamp); - deviceType.setCreateBy(currentUser.getUsername()); - deviceType.setCustomerId(customerVo.getCustomerId()); - deviceTypes.add(deviceType); + } - device.setDeviceType(deviceType.getId()); - } - this.saveBatch(devices); - deviceTypeService.saveBatch(deviceTypes); + /** + * 创建并保存设备类型授权记录 + * + * @param device 当前设备对象 + * @param currentUser 当前登录用户 + * @param customerVo 客户信息 + * @param deviceTypeGrants 授权记录集合 + */ + private void createAndSaveDeviceTypeGrants(Device device, User currentUser, CustomerVo customerVo, List deviceTypeGrants) { + Long generatedId = NanoId.generate(NanoIdLengthEnum.HIGH_CONCURRENCY.getLength()); + DeviceTypeGrants deviceTypeGrant = new DeviceTypeGrants(); + deviceTypeGrant.setGrantedAt(new Date()); + deviceTypeGrant.setDeviceTypeId(device.getDeviceType()); + deviceTypeGrant.setAssignmentId(generatedId); + deviceTypeGrant.setCustomerId(customerVo.getCustomerId()); + deviceTypeGrant.setGrantorCustomerId(currentUser.getId()); + deviceTypeGrants.add(deviceTypeGrant); } diff --git a/fys-system/src/main/java/com/fuyuanshen/modules/system/service/impl/DeviceTypeServiceImpl.java b/fys-system/src/main/java/com/fuyuanshen/modules/system/service/impl/DeviceTypeServiceImpl.java index 11be835..4b89a70 100644 --- a/fys-system/src/main/java/com/fuyuanshen/modules/system/service/impl/DeviceTypeServiceImpl.java +++ b/fys-system/src/main/java/com/fuyuanshen/modules/system/service/impl/DeviceTypeServiceImpl.java @@ -129,6 +129,7 @@ public class DeviceTypeServiceImpl extends ServiceImpl @@ -69,6 +69,7 @@ AND tenant_id = #{criteria.tenantId} + AND da.assignee_id = #{criteria.currentOwnerId}