|
@@ -1,8 +1,14 @@
|
|
|
package com.fs.decoration.service.impl;
|
|
package com.fs.decoration.service.impl;
|
|
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
+import com.fs.decoration.domain.DecorationCompanyTemplate;
|
|
|
import com.fs.decoration.domain.DecorationTemplate;
|
|
import com.fs.decoration.domain.DecorationTemplate;
|
|
|
import com.fs.decoration.dto.DecorationTemplateSaveReq;
|
|
import com.fs.decoration.dto.DecorationTemplateSaveReq;
|
|
|
|
|
+import com.fs.decoration.mapper.DecorationCompanyTemplateMapper;
|
|
|
import com.fs.decoration.mapper.DecorationTemplateMapper;
|
|
import com.fs.decoration.mapper.DecorationTemplateMapper;
|
|
|
import com.fs.decoration.service.IDecorationTemplateService;
|
|
import com.fs.decoration.service.IDecorationTemplateService;
|
|
|
import com.fs.decoration.vo.TemplateContentVO;
|
|
import com.fs.decoration.vo.TemplateContentVO;
|
|
@@ -21,6 +27,9 @@ public class DecorationTemplateServiceImpl implements IDecorationTemplateService
|
|
|
@Resource
|
|
@Resource
|
|
|
private DecorationTemplateMapper decorationTemplateMapper;
|
|
private DecorationTemplateMapper decorationTemplateMapper;
|
|
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private DecorationCompanyTemplateMapper decorationCompanyTemplateMapper;
|
|
|
|
|
+
|
|
|
/** 用于校验 templateData JSON 结构 */
|
|
/** 用于校验 templateData JSON 结构 */
|
|
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
|
|
@@ -92,4 +101,151 @@ public class DecorationTemplateServiceImpl implements IDecorationTemplateService
|
|
|
throw new RuntimeException("模板JSON格式错误:" + e.getMessage());
|
|
throw new RuntimeException("模板JSON格式错误:" + e.getMessage());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 根据公司ID和页面类型查询运行态模板
|
|
|
|
|
+ * 返回给前端时过滤掉组件实例ID
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public JSONObject getCompanyRuntimeTemplate(Long companyId, String pageType) {
|
|
|
|
|
+ if (companyId == null) {
|
|
|
|
|
+ throw new RuntimeException("公司ID不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (pageType == null || pageType.trim().isEmpty()) {
|
|
|
|
|
+ pageType = "home";
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 1. 查询公司绑定的模板
|
|
|
|
|
+ DecorationCompanyTemplate bind =
|
|
|
|
|
+ decorationCompanyTemplateMapper.selectByCompanyAndTemplateType(companyId, pageType);
|
|
|
|
|
+
|
|
|
|
|
+ if (bind == null) {
|
|
|
|
|
+ throw new RuntimeException("当前公司未绑定模板");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 查询模板详情
|
|
|
|
|
+ DecorationTemplate template = decorationTemplateMapper.selectById(bind.getTemplateId());
|
|
|
|
|
+
|
|
|
|
|
+ if (template == null) {
|
|
|
|
|
+ throw new RuntimeException("模板不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (template.getStatus() == null || template.getStatus() != 1) {
|
|
|
|
|
+ throw new RuntimeException("模板未启用");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 解析模板JSON
|
|
|
|
|
+ JSONObject sourceJson = JSON.parseObject(template.getTemplateData());
|
|
|
|
|
+
|
|
|
|
|
+ JSONObject pageConfig = sourceJson.getJSONObject("pageConfig");
|
|
|
|
|
+ JSONArray sourceComponents = sourceJson.getJSONArray("components");
|
|
|
|
|
+
|
|
|
|
|
+ JSONArray targetComponents = new JSONArray();
|
|
|
|
|
+
|
|
|
|
|
+ if (sourceComponents != null) {
|
|
|
|
|
+ for (int i = 0; i < sourceComponents.size(); i++) {
|
|
|
|
|
+ JSONObject sourceItem = sourceComponents.getJSONObject(i);
|
|
|
|
|
+
|
|
|
|
|
+ JSONObject targetItem = new JSONObject();
|
|
|
|
|
+
|
|
|
|
|
+ // 只返回前端运行需要的字段,不返回 id
|
|
|
|
|
+ targetItem.put("componentCode", sourceItem.getString("componentCode"));
|
|
|
|
|
+
|
|
|
|
|
+ // props 可能为空,给默认 {}
|
|
|
|
|
+ JSONObject props = sourceItem.getJSONObject("props");
|
|
|
|
|
+ targetItem.put("props", props == null ? new JSONObject() : props);
|
|
|
|
|
+
|
|
|
|
|
+ targetComponents.add(targetItem);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 4. 组装返回
|
|
|
|
|
+ JSONObject result = new JSONObject();
|
|
|
|
|
+ result.put("pageConfig", pageConfig == null ? new JSONObject() : pageConfig);
|
|
|
|
|
+ result.put("components", targetComponents);
|
|
|
|
|
+
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public JSONObject getRuntimeTemplate(String pageType) {
|
|
|
|
|
+ if (pageType == null || pageType.trim().isEmpty()) {
|
|
|
|
|
+ pageType = "home";
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 1. 查询启用的模板
|
|
|
|
|
+ DecorationTemplate template = decorationTemplateMapper.selectOne(new LambdaQueryWrapper<DecorationTemplate>().eq(DecorationTemplate::getStatus, 1)
|
|
|
|
|
+ .eq(DecorationTemplate::getTemplateType, pageType));
|
|
|
|
|
+
|
|
|
|
|
+ if (template == null) {
|
|
|
|
|
+ throw new RuntimeException("未找到启用的模板");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 解析模板JSON
|
|
|
|
|
+ JSONObject sourceJson = JSON.parseObject(template.getTemplateData());
|
|
|
|
|
+
|
|
|
|
|
+ JSONObject pageConfig = sourceJson.getJSONObject("pageConfig");
|
|
|
|
|
+ JSONArray sourceComponents = sourceJson.getJSONArray("components");
|
|
|
|
|
+
|
|
|
|
|
+ JSONArray targetComponents = new JSONArray();
|
|
|
|
|
+
|
|
|
|
|
+ if (sourceComponents != null) {
|
|
|
|
|
+ for (int i = 0; i < sourceComponents.size(); i++) {
|
|
|
|
|
+ JSONObject sourceItem = sourceComponents.getJSONObject(i);
|
|
|
|
|
+
|
|
|
|
|
+ JSONObject targetItem = new JSONObject();
|
|
|
|
|
+
|
|
|
|
|
+ // 只返回前端运行需要的字段,不返回 id
|
|
|
|
|
+ targetItem.put("componentCode", sourceItem.getString("componentCode"));
|
|
|
|
|
+
|
|
|
|
|
+ // props 可能为空,给默认 {}
|
|
|
|
|
+ JSONObject props = sourceItem.getJSONObject("props");
|
|
|
|
|
+ targetItem.put("props", props == null ? new JSONObject() : props);
|
|
|
|
|
+
|
|
|
|
|
+ targetComponents.add(targetItem);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 4. 组装返回
|
|
|
|
|
+ JSONObject result = new JSONObject();
|
|
|
|
|
+ result.put("pageConfig", pageConfig == null ? new JSONObject() : pageConfig);
|
|
|
|
|
+ result.put("components", targetComponents);
|
|
|
|
|
+
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public void enableTemplate(Long id) {
|
|
|
|
|
+
|
|
|
|
|
+ if (id == null) {
|
|
|
|
|
+ throw new RuntimeException("模板ID不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ DecorationTemplate template = decorationTemplateMapper.selectById(id);
|
|
|
|
|
+
|
|
|
|
|
+ if (template == null) {
|
|
|
|
|
+ throw new RuntimeException("模板不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String templateType =
|
|
|
|
|
+ template.getTemplateType();
|
|
|
|
|
+
|
|
|
|
|
+ if (templateType == null) {
|
|
|
|
|
+ throw new RuntimeException("模板类型不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 先关闭同类型模板
|
|
|
|
|
+ */
|
|
|
|
|
+ decorationTemplateMapper.disableByTemplateType(templateType);
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 再启用当前模板
|
|
|
|
|
+ */
|
|
|
|
|
+ decorationTemplateMapper.enableTemplate(id);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|