diff --git a/src/com/engine/organization/entity/chart/ChartPO.java b/src/com/engine/organization/entity/chart/ChartPO.java new file mode 100644 index 00000000..5495144b --- /dev/null +++ b/src/com/engine/organization/entity/chart/ChartPO.java @@ -0,0 +1,55 @@ +package com.engine.organization.entity.chart; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.apache.commons.lang3.StringUtils; + +/** + * @author:dxfeng + * @createTime: 2023/06/30 + * @version: 1.0 + */ +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class ChartPO { + private String fname; + /** + * 0:集团,1:分部,2:部门 + */ + private String ftype; + private String fnumber; + private String fobjid; + private String parentId; + private String expand; + private String fisvitual; + private String hasChildren; + + private String id; + + public String getId() { + if (StringUtils.isNotBlank(ftype)) { + switch (ftype) { + case "0": + id = "c_" + fobjid; + break; + case "1": + id = "s_" + fobjid; + break; + case "2": + id = "d_" + fobjid; + break; + default: + break; + } + } + return id; + } + + public String getFisvitual() { + return StringUtils.isBlank(fisvitual) ? "0" : fisvitual; + } +} diff --git a/src/com/engine/organization/service/ChartService.java b/src/com/engine/organization/service/ChartService.java new file mode 100644 index 00000000..15f8e4aa --- /dev/null +++ b/src/com/engine/organization/service/ChartService.java @@ -0,0 +1,14 @@ +package com.engine.organization.service; + +import java.util.Map; + +/** + * @author:dxfeng + * @createTime: 2023/06/29 + * @version: 1.0 + */ +public interface ChartService { + + Map getCompanyData(Map params); + +} diff --git a/src/com/engine/organization/service/impl/ChartServiceImpl.java b/src/com/engine/organization/service/impl/ChartServiceImpl.java new file mode 100644 index 00000000..6da60222 --- /dev/null +++ b/src/com/engine/organization/service/impl/ChartServiceImpl.java @@ -0,0 +1,286 @@ +package com.engine.organization.service.impl; + +import com.engine.core.impl.Service; +import com.engine.organization.entity.chart.ChartPO; +import com.engine.organization.service.ChartService; +import com.engine.organization.util.HasRightUtil; +import org.apache.commons.lang3.StringUtils; +import weaver.conn.RecordSet; +import weaver.general.Util; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author:dxfeng + * @createTime: 2023/06/29 + * @version: 1.0 + */ +public class ChartServiceImpl extends Service implements ChartService { + private static final String COMPANY_RIGHT = "OrgChart:All"; + private static final String USER_RIGHT = "OrgPerspective:All"; + + public String COMPANY_TABLE = ""; + public String SUB_COMPANY_TABLE = ""; + public String SUB_COMPANY_DEFINED_TABLE = ""; + public String DEPARTMENT_TABLE = ""; + public String DEPARTMENT_DEFINED_TABLE = ""; + + /** + * 显示虚拟组织 + */ + boolean showVirtual = false; + + /** + * 是否已创建虚拟组织字段 + */ + boolean hasVirtualFields; + + public ChartServiceImpl() { + super(); + RecordSet rs = new RecordSet(); + rs.executeQuery("select count(1) as num from hrm_formfield where (GROUPID =6 and FIELDNAME = 'fblx') or (GROUPID =7 and FIELDNAME = 'bmlx')"); + if (rs.next()) { + String num = rs.getString("num"); + hasVirtualFields = "2".equals(num); + + } else { + hasVirtualFields = false; + } + } + + @Override + public Map getCompanyData(Map params) { + Map result = new HashMap<>(); + boolean hasRight = HasRightUtil.hasRight(user, COMPANY_RIGHT, true); + result.put("hasRight", hasRight); + if (!hasRight) { + return result; + } + + // 根结点 + String root = Util.null2String(params.get("root")); + root = StringUtils.isBlank(root) ? "0" : root; + + // 维度 + String dimension = Util.null2String(params.get("fclass")); + dimension = StringUtils.isBlank(dimension) ? "0" : dimension; + + // 是否展示虚拟组织 + String isVirtual = Util.null2String(params.get("fisvitual")); + showVirtual = "1".equals(isVirtual); + + // 初始化表名 + initTableNameByClass(dimension); + + RecordSet rs = new RecordSet(); + List dataList = new ArrayList<>(); + String sql = ""; + + ChartPO topChartPO = null; + //TODO 查询当前实际数据 + sql = getRealTimeTopSql(root, dimension); + rs.executeQuery(sql); + + // 封装顶部节点 + if (rs.next()) { + topChartPO = new ChartPO(); + topChartPO.setFtype(rs.getString("type")); + topChartPO.setFobjid(rs.getString("id")); + topChartPO.setFname(rs.getString("name")); + topChartPO.setParentId(null); + topChartPO.setExpand("1"); + topChartPO.setFisvitual(rs.getString("isvitual")); + topChartPO.setHasChildren(getHasChildren(topChartPO.getFtype(), topChartPO.getFobjid()).toString()); + dataList.add(topChartPO); + } + + // 向下查询数据 + if (null != topChartPO) { + findChildData(topChartPO, dataList); + } + + + result.put("api_status", true); + result.put("data", dataList); + return result; + } + + /** + * 查询实时数据,顶级元素SQL + * + * @param root 顶级元素ID + * @param dimension 维度ID + * @return 查询SQL + */ + private String getRealTimeTopSql(String root, String dimension) { + if ("0".equals(root)) { + // 查询集团数据 + if ("0".equals(dimension)) { + // 查询实际集团表 + return "select id,companyname as 'name','0' as 'type' from " + COMPANY_TABLE; + } else { + // 查询其他维度集团信息 + return "select id,companyname as 'name','0' as 'type' from " + COMPANY_TABLE + "where id = '" + dimension + "'"; + } + } else { + if (hasVirtualFields) { + if (showVirtual) { + return "select a.id,a.subcompanyname as 'name','1' as 'type' ,b.fblx as 'isvitual' from " + SUB_COMPANY_TABLE + " a left join " + SUB_COMPANY_DEFINED_TABLE + " b on a.id = b.subcomid where a.id = '" + root + "'"; + } else { + return "select a.id,a.subcompanyname as 'name','1' as 'type' ,b.fblx as 'isvitual' from " + SUB_COMPANY_TABLE + " a left join " + SUB_COMPANY_DEFINED_TABLE + " b on a.id = b.subcomid where a.id = '" + root + "' and (b.fblx is null or b.fblx!='1')"; + + } + } else { + return "select id,subcompanyname as 'name','1' as 'type' from " + SUB_COMPANY_TABLE + "where id = '" + root + "'"; + } + } + } + + /** + * 查询实时数据,子节点元素 + * + * @param topChartPO 父级元素 + * @param dataList 所有元素集合 + */ + private void findChildData(ChartPO topChartPO, List dataList) { + String fType = topChartPO.getFtype(); + String fObjId = topChartPO.getFobjid(); + String sql = ""; + if (StringUtils.isNotBlank(fType)) { + switch (fType) { + case "0": + if (hasVirtualFields) { + if (showVirtual) { + sql = "select a.id,a.subcompanyname as 'name','1' as 'type' ,b.fblx as 'isvitual' from " + SUB_COMPANY_TABLE + " a left join " + SUB_COMPANY_DEFINED_TABLE + " b on a.id = b.subcomid where (a.canceled is null or a.canceled != '1') and (a.supsubcomid is null or a.supsubcomid = '0') and a.companyid = '" + fObjId + "'"; + } else { + sql = "select a.id,a.subcompanyname as 'name','1' as 'type' ,b.fblx as 'isvitual' from " + SUB_COMPANY_TABLE + " a left join " + SUB_COMPANY_DEFINED_TABLE + " b on a.id = b.subcomid where (a.canceled is null or a.canceled != '1') and (a.supsubcomid is null or a.supsubcomid = '0') and (b.fblx is null or b.fblx != '1') and a.companyid = '" + fObjId + "'"; + } + } else { + sql = "select a.id,a.subcompanyname as 'name','1' as 'type' from " + SUB_COMPANY_TABLE + " a where (a.canceled is null or a.canceled != '1') and (a.supsubcomid is null or a.supsubcomid = '0') and a.companyid = '" + fObjId + "'"; + } + break; + case "1": + if (hasVirtualFields) { + if (showVirtual) { + sql = "select a.id,a.subcompanyname as 'name','1' as 'type' ,b.fblx as 'isvitual' from " + SUB_COMPANY_TABLE + " a left join " + SUB_COMPANY_DEFINED_TABLE + " b on a.id = b.subcomid where (a.canceled is null or a.canceled != '1') and a.supsubcomid = '" + fObjId + "'" + + " union select a.id,a.departmentname as 'name','2' as 'type' ,b.bmlx as 'isvitual' from " + DEPARTMENT_TABLE + " a left join " + DEPARTMENT_DEFINED_TABLE + " b on a.id = b.deptid where (a.canceled is null or a.canceled != '1') and (a.supdepid is null or a.supdepid = '0') and subcompanyid1 = '" + fObjId + "'"; + } else { + sql = "select a.id,a.subcompanyname as 'name','1' as 'type' ,b.fblx as 'isvitual' from " + SUB_COMPANY_TABLE + " a left join " + SUB_COMPANY_DEFINED_TABLE + " b on a.id = b.subcomid where (a.canceled is null or a.canceled != '1') and (b.fblx is null or b.fblx != '1') and a.supsubcomid = '" + fObjId + "'" + + " union select a.id,a.departmentname as 'name','2' as 'type' ,b.bmlx as 'isvitual' from " + DEPARTMENT_TABLE + " a left join " + DEPARTMENT_DEFINED_TABLE + " b on a.id = b.deptid where (a.canceled is null or a.canceled != '1') and (a.supdepid is null or a.supdepid = '0') and (b.bmlx is null or b.bmlx != '1') and subcompanyid1 = '" + fObjId + "'"; + } + } else { + sql = "select a.id,a.subcompanyname as 'name','1' as 'type' from " + SUB_COMPANY_TABLE + " a where (canceled is null or canceled != '1') and supsubcomid = '" + fObjId + "'" + + " union select a.id,a.departmentname as 'name','2' as 'type' from " + DEPARTMENT_TABLE + " a where (canceled is null or canceled != '1') and (supdepid is null or supdepid = '0') and subcompanyid1 = '" + fObjId + "'"; + } + break; + case "2": + if (hasVirtualFields) { + if (showVirtual) { + sql = "select a.id,a.departmentname as 'name','2' as 'type' ,b.bmlx as 'isvitual' from " + DEPARTMENT_TABLE + " a left join " + DEPARTMENT_DEFINED_TABLE + " b on a.id = b.deptid where (a.canceled is null or a.canceled != '1') and supdepid = '" + fObjId + "'"; + } else { + sql = "select a.id,a.departmentname as 'name','2' as 'type' ,b.bmlx as 'isvitual' from " + DEPARTMENT_TABLE + " a left join hrmdepartmentdefined b on a.id = b.deptid where (canceled is null or canceled != '1') and (b.bmlx is null or b.bmlx != '1') and a.supdepid = '" + fObjId + "'"; + } + } else { + sql = "select a.id,a.departmentname as 'name','2' as 'type' from " + DEPARTMENT_TABLE + " a where (canceled is null or canceled != '1') and supdepid = '" + fObjId + "'"; + } + break; + default: + break; + } + } + + if (StringUtils.isNotBlank(sql)) { + List currentList = new ArrayList<>(); + RecordSet recordSet = new RecordSet(); + recordSet.executeQuery(sql); + while (recordSet.next()) { + ChartPO chartPO = new ChartPO(); + chartPO.setFtype(recordSet.getString("type")); + chartPO.setFobjid(recordSet.getString("id")); + chartPO.setFname(recordSet.getString("name")); + chartPO.setParentId(topChartPO.getId()); + // 非部门元素展开 + chartPO.setExpand(topChartPO.getId().startsWith("d") ? "0" : "1"); + chartPO.setFisvitual(recordSet.getString("isvitual")); + chartPO.setHasChildren(getHasChildren(chartPO.getFtype(), chartPO.getFobjid()).toString()); + currentList.add(chartPO); + } + for (ChartPO chartPO : currentList) { + if (chartPO.getParentId().startsWith("d")) { + // 部门只展示一级 + continue; + } + findChildData(chartPO, dataList); + } + dataList.addAll(currentList); + } + + + } + + + /** + * 根据维度初始化表名 + * + * @param fClass 所选维度 + */ + public void initTableNameByClass(String fClass) { + if (StringUtils.isBlank(fClass) || "0".equals(fClass)) { + COMPANY_TABLE = "hrmcompany"; + SUB_COMPANY_TABLE = "hrmsubcompany"; + SUB_COMPANY_DEFINED_TABLE = "hrmsubcompanydefined"; + DEPARTMENT_TABLE = "hrmdepartment"; + DEPARTMENT_DEFINED_TABLE = "hrmdepartmentdefined"; + } else { + COMPANY_TABLE = "hrmcompanyvirtual"; + SUB_COMPANY_TABLE = "hrmsubcompanyvirtual"; + DEPARTMENT_TABLE = "hrmdepartmentvirtual"; + // 其他维度,无虚拟组织 + hasVirtualFields = false; + } + } + + + /** + * 判断是否有子元素 + * + * @param fType 元素类型 + * @param fObjId 元素ID + * @return boolean 包含子元素:true,不包含子元素:false + */ + private Boolean getHasChildren(String fType, String fObjId) { + String sql = ""; + if (StringUtils.isNotBlank(fType)) { + switch (fType) { + case "0": + sql = "select id from " + SUB_COMPANY_TABLE + " where (supsubcomid is null or supsubcomid = '0') and companyid = '" + fObjId + "'"; + break; + case "1": + if (hasVirtualFields && !showVirtual) { + sql = "select a.id from " + SUB_COMPANY_TABLE + " a left join " + SUB_COMPANY_DEFINED_TABLE + " b on a.id = b.subcomid where (a.canceled is null or a.canceled != '1') and (b.fblx is null or b.fblx != '1') and a.supsubcomid = '" + fObjId + "' union select a.id from " + DEPARTMENT_TABLE + " a left join " + DEPARTMENT_DEFINED_TABLE + " b on a.id = b.deptid where (a.canceled is null or a.canceled != '1') and (a.supdepid is null or a.supdepid = '0') and (b.bmlx is null or b.bmlx != '1') and subcompanyid1 = '" + fObjId + "'"; + } else { + sql = "select id from " + SUB_COMPANY_TABLE + " where (canceled is null or canceled != '1') and supsubcomid = '" + fObjId + "' union select id from " + DEPARTMENT_TABLE + " where (canceled is null or canceled != '1') and (supdepid is null or supdepid = '0') and subcompanyid1 = '" + fObjId + "'"; + } + break; + case "2": + if (hasVirtualFields && !showVirtual) { + sql = "select a.id from " + DEPARTMENT_TABLE + " a left join hrmdepartmentdefined b on a.id = b.deptid where (canceled is null or canceled != '1') and (b.bmlx is null or b.bmlx != '1') and a.supdepid = '" + fObjId + "'"; + } else { + sql = "select id from " + DEPARTMENT_TABLE + " where (canceled is null or canceled != '1') and supdepid = '" + fObjId + "'"; + } + break; + default: + break; + } + } + if (StringUtils.isNotBlank(sql)) { + RecordSet rs = new RecordSet(); + rs.executeQuery(sql); + return rs.next(); + } + return false; + } +} diff --git a/src/com/engine/organization/wrapper/OrgChartWrapper.java b/src/com/engine/organization/wrapper/OrgChartWrapper.java index 78502c40..47f35bc7 100644 --- a/src/com/engine/organization/wrapper/OrgChartWrapper.java +++ b/src/com/engine/organization/wrapper/OrgChartWrapper.java @@ -2,7 +2,9 @@ package com.engine.organization.wrapper; import com.engine.common.util.ServiceUtil; import com.engine.core.impl.Service; +import com.engine.organization.service.ChartService; import com.engine.organization.service.OrgChartService; +import com.engine.organization.service.impl.ChartServiceImpl; import com.engine.organization.service.impl.OrgChartServiceImpl; import weaver.hrm.User; @@ -17,6 +19,11 @@ import java.util.Map; public class OrgChartWrapper extends Service { private OrgChartService getOrgChartService(User user) { return ServiceUtil.getService(OrgChartServiceImpl.class, user); + + } + + private ChartService getChartService(User user) { + return ServiceUtil.getService(ChartServiceImpl.class, user); } @@ -29,7 +36,8 @@ public class OrgChartWrapper extends Service { } public Map getCompanyData(Map request2Map, User user) { - return getOrgChartService(user).getCompanyData(request2Map, user); + // return getOrgChartService(user).getCompanyData(request2Map, user); + return getChartService(user).getCompanyData(request2Map); } public Map getUserData(Map request2Map, User user) {