mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-18 09:35:29 +08:00
数据迁移
This commit is contained in:
parent
c9f291a898
commit
82f8d96908
@ -2,19 +2,24 @@ package com.luban.api;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
|
||||
import com.luban.api.dto.H5ProjectContentDTO;
|
||||
import com.luban.api.entity.H5Project;
|
||||
import com.luban.api.service.H5ProjectService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
@SpringBootTest
|
||||
@Slf4j
|
||||
public class ImportH5Project {
|
||||
|
||||
@Autowired
|
||||
@ -43,7 +48,7 @@ public class ImportH5Project {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void migrateTaobaoZhihuGaodingData() {
|
||||
public void migrateTaobaoZhihuGaodingData() throws JsonProcessingException {
|
||||
// 配置源数据源
|
||||
DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
|
||||
@ -63,6 +68,7 @@ public class ImportH5Project {
|
||||
List<Map<String, Object>> dataList = jdbcTemplate.queryForList(sql1);
|
||||
|
||||
System.out.println("从源数据表查询到 " + dataList.size() + " 条记录");
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
// 转换并保存数据到目标表
|
||||
List<H5Project> projects = new ArrayList<>();
|
||||
@ -81,6 +87,16 @@ public class ImportH5Project {
|
||||
obj1.get("title");//H5长页简约可爱点击打开盲盒拆盲盒产品促销
|
||||
obj1.get("group_ids");
|
||||
|
||||
if (h5ProjectService.exists(new LambdaQueryWrapper<H5Project>()
|
||||
.eq(H5Project::getId, obj1.getLong("id")))) {
|
||||
log.info("已存在 {}", obj1.getLong("id"));
|
||||
continue;
|
||||
}
|
||||
|
||||
if("101097300".equals(obj1.getStr("id"))){
|
||||
continue;
|
||||
}
|
||||
|
||||
// 查询源数据
|
||||
String sql12 = """
|
||||
SELECT id, 链接, page, 作者姓名, 点赞数, 答案, html, create_time
|
||||
@ -100,23 +116,53 @@ public class ImportH5Project {
|
||||
project.setType(obj1.getStr("type")); // 设置类型为知乎相关
|
||||
|
||||
|
||||
//TODO 写到这里了,明天继续
|
||||
project.setDescription("");
|
||||
// typeToLowerCase(obj2.getBeanList("layouts", JSONObject.class));
|
||||
|
||||
project.setContent("");
|
||||
project.setPreviewUrl(""); // 可以根据需要设置预览图URL
|
||||
project.setPreviewUrlWatermark(""); // 可以根据需要设置水印预览图URL
|
||||
project.setPreviewWidth(375); // 默认预览图宽度
|
||||
project.setPreviewHeight(667); // 默认预览图高度
|
||||
H5ProjectContentDTO ddd = null;
|
||||
try {
|
||||
ddd = objectMapper.readValue(obj2.getStr("content"), H5ProjectContentDTO.class);
|
||||
} catch (MismatchedInputException e) {
|
||||
throw e;
|
||||
}
|
||||
project.setContent(objectMapper.writeValueAsString(ddd));
|
||||
project.setContentUpdatedAt(obj2.getLong("content_updated_at"));
|
||||
project.setDescription(obj2.getStr("description"));
|
||||
project.setPreviewUrl(obj2.getByPath("preview.url",String.class)); // 可以根据需要设置预览图URL
|
||||
project.setPreviewUrlWatermark(obj2.getByPath("preview.url_watermark",String.class)); // 可以根据需要设置水印预览图URL
|
||||
project.setPreviewWidth(obj2.getByPath("preview.width",Integer.class)); // 默认预览图宽度
|
||||
project.setPreviewHeight(obj2.getByPath("preview.height",Integer.class)); // 默认预览图高度
|
||||
project.setStatus(1); // 设置为启用状态
|
||||
projects.add(project);
|
||||
log.info("开始保存 {}", project.getId());
|
||||
try {
|
||||
h5ProjectService.saveOrUpdate(project);
|
||||
}catch (Exception e){
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("开始保存 " + projects.size() + " 条记录到目标表");
|
||||
// 保存项目
|
||||
boolean result = h5ProjectService.saveBatchH5ProjectsWithContent(projects);
|
||||
System.out.println("数据迁移结果: " + result + ",共迁移 " + projects.size() + " 条数据");
|
||||
}
|
||||
|
||||
// 批量保存项目
|
||||
// boolean result = h5ProjectService.saveBatchH5ProjectsWithContent(projects);
|
||||
// System.out.println("数据迁移结果: " + result + ",共迁移 " + projects.size() + " 条数据");
|
||||
|
||||
/**
|
||||
*便利全部的元素:typeToLowerCase(content.getBeanList("layouts", JSONObject.class));
|
||||
* @param layouts
|
||||
*/
|
||||
private void typeToLowerCase(List<JSONObject> layouts) {
|
||||
for (JSONObject layout : layouts) {
|
||||
dg(layout.getBeanList("elements", JSONObject.class));
|
||||
}
|
||||
}
|
||||
private void dg(List<JSONObject> elements) {
|
||||
elements.forEach(item -> {
|
||||
item.set("type", item.getStr("type").toLowerCase(Locale.ROOT));
|
||||
if (item.getStr("type").equals("group")) {
|
||||
dg(item.getBeanList("elements", JSONObject.class));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user