package cn.iocoder.yudao.module.hrm.controller.admin.attendance; import cn.hutool.core.collection.CollUtil; import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.PageParam; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; import cn.iocoder.yudao.module.hrm.controller.admin.attendance.vo.HrmAttendanceSummaryPageReqVO; import cn.iocoder.yudao.module.hrm.controller.admin.attendance.vo.HrmAttendanceSummaryRespVO; import cn.iocoder.yudao.module.hrm.dal.dataobject.attendance.HrmAttendanceSummaryDO; import cn.iocoder.yudao.module.hrm.dal.dataobject.employee.HrmEmployeeDO; import cn.iocoder.yudao.module.hrm.service.attendance.HrmAttendanceSummaryService; 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 io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; import jakarta.servlet.http.HttpServletResponse; import jakarta.validation.Valid; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.io.IOException; import java.util.*; import java.util.stream.Collectors; import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; @Tag(name = "管理后台 - HRM 考勤统计汇总") @RestController @RequestMapping("/hrm/attendance/summary") @Validated public class HrmAttendanceSummaryController { @Resource private HrmAttendanceSummaryService attendanceSummaryService; @Resource private HrmEmployeeService employeeService; @Resource private DeptApi deptApi; @GetMapping("/page") @Operation(summary = "获得考勤统计汇总分页") @PreAuthorize("@ss.hasPermission('hrm:attendance:query')") public CommonResult> getAttendanceSummaryPage(@Valid HrmAttendanceSummaryPageReqVO pageReqVO) { PageResult pageResult = attendanceSummaryService.getAttendanceSummaryPage(pageReqVO); List list = BeanUtils.toBean(pageResult.getList(), HrmAttendanceSummaryRespVO.class); // 填充员工和部门信息 if (CollUtil.isNotEmpty(list)) { fillUserInfo(list, pageResult.getList()); } return success(new PageResult<>(list, pageResult.getTotal())); } /** * 填充员工和部门信息 */ private void fillUserInfo(List respList, List summaryList) { // 批量获取员工信息 Set employeeIds = summaryList.stream() .map(HrmAttendanceSummaryDO::getUserId) .filter(Objects::nonNull) .collect(Collectors.toSet()); Map employeeMap = new HashMap<>(); for (Long employeeId : employeeIds) { HrmEmployeeDO employee = employeeService.getEmployee(employeeId); if (employee != null) { employeeMap.put(employeeId, employee); } } // 批量获取部门信息 Set deptIds = summaryList.stream() .map(HrmAttendanceSummaryDO::getDeptId) .filter(Objects::nonNull) .collect(Collectors.toSet()); Map deptMap = CollUtil.isEmpty(deptIds) ? Collections.emptyMap() : deptApi.getDeptMap(deptIds); // 填充信息 for (HrmAttendanceSummaryRespVO respVO : respList) { HrmEmployeeDO employee = employeeMap.get(respVO.getUserId()); if (employee != null) { respVO.setUserName(employee.getName()); } DeptRespDTO dept = deptMap.get(respVO.getDeptId()); if (dept != null) { respVO.setDeptName(dept.getName()); } } } @GetMapping("/get") @Operation(summary = "获得员工月度考勤汇总") @PreAuthorize("@ss.hasPermission('hrm:attendance:query')") public CommonResult getMonthlySummary( @RequestParam("userId") Long userId, @RequestParam("yearMonth") String yearMonth) { HrmAttendanceSummaryDO summary = attendanceSummaryService.getMonthlySummary(userId, yearMonth); if (summary == null) { return success(null); } HrmAttendanceSummaryRespVO respVO = BeanUtils.toBean(summary, HrmAttendanceSummaryRespVO.class); // 填充员工和部门信息 HrmEmployeeDO employee = employeeService.getEmployee(userId); if (employee != null) { respVO.setUserName(employee.getName()); } if (summary.getDeptId() != null) { DeptRespDTO dept = deptApi.getDept(summary.getDeptId()); if (dept != null) { respVO.setDeptName(dept.getName()); } } return success(respVO); } @PostMapping("/generate") @Operation(summary = "生成月度考勤汇总") @Parameter(name = "yearMonth", description = "年月,格式:yyyy-MM", required = true) @PreAuthorize("@ss.hasPermission('hrm:attendance:calculate')") public CommonResult generateMonthlySummary(@RequestParam("yearMonth") String yearMonth, @RequestParam(value = "deptId", required = false) Long deptId, @RequestParam(value = "userId", required = false) Long userId) { if (userId != null) { attendanceSummaryService.generateMonthlySummary(userId, yearMonth); } else if (deptId != null) { attendanceSummaryService.generateDeptMonthlySummary(deptId, yearMonth); } else { attendanceSummaryService.generateAllMonthlySummary(yearMonth); } return success(true); } @GetMapping("/export-excel") @Operation(summary = "导出考勤统计汇总 Excel") @PreAuthorize("@ss.hasPermission('hrm:attendance:export')") @ApiAccessLog(operateType = EXPORT) public void exportAttendanceSummaryExcel(@Valid HrmAttendanceSummaryPageReqVO pageReqVO, HttpServletResponse response) throws IOException { pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE); PageResult pageResult = attendanceSummaryService.getAttendanceSummaryPage(pageReqVO); List data = BeanUtils.toBean(pageResult.getList(), HrmAttendanceSummaryRespVO.class); // 填充员工和部门信息 if (CollUtil.isNotEmpty(data)) { fillUserInfo(data, pageResult.getList()); } ExcelUtils.write(response, "考勤统计汇总.xls", "数据", HrmAttendanceSummaryRespVO.class, data); } }