每日总结
package com.mentalhealth.service;
import com.mentalhealth.dto.CorrectionRecordDTO;
import com.mentalhealth.entity.Consultant;
import com.mentalhealth.entity.CorrectionMetric;
import com.mentalhealth.entity.CorrectionRecord;
import com.mentalhealth.entity.StudentElectronicRecord;
import com.mentalhealth.repository.ConsultantRepository;
import com.mentalhealth.repository.CorrectionRecordRepository;
import com.mentalhealth.repository.StudentElectronicRecordRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
public class CorrectionRecordService {
private final CorrectionRecordRepository correctionRecordRepository;
private final ConsultantRepository consultantRepository;
private final StudentElectronicRecordRepository studentRecordRepository;
// 创建矫正记录
@Transactional
public CorrectionRecord createCorrectionRecord(String username, CorrectionRecordDTO correctionDTO) {
Consultant consultant = consultantRepository.findByUserUsername(username)
.orElseThrow(() -> new RuntimeException("咨询师不存在"));
StudentElectronicRecord studentRecord = studentRecordRepository.findById(correctionDTO.getStudentRecordId())
.orElseThrow(() -> new RuntimeException("学生档案不存在"));
// 验证咨询师是否有权限访问该学生档案
if (!studentRecord.getConsultant().getId().equals(consultant.getId())) {
throw new RuntimeException("无权访问该学生档案");
}
CorrectionRecord record = new CorrectionRecord();
record.setConsultant(consultant);
record.setStudentRecord(studentRecord);
record.setSessionDate(correctionDTO.getSessionDate());
record.setSessionDuration(correctionDTO.getSessionDuration());
record.setInterventionMethods(correctionDTO.getInterventionMethods());
record.setSessionSummary(correctionDTO.getSessionSummary());
record.setProgressNotes(correctionDTO.getProgressNotes());
record.setNextSessionPlan(correctionDTO.getNextSessionPlan());
// 设置矫正指标
if (correctionDTO.getMetrics() != null) {
record.setMetrics(correctionDTO.getMetrics().stream()
.map(metricDTO -> {
CorrectionMetric metric = new CorrectionMetric();
metric.setMetricName(metricDTO.getMetricName());
metric.setMetricValue(metricDTO.getMetricValue());
metric.setMetricUnit(metricDTO.getMetricUnit());
return metric;
})
.collect(Collectors.toList()));
}
return correctionRecordRepository.save(record);
}
// 获取学生的矫正记录
public List<CorrectionRecord> getRecordsByStudent(Long studentRecordId, String username) {
Consultant consultant = consultantRepository.findByUserUsername(username)
.orElseThrow(() -> new RuntimeException("咨询师不存在"));
StudentElectronicRecord studentRecord = studentRecordRepository.findById(studentRecordId)
.orElseThrow(() -> new RuntimeException("学生档案不存在"));
// 验证权限
if (!studentRecord.getConsultant().getId().equals(consultant.getId())) {
throw new RuntimeException("无权访问该学生档案");
}
return correctionRecordRepository.findByStudentRecordOrderBySessionDateDesc(studentRecord);
}
// 获取矫正记录详情
public CorrectionRecord getRecordDetail(Long correctionId, String username) {
Consultant consultant = consultantRepository.findByUserUsername(username)
.orElseThrow(() -> new RuntimeException("咨询师不存在"));
CorrectionRecord record = correctionRecordRepository.findById(correctionId)
.orElseThrow(() -> new RuntimeException("矫正记录不存在"));
// 验证权限
if (!record.getConsultant().getId().equals(consultant.getId())) {
throw new RuntimeException("无权访问该矫正记录");
}
return record;
}
// 更新矫正记录
@Transactional
public CorrectionRecord updateRecord(Long correctionId, String username, CorrectionRecordDTO correctionDTO) {
CorrectionRecord record = getRecordDetail(correctionId, username);
record.setSessionDate(correctionDTO.getSessionDate());
record.setSessionDuration(correctionDTO.getSessionDuration());
record.setInterventionMethods(correctionDTO.getInterventionMethods());
record.setSessionSummary(correctionDTO.getSessionSummary());
record.setProgressNotes(correctionDTO.getProgressNotes());
record.setNextSessionPlan(correctionDTO.getNextSessionPlan());
// 更新矫正指标
if (correctionDTO.getMetrics() != null) {
record.setMetrics(correctionDTO.getMetrics().stream()
.map(metricDTO -> {
CorrectionMetric metric = new CorrectionMetric();
metric.setMetricName(metricDTO.getMetricName());
metric.setMetricValue(metricDTO.getMetricValue());
metric.setMetricUnit(metricDTO.getMetricUnit());
return metric;
})
.collect(Collectors.toList()));
}
return correctionRecordRepository.save(record);
}
// 获取矫正分析结果
public CorrectionRecord getAnalysisResult(Long correctionId, String username) {
CorrectionRecord record = getRecordDetail(correctionId, username);
// 这里可以添加分析逻辑,比如:
// 1. 计算各项指标的改进情况
// 2. 生成进度报告
// 3. 提供干预效果评估
// 示例分析逻辑
if (record.getMetrics() != null && !record.getMetrics().isEmpty()) {
// 计算平均改进值
double avgImprovement = record.getMetrics().stream()
.mapToDouble(metric -> metric.getImprovement() != null ? metric.getImprovement() : 0.0)
.average()
.orElse(0.0);
// 这里可以将分析结果添加到记录中或返回单独的分析对象
// 目前先返回原始记录,实际项目中可以扩展分析功能
}
return record;
}
// 获取咨询师的所有矫正记录
public List<CorrectionRecord> getRecordsByConsultant(String username) {
Consultant consultant = consultantRepository.findByUserUsername(username)
.orElseThrow(() -> new RuntimeException("咨询师不存在"));
return correctionRecordRepository.findByConsultant(consultant);
}
// 获取时间范围内的矫正记录
public List<CorrectionRecord> getRecordsByDateRange(String username, LocalDateTime start, LocalDateTime end) {
Consultant consultant = consultantRepository.findByUserUsername(username)
.orElseThrow(() -> new RuntimeException("咨询师不存在"));
return correctionRecordRepository.findByConsultantAndSessionDateBetween(consultant, start, end);
}
// 删除矫正记录
@Transactional
public void deleteRecord(Long correctionId, String username) {
CorrectionRecord record = getRecordDetail(correctionId, username);
correctionRecordRepository.delete(record);
}
// 在 CorrectionRecordService.java 中修复这个方法:
// 获取最新的矫正记录
public Optional<CorrectionRecord> getLatestRecordByConsultant(String username) {
Consultant consultant = consultantRepository.findByUserUsername(username)
.orElseThrow(() -> new RuntimeException("咨询师不存在"));
return correctionRecordRepository.findLatestByConsultantId(consultant.getId());
}
}


浙公网安备 33010602011771号