Commit cfbe2210 by wangxin

咨询查询评论数量

parent 37358d6b
......@@ -73,6 +73,9 @@ public class DataClassificationVo {
@ApiModelProperty(value = "咨询文章信息")
private List<InformationVo> informationVoList;
@ApiModelProperty(value = "该分类下所有资讯的评论总数")
private Integer commentNum;
protected Serializable pkVal() {
return this.id;
}
......
......@@ -29,4 +29,6 @@ public class ClassificationVO {
@ApiModelProperty(value = "咨询文章信息")
private List<InformationVo> informationVoList;
@ApiModelProperty(value = "该分类下所有资讯的评论总数")
private Integer commentNum ;
}
\ No newline at end of file
......@@ -57,7 +57,7 @@ public class ClassifyManageController {
*/
@GetMapping("/site/classify/list")
public List<ClassificationVO> getClassification(@RequestBody SiteComponyIdVO siteComponyIdVO) {
return dataClassificationService.getClassification(siteComponyIdVO);
return dataClassificationService.getClassificationAndCommentNum(siteComponyIdVO);
}
/**
......
package com.yizhi.site.application.mapper;
import java.util.List;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.yizhi.site.application.domain.DataClassification;
import org.apache.ibatis.annotations.Param;
import com.yizhi.site.application.vo.domain.DataClassificationVo;
import com.yizhi.site.application.vo.site.ClassificationVO;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
/**
* <p>
......@@ -29,7 +29,7 @@ public interface DataClassificationMapper extends BaseMapper<DataClassification>
* @param typeOne
* @return
*/
List<DataClassification> getSubcategories(@Param("siteId") Long siteId,@Param("typeOne") Long typeOne);
List<DataClassification> getSubcategories(@Param("siteId") Long siteId, @Param("typeOne") Long typeOne);
/**
* 获取新闻的分类
......@@ -38,4 +38,13 @@ public interface DataClassificationMapper extends BaseMapper<DataClassification>
* @return
*/
List<DataClassificationVo> selectClassification(@Param("siteId") Long siteId, @Param("parentId") Long parentId);
/**
* 获取分类列表的评论数量
* @param classificationIds 分类ID列表
* @param siteId 站点ID
* @return 分类ID和评论数量的映射关系
*/
List<Map<String, Object>> getCommentCountForClassifications(@Param("classificationIds") List<Long> classificationIds, @Param("siteId") Long siteId);
}
\ No newline at end of file
......@@ -35,6 +35,8 @@ public interface DataClassificationService extends IService<DataClassification>
* @return
*/
public List<ClassificationVO> getClassification(SiteComponyIdVO SiteComponyIdVO);
public List<ClassificationVO> getClassificationAndCommentNum(SiteComponyIdVO SiteComponyIdVO);
/**
* 添加分类
......
......@@ -166,6 +166,173 @@ public class DataClassificationServiceImpl extends ServiceImpl<DataClassificatio
}
return list;
}
@Override
public List<ClassificationVO> getClassificationAndCommentNum(SiteComponyIdVO siteComponyIdVO) {
Long siteId = siteComponyIdVO.getSiteId();
log.info("查询的站点id:{}",siteId);
Long createById = siteComponyIdVO.getCreateById();
String createByName = siteComponyIdVO.getCreateByName();
ClassificationVO classificationVO = null;
Date date = new Date();
// 防止并发添加数据
List<ClassificationVO> list = dataClassificationMapper.selectSonClassification(siteId);
if (list == null || list.size() < 1) {
/*//没有初始化的数据,2024-12-16注释
synchronized (this) {
DataClassification dataClassification = new DataClassification();
dataClassification.setId(idGenerator.generate());
dataClassification.setName("公告");
dataClassification.setDescription("公告");
dataClassification.setParentId(0L);
dataClassification.setSiteId(siteId);
dataClassification.setCreateById(createById);
dataClassification.setCreateByName(createByName);
dataClassification.setUpdateById(createById);
dataClassification.setUpdateByName(createByName);
dataClassification.setSort(1L);
dataClassification.setState(1);
dataClassification.setCreateTime(date);
dataClassification.setUpdateTime(date);
dataClassificationService.insert(dataClassification);
classificationVO = new ClassificationVO();
BeanUtils.copyProperties(dataClassification, classificationVO);
list.add(classificationVO);
dataClassification.setId(idGenerator.generate());
dataClassification.setName("新闻");
dataClassification.setDescription("新闻");
dataClassification.setParentId(0L);
dataClassification.setSiteId(siteId);
dataClassification.setCreateById(createById);
dataClassification.setCreateByName(createByName);
dataClassification.setUpdateById(createById);
dataClassification.setUpdateByName(createByName);
dataClassification.setSort(2L);
dataClassification.setState(1);
dataClassification.setCreateTime(date);
dataClassification.setUpdateTime(date);
dataClassificationService.insert(dataClassification);
classificationVO = new ClassificationVO();
BeanUtils.copyProperties(dataClassification, classificationVO);
list.add(classificationVO);
}*/
} else {
// 用于汇总所有分类的评论数量
int totalCommentNum = 0;
Map<String, Object> map = null;
for(ClassificationVO vo : list) {
List<String> wrapperList = new ArrayList<String>();
wrapperList.add("sort");
// 移除了 vo.getId() != 0L 的判断,确保所有分类都会被处理
if(vo.getId() != 0L){
EntityWrapper<DataClassification> wrapper = new EntityWrapper<>();
wrapper.eq("state", 1);
wrapper.eq("parent_id", vo.getId());
wrapper.orderAsc(wrapperList);
List<DataClassification> selectByMap = dataClassificationService.selectList(wrapper);
List<DataClassificationVo> selectVoByMap = new ArrayList<>();
// 父类的评论数量累计值
int parentCommentNum = 0;
if (CollectionUtil.isNotEmpty(selectByMap)) {
// 创建一个Map来存储分类ID和评论数量的映射关系
Map<Long, Integer> commentCountMap = getCommentCountForClassifications(selectByMap, siteId);
for (DataClassification d : selectByMap) {
DataClassificationVo entityVo = new DataClassificationVo();
BeanUtils.copyProperties(d, entityVo);
// 获取该分类的评论数量
int classificationCommentNum = commentCountMap.getOrDefault(d.getId(), 0);
// 设置该分类的评论数量
entityVo.setCommentNum(classificationCommentNum);
// 累加到父类的评论数量
parentCommentNum += classificationCommentNum;
// 累加到总评论数量
totalCommentNum += classificationCommentNum;
selectVoByMap.add(entityVo);
//获取子节点
EntityWrapper<DataClassification> wrapperChild = new EntityWrapper<>();
wrapperChild.eq("state", 1);
wrapperChild.eq("parent_id", entityVo.getId());
wrapperChild.orderAsc(wrapperList);
List<DataClassification> classificationChild = dataClassificationService.selectList(wrapperChild);
List<DataClassificationVo> childClassifyNews = new ArrayList<>();
if (CollectionUtil.isNotEmpty(classificationChild)) {
// 创建一个Map来存储子分类ID和评论数量的映射关系
Map<Long, Integer> childCommentCountMap = getCommentCountForClassifications(classificationChild, siteId);
for (DataClassification child : classificationChild) {
DataClassificationVo entityChild = new DataClassificationVo();
BeanUtils.copyProperties(child, entityChild);
// 获取该子分类的评论数量
int childCommentNum = childCommentCountMap.getOrDefault(child.getId(), 0);
// 设置该子分类的评论数量
entityChild.setCommentNum(childCommentNum);
// 累加到父类的评论数量
parentCommentNum += childCommentNum;
// 累加到总评论数量
totalCommentNum += childCommentNum;
childClassifyNews.add(entityChild);
}
entityVo.setChildClassifyNews(childClassifyNews);
}
}
}
// 设置父类的评论数量(所有子类评论数量的累加)
vo.setCommentNum(parentCommentNum);
vo.setData(selectVoByMap);
}
}
// 为ID为0的分类设置总评论数量
for (ClassificationVO vo : list) {
if (vo.getId() == 0L) {
vo.setCommentNum(totalCommentNum);
// break;
}
}
}
return list;
}
/**
* 获取分类列表的评论数量
* @param classifications 分类列表
* @param siteId 站点ID
* @return 分类ID和评论数量的映射关系
*/
private Map<Long, Integer> getCommentCountForClassifications(List<DataClassification> classifications, Long siteId) {
Map<Long, Integer> commentCountMap = new HashMap<>();
if (CollectionUtils.isEmpty(classifications)) {
return commentCountMap;
}
// 提取分类ID列表
List<Long> classificationIds = new ArrayList<>();
for (DataClassification classification : classifications) {
classificationIds.add(classification.getId());
}
// 通过SQL查询获取这些分类下的资讯评论数量
List<Map<String, Object>> commentCounts = dataClassificationMapper.getCommentCountForClassifications(classificationIds, siteId);
// 将查询结果转换为Map
for (Map<String, Object> commentCount : commentCounts) {
Long classificationId = (Long) commentCount.get("classificationId");
Long count = (Long) commentCount.get("commentCount");
commentCountMap.put(classificationId, count != null ? count.intValue() : 0);
}
return commentCountMap;
}
@Override
public Boolean insertClassification(DataClassification dataClassification) {
......
......@@ -78,4 +78,19 @@
from data_classification where parent_id=#{parentId} and site_id=#{siteId} AND state=1
order by sort
</select>
<select id="getCommentCountForClassifications" resultType="java.util.Map">
SELECT
dcl.id as classificationId,
COALESCE(COUNT(nc.id), 0) as commentCount
FROM data_classification dcl
LEFT JOIN information i ON dcl.id = i.type_two AND i.state = 2
LEFT JOIN cloud_trainning_project.tp_comment nc ON i.id = nc.training_project_id AND nc.state != 0
WHERE dcl.id IN
<foreach item="id" collection="classificationIds" open="(" separator="," close=")">
#{id}
</foreach>
AND dcl.site_id = #{siteId}
GROUP BY dcl.id
</select>
</mapper>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment