u
This commit is contained in:
1 parent
9bf7c40b70
commit
9eb5639d84
24 files changed
+915
-98
No files matched your search
+54
@@ -0,0 +1,54 @@
|
||||
package cn.link56.common.mybatis;
|
||||
|
||||
import org.apache.ibatis.type.BaseTypeHandler;
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
import org.apache.ibatis.type.MappedJdbcTypes;
|
||||
import org.apache.ibatis.type.MappedTypes;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@MappedTypes(List.class)
|
||||
@MappedJdbcTypes(JdbcType.VARCHAR)
|
||||
public class StringListTypeHandler extends BaseTypeHandler<List<String>> {
|
||||
|
||||
@Override
|
||||
public void setNonNullParameter(
|
||||
PreparedStatement ps,
|
||||
int i,
|
||||
List<String> parameter,
|
||||
JdbcType jdbcType
|
||||
) throws SQLException {
|
||||
ps.setString(i, String.join(",", parameter));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getNullableResult(ResultSet rs, String columnName)
|
||||
throws SQLException {
|
||||
return parse(rs.getString(columnName));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getNullableResult(ResultSet rs, int columnIndex)
|
||||
throws SQLException {
|
||||
return parse(rs.getString(columnIndex));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getNullableResult(CallableStatement cs, int columnIndex)
|
||||
throws SQLException {
|
||||
return parse(cs.getString(columnIndex));
|
||||
}
|
||||
|
||||
private List<String> parse(String value) {
|
||||
if (value == null || value.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return Arrays.asList(value.split(","));
|
||||
}
|
||||
}
|
||||
+21
-8
@@ -1,16 +1,17 @@
|
||||
package cn.link56.post.controller;
|
||||
|
||||
import cn.link56.common.BasePageVO;
|
||||
import cn.link56.common.Result;
|
||||
import cn.link56.post.model.dto.PostAddDTO;
|
||||
import cn.link56.post.model.vo.PostEditVO;
|
||||
import cn.link56.post.model.vo.PostTagVO;
|
||||
import cn.link56.post.model.dto.AddPostDTO;
|
||||
import cn.link56.post.model.dto.QueryPostDTO;
|
||||
import cn.link56.post.model.dto.UpdatePostDTO;
|
||||
import cn.link56.post.model.vo.EditPostVO;
|
||||
import cn.link56.post.model.vo.QueryPostVO;
|
||||
import cn.link56.post.service.PostService;
|
||||
import cn.link56.post.service.PostTagService;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/post")
|
||||
@@ -20,8 +21,20 @@ public class PostController {
|
||||
private PostService postService;
|
||||
|
||||
@PostMapping("/add")
|
||||
public Result<PostEditVO> addPost(@RequestBody @Validated PostAddDTO postAddDTO) {
|
||||
PostEditVO postEditVO = postService.addPost(postAddDTO);
|
||||
return Result.success(postEditVO);
|
||||
public Result<EditPostVO> addPost(@RequestBody @Validated AddPostDTO addPostDTO) {
|
||||
EditPostVO editPostVO = postService.addPost(addPostDTO);
|
||||
return Result.success(editPostVO);
|
||||
}
|
||||
|
||||
@PostMapping("/update")
|
||||
public Result<EditPostVO> updatePost(@RequestBody @Validated UpdatePostDTO updatePostDTO){
|
||||
EditPostVO editPostVO = postService.updatePost(updatePostDTO);
|
||||
return Result.success(editPostVO);
|
||||
}
|
||||
|
||||
@PostMapping("/query")
|
||||
public Result<BasePageVO<QueryPostVO>> queryPost(@RequestBody QueryPostDTO queryPostDTO){
|
||||
BasePageVO<QueryPostVO> data = postService.queryPost(queryPostDTO);
|
||||
return Result.success(data);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,31 @@
|
||||
package cn.link56.post.mapper;
|
||||
|
||||
import cn.link56.post.model.po.PostPO;
|
||||
import cn.link56.post.model.vo.QueryPostVO;
|
||||
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 PostMapper extends BaseMapper<PostPO> {
|
||||
|
||||
List<QueryPostVO> queryPostPage(
|
||||
@Param("offset") int offset,
|
||||
@Param("limit") int limit,
|
||||
@Param("userId") Long userId,
|
||||
@Param("title") String title,
|
||||
@Param("status") Integer status,
|
||||
@Param("year") String year,
|
||||
@Param("month") String month
|
||||
);
|
||||
|
||||
Long queryPostTotal(
|
||||
@Param("userId") Long userId,
|
||||
@Param("title") String title,
|
||||
@Param("status") Integer status,
|
||||
@Param("year") String year,
|
||||
@Param("month") String month
|
||||
);
|
||||
}
|
||||
+1
-1
@@ -10,7 +10,7 @@ import java.util.List;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class PostAddDTO {
|
||||
public class AddPostDTO {
|
||||
@NotBlank(message = "帖子标题不能为空")
|
||||
private String title;
|
||||
@NotBlank(message = "帖子内容不能为空")
|
||||
@@ -0,0 +1,19 @@
|
||||
package cn.link56.post.model.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class QueryPostDTO {
|
||||
private Integer currentPage;
|
||||
private Integer pageSize;
|
||||
|
||||
private Integer status;
|
||||
private String title;
|
||||
|
||||
private String year;
|
||||
private String month;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package cn.link56.post.model.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class UpdatePostDTO {
|
||||
@NotNull(message = "消息id不能为空")
|
||||
private Long id;
|
||||
@NotBlank(message = "帖子标题不能为空")
|
||||
private String title;
|
||||
@NotBlank(message = "帖子内容不能为空")
|
||||
private String content;
|
||||
@NotBlank(message = "帖子摘要不能为空")
|
||||
private String summary;
|
||||
private String cover;
|
||||
private List<String> tags;
|
||||
|
||||
private Integer status;
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package cn.link56.post.model.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum PostStatusEnum {
|
||||
DRAFT(0, "草稿"),
|
||||
PUBLISHED(1, "已发布"),
|
||||
OFFLINE(2, "已下架");
|
||||
|
||||
private final int code;
|
||||
private final String desc;
|
||||
}
|
||||
@@ -39,7 +39,7 @@ public class PostPO {
|
||||
private String cover;
|
||||
|
||||
@TableField("b_publish_time")
|
||||
private String publishTime;
|
||||
private Date publishTime;
|
||||
|
||||
@TableField("b_status")
|
||||
private Integer status;
|
||||
|
||||
+1
-2
@@ -1,6 +1,5 @@
|
||||
package cn.link56.post.model.vo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
@@ -11,7 +10,7 @@ import java.util.List;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class PostEditVO {
|
||||
public class EditPostVO {
|
||||
private Long id;
|
||||
private String title;
|
||||
private String content;
|
||||
@@ -0,0 +1,28 @@
|
||||
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 QueryPostVO {
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
private String cover;
|
||||
private List<String> tags;
|
||||
|
||||
private Date updateTime;
|
||||
private Date publishTime;
|
||||
private Integer status;
|
||||
|
||||
private Integer viewCount;
|
||||
private Integer likeCount;
|
||||
private Integer favoriteCount;
|
||||
private Integer commentCount;
|
||||
}
|
||||
@@ -1,17 +1,31 @@
|
||||
package cn.link56.post.service;
|
||||
|
||||
import cn.link56.common.BasePageVO;
|
||||
import cn.link56.common.context.UserContext;
|
||||
import cn.link56.common.utils.snowflake.idgen.IdGenerator;
|
||||
import cn.link56.company.model.po.CompanyUserPO;
|
||||
import cn.link56.post.mapper.PostMapper;
|
||||
import cn.link56.post.model.dto.PostAddDTO;
|
||||
import cn.link56.post.model.dto.AddPostDTO;
|
||||
import cn.link56.post.model.dto.QueryPostDTO;
|
||||
import cn.link56.post.model.dto.UpdatePostDTO;
|
||||
import cn.link56.post.model.enums.PostStatusEnum;
|
||||
import cn.link56.post.model.po.PostPO;
|
||||
import cn.link56.post.model.vo.PostEditVO;
|
||||
import cn.link56.post.model.vo.EditPostVO;
|
||||
import cn.link56.post.model.vo.QueryPostVO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
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 java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class PostService extends ServiceImpl<PostMapper, PostPO> {
|
||||
@@ -19,21 +33,68 @@ public class PostService extends ServiceImpl<PostMapper, PostPO> {
|
||||
private PostMapper postMapper;
|
||||
|
||||
@Transactional
|
||||
public PostEditVO addPost(PostAddDTO postAddDTO) {
|
||||
public EditPostVO addPost(AddPostDTO addPostDTO) {
|
||||
PostPO postPO = new PostPO();
|
||||
|
||||
BeanUtils.copyProperties(postAddDTO, postPO);
|
||||
BeanUtils.copyProperties(addPostDTO, postPO);
|
||||
|
||||
postPO.setTag(StringUtils.join(postAddDTO.getTags(), ","));
|
||||
postPO.setTag(StringUtils.join(addPostDTO.getTags(), ","));
|
||||
postPO.setCompanyId(UserContext.getCompanyId());
|
||||
|
||||
if(addPostDTO.getStatus() == PostStatusEnum.PUBLISHED.getCode()){
|
||||
postPO.setPublishTime(new Date());
|
||||
}
|
||||
|
||||
postPO.setId(IdGenerator.nextId());
|
||||
postMapper.insert(postPO);
|
||||
|
||||
PostEditVO postEditVO = new PostEditVO();
|
||||
EditPostVO editPostVO = new EditPostVO();
|
||||
|
||||
BeanUtils.copyProperties(postPO, postEditVO);
|
||||
postEditVO.setTags(postAddDTO.getTags());
|
||||
BeanUtils.copyProperties(postPO, editPostVO);
|
||||
editPostVO.setTags(addPostDTO.getTags());
|
||||
|
||||
return postEditVO;
|
||||
return editPostVO;
|
||||
}
|
||||
|
||||
public BasePageVO<QueryPostVO> queryPost(QueryPostDTO queryPostDTO) {
|
||||
Integer currentPage = queryPostDTO.getCurrentPage();
|
||||
Integer pageSize = queryPostDTO.getPageSize();
|
||||
|
||||
String title = queryPostDTO.getTitle();
|
||||
Integer status = queryPostDTO.getStatus();
|
||||
String year = queryPostDTO.getYear();
|
||||
String month = queryPostDTO.getMonth();
|
||||
|
||||
int offset = (currentPage - 1) * pageSize + 1;
|
||||
int limit = currentPage * pageSize;
|
||||
|
||||
List<QueryPostVO> queryPostVOS = postMapper.queryPostPage(offset, limit, UserContext.getUserId(), title, status, year, month);
|
||||
Long total = postMapper.queryPostTotal(UserContext.getUserId(), title, status, year, month);
|
||||
|
||||
BasePageVO<QueryPostVO> result = new BasePageVO<>();
|
||||
result.setRecords(queryPostVOS);
|
||||
result.setTotal(total);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public EditPostVO updatePost(UpdatePostDTO updatePostDTO) {
|
||||
Long id = updatePostDTO.getId();
|
||||
|
||||
PostPO postPO = postMapper.selectById(id);
|
||||
|
||||
System.out.println(postPO);
|
||||
|
||||
BeanUtils.copyProperties(updatePostDTO,postPO);
|
||||
postPO.setTag(StringUtils.join(updatePostDTO.getTags(), ","));
|
||||
|
||||
postMapper.updateById(postPO);
|
||||
|
||||
EditPostVO editPostVO = new EditPostVO();
|
||||
|
||||
BeanUtils.copyProperties(postPO, editPostVO);
|
||||
editPostVO.setTags(updatePostDTO.getTags());
|
||||
|
||||
return editPostVO;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
<?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.PostMapper">
|
||||
|
||||
<resultMap id="QueryPostVOMap"
|
||||
type="cn.link56.post.model.vo.QueryPostVO">
|
||||
<id property="id" column="id"/>
|
||||
<result property="title" column="title"/>
|
||||
<result property="cover" column="cover"/>
|
||||
|
||||
<!-- tags 字符串 → List<String> -->
|
||||
<result
|
||||
property="tags"
|
||||
column="tagsString"
|
||||
typeHandler="cn.link56.common.mybatis.StringListTypeHandler"/>
|
||||
|
||||
<result property="publishTime" column="publishTime"/>
|
||||
<result property="updateTime" column="updateTime"/>
|
||||
<result property="status" column="status"/>
|
||||
|
||||
<result property="viewCount" column="viewCount"/>
|
||||
<result property="likeCount" column="likeCount"/>
|
||||
<result property="favoriteCount" column="favoriteCount"/>
|
||||
<result property="commentCount" column="commentCount"/>
|
||||
</resultMap>
|
||||
|
||||
<select id="queryPostPage" resultMap="QueryPostVOMap">
|
||||
SELECT *
|
||||
FROM (
|
||||
SELECT
|
||||
p.b_id AS id,
|
||||
p.b_title AS title,
|
||||
p.b_cover AS cover,
|
||||
p.b_tag AS tagsString,
|
||||
p.b_update_time AS updateTime,
|
||||
p.b_publish_time AS publishTime,
|
||||
p.b_status AS status,
|
||||
|
||||
(SELECT COUNT(*) FROM b_post_view v WHERE v.b_post_id = p.b_id) AS viewCount,
|
||||
(SELECT COUNT(*) FROM b_post_like l WHERE l.b_post_id = p.b_id) AS likeCount,
|
||||
(SELECT COUNT(*) FROM b_post_favorite f WHERE f.b_post_id = p.b_id) AS favoriteCount,
|
||||
(SELECT COUNT(*) FROM b_post_comment c WHERE c.b_post_id = p.b_id) AS commentCount,
|
||||
|
||||
ROW_NUMBER() OVER (ORDER BY p.b_update_time DESC) AS rn
|
||||
FROM b_post p
|
||||
<where>
|
||||
<if test="userId != null">
|
||||
AND p.b_create_by = #{userId}
|
||||
</if>
|
||||
|
||||
<if test="status != null and status != -1">
|
||||
AND p.b_status = #{status}
|
||||
</if>
|
||||
|
||||
<if test="title != null and title != ''">
|
||||
AND p.b_title LIKE '%' + #{title} + '%'
|
||||
</if>
|
||||
|
||||
<if test="year != null and year != ''">
|
||||
AND YEAR(p.b_update_time) = #{year}
|
||||
</if>
|
||||
|
||||
<if test="month != null and month != ''">
|
||||
AND MONTH(p.b_update_time) = #{month}
|
||||
</if>
|
||||
</where>
|
||||
) t
|
||||
WHERE t.rn BETWEEN #{offset} AND #{limit}
|
||||
</select>
|
||||
|
||||
<select id="queryPostTotal" resultType="java.lang.Long">
|
||||
SELECT COUNT(*)
|
||||
FROM b_post p
|
||||
<where>
|
||||
<if test="userId != null">
|
||||
AND p.b_create_by = #{userId}
|
||||
</if>
|
||||
|
||||
<if test="status != null and status != -1">
|
||||
AND p.b_status = #{status}
|
||||
</if>
|
||||
|
||||
<if test="title != null and title != ''">
|
||||
AND p.b_title LIKE '%' + #{title} + '%'
|
||||
</if>
|
||||
|
||||
<if test="year != null and year != ''">
|
||||
AND YEAR(p.b_update_time) = #{year}
|
||||
</if>
|
||||
|
||||
<if test="month != null and month != ''">
|
||||
AND MONTH(p.b_update_time) = #{month}
|
||||
</if>
|
||||
</where>
|
||||
</select>
|
||||
</mapper>
|
||||
Reference in new issue
Block a user