Helius
2022-07-08 9ab2d8c2617eb256b7ca03253ef76bb9d8ac2210
src/main/java/com/xcong/farmer/cms/core/handler/ArticleDataParserHandler.java
@@ -2,11 +2,15 @@
import com.alibaba.fastjson.JSONObject;
import com.xcong.farmer.cms.configurations.properties.CmsProperties;
import com.xcong.farmer.cms.conversion.ArticleConversion;
import com.xcong.farmer.cms.core.node.AttrNode;
import com.xcong.farmer.cms.core.tag.data.ArticleData;
import com.xcong.farmer.cms.core.tag.model.Article;
import com.xcong.farmer.cms.modules.system.entity.ArticleEntity;
import com.xcong.farmer.cms.modules.system.mapper.ArticleMapper;
import com.xcong.farmer.cms.utils.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -17,25 +21,36 @@
 * @author wzy
 * @date 2022-06-24
 **/
@Slf4j
public class ArticleDataParserHandler implements DataParserHandler {
    private final ArticleMapper articleMapper = SpringContextHolder.getBean(ArticleMapper.class);
    private final CmsProperties cmsProperties = SpringContextHolder.getBean(CmsProperties.class);
    @Override
    public void dataParser(AttrNode node) {
        System.out.println("ArticleDataParserHandler");
        log.info("文章解析");
        Article tag = (Article) node.getParam();
        ArticleEntity data = articleMapper.selectById(tag.getId());
        ArticleEntity prev = articleMapper.selectPrevOrNextArticle(data.getId(), data.getColumnId(), 1);
        ArticleEntity next = articleMapper.selectPrevOrNextArticle(data.getId(), data.getColumnId(), 2);
        ArticleEntity prevEntity = articleMapper.selectPrevOrNextArticle(data.getId(), data.getColumnId(), 1);
        ArticleData prev = ArticleConversion.INSTANCE.entityToData(prevEntity);
        if (prevEntity.getType() == 2) {
            prev.setUrl(prevEntity.getArticleUrl());
        } else {
            prev.setUrl(cmsProperties.getBaseUrl() + prevEntity.getPath() + "/" + prevEntity.getId() + ".html");
        }
        Map map = JSONObject.parseObject(JSONObject.toJSONString(data), Map.class);
//        Map<String, Object> map = new HashMap<>();
//        map.put("title", "这是单个文章标题");
        ArticleEntity nextEntity = articleMapper.selectPrevOrNextArticle(data.getId(), data.getColumnId(), 2);
        ArticleData next = ArticleConversion.INSTANCE.entityToData(nextEntity);
        if (nextEntity.getType() == 2) {
            next.setUrl(prevEntity.getArticleUrl());
        } else {
            next.setUrl(cmsProperties.getBaseUrl() + nextEntity.getPath() + "/" + nextEntity.getId() + ".html");
        }
        map.put("prev", prev);
        map.put("next", next);
        node.setData(map);
        ArticleData articleData = ArticleConversion.INSTANCE.entityToData(data);
        articleData.setNext(next);
        articleData.setPrev(prev);
        node.setData(articleData);
    }
}