You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.7 KiB

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
@RequestMapping("/main")
public class MainController {
//注入实例
//注入楼宇服务类
@Autowired
private BuildingService buildingService;
//注入宿舍服务类
@Autowired
private DormitoryService dormitoryService;
//注入学生宿舍服务类
@Autowired
private DormitoryStudentService dormitoryStudentService;
//注入公告通知服务类
@Autowired
private NoticeService noticeService;
//创建新的实例
@GetMapping("/building")
public Result building(){
//创建楼宇信息
Building building = new Building();
//设置查询限制
building.setLimit(1000);
//在首页服务类中引用building方法
PageInfo<Building> pageInfo = buildingService.query(building);
//楼宇信息存入列表
List<Map<String,Object>> list = new ArrayList<>();
//定义一个格式化工具
DecimalFormat df = new DecimalFormat("######0.00");
// 遍历查询到的楼宇列表。
pageInfo.getList().forEach(entity->{
//楼宇信息存入Map中
Map<String,Object> map = new HashMap<>();
//创建新的宿舍
Dormitory param = new Dormitory();
//获取宿舍对应的楼宇信息ID
param.setBuildingId(entity.getId());
//设置限制
param.setLimit(1000000);
//在首页服务类中引用param方法
PageInfo<Dormitory> dormitoryPageInfo = dormitoryService.query(param);
//获取楼宇宿舍信息
int all = dormitoryPageInfo.getList().size();
//楼宇宿舍信息存入Map
map.put("name",entity.getName());
map.put("all",all);
//获取已分配宿舍的学生信息
int used = dormitoryStudentService.countByBuildingId(entity.getId());
//已分配学生信息存入Map中
map.put("used",used);
//计算未分配宿舍的数量
int unused = all-used;
//未分配学生信息存入Map
map.put("unused",unused);
//判断宿舍数量
if(all == 0){
//返回宿舍信息
map.put("percent",0);
}else{
//返回信息
map.put("percent",df.format((float)used/all));
}
//当前楼宇的信息添加到列表
list.add(map);
});
//返回结果
return Result.ok(list);
}
//获取公告通知
@GetMapping("/notice")
public Result notice(){
//创建公告
Notice notice = new Notice();
//设置查询条件
notice.setLimit(5);
//创建分页对象
PageInfo<Notice> pageInfo = noticeService.query(notice);
//返回结果
return Result.ok(pageInfo.getList());
}
}