Search in sources :

Example 1 with LibraryRecordMapper

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);
    }
}
Also used : Condition(tk.mybatis.mapper.entity.Condition) LibraryAnswer(com.cas.sim.tis.entity.LibraryAnswer) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) LibraryRecordMapper(com.cas.sim.tis.mapper.LibraryRecordMapper) TransactionStatus(org.springframework.transaction.TransactionStatus) Criteria(tk.mybatis.mapper.entity.Example.Criteria) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager) LibraryPublish(com.cas.sim.tis.entity.LibraryPublish)

Aggregations

LibraryAnswer (com.cas.sim.tis.entity.LibraryAnswer)1 LibraryPublish (com.cas.sim.tis.entity.LibraryPublish)1 LibraryRecordMapper (com.cas.sim.tis.mapper.LibraryRecordMapper)1 DataSourceTransactionManager (org.springframework.jdbc.datasource.DataSourceTransactionManager)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)1 Condition (tk.mybatis.mapper.entity.Condition)1 Criteria (tk.mybatis.mapper.entity.Example.Criteria)1