|
|
package com.dsideal.base.DataEase.Util;
|
|
|
|
|
|
import cn.hutool.core.lang.Snowflake;
|
|
|
import cn.hutool.core.util.IdUtil;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.dsideal.base.DataEase.Model.DataEaseModel;
|
|
|
import com.dsideal.base.Tools.Util.LocalMysqlConnectUtil;
|
|
|
import com.dsideal.base.Util.FileUtil;
|
|
|
import com.jfinal.kit.PathKit;
|
|
|
import com.jfinal.plugin.activerecord.Db;
|
|
|
import com.jfinal.plugin.activerecord.Record;
|
|
|
|
|
|
import java.util.List;
|
|
|
import java.util.Random;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
import static com.dsideal.base.DataEase.Model.DataEaseModel.DB_NAME;
|
|
|
|
|
|
public class CopyBigScreen {
|
|
|
//DataEase部署的地址
|
|
|
public static String urlPrefix = "http://10.10.14.203";
|
|
|
|
|
|
public static DataEaseModel dm = new DataEaseModel();
|
|
|
|
|
|
/**
|
|
|
* 功能:获取DataEase的Token
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
public static String getToken() {
|
|
|
//读取Data目录下DataEaseLogin.json文件
|
|
|
JSONObject jo = FileUtil.readJsonFile(PathKit.getRootClassPath() + "/Data/DataEaseLogin.json");
|
|
|
String url = urlPrefix + "/de2api/login/localLogin";
|
|
|
String res = HttpUtil.createPost(url).contentType("application/json").body(jo.toString()).execute().body();
|
|
|
return JSONObject.parseObject(res).getJSONObject("data").getString("token");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 利用雪花算法获取唯一ID
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
public static long getSnowId() {
|
|
|
Snowflake snowflake = IdUtil.createSnowflake(1, 1);
|
|
|
return snowflake.nextId();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 生成随机字符串
|
|
|
*
|
|
|
* @param len
|
|
|
* @return
|
|
|
*/
|
|
|
static String randomString(int len) {
|
|
|
String alphabetsInUpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
String alphabetsInLowerCase = "abcdefghijklmnopqrstuvwxyz";
|
|
|
String numbers = "0123456789";
|
|
|
String allCharacters = alphabetsInLowerCase + alphabetsInUpperCase + numbers;
|
|
|
StringBuilder randomString = new StringBuilder();
|
|
|
Random random = new Random();
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
int randomIndex = random.nextInt(allCharacters.length());
|
|
|
randomString.append(allCharacters.charAt(randomIndex));
|
|
|
}
|
|
|
return randomString.toString();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 清理通过本程序生成的所有副本,为了再次生成不重复
|
|
|
*
|
|
|
* @param dataVisualizationName
|
|
|
*/
|
|
|
public static void clear(String dataVisualizationName) {
|
|
|
//一、先删除后插入保持健康:保留:【云南省教育决策支持系统【市州】】,删除掉云南省教育决策支持系统【市州】+昆明市,云南省教育决策支持系统【市州】+楚雄州等数据
|
|
|
String sql = "select * from data_visualization_info where name like '%" + dataVisualizationName + "%' and name <>'" + dataVisualizationName + "'";
|
|
|
List<Record> toDelList = Db.use(DB_NAME).find(sql);
|
|
|
for (Record record : toDelList) {
|
|
|
long id = record.getLong("id");
|
|
|
//(1) 删除关联的表core_chart_view
|
|
|
sql = "delete from core_chart_view where scene_id=?";
|
|
|
Db.use(DB_NAME).update(sql, id);
|
|
|
//(2) 删除共享链接表
|
|
|
sql = "delete from xpack_share WHERE resource_id=?";
|
|
|
Db.use(DB_NAME).update(sql, id);
|
|
|
//(3) 删除可视化资源表
|
|
|
sql = "delete from core_opt_recent where resource_id=?";
|
|
|
Db.use(DB_NAME).update(sql, id);
|
|
|
// (4) 跳转记录表
|
|
|
sql = "delete from visualization_link_jump where source_dv_id=?";
|
|
|
Db.use(DB_NAME).update(sql, id);
|
|
|
// (5) 跳转配置表
|
|
|
sql = "select * from visualization_link_jump_info where target_dv_id=?";
|
|
|
List<Record> jumpList = Db.use(DB_NAME).find(sql, id);
|
|
|
for (Record r : jumpList) {
|
|
|
long jumpId = r.getLong("id");
|
|
|
sql = "delete from visualization_link_jump_target_view_info where link_jump_info_id =?";
|
|
|
Db.use(DB_NAME).update(sql, jumpId);
|
|
|
sql = "delete from visualization_link_jump_info where id=?";
|
|
|
Db.use(DB_NAME).update(sql, jumpId);
|
|
|
}
|
|
|
// (6) 外部参数关联关系表
|
|
|
sql = "select params_id from visualization_outer_params where visualization_id=?";
|
|
|
String paramsId = Db.use(DB_NAME).queryStr(sql, id);
|
|
|
sql = "delete from visualization_outer_params_info where params_id=?";
|
|
|
Db.use(DB_NAME).update(sql, paramsId);
|
|
|
sql = "delete from visualization_outer_params where visualization_id=?";
|
|
|
Db.use(DB_NAME).update(sql, id);
|
|
|
//(7) 删除主表数据
|
|
|
Db.use(DB_NAME).deleteById("data_visualization_info", id);
|
|
|
}
|
|
|
System.out.println("成功删除大屏数据" + toDelList.size() + "条~");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 拷贝大屏
|
|
|
*
|
|
|
* @param dataVisualizationName
|
|
|
*/
|
|
|
public static void Copy(String dataVisualizationName) {
|
|
|
//清理掉旧的数据
|
|
|
clear(dataVisualizationName);
|
|
|
//母屏信息
|
|
|
String sql = "select * from data_visualization_info where name =?";
|
|
|
Record motherRecord = Db.use(DataEaseModel.DB_NAME).findFirst(sql, dataVisualizationName);
|
|
|
long motherId = motherRecord.getLong("id");//母屏ID
|
|
|
long pid = motherRecord.getLong("pid"); //隶属文件夹
|
|
|
//母屏共享链接
|
|
|
sql = "select * from xpack_share where resource_id=?";
|
|
|
Record motherShareRecord = Db.use(DataEaseModel.DB_NAME).findFirst(sql, motherId);
|
|
|
|
|
|
//按16个市州进行生成
|
|
|
for (String cityName : dm.getCityNameList()) {
|
|
|
String screenName = dataVisualizationName + cityName;//要拷贝出来的屏幕名称
|
|
|
//拷贝API
|
|
|
String url = urlPrefix + "/de2api/dataVisualization/copy";
|
|
|
JSONObject jo = new JSONObject();
|
|
|
jo.put("nodeType", "leaf");
|
|
|
jo.put("name", screenName);
|
|
|
jo.put("type", "dataV");
|
|
|
jo.put("id", motherId);
|
|
|
jo.put("pid", pid);
|
|
|
String res = HttpUtil.createPost(url).contentType("application/json")
|
|
|
.header("x-de-token", getToken()).body(jo.toString()).execute().body();
|
|
|
long childId = Long.parseLong(JSONObject.parseObject(res).getString("data"));
|
|
|
|
|
|
//更新pid,没有这步的话,在界面上看不到拷贝出来的大屏!
|
|
|
sql = "update data_visualization_info set pid=? where id=?";
|
|
|
Db.use(DB_NAME).update(sql, pid, childId);
|
|
|
|
|
|
// 修改地图中城市
|
|
|
List<Record> list = dm.getMap(childId);
|
|
|
//获取城市编码
|
|
|
String area_code = dm.getCityCode(cityName);
|
|
|
//core_chart_view表
|
|
|
for (Record record : list) {
|
|
|
long id = record.getLong("id");
|
|
|
jo = JSONObject.parseObject(record.getStr("custom_attr"));
|
|
|
//修改城市编码
|
|
|
jo.getJSONObject("map").put("id", area_code);
|
|
|
jo.getJSONObject("map").put("level", "city");
|
|
|
//写到数据库
|
|
|
String jsonString = jo.toString();
|
|
|
Db.use(DB_NAME).update("update core_chart_view set custom_attr=? where id=?", jsonString, id);
|
|
|
}
|
|
|
|
|
|
//修改外部参数
|
|
|
sql = "select * from visualization_outer_params where visualization_id =?";
|
|
|
Record motherOuterParamsRecord = Db.use(DB_NAME).findFirst(sql, motherId);
|
|
|
String motherParamsId = motherOuterParamsRecord.getStr("params_id");
|
|
|
Record cityOuterParamsRecord = new Record().setColumns(motherOuterParamsRecord);
|
|
|
cityOuterParamsRecord.set("params_id", UUID.randomUUID().toString());
|
|
|
cityOuterParamsRecord.set("visualization_id", childId);
|
|
|
Db.use(DB_NAME).save("visualization_outer_params", "params_id", cityOuterParamsRecord);
|
|
|
//复制外部参数关联关系表
|
|
|
sql = "select * from visualization_outer_params_info where params_id=?";
|
|
|
Record paramsInfoRecord = Db.use(DB_NAME).findFirst(sql, motherParamsId);
|
|
|
Record cityParamsInfoRecord = new Record().setColumns(paramsInfoRecord);
|
|
|
cityParamsInfoRecord.set("params_id", cityOuterParamsRecord.getStr("params_id"));
|
|
|
cityParamsInfoRecord.set("params_info_id", UUID.randomUUID().toString());
|
|
|
Db.use(DB_NAME).save("visualization_outer_params_info", "params_info_id", cityParamsInfoRecord);
|
|
|
|
|
|
//发布共享链接
|
|
|
Record shareRecord = new Record().setColumns(motherShareRecord);
|
|
|
shareRecord.set("resource_id", childId);
|
|
|
shareRecord.set("id", getSnowId());
|
|
|
shareRecord.set("uuid", randomString(8));//大小写字母和数字组合,长度为8
|
|
|
Db.use(DataEaseModel.DB_NAME).save("xpack_share", "id", shareRecord);
|
|
|
System.out.println(cityName + "修改完成");
|
|
|
}
|
|
|
System.out.println("恭喜,所有操作成功完成!");
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) throws InterruptedException {
|
|
|
LocalMysqlConnectUtil.Init();
|
|
|
//获取数据可视化名称
|
|
|
String dataVisualizationName = LocalMysqlConnectUtil.PropKit.get("dataEase.dataVisualizationName");
|
|
|
Copy(dataVisualizationName);
|
|
|
}
|
|
|
}
|