Browse Source

在线问答

1018653686@qq.com 1 year ago
parent
commit
6253c6a3a8

+ 61 - 30
hx-xmhjc/src/main/java/com/fjhx/xmhjc/controller/open/OpenTopicController.java

@@ -2,6 +2,7 @@ package com.fjhx.xmhjc.controller.open;
 
 
 import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.util.NumberUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -24,7 +25,9 @@ import com.fjhx.xmhjc.utils.WebsiteUserUtil;
 import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 @RestController
 @RequestMapping("/open/topic")
@@ -36,17 +39,52 @@ public class OpenTopicController {
     @Resource
     private WebsiteUsersService websiteUsersService;
 
-    @PostMapping("/contentPage")
-    public Page<TopicContentVo> contentPage(TopicContentSelectDto dto) {
+    /**
+     * @description: 主题列表
+     * @author hj
+     * @date 2023/11/23 22:29
+     */
+    @PostMapping("/contentPage/{sortType}")
+    public Page<TopicContentVo> contentPage(TopicContentSelectDto dto, @PathVariable String sortType) {
         dto.setType("1");
-        Page<TopicContentVo> page = topicContentService.contentPage(dto);
+        Page<TopicContentVo> page = topicContentService.contentPage(dto, sortType);
         page.getRecords().forEach(topicContentVo -> {
-            WebsiteUsers websiteUsers = websiteUsersService.getById(topicContentVo.getAuthor());
-            topicContentVo.setAuthorName(ObjectUtil.isNull(websiteUsers) ? "匿名" : websiteUsers.getUserName());
+            topicContentVo.setAuthorName(topicContentService.getAuthorName(topicContentVo.getAuthor()));
         });
         return page;
     }
 
+
+    /**
+     * 回复列表
+     * @author hj
+     * @date 2023/11/23 22:29
+     */
+    @PostMapping("/replyPage")
+    public Page<TopicRepliesVo> replyPage(@RequestBody TopicRepliesSelectDto dto) {
+        if(!NumberUtil.isValidNumber(dto.getFloorId())){
+            throw new RuntimeException("参数错误");
+        }
+        Map<Long, WebsiteUsers> websiteUsersMap = new HashMap<>();
+        Page<TopicRepliesVo> page = topicRepliesService.replyPage(dto);
+        page.getRecords().forEach(topicContentVo -> {
+            topicContentVo.setAuthorName(topicContentService.getAuthorName(topicContentVo.getAuthor(), websiteUsersMap));
+
+            WebsiteUsers websiteUsers = websiteUsersMap.get(topicContentVo.getAuthor());
+            if(ObjectUtil.isNull(websiteUsers)){
+                websiteUsers = websiteUsersService.getById(topicContentVo.getAuthor());
+                websiteUsersMap.put(topicContentVo.getAuthor(),websiteUsers);
+            }
+            topicContentVo.setCiteAuthorName(topicContentService.getAuthorName(topicContentVo.getCiteAuthor(), websiteUsersMap));
+        });
+        return page;
+    }
+
+    /**
+     * 创建提问
+     * @author hj
+     * @date 2023/11/23 22:29
+     */
     @LoginValid
     @PostMapping("/createQuestion")
     public void createQuestion(@RequestBody TopicContentDto topicContentDto) {
@@ -62,6 +100,12 @@ public class OpenTopicController {
         topicContentService.add(topicContentDto);
     }
 
+
+    /**
+     * 回复主题/楼层
+     * @author hj
+     * @date 2023/11/23 22:30
+     */
     @LoginValid
     @PostMapping("/reply/{topicId}")
     public void reply(@RequestBody TopicRepliesDto topicRepliesDto, @PathVariable Long topicId) {
@@ -71,50 +115,36 @@ public class OpenTopicController {
         topicRepliesService.add(topicRepliesDto);
     }
 
-    @LoginValid
+    /**
+     * 主题详情
+     * @author hj
+     * @date 2023/11/23 22:30
+     */
     @PostMapping("/detail/{topicId}")
-    public OpenTopicContentVO detail(@RequestBody TopicRepliesSelectDto topicRepliesSelectDto, @PathVariable Long topicId) {
+    public OpenTopicContentVO detail(@PathVariable Long topicId) {
         TopicContent topicContent = topicContentService.getById(topicId);
         if (ObjectUtil.isNull(topicContent)) {
             throw new RuntimeException("主题不存在");
         }
+        topicContent.setViews(topicContent.getViews() + 1);
+        topicContentService.updateById(topicContent);
+
         OpenTopicContentVO openTopicContentVO = BeanUtil.copyProperties(topicContent, OpenTopicContentVO.class);
-        openTopicContentVO.setAuthorName(getAuthorName(topicContent.getAuthor()));
+        openTopicContentVO.setAuthorName(topicContentService.getAuthorName(topicContent.getAuthor()));
         //分页查出楼层回复
         TopicRepliesSelectDto dto = new TopicRepliesSelectDto();
         Page<TopicRepliesVo> page = topicRepliesService.getPageByOpen(dto);
         List<TopicRepliesVo> records = page.getRecords();
         records.forEach(topicRepliesVo -> {
-            topicRepliesVo.setAuthorName(getAuthorName(topicRepliesVo.getAuthor()));
+            topicRepliesVo.setAuthorName(topicContentService.getAuthorName(topicRepliesVo.getAuthor()));
             dto.setFloorId(topicRepliesVo.getId());
             Page<TopicRepliesVo> floorPage = topicRepliesService.getPageByOpen(dto);
             topicRepliesVo.setFloorPage(floorPage);
         });
-
         openTopicContentVO.setRepliesPage(page);
-
         return openTopicContentVO;
     }
 
-    private String getAuthorName(Long author) {
-        WebsiteUsers websiteUsers = websiteUsersService.getById(author);
-        return ObjectUtil.isNull(websiteUsers) ? "匿名" : websiteUsers.getUserName();
-    }
-
-    @LoginValid
-    @PostMapping("/replyPage/{topicId}")
-    public void subReplyList(@RequestBody TopicRepliesDto topicRepliesDto, @PathVariable Long topicId) {
-        TopicContent topicContent = topicContentService.getById(topicId);
-        if (ObjectUtil.isNull(topicContent)) {
-            throw new RuntimeException("主题不存在");
-        }
-        //分页查出楼层回复
-        validateReply(topicRepliesDto, topicId);
-        WebsiteUsers loginWebsiteUser = WebsiteUserUtil.getLoginWebsiteUser();
-        topicRepliesDto.setAuthor(loginWebsiteUser.getId());
-        topicRepliesService.add(topicRepliesDto);
-    }
-
 
     private void validateReply(TopicRepliesDto topicRepliesDto, Long topicId) {
         if (StrUtil.isBlank(topicRepliesDto.getContent())) {
@@ -140,6 +170,7 @@ public class OpenTopicController {
             if (ObjectUtil.isNull(replyTo)) {
                 throw new RuntimeException("回复对象不存在");
             }
+            topicRepliesDto.setCiteAuthor(replyTo.getAuthor());
         }
         topicRepliesDto.setTopicId(topicId);
     }

+ 9 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/controller/topic/TopicContentController.java

@@ -1,5 +1,6 @@
 package com.fjhx.xmhjc.controller.topic;
 
+import com.fjhx.xmhjc.service.topic.TopicRepliesService;
 import org.springframework.web.bind.annotation.*;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.fjhx.xmhjc.entity.topic.vo.TopicContentVo;
@@ -9,6 +10,7 @@ import com.ruoyi.common.core.domain.BaseSelectDto;
 import com.fjhx.xmhjc.service.topic.TopicContentService;
 import org.springframework.beans.factory.annotation.Autowired;
 
+import javax.annotation.Resource;
 import java.util.List;
 
 /**
@@ -25,6 +27,8 @@ public class TopicContentController {
 
     @Autowired
     private TopicContentService topicContentService;
+    @Resource
+    private TopicRepliesService topicRepliesService;
 
     /**
      * 内容主题列表
@@ -74,4 +78,9 @@ public class TopicContentController {
         topicContentService.delete(dto.getId());
     }
 
+    @PostMapping("/hasReply")
+    public Boolean hasReply(@RequestBody TopicContentDto topicContentDto) {
+        return topicRepliesService.hasReply(topicContentDto.getId());
+    }
+
 }

+ 4 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/entity/topic/dto/TopicRepliesDto.java

@@ -13,5 +13,9 @@ import lombok.Setter;
 @Getter
 @Setter
 public class TopicRepliesDto extends TopicReplies {
+    // 回复类型 1:回复主题 2:回复楼层 3:回复楼层内的评论
     String replyType;
+
+    // 被回复人id
+    Long citeAuthor;
 }

+ 2 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/entity/topic/po/TopicContent.java

@@ -36,4 +36,6 @@ public class TopicContent extends BasePo {
 
     private Long author;
 
+    private Integer views;
+
 }

+ 2 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/entity/topic/po/TopicReplies.java

@@ -28,4 +28,6 @@ public class TopicReplies extends BasePo {
     private Long replyId;
 
     private Long author;
+
+    private Long citeAuthor;
 }

+ 5 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/entity/topic/vo/TopicRepliesVo.java

@@ -19,5 +19,10 @@ public class TopicRepliesVo extends TopicReplies {
      */
     private String authorName;
 
+    /**
+     * 被回复人名称
+     */
+    private String citeAuthorName;
+
     private Page<TopicRepliesVo> floorPage;
 }

+ 7 - 1
hx-xmhjc/src/main/java/com/fjhx/xmhjc/service/topic/TopicContentService.java

@@ -2,6 +2,7 @@ package com.fjhx.xmhjc.service.topic;
 
 import com.fjhx.xmhjc.entity.product.vo.ProductCategoryVo;
 import com.fjhx.xmhjc.entity.topic.po.TopicContent;
+import com.fjhx.xmhjc.entity.website.po.WebsiteUsers;
 import com.ruoyi.common.core.service.BaseService;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.fjhx.xmhjc.entity.topic.vo.TopicContentVo;
@@ -9,6 +10,7 @@ import com.fjhx.xmhjc.entity.topic.dto.TopicContentSelectDto;
 import com.fjhx.xmhjc.entity.topic.dto.TopicContentDto;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * <p>
@@ -50,5 +52,9 @@ public interface TopicContentService extends BaseService<TopicContent> {
      */
     void delete(Long id);
 
-    Page<TopicContentVo> contentPage(TopicContentSelectDto dto);
+    Page<TopicContentVo> contentPage(TopicContentSelectDto dto, String sortType);
+
+    String getAuthorName(Long author);
+
+    String getAuthorName(Long author, Map<Long, WebsiteUsers> websiteUsersMap);
 }

+ 4 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/service/topic/TopicRepliesService.java

@@ -50,4 +50,8 @@ public interface TopicRepliesService extends BaseService<TopicReplies> {
     void delete(Long id);
 
     Page<TopicRepliesVo> getPageByOpen(TopicRepliesSelectDto dto);
+
+    Page<TopicRepliesVo> replyPage(TopicRepliesSelectDto dto);
+
+    Boolean hasReply(Long id);
 }

+ 40 - 3
hx-xmhjc/src/main/java/com/fjhx/xmhjc/service/topic/impl/TopicContentServiceImpl.java

@@ -1,10 +1,14 @@
 package com.fjhx.xmhjc.service.topic.impl;
 
-import com.fjhx.xmhjc.entity.product.vo.ProductCategoryVo;
+import cn.hutool.core.util.ObjectUtil;
 import com.fjhx.xmhjc.entity.topic.po.TopicContent;
+import com.fjhx.xmhjc.entity.website.po.WebsiteUsers;
 import com.fjhx.xmhjc.mapper.topic.TopicContentMapper;
 import com.fjhx.xmhjc.service.topic.TopicContentService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.fjhx.xmhjc.service.website.WebsiteUsersService;
+import com.fjhx.xmhjc.utils.WebsiteUserUtil;
+import com.ruoyi.common.utils.wrapper.SqlField;
 import org.springframework.stereotype.Service;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.fjhx.xmhjc.entity.topic.vo.TopicContentVo;
@@ -13,7 +17,9 @@ import com.ruoyi.common.utils.wrapper.IWrapper;
 import com.fjhx.xmhjc.entity.topic.dto.TopicContentDto;
 import cn.hutool.core.bean.BeanUtil;
 
+import javax.annotation.Resource;
 import java.util.List;
+import java.util.Map;
 
 /**
  * <p>
@@ -25,6 +31,9 @@ import java.util.List;
  */
 @Service
 public class TopicContentServiceImpl extends ServiceImpl<TopicContentMapper, TopicContent> implements TopicContentService {
+    @Resource
+    private WebsiteUsersService websiteUsersService;
+
 
     @Override
     public List<TopicContentVo> getList(TopicContentSelectDto dto) {
@@ -38,7 +47,12 @@ public class TopicContentServiceImpl extends ServiceImpl<TopicContentMapper, Top
     public Page<TopicContentVo> getPage(TopicContentSelectDto dto) {
         IWrapper<TopicContent> wrapper = getWrapper();
         wrapper.orderByDesc("tc", TopicContent::getId);
+        wrapper.keyword(dto, new SqlField("tc", TopicContent::getTitle));
         Page<TopicContentVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
+        List<TopicContentVo> records = page.getRecords();
+        for (TopicContentVo record : records) {
+            record.setAuthorName(getAuthorName(record.getAuthor()));
+        }
         return page;
     }
 
@@ -66,11 +80,34 @@ public class TopicContentServiceImpl extends ServiceImpl<TopicContentMapper, Top
 
 
     @Override
-    public Page<TopicContentVo> contentPage(TopicContentSelectDto dto) {
+    public Page<TopicContentVo> contentPage(TopicContentSelectDto dto, String sortType) {
         IWrapper<TopicContent> wrapper = getWrapper();
-        wrapper.orderByDesc("tc", TopicContent::getToping);
+        if("1".equals(sortType)) {
+            wrapper.orderByDesc("tc", TopicContent::getToping);
+        }else if("3".equals(sortType)) {
+            wrapper.eq("tc", TopicContent::getAuthor, WebsiteUserUtil.getLoginWebsiteUser().getId());
+        }
+
         wrapper.orderByDesc("tc", TopicContent::getId);
+
         Page<TopicContentVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
         return page;
     }
+
+
+    @Override
+    public String getAuthorName(Long author) {
+        WebsiteUsers websiteUsers = websiteUsersService.getById(author);
+        return ObjectUtil.isNull(websiteUsers) ? "" : websiteUsers.getUserName();
+    }
+
+    @Override
+    public String getAuthorName(Long author, Map<Long, WebsiteUsers> websiteUsersMap) {
+        WebsiteUsers websiteUsers = websiteUsersMap.get(author);
+        if (ObjectUtil.isNull(websiteUsers)) {
+            websiteUsers = websiteUsersService.getById(author);
+            websiteUsersMap.put(author, websiteUsers);
+        }
+        return ObjectUtil.isNull(websiteUsers) ? "" : websiteUsers.getUserName();
+    }
 }

+ 20 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/service/topic/impl/TopicRepliesServiceImpl.java

@@ -73,4 +73,24 @@ public class TopicRepliesServiceImpl extends ServiceImpl<TopicRepliesMapper, Top
         Page<TopicRepliesVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
         return page;
     }
+
+    @Override
+    public Page<TopicRepliesVo> replyPage(TopicRepliesSelectDto dto) {
+        IWrapper<TopicReplies> wrapper = getWrapper();
+        wrapper.orderByAsc("tr", TopicReplies::getId);
+        wrapper.eq("tr", TopicReplies::getTopicId, dto.getTopicId());
+        wrapper.eq(ObjectUtil.isNotNull(dto.getFloorId()), "floor_id", dto.getFloorId());
+        Page<TopicRepliesVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
+        return page;
+    }
+
+    @Override
+    public Boolean hasReply(Long id) {
+        Boolean hasReply = false;
+        Long count = lambdaQuery().eq(TopicReplies::getTopicId, id).count();
+        if (count > 0) {
+            hasReply = true;
+        }
+        return hasReply;
+    }
 }

+ 4 - 2
hx-xmhjc/src/main/resources/mapper/topic/TopicContentMapper.xml

@@ -13,7 +13,8 @@
             tc.update_time,
             tc.toping,
             tc.content,
-            tc.author
+            tc.author,
+            tc.views
         from topic_content tc
             ${ew.customSqlSegment}
     </select>
@@ -30,7 +31,8 @@
             tc.update_time,
             tc.toping,
             tc.content,
-            tc.author
+            tc.author,
+            tc.views
         from topic_content tc
             ${ew.customSqlSegment}
     </select>

+ 4 - 2
hx-xmhjc/src/main/resources/mapper/topic/TopicRepliesMapper.xml

@@ -12,7 +12,8 @@
             tr.create_time,
             tr.update_user,
             tr.update_time,
-            tr.author
+            tr.author,
+            tr.cite_author
         from topic_replies tr
             ${ew.customSqlSegment}
     </select>
@@ -28,7 +29,8 @@
             tr.create_time,
             tr.update_user,
             tr.update_time,
-            tr.author
+            tr.author,
+            tr.cite_author
         from topic_replies tr
             ${ew.customSqlSegment}
     </select>