package com.yanzhen.controller; import com.github.pagehelper.PageInfo; import com.yanzhen.entity.Building; import com.yanzhen.entity.Dormitory; import com.yanzhen.entity.Notice; import com.yanzhen.service.BuildingService楼层设置; import com.yanzhen.service.DormitoryService宿舍编号设置; import com.yanzhen.service.DormitoryStudentService宿舍预选设置; import com.yanzhen.service.NoticeService公告管理; import com.yanzhen.utils.Result; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @RestController // 声明这是一个控制器,并且返回的数据直接写入 HTTP 响应体中,而不是解析为跳转路径。 @RequestMapping("/main") // 设置请求的根路径为 /main,所有该控制器下的请求都会以 /main 开头。 public class MainController { @Autowired // 自动注入 BuildingService 实例,用于处理楼宇相关的业务逻辑。 private BuildingService楼层设置 buildingService; @Autowired // 自动注入 DormitoryService 实例,用于处理宿舍相关的业务逻辑。 private DormitoryService宿舍编号设置 dormitoryService; @Autowired // 自动注入 DormitoryStudentService 实例,用于处理学生宿舍分配的业务逻辑。 private DormitoryStudentService宿舍预选设置 dormitoryStudentService; @Autowired // 自动注入 NoticeService 实例,用于处理公告通知的业务逻辑。 private NoticeService公告管理 noticeService; @GetMapping("/building") // 映射 HTTP GET 请求到 /main/building 路径。 public Result building(){ Building building = new Building(); // 创建一个新的楼宇对象。 building.setLimit(1000); // 设置查询限制为1000条记录。 PageInfo pageInfo = buildingService.query(building); // 调用服务层方法查询楼宇信息。 List> list = new ArrayList<>(); // 创建一个列表来存储每个楼宇的信息。 DecimalFormat df = new DecimalFormat("######0.00"); // 定义一个格式化工具,用于格式化百分比。 pageInfo.getList().forEach(entity->{ // 遍历查询到的楼宇列表。 Map map = new HashMap<>(); // 创建一个 Map 来存储单个楼宇的信息。 Dormitory param = new Dormitory(); // 创建一个新的宿舍对象。 param.setBuildingId(entity.getId()); // 设置宿舍对象的楼宇ID。 param.setLimit(1000000); // 设置查询限制为1000000条记录。 PageInfo dormitoryPageInfo = dormitoryService.query(param); // 调用服务层方法查询宿舍信息。 int all = dormitoryPageInfo.getList().size(); // 获取该楼宇下的总宿舍数。 map.put("name",entity.getName()); // 将楼宇名称存入 Map。 map.put("all",all); // 将总宿舍数存入 Map。 int used = dormitoryStudentService.countByBuildingId(entity.getId()); // 计算已分配宿舍的学生数量。 map.put("used",used); // 将已分配宿舍的学生数量存入 Map。 int unused = all-used; // 计算未分配宿舍的数量。 map.put("unused",unused); // 将未分配宿舍的数量存入 Map。 if(all == 0){ // 如果总宿舍数为0,则百分比为0。 map.put("percent",0); }else{ // 否则,计算并格式化已使用宿舍的百分比。 map.put("percent",df.format((float)used/all)); } list.add(map); // 将当前楼宇的信息添加到列表中。 }); return Result.ok(list); // 返回包含所有楼宇信息的列表。 } @GetMapping("/notice") // 映射 HTTP GET 请求到 /main/notice 路径。 public Result notice(){ Notice notice = new Notice(); // 创建一个新的公告对象。 notice.setLimit(5); // 设置查询限制为5条记录。 PageInfo pageInfo = noticeService.query(notice); // 调用服务层方法查询公告信息。 return Result.ok(pageInfo.getList()); // 返回包含公告信息的列表。 } }