serv-学习

This commit is contained in:
2025-10-24 18:28:26 +08:00
parent 6f5603dd8b
commit 8968409b2d
55 changed files with 2144 additions and 2570 deletions

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);

View File

@@ -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>