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.
333 lines
13 KiB
333 lines
13 KiB
package com.gym.service;
|
|
|
|
import com.gym.model.Equipment;
|
|
import com.gym.model.MaintenanceRecord;
|
|
import com.gym.repository.EquipmentRepository;
|
|
import com.gym.repository.MaintenanceRecordRepository;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.PageRequest;
|
|
import org.springframework.data.domain.Pageable;
|
|
import org.springframework.data.domain.Sort;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import java.time.LocalDate;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Optional;
|
|
|
|
@Service
|
|
public class EquipmentService {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(EquipmentService.class);
|
|
|
|
@Autowired
|
|
private EquipmentRepository equipmentRepository;
|
|
|
|
@Autowired
|
|
private MaintenanceRecordRepository maintenanceRecordRepository;
|
|
|
|
// 添加器材
|
|
@Transactional
|
|
public Equipment addEquipment(Equipment equipment) {
|
|
logger.info("添加器材: name={}, brand={}, status={}",
|
|
equipment.getName(), equipment.getBrand(), equipment.getStatus());
|
|
|
|
if (equipment.getPurchaseDate() == null) {
|
|
equipment.setPurchaseDate(LocalDate.now());
|
|
}
|
|
equipment.setUsageCount(0);
|
|
equipment.setIsAvailable(true);
|
|
if (equipment.getStatus() == null) {
|
|
equipment.setStatus("NORMAL");
|
|
}
|
|
if (equipment.getCreatedAt() == null) {
|
|
equipment.setCreatedAt(LocalDate.now());
|
|
}
|
|
if (equipment.getUpdatedAt() == null) {
|
|
equipment.setUpdatedAt(LocalDate.now());
|
|
}
|
|
|
|
Equipment saved = equipmentRepository.save(equipment);
|
|
logger.info("器材添加成功: id={}, name={}", saved.getId(), saved.getName());
|
|
|
|
// 如果是添加维护状态的器材,自动创建维护记录
|
|
if ("MAINTENANCE".equals(equipment.getStatus())) {
|
|
addMaintenanceRecordDirect(
|
|
saved.getId(),
|
|
"新增器材,初始设置为维护状态",
|
|
null,
|
|
"系统自动"
|
|
);
|
|
}
|
|
|
|
return saved;
|
|
}
|
|
|
|
// 直接添加维护记录
|
|
@Transactional
|
|
protected MaintenanceRecord addMaintenanceRecordDirect(Long equipmentId, String notes, Long maintainerId, String maintainerName) {
|
|
logger.info("添加维护记录: equipmentId={}", equipmentId);
|
|
|
|
Equipment equipment = equipmentRepository.findById(equipmentId)
|
|
.orElseThrow(() -> new RuntimeException("器材不存在"));
|
|
|
|
// 创建维护记录
|
|
MaintenanceRecord record = new MaintenanceRecord();
|
|
record.setEquipmentId(equipmentId);
|
|
record.setMaintenanceDate(LocalDate.now());
|
|
record.setMaintainerId(maintainerId);
|
|
record.setMaintainerName(maintainerName);
|
|
record.setNotes(notes);
|
|
record.setStatus("COMPLETED");
|
|
|
|
// 更新设备状态
|
|
equipment.setLastMaintenance(LocalDate.now());
|
|
equipment.setNextMaintenance(LocalDate.now().plusMonths(3));
|
|
equipment.setStatus("NORMAL");
|
|
equipment.setIsAvailable(true);
|
|
|
|
// 更新设备描述,添加维护记录
|
|
String newDescription = equipment.getDescription() == null ? "" : equipment.getDescription();
|
|
newDescription += "\n[维护记录 " + LocalDate.now() + " - " + maintainerName + "]: " + notes;
|
|
equipment.setDescription(newDescription);
|
|
|
|
equipmentRepository.save(equipment);
|
|
MaintenanceRecord savedRecord = maintenanceRecordRepository.save(record);
|
|
logger.info("维护记录添加成功: recordId={}", savedRecord.getId());
|
|
|
|
return savedRecord;
|
|
}
|
|
|
|
// 获取所有器材(带分页和筛选)
|
|
public Map<String, Object> getEquipmentWithFilter(int page, int size, String search, String status, String brand) {
|
|
logger.info("查询器材: page={}, size={}, search={}, status={}, brand={}",
|
|
page, size, search, status, brand);
|
|
|
|
Pageable pageable = PageRequest.of(page - 1, size, Sort.by("id").descending());
|
|
Page<Equipment> equipmentPage;
|
|
|
|
if (search != null && !search.isEmpty()) {
|
|
equipmentPage = equipmentRepository.findByNameContaining(search, pageable);
|
|
} else if (status != null && !status.isEmpty() && brand != null && !brand.isEmpty()) {
|
|
equipmentPage = equipmentRepository.findByStatusAndBrand(status, brand, pageable);
|
|
} else if (status != null && !status.isEmpty()) {
|
|
equipmentPage = equipmentRepository.findByStatus(status, pageable);
|
|
} else if (brand != null && !brand.isEmpty()) {
|
|
equipmentPage = equipmentRepository.findByBrand(brand, pageable);
|
|
} else {
|
|
equipmentPage = equipmentRepository.findAll(pageable);
|
|
}
|
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
result.put("equipment", equipmentPage.getContent());
|
|
result.put("pagination", Map.of(
|
|
"page", page,
|
|
"size", size,
|
|
"total", equipmentPage.getTotalElements(),
|
|
"totalPages", equipmentPage.getTotalPages()
|
|
));
|
|
|
|
// 统计数据
|
|
Map<String, Long> stats = getEquipmentStats();
|
|
result.put("stats", stats);
|
|
|
|
logger.info("查询完成: 找到{}条记录", equipmentPage.getTotalElements());
|
|
|
|
return result;
|
|
}
|
|
|
|
// 获取器材统计信息
|
|
public Map<String, Long> getEquipmentStats() {
|
|
logger.info("获取器材统计信息");
|
|
|
|
Map<String, Long> stats = new HashMap<>();
|
|
long total = equipmentRepository.count();
|
|
long normal = equipmentRepository.countByStatus("NORMAL");
|
|
long maintenance = equipmentRepository.countByStatus("MAINTENANCE");
|
|
long fault = equipmentRepository.countByStatus("FAULT");
|
|
|
|
stats.put("total", total);
|
|
stats.put("normal", normal);
|
|
stats.put("maintenance", maintenance);
|
|
stats.put("fault", fault);
|
|
|
|
logger.info("统计结果: 总计={}, 正常={}, 维护={}, 故障={}",
|
|
total, normal, maintenance, fault);
|
|
|
|
return stats;
|
|
}
|
|
|
|
// 获取器材使用趋势数据
|
|
public Map<String, Object> getEquipmentTrendData() {
|
|
logger.info("获取器材趋势数据");
|
|
|
|
Map<String, Object> trendData = new HashMap<>();
|
|
|
|
// 模拟过去5个月的数据
|
|
List<String> months = List.of("1月", "2月", "3月", "4月", "5月");
|
|
trendData.put("months", months);
|
|
|
|
// 这里应该从数据库查询实际数据,现在用模拟数据
|
|
List<Long> normalData = List.of(75L, 74L, 73L, 72L, 72L);
|
|
List<Long> maintenanceData = List.of(8L, 9L, 9L, 10L, 10L);
|
|
List<Long> faultData = List.of(3L, 3L, 4L, 4L, 4L);
|
|
|
|
trendData.put("normal", normalData);
|
|
trendData.put("maintenance", maintenanceData);
|
|
trendData.put("fault", faultData);
|
|
|
|
return trendData;
|
|
}
|
|
|
|
// 根据ID获取器材
|
|
public Optional<Equipment> getEquipmentById(Long id) {
|
|
logger.info("根据ID获取器材: id={}", id);
|
|
return equipmentRepository.findById(id);
|
|
}
|
|
|
|
// 获取单个器材(包含维护记录)
|
|
public Optional<Equipment> getEquipmentByIdWithMaintenance(Long id) {
|
|
logger.info("获取器材及维护记录: id={}", id);
|
|
return equipmentRepository.findById(id)
|
|
.map(equipment -> {
|
|
List<MaintenanceRecord> records = maintenanceRecordRepository
|
|
.findByEquipmentIdOrderByMaintenanceDateDesc(id);
|
|
equipment.setMaintenanceRecords(records);
|
|
return equipment;
|
|
});
|
|
}
|
|
|
|
// 更新器材信息 - 简化版本
|
|
@Transactional
|
|
public Equipment updateEquipment(Long id, Equipment updatedEquipment) {
|
|
logger.info("更新器材: id={}, 新状态={}", id, updatedEquipment.getStatus());
|
|
|
|
Equipment existing = equipmentRepository.findById(id)
|
|
.orElseThrow(() -> new RuntimeException("器材不存在"));
|
|
|
|
// 直接更新所有字段,不需要复杂的判断
|
|
if (updatedEquipment.getName() != null) {
|
|
existing.setName(updatedEquipment.getName());
|
|
}
|
|
if (updatedEquipment.getModel() != null) {
|
|
existing.setModel(updatedEquipment.getModel());
|
|
}
|
|
if (updatedEquipment.getBrand() != null) {
|
|
existing.setBrand(updatedEquipment.getBrand());
|
|
}
|
|
if (updatedEquipment.getStatus() != null) {
|
|
// 直接更新状态
|
|
existing.setStatus(updatedEquipment.getStatus());
|
|
// 根据状态设置可用性
|
|
if ("MAINTENANCE".equals(updatedEquipment.getStatus()) ||
|
|
"FAULT".equals(updatedEquipment.getStatus())) {
|
|
existing.setIsAvailable(false);
|
|
} else {
|
|
existing.setIsAvailable(true);
|
|
}
|
|
}
|
|
if (updatedEquipment.getLocation() != null) {
|
|
existing.setLocation(updatedEquipment.getLocation());
|
|
}
|
|
if (updatedEquipment.getPurchaseDate() != null) {
|
|
existing.setPurchaseDate(updatedEquipment.getPurchaseDate());
|
|
}
|
|
if (updatedEquipment.getNextMaintenance() != null) {
|
|
existing.setNextMaintenance(updatedEquipment.getNextMaintenance());
|
|
}
|
|
if (updatedEquipment.getPrice() != null) {
|
|
existing.setPrice(updatedEquipment.getPrice());
|
|
}
|
|
if (updatedEquipment.getDescription() != null) {
|
|
existing.setDescription(updatedEquipment.getDescription());
|
|
}
|
|
|
|
existing.setUpdatedAt(LocalDate.now());
|
|
|
|
logger.info("更新完成: id={}, 状态={}, 可用={}",
|
|
existing.getId(), existing.getStatus(), existing.getIsAvailable());
|
|
|
|
return equipmentRepository.save(existing);
|
|
}
|
|
|
|
// 删除器材
|
|
@Transactional
|
|
public void deleteEquipment(Long id) {
|
|
logger.info("删除器材: id={}", id);
|
|
|
|
if (!equipmentRepository.existsById(id)) {
|
|
throw new RuntimeException("器材不存在");
|
|
}
|
|
|
|
// 先删除相关的维护记录
|
|
List<MaintenanceRecord> records = maintenanceRecordRepository.findByEquipmentIdOrderByMaintenanceDateDesc(id);
|
|
if (!records.isEmpty()) {
|
|
logger.info("删除相关维护记录: {}条", records.size());
|
|
maintenanceRecordRepository.deleteAll(records);
|
|
}
|
|
|
|
equipmentRepository.deleteById(id);
|
|
logger.info("器材删除成功: id={}", id);
|
|
}
|
|
|
|
// 器材使用(增加使用次数)
|
|
@Transactional
|
|
public Equipment useEquipment(Long id) {
|
|
logger.info("器材使用: id={}", id);
|
|
|
|
return equipmentRepository.findById(id)
|
|
.map(equipment -> {
|
|
int newCount = equipment.getUsageCount() + 1;
|
|
equipment.setUsageCount(newCount);
|
|
Equipment saved = equipmentRepository.save(equipment);
|
|
logger.info("使用次数更新: id={}, 次数={}", saved.getId(), saved.getUsageCount());
|
|
return saved;
|
|
})
|
|
.orElseThrow(() -> new RuntimeException("器材不存在"));
|
|
}
|
|
|
|
// 设置器材状态
|
|
@Transactional
|
|
public Equipment setEquipmentStatus(Long id, String status) {
|
|
logger.info("设置器材状态: id={}, status={}", id, status);
|
|
|
|
return equipmentRepository.findById(id)
|
|
.map(equipment -> {
|
|
String oldStatus = equipment.getStatus();
|
|
equipment.setStatus(status);
|
|
|
|
if ("MAINTENANCE".equals(status) || "FAULT".equals(status)) {
|
|
equipment.setIsAvailable(false);
|
|
logger.info("状态变更: {} -> {}, 可用=false", oldStatus, status);
|
|
} else {
|
|
equipment.setIsAvailable(true);
|
|
logger.info("状态变更: {} -> {}, 可用=true", oldStatus, status);
|
|
}
|
|
|
|
Equipment saved = equipmentRepository.save(equipment);
|
|
return saved;
|
|
})
|
|
.orElseThrow(() -> new RuntimeException("器材不存在"));
|
|
}
|
|
|
|
// 搜索器材
|
|
public List<Equipment> searchEquipment(String keyword) {
|
|
logger.info("搜索器材: keyword={}", keyword);
|
|
return equipmentRepository.findByNameContaining(keyword);
|
|
}
|
|
|
|
// 添加维护记录(对外暴露的方法)
|
|
@Transactional
|
|
public Equipment addMaintenanceRecord(Long id, String notes, Long maintainerId, String maintainerName) {
|
|
logger.info("添加维护记录: id={}, maintainer={}", id, maintainerName);
|
|
|
|
addMaintenanceRecordDirect(id, notes, maintainerId, maintainerName);
|
|
return equipmentRepository.findById(id)
|
|
.orElseThrow(() -> new RuntimeException("器材不存在"));
|
|
}
|
|
} |