|
|
|
|
@ -2,173 +2,152 @@ package com.campus.water.controller.web;
|
|
|
|
|
|
|
|
|
|
import com.campus.water.entity.Area;
|
|
|
|
|
import com.campus.water.service.AreaService;
|
|
|
|
|
import com.campus.water.util.ResultVO;
|
|
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
|
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 区域管理控制器
|
|
|
|
|
* 处理校园/楼宇/区域的增删改查接口请求
|
|
|
|
|
* 不使用 ResultVO,直接通过 ResponseEntity 返回响应
|
|
|
|
|
* 适配 Area 实体(areaId 主键、市区-校园层级)
|
|
|
|
|
*/
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/api/web/area")
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
@Tag(name = "区域管理接口", description = "校园、楼宇、区域的层级管理(增删改查)")
|
|
|
|
|
@RequestMapping("/api/area")
|
|
|
|
|
@CrossOrigin // 允许跨域(前端调用时需要)
|
|
|
|
|
public class AreaController {
|
|
|
|
|
|
|
|
|
|
private final AreaService areaService;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 新增区域(校园/楼宇/区域)
|
|
|
|
|
* 仅超级管理员可操作
|
|
|
|
|
*/
|
|
|
|
|
@PostMapping("/add")
|
|
|
|
|
@PreAuthorize("hasRole('SUPER_ADMIN')")
|
|
|
|
|
@Operation(summary = "新增区域", description = "创建校园/楼宇/区域,严格校验层级关联规则")
|
|
|
|
|
public ResponseEntity<ResultVO<Area>> addArea(
|
|
|
|
|
@RequestBody @Parameter(description = "区域信息(名称/类型为必填)") Area area
|
|
|
|
|
) {
|
|
|
|
|
try {
|
|
|
|
|
Area newArea = areaService.addArea(area);
|
|
|
|
|
return ResponseEntity.ok(ResultVO.success(newArea, "新增区域成功"));
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
return ResponseEntity.ok(ResultVO.error(500, "新增失败:" + e.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
// 构造器注入
|
|
|
|
|
public AreaController(AreaService areaService) {
|
|
|
|
|
this.areaService = areaService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除区域
|
|
|
|
|
* 仅超级管理员可操作,需校验无关联管理员和子区域
|
|
|
|
|
* 构建通用响应体
|
|
|
|
|
* @param code 响应码(200成功,400参数/业务异常,500系统异常)
|
|
|
|
|
* @param msg 响应消息
|
|
|
|
|
* @param data 响应数据
|
|
|
|
|
* @return 封装后的Map响应体
|
|
|
|
|
*/
|
|
|
|
|
@DeleteMapping("/delete/{areaId}")
|
|
|
|
|
@PreAuthorize("hasRole('SUPER_ADMIN')")
|
|
|
|
|
@Operation(summary = "删除区域", description = "删除指定区域,需确保无关联管理员和子区域")
|
|
|
|
|
public ResponseEntity<ResultVO<Void>> deleteArea(
|
|
|
|
|
@PathVariable @Parameter(description = "区域ID") String areaId
|
|
|
|
|
) {
|
|
|
|
|
try {
|
|
|
|
|
areaService.deleteArea(areaId);
|
|
|
|
|
return ResponseEntity.ok(ResultVO.success(null, "删除区域成功"));
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
return ResponseEntity.ok(ResultVO.error(500, "删除失败:" + e.getMessage()));
|
|
|
|
|
}
|
|
|
|
|
private Map<String, Object> buildResponse(int code, String msg, Object data) {
|
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
|
|
|
response.put("code", code);
|
|
|
|
|
response.put("msg", msg);
|
|
|
|
|
response.put("data", data);
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 修改区域信息
|
|
|
|
|
* 仅超级管理员可操作,不允许修改区域类型
|
|
|
|
|
* 新增区域(市区/校园)
|
|
|
|
|
* @param area 区域信息(JSON格式)
|
|
|
|
|
* @return 新增后的区域对象
|
|
|
|
|
*/
|
|
|
|
|
@PutMapping("/update")
|
|
|
|
|
@PreAuthorize("hasRole('SUPER_ADMIN')")
|
|
|
|
|
@Operation(summary = "修改区域", description = "更新区域名称/父级/地址/负责人等信息,不允许修改区域类型")
|
|
|
|
|
public ResponseEntity<ResultVO<Area>> updateArea(
|
|
|
|
|
@RequestBody @Parameter(description = "区域信息(areaId为必填)") Area area
|
|
|
|
|
) {
|
|
|
|
|
@PostMapping("/add")
|
|
|
|
|
public ResponseEntity<Map<String, Object>> addArea(@RequestBody Area area) {
|
|
|
|
|
try {
|
|
|
|
|
if (area.getAreaId() == null) {
|
|
|
|
|
return ResponseEntity.ok(ResultVO.error(400, "区域ID不能为空"));
|
|
|
|
|
}
|
|
|
|
|
Area updatedArea = areaService.updateArea(area);
|
|
|
|
|
return ResponseEntity.ok(ResultVO.success(updatedArea, "修改区域成功"));
|
|
|
|
|
Area savedArea = areaService.addArea(area);
|
|
|
|
|
return ResponseEntity.ok(buildResponse(200, "新增成功", savedArea));
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
return ResponseEntity.ok(ResultVO.error(500, "修改失败:" + e.getMessage()));
|
|
|
|
|
// 业务异常:400状态码 + 具体提示
|
|
|
|
|
return ResponseEntity.badRequest().body(buildResponse(400, e.getMessage(), null));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// 系统异常:500状态码 + 通用提示
|
|
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
|
|
.body(buildResponse(500, "新增区域失败:" + e.getMessage(), null));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 按ID查询区域详情
|
|
|
|
|
* 超级管理员/区域管理员均可查询
|
|
|
|
|
* 修改区域
|
|
|
|
|
* @param areaId 区域ID
|
|
|
|
|
* @param area 待修改的区域信息
|
|
|
|
|
* @return 修改后的区域对象
|
|
|
|
|
*/
|
|
|
|
|
@GetMapping("/detail/{areaId}")
|
|
|
|
|
@PreAuthorize("hasAnyRole('SUPER_ADMIN', 'AREA_ADMIN')")
|
|
|
|
|
@Operation(summary = "查询区域详情", description = "按ID查询单个区域的完整信息")
|
|
|
|
|
public ResponseEntity<ResultVO<Area>> getAreaDetail(
|
|
|
|
|
@PathVariable @Parameter(description = "区域ID") String areaId
|
|
|
|
|
) {
|
|
|
|
|
@PutMapping("/update/{areaId}")
|
|
|
|
|
public ResponseEntity<Map<String, Object>> updateArea(@PathVariable String areaId, @RequestBody Area area) {
|
|
|
|
|
try {
|
|
|
|
|
Area area = areaService.getAreaById(areaId);
|
|
|
|
|
return ResponseEntity.ok(ResultVO.success(area));
|
|
|
|
|
Area updatedArea = areaService.updateArea(areaId, area);
|
|
|
|
|
return ResponseEntity.ok(buildResponse(200, "修改成功", updatedArea));
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
return ResponseEntity.ok(ResultVO.error(500, "查询失败:" + e.getMessage()));
|
|
|
|
|
return ResponseEntity.badRequest().body(buildResponse(400, e.getMessage(), null));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
|
|
.body(buildResponse(500, "修改区域失败:" + e.getMessage(), null));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 条件查询区域列表
|
|
|
|
|
* 超级管理员/区域管理员均可查询,支持多条件筛选
|
|
|
|
|
* 删除区域
|
|
|
|
|
* @param areaId 区域ID
|
|
|
|
|
* @return 操作结果
|
|
|
|
|
*/
|
|
|
|
|
@GetMapping("/list")
|
|
|
|
|
@PreAuthorize("hasAnyRole('SUPER_ADMIN', 'AREA_ADMIN')")
|
|
|
|
|
@Operation(summary = "查询区域列表", description = "支持按父级ID/区域类型/名称关键词筛选区域")
|
|
|
|
|
public ResponseEntity<ResultVO<List<Area>>> listAreas(
|
|
|
|
|
@RequestParam(required = false) @Parameter(description = "父级区域ID") String parentAreaId,
|
|
|
|
|
@RequestParam(required = false) @Parameter(description = "区域类型(campus/building/zone)") Area.AreaType areaType,
|
|
|
|
|
@RequestParam(required = false) @Parameter(description = "名称关键词(模糊匹配)") String keyword
|
|
|
|
|
) {
|
|
|
|
|
@DeleteMapping("/delete/{areaId}")
|
|
|
|
|
public ResponseEntity<Map<String, Object>> deleteArea(@PathVariable String areaId) {
|
|
|
|
|
try {
|
|
|
|
|
List<Area> areas = areaService.listAreas(parentAreaId, areaType, keyword);
|
|
|
|
|
return ResponseEntity.ok(ResultVO.success(areas));
|
|
|
|
|
areaService.deleteArea(areaId);
|
|
|
|
|
return ResponseEntity.ok(buildResponse(200, "删除成功", null));
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
return ResponseEntity.ok(ResultVO.error(500, "查询失败:" + e.getMessage()));
|
|
|
|
|
return ResponseEntity.badRequest().body(buildResponse(400, e.getMessage(), null));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
|
|
.body(buildResponse(500, "删除区域失败:" + e.getMessage(), null));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 查询所有校园(顶级节点)
|
|
|
|
|
* 超级管理员/区域管理员均可查询
|
|
|
|
|
* 查询所有市区(根节点)
|
|
|
|
|
* @return 市区列表
|
|
|
|
|
*/
|
|
|
|
|
@GetMapping("/list-campus")
|
|
|
|
|
@PreAuthorize("hasAnyRole('SUPER_ADMIN', 'AREA_ADMIN')")
|
|
|
|
|
@Operation(summary = "查询所有校园", description = "快速获取所有校园级别的顶级区域")
|
|
|
|
|
public ResponseEntity<ResultVO<List<Area>>> listCampus() {
|
|
|
|
|
@GetMapping("/cities")
|
|
|
|
|
public ResponseEntity<Map<String, Object>> getAllCities() {
|
|
|
|
|
try {
|
|
|
|
|
List<Area> campusList = areaService.listAreas(null, Area.AreaType.campus, null);
|
|
|
|
|
return ResponseEntity.ok(ResultVO.success(campusList));
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
return ResponseEntity.ok(ResultVO.error(500, "查询失败:" + e.getMessage()));
|
|
|
|
|
List<Area> cities = areaService.getAllCities();
|
|
|
|
|
return ResponseEntity.ok(buildResponse(200, "查询成功", cities));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
|
|
.body(buildResponse(500, "查询市区列表失败:" + e.getMessage(), null));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 查询指定校园下的所有楼宇
|
|
|
|
|
* 超级管理员/区域管理员均可查询
|
|
|
|
|
* 根据市区ID查询下属校园
|
|
|
|
|
* @param cityId 市区ID(areaId)
|
|
|
|
|
* @return 该市区下的校园列表
|
|
|
|
|
*/
|
|
|
|
|
@GetMapping("/list-building/{campusId}")
|
|
|
|
|
@PreAuthorize("hasAnyRole('SUPER_ADMIN', 'AREA_ADMIN')")
|
|
|
|
|
@Operation(summary = "查询校园下的楼宇", description = "按校园ID查询该校园下的所有楼宇")
|
|
|
|
|
public ResponseEntity<ResultVO<List<Area>>> listBuildingByCampus(
|
|
|
|
|
@PathVariable @Parameter(description = "校园ID") String campusId
|
|
|
|
|
) {
|
|
|
|
|
@GetMapping("/campuses/{cityId}")
|
|
|
|
|
public ResponseEntity<Map<String, Object>> getCampusesByCityId(@PathVariable String cityId) {
|
|
|
|
|
try {
|
|
|
|
|
List<Area> buildingList = areaService.listAreas(campusId, Area.AreaType.building, null);
|
|
|
|
|
return ResponseEntity.ok(ResultVO.success(buildingList));
|
|
|
|
|
List<Area> campuses = areaService.getCampusesByCityId(cityId);
|
|
|
|
|
return ResponseEntity.ok(buildResponse(200, "查询成功", campuses));
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
return ResponseEntity.ok(ResultVO.error(500, "查询失败:" + e.getMessage()));
|
|
|
|
|
return ResponseEntity.badRequest().body(buildResponse(400, e.getMessage(), null));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
|
|
.body(buildResponse(500, "查询校园列表失败:" + e.getMessage(), null));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 查询指定楼宇下的所有区域
|
|
|
|
|
* 超级管理员/区域管理员均可查询
|
|
|
|
|
* 根据区域ID查询单个区域信息
|
|
|
|
|
* (扩展接口:方便前端回显详情)
|
|
|
|
|
* @param areaId 区域ID
|
|
|
|
|
* @return 区域详情
|
|
|
|
|
*/
|
|
|
|
|
@GetMapping("/list-zone/{buildingId}")
|
|
|
|
|
@PreAuthorize("hasAnyRole('SUPER_ADMIN', 'AREA_ADMIN')")
|
|
|
|
|
@Operation(summary = "查询楼宇下的区域", description = "按楼宇ID查询该楼宇下的所有子区域")
|
|
|
|
|
public ResponseEntity<ResultVO<List<Area>>> listZoneByBuilding(
|
|
|
|
|
@PathVariable @Parameter(description = "楼宇ID") String buildingId
|
|
|
|
|
) {
|
|
|
|
|
@GetMapping("/{areaId}")
|
|
|
|
|
public ResponseEntity<Map<String, Object>> getAreaById(@PathVariable String areaId) {
|
|
|
|
|
try {
|
|
|
|
|
List<Area> zoneList = areaService.listAreas(buildingId, Area.AreaType.zone, null);
|
|
|
|
|
return ResponseEntity.ok(ResultVO.success(zoneList));
|
|
|
|
|
Area area = areaService.getAreaById(areaId);
|
|
|
|
|
return ResponseEntity.ok(buildResponse(200, "查询成功", area));
|
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
|
return ResponseEntity.ok(ResultVO.error(500, "查询失败:" + e.getMessage()));
|
|
|
|
|
return ResponseEntity.badRequest().body(buildResponse(400, e.getMessage(), null));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
|
|
.body(buildResponse(500, "查询区域详情失败:" + e.getMessage(), null));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|