use of com.cas.sim.tis.entity.BrowseHistory in project TeachingInSimulation by ScOrPiOzzy.
the class ResourceServiceImpl method browsed.
@Override
public void browsed(Integer id, Integer userId) {
// 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 {
// 增加资源查看数量
ResourceMapper resourceMapper = (ResourceMapper) mapper;
resourceMapper.increaseBrowse(id);
Condition condition = new Condition(BrowseHistory.class);
condition.orderBy("createDate").asc();
Criteria criteria = condition.createCriteria();
criteria.andEqualTo("creator", userId);
List<BrowseHistory> histories = browseHistoryService.findByCondition(condition);
// 查看历史记录是否超过100条,注意这里是物理删除!!!
if (histories.size() >= 100) {
BrowseHistory history = histories.get(0);
browseHistoryService.deleteById(history.getId());
}
BrowseHistory history = new BrowseHistory();
history.setResourceId(id);
history.setCreator(userId);
browseHistoryService.save(history);
transactionManager.commit(status);
} catch (Exception e) {
e.printStackTrace();
transactionManager.rollback(status);
}
}
Aggregations