JEPaaS 社区

 找回密码
 立即注册
JEPaaS低代码平台-官网
查看: 2964|回复: 0

[知识经验] 重写树形后台方法

[复制链接]

3

主题

3

帖子

155

积分

凯特员工

积分
155
发表于 2021-9-13 14:04:24 | 显示全部楼层 |阅读模式
一、实现效果
如果需求需要的树形结构数据,平台配置过滤满足不了的话,就要根据需求条件重写后台getTree方法和load方法
二、实现思路

三、具体操作
重写方法加入当前需求的过滤条件后,在平台配置的Action改为方法中的路径。(提交git代码注意先更新再提交)
四、相关代码(可选)
  1. package com.project.sidri.controller;

  2. import cn.hutool.core.util.StrUtil;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.google.common.collect.Lists;
  7. import com.je.core.base.AbstractPlatformController;
  8. import com.je.core.base.MethodArgument;
  9. import com.je.core.entity.extjs.JSONTreeNode;
  10. import com.je.core.exception.PlatformException;
  11. import com.je.core.exception.PlatformExceptionEnum;
  12. import com.je.core.mapper.query.Condition;
  13. import com.je.core.mapper.query.Query;
  14. import com.je.core.result.BaseRespResult;
  15. import com.je.core.service.PCDynaBeanTemplate;
  16. import com.je.core.service.PCDynaServiceTemplate;
  17. import com.je.core.service.PCServiceTemplate;
  18. import com.je.core.util.SecurityUserHolder;
  19. import com.je.core.util.bean.BeanUtils;
  20. import com.je.core.util.bean.DynaBean;
  21. import com.je.ibatis.extension.conditions.ConditionsWrapper;
  22. import com.je.ibatis.extension.plugins.pagination.Page;

  23. import com.je.wf.processVo.ProcessVoBuilder;
  24. import com.project.sidri.service.LoadTreeService;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.stereotype.Controller;
  27. import org.springframework.web.bind.annotation.RequestMapping;
  28. import org.springframework.web.bind.annotation.RequestMethod;
  29. import org.springframework.web.bind.annotation.ResponseBody;

  30. import java.util.ArrayList;
  31. import java.util.HashSet;
  32. import java.util.List;
  33. import java.util.Map;

  34. import static com.je.wf.processVo.ProcessVoBuilder.jsonAssist;

  35. /**
  36. * @ClassName TreeLodeDemoController
  37. * @Description
  38. * @Author Administrator
  39. * @Date 2021/7/15 0015 9:40
  40. * @Version V1.0
  41. */
  42. @Controller
  43. @RequestMapping(value = "/je/demoDynaTree")
  44. public class LoadTreeController extends AbstractPlatformController{


  45.     @Autowired
  46.     PCDynaBeanTemplate dynabeanTemplate;

  47.     @Autowired
  48.     PCDynaServiceTemplate serviceTemplate;

  49.     @Autowired
  50.     //@Qualifier("LoadTreeService")
  51.     protected LoadTreeService loadTreeService;
  52.     /**
  53.      * 默认的列表读取
  54.      *
  55.      * @param param 请求参数封装对象
  56.      */
  57.     @RequestMapping(value = "/load", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
  58.     @ResponseBody
  59.     @Override
  60.     public void load(MethodArgument param) {
  61.         Page page = new Page<>(param.getPage(), param.getLimit());
  62.         Query query = param.getQuery();
  63.         List<Condition> tree = query.getTree();
  64.         List<Condition> custom = query.getCustom();
  65.         String userId = SecurityUserHolder.getCurrentUser().getUserId();
  66.         String userCode = SecurityUserHolder.getCurrentUser().getUserCode();
  67.         //获取登录人相关数据
  68.         List<DynaBean> lists = metaService.select("RWGL_RWXX", ConditionsWrapper.builder().eq("RWXX_RWSFGD_CODE", 1)
  69.                 .like("RWXX_FZRID", userId)
  70.                 .or().like("RWXX_FZRID", userCode)
  71.                 .or().like("RWXX_ZPRID", userId)
  72.                 .or().like("RWXX_ZPRID", userCode)
  73.                 .or().like("RWXX_CSRID", userId)
  74.                 .or().like("RWXX_CSRID", userCode)
  75.                 .or().like("SY_CREATEUSERID",userId));

  76.         //获取所有根节点主键
  77.         HashSet<String> sites = new HashSet<>();
  78.         for (int i = 0; i < lists.size(); i++) {
  79.             String sy_path = lists.get(i).getStr("SY_PATH");
  80.             String[] a = sy_path.split("/");
  81.             if(a.length > 2){
  82.                 sites.add(a[2]);
  83.             }
  84.         }
  85.         //拼接查询条件
  86.         Condition condition = new Condition();
  87.         condition.setType("and");
  88.         List<Condition> valueCondition = new ArrayList<>();
  89.         int i = 0;
  90.         for (String str : sites){
  91.             Condition con = new Condition();
  92.             if(i != 0){
  93.                 con.setCn("or");
  94.             }
  95.             con.setValue(str);
  96.             con.setType("like");
  97.             con.setCode("SY_PATH");
  98.             valueCondition.add(con);
  99.             i++;
  100.         }
  101.         condition.setValue(valueCondition);
  102.         tree.add(condition);
  103.         query.setTree(tree);

  104.         Condition customCon = new Condition();
  105.         customCon.setValue("1");
  106.         customCon.setCode("RWXX_RWSFGD_CODE");
  107.         customCon.setType("=");
  108.         custom.add(customCon);
  109.         super.load(param);
  110.     }

  111.     /**
  112.      * 默认平台
  113.      *
  114.      * @param param 请求参数封装对象
  115.      */
  116.     @RequestMapping(value = "/getTree", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
  117.     @ResponseBody
  118.     public void getTree(MethodArgument param) {
  119.         try {
  120.             Query query = new Query();
  121.             //query.addCustom();
  122.             manager.getTree(param);
  123.             if (isSecurity(param)) {
  124.                 JSONTreeNode tree = loadTreeService.getTree(param);
  125.                 if (tree != null) {
  126.                     this.toWrite(ProcessVoBuilder.jsonAssist.buildModelJson(tree, param.getExcludes().split(",")), param);
  127.                 } else {
  128.                     this.toWrite("{}", param);
  129.                 }
  130.             } else {
  131.                 this.dynaManager.getTree(param);
  132.             }

  133.         } catch (PlatformException var3) {
  134.             throw var3;
  135.         } catch (Exception var4) {
  136.             throw new PlatformException("数据加载失败!", PlatformExceptionEnum.UNKOWN_ERROR, var4);
  137.         }

  138.     }




  139. }
复制代码
  1. package com.project.sidri.service;

  2. import com.je.core.base.MethodArgument;
  3. import com.je.core.entity.extjs.JSONTreeNode;
  4. import com.je.ibatis.extension.plugins.pagination.Page;

  5. import java.util.List;
  6. import java.util.Map;

  7. public interface LoadTreeService {



  8.     JSONTreeNode getTree(MethodArgument param);

  9. }
复制代码
  1. package com.project.sidri.service;

  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.je.core.base.MethodArgument;
  5. import com.je.core.constants.ConstantVars;
  6. import com.je.core.entity.extjs.JSONTreeNode;
  7. import com.je.core.exception.PlatformException;
  8. import com.je.core.exception.PlatformExceptionEnum;

  9. import com.je.core.mapper.query.Query;
  10. import com.je.core.result.BaseRespResult;
  11. import com.je.core.service.*;
  12. import com.je.core.util.ArrayUtils;
  13. import com.je.core.util.SecurityUserHolder;
  14. import com.je.core.util.StringUtil;
  15. import com.je.core.util.bean.BeanUtils;
  16. import com.je.core.util.bean.DynaBean;
  17. import com.je.develop.vo.FuncInfo;
  18. import com.je.develop.vo.FuncPermVo;
  19. import com.je.develop.vo.FuncQueryStrategy;
  20. import com.je.ibatis.extension.conditions.ConditionsWrapper;
  21. import com.je.ibatis.extension.enums.Conditions;
  22. import com.je.ibatis.extension.plugins.pagination.Page;

  23. import com.je.rbac.model.EndUser;
  24. import com.sun.xml.bind.v2.model.core.ID;
  25. import lombok.extern.slf4j.Slf4j;
  26. import org.apache.commons.lang.StringUtils;
  27. import org.apache.derby.iapi.services.io.Limit;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.context.annotation.Bean;
  32. import org.springframework.stereotype.Service;

  33. import javax.servlet.http.HttpServletRequest;
  34. import javax.xml.soap.Text;
  35. import java.util.*;
  36. import java.util.function.Consumer;
  37. import java.util.stream.Collectors;

  38. @Slf4j
  39. @Service("loadTreeService")
  40. public class LoadTreeServiceImpl implements LoadTreeService {
  41.     private Logger logger = LoggerFactory.getLogger(this.getClass());
  42.     @Autowired
  43.     private PCDynaBeanTemplate dynaBeanTemplate;

  44.     @Autowired
  45.     private MetaService metaService;

  46.     @Autowired
  47.     private CommonService commonService;

  48.     @Autowired
  49.     private QueryBuilder queryBuilder;
  50.     @Autowired
  51.     private PlatformManager platformManager;
  52.     @Override

  53.     public JSONTreeNode getTree(MethodArgument param) {

  54.         String node = param.getNode();

  55.         if (StringUtils.isEmpty(node)) {
  56.             node = "ROOT";
  57.         }

  58.         if (StringUtils.isNotEmpty(param.getRootId())) {
  59.             node = param.getRootId();
  60.         }

  61.         //查询当前登录人拥有的数据
  62.         //构建查询条件
  63.         List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
  64.         Map<String, Object> map1 = new HashMap<String, Object>();
  65.         //获取当前登陆人
  66.         String userCode = SecurityUserHolder.getCurrentUser().getUserCode();
  67.         String userId = SecurityUserHolder.getCurrentUser().getUserId();
  68.         //--------------获取当前登录人为负责人和抄送人的数据-----------
  69.         List<DynaBean> lists = metaService.select("RWGL_RWXX", ConditionsWrapper.builder().eq("RWXX_RWSFGD_CODE", "1")
  70.                 .like("RWXX_FZRID", userId)
  71.                 .or().like("RWXX_FZRID", userCode)
  72.                 .or().like("RWXX_ZPRID", userId)
  73.                 .or().like("RWXX_ZPRID", userCode)
  74.                 .or().like("RWXX_CSRID", userId)
  75.                 .or().like("RWXX_CSRID", userCode)
  76.                 .or().like("SY_CREATEUSERID",userId));
  77.         String whereSqlbyUserId = "";
  78.         for (int i = 0; i < lists.size(); i++) {
  79.             String sy_path = lists.get(i).getStr("SY_PATH");
  80.             String sy_layer = lists.get(i).getStr("SY_LAYER");
  81.             if (i == 0) {
  82.                 if (sy_layer == "1") {
  83.                     whereSqlbyUserId += "  SY_PATH LIKE '%" + sy_path + "%'";
  84.                 } else {
  85.                     String[] a = sy_path.split("/");
  86.                     if(a.length>2){
  87.                         String new_path = "/" + a[1] + "/" + a[2];
  88.                         whereSqlbyUserId += "  SY_PATH LIKE '%" + new_path + "%'";
  89.                     }else{
  90.                         whereSqlbyUserId += " OR SY_PATH LIKE '%" + sy_path + "%'";
  91.                     }
  92.                 }
  93.             } else {
  94.                 if (sy_layer == "1") {
  95.                     whereSqlbyUserId += " OR SY_PATH LIKE '%" + sy_path + "%'";
  96.                 } else {
  97.                     String[] aa = sy_path.split("/");
  98.                     if(aa.length>2){
  99.                         String new_path = "/" + aa[1] + "/" + aa[2];
  100.                         whereSqlbyUserId += " OR SY_PATH LIKE '%" + new_path + "%'";
  101.                     }else {
  102.                         whereSqlbyUserId += " OR SY_PATH LIKE '%" + sy_path + "%'";
  103.                     }
  104.                 }
  105.             }

  106.         }
  107.         if("".equals(whereSqlbyUserId)){
  108.             return this.commonService.buildJSONNewTree(new ArrayList<>(), node);
  109.         }else {
  110.             whereSqlbyUserId = param.getWhereSql() + "and (" + whereSqlbyUserId + ")";

  111.         }
  112.         String querySql = "SELECT * FROM RWGL_RWXX WHERE 1=1 " + whereSqlbyUserId + " and RWXX_RWSFGD_CODE = '1' or SY_PATH = '/ROOT'";
  113.         querySql = querySql+" ORDER BY SY_TREEORDERINDEX,SY_CREATETIME DESC";
  114.         List<Map<String, Object>> maps = metaService.selectSql(querySql, param);
  115.         List<JSONTreeNode> jsonTreeNodes = new ArrayList<JSONTreeNode>();
  116.         if(maps.size()>0){
  117.             for (Map<String, Object> map  : maps){
  118.                 JSONTreeNode jsonTreeNode = new JSONTreeNode();
  119.                 jsonTreeNode.setId(map.get("RWGL_RWXX_ID").toString());
  120.                 jsonTreeNode.setText(map.get("RWXX_RWM").toString());
  121.                 jsonTreeNode.setEnField("");
  122.                 jsonTreeNode.setCode(map.get("RWXX_RWBM") == null ? "" : map.get("RWXX_RWBM").toString());
  123.                 //jsonTreeNode.setCode(map.get("RWXX_RWBM").toString());
  124.                 jsonTreeNode.setParent(map.get("SY_PARENT") == null ? "" :map.get("SY_PARENT").toString());
  125.                 jsonTreeNode.setIcon(null);
  126.                 jsonTreeNode.setIconCls(null);
  127.                 jsonTreeNode.setHref(null);
  128.                 jsonTreeNode.setHrefTarget("");
  129.                 jsonTreeNode.setNodeType(map.get("SY_NODETYPE").toString());
  130.                 jsonTreeNode.setLayer(map.get("SY_LAYER").toString());
  131.                 jsonTreeNode.setDescription("");
  132.                 jsonTreeNode.setExpandable(true);
  133.                 jsonTreeNode.setExpanded(false);
  134.                 jsonTreeNode.setChecked(false);
  135.                 jsonTreeNode.setAsync(false);
  136.                 jsonTreeNode.setNodeInfo(map.get("SY_NODETYPE").toString());
  137.                 jsonTreeNode.setNodeInfoType(map.get("SY_NODETYPE").toString());
  138.                 if ("LEAF".equalsIgnoreCase(map.get("SY_NODETYPE") + "")) {
  139.                     jsonTreeNode.setLeaf(true);
  140.                 } else {
  141.                     jsonTreeNode.setLeaf(false);
  142.                 }
  143.                 jsonTreeNode.setParentText(map.get("SY_PARENTPATH") == null ? "" :map.get("SY_PARENTPATH").toString());
  144.                 jsonTreeNode.setNodePath(map.get("SY_PATH").toString());
  145.                 jsonTreeNode.setParentPath(map.get("SY_PARENTPATH") == null ? "" :map.get("SY_PARENTPATH").toString());
  146.                 jsonTreeNode.setCode(map.get("SY_DISABLED") == null ? "" : map.get("SY_DISABLED").toString());
  147.                 //jsonTreeNode.setDisabled(map.get("SY_DISABLED").toString());
  148.                 //jsonTreeNode.setOrderIndex(map.get("SY_ORDERINDEX").toString());
  149.                 //jsonTreeNode.setParentPath(map.get("SY_ORDERINDEX") == null ? "" :map.get("SY_ORDERINDEX").toString());
  150.                 jsonTreeNode.setTreeOrderIndex(map.get("SY_TREEORDERINDEX").toString());
  151.                 jsonTreeNode.setFieldCodes(null);
  152.                 jsonTreeNode.setBean(map);
  153.                 jsonTreeNode.setChildren(new ArrayList<JSONTreeNode>());
  154.                 jsonTreeNodes.add(jsonTreeNode);
  155.             }
  156.         }
  157.         return this.commonService.buildJSONNewTree(jsonTreeNodes, node);
  158.     }
  159. }
复制代码

五、总结(可选)
当前代码只是根据自身特有的需求条件生成树形,可根据不同需求修改过滤条件
六、关键字

重写树形方法  getTree
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|JEPaaS 低代码平台社区 ( 京ICP备18023569号 )

GMT+8, 2024-5-1 22:10 , Processed in 0.057936 second(s), 20 queries .

Powered by 北京凯特伟业科技有限公司

Copyright © 2001-2021, JEPaaS.COM

快速回复 返回顶部 返回列表