去掉演示数据bizdata

This commit is contained in:
wh
2026-04-27 00:05:59 +08:00
parent ebe8b6c7ed
commit 849e78658d
7 changed files with 9 additions and 186 deletions

View File

@@ -1,24 +0,0 @@
package com.labelsys.backend.dto.response;
import com.labelsys.backend.entity.BizDataRecord;
import com.labelsys.backend.enums.UserRole;
import io.swagger.v3.oas.annotations.media.Schema;
@Schema(description = "数据记录响应")
public record DataRecordResponse(
@Schema(description = "记录ID") Long id,
@Schema(description = "公司ID") Long companyId,
@Schema(description = "创建人ID") Long creatorId,
@Schema(description = "创建人角色枚举值EMPLOYEE员工、MANAGER部门经理、ENGINEER总工程师") UserRole creatorRole,
@Schema(description = "记录名称") String recordName
) {
public static DataRecordResponse from(BizDataRecord record) {
return new DataRecordResponse(
record.getId(),
record.getCompanyId(),
record.getCreatorId(),
record.getCreatorRole(),
record.getRecordName()
);
}
}

View File

@@ -1,27 +0,0 @@
package com.labelsys.backend.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.labelsys.backend.enums.UserRole;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName("biz_data_record")
public class BizDataRecord {
@TableId(type = IdType.INPUT)
private Long id;
private Long companyId;
private Long creatorId;
private UserRole creatorRole;
private String recordName;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
}

View File

@@ -1,15 +0,0 @@
package com.labelsys.backend.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.labelsys.backend.entity.BizDataRecord;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface BizDataRecordMapper extends BaseMapper<BizDataRecord> {
List<BizDataRecord> listVisibleByEmployee(@Param("companyId") Long companyId, @Param("creatorId") Long creatorId);
List<BizDataRecord> listVisibleByManager(@Param("companyId") Long companyId);
List<BizDataRecord> listVisibleByEngineer(@Param("companyId") Long companyId);
}

View File

@@ -1,30 +1,15 @@
package com.labelsys.backend.service;
import com.labelsys.backend.context.LoginUser;
import com.labelsys.backend.entity.BizDataRecord;
import com.labelsys.backend.enums.UserRole;
import com.labelsys.backend.mapper.BizDataRecordMapper;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class DataPermissionService {
private final BizDataRecordMapper bizDataRecordMapper;
public List<BizDataRecord> listVisibleRecords(LoginUser currentUser) {
return switch (currentUser.role()) {
case EMPLOYEE -> bizDataRecordMapper.listVisibleByEmployee(currentUser.companyId(), currentUser.userId());
case MANAGER -> bizDataRecordMapper.listVisibleByManager(currentUser.companyId());
case ENGINEER -> bizDataRecordMapper.listVisibleByEngineer(currentUser.companyId());
};
}
public boolean canAccessCreator(LoginUser currentUser, Long creatorId, UserRole creatorRole) {
return switch (currentUser.role()) {
case EMPLOYEE -> currentUser.userId().equals(creatorId);
@@ -34,21 +19,14 @@ public class DataPermissionService {
}
/**
* 通用数据过滤方法(内存过滤,适用于已加载的数据)
*
* @param currentUser 当前登录用户
* @param allRecords 待过滤的全量数据列表
* @param roleExtractor 从数据对象中提取"关联角色"或"创建者角色"的函数
* @param ownerIdExtractor 从数据对象中提取"所有者ID"的函数(用于员工只能看自己的情况)
* @param <T> 数据类型
* @return 过滤后的数据列表
* Generic in-memory role-based data filter for records already loaded in memory.
*/
public <T> List<T> filterByRole(
LoginUser currentUser,
List<T> allRecords,
Function<T, UserRole> roleExtractor,
Function<T, Long> ownerIdExtractor) {
if (allRecords == null || allRecords.isEmpty()) {
return List.of();
}
@@ -62,49 +40,29 @@ public class DataPermissionService {
Long recordOwnerId = ownerIdExtractor.apply(record);
return switch (currentRole) {
case EMPLOYEE ->
currentUserId.equals(recordOwnerId);
case MANAGER ->
recordRole == UserRole.EMPLOYEE || recordRole == UserRole.MANAGER;
case ENGINEER ->
true;
case EMPLOYEE -> currentUserId.equals(recordOwnerId);
case MANAGER -> recordRole == UserRole.EMPLOYEE || recordRole == UserRole.MANAGER;
case ENGINEER -> true;
};
})
.collect(Collectors.toList());
}
/**
* 针对 BizDataRecord 的便捷调用方法
*/
public List<BizDataRecord> listVisibleRecordsGeneric(LoginUser currentUser, List<BizDataRecord> allRecords) {
return filterByRole(
currentUser,
allRecords,
BizDataRecord::getCreatorRole,
BizDataRecord::getCreatorId
);
}
/**
* 获取当前用户允许查看的角色列表(用于构建 SQL 查询条件)
*
* @param currentUser 当前登录用户
* @return 允许查看的角色列表
* Returns the creator roles visible to the current user for SQL-side filtering.
*/
public List<String> getAllowedRoles(LoginUser currentUser) {
return switch (currentUser.role()) {
case EMPLOYEE -> List.of(); // 员工通过 userId 过滤,不需要角色列表
case EMPLOYEE -> List.of();
case MANAGER -> List.of("EMPLOYEE", "MANAGER");
case ENGINEER -> List.of("EMPLOYEE", "MANAGER", "ENGINEER");
};
}
/**
* 判断当前用户是否应该通过 userId 过滤(员工专属)
* Whether SQL queries should additionally restrict by creator/user id.
*/
public boolean shouldFilterByUserId(LoginUser currentUser) {
return currentUser.role() == UserRole.EMPLOYEE;
}
}
}