From fab01583ad4cef0df4fdd6babc3f4598e7a0f080 Mon Sep 17 00:00:00 2001 From: dxfeng Date: Mon, 24 Jul 2023 16:38:17 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=84=E7=BB=87=E6=9E=B6=E6=9E=84=E5=9B=BE?= =?UTF-8?q?=EF=BC=8C=E9=83=A8=E9=97=A8=E9=80=89=E6=8B=A9=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../organization/service/ChartService.java | 8 ++ .../service/impl/ChartServiceImpl.java | 100 ++++++++++++++++++ .../util/db/DBOperateAdapter.java | 2 + .../engine/organization/util/db/DBType.java | 23 ++++ .../organization/web/OrgChartController.java | 20 ++++ .../organization/wrapper/OrgChartWrapper.java | 4 + 6 files changed, 157 insertions(+) diff --git a/src/com/engine/organization/service/ChartService.java b/src/com/engine/organization/service/ChartService.java index 783040d6..bbb05649 100644 --- a/src/com/engine/organization/service/ChartService.java +++ b/src/com/engine/organization/service/ChartService.java @@ -47,4 +47,12 @@ public interface ChartService { * @return 数据集合 */ Map versionRecord(Map params, User user); + + /** + * 获取部门下拉框树结构 + * + * @param params 前端入参 + * @return 树结构 + */ + Map getDepartmentTree(Map params); } diff --git a/src/com/engine/organization/service/impl/ChartServiceImpl.java b/src/com/engine/organization/service/impl/ChartServiceImpl.java index becccc3e..e5d8a7b7 100644 --- a/src/com/engine/organization/service/impl/ChartServiceImpl.java +++ b/src/com/engine/organization/service/impl/ChartServiceImpl.java @@ -3,6 +3,7 @@ package com.engine.organization.service.impl; import com.engine.common.util.ServiceUtil; import com.engine.core.impl.Service; import com.engine.organization.entity.chart.ChartPO; +import com.engine.organization.entity.chart.CompanyTreePO; import com.engine.organization.mapper.hrmresource.SystemDataMapper; import com.engine.organization.mapper.jclorgmap.JclOrgMapper; import com.engine.organization.service.ChartService; @@ -10,6 +11,7 @@ import com.engine.organization.service.OrgChartService; import com.engine.organization.util.HasRightUtil; import com.engine.organization.util.OrganizationAssert; import com.engine.organization.util.OrganizationDateUtil; +import com.engine.organization.util.db.DBType; import com.engine.organization.util.db.MapperProxyFactory; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -446,6 +448,29 @@ public class ChartServiceImpl extends Service implements ChartService { result.put("api_status", true); return result; } + + @Override + public Map getDepartmentTree(Map params) { + Map result = new HashMap<>(2); + + RecordSet rs = new RecordSet(); + List departmentTree = new ArrayList<>(); + String subCompany = Util.null2String(params.get("subcompany")); + String dimension = Util.null2String(params.get("fclass")); + String versionId = Util.null2String(params.get("id")); + if (StringUtils.isBlank(subCompany)) { + subCompany = "0"; + } + String sql = getDepartmentTreeSql(versionId, dimension, subCompany); + rs.executeQuery(sql); + while (rs.next()) { + departmentTree.add(CompanyTreePO.builder().id(rs.getString("id")).pId(rs.getString("pId")).value(rs.getString("value")).title(rs.getString("title")).isLeaf(judgeTreeLeaf(versionId, dimension, rs.getString("id"))).build()); + } + result.put("companyTree", departmentTree); + result.put("api_status", true); + return result; + } + private void trueDimension(RecordSetTrans recordSetTrans,String versionId,String currentUser,String currentDate){ RecordSet rs = new RecordSet(); rs.execute("delete from jcl_chart_subcompany where versionid = "+versionId); @@ -1184,4 +1209,79 @@ public class ChartServiceImpl extends Service implements ChartService { } return 0; } + + + /** + * 获取部门树下拉框SQL + * + * @param versionId 版本ID + * @param dimension 维度 + * @param subCompany 上级Id + * @return SQL + */ + private String getDepartmentTreeSql(String versionId, String dimension, String subCompany) { + // 是否展示当前数据 + boolean isSearchCurrent = StringUtils.isBlank(versionId) || "0".equals(versionId); + boolean isCurrentDimension = StringUtils.isNotBlank(dimension) && !"0".equals(dimension); + boolean isDepartment = subCompany.startsWith("d"); + String sql; + DBType dbType = DBType.get(new RecordSet().getDBType()); + if (isDepartment) { + subCompany = subCompany.replace("d", ""); + if (isSearchCurrent) { + sql = "select " + dbType.concat("d", "id") + "as id, id as value, departmentname as title, " + dbType.concat("d", "supdepid") + " as pId from hrmdepartment where (canceled is null or canceled != '1') and " + dbType.ifNull("supdepid", "0") + " = " + subCompany; + if (isCurrentDimension) { + sql = "select " + dbType.concat("d", "id") + "as id, id as value, departmentname as title, " + dbType.concat("d", "supdepid") + " as pId from hrmdepartmentvirtual where (canceled is null or canceled != '1') and " + dbType.ifNull("supdepid", "0") + " = " + subCompany; + } + } else { + sql = "select " + dbType.concat("d", "departmentid") + "as id, departmentid as value, departmentname as title, " + dbType.concat("d", "supdepartmentid") + " as pId from jcl_chart_department where (canceled is null or canceled != '1') and " + dbType.ifNull("supdepartmentid", "0") + " = " + subCompany; + if (isCurrentDimension) { + sql = "select " + dbType.concat("d", "departmentvirtualid") + "as id, departmentvirtualid as value, departmentname as title, " + dbType.concat("d", "supdepid") + " as pId from jcl_chart_departmentvirtual where (canceled is null or canceled != '1') and " + dbType.ifNull("supdepid", "0") + " = " + subCompany; + } + // 添加时间轴条件 + sql += " and versionid = " + versionId; + } + } else { + if (isSearchCurrent) { + sql = "select id as id, id as value, subcompanyname as title, supsubcomid as pId from hrmsubcompany where (canceled is null or canceled != '1') and " + dbType.ifNull("supsubcomid", "0") + " = " + subCompany + + " union select " + dbType.concat("d", "id") + "as id, id as value, departmentname as title, subcompanyid1 as pId from hrmdepartment where (canceled is null or canceled != '1') and " + dbType.ifNull("subcompanyid1", "0") + " = " + subCompany; + if (isCurrentDimension) { + sql = "select id as id, id as value, subcompanyname as title, supsubcomid as pId from hrmsubcompanyvirtual where (canceled is null or canceled != '1') and " + dbType.ifNull("supsubcomid", "0") + " = " + subCompany + " and companyid = '" + dimension + "' " + + " union select " + dbType.concat("d", "id") + "as id, id as value, departmentname as title, subcompanyid1 as pId from hrmdepartmentvirtual where (canceled is null or canceled != '1') and " + dbType.ifNull("subcompanyid1", "0") + " = " + subCompany; + } + } else { + sql = "select subcompanyid as id, subcompanyid as value, subcompanyname as title, supsubcompanyid as pId from jcl_chart_subcompany where (canceled is null or canceled != '1') and " + dbType.ifNull("supsubcompanyid", "0") + " = " + subCompany + + " union select " + dbType.concat("d", "subcompanyid") + "as id, subcompanyid as value, departmentname as title, subcompanyid as pId from jcl_chart_department where (canceled is null or canceled != '1') and " + dbType.ifNull("subcompanyid", "0") + " = " + subCompany; + if (isCurrentDimension) { + sql = "select subcompanyvirtualid as id, subcompanyvirtualid as value, subcompanyname as title, supsubcompanyid as pId from jcl_chart_subcompanyvirtual where (canceled is null or canceled != '1') and " + dbType.ifNull("supsubcompanyid", "0") + " = " + subCompany + " and companyid = '" + dimension + "' " + + " union select " + dbType.concat("d", "departmentvirtualid") + "as id, departmentvirtualid as value, departmentname as title, subcompanyid as pId from jcl_chart_departmentvirtual where (canceled is null or canceled != '1') and " + dbType.ifNull("subcompanyid", "0") + " = " + subCompany; + } + // 添加时间轴条件 + sql += " and versionid = " + versionId; + } + } + return sql; + } + + /** + * 判断树是否为叶子节点 + * + * @param sql 查询下级元素SQL + * @param treeId 当前元素ID + * @return + */ + + /** + * 判断树是否为叶子节点 + * @param versionId 版本ID + * @param dimension 维度 + * @param subCompany 上级元素ID + * @return + */ + private boolean judgeTreeLeaf(String versionId, String dimension, String subCompany) { + RecordSet recordSet = new RecordSet(); + String sql = getDepartmentTreeSql(versionId, dimension, subCompany); + recordSet.executeQuery(sql); + return !recordSet.next(); + } } diff --git a/src/com/engine/organization/util/db/DBOperateAdapter.java b/src/com/engine/organization/util/db/DBOperateAdapter.java index e9caa997..97fd62a9 100644 --- a/src/com/engine/organization/util/db/DBOperateAdapter.java +++ b/src/com/engine/organization/util/db/DBOperateAdapter.java @@ -5,6 +5,8 @@ public interface DBOperateAdapter { String concat(String some); + String concat(String concatStr, String fieldName); + String currentDate(); String ifNull(String some, String defaultValue); diff --git a/src/com/engine/organization/util/db/DBType.java b/src/com/engine/organization/util/db/DBType.java index 35fe3346..fa12c713 100644 --- a/src/com/engine/organization/util/db/DBType.java +++ b/src/com/engine/organization/util/db/DBType.java @@ -5,6 +5,9 @@ import com.engine.organization.exception.OrganizationRunTimeException; import weaver.conn.RecordSet; public enum DBType implements DBOperateAdapter { + /** + * 根据数据库类型,匹配不通的函数 + */ MYSQL("mysql") { @Override public String like(String some) { @@ -16,6 +19,11 @@ public enum DBType implements DBOperateAdapter { return " concat(','," + some + ",',') "; } + @Override + public String concat(String concatStr, String fieldName) { + return " concat('" + concatStr + "'," + fieldName + ") "; + } + @Override public String currentDate() { return "now()"; @@ -37,6 +45,11 @@ public enum DBType implements DBOperateAdapter { return " ','+" + some + "+',' "; } + @Override + public String concat(String concatStr, String fieldName) { + return " '" + concatStr + "' + " + fieldName; + } + @Override public String currentDate() { return "GETDATE()"; @@ -58,6 +71,11 @@ public enum DBType implements DBOperateAdapter { return " ',' ||" + some + "|| ',' "; } + @Override + public String concat(String concatStr, String fieldName) { + return " '" + concatStr + "' ||" + fieldName; + } + @Override public String currentDate() { return "SYSDATE"; @@ -79,6 +97,11 @@ public enum DBType implements DBOperateAdapter { return " ',' ||" + some + "|| ',' "; } + @Override + public String concat(String concatStr, String fieldName) { + return " '" + concatStr + "' ||" + fieldName; + } + @Override public String currentDate() { return "now()"; diff --git a/src/com/engine/organization/web/OrgChartController.java b/src/com/engine/organization/web/OrgChartController.java index a8c2015c..514fffdf 100644 --- a/src/com/engine/organization/web/OrgChartController.java +++ b/src/com/engine/organization/web/OrgChartController.java @@ -118,6 +118,26 @@ public class OrgChartController { return JSONObject.toJSONString(apidatas); } + @GET + @Path("/getDepartmentTree") + @Produces(MediaType.APPLICATION_JSON) + public String getDepartmentTree(@Context HttpServletRequest request, @Context HttpServletResponse response) { + Map apidatas = new HashMap<>(); + try { + User user = HrmUserVarify.getUser(request, response); + //实例化Service 并调用业务类处理 + apidatas = getOrgChartWrapper(user).getDepartmentTree(ParamUtil.request2Map(request), user); + apidatas.put("api_status", true); + } catch (Exception e) { + //异常处理 + e.printStackTrace(); + apidatas.put("api_status", false); + apidatas.put("api_errormsg", "catch exception : " + e.getMessage()); + } + //数据转换 + return JSONObject.toJSONString(apidatas); + } + /** * 组织架构数据 */ diff --git a/src/com/engine/organization/wrapper/OrgChartWrapper.java b/src/com/engine/organization/wrapper/OrgChartWrapper.java index 8d6c59ea..2bbf5a67 100644 --- a/src/com/engine/organization/wrapper/OrgChartWrapper.java +++ b/src/com/engine/organization/wrapper/OrgChartWrapper.java @@ -68,4 +68,8 @@ public class OrgChartWrapper extends Service { public Map versionRecord(Map request2Map, User user) { return getChartService(user).versionRecord(request2Map,user); } + + public Map getDepartmentTree(Map request2Map, User user) { + return getChartService(user).getDepartmentTree(request2Map); + } }