package com.designhub.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.designhub.entity.Role; import com.designhub.mapper.DepartmentMapper; import com.designhub.mapper.RoleMapper; import com.designhub.mapper.TaskMapper; import com.designhub.mapper.UserMapper; import com.designhub.vo.SystemHealthVO; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.util.List; /** * 系统健康检查服务 */ @Service @RequiredArgsConstructor public class SystemHealthService { private final UserMapper userMapper; private final TaskMapper taskMapper; private final RoleMapper roleMapper; private final DepartmentMapper departmentMapper; @Value("${spring.profiles.active:default}") private String activeProfile; /** * 执行健康检查 */ public SystemHealthVO check() { SystemHealthVO health = new SystemHealthVO(); health.setProfile(activeProfile); try { health.setUserCount(userMapper.selectCount(null)); health.setTaskCount(taskMapper.selectCount(null)); health.setDepartmentCount(departmentMapper.selectCount(null)); health.setRoleCount(roleMapper.selectCount(new LambdaQueryWrapper().select(Role::getId))); health.setDatabaseUp(true); } catch (Exception ex) { health.setDatabaseUp(false); health.getChecks().add(new SystemHealthVO.CheckItem("database", false, ex.getMessage())); return health; } // 校验 sys_role 表结构是否与实体一致(只查询存在的列) try { roleMapper.selectList(new LambdaQueryWrapper() .select(Role::getId, Role::getRoleCode, Role::getRoleName, Role::getStatus) .last("LIMIT 1")); health.getChecks().add(new SystemHealthVO.CheckItem("sys_role_schema", true, "Role columns verified")); } catch (Exception ex) { health.getChecks().add(new SystemHealthVO.CheckItem("sys_role_schema", false, ex.getMessage())); } return health; } }