This commit is contained in:
oneao committed 2025-12-26 17:30:43 +08:00
1 parent 4f5218c892
commit 2f8a815c62
32 files changed
+1915 -1738

No files matched your search

@@ -35,5 +35,4 @@ public class CompanyDetailVO {
private String position;
private String name;
}
}
@@ -0,0 +1,40 @@
package cn.link56.post.controller;
import cn.link56.common.Result;
import cn.link56.post.model.dto.CreatePostCommentDTO;
import cn.link56.post.model.vo.CreatePostCommentVO;
import cn.link56.post.model.vo.PostCommentVO;
import cn.link56.post.service.PostCommentService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
@RestController
@RequestMapping("/post/comment")
public class PostCommentController {
@Resource
private PostCommentService postCommentService;
@PostMapping("/create")
public Result<CreatePostCommentVO> createPostComment(@RequestBody CreatePostCommentDTO dto){
CreatePostCommentVO commentVO = postCommentService.createPostComment(dto);
return Result.success(commentVO);
}
@GetMapping("/{id}")
public Result<List<PostCommentVO>> listPostComments(@PathVariable("id") @NotNull(message = "文章ID不能为空") Long id){
List<PostCommentVO> postComments = postCommentService.listPostComments(id);
return Result.success(postComments);
}
@PostMapping("/delete/{id}")
public Result<Void> delete(@PathVariable("id") @NotNull(message = "评论ID不能为空") Long postCommentId) {
postCommentService.deletePostComment(postCommentId);
return Result.success();
}
}
@@ -3,13 +3,13 @@ package cn.link56.post.controller;
import cn.link56.common.BasePageVO;
import cn.link56.common.Result;
import cn.link56.post.model.dto.QueryPublicPostDTO;
import cn.link56.post.model.vo.PostTagVO;
import cn.link56.post.model.vo.QueryPublicPostVO;
import cn.link56.post.model.vo.*;
import cn.link56.post.service.PostService;
import cn.link56.post.service.PostTagService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
@RestController
@@ -32,4 +32,19 @@ public class PostController {
BasePageVO<QueryPublicPostVO> res = postService.queryPublicPosts(dto);
return Result.success(res);
}
@GetMapping("/{id}")
public Result<PublicPostVO> getPublicDetail(@PathVariable("id") @NotNull(message = "文章ID不能为空") Long id){
PublicPostVO publicPostVO = postService.getPublicDetail(id);
return Result.success(publicPostVO);
}
@GetMapping("/company/{id}")
public Result<PostCompanyVO> getForumCompany(@PathVariable("id") @NotNull(message = "公司ID不能为空") Long companyId){
PostCompanyVO postCompanyVO = postService.getPostCompanyById(companyId);
return Result.success(postCompanyVO);
}
}
@@ -1,9 +1,14 @@
package cn.link56.post.mapper;
import cn.link56.post.model.po.PostComment;
import cn.link56.post.model.vo.PostCommentVO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface PostCommentMapper extends BaseMapper<PostComment> {
List<PostCommentVO> selectComments(@Param("postId") Long postId);
}
@@ -2,7 +2,9 @@ package cn.link56.post.mapper;
import cn.link56.post.model.po.Post;
import cn.link56.post.model.vo.EditorDashboardVO;
import cn.link56.post.model.vo.PublicPostVO;
import cn.link56.post.model.vo.QueryEditorPostVO;
import cn.link56.post.model.vo.QueryPublicPostVO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@@ -33,4 +35,16 @@ public interface PostMapper extends BaseMapper<Post> {
List<EditorDashboardVO.Chart> selectDashboardViewStats(@Param("userId") Long userId);
List<EditorDashboardVO.Chart> selectDashboardNumberStats(@Param("userId") Long userId);
List<QueryPublicPostVO> selectPublicPostPage(
@Param("offset") int offset,
@Param("limit") int limit,
@Param("title") String title,
@Param("tag") String tag
);
Long selectPublicPostTotal(
@Param("title") String title,
@Param("tag") String tag
);
}
@@ -0,0 +1,15 @@
package cn.link56.post.model.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CreatePostCommentDTO {
private Long fatherId;
private Long postId;
private String content;
}
@@ -8,6 +8,9 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
public class QueryPublicPostDTO {
private Integer currentPage;
private Integer pageSize;
private String title;
private String tag;
}
@@ -8,6 +8,8 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
@@ -26,14 +28,14 @@ public class PostComment {
private String content;
@TableField(
value = "b_create_user_id",
value = "b_create_by",
fill = FieldFill.INSERT
)
private Long createUserId;
private Long createBy;
@TableField(
value = "b_create_time",
fill = FieldFill.INSERT
)
private Long createTime;
private Date createTime;
}
@@ -0,0 +1,18 @@
package cn.link56.post.model.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CreatePostCommentVO {
private Long id;
private Long fatherId;
private String content;
private Long createId;
private Date createTime;
}
@@ -0,0 +1,31 @@
package cn.link56.post.model.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PostCommentVO {
private Long id;
private Long fatherId;
private String content;
private Date createTime;
private User publishUser;
private User replyUser;
private List<PostCommentVO> children;
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class User{
private Long id;
private Long companyId;
private String name;
private String avatar;
}
}
@@ -0,0 +1,19 @@
package cn.link56.post.model.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PostCompanyVO {
private Long id;
private String name;
private String logo;
private String profile;
private List<String> areas;
private List<String> industries;
}
@@ -0,0 +1,24 @@
package cn.link56.post.model.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PublicPostVO {
private Long id;
private String title;
private String content;
private List<String> tags;
private Date activeTime;
private Long viewCount;
private Long commentCount;
private Long companyId;
}
@@ -1,14 +1,133 @@
package cn.link56.post.service;
import cn.link56.common.utils.snowflake.idgen.IdGenerator;
import cn.link56.post.mapper.PostCommentMapper;
import cn.link56.post.model.dto.CreatePostCommentDTO;
import cn.link56.post.model.po.PostComment;
import cn.link56.post.model.po.PostView;
import cn.link56.post.model.vo.CreatePostCommentVO;
import cn.link56.post.model.vo.PostCommentVO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.*;
@Service
public class PostCommentService extends ServiceImpl<PostCommentMapper, PostComment> {
@Resource
private PostCommentMapper postCommentMapper;
/**
* 根据文章 id 获取浏览总数
*/
public Long getTotalByPostId(Long postId) {
return lambdaQuery()
.eq(PostComment::getPostId, postId)
.count();
}
/**
* 根据文章 id 获取最新一条评论
*/
public PostComment getLatestCommentByPostId(Long postId) {
return null;
}
public List<PostCommentVO> listPostComments(Long id) {
// 1. 查询所有评论
List<PostCommentVO> list = postCommentMapper.selectComments(id);
// 2. 构建扁平化树形结构(所有子评论提升到根评论层级)
List<PostCommentVO> rootComments = new ArrayList<>();
Map<Long, PostCommentVO> commentMap = new HashMap<>();
// 2.1 初始化映射并收集根评论
for (PostCommentVO comment : list) {
commentMap.put(comment.getId(), comment);
if (ObjectUtils.isEmpty(comment.getFatherId())) {
rootComments.add(comment);
comment.setChildren(new ArrayList<>()); // 提前初始化children
}
}
// 2.2 遍历所有评论,将子评论挂到根祖先的children中
for (PostCommentVO comment : list) {
if (ObjectUtils.isEmpty(comment.getFatherId())) {
continue; // 跳过根评论
}
// 查找当前评论的根祖先(追溯到没有fatherId的祖先)
PostCommentVO rootAncestor = null;
Long currentFatherId = comment.getFatherId();
while (true) {
PostCommentVO parent = commentMap.get(currentFatherId);
if (parent == null) break; // 父节点不存在,终止循环
if (ObjectUtils.isEmpty(comment.getFatherId())) {
rootAncestor = parent; // 找到根祖先
break;
} else {
currentFatherId = parent.getFatherId(); // 继续向上查找
}
}
// 挂载到根祖先的children中
if (rootAncestor != null && rootAncestor.getChildren() != null) {
rootAncestor.getChildren().add(comment);
}
}
// 3. 按原始顺序排序(可选)
rootComments.forEach(root -> {
if (root.getChildren() != null) {
// 根据需求排序,例如按创建时间升序
root.getChildren().sort(Comparator.comparing(PostCommentVO::getCreateTime));
}
});
return rootComments;
}
@Transactional
public CreatePostCommentVO createPostComment(CreatePostCommentDTO dto) {
PostComment postComment = new PostComment();
BeanUtils.copyProperties(dto,postComment);
postComment.setId(IdGenerator.nextId());
postCommentMapper.insert(postComment);
CreatePostCommentVO commentVO = new CreatePostCommentVO();
BeanUtils.copyProperties(postComment,commentVO);
return commentVO;
}
@Transactional
public void deletePostComment(@NotBlank(message = "评论ID不能为空") Long postCommentId) {
PostComment postComment = postCommentMapper.selectById(postCommentId);
if (ObjectUtils.isEmpty(postComment)){
return;
}
Long fatherId = postComment.getFatherId();
if(ObjectUtils.isEmpty(fatherId)){
postCommentMapper.deleteById(postCommentId);
}else{
LambdaUpdateWrapper<PostComment> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(PostComment::getId,postCommentId)
.or()
.eq(PostComment::getFatherId,postCommentId);
postCommentMapper.delete(updateWrapper);
}
}
}
@@ -2,24 +2,27 @@ package cn.link56.post.service;
import cn.link56.common.BasePageVO;
import cn.link56.common.context.UserContext;
import cn.link56.common.exception.BusinessException;
import cn.link56.common.utils.snowflake.idgen.IdGenerator;
import cn.link56.company.model.po.CompanyPO;
import cn.link56.company.service.CompanyService;
import cn.link56.post.mapper.PostMapper;
import cn.link56.post.model.dto.*;
import cn.link56.post.model.enums.PostStatusEnum;
import cn.link56.post.model.po.Post;
import cn.link56.post.model.vo.EditorDashboardVO;
import cn.link56.post.model.vo.EditorPostVO;
import cn.link56.post.model.vo.QueryEditorPostVO;
import cn.link56.post.model.vo.QueryPublicPostVO;
import cn.link56.post.model.po.PostComment;
import cn.link56.post.model.vo.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
@@ -29,6 +32,12 @@ public class PostService extends ServiceImpl<PostMapper, Post> {
@Resource
private PostMapper postMapper;
@Resource
private PostViewService postViewService;
@Resource
private PostCommentService postCommentService;
@Resource
private CompanyService companyService;
@Transactional
public EditorPostVO createPost(CreatePostDTO dto) {
@@ -219,10 +228,63 @@ public class PostService extends ServiceImpl<PostMapper, Post> {
public BasePageVO<QueryPublicPostVO> queryPublicPosts(QueryPublicPostDTO dto) {
String title = dto.getTitle();
String tag = dto.getTag();
int offset = (dto.getCurrentPage() - 1) * dto.getPageSize() + 1;
int limit = dto.getCurrentPage() * dto.getPageSize();
BasePageVO<QueryPublicPostVO> result = new BasePageVO<>();
return result;
List<QueryPublicPostVO> queryEditorPostVOS = postMapper.selectPublicPostPage(offset, limit, title, tag);
Long total = postMapper.selectPublicPostTotal(title, tag);
return new BasePageVO<QueryPublicPostVO>(total, queryEditorPostVOS);
}
public PublicPostVO getPublicDetail(Long id) {
Post post = postMapper.selectById(id);
if (post == null) {
throw new BusinessException("帖子不存在或已被删除");
}
if (PostStatusEnum.DRAFT.getCode() == post.getStatus()) {
throw new BusinessException("该帖子尚未发布");
}
PublicPostVO postVO = new PublicPostVO();
BeanUtils.copyProperties(post,postVO);
postVO.setTags(Arrays.stream(StringUtils.split(post.getTag(),",")).collect(Collectors.toList()));
Long viewTotal = postViewService.getTotalByPostId(id);
Long commentTotal = postCommentService.getTotalByPostId(id);
postVO.setViewCount(viewTotal);
postVO.setCommentCount(commentTotal);
PostComment latestCommentByPostId = postCommentService.getLatestCommentByPostId(id);
if (latestCommentByPostId == null){
postVO.setActiveTime(post.getUpdateTime());
} else {
postVO.setActiveTime(latestCommentByPostId.getCreateTime());
}
return postVO;
}
public PostCompanyVO getPostCompanyById(Long id) {
CompanyPO companyPO = companyService.getById(id);
if (companyPO == null){
throw new BusinessException("该公司不存在或已注销");
}
PostCompanyVO postCompanyVO = new PostCompanyVO();
BeanUtils.copyProperties(companyPO,postCompanyVO);
postCompanyVO.setAreas(Arrays.stream(StringUtils.split(companyPO.getArea(),",")).collect(Collectors.toList()));
postCompanyVO.setIndustries(Arrays.stream(StringUtils.split(companyPO.getIndustry(),",")).collect(Collectors.toList()));
return postCompanyVO;
}
}
@@ -11,4 +11,14 @@ import javax.annotation.Resource;
public class PostViewService extends ServiceImpl<PostViewMapper, PostView> {
@Resource
private PostViewMapper postViewMapper;
/**
* 根据文章 id 获取浏览总数
*/
public Long getTotalByPostId(Long postId) {
return lambdaQuery()
.eq(PostView::getPostId, postId)
.count();
}
}
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.link56.post.mapper.PostCommentMapper">
<resultMap id="CommentListMap" type="cn.link56.post.model.vo.PostCommentVO">
<id property="id" column="b_id"/>
<result property="fatherId" column="b_father_id"/>
<result property="content" column="b_content"/>
<result property="createTime" column="b_create_time"/>
<!-- 映射发布公司 -->
<association property="publishUser" javaType="cn.link56.post.model.vo.PostCommentVO$User">
<result property="id" column="pub_user_id"/>
<result property="companyId" column="pub_company_id"/>
<result property="name" column="pub_user_name"/>
<result property="avatar" column="pub_user_avatar"/>
</association>
<!-- 映射回复公司 -->
<association property="replyUser" javaType="cn.link56.post.model.vo.PostCommentVO$User">
<result property="id" column="rep_user_id"/>
<result property="companyId" column="rep_company_id"/>
<result property="name" column="rep_user_name"/>
<result property="avatar" column="rep_user_avatar"/>
</association>
</resultMap>
<select id="selectComments" resultMap="CommentListMap">
SELECT c.*,
pc.b_id AS pub_user_id,pc.b_company_id AS pub_company_id,pc.b_name AS pub_user_name,
rc.b_id AS rep_user_id,rc.b_company_id AS rep_company_id,rc.b_name AS rep_company_id
FROM b_post_comment c
LEFT JOIN b_company_user pc ON c.b_create_by = pc.b_id
LEFT JOIN b_company_user rc ON (
SELECT b_create_by FROM b_post_comment WHERE b_id = c.b_father_id
) = rc.b_id
WHERE c.b_post_id = #{postId}
ORDER BY c.b_create_time
</select>
</mapper>
@@ -163,7 +163,7 @@
p.b_id AS id,
p.b_title AS title,
p.b_tag AS tagsString,
p.company_id AS companyId,
p.b_company_id AS companyId,
bc.b_name AS companyName,
ISNULL(pv.viewCount, 0) AS viewCount,
@@ -173,10 +173,10 @@
ISNULL(pc.lastCommentTime, p.b_update_time) AS activeTime,
ROW_NUMBER() OVER (
ORDER BY ISNULL(pc.lastCommentTime, p.b_update_time) DESC
ORDER BY ISNULL(pc.lastCommentTime, p.b_update_time) DESC
) AS rn
FROM b_post p
LEFT JOIN b_company bc ON bc.b_id = p.company_id
LEFT JOIN b_company bc ON bc.b_id = p.b_company_id
LEFT JOIN (
SELECT
b_post_id,
@@ -193,10 +193,11 @@
GROUP BY b_post_id
) pc ON pc.b_post_id = p.b_id
<where>
AND p.b_status = 1
<if test="title != null and title != ''">
AND p.b_title LIKE '%' + #{title} + '%'
</if>
<if test="tag != null and tag != -1">
<if test="tag != null and tag != ''">
AND p.b_tag LIKE '%' + #{tag} + '%'
</if>
</where>
@@ -204,4 +205,19 @@
WHERE t.rn BETWEEN #{offset} AND #{limit}
</select>
<select id="selectPublicPostTotal" resultType="java.lang.Long">
SELECT COUNT(1)
FROM b_post p
<where>
AND p.b_status = 1
<if test="title != null and title != ''">
AND p.b_title LIKE '%' + #{title} + '%'
</if>
<if test="tag != null and tag != ''">
AND p.b_tag LIKE '%' + #{tag} + '%'
</if>
</where>
</select>
</mapper>