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.
spring/wll/RepairController.java

121 lines
3.7 KiB

package com.yanzhen.controller;
import com.github.pagehelper.PageInfo;
import com.yanzhen.entity.Repair;
import com.yanzhen.entity.Student;
import com.yanzhen.service.BuildingService;
import com.yanzhen.service.DormitoryService;
import com.yanzhen.service.RepairService;
import com.yanzhen.service.StudentService;
import com.yanzhen.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
//报修管理的控制器
@RestController
@RequestMapping("/repair")
public class RepairController {
//注入实例
//注入报修管理的服务类
@Autowired
private RepairService repairService;
//注入学生服务类
@Autowired
private StudentService studentService;
//注入宿舍服务类
@Autowired
private DormitoryService dormitoryService;
//注入楼宇服务类
@Autowired
private BuildingService buildingService;
//创建新的实例
@PostMapping("create")
public Result create(@RequestBody Repair repair){
//在报修管理服务类中引用create方法
int flag = repairService.create(repair);
//判断是否创建成功
if(flag>0){
//成功
return Result.ok();
}else{
//失败
return Result.fail();
}
}
//删除报修信息
@GetMapping("delete")
public Result delete(String ids){
//在报修管理服务类中引用delete方法
int flag = repairService.delete(ids);
//判断是否删除成功
if(flag>0){
//成功
return Result.ok();
}else{
//失败
return Result.fail();
}
}
//更新报修信息
@PostMapping("update")
public Result update(@RequestBody Repair repair){
//在报修管理服务类中引用update方法
int flag = repairService.updateSelective(repair);
//判断是否更新成功
if(flag>0){
//成功
return Result.ok();
}else{
//失败
return Result.fail();
}
}
//获取报修详细信息
@GetMapping("detail")
public Repair detail(Integer id){
//返回结果
return repairService.detail(id);
}
//查询报修信息
@PostMapping("query")
public Map<String,Object> query(@RequestBody Repair repair){
//创建分页对象
PageInfo<Repair> pageInfo = new PageInfo<>();
//判断查询条件是否包含学生姓名
if(repair.getName() != null){
//根据姓名查找学生ID
Student detailId = studentService.detailByName(repair.getName());
//判断是否找到学生ID
if(detailId != null){
//设置查询条件
repair.setStudentId(detailId.getId());
}else{
//设置分页列表为空
pageInfo.setList(null);
//设置分页大小
pageInfo.setSize(0);
//返回结果
return Result.ok(pageInfo);
}
}
//在报修管理服务类中引用query方法
pageInfo = repairService.query(repair);
//遍历查询结果列表
pageInfo.getList().forEach(entity->{
//设置维修记录的楼宇信息
entity.setBuilding(buildingService.detail(entity.getBuildingId()));
//设置维修记录的学生信息
entity.setStudent(studentService.detail(entity.getStudentId()));
//设置维修记录的宿舍信息
entity.setDormitory(dormitoryService.detail(entity.getDormitoryId()));
});
//返回结果
return Result.ok(pageInfo);
}
}