From f91ab4522cc3028fb2ed39f93b2e795d06b507e5 Mon Sep 17 00:00:00 2001 From: Yunfei Du <278774021@qq.com> Date: Mon, 13 May 2024 19:31:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E8=B5=84=E4=BA=A7=E6=8E=88=E6=9D=83(?= =?UTF-8?q?=E4=BC=98=E5=8C=96)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/AssetAccreditServiceImpl.java | 157 +++++++++++------- .../impl/SourceAccreditServiceImpl.java | 4 +- etl-modules/etl-data-source/pom.xml | 1 + 3 files changed, 104 insertions(+), 58 deletions(-) diff --git a/etl-modules/etl-data-source/etl-data-source-server/src/main/java/com/etl/data/service/impl/AssetAccreditServiceImpl.java b/etl-modules/etl-data-source/etl-data-source-server/src/main/java/com/etl/data/service/impl/AssetAccreditServiceImpl.java index c04e26b..bd307a1 100644 --- a/etl-modules/etl-data-source/etl-data-source-server/src/main/java/com/etl/data/service/impl/AssetAccreditServiceImpl.java +++ b/etl-modules/etl-data-source/etl-data-source-server/src/main/java/com/etl/data/service/impl/AssetAccreditServiceImpl.java @@ -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) { - deleteAssetAccreditByAssetIds(assetAccreditReq); - List assetAccredits = new ArrayList<> (); - if (assetAccreditReq.getUserId() != null){ - 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 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(); + try { + deleteAssetAccreditByAssetIds(assetAccreditReq); + + List assetAccredits = new ArrayList<>(); + // 添加用户ID不为null的情况 + if (assetAccreditReq.getUserId() != null) { + assetAccredits.add(createAssetAccreditForUser(assetAccreditReq)); + } + // 添加部门ID的情况 + if (assetAccreditReq.getDeptIds() != null && !assetAccreditReq.getDeptIds().isEmpty()) { + Set deptIdsSet = new HashSet<>(assetAccreditReq.getDeptIds()); + List sysUsers = remoteSys.userList().getData().getRows(); + + List< AssetAccredit > collect = assetAccreditReq.getDeptIds ( ).stream ( ).map ( deptId -> + createAssetAccreditForDept ( assetAccreditReq, deptId ) ) + .collect ( Collectors.toList ( ) ); + + // 使用HashSet改善查找性能 + List 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()); + 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; + } @Override public Result deleteAssetAccreditByAssetIds(AssetAccreditReq assetAccreditReq) { @@ -91,23 +124,35 @@ public class AssetAccreditServiceImpl extends ServiceImpl< AssetAccreditMapper, this.remove ( new LambdaQueryWrapper< AssetAccredit> ( ) .eq ( AssetAccredit::getDataAssetId, assetAccreditReq.getDataAssetId () ) .eq ( AssetAccredit::getUserId, assetAccreditReq.getUserId () ) ); - } - // 获取所有系统部门ID和所有用户信息 - List< Long > deptIds = remoteSys.list ( ).getData ( ).stream ( ).map ( sysDept -> sysDept.getDeptId ( ) ).toList ( ); - List< SysUser > userList = remoteSys.userList ( ).getData ( ).getRows ( ); - // 筛选出属于指定部门ID的用户ID - List< Long > userIds = userList.stream ( ) - .filter ( sysUser -> deptIds.contains ( sysUser.getDeptId ( ) ) ).toList ( ).stream ( ) - .map ( SysUser::getUserId ).toList ( ); + }else { + // 获取所有系统部门ID和所有用户信息 + List< Long > deptIds = remoteSys.list ( ).getData ( ).stream ( ).map ( sysDept -> sysDept.getDeptId ( ) ).toList ( ); + List< SysUser > userList = remoteSys.userList ( ).getData ( ).getRows ( ); + // 筛选出属于指定部门ID的用户ID + List< Long > userIds = userList.stream ( ) + .filter ( sysUser -> deptIds.contains ( sysUser.getDeptId ( ) ) ).toList ( ).stream ( ) + .map ( SysUser::getUserId ).toList ( ); + + // 删除指定部门ID的源数据授权信息 + this.remove ( new LambdaQueryWrapper ( ) + .eq ( AssetAccredit::getDataAssetId, assetAccreditReq.getDataAssetId () ) + .in ( AssetAccredit::getDeptId, assetAccreditReq.getDeptIds () )); + // 删除属于指定部门ID的用户的源数据授权信息 + this.remove ( new LambdaQueryWrapper ( ) + .eq ( AssetAccredit::getDataAssetId, assetAccreditReq.getDataAssetId () ) + .in ( AssetAccredit::getUserId, userIds )); + } - // 删除指定部门ID的源数据授权信息 - this.remove ( new LambdaQueryWrapper ( ) - .eq ( AssetAccredit::getDataAssetId, assetAccreditReq.getDataAssetId () ) - .in ( AssetAccredit::getDeptId, assetAccreditReq.getDeptIds () )); - // 删除属于指定部门ID的用户的源数据授权信息 - this.remove ( new LambdaQueryWrapper ( ) - .eq ( AssetAccredit::getDataAssetId, assetAccreditReq.getDataAssetId () ) - .in ( AssetAccredit::getUserId, userIds )); return Result.success ( ); } + + public static void main(String[] args) { + + ThreadLocal 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()); + } } diff --git a/etl-modules/etl-data-source/etl-data-source-server/src/main/java/com/etl/data/service/impl/SourceAccreditServiceImpl.java b/etl-modules/etl-data-source/etl-data-source-server/src/main/java/com/etl/data/service/impl/SourceAccreditServiceImpl.java index af12d88..c7d4872 100644 --- a/etl-modules/etl-data-source/etl-data-source-server/src/main/java/com/etl/data/service/impl/SourceAccreditServiceImpl.java +++ b/etl-modules/etl-data-source/etl-data-source-server/src/main/java/com/etl/data/service/impl/SourceAccreditServiceImpl.java @@ -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 ); diff --git a/etl-modules/etl-data-source/pom.xml b/etl-modules/etl-data-source/pom.xml index df97723..299a9c7 100644 --- a/etl-modules/etl-data-source/pom.xml +++ b/etl-modules/etl-data-source/pom.xml @@ -15,6 +15,7 @@ etl-data-source-common etl-data-source-remote etl-data-source-server + etl-data-source-client