package com.iailab.module.knowledge.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.iailab.framework.common.constant.Constant; import com.iailab.framework.common.page.PageData; import com.iailab.framework.common.service.impl.BaseServiceImpl; import com.iailab.framework.common.util.object.ConvertUtils; import com.iailab.module.knowledge.dao.KnowledgePathDao; import com.iailab.module.knowledge.dto.KnowledgePathDTO; import com.iailab.module.knowledge.entity.KnowledgePathEntity; import com.iailab.module.knowledge.service.KnowledgePathService; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * @author PanZhibao * @Description * @createTime 2023年12月25日 17:33:00 */ @Service public class KnowledgePathServiceImpl extends BaseServiceImpl implements KnowledgePathService { @Resource public KnowledgePathDao knowledgePathDao; final String ROOT = "0000000000"; @Override public PageData page(Map params) { IPage page = baseDao.selectPage( getPage(params, Constant.CREATE_DATE, false), getWrapper(params) ); return getPageData(page, KnowledgePathDTO.class); } private QueryWrapper getWrapper(Map params) { String treeId = (String) params.get("treeId"); String title = (String) params.get("title"); String keyWords = (String) params.get("keyWords"); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq(StringUtils.isNotBlank(treeId), "tree_id", treeId) .like(StringUtils.isNotBlank(title), "title", title) .like(StringUtils.isNotBlank(keyWords), "key_words", keyWords); return wrapper; } @Override public List list(Map params) { return baseDao.selectPathList(); } /** * 查询树形图 * * @return 树形图 */ @Override public List tree() { List resultList = new ArrayList<>(); // 所有树形图节点 List dataList = knowledgePathDao.selectPathList(); // 先取出根节点 for (KnowledgePathDTO item : dataList) { // 根节点的父id为0000000000 if (ROOT.equals(item.getParentId())) { resultList.add(item); } } // 根据根节点id,查询对应子节点 for (KnowledgePathDTO parent : resultList) { List childList = getChild(parent.getId(), dataList); parent.setChildren(childList); } return resultList; } /** * 设置子节点 * * @param parentId 父节点id * @param dataList 所有节点 * @return 子节点列表 */ private List getChild(String parentId, List dataList) { List childList = new ArrayList<>(); // 把节点的子节点找出 for (KnowledgePathDTO item : dataList) { if (parentId.equals(item.getParentId())) { childList.add(item); } } // 递归 for (KnowledgePathDTO parent : childList) { parent.setChildren(getChild(parent.getId(), dataList)); } // 如果没有子节点了,设置一个空列表 if (childList.size() == 0) { return new ArrayList<>(); } return childList; } @Override public KnowledgePathDTO get(String id) { KnowledgePathEntity entity = baseDao.selectById(id); return ConvertUtils.sourceToTarget(entity, KnowledgePathDTO.class); } @Override public void save(KnowledgePathDTO dto) { KnowledgePathEntity entity = ConvertUtils.sourceToTarget(dto, KnowledgePathEntity.class); insert(entity); } @Override public void update(KnowledgePathDTO dto) { KnowledgePathEntity entity = ConvertUtils.sourceToTarget(dto, KnowledgePathEntity.class); updateById(entity); } @Override public void delete(String id) { baseDao.delete(new QueryWrapper() .eq(StringUtils.isNotBlank(id), "id", id) .or().eq(StringUtils.isNotBlank(id), "parent_id", id) ); } }