liyong
15 小时以前 217f8ee47bb19f8e154d4012dbe232f0d7af3a3d
src/main/java/com/ruoyi/staff/service/impl/AnalyticsServiceImpl.java
@@ -16,7 +16,9 @@
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@AllArgsConstructor
@Service
@@ -29,16 +31,32 @@
    @Override
    public List<StaffLeaveDto> staffLeaveReasonAnalytics() {
        List<StaffLeaveDto> result = staffLeaveMapper.staffLeaveReasonAnalytics();
        result.forEach(dto -> {
        List<StaffLeaveDto> dbResult = staffLeaveMapper.staffLeaveReasonAnalytics();
        // 创建一个Map用于存储所有枚举原因的数量,默认值为0
        Map<String, Integer> reasonCountMap = new HashMap<>();
        for (StaffLeaveReason reasonEnum : StaffLeaveReason.values()) {
            reasonCountMap.put(reasonEnum.getCode(), 0);
        }
        // 将数据库查询结果合并到Map中
        for (StaffLeaveDto dto : dbResult) {
            String reasonCode = dto.getReason();
            StaffLeaveReason reasonEnum = StaffLeaveReason.getByCode(reasonCode);
            if (reasonEnum != null) {
                dto.setReasonText(reasonEnum.getInfo());
            } else {
                dto.setReasonText("未知原因");
            if (reasonCountMap.containsKey(reasonCode)) {
                reasonCountMap.put(reasonCode, dto.getCount());
            }
        });
        }
        // 将Map转换为List<StaffLeaveDto>
        List<StaffLeaveDto> result = new ArrayList<>();
        for (StaffLeaveReason reasonEnum : StaffLeaveReason.values()) {
            StaffLeaveDto dto = new StaffLeaveDto();
            dto.setReason(reasonEnum.getCode());
            dto.setCount(reasonCountMap.get(reasonEnum.getCode()));
            dto.setReasonText(reasonEnum.getInfo());
            result.add(dto);
        }
        return result;
    }
@@ -76,19 +94,22 @@
            Integer leaveCount = staffLeaveMapper.countLeaveByMonth(monthStart, monthEnd);
            vo.setLeaveCount(leaveCount != null ? leaveCount : 0);
            // 计算流失率:流失率 = 月度离职员工数 / 月初员工数 * 100%
            // 计算当期平均在职人数 = (月初员工数 + 月末员工数) / 2
            Double averageStaffCount = (vo.getBeginMonthStaffCount() + vo.getEndMonthStaffCount()) / 2.0;
            // 计算流失率:流失率 = 月度离职员工数 / 当期平均在职人数 * 100%
            Double turnoverRate = 0.0;
            if (vo.getBeginMonthStaffCount() > 0) {
                turnoverRate = (double) vo.getLeaveCount() / vo.getBeginMonthStaffCount() * 100;
            if (averageStaffCount > 0) {
                turnoverRate = (double) vo.getLeaveCount() / averageStaffCount * 100;
                // 保留两位小数
                turnoverRate = Math.round(turnoverRate * 100.0) / 100.0;
            }
            vo.setTurnoverRate(turnoverRate);
            // 计算流动率:流动率 = (月度入职员工数 + 月度离职员工数) / 月初员工数 * 100%
            // 计算流动率:流动率 = (月度入职员工数 + 月度离职员工数) / 当期平均在职人数 * 100%
            Double flowRate = 0.0;
            if (vo.getBeginMonthStaffCount() > 0) {
                flowRate = (double) (vo.getNewHireCount() + vo.getLeaveCount()) / vo.getBeginMonthStaffCount() * 100;
            if (averageStaffCount > 0) {
                flowRate = (double) (vo.getNewHireCount() + vo.getLeaveCount()) / averageStaffCount * 100;
                // 保留两位小数
                flowRate = Math.round(flowRate * 100.0) / 100.0;
            }
@@ -115,26 +136,36 @@
        // 获取月初员工数(即上月末员工数)
        Integer beginMonthStaffCount = staffOnJobMapper.countOnJobStaffByDate(monthStartDate.minusDays(1));
        beginMonthStaffCount = beginMonthStaffCount != null ? beginMonthStaffCount : 0;
        // 获取月末员工数
        Integer endMonthStaffCount = staffOnJobMapper.countOnJobStaffByDate(monthEndDate);
        endMonthStaffCount = endMonthStaffCount != null ? endMonthStaffCount : 0;
        // 获取本月新入职员工数
        Integer newHireCount = staffOnJobMapper.countNewHireByMonth(monthStartDate, monthEndDate);
        newHireCount = newHireCount != null ? newHireCount : 0;
        // 获取本月离职员工数
        Integer leaveCount = staffLeaveMapper.countLeaveByMonth(monthStartDate, monthEndDate);
        leaveCount = leaveCount != null ? leaveCount : 0;
        // 计算总流动率 = (入职人数 + 离职人数) / 月初员工数 * 100%
        // 计算当期平均在职人数 = (月初员工数 + 月末员工数) / 2
        Double averageStaffCount = (beginMonthStaffCount + endMonthStaffCount) / 2.0;
        // 计算总流动率 = (入职人数 + 离职人数) / 当期平均在职人数 * 100%
        Double totalFlowRate = 0.0;
        if (beginMonthStaffCount > 0) {
            totalFlowRate = (double) (newHireCount + leaveCount) / beginMonthStaffCount * 100;
        if (averageStaffCount > 0) {
            totalFlowRate = (double) (newHireCount + leaveCount) / averageStaffCount * 100;
            // 保留两位小数
            totalFlowRate = Math.round(totalFlowRate * 100.0) / 100.0;
        }
        result.setTotalFlowRate(totalFlowRate);
        // 计算总流失率 = 离职人数 / 月初员工数 * 100%
        // 计算总流失率 = 离职人数 / 当期平均在职人数 * 100%
        Double totalTurnoverRate = 0.0;
        if (beginMonthStaffCount > 0) {
            totalTurnoverRate = (double) leaveCount / beginMonthStaffCount * 100;
        if (averageStaffCount > 0) {
            totalTurnoverRate = (double) leaveCount / averageStaffCount * 100;
            // 保留两位小数
            totalTurnoverRate = Math.round(totalTurnoverRate * 100.0) / 100.0;
        }