use of com.cas.sim.tis.mapper.LibraryRecordMapper in project TeachingInSimulation by ScOrPiOzzy.
the class LibraryRecordServiceImpl method addRecord.
@Override
public void addRecord(LibraryRecord record, List<LibraryAnswer> answers) {
// 1.获取事务控制管理器
DataSourceTransactionManager transactionManager = SpringUtil.getBean(DataSourceTransactionManager.class);
// 2.获取事务定义
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
// 3.设置事务隔离级别,开启新事务
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
// 4.获得事务状态
TransactionStatus status = transactionManager.getTransaction(def);
try {
saveUseGeneratedKeys(record);
int recordId = record.getId();
for (LibraryAnswer answer : answers) {
answer.setRecordId(recordId);
}
answerService.save(answers);
LibraryPublish publish = publishService.findById(record.getPublishId());
// 获得班级总人数
if (publish.getClassId() == null) {
publish.setAverage(MathUtil.round(2, record.getScore()));
} else {
Condition userCon = new Condition(User.class);
Criteria criteria = userCon.createCriteria();
criteria.andEqualTo("classId", publish.getClassId());
criteria.andEqualTo("del", 0);
int total = userService.getTotalBy(userCon);
// 获得已交卷成绩总和
LibraryRecordMapper recordMapper = (LibraryRecordMapper) mapper;
float sum = recordMapper.getRecordScoresSumByPublishId(publish.getId());
publish.setAverage(MathUtil.round(2, sum / total));
}
// 更新平均成绩
publishService.update(publish);
transactionManager.commit(status);
} catch (Exception e) {
e.printStackTrace();
transactionManager.rollback(status);
}
}
Aggregations