Search in sources :

Example 1 with ResourceMapper

use of com.cas.sim.tis.mapper.ResourceMapper in project TeachingInSimulation by ScOrPiOzzy.

the class ResourceServiceImpl method findResourceInfoByID.

@Override
public ResourceInfo findResourceInfoByID(int id) {
    ResourceMapper resourceMapper = (ResourceMapper) mapper;
    ResourceInfo result = resourceMapper.selectResourceInfoByID(id);
    return result;
}
Also used : ResourceInfo(com.cas.sim.tis.vo.ResourceInfo) ResourceMapper(com.cas.sim.tis.mapper.ResourceMapper)

Example 2 with ResourceMapper

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

Example 3 with ResourceMapper

use of com.cas.sim.tis.mapper.ResourceMapper in project TeachingInSimulation by ScOrPiOzzy.

the class ResourceServiceImpl method collected.

@Override
public void collected(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.increaseCollection(id);
        // 新增用户收藏记录
        Collection collection = new Collection();
        collection.setResourceId(id);
        collection.setCreator(userId);
        collectionService.save(collection);
        transactionManager.commit(status);
    } catch (Exception e) {
        e.printStackTrace();
        transactionManager.rollback(status);
    }
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) ResourceMapper(com.cas.sim.tis.mapper.ResourceMapper) TransactionStatus(org.springframework.transaction.TransactionStatus) Collection(com.cas.sim.tis.entity.Collection) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager)

Example 4 with ResourceMapper

use of com.cas.sim.tis.mapper.ResourceMapper in project TeachingInSimulation by ScOrPiOzzy.

the class ResourceServiceImpl method uncollect.

@Override
public void uncollect(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.decreaseCollection(id);
        // 修改用户收藏记录
        Condition condition = new Condition(Collection.class);
        Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("creator", userId);
        criteria.andEqualTo("resourceId", id);
        criteria.andEqualTo("del", 0);
        List<Collection> collections = collectionService.findByCondition(condition);
        for (Collection collection : collections) {
            collection.setDel(1);
            collectionService.update(collection);
        }
        transactionManager.commit(status);
    } catch (Exception e) {
        e.printStackTrace();
        transactionManager.rollback(status);
    }
}
Also used : Condition(tk.mybatis.mapper.entity.Condition) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) ResourceMapper(com.cas.sim.tis.mapper.ResourceMapper) TransactionStatus(org.springframework.transaction.TransactionStatus) Collection(com.cas.sim.tis.entity.Collection) Criteria(tk.mybatis.mapper.entity.Example.Criteria) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager)

Example 5 with ResourceMapper

use of com.cas.sim.tis.mapper.ResourceMapper in project TeachingInSimulation by ScOrPiOzzy.

the class ResourceServiceImpl method findResourcesByBrowseHistory.

@Override
public PageInfo<Resource> findResourcesByBrowseHistory(int pagination, int pageSize, List<Integer> resourceTypes, String keyword, String orderByClause, Integer creator) {
    ResourceMapper resourceMapper = (ResourceMapper) mapper;
    // 开始分页查询
    PageHelper.startPage(pagination, pageSize, orderByClause);
    List<Resource> result = resourceMapper.findResourcesByBrowseHistory(resourceTypes, keyword, creator);
    PageInfo<Resource> page = new PageInfo<Resource>(result);
    // 查到的总记录数
    // 解释一下:这个page.getTotal(),是所有符合条件的记录数。
    // result.size():是当前页中的数据量 <= pageSize
    LOG.info("成功查找到{}条资源,当前页码{},每页{}条资源,共{}页", result.size(), pagination, pageSize, page.getPages());
    return page;
}
Also used : PageInfo(com.github.pagehelper.PageInfo) ResourceMapper(com.cas.sim.tis.mapper.ResourceMapper) Resource(com.cas.sim.tis.entity.Resource)

Aggregations

ResourceMapper (com.cas.sim.tis.mapper.ResourceMapper)6 DataSourceTransactionManager (org.springframework.jdbc.datasource.DataSourceTransactionManager)3 TransactionStatus (org.springframework.transaction.TransactionStatus)3 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)3 Collection (com.cas.sim.tis.entity.Collection)2 Resource (com.cas.sim.tis.entity.Resource)2 PageInfo (com.github.pagehelper.PageInfo)2 Condition (tk.mybatis.mapper.entity.Condition)2 Criteria (tk.mybatis.mapper.entity.Example.Criteria)2 BrowseHistory (com.cas.sim.tis.entity.BrowseHistory)1 ResourceInfo (com.cas.sim.tis.vo.ResourceInfo)1