@Override
public BaseResponse transferStuPlan(GxyPlanTeacherStudentEntity teacherStudentEntity) {
boolean stuIHaveJob = false;
List<GxyPlanTeacherStudentEntity> deleteStu = new ArrayList<>();
for(String stuId:teacherStudentEntity.getStuIds()){
GxyPlanTeacherStudentEntity teacherStudentDto = planTeacherStudentMapper.selectPlanIdAndStuId(teacherStudentEntity.getPlanId(),stuId,teacherStudentEntity.getSnowFlakeId());
Integer countReport = reportService.countReport(reportEntity);
if(countReport != null && countReport>0){
stuIHaveJob = true;
break;
}
planTeacherStudentMapper.delete(new QueryWrapper<GxyPlanTeacherStudentDto>().eq("plan_teacher_student_id",teacherStudentDto.getPlanTeacherStudentId()).eq("snow_flake_id",teacherStudentEntity.getSnowFlakeId()));
deleteStu.add(teacherStudentDto);
}
if(stuIHaveJob){
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
if(teacherStudentEntity.getStuIds().size()>1){
return BaseResponse.error("無法刪除");
}
}else{
updatePlanIdIntoNewPlan(deleteStu,teacherStudentEntity.getNewPlanId(),teacherStudentEntity.getSnowFlakeId());
}
return BaseResponse.ok();
}
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); 手動回滾。
當 stuIHaveJob 為true 時,上面的 planTeacherStudentMapper.delete 會回滾。
當 stuIHaveJob 為false 時,上面的 planTeacherStudentMapper.delete 不會回滾,繼續執行 updatePlanIdIntoNewPlan。(當此方法中有異常時,也會回滾)