package com.xcong.farmer.cms.modules.core.service.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import com.xcong.farmer.cms.common.contants.AppContants; import com.xcong.farmer.cms.core.template.TemplateConfiguration; import com.xcong.farmer.cms.modules.core.service.ICmsCoreService; import com.xcong.farmer.cms.modules.system.entity.ArticleEntity; import com.xcong.farmer.cms.modules.system.entity.ColumnEntity; import com.xcong.farmer.cms.modules.system.mapper.ArticleMapper; import com.xcong.farmer.cms.modules.system.mapper.ColumnMapper; import com.xcong.farmer.cms.modules.system.mapper.WebSetMapper; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.validation.constraints.NotNull; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.Executor; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * @author wzy * @date 2022-07-03 **/ @Slf4j @Service public class CmsCoreServiceImpl implements ICmsCoreService { @Autowired private TemplateConfiguration cfg; @Autowired private WebSetMapper webSetMapper; @Autowired private ColumnMapper columnMapper; @Autowired private ArticleMapper articleMapper; // private final Executor executor = new ThreadPoolExecutor(10, 20, 600, TimeUnit.SECONDS, new LinkedBlockingQueue()); @Override public void articleProcess(Map data, String templateName, String templatePath) { data.put("templateType", "article"); data.put("templatePath", templatePath); data.put("templateName", data.get("id")); globalData(data); if (StrUtil.isEmpty(templateName)) { templateName = "defualt.article.html"; } try { String finalTemplateName = templateName; AppContants.EXECUTOR.execute(() -> { cfg.process(data, finalTemplateName); }); } catch (Exception e) { e.printStackTrace(); log.error("发布文章出错", e); } } @Override public void articlesProcess(Map data, List ids, String templateName, String templatePath) { if (CollUtil.isEmpty(ids)) { return; } for (Long id : ids) { data.put("id", id); articleProcess(data, templateName, templatePath); } } @Override public void columnsProcess(Map data, List ids, String templateName) { if (CollUtil.isEmpty(ids)) { return; } for (Long id : ids) { data.put("id", id); columnProcess(data, templateName); } } @Override public void columnProcess(Map data, String templateName) { data.put("templateType", "column"); data.put("page", 1); globalData(data); if (StrUtil.isEmpty(templateName)) { templateName = "defualt.list.html"; } try { String finalTemplateName = templateName; AppContants.EXECUTOR.execute(() -> { cfg.process(data, finalTemplateName); }); } catch (Exception e) { e.printStackTrace(); log.error("发布栏目错误", e); } } @Override public void indexProcess(@NotNull Map data, String templateName) { data.put("templateType", "index"); globalData(data); if (StrUtil.isEmpty(templateName)) { templateName = "index.html"; } try { String finalTemplateName = templateName; AppContants.EXECUTOR.execute(() -> { cfg.process(data, finalTemplateName); }); } catch (Exception e) { e.printStackTrace(); log.error("发布首页错误", e); } } @Override public void process(Map data, String templateType, String templateName) { data.put("templateType", templateType); globalData(data); if ("search".equals(templateType) && StrUtil.isBlank(templateName)) { templateName = "search.html"; } if ("message".equals(templateType) && StrUtil.isBlank(templateName)) { templateName = "message.html"; } try { String finalTemplateName = templateName; AppContants.EXECUTOR.execute(() -> { cfg.process(data, finalTemplateName); }); } catch (Exception e) { e.printStackTrace(); log.error("发布错误", e); } } private void globalData(Map data) { Long companyId = (Long) data.get("companyId"); Map globalSetting = webSetMapper.selectSiteGlobalSetting(companyId); String type = (String) data.get("templateType"); if ("column".equals(type)){ Long id = (Long) data.get("id"); ColumnEntity columnEntity = columnMapper.selectById(id); globalSetting.put("title", columnEntity.getColumnName() + "_" + globalSetting.get("title")); } else if ("article".equals(type)) { Long id = (Long) data.get("id"); ArticleEntity article = articleMapper.selectById(id); ColumnEntity columnEntity = columnMapper.selectById(article.getColumnId()); globalSetting.put("title", article.getTitle() + "_" + columnEntity.getColumnName() +"_" + globalSetting.get("title")); } data.putAll(globalSetting); } }