feat:资产授权(优化)

master_fei
Yunfei Du 2024-05-13 19:31:48 +08:00
parent 70cad5369c
commit f91ab4522c
3 changed files with 104 additions and 58 deletions

View File

@ -5,21 +5,21 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etl.common.core.domain.Result;
import com.etl.common.security.utils.SecurityUtils;
import com.etl.common.system.domain.SysUser;
import com.etl.common.system.remote.RemoteUserService;
import com.etl.data.domain.AssetAccredit;
import com.etl.data.domain.SourceAccredit;
import com.etl.data.domain.model.AccreditModel;
import com.etl.data.domain.req.AssetAccreditReq;
import com.etl.data.mapper.AssetAccreditMapper;
import com.etl.data.remote.RemoteSys;
import com.etl.data.service.AssetAccreditService;
import lombok.extern.log4j.Log4j2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
*
@ -27,19 +27,21 @@ import java.util.Objects;
* @date 15:30 2024/5/1
*/
@Service
@Log4j2
public class AssetAccreditServiceImpl extends ServiceImpl< AssetAccreditMapper, AssetAccredit > implements AssetAccreditService {
@Autowired
private RemoteSys remoteSys;
private static final Logger log = LoggerFactory.getLogger ( AssetAccreditServiceImpl.class );
@Override
public Result getAssetAccreditByDataAssetId(Long id) {
List< AssetAccredit > assetAccreditList = this.list ( new LambdaQueryWrapper< AssetAccredit > ( ).eq ( AssetAccredit::getDataAssetId, id ) );
List< Long > userAccreditIds = assetAccreditList.stream ().filter ( sourceAccredit -> sourceAccredit.getDeptId ( ) != null ).map ( AssetAccredit::getDeptId ).toList ();
List< Long > deptAccreditIds = assetAccreditList.stream ().filter ( sourceAccredit -> sourceAccredit.getUserId ( ) != null ).map ( AssetAccredit::getUserId ).toList ();
List< Long > deptAccreditIds = assetAccreditList.stream ().filter ( sourceAccredit -> sourceAccredit.getDeptId ( ) != null ).map ( AssetAccredit::getDeptId ).toList ();
List< Long > userAccreditIds = assetAccreditList.stream ().filter ( sourceAccredit -> sourceAccredit.getUserId ( ) != null ).map ( AssetAccredit::getUserId ).toList ();
AccreditModel accreditModel = new AccreditModel ( );
@ -50,39 +52,70 @@ public class AssetAccreditServiceImpl extends ServiceImpl< AssetAccreditMapper,
@Override
public Result insertAssetAccredit(AssetAccreditReq assetAccreditReq) {
try {
deleteAssetAccreditByAssetIds(assetAccreditReq);
List<AssetAccredit> assetAccredits = new ArrayList<> ();
if (assetAccreditReq.getUserId() != null){
List<AssetAccredit> assetAccredits = new ArrayList<>();
// 添加用户ID不为null的情况
if (assetAccreditReq.getUserId() != null) {
assetAccredits.add(createAssetAccreditForUser(assetAccreditReq));
}
// 添加部门ID的情况
if (assetAccreditReq.getDeptIds() != null && !assetAccreditReq.getDeptIds().isEmpty()) {
Set<Long> deptIdsSet = new HashSet<>(assetAccreditReq.getDeptIds());
List<SysUser> sysUsers = remoteSys.userList().getData().getRows();
List< AssetAccredit > collect = assetAccreditReq.getDeptIds ( ).stream ( ).map ( deptId ->
createAssetAccreditForDept ( assetAccreditReq, deptId ) )
.collect ( Collectors.toList ( ) );
// 使用HashSet改善查找性能
List<AssetAccredit> deptAccredits = sysUsers.stream()
.filter(sysUser -> deptIdsSet.contains(sysUser.getDeptId()))
.map(sysUser -> createAssetAccreditForUser1(assetAccreditReq, sysUser.getUserId()))
.collect( Collectors.toList());
assetAccredits.addAll ( collect );
assetAccredits.addAll(deptAccredits);
log.debug("assetAccreditList: {}", deptAccredits); // 只在调试模式下记录详细列表
}
this.saveBatch(assetAccredits);
return Result.success();
} catch (Exception e) {
// 异常处理逻辑
log.error("Error inserting asset accreditations", e);
// 根据实际情况返回错误结果或进行其他处理
return Result.error ( "Error during asset accreditation insertion." );
}
}
private AssetAccredit createAssetAccreditForUser(AssetAccreditReq assetAccreditReq) {
AssetAccredit assetAccredit = new AssetAccredit();
assetAccredit.setDataAssetId(assetAccreditReq.getDataAssetId());
assetAccredit.setUserId(assetAccreditReq.getUserId());
assetAccredit.setCreateBy( SecurityUtils.getUsername());
assetAccredit.setCreateTime(new Date ());
assetAccredits.add(assetAccredit);
}else{
assetAccredits=assetAccreditReq.getDeptIds().stream().map(deptId -> {
assetAccredit.setCreateBy(SecurityUtils.getUsername());
assetAccredit.setCreateTime(new Date());
return assetAccredit;
}
private AssetAccredit createAssetAccreditForUser1(AssetAccreditReq assetAccreditReq, Long userId) {
AssetAccredit assetAccredit = new AssetAccredit();
assetAccredit.setDataAssetId(assetAccreditReq.getDataAssetId());
assetAccredit.setUserId(userId);
assetAccredit.setCreateBy(SecurityUtils.getUsername());
assetAccredit.setCreateTime(new Date());
return assetAccredit;
}
private AssetAccredit createAssetAccreditForDept(AssetAccreditReq assetAccreditReq, Long deptId) {
AssetAccredit assetAccredit = new AssetAccredit();
assetAccredit.setDataAssetId(assetAccreditReq.getDataAssetId());
assetAccredit.setDeptId(deptId);
assetAccredit.setCreateBy(SecurityUtils.getUsername());
assetAccredit.setCreateTime(new Date());
return assetAccredit;
}).toList();
List< Long > deptIds = assetAccreditReq.getDeptIds ( );
List< SysUser > sysUsers = remoteSys.userList ( ).getData ( ).getRows ( );
assetAccredits.addAll(sysUsers.stream().filter(sysUser -> deptIds.contains(sysUser.getDeptId())).toList().stream().map(sysUser -> {
AssetAccredit assetAccredit = new AssetAccredit();
assetAccredit.setDataAssetId(assetAccreditReq.getDataAssetId());
assetAccredit.setUserId(sysUser.getUserId());
assetAccredit.setCreateBy(SecurityUtils.getUsername());
assetAccredit.setCreateTime(new Date());
return assetAccredit;
}).toList());
}
this.saveBatch ( assetAccredits );
return Result.success();
}
@Override
public Result deleteAssetAccreditByAssetIds(AssetAccreditReq assetAccreditReq) {
@ -91,7 +124,7 @@ public class AssetAccreditServiceImpl extends ServiceImpl< AssetAccreditMapper,
this.remove ( new LambdaQueryWrapper< AssetAccredit> ( )
.eq ( AssetAccredit::getDataAssetId, assetAccreditReq.getDataAssetId () )
.eq ( AssetAccredit::getUserId, assetAccreditReq.getUserId () ) );
}
}else {
// 获取所有系统部门ID和所有用户信息
List< Long > deptIds = remoteSys.list ( ).getData ( ).stream ( ).map ( sysDept -> sysDept.getDeptId ( ) ).toList ( );
List< SysUser > userList = remoteSys.userList ( ).getData ( ).getRows ( );
@ -108,6 +141,18 @@ public class AssetAccreditServiceImpl extends ServiceImpl< AssetAccreditMapper,
this.remove ( new LambdaQueryWrapper<AssetAccredit> ( )
.eq ( AssetAccredit::getDataAssetId, assetAccreditReq.getDataAssetId () )
.in ( AssetAccredit::getUserId, userIds ));
}
return Result.success ( );
}
public static void main(String[] args) {
ThreadLocal<String> local = new ThreadLocal<>();
IntStream.range(0, 10).forEach( i -> new Thread(() -> {
local.set(Thread.currentThread().getName() + ":" + i);
System.out.println("线程:" + Thread.currentThread().getName() + ",local:" + local.get());
}).start());
}
}

View File

@ -46,8 +46,8 @@ public class SourceAccreditServiceImpl extends ServiceImpl< SourceAccreditMapper
public Result getSourceAccreditByDataSourceId(Long id) {
List< SourceAccredit > sourceAccreditList = this.list ( new LambdaQueryWrapper< SourceAccredit > ( ).eq ( SourceAccredit::getDataSourceId, id ) );
List< Long > userAccreditIds = sourceAccreditList.stream ().filter ( sourceAccredit -> sourceAccredit.getDeptId ( ) != null ).map ( SourceAccredit::getDeptId ).toList ();
List< Long > deptAccreditIds = sourceAccreditList.stream ().filter ( sourceAccredit -> sourceAccredit.getUserId ( ) != null ).map ( SourceAccredit::getUserId ).toList ();
List< Long > deptAccreditIds = sourceAccreditList.stream ().filter ( sourceAccredit -> sourceAccredit.getDeptId ( ) != null ).map ( SourceAccredit::getDeptId ).toList ();
List< Long > userAccreditIds = sourceAccreditList.stream ().filter ( sourceAccredit -> sourceAccredit.getUserId ( ) != null ).map ( SourceAccredit::getUserId ).toList ();
AccreditModel accreditModel = new AccreditModel ( );
accreditModel.setDeptAccreditModelIds ( deptAccreditIds );

View File

@ -15,6 +15,7 @@
<module>etl-data-source-common</module>
<module>etl-data-source-remote</module>
<module>etl-data-source-server</module>
<module>etl-data-source-client</module>
</modules>
<properties>