xiejun
2024-09-09 c826afb6dbc2c1d4433b3c127c5f30f9f72fb2dc
Source/plt-web/plt-web-parent/plt-web/src/main/java/com/vci/web/service/impl/UIManagerServiceImpl.java
@@ -1,22 +1,31 @@
package com.vci.web.service.impl;
import com.vci.client.mw.ClientContextVariable;
import com.vci.common.qt.object.QTConstants;
import com.vci.common.utility.ObjectUtility;
import com.vci.corba.common.PLException;
import com.vci.corba.framework.data.RoleRightInfo;
import com.vci.corba.omd.btm.BizType;
import com.vci.corba.portal.PortalService;
import com.vci.corba.portal.data.*;
import com.vci.dto.RoleRightDTO;
import com.vci.dto.UIAuthorDTO;
import com.vci.frameworkcore.compatibility.SmRoleQueryServiceI;
import com.vci.model.PLDefination;
import com.vci.pagemodel.PLDefinationVO;
import com.vci.pagemodel.PLUILayoutCloneVO;
import com.vci.pagemodel.RoleRightVO;
import com.vci.starter.web.exception.VciBaseException;
import com.vci.starter.web.pagemodel.*;
import com.vci.starter.web.pagemodel.BaseQueryObject;
import com.vci.starter.web.pagemodel.BaseResult;
import com.vci.starter.web.pagemodel.DataGrid;
import com.vci.starter.web.pagemodel.SessionInfo;
import com.vci.starter.web.util.VciBaseUtil;
import com.vci.starter.web.util.VciDateUtil;
import com.vci.starter.web.util.WebThreadLocalUtil;
import com.vci.web.service.OsBtmServiceI;
import com.vci.web.service.UIManagerServiceI;
import com.vci.web.util.*;
import org.apache.commons.lang3.StringUtils;
import com.vci.web.util.Func;
import com.vci.web.util.PlatformClientUtil;
import com.vci.web.util.UITools;
@@ -24,17 +33,16 @@
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.io.IOException;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.regex.Pattern;
/**
 * UI定义服务界面相关接口
@@ -49,6 +57,22 @@
     */
    @Resource
    private PlatformClientUtil platformClientUtil;
    /**
     * 角色
     */
    @Resource
    private SmRoleQueryServiceI smRoleQueryServiceI;
    /***
     * 是否是管理员
     */
    @Autowired
    RightControlUtil rightControlUtil;
    /**
     * 业务类型
     */
    @Resource
    private OsBtmServiceI osBtmServiceI;
    /**
     * 日志
@@ -95,6 +119,37 @@
        List<PLUILayout> pluiLayouts = Arrays.asList(res);
        dataGrid.setData(pluiLayouts);
        return dataGrid;
    }
    /**
     * ton通过业务类型和名称查询
     * @param btemName
     * @param context
     * @return
     * @throws PLException
     */
    public List<PLUILayout> getUIContextDataByBtName(String btemName,String context) throws PLException {
        VciBaseUtil.alertNotNull(btemName,"业务类型");
        List<PLUILayout> pluiLayoutList=new ArrayList<>();
        List<String> contextList= VciBaseUtil.str2List(context);
        if(StringUtils.isNotBlank(context)){
            contextList=VciBaseUtil.str2List(context);
        }else{
            contextList.add("");
        }
        contextList.stream().forEach(code->{
            PLUILayout[]  pluiLayouts= new PLUILayout[0];
            try {
                pluiLayouts = platformClientUtil.getUIService().getPLUILayoutEntityByTypeAndCode(btemName,code);
            } catch (PLException e) {
                e.printStackTrace();
            }
            if(pluiLayouts!=null&&pluiLayouts.length>0) {
                pluiLayoutList.addAll(Arrays.stream(pluiLayouts).collect(Collectors.toList()));
            }
        });
        return pluiLayoutList;
    }
    /**
@@ -726,6 +781,418 @@
    }
    /**
     * 获取UI授权树
     * @param treeQueryObject
     * @return
     * @throws Exception
     */
    @Override
    public List<Tree> getUIAuthor(BaseQueryObject treeQueryObject) throws Exception {
        Map<String, String> conditionMap = treeQueryObject.getConditionMap();
        if (conditionMap == null) {
            conditionMap = new HashMap<>();
        }
        String roleId = StringUtils.isBlank(conditionMap.get("roleId")) ? "" : conditionMap.get("roleId");
        String type = StringUtils.isBlank(conditionMap.get("type")) ? "" : conditionMap.get("type");
        String context = StringUtils.isBlank(conditionMap.get("context")) ? "" : conditionMap.get("context");
        boolean showCheckBox = Boolean.parseBoolean(conditionMap.get("showCheckBox"));
       Map<String,RoleRightVO> roleRightVOMap=new HashMap<>();
        if(StringUtils.isNotBlank(roleId)){
          String userName= WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId();
            RoleRightInfo[] rightInfos= platformClientUtil.getFrameworkService().getRoleRightList(roleId,userName);
            List<RoleRightVO>  roleRightVOList=roleRightDOO2VOS(Arrays.asList(rightInfos));
            roleRightVOMap=roleRightVOList.stream().collect(Collectors.toMap(RoleRightVO::getFuncId,roleRightVO ->roleRightVO));
        }
        BizType[] bizTypes=osBtmServiceI.getBizTypes(type);
        List<Tree> treeList=new ArrayList<>();
        Tree   rootNode =new Tree("root","功能模块","root");
        rootNode.setLevel(0);
        rootNode.setShowCheckbox(true);
        rootNode.setExpanded(true);
        List<Tree> childList=new ArrayList<>();
        for (int i = 0; i < bizTypes.length; i++) {
            Tree bizTypeTree = new Tree(bizTypes[i].oid,bizTypes[i].name,bizTypes[i]);//(btmItems[i].label+" ["+ btmItems[i].name+"]", btmItems[i]);
            bizTypeTree.setLevel(1);
            bizTypeTree.setShowCheckbox(true);
            bizTypeTree.setParentId(rootNode.getOid());
            bizTypeTree.setParentName(rootNode.getText());
            bizTypeTree.setShowCheckbox(true);
            bizTypeTree.setParentBtmName(bizTypes[i].name);
            childList.add(bizTypeTree);
            if(roleRightVOMap.containsKey(bizTypes[i].oid)){
                List<PLUILayout>contextList=getUIContextDataByBtName(bizTypes[i].name,context);
                List<Tree> btmChildList=new ArrayList<>();
                btmChildList.add(bizTypeTree);
                setChildNode(btmChildList,contextList,roleRightVOMap,showCheckBox);
            }
        }
        rootNode.setChildren(childList);
        treeList.add(rootNode);
        return treeList;
    }
    /***
     * UI授权
     * @param uiAuthorDTO
     * @return
     * @throws Exception
     */
    @Override
    public boolean authorizedUI(UIAuthorDTO uiAuthorDTO) throws Exception {
        boolean res=false;
        if(uiAuthorDTO==null||CollectionUtil.isEmpty(uiAuthorDTO.getSelectTreeList())){
            throw  new VciBaseException("请选择节点进行授权!");
        }
        BaseQueryObject treeQueryObject=new BaseQueryObject();
        Map<String,String> conditionMap = new HashMap<>();
        conditionMap.put("roleId",uiAuthorDTO.getRoleId());
        conditionMap.put("type",uiAuthorDTO.getType());
        conditionMap.put("context",uiAuthorDTO.getContext());
        conditionMap.put("showCheckBox","true");
        treeQueryObject.setConditionMap(conditionMap);
        List<Tree> treeList=this.getUIAuthor(treeQueryObject);
        HashMap<String,Tree> allTreeMap=new HashMap<>();
        Map<String,RoleRightVO> roleRightVOMap=new HashMap<>();
        if(!CollectionUtil.isEmpty(treeList)){
            if(StringUtils.isNotBlank(uiAuthorDTO.getRoleId())){
                String userName= WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId();
                RoleRightInfo[] rightInfos= platformClientUtil.getFrameworkService().getRoleRightList(uiAuthorDTO.getRoleId(),userName);
                List<RoleRightVO>  roleRightVOList=roleRightDOO2VOS(Arrays.asList(rightInfos));
                roleRightVOMap=roleRightVOList.stream().collect(Collectors.toMap(RoleRightVO::getFuncId,roleRightVO ->roleRightVO));
            }
            convertTreeDOO2Map(treeList,allTreeMap);
            List<RoleRightDTO> roleRightDTOList=new ArrayList<>();
            List<Tree>  selectTreeList= uiAuthorDTO.getSelectTreeList();
            getSelectedRoleRightObjs(uiAuthorDTO.getRoleId(),selectTreeList,allTreeMap,roleRightDTOList);
        }
        return res;
    }
    /**
     *
     * @param roleOid
     * @param selectTreeList
     * @param allTreeMap
     * @param roleRightDTOList
     */
    private void getSelectedRoleRightObjs(String roleOid,List<Tree>  selectTreeList,HashMap<String,Tree> allTreeMap,  List<RoleRightDTO> roleRightDTOList){
        Date date=new Date();
        Map<String,RoleRightDTO> roleRightDTOMap=new HashMap<>();
        selectTreeList.stream().forEach(tree -> {
            RoleRightDTO roleRightDTO=new RoleRightDTO();
            String id=ObjectUtility.getNewObjectID36();
            Object data=  tree.getData();
            if(data instanceof String){
                getRightValue(roleOid,tree,allTreeMap,false,roleRightDTOMap);//向下获取所有模块的权限值
            }else if (!(data instanceof PLTabButton)) {//业务类型
                getRightValue(roleOid,tree,allTreeMap,true,roleRightDTOMap);//向上处理
                getRightValue(roleOid,tree,allTreeMap,false,roleRightDTOMap);//向下处理(包含当前节点)
            }else if (data instanceof PLTabButton) {//按钮
            }
        });
    }
    /**
     * 获取权限
     * @param isUp 是否是向上获取,如果是向上获取,传进来的必然是模块节点,且上级模块必然是没有选中
     */
    private void getRightValue(String roleId,Tree node,HashMap<String,Tree> allTreeMap,boolean isUp,Map<String,RoleRightDTO> rightMap){
        SessionInfo sessionInfo = WebThreadLocalUtil.getCurrentUserSessionInfoInThread();
        String currentUserName = sessionInfo.getUserId();
        boolean isDeveloper= rightControlUtil.isDeveloper(currentUserName);
        String parentOid=node.getParentId();
        if(allTreeMap.containsKey(parentOid)){
            String id=ObjectUtility.getNewObjectID36();
            Tree parentNode =allTreeMap.get(parentOid);
            Object parentData=  parentNode.getData();
            if(isUp) {//向上获取,存储每个上级模块的权限值
                while (!"root".equals(parentNode.getData())){
                    String funcId = "";
                    if (parentData instanceof BizType) {
                        BizType bizType = (BizType) parentData;
                        funcId = bizType.name;
                    } else if (parentData instanceof PLUILayout) {
                        PLUILayout context = (PLUILayout)parentData;
                        funcId = context.plOId;
                    } else if (parentData instanceof PLTabPage) {
                        PLTabPage tab = (PLTabPage) parentData;
                        funcId = tab.plOId;
                    } else if (parentData instanceof PLPageDefination){
                        PLPageDefination pageDef = (PLPageDefination) parentData;
                        funcId = pageDef.plOId;
                    } else if (parentData instanceof PLTabButton) {
                        PLTabButton but = (PLTabButton)parentData;
                        funcId = but.plOId;
                    }
                    RoleRightDTO roleRightDTO = new RoleRightDTO();
                    roleRightDTO.setId(id);//主键
                    roleRightDTO.setFuncId(funcId);
                    if(isDeveloper) {
                        roleRightDTO.setRightType((short) 1);//权限类型 权限类型,超级管理员给管理员授权为1,管理员给普通用户授权为2
                    }else{
                        roleRightDTO.setRightType((short) 2);
                    }
                    roleRightDTO.setRightValue(1);// 权限值,没有操作的模块权限值存储为0
                    roleRightDTO.setRoleId(roleId);//角色ID
                    roleRightDTO.setCreateUser(currentUserName);//创建者
                    roleRightDTO.setCreateTime(new Date());//创建时间
                    roleRightDTO.setModifyUser(currentUserName);//修改者
                    roleRightDTO.setModifyTime(new Date());//修改时间
                    roleRightDTO.setLicensor("");
                    if(!rightMap.containsKey(funcId)){
                        rightMap.put(funcId, roleRightDTO);
                    }
                }
            }else{
                String funcId = "";
                if(parentData instanceof String){
                    funcId = (String)parentData;
                } else if (parentData instanceof BizType) {
                    BizType bizType = (BizType)parentData;
                    funcId = bizType.name;
                } else if (parentData instanceof PLUILayout) {
                    PLUILayout context = (PLUILayout)parentData;
                    funcId = context.plOId;
                } else if (parentData instanceof PLTabPage) {
                    PLTabPage tab = (PLTabPage) parentData;
                    funcId = tab.plOId;
                } else if (parentData instanceof PLPageDefination){
                    PLPageDefination pageDef = (PLPageDefination) parentData;
                    funcId = pageDef.plOId;
                } else if (parentData instanceof PLTabButton) {
                    PLTabButton but = (PLTabButton)parentData;
                    funcId = but.plOId;
                }
                if(!(parentData instanceof PLPageDefination)) {//子节点不是操作
                    if(!rightMap.containsKey(funcId)&&!funcId.equals("root")){
                        RoleRightDTO roleRightDTO = new RoleRightDTO();
                        roleRightDTO.setFuncId(funcId);
                        if(isDeveloper) {
                            roleRightDTO.setRightType((short) 1);//权限类型 权限类型,超级管理员给管理员授权为1,管理员给普通用户授权为2
                        }else{
                            roleRightDTO.setRightType((short) 2);
                        }
                        roleRightDTO.setRightValue(0);//没有操作的模块权限值存储为0
                        roleRightDTO.setRoleId(roleId);
                        roleRightDTO.setCreateUser(currentUserName);
                        roleRightDTO.setCreateTime(new Date());
                        roleRightDTO.setModifyUser(currentUserName);
                        roleRightDTO.setModifyTime(new Date());
                        roleRightDTO.setLicensor("");
                        rightMap.put(funcId, roleRightDTO);
                    }
                    for(int i = 0;i < parentNode.getChildren().size();i++){
                        //对每个子向下递归遍历
                        getRightValue(roleId,parentNode.getChildren().get(i),allTreeMap,false,rightMap);
                    }
                }else {
                    if(!rightMap.containsKey(funcId)){
                        RoleRightDTO roleRightDTO = new RoleRightDTO();
                        roleRightDTO.setFuncId(funcId);
                        roleRightDTO.setRightType((short)2); // 设置UI权限
                        roleRightDTO.setRightValue(countRightValue(parentNode,true));//没有操作的模块权限值存储为0
                        roleRightDTO.setRoleId(roleId);
                        roleRightDTO.setCreateUser(currentUserName);
                        roleRightDTO.setCreateTime(new Date());
                        roleRightDTO.setModifyUser(currentUserName);
                        roleRightDTO.setModifyTime(new Date());
                        roleRightDTO.setLicensor("");
                        rightMap.put(funcId, roleRightDTO);
                    }
                }
            }
        }
    }
    /**
     * 传入直接挂接操作的模块的节点,计算该节点的权限值
     * @param node 模块节点
     * @param isAll 是否子级全部选中
     * @return
     */
    private long countRightValue(Tree node,boolean isAll){
        long value = 0;
        for(int i = 0;i < node.getChildren().size();i++){
            Tree childNode = (Tree)node.getChildren().get(i);
            if(isAll && node.getData() instanceof PLTabButton ){
                PLTabButton obj = (PLTabButton)node.getData();
                value += (long)Math.pow(2, obj.plSeq);//累计加上各个操作的权限值
            }
        }
        return value;
    }
    /**
     *
     * @param treeList 树节点
     * @param allTreeMap,所有的节点
     */
    private void convertTreeDOO2Map(List<Tree> treeList,Map<String,Tree> allTreeMap){
        Optional.ofNullable(treeList).orElseGet(()->new ArrayList<Tree>()).stream().forEach(tree -> {
            List<Tree> childTreeList= tree.getChildren();
            allTreeMap.put(tree.getOid(),tree);
            if(!CollectionUtil.isEmpty(childTreeList)){
                convertTreeDOO2Map(childTreeList,allTreeMap);
            }
        });
    }
    private void setChildNode(List<Tree> parentTree, List<PLUILayout>contextList,Map<String,RoleRightVO> roleRightVOMap,boolean isShowCheckBox){
        Optional.ofNullable(parentTree).orElseGet(()->new ArrayList<Tree>()).stream().forEach(pTree -> {
            Object funcObj=  pTree.getData();
            List<Tree> chiledTreeList=new ArrayList<>();
            if (funcObj instanceof BizType) {//业务类型
                BizType bizType = (BizType) funcObj;
                if(!CollectionUtil.isEmpty(contextList)) {
                    contextList.stream().forEach(context->{
                        Tree childTree=new Tree(context.plOId,context.plName+"("+context.plCode+")",context);
                        childTree.setParentName(pTree.getText());
                        childTree.setParentBtmName(pTree.getParentBtmName());
                        childTree.setParentId(pTree.getOid());
                        childTree.setLevel(pTree.getLevel()+1);
                        childTree.setShowCheckbox(isShowCheckBox);
                        chiledTreeList.add(childTree);
                    });
                    pTree.setChildren(chiledTreeList);
                }
                if(!CollectionUtil.isEmpty(chiledTreeList)) {
                    setChildNode(chiledTreeList, contextList, roleRightVOMap, isShowCheckBox);
                }
            }else  if (funcObj instanceof PLUILayout){//UI
                PLUILayout context = (PLUILayout) funcObj;
                PLTabPage[] pages = new PLTabPage[0];
                try {
                    pages = platformClientUtil.getUIService().getPLTabPagesByPageDefinationOId(context.plOId);
                } catch (PLException e) {
                    e.printStackTrace();
                }
                if(pages!=null&&pages.length>0){
                    List<PLTabPage> plTabPageList= Arrays.stream(pages).collect(Collectors.toList());
                    plTabPageList.stream().forEach(plTabPage -> {
                        Tree childTree=new Tree(plTabPage.plOId,plTabPage.plName,plTabPage);
                        childTree.setParentName(pTree.getText());
                        childTree.setParentId(pTree.getOid());
                        childTree.setParentBtmName(pTree.getParentBtmName());
                        childTree.setLevel(pTree.getLevel()+1);
                        childTree.setShowCheckbox(isShowCheckBox);
                        chiledTreeList.add(childTree);
                    });
                    pTree.setChildren(chiledTreeList);
                }
                if(!CollectionUtil.isEmpty(chiledTreeList)) {
                    setChildNode(chiledTreeList, contextList, roleRightVOMap, isShowCheckBox);
                }
            }else if (funcObj instanceof PLTabPage) {//上下文
                PLTabPage plTabPage = (PLTabPage) funcObj;
                List<PLPageDefination>plPageDefinationList=new ArrayList<>();
                try {
                    PLPageDefination[] pLPageDefinations = platformClientUtil.getUIService().getPLPageDefinationsByPageContextOId(plTabPage.plOId);
                    if(pLPageDefinations!=null&&pLPageDefinations.length>0){
                        plPageDefinationList= Arrays.stream(pLPageDefinations).collect(Collectors.toList());
                        plPageDefinationList.stream().forEach(plPageDefination -> {
                            Tree childTree=new Tree(plPageDefination.plOId,plPageDefination.name,plPageDefination);
                            childTree.setParentName(pTree.getText());
                            childTree.setParentId(pTree.getOid());
                            childTree.setParentBtmName(pTree.getParentBtmName());
                            childTree.setLevel(pTree.getLevel()+1);
                            childTree.setShowCheckbox(isShowCheckBox);
                            chiledTreeList.add(childTree);
                        });
                        pTree.setChildren(chiledTreeList);
                    }
                    if(!CollectionUtil.isEmpty(chiledTreeList)) {
                        setChildNode(chiledTreeList, contextList, roleRightVOMap, isShowCheckBox);
                    }
                } catch (PLException e) {
                    e.printStackTrace();
                }
            }else if (funcObj instanceof PLPageDefination) {//
                PLPageDefination plPageDefination = (PLPageDefination) funcObj;
                try {
                    List<PLTabButton>plTabButtonList=new ArrayList<>();
                    PLTabButton[] pLTabButtons = platformClientUtil.getUIService().getPLTabButtonsByTableOId(plPageDefination.plOId);
                    if(pLTabButtons!=null&&pLTabButtons.length>0){
                        plTabButtonList= Arrays.stream(pLTabButtons).collect(Collectors.toList());
                        plTabButtonList.stream().forEach(plTabButton -> {
                            Tree childTree=new Tree(plTabButton.plOId,plTabButton.plLabel,plTabButton);
                            childTree.setParentName(pTree.getText());
                            childTree.setParentId(pTree.getOid());
                            childTree.setParentBtmName(pTree.getParentBtmName());
                            childTree.setLevel(pTree.getLevel()+1);
                            childTree.setShowCheckbox(isShowCheckBox);
                            childTree.setLeaf(true);
                            chiledTreeList.add(childTree);
                        });
                        pTree.setChildren(chiledTreeList);
                    }
                    if(!CollectionUtil.isEmpty(chiledTreeList)) {
                        setChildNode(chiledTreeList, contextList, roleRightVOMap, isShowCheckBox);
                    }
                } catch (PLException e) {
                    e.printStackTrace();
                }
            }else if (funcObj instanceof PLTabButton) {//按钮
                PLTabButton plTabButton= (PLTabButton) funcObj;
                String id =plTabButton.plTableOId;
                if(roleRightVOMap.containsKey(id)){
                    RoleRightVO roleRightVO = roleRightVOMap.get(id);
                    Long rightValue =  roleRightVO.getRightValue();
                    int nodeValue = plTabButton.plSeq;
                    if (nodeValue >= 0 && nodeValue <= 63) {
                        long preValue = (rightValue >> nodeValue) & 1;
                        if (preValue == 1) {
                            pTree.setChecked(true);
                        }
                    }
                }else{
                    pTree.setChecked(false);
                }
            }
        });
    }
    /**
     * UI角色对象转换
     * @param infos
     * @return
     */
    private List<RoleRightVO> roleRightDOO2VOS(List<RoleRightInfo> infos){
        List<RoleRightVO> roleRightVOS=new ArrayList<>();
        Optional.ofNullable(infos).orElseGet(()->new ArrayList<>()).stream().forEach(info -> {
            RoleRightVO vo=roleRightDOO2VO(info);
            roleRightVOS.add(vo);
        });
        return roleRightVOS;
    }
    /**
     * UI角色对象转换
     * @param info
     * @return
     */
    private RoleRightVO roleRightDOO2VO(RoleRightInfo info){
        RoleRightVO vo=new RoleRightVO();
        vo.setId(info.id);
        vo.setCreateTime(VciDateUtil.date2Str(VciDateUtil.long2Date(info.createTime),""));
        vo.setCreateUser(info.createUser);
        vo.setRoleId(info.roleId);
        vo.setRightType(info.rightType);
        vo.setLicensor(info.licensor);
        vo.setRightValue(info.rightValue);
        vo.setFuncId(info.funcId);
        vo.setModifyTime(VciDateUtil.date2Str(VciDateUtil.long2Date(info.modifyTime),""));
        vo.setModifyUser(info.modifyUser);
        return vo;
    }
    /**
     * 控制区节点及其子节点的克隆
     * @param obj
     */