package cn.iocoder.yudao.module.hrm.service.workhour; import cn.hutool.core.collection.CollUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.hrm.controller.admin.workhour.vo.HrmEmployeeWorkHourReqVO; import cn.iocoder.yudao.module.hrm.controller.admin.workhour.vo.HrmWorkHourSummaryPageReqVO; import cn.iocoder.yudao.module.hrm.controller.admin.workhour.vo.HrmWorkHourSummaryRespVO; import cn.iocoder.yudao.module.hrm.dal.dataobject.employee.HrmEmployeeDO; import cn.iocoder.yudao.module.hrm.dal.dataobject.workhour.HrmWorkHourSummaryDO; import cn.iocoder.yudao.module.hrm.dal.mysql.attendance.HrmAttendanceRecordMapper; import cn.iocoder.yudao.module.hrm.dal.mysql.workhour.HrmWorkHourSummaryMapper; import cn.iocoder.yudao.module.hrm.service.employee.HrmEmployeeService; import cn.iocoder.yudao.module.system.api.dept.DeptApi; import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import java.math.BigDecimal; import java.time.YearMonth; import java.util.Collections; import java.util.List; import java.util.Map; /** * 月度工时汇总 Service 实现类 * * @author 超级管理员 */ @Service @Validated public class HrmWorkHourSummaryServiceImpl implements HrmWorkHourSummaryService { @Resource private HrmWorkHourSummaryMapper workHourSummaryMapper; @Resource private HrmAttendanceRecordMapper attendanceRecordMapper; @Resource private HrmEmployeeService employeeService; @Resource private DeptApi deptApi; @Override @Transactional(rollbackFor = Exception.class) public void generateMonthly(String period) { YearMonth yearMonth = YearMonth.parse(period); HrmEmployeeWorkHourReqVO reqVO = new HrmEmployeeWorkHourReqVO(); reqVO.setStartDate(yearMonth.atDay(1)); reqVO.setEndDate(yearMonth.atEndOfMonth()); for (Map row : attendanceRecordMapper.selectWorkHourSummary(reqVO)) { Long employeeId = toLong(row.get("user_id")); if (employeeId == null) { continue; } HrmEmployeeDO employee = employeeService.getEmployee(employeeId); HrmWorkHourSummaryDO summary = workHourSummaryMapper.selectByEmployeeIdAndPeriod(employeeId, period); if (summary == null) { summary = new HrmWorkHourSummaryDO(); summary.setEmployeeId(employeeId); summary.setPeriod(period); } summary.setDeptId(employee != null ? employee.getDeptId() : null); summary.setWorkDays(toInteger(row.get("work_days"))); summary.setWorkHours(toDecimal(row.get("work_hours"))); summary.setOvertimeHours(toDecimal(row.get("overtime_hours"))); if (summary.getId() == null) { workHourSummaryMapper.insert(summary); } else { workHourSummaryMapper.updateById(summary); } } } @Override public PageResult getWorkHourSummaryPage(HrmWorkHourSummaryPageReqVO pageReqVO) { PageResult pageResult = workHourSummaryMapper.selectPage(pageReqVO); if (CollUtil.isEmpty(pageResult.getList())) { return new PageResult<>(Collections.emptyList(), pageResult.getTotal()); } List list = pageResult.getList().stream() .map(this::convert) .toList(); return new PageResult<>(list, pageResult.getTotal()); } private HrmWorkHourSummaryRespVO convert(HrmWorkHourSummaryDO summary) { HrmWorkHourSummaryRespVO respVO = new HrmWorkHourSummaryRespVO(); respVO.setId(summary.getId()); respVO.setEmployeeId(summary.getEmployeeId()); respVO.setDeptId(summary.getDeptId()); respVO.setPeriod(summary.getPeriod()); respVO.setWorkDays(summary.getWorkDays()); respVO.setWorkHours(summary.getWorkHours()); respVO.setOvertimeHours(summary.getOvertimeHours()); respVO.setRemark(summary.getRemark()); respVO.setCreateTime(summary.getCreateTime()); HrmEmployeeDO employee = employeeService.getEmployee(summary.getEmployeeId()); if (employee != null) { respVO.setEmployeeNo(employee.getEmployeeNo()); respVO.setEmployeeName(employee.getName()); } if (summary.getDeptId() != null) { DeptRespDTO dept = deptApi.getDept(summary.getDeptId()); if (dept != null) { respVO.setDeptName(dept.getName()); } } return respVO; } private Long toLong(Object value) { return value instanceof Number ? ((Number) value).longValue() : null; } private Integer toInteger(Object value) { return value instanceof Number ? ((Number) value).intValue() : 0; } private BigDecimal toDecimal(Object value) { return value instanceof Number ? new BigDecimal(value.toString()) : BigDecimal.ZERO; } }