@ -2,8 +2,7 @@ package apelet.tenantadmin.tenant.service.impl;
@@ -2,8 +2,7 @@ package apelet.tenantadmin.tenant.service.impl;
import apelet.common.core.base.dao.BaseDaoMapper ;
import apelet.common.core.base.service.BaseService ;
import apelet.common.core.object.CallResult ;
import apelet.common.core.object.MyOrderParam ;
import apelet.common.core.object.* ;
import apelet.common.online.model.OnlineForm ;
import apelet.common.online.model.OnlinePage ;
import apelet.common.online.service.OnlineFormService ;
@ -15,8 +14,11 @@ import apelet.tenantadmin.tenant.model.RoleFormButton;
@@ -15,8 +14,11 @@ import apelet.tenantadmin.tenant.model.RoleFormButton;
import apelet.tenantadmin.tenant.service.IRoleFormButtonService ;
import apelet.tenantadmin.tenant.util.WidgetJsonButtonParser ;
import cn.hutool.core.collection.CollectionUtil ;
import cn.hutool.core.util.StrUtil ;
import com.alibaba.fastjson.JSON ;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper ;
import com.github.pagehelper.Page ;
import com.github.pagehelper.page.PageMethod ;
import lombok.extern.slf4j.Slf4j ;
import org.jetbrains.annotations.NotNull ;
import org.springframework.beans.factory.annotation.Autowired ;
@ -60,11 +62,10 @@ public class RoleFormButtonServiceImpl extends BaseService<RoleFormButton, Long>
@@ -60,11 +62,10 @@ public class RoleFormButtonServiceImpl extends BaseService<RoleFormButton, Long>
return mapper . getListByFilter ( filter , orderBy ) ;
}
// ========== 核心:查询按钮配置VO ==========
// ========== 核心:查询按钮配置VO(合并当前用户所有角色) ==========
@Override
public Map < String , Object > getButtonConfigVo ( Long roleId , Long formId ) {
public Map < String , Object > getButtonConfigVo ( Long formId ) {
Map < String , Object > result = new HashMap < > ( ) ;
result . put ( "roleId" , roleId ) ;
result . put ( "formId" , formId ) ;
// 1. 获取 widgetJson,提取所有开启的按钮
@ -73,22 +74,25 @@ public class RoleFormButtonServiceImpl extends BaseService<RoleFormButton, Long>
@@ -73,22 +74,25 @@ public class RoleFormButtonServiceImpl extends BaseService<RoleFormButton, Long>
result . put ( "buttonList" , Collections . emptyList ( ) ) ;
return result ;
}
List < WidgetJsonButtonParser . ButtonInfo > enabledButtons = WidgetJsonButtonParser . extractEnabledButtons ( onlineForm . getWidgetJson ( ) ) ;
List < WidgetJsonButtonParser . ButtonInfo > enabledButtons =
WidgetJsonButtonParser . extractEnabledButtons ( onlineForm . getWidgetJson ( ) ) ;
// 2. 获取当前登录用户的所有角色
Set < Long > roleIds = getCurrentUserRoleIds ( ) ;
// 2. 查询已存储的配置
RoleFormButton config = getByRoleIdAndFormId ( roleId , formId ) ;
// 3. 解析已勾选的按钮名称集合
// 3. 遍历所有角色,合并各角色在该表单的勾选按钮(取并集)
Set < String > checkedNames = new HashSet < > ( ) ;
if ( config ! = null & & config . getButtonConfig ( ) ! = null ) {
List < WidgetJsonButtonParser . ButtonInfo > savedButtons =
JSON . parseArray ( config . getButtonConfig ( ) ,
WidgetJsonButtonParser . ButtonInfo . class ) ;
for ( WidgetJsonButtonParser . ButtonInfo btn : savedButtons ) {
if ( Boolean . TRUE . equals ( btn . getEnabled ( ) ) ) {
checkedNames . add ( btn . getName ( ) ) ;
if ( CollectionUtil . isNotEmpty ( roleIds ) ) {
for ( Long roleId : roleIds ) {
RoleFormButton config = getByRoleIdAndFormId ( roleId , formId ) ;
if ( config ! = null & & config . getButtonConfig ( ) ! = null ) {
List < WidgetJsonButtonParser . ButtonInfo > savedButtons =
JSON . parseArray ( config . getButtonConfig ( ) ,
WidgetJsonButtonParser . ButtonInfo . class ) ;
for ( WidgetJsonButtonParser . ButtonInfo btn : savedButtons ) {
if ( Boolean . TRUE . equals ( btn . getEnabled ( ) ) ) {
checkedNames . add ( btn . getName ( ) ) ;
}
}
}
}
}
@ -102,11 +106,21 @@ public class RoleFormButtonServiceImpl extends BaseService<RoleFormButton, Long>
@@ -102,11 +106,21 @@ public class RoleFormButtonServiceImpl extends BaseService<RoleFormButton, Long>
buttonList . add ( item ) ;
}
result . put ( "id" , config ! = null ? config . getId ( ) : null ) ;
result . put ( "buttonList" , buttonList ) ;
return result ;
}
/ * *
* 获取当前登录用户的所有角色ID集合 ( 从 TokenData 中解析 ) 。
* /
private Set < Long > getCurrentUserRoleIds ( ) {
TokenData tokenData = TokenData . takeFromRequest ( ) ;
if ( tokenData = = null | | StrUtil . isBlank ( tokenData . getRoleIds ( ) ) ) {
return Collections . emptySet ( ) ;
}
return StrUtil . split ( tokenData . getRoleIds ( ) , ',' ) . stream ( ) . map ( Long : : valueOf ) . collect ( Collectors . toSet ( ) ) ;
}
// ========== 核心:批量保存按钮配置(事务) ==========
@Override
@Transactional ( rollbackFor = Exception . class )
@ -190,7 +204,7 @@ public class RoleFormButtonServiceImpl extends BaseService<RoleFormButton, Long>
@@ -190,7 +204,7 @@ public class RoleFormButtonServiceImpl extends BaseService<RoleFormButton, Long>
private static @NotNull Map < String , Object > getButtonResult ( OnlineForm form , List < WidgetJsonButtonParser . ButtonInfo > buttons ) {
Map < String , Object > item = new HashMap < > ( ) ;
item . put ( "formId" , form . getFormId ( ) ) ;
item . put ( "formId" , form . getFormId ( ) . toString ( ) ) ;
item . put ( "formName" , form . getFormName ( ) ) ;
List < Map < String , Object > > buttonList = new ArrayList < > ( ) ;
if ( ! CollectionUtils . isEmpty ( buttons ) ) {
@ -206,24 +220,82 @@ public class RoleFormButtonServiceImpl extends BaseService<RoleFormButton, Long>
@@ -206,24 +220,82 @@ public class RoleFormButtonServiceImpl extends BaseService<RoleFormButton, Long>
}
@Override
public List < Map < String , Object > > listAllFormButtons ( MyOrderParam orderParam ) {
// 查询已发布的 page
public MyPageData < Map < String , Object > > listAllFormButtons ( Long roleId , MyOrderParam orderParam , MyPageParam page Param ) {
// 1. 查询已发布的 page(不分页,获取全量)
List < OnlinePage > pageList = onlinePageService . queryByEnableList ( ) ;
if ( CollectionUtil . isEmpty ( pageList ) ) {
return Collections . emptyList ( ) ;
return new MyPageData < > ( Collections . emptyList ( ) , 0L ) ;
}
Set < Long > pageIdSet = pageList . stream ( ) . map ( OnlinePage : : getPageId ) . collect ( Collectors . toSet ( ) ) ;
// 分页查询在线表单
// 2. 分页查询 OnlineForm
if ( pageParam ! = null ) {
PageMethod . startPage ( pageParam . getPageNum ( ) , pageParam . getPageSize ( ) ) ;
}
String orderBy = MyOrderParam . buildOrderBy ( orderParam , OnlineForm . class ) ;
OnlineForm onlineForm = new OnlineForm ( ) ;
onlineForm . setPageIdSet ( pageIdSet ) ;
List < OnlineForm > formList = onlineFormService . getOnlineFormList ( null , orderBy ) ;
List < OnlineForm > formList = onlineFormService . getOnlineFormList ( onlineForm , orderBy ) ;
long total = formList instanceof Page ? ( ( Page ) formList ) . getTotal ( ) : formList . size ( ) ;
// 3. 如果传了 roleId,查询 RoleFormButton 获取勾选状态
Map < Long , Set < String > > formCheckedMap = Collections . emptyMap ( ) ;
if ( roleId ! = null ) {
List < RoleFormButton > configList = this . listByRoleId ( roleId ) ;
if ( CollectionUtil . isNotEmpty ( configList ) ) {
formCheckedMap = new HashMap < > ( configList . size ( ) ) ;
for ( RoleFormButton config : configList ) {
formCheckedMap . put ( config . getFormId ( ) , extractCheckedNames ( config ) ) ;
}
}
}
// 4. 构建结果
List < Map < String , Object > > result = new ArrayList < > ( ) ;
for ( OnlineForm form : formList ) {
List < WidgetJsonButtonParser . ButtonInfo > buttons =
WidgetJsonButtonParser . extractEnabledButtons ( form . getWidgetJson ( ) ) ;
result . add ( getButtonResult ( form , buttons ) ) ;
Map < String , Object > item = getButtonResult ( form , buttons ) ;
if ( roleId ! = null ) {
Set < String > checkedNames = formCheckedMap . get ( form . getFormId ( ) ) ;
if ( CollectionUtil . isNotEmpty ( checkedNames ) ) {
applyCheckedStatus ( item , checkedNames ) ;
}
}
result . add ( item ) ;
}
return new MyPageData < > ( result , total ) ;
}
/ * *
* 从 RoleFormButton . buttonConfig 中提取已勾选的按钮名称集合 。
* /
private Set < String > extractCheckedNames ( RoleFormButton config ) {
if ( config . getButtonConfig ( ) = = null ) {
return Collections . emptySet ( ) ;
}
List < WidgetJsonButtonParser . ButtonInfo > savedButtons =
JSON . parseArray ( config . getButtonConfig ( ) , WidgetJsonButtonParser . ButtonInfo . class ) ;
if ( CollectionUtils . isEmpty ( savedButtons ) ) {
return Collections . emptySet ( ) ;
}
return savedButtons . stream ( )
. filter ( b - > Boolean . TRUE . equals ( b . getEnabled ( ) ) )
. map ( WidgetJsonButtonParser . ButtonInfo : : getName )
. collect ( Collectors . toSet ( ) ) ;
}
/ * *
* 将已勾选的按钮名称集合应用到返回结果中 。
* /
private void applyCheckedStatus ( Map < String , Object > item , Set < String > checkedNames ) {
@SuppressWarnings ( "unchecked" )
List < Map < String , Object > > buttonList =
( List < Map < String , Object > > ) item . get ( "buttonList" ) ;
if ( buttonList ! = null ) {
for ( Map < String , Object > btn : buttonList ) {
btn . put ( "checked" , checkedNames . contains ( btn . get ( "name" ) ) ) ;
}
}
return result ;
}
}