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.
213 lines
8.9 KiB
213 lines
8.9 KiB
package com.gym.controller;
|
|
|
|
import com.gym.model.Equipment;
|
|
import com.gym.service.EquipmentService;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/equipment")
|
|
@CrossOrigin(origins = "*")
|
|
public class EquipmentController {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(EquipmentController.class);
|
|
|
|
@Autowired
|
|
private EquipmentService equipmentService;
|
|
|
|
// 添加器材 - 添加详细日志
|
|
@PostMapping
|
|
public Map<String, Object> addEquipment(@RequestBody Equipment equipment) {
|
|
logger.info("接收到添加器材请求: {}", equipment);
|
|
Map<String, Object> response = new HashMap<>();
|
|
try {
|
|
logger.info("器材信息: name={}, brand={}, location={}, purchaseDate={}",
|
|
equipment.getName(), equipment.getBrand(),
|
|
equipment.getLocation(), equipment.getPurchaseDate());
|
|
|
|
Equipment savedEquipment = equipmentService.addEquipment(equipment);
|
|
logger.info("器材保存成功, ID: {}", savedEquipment.getId());
|
|
|
|
response.put("code", 200);
|
|
response.put("message", "器材添加成功");
|
|
response.put("data", savedEquipment);
|
|
} catch (Exception e) {
|
|
logger.error("添加器材失败", e);
|
|
response.put("code", 400);
|
|
response.put("message", e.getMessage());
|
|
response.put("error", e.toString());
|
|
}
|
|
return response;
|
|
}
|
|
|
|
// 获取所有器材(带分页和筛选)
|
|
@GetMapping
|
|
public Map<String, Object> getEquipment(
|
|
@RequestParam(defaultValue = "1") int page,
|
|
@RequestParam(defaultValue = "10") int size,
|
|
@RequestParam(required = false) String search,
|
|
@RequestParam(required = false) String status,
|
|
@RequestParam(required = false) String brand) {
|
|
|
|
logger.info("获取器材列表: page={}, size={}, search={}, status={}, brand={}",
|
|
page, size, search, status, brand);
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
try {
|
|
Map<String, Object> result = equipmentService.getEquipmentWithFilter(page, size, search, status, brand);
|
|
logger.info("查询到{}条器材记录", result.containsKey("pagination") ?
|
|
((Map<?, ?>)result.get("pagination")).get("total") : 0);
|
|
|
|
response.put("code", 200);
|
|
response.put("message", "获取成功");
|
|
response.put("data", result);
|
|
} catch (Exception e) {
|
|
logger.error("获取器材列表失败", e);
|
|
response.put("code", 500);
|
|
response.put("message", "获取数据失败: " + e.getMessage());
|
|
}
|
|
return response;
|
|
}
|
|
|
|
// 获取单个器材详情(用于编辑)
|
|
@GetMapping("/{id}")
|
|
public Map<String, Object> getEquipmentById(@PathVariable Long id) {
|
|
logger.info("接收到获取器材详情请求: id={}", id);
|
|
Map<String, Object> response = new HashMap<>();
|
|
try {
|
|
return equipmentService.getEquipmentById(id)
|
|
.map(equipment -> {
|
|
response.put("code", 200);
|
|
response.put("message", "获取成功");
|
|
response.put("data", equipment);
|
|
return response;
|
|
})
|
|
.orElseGet(() -> {
|
|
response.put("code", 404);
|
|
response.put("message", "器材不存在");
|
|
return response;
|
|
});
|
|
} catch (Exception e) {
|
|
logger.error("获取器材详情失败", e);
|
|
response.put("code", 500);
|
|
response.put("message", "获取失败: " + e.getMessage());
|
|
return response;
|
|
}
|
|
}
|
|
|
|
// 更新器材信息
|
|
@PutMapping("/{id}")
|
|
public Map<String, Object> updateEquipment(@PathVariable Long id, @RequestBody Equipment equipment) {
|
|
logger.info("接收到更新器材请求: id={}, equipment={}", id, equipment);
|
|
logger.info("更新数据详情: name={}, status={}, location={}, brand={}, purchaseDate={}",
|
|
equipment.getName(), equipment.getStatus(), equipment.getLocation(),
|
|
equipment.getBrand(), equipment.getPurchaseDate());
|
|
|
|
Map<String, Object> response = new HashMap<>();
|
|
try {
|
|
Equipment updatedEquipment = equipmentService.updateEquipment(id, equipment);
|
|
logger.info("器材更新成功: id={}, 新状态={}, 原状态={}",
|
|
updatedEquipment.getId(), updatedEquipment.getStatus(), equipment.getStatus());
|
|
logger.info("更新后的器材信息: {}", updatedEquipment);
|
|
|
|
response.put("code", 200);
|
|
response.put("message", "更新成功");
|
|
response.put("data", updatedEquipment);
|
|
} catch (Exception e) {
|
|
logger.error("更新器材失败", e);
|
|
response.put("code", 400);
|
|
response.put("message", e.getMessage());
|
|
}
|
|
return response;
|
|
}
|
|
|
|
// 删除器材
|
|
@DeleteMapping("/{id}")
|
|
public Map<String, Object> deleteEquipment(@PathVariable Long id) {
|
|
logger.info("接收到删除器材请求: id={}", id);
|
|
Map<String, Object> response = new HashMap<>();
|
|
try {
|
|
equipmentService.deleteEquipment(id);
|
|
logger.info("器材删除成功: id={}", id);
|
|
|
|
response.put("code", 200);
|
|
response.put("message", "删除成功");
|
|
} catch (Exception e) {
|
|
logger.error("删除器材失败", e);
|
|
response.put("code", 400);
|
|
response.put("message", e.getMessage());
|
|
}
|
|
return response;
|
|
}
|
|
|
|
// 添加维护记录
|
|
@PostMapping("/{id}/maintenance")
|
|
public Map<String, Object> addMaintenanceRecord(
|
|
@PathVariable Long id,
|
|
@RequestBody Map<String, Object> data) {
|
|
|
|
logger.info("接收到添加维护记录请求: equipmentId={}, data={}", id, data);
|
|
Map<String, Object> response = new HashMap<>();
|
|
try {
|
|
String notes = (String) data.get("notes");
|
|
Long maintainerId = data.containsKey("maintainerId") ?
|
|
Long.valueOf(data.get("maintainerId").toString()) : null;
|
|
String maintainerName = data.containsKey("maintainerName") ?
|
|
(String) data.get("maintainerName") : "管理员";
|
|
|
|
logger.info("维护信息: notes={}, maintainer={}", notes, maintainerName);
|
|
|
|
Equipment equipment = equipmentService.addMaintenanceRecord(id, notes, maintainerId, maintainerName);
|
|
logger.info("维护记录添加成功,器材状态已更新: id={}, status={}", id, equipment.getStatus());
|
|
|
|
response.put("code", 200);
|
|
response.put("message", "维护记录添加成功,器材状态已更新为正常");
|
|
response.put("data", equipment);
|
|
} catch (Exception e) {
|
|
logger.error("添加维护记录失败", e);
|
|
response.put("code", 400);
|
|
response.put("message", e.getMessage());
|
|
}
|
|
return response;
|
|
}
|
|
|
|
// 添加统计信息接口(可选,但建议添加)
|
|
@GetMapping("/stats")
|
|
public Map<String, Object> getEquipmentStats() {
|
|
logger.info("接收到获取器材统计请求");
|
|
Map<String, Object> response = new HashMap<>();
|
|
try {
|
|
Map<String, Long> stats = equipmentService.getEquipmentStats();
|
|
response.put("code", 200);
|
|
response.put("message", "获取成功");
|
|
response.put("data", stats);
|
|
} catch (Exception e) {
|
|
logger.error("获取器材统计失败", e);
|
|
response.put("code", 500);
|
|
response.put("message", "获取统计信息失败: " + e.getMessage());
|
|
}
|
|
return response;
|
|
}
|
|
|
|
// 添加趋势数据接口(可选,但建议添加)
|
|
@GetMapping("/trend")
|
|
public Map<String, Object> getEquipmentTrend() {
|
|
logger.info("接收到获取器材趋势数据请求");
|
|
Map<String, Object> response = new HashMap<>();
|
|
try {
|
|
Map<String, Object> trendData = equipmentService.getEquipmentTrendData();
|
|
response.put("code", 200);
|
|
response.put("message", "获取成功");
|
|
response.put("data", trendData);
|
|
} catch (Exception e) {
|
|
logger.error("获取器材趋势数据失败", e);
|
|
response.put("code", 500);
|
|
response.put("message", "获取趋势数据失败: " + e.getMessage());
|
|
}
|
|
return response;
|
|
}
|
|
} |