PersonnelStatUtil.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package com.kingdee.eas.custom.perfweb.util;
  2. import com.kingdee.bos.BOSException;
  3. import com.kingdee.bos.Context;
  4. import com.kingdee.bos.ctrl.swing.StringUtils;
  5. import com.kingdee.jdbc.rowset.IRowSet;
  6. import com.kingdee.eas.util.app.DbUtil;
  7. import org.apache.log4j.Logger;
  8. import java.sql.SQLException;
  9. /**
  10. * 类名称: PersonnelStatUtil
  11. * 功能描述: 人员结构统计工具(在职人数/正式/试用/关键人才)
  12. * 创建日期: 2026-06-23
  13. * 作 者: 青梧
  14. * 版 本: 1.3
  15. *
  16. * 统计口径:
  17. * - 员工类型 FIsOnTheStrength = 1
  18. * - 组织范围由 StatScope 决定,委托 ReportOrgUtil 拼接 SQL
  19. * - 可选公司筛选 personPos.FCompanyID
  20. */
  21. public final class PersonnelStatUtil {
  22. /** 正式员工类型编码 */
  23. private static final String TYPE_FORMAL = "001";
  24. /** 试用员工类型编码 */
  25. private static final String TYPE_TRIAL = "002";
  26. /** 关键人才标识 person.CFGjrc */
  27. private static final String FLAG_KEY_TALENT = "1";
  28. private PersonnelStatUtil() {
  29. }
  30. /** 人员结构统计结果 */
  31. public static final class PersonnelStat {
  32. /** 在职总人数 */
  33. public int totalCount;
  34. /** 正式员工人数 */
  35. public int formalCount;
  36. /** 试用员工人数 */
  37. public int trialCount;
  38. /** 关键人才人数 */
  39. public int keyTalentCount;
  40. }
  41. /**
  42. * 统计范围。
  43. * <p>
  44. * 工厂方法说明:
  45. * center - 按中心名称(跨公司)
  46. * centerByAdminOrg - 按行政组织FID(中心子树)
  47. * dept - 按部门名称(全集团)
  48. * deptUnderCenter - 中心下指定部门(名称)
  49. * deptUnderAdminOrg - 行政组织FID下指定部门(名称)
  50. * deptUnder*ByCode - 同上,优先按部门编码 FNUMBER 定位
  51. */
  52. public static final class StatScope {
  53. /** 中心名称 FNAME_L2(跨公司合并时使用) */
  54. public final String centerName;
  55. /** 组织名称(中心名或部门名) */
  56. public final String orgName;
  57. /** 组织层级: 中心 / 部门 */
  58. public final String layerType;
  59. /** 公司FID,限定 personPos.FCompanyID */
  60. public final String companyId;
  61. /** 行政组织FID(快筛 company/adminOrg 选中心时) */
  62. public final String adminOrgId;
  63. /** 部门编码 FNUMBER(优先于 orgName 精确定位) */
  64. public final String deptCode;
  65. private StatScope(String centerName, String orgName, String layerType, String companyId,
  66. String adminOrgId, String deptCode) {
  67. this.centerName = centerName;
  68. this.orgName = orgName;
  69. this.layerType = layerType;
  70. this.companyId = companyId;
  71. this.adminOrgId = adminOrgId;
  72. this.deptCode = deptCode;
  73. }
  74. public static StatScope center(String centerName, String companyId) {
  75. return new StatScope(null, centerName, ReportOrgUtil.LAYER_CENTER, companyId, null, null);
  76. }
  77. public static StatScope centerByAdminOrg(String adminOrgId, String companyId) {
  78. return new StatScope(null, null, ReportOrgUtil.LAYER_CENTER, companyId, adminOrgId, null);
  79. }
  80. public static StatScope dept(String deptName, String companyId) {
  81. return new StatScope(null, deptName, ReportOrgUtil.LAYER_DEPT, companyId, null, null);
  82. }
  83. public static StatScope deptUnderCenter(String centerName, String deptName, String companyId) {
  84. return new StatScope(centerName, deptName, ReportOrgUtil.LAYER_DEPT, companyId, null, null);
  85. }
  86. public static StatScope deptUnderAdminOrg(String adminOrgId, String deptName, String companyId) {
  87. return new StatScope(null, deptName, ReportOrgUtil.LAYER_DEPT, companyId, adminOrgId, null);
  88. }
  89. public static StatScope deptUnderAdminOrgByCode(String adminOrgId, String deptCode, String companyId) {
  90. return new StatScope(null, null, ReportOrgUtil.LAYER_DEPT, companyId, adminOrgId, deptCode);
  91. }
  92. public static StatScope deptUnderCenterByCode(String centerName, String deptCode, String companyId) {
  93. return new StatScope(centerName, null, ReportOrgUtil.LAYER_DEPT, companyId, null, deptCode);
  94. }
  95. }
  96. /**
  97. * 一次统计在职总人数、正式、试用、关键人才。
  98. */
  99. public static PersonnelStat countStructure(Context ctx, StatScope scope) throws BOSException, SQLException {
  100. return countStructure(ctx, scope, null);
  101. }
  102. public static PersonnelStat countStructure(Context ctx, StatScope scope, Logger logger)
  103. throws BOSException, SQLException {
  104. PersonnelStat stat = new PersonnelStat();
  105. if (scope == null || !hasValidScope(scope)) {
  106. return stat;
  107. }
  108. stat.totalCount = countOnDuty(ctx, scope, null, null, logger);
  109. stat.formalCount = countOnDuty(ctx, scope, TYPE_FORMAL, null, logger);
  110. stat.trialCount = countOnDuty(ctx, scope, TYPE_TRIAL, null, logger);
  111. stat.keyTalentCount = countOnDuty(ctx, scope, null, FLAG_KEY_TALENT, logger);
  112. return stat;
  113. }
  114. /**
  115. * 从基础资料读取劳务工人数(中心报表合计行使用)。
  116. */
  117. public static int getLaborCount(Context ctx, Logger logger) throws BOSException, SQLException {
  118. StringBuilder sql = new StringBuilder();
  119. sql.append("SELECT FSimpleName FROM T_HR_ZDY0HRBase WHERE fnumber = 'LWG'");
  120. IRowSet rs = DbUtil.executeQuery(ctx, sql.toString());
  121. if (rs.next()) {
  122. String simpleName = rs.getString("FSimpleName");
  123. if (!StringUtils.isEmpty(simpleName)) {
  124. try {
  125. return Integer.parseInt(simpleName);
  126. } catch (NumberFormatException e) {
  127. if (logger != null) {
  128. logger.error("劳务工人数格式无效: " + simpleName);
  129. }
  130. return 0;
  131. }
  132. }
  133. }
  134. return 0;
  135. }
  136. /** 判断统计范围是否有效(避免空范围查全表) */
  137. private static boolean hasValidScope(StatScope scope) {
  138. if (!StringUtils.isEmpty(scope.adminOrgId)) {
  139. return true;
  140. }
  141. if (!StringUtils.isEmpty(scope.centerName) && (!StringUtils.isEmpty(scope.orgName)
  142. || !StringUtils.isEmpty(scope.deptCode))) {
  143. return true;
  144. }
  145. return !StringUtils.isEmpty(scope.orgName);
  146. }
  147. /**
  148. * 统计在编人数。
  149. *
  150. * @param employeeTypeNumber 员工类型编码(001正式/002试用),null 表示不限
  151. * @param keyTalentFlag 关键人才标识(1),null 表示不限
  152. */
  153. private static int countOnDuty(Context ctx, StatScope scope, String employeeTypeNumber, String keyTalentFlag,
  154. Logger logger) throws BOSException, SQLException {
  155. StringBuilder sql = new StringBuilder();
  156. sql.append("SELECT COUNT(*) as personCount ");
  157. sql.append("FROM T_BD_PERSON person ");
  158. sql.append("LEFT JOIN T_HR_PersonPosition personPos ON person.FID = personPos.FPERSONID ");
  159. sql.append("INNER JOIN T_HR_BDEmployeeType type ON type.FID = person.FEMPLOYEETYPEID ");
  160. sql.append("WHERE type.FIsOnTheStrength = 1 ");
  161. appendScopeFilter(sql, ctx, scope);
  162. if (!StringUtils.isEmpty(scope.companyId)) {
  163. sql.append("AND personPos.FCompanyID = '").append(escapeSql(scope.companyId)).append("' ");
  164. }
  165. if (!StringUtils.isEmpty(employeeTypeNumber)) {
  166. sql.append("AND type.FNumber = '").append(employeeTypeNumber).append("' ");
  167. }
  168. if (!StringUtils.isEmpty(keyTalentFlag)) {
  169. sql.append("AND person.CFGjrc = '").append(keyTalentFlag).append("' ");
  170. }
  171. if (logger != null) {
  172. logger.error("人员结构统计SQL: " + sql.toString());
  173. }
  174. IRowSet rs = DbUtil.executeQuery(ctx, sql.toString());
  175. if (rs.next()) {
  176. return rs.getInt("personCount");
  177. }
  178. return 0;
  179. }
  180. /**
  181. * 按 StatScope 追加人员任职部门 personPos.FPersonDep 过滤条件。
  182. * 优先级: 中心+部门编码 > 中心下部门 > 行政组织+部门编码 > 行政组织+部门名 > 行政组织子树 > 按名称
  183. */
  184. private static void appendScopeFilter(StringBuilder sql, Context ctx, StatScope scope)
  185. throws BOSException, SQLException {
  186. if (!StringUtils.isEmpty(scope.centerName) && !StringUtils.isEmpty(scope.deptCode)) {
  187. ReportOrgUtil.appendDeptUnderCenterFilterByCode(sql, ctx, scope.centerName, scope.deptCode,
  188. "personPos.FPersonDep");
  189. return;
  190. }
  191. if (!StringUtils.isEmpty(scope.centerName)) {
  192. ReportOrgUtil.appendDeptUnderCenterFilter(sql, ctx, scope.centerName, scope.orgName, "personPos.FPersonDep");
  193. return;
  194. }
  195. if (!StringUtils.isEmpty(scope.adminOrgId) && !StringUtils.isEmpty(scope.deptCode)) {
  196. ReportOrgUtil.appendDeptUnderAdminOrgFilterByCode(sql, ctx, scope.adminOrgId, scope.deptCode,
  197. "personPos.FPersonDep");
  198. return;
  199. }
  200. if (!StringUtils.isEmpty(scope.adminOrgId) && !StringUtils.isEmpty(scope.orgName)) {
  201. ReportOrgUtil.appendDeptUnderAdminOrgFilter(sql, ctx, scope.adminOrgId, scope.orgName,
  202. "personPos.FPersonDep");
  203. return;
  204. }
  205. if (!StringUtils.isEmpty(scope.adminOrgId) && StringUtils.isEmpty(scope.orgName)) {
  206. ReportOrgUtil.appendOrgFilter(sql, ctx, scope.adminOrgId, "personPos.FPersonDep");
  207. return;
  208. }
  209. ReportOrgUtil.appendOrgFilterByName(sql, ctx, scope.orgName, scope.layerType, "personPos.FPersonDep");
  210. }
  211. private static String escapeSql(String value) {
  212. if (value == null) {
  213. return "";
  214. }
  215. return value.replace("'", "''");
  216. }
  217. }