serv-学习
This commit is contained in:
@@ -124,7 +124,7 @@ public class ResourceController {
|
||||
* 增加浏览次数
|
||||
*/
|
||||
@PostMapping("/resource/{resourceID}/view")
|
||||
public ResultDomain<TbResource> incrementViewCount(@PathVariable("resourceID") String resourceID) {
|
||||
public ResultDomain<Boolean> incrementViewCount(@PathVariable("resourceID") String resourceID) {
|
||||
return resourceService.incrementViewCount(resourceID);
|
||||
}
|
||||
|
||||
|
||||
@@ -173,4 +173,13 @@ public interface ResourceMapper extends BaseMapper<TbResource> {
|
||||
* @since 2025-10-15
|
||||
*/
|
||||
int updateResourceCollectCount(@Param("resourceID") String resourceID, @Param("collectionValue") Integer collectionValue);
|
||||
|
||||
/**
|
||||
* @description 增加资源浏览次数
|
||||
* @param resourceID 资源ID
|
||||
* @return int 影响行数
|
||||
* @author yslg
|
||||
* @since 2025-10-15
|
||||
*/
|
||||
int incrementViewCount(@Param("resourceID") String resourceID);
|
||||
}
|
||||
|
||||
@@ -397,7 +397,7 @@ public class NCResourceServiceImpl implements ResourceService {
|
||||
if (result > 0) {
|
||||
logger.info("发布资源成功: {}", resourceID);
|
||||
// 重新查询返回完整数据
|
||||
TbResource updated = resourceMapper.selectById(resource.getID());
|
||||
TbResource updated = resourceMapper.selectByResourceId(resource.getID());
|
||||
resultDomain.success("发布资源成功", updated);
|
||||
return resultDomain;
|
||||
} else {
|
||||
@@ -437,7 +437,7 @@ public class NCResourceServiceImpl implements ResourceService {
|
||||
if (result > 0) {
|
||||
logger.info("下架资源成功: {}", resourceID);
|
||||
// 重新查询返回完整数据
|
||||
TbResource updated = resourceMapper.selectById(resource.getID());
|
||||
TbResource updated = resourceMapper.selectByResourceId(resource.getID());
|
||||
resultDomain.success("下架资源成功", updated);
|
||||
return resultDomain;
|
||||
} else {
|
||||
@@ -453,33 +453,20 @@ public class NCResourceServiceImpl implements ResourceService {
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public ResultDomain<TbResource> incrementViewCount(String resourceID) {
|
||||
ResultDomain<TbResource> resultDomain = new ResultDomain<>();
|
||||
public ResultDomain<Boolean> incrementViewCount(String resourceID) {
|
||||
ResultDomain<Boolean> resultDomain = new ResultDomain<>();
|
||||
try {
|
||||
// 参数验证
|
||||
// 参数验证
|
||||
if (!StringUtils.hasText(resourceID)) {
|
||||
resultDomain.fail("资源ID不能为空");
|
||||
return resultDomain;
|
||||
}
|
||||
|
||||
// 查询资源
|
||||
TbResource resource = resourceMapper.selectByResourceId(resourceID);
|
||||
if (resource == null || resource.getDeleted()) {
|
||||
resultDomain.fail("资源不存在");
|
||||
return resultDomain;
|
||||
}
|
||||
|
||||
// 增加浏览次数
|
||||
Integer currentCount = resource.getViewCount() != null ? resource.getViewCount() : 0;
|
||||
resource.setViewCount(currentCount + 1);
|
||||
resource.setUpdateTime(new Date());
|
||||
|
||||
int result = resourceMapper.updateResource(resource);
|
||||
int result = resourceMapper.incrementViewCount(resourceID);
|
||||
if (result > 0) {
|
||||
logger.info("增加资源浏览次数成功: {}", resourceID);
|
||||
// 重新查询返回完整数据
|
||||
TbResource updated = resourceMapper.selectById(resource.getID());
|
||||
resultDomain.success("增加浏览次数成功", updated);
|
||||
resultDomain.success("增加浏览次数成功", true);
|
||||
return resultDomain;
|
||||
} else {
|
||||
resultDomain.fail("增加浏览次数失败");
|
||||
@@ -563,7 +550,7 @@ public class NCResourceServiceImpl implements ResourceService {
|
||||
return resultDomain;
|
||||
}
|
||||
else if (isCollected.isSuccess() && isCollected.getData() && collectionValue == -1) {
|
||||
ResultDomain<Boolean> removeCollection = userCollectionService.removeCollection(user.getID(), collection.getCollectionType(), resourceID);
|
||||
ResultDomain<Boolean> removeCollection = userCollectionService.removeCollection(collection);
|
||||
if (removeCollection.isSuccess()) {
|
||||
resourceMapper.updateResourceCollectCount(resourceID, -1);
|
||||
resultDomain.success("已取消收藏", resource);
|
||||
|
||||
@@ -310,4 +310,12 @@
|
||||
SET collect_count = collect_count + #{collectionValue}
|
||||
WHERE resource_id = #{resourceID}
|
||||
</update>
|
||||
|
||||
<!-- incrementViewCount -->
|
||||
|
||||
<update id="incrementViewCount">
|
||||
UPDATE tb_resource
|
||||
SET view_count = view_count + 1
|
||||
WHERE resource_id = #{resourceID}
|
||||
</update>
|
||||
</mapper>
|
||||
|
||||
Reference in New Issue
Block a user