|
@@ -0,0 +1,330 @@
|
|
|
+package com.dt.shequ.service.impl;
|
|
|
+
|
|
|
+
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.dt.shequ.domain.*;
|
|
|
+import com.dt.shequ.dto.*;
|
|
|
+import com.dt.shequ.mapper.*;
|
|
|
+import com.dt.shequ.service.PostsService;
|
|
|
+import com.dt.shequ.util.ResponseResult;
|
|
|
+import com.dt.shequ.util.Result;
|
|
|
+import com.dt.shequ.utils.TextModerationPlusDemo;
|
|
|
+import com.dt.user.mapper.UserMapper;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+* @author 86155
|
|
|
+* @description 针对表【posts(帖子表)】的数据库操作Service实现
|
|
|
+* @createDate 2025-07-02 19:15:07
|
|
|
+*/
|
|
|
+@Service
|
|
|
+public class PostsServiceImpl extends ServiceImpl<PostsMapper, Posts>
|
|
|
+ implements PostsService {
|
|
|
+ @Autowired
|
|
|
+ private PostsMapper postsMapper;
|
|
|
+ @Autowired
|
|
|
+ private PostMediaMapper postMediaMapper;
|
|
|
+ @Autowired
|
|
|
+ private TopicsMapper topicsMapper;
|
|
|
+ @Autowired
|
|
|
+ private UserMapper userMapper;
|
|
|
+ @Autowired
|
|
|
+ private UserInfoMapper userInfoMapper;
|
|
|
+ @Autowired
|
|
|
+ private UserFollowMapper userFollowMapper;
|
|
|
+ @Autowired
|
|
|
+ private PostCommentMapper postCommentMapper;
|
|
|
+ @Autowired
|
|
|
+ private TopicRepliesMapper topicRepliesMapper;
|
|
|
+ @Override
|
|
|
+ public Result getPosts(ParamsDto paramsDto) {
|
|
|
+ Page<Posts> page = new Page<>(paramsDto.getPageNum(), paramsDto.getPageSize());
|
|
|
+ QueryWrapper<Posts> queryWrapper = new QueryWrapper<>();
|
|
|
+ if (paramsDto.getCategory()!=null && paramsDto.getCategory().equals("问答")){
|
|
|
+ queryWrapper.eq("category",paramsDto.getCategory());
|
|
|
+ }
|
|
|
+ if (paramsDto.getCategory()!=null && paramsDto.getCategory().equals("活动")){
|
|
|
+ queryWrapper.eq("category",paramsDto.getCategory());
|
|
|
+ }
|
|
|
+ Page<Posts> postsPage = postsMapper.selectPage(page, queryWrapper);
|
|
|
+ for (Posts post : postsPage.getRecords()) {
|
|
|
+ com.dt.user.pojo.User user = userMapper.selectById(post.getUserId());
|
|
|
+// 根据用户id来获取姓名,图片等信息。
|
|
|
+ post.setUsername(user.getUsername());
|
|
|
+ post.setAvatar(user.getAvatar());
|
|
|
+// post.setAvatar("https://tse2-mm.cn.bing.net/th/id/OIP-C.5OkGLT2dXMg2bxzWGO7igAHaDq?w=320&h=173&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3");
|
|
|
+ List<PostMedia> postId = postMediaMapper.selectList(new QueryWrapper<PostMedia>().eq("post_id", post.getId()));
|
|
|
+ // 分类存储图片和视频URL
|
|
|
+ List<String> images = new ArrayList<>();
|
|
|
+ // 媒体类型:1-图片,2-视频(根据你的数据库定义调整)
|
|
|
+ for (PostMedia media : postId) {
|
|
|
+ images.add(media.getMediaUrl());
|
|
|
+ }
|
|
|
+ post.setImageList(images);
|
|
|
+ System.err.println(postsPage.getRecords());
|
|
|
+ }
|
|
|
+ return new Result(200, "success",postsPage.getRecords());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result getbYids(Long id) {
|
|
|
+ Posts posts = postsMapper.selectById(id);
|
|
|
+ // 分类存储图片和视频URL
|
|
|
+ List<String> images = new ArrayList<>();
|
|
|
+ com.dt.user.pojo.User user = userMapper.selectById(posts.getUserId());
|
|
|
+// 根据用户id来获取姓名,图片等信息。
|
|
|
+ posts.setUsername(user.getUsername());
|
|
|
+ posts.setAvatar(user.getAvatar());
|
|
|
+
|
|
|
+// posts.setUsername("宝妈玛丽");
|
|
|
+// posts.setAvatar("https://tse2-mm.cn.bing.net/th/id/OIP-C.5OkGLT2dXMg2bxzWGO7igAHaDq?w=320&h=173&c=7&r=0&o=7&dpr=1.5&pid=1.7&rm=3");
|
|
|
+ List<PostMedia> postId = postMediaMapper.selectList(new QueryWrapper<PostMedia>().eq("post_id", posts.getId()));
|
|
|
+ for (PostMedia media : postId) {
|
|
|
+ images.add(media.getMediaUrl());
|
|
|
+ }
|
|
|
+ posts.setImageList(images);
|
|
|
+ return new Result(200, "成功",posts);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public Result addposts(Posts posts) throws Exception {
|
|
|
+ // 直接调用MyBatis-Plus的save方法,会自动回填自增主键
|
|
|
+ String content = posts.getContent()+posts.getTitle();
|
|
|
+ ResponseResult responseResult = TextModerationPlusDemo.textModerationPlus(content);
|
|
|
+ if(responseResult.getCode()==200){
|
|
|
+ int saveResult = postsMapper.insert(posts); // 假设postsService是IService<Posts>的实现类
|
|
|
+ if (saveResult != 1) {
|
|
|
+ return new Result(500, "帖子表插入失败", null);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 自增主键已自动回填到posts对象中
|
|
|
+ Integer postId = posts.getId();
|
|
|
+ if (postId == null) {
|
|
|
+ return new Result(500, "获取帖子ID失败", null);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> imageList = posts.getImageList();
|
|
|
+ if (imageList != null && !imageList.isEmpty()) {
|
|
|
+ Date now = new Date();
|
|
|
+ for (int i = 0; i < imageList.size(); i++) {
|
|
|
+ PostMedia postMedia = new PostMedia();
|
|
|
+ // 假设PostMedia的id仍使用雪花算法(如果PostMedia的id也是自增,移除这行)
|
|
|
+ long mediaId = IdUtil.getSnowflake(2, 2).nextId();
|
|
|
+ postMedia.setId(mediaId);
|
|
|
+ postMedia.setPostId(Long.valueOf(postId)); // 使用回填的帖子ID
|
|
|
+ postMedia.setMediaType(1);
|
|
|
+ postMedia.setMediaUrl(imageList.get(i));
|
|
|
+ postMedia.setDisplayOrder(i + 1);
|
|
|
+ postMedia.setCreatedAt(now);
|
|
|
+
|
|
|
+ int mediaSaveResult = postMediaMapper.insert(postMedia);
|
|
|
+ if (mediaSaveResult != 1) {
|
|
|
+ return new Result(500, "图片媒体表插入失败,第" + (i + 1) + "条数据异常", null);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ System.err.println("审核通过");
|
|
|
+ return new Result(200,"添加成功","添加成功");
|
|
|
+ }else {
|
|
|
+ return new Result(400,"审核失败,存在违规内容",null);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result gettopic() {
|
|
|
+// List<Topics> topics = topicsMapper.selectList(null);
|
|
|
+ List<Topics> topics = topicsMapper.selectList(null);
|
|
|
+
|
|
|
+// 根据某个字段去重(例如:name字段)
|
|
|
+ List<Topics> distinctTopics = topics.stream()
|
|
|
+ .collect(Collectors.toMap(
|
|
|
+ Topics::getTitle, // key: 去重字段
|
|
|
+ Function.identity(), // value: 对象本身
|
|
|
+ (existing, replacement) -> existing // 冲突时保留先出现的对象
|
|
|
+ ))
|
|
|
+ .values()
|
|
|
+ .stream()
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ return new Result(200, "success", distinctTopics);
|
|
|
+// return new Result(200, "success",topics);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result profile(Integer id) {
|
|
|
+ com.dt.user.pojo.User user = userMapper.selectById(id);
|
|
|
+ QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.eq("user_id",id);
|
|
|
+ UserInfo userInfo = userInfoMapper.selectOne(wrapper);
|
|
|
+ user.setBio(userInfo.getBio());
|
|
|
+ user.setCitys(userInfo.getCitys());
|
|
|
+ user.setGuanzu(userInfo.getGuanzu());
|
|
|
+ user.setFensi(userInfo.getFensi());
|
|
|
+ user.setHuozan(userInfo.getHuozan());
|
|
|
+ user.setAvatares(userInfo.getAvatares());
|
|
|
+ return new Result(200, "success",user);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result hot(Integer hotid) {
|
|
|
+ QueryWrapper<Topics> wrapper = new QueryWrapper<>();
|
|
|
+ if (hotid==1){
|
|
|
+ wrapper.eq("is_hot",hotid);
|
|
|
+ }
|
|
|
+ List<Topics> topics = topicsMapper.selectList(wrapper);
|
|
|
+ if (topics!=null){
|
|
|
+ return new Result(200, "success",topics);
|
|
|
+
|
|
|
+ }else {
|
|
|
+ return new Result(500, "success",null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result findFollow() {
|
|
|
+ List<UserFollow> userFollows = userFollowMapper.selectList(null);
|
|
|
+ return new Result(200, "success",userFollows);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result follow(ParamsGZdto paramsGZdto) {
|
|
|
+ UserFollow userFollow= new UserFollow();
|
|
|
+ userFollow.setFollowerId(paramsGZdto.getFollowerId());
|
|
|
+ userFollow.setFollowingId(paramsGZdto.getFollowingId());
|
|
|
+ userFollow.setCreatedAt(new Date());
|
|
|
+ userFollow.setStatus(1);
|
|
|
+ int insert = userFollowMapper.insert(userFollow);
|
|
|
+ if (insert==1){
|
|
|
+ return new Result(200, "success",null);
|
|
|
+ }else {
|
|
|
+ return new Result(500, "失败",null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result unfollow(ParamsGZdto paramsGZdto) {
|
|
|
+ QueryWrapper<UserFollow> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("follower_id",paramsGZdto.getFollowerId());
|
|
|
+ queryWrapper.eq("following_id",paramsGZdto.getFollowingId());
|
|
|
+ UserFollow userFollow = userFollowMapper.selectOne(queryWrapper);
|
|
|
+ int i = userFollowMapper.deleteById(userFollow);
|
|
|
+// if (userFollow.getStatus()==1){
|
|
|
+// userFollow.setStatus(0);
|
|
|
+// }else {
|
|
|
+// userFollow.setStatus(1);
|
|
|
+// }
|
|
|
+//
|
|
|
+// int i = userFollowMapper.updateById(userFollow);
|
|
|
+ if (i==1){
|
|
|
+ return new Result(200, "success",null);
|
|
|
+ }else {
|
|
|
+ return new Result(500, "失败",null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result topics(Integer id) {
|
|
|
+ Topics topics = topicsMapper.selectById(id);
|
|
|
+ return new Result(200, "success",topics);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result addTopice(TopicsDto topics) throws Exception {
|
|
|
+ String title = topics.getTitle();
|
|
|
+ String contents = topics.getContent();
|
|
|
+ String content = title+contents;
|
|
|
+ ResponseResult responseResult = TextModerationPlusDemo.textModerationPlus(content);
|
|
|
+ if(responseResult.getCode()==200){
|
|
|
+ topics.setCreatedAt(new Date());
|
|
|
+ Topics topics1 = new Topics();
|
|
|
+ BeanUtils.copyProperties(topics,topics1);
|
|
|
+ int insert = topicsMapper.insert(topics1);
|
|
|
+ System.err.println("审核通过");
|
|
|
+ return new Result(200,"添加成功","添加成功");
|
|
|
+ }else {
|
|
|
+ return new Result(400,"审核失败,存在违规内容",null);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result findbyport(Integer postId) {
|
|
|
+ QueryWrapper<PostComment> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.eq("post_id", postId);
|
|
|
+ List<PostComment> postComments = postCommentMapper.selectList(wrapper);
|
|
|
+
|
|
|
+ List<PostCommentDto> postCommentDtoList = new ArrayList<>();
|
|
|
+// 遍历查询结果并转换为DTO
|
|
|
+ for (PostComment comment : postComments) {
|
|
|
+ com.dt.user.pojo.User byId = userMapper.selectById(comment.getUserId());
|
|
|
+ comment.setUsername(byId.getUsername());
|
|
|
+ comment.setAvatar(byId.getAvatar());
|
|
|
+ PostCommentDto dto = new PostCommentDto();
|
|
|
+ BeanUtils.copyProperties(comment, dto);
|
|
|
+ postCommentDtoList.add(dto);
|
|
|
+ }
|
|
|
+
|
|
|
+ return new Result(200, null, postCommentDtoList);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result topicsgetbyid(Integer id) {
|
|
|
+ QueryWrapper<TopicReplies> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.eq("topic_id", id);
|
|
|
+ List<TopicReplies> topicReplies = topicRepliesMapper.selectList(wrapper);
|
|
|
+ List<TopicRepliesDto> topicRepliesDtoList = new ArrayList<>();
|
|
|
+ for (TopicReplies topicReply : topicReplies) {
|
|
|
+ TopicRepliesDto dto = new TopicRepliesDto();
|
|
|
+ topicReply.getUserId();
|
|
|
+ com.dt.user.pojo.User byId = userMapper.selectById(topicReply.getUserId());
|
|
|
+ QueryWrapper<UserInfo> wrapper1 = new QueryWrapper<>();
|
|
|
+ wrapper1.eq("user_id", topicReply.getUserId());
|
|
|
+ UserInfo userInfo = userInfoMapper.selectOne(wrapper1);
|
|
|
+ dto.setAvatar(byId.getAvatar());
|
|
|
+ dto.setUsername(byId.getUsername());
|
|
|
+ dto.setBio(userInfo.getBio());
|
|
|
+ // 把源对象(topicReply)的属性值复制到目标对象(dto)
|
|
|
+ BeanUtils.copyProperties(topicReply, dto);
|
|
|
+ topicRepliesDtoList.add(dto);
|
|
|
+ }
|
|
|
+ return new Result(200,"查询成功", topicRepliesDtoList);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result wenzhangid(Long myid, Long wenzhangid) {
|
|
|
+ QueryWrapper<UserFollow> wrapper = new QueryWrapper<>();
|
|
|
+ wrapper.eq("follower_id", myid);
|
|
|
+ wrapper.eq("following_id", wenzhangid);
|
|
|
+ UserFollow userFollow = userFollowMapper.selectOne(wrapper);
|
|
|
+ List<UserFollow> userFollows = userFollowMapper.selectList(null);
|
|
|
+ System.err.println(userFollows);
|
|
|
+ if (userFollow != null) {
|
|
|
+ return new Result(200,"关注成功", userFollow);
|
|
|
+ }else {
|
|
|
+ return new Result(200,"未关注", null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|