package com.kingdee.eas.custom.perfweb.util; import com.kingdee.bos.BOSException; import com.kingdee.bos.Context; import com.kingdee.bos.ctrl.swing.StringUtils; import com.kingdee.jdbc.rowset.IRowSet; import com.kingdee.eas.util.app.DbUtil; import org.apache.log4j.Logger; import java.sql.SQLException; /** * 类名称: PersonnelStatUtil * 功能描述: 人员结构统计工具(在职人数/正式/试用/关键人才) * 创建日期: 2026-06-23 * 作 者: 青梧 * 版 本: 1.3 * * 统计口径: * - 员工类型 FIsOnTheStrength = 1 * - 组织范围由 StatScope 决定,委托 ReportOrgUtil 拼接 SQL * - 可选公司筛选 personPos.FCompanyID */ public final class PersonnelStatUtil { /** 正式员工类型编码 */ private static final String TYPE_FORMAL = "001"; /** 试用员工类型编码 */ private static final String TYPE_TRIAL = "002"; /** 关键人才标识 person.CFGjrc */ private static final String FLAG_KEY_TALENT = "1"; private PersonnelStatUtil() { } /** 人员结构统计结果 */ public static final class PersonnelStat { /** 在职总人数 */ public int totalCount; /** 正式员工人数 */ public int formalCount; /** 试用员工人数 */ public int trialCount; /** 关键人才人数 */ public int keyTalentCount; } /** * 统计范围。 *

* 工厂方法说明: * center - 按中心名称(跨公司) * centerByAdminOrg - 按行政组织FID(中心子树) * dept - 按部门名称(全集团) * deptUnderCenter - 中心下指定部门(名称) * deptUnderAdminOrg - 行政组织FID下指定部门(名称) * deptUnder*ByCode - 同上,优先按部门编码 FNUMBER 定位 */ public static final class StatScope { /** 中心名称 FNAME_L2(跨公司合并时使用) */ public final String centerName; /** 组织名称(中心名或部门名) */ public final String orgName; /** 组织层级: 中心 / 部门 */ public final String layerType; /** 公司FID,限定 personPos.FCompanyID */ public final String companyId; /** 行政组织FID(快筛 company/adminOrg 选中心时) */ public final String adminOrgId; /** 部门编码 FNUMBER(优先于 orgName 精确定位) */ public final String deptCode; private StatScope(String centerName, String orgName, String layerType, String companyId, String adminOrgId, String deptCode) { this.centerName = centerName; this.orgName = orgName; this.layerType = layerType; this.companyId = companyId; this.adminOrgId = adminOrgId; this.deptCode = deptCode; } public static StatScope center(String centerName, String companyId) { return new StatScope(null, centerName, ReportOrgUtil.LAYER_CENTER, companyId, null, null); } public static StatScope centerByAdminOrg(String adminOrgId, String companyId) { return new StatScope(null, null, ReportOrgUtil.LAYER_CENTER, companyId, adminOrgId, null); } public static StatScope dept(String deptName, String companyId) { return new StatScope(null, deptName, ReportOrgUtil.LAYER_DEPT, companyId, null, null); } public static StatScope deptUnderCenter(String centerName, String deptName, String companyId) { return new StatScope(centerName, deptName, ReportOrgUtil.LAYER_DEPT, companyId, null, null); } public static StatScope deptUnderAdminOrg(String adminOrgId, String deptName, String companyId) { return new StatScope(null, deptName, ReportOrgUtil.LAYER_DEPT, companyId, adminOrgId, null); } public static StatScope deptUnderAdminOrgByCode(String adminOrgId, String deptCode, String companyId) { return new StatScope(null, null, ReportOrgUtil.LAYER_DEPT, companyId, adminOrgId, deptCode); } public static StatScope deptUnderCenterByCode(String centerName, String deptCode, String companyId) { return new StatScope(centerName, null, ReportOrgUtil.LAYER_DEPT, companyId, null, deptCode); } } /** * 一次统计在职总人数、正式、试用、关键人才。 */ public static PersonnelStat countStructure(Context ctx, StatScope scope) throws BOSException, SQLException { return countStructure(ctx, scope, null); } public static PersonnelStat countStructure(Context ctx, StatScope scope, Logger logger) throws BOSException, SQLException { PersonnelStat stat = new PersonnelStat(); if (scope == null || !hasValidScope(scope)) { return stat; } stat.totalCount = countOnDuty(ctx, scope, null, null, logger); stat.formalCount = countOnDuty(ctx, scope, TYPE_FORMAL, null, logger); stat.trialCount = countOnDuty(ctx, scope, TYPE_TRIAL, null, logger); stat.keyTalentCount = countOnDuty(ctx, scope, null, FLAG_KEY_TALENT, logger); return stat; } /** * 从基础资料读取劳务工人数(中心报表合计行使用)。 */ public static int getLaborCount(Context ctx, Logger logger) throws BOSException, SQLException { StringBuilder sql = new StringBuilder(); sql.append("SELECT FSimpleName FROM T_HR_ZDY0HRBase WHERE fnumber = 'LWG'"); IRowSet rs = DbUtil.executeQuery(ctx, sql.toString()); if (rs.next()) { String simpleName = rs.getString("FSimpleName"); if (!StringUtils.isEmpty(simpleName)) { try { return Integer.parseInt(simpleName); } catch (NumberFormatException e) { if (logger != null) { logger.error("劳务工人数格式无效: " + simpleName); } return 0; } } } return 0; } /** 判断统计范围是否有效(避免空范围查全表) */ private static boolean hasValidScope(StatScope scope) { if (!StringUtils.isEmpty(scope.adminOrgId)) { return true; } if (!StringUtils.isEmpty(scope.centerName) && (!StringUtils.isEmpty(scope.orgName) || !StringUtils.isEmpty(scope.deptCode))) { return true; } return !StringUtils.isEmpty(scope.orgName); } /** * 统计在编人数。 * * @param employeeTypeNumber 员工类型编码(001正式/002试用),null 表示不限 * @param keyTalentFlag 关键人才标识(1),null 表示不限 */ private static int countOnDuty(Context ctx, StatScope scope, String employeeTypeNumber, String keyTalentFlag, Logger logger) throws BOSException, SQLException { StringBuilder sql = new StringBuilder(); sql.append("SELECT COUNT(*) as personCount "); sql.append("FROM T_BD_PERSON person "); sql.append("LEFT JOIN T_HR_PersonPosition personPos ON person.FID = personPos.FPERSONID "); sql.append("INNER JOIN T_HR_BDEmployeeType type ON type.FID = person.FEMPLOYEETYPEID "); sql.append("WHERE type.FIsOnTheStrength = 1 "); appendScopeFilter(sql, ctx, scope); if (!StringUtils.isEmpty(scope.companyId)) { sql.append("AND personPos.FCompanyID = '").append(escapeSql(scope.companyId)).append("' "); } if (!StringUtils.isEmpty(employeeTypeNumber)) { sql.append("AND type.FNumber = '").append(employeeTypeNumber).append("' "); } if (!StringUtils.isEmpty(keyTalentFlag)) { sql.append("AND person.CFGjrc = '").append(keyTalentFlag).append("' "); } if (logger != null) { logger.error("人员结构统计SQL: " + sql.toString()); } IRowSet rs = DbUtil.executeQuery(ctx, sql.toString()); if (rs.next()) { return rs.getInt("personCount"); } return 0; } /** * 按 StatScope 追加人员任职部门 personPos.FPersonDep 过滤条件。 * 优先级: 中心+部门编码 > 中心下部门 > 行政组织+部门编码 > 行政组织+部门名 > 行政组织子树 > 按名称 */ private static void appendScopeFilter(StringBuilder sql, Context ctx, StatScope scope) throws BOSException, SQLException { if (!StringUtils.isEmpty(scope.centerName) && !StringUtils.isEmpty(scope.deptCode)) { ReportOrgUtil.appendDeptUnderCenterFilterByCode(sql, ctx, scope.centerName, scope.deptCode, "personPos.FPersonDep"); return; } if (!StringUtils.isEmpty(scope.centerName)) { ReportOrgUtil.appendDeptUnderCenterFilter(sql, ctx, scope.centerName, scope.orgName, "personPos.FPersonDep"); return; } if (!StringUtils.isEmpty(scope.adminOrgId) && !StringUtils.isEmpty(scope.deptCode)) { ReportOrgUtil.appendDeptUnderAdminOrgFilterByCode(sql, ctx, scope.adminOrgId, scope.deptCode, "personPos.FPersonDep"); return; } if (!StringUtils.isEmpty(scope.adminOrgId) && !StringUtils.isEmpty(scope.orgName)) { ReportOrgUtil.appendDeptUnderAdminOrgFilter(sql, ctx, scope.adminOrgId, scope.orgName, "personPos.FPersonDep"); return; } if (!StringUtils.isEmpty(scope.adminOrgId) && StringUtils.isEmpty(scope.orgName)) { ReportOrgUtil.appendOrgFilter(sql, ctx, scope.adminOrgId, "personPos.FPersonDep"); return; } ReportOrgUtil.appendOrgFilterByName(sql, ctx, scope.orgName, scope.layerType, "personPos.FPersonDep"); } private static String escapeSql(String value) { if (value == null) { return ""; } return value.replace("'", "''"); } }