|
@@ -2,13 +2,19 @@ package com.persagy.proxy.migration.controller;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.http.HttpException;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
|
import com.alibaba.excel.ExcelWriter;
|
|
|
import com.alibaba.excel.write.metadata.WriteSheet;
|
|
|
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
|
|
+import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
import com.persagy.dmp.basic.model.QueryCriteria;
|
|
|
import com.persagy.dmp.common.constant.ResponseCode;
|
|
|
import com.persagy.dmp.common.exception.BusinessException;
|
|
|
import com.persagy.dmp.common.model.response.CommonResult;
|
|
|
+import com.persagy.dmp.common.utils.JsonHelper;
|
|
|
import com.persagy.dmp.migrate.client.DigitalMigrateLogFacade;
|
|
|
import com.persagy.dmp.rwd.migrate.entity.SyncData;
|
|
|
import com.persagy.proxy.adm.request.AdmQueryCriteria;
|
|
@@ -17,6 +23,8 @@ import com.persagy.proxy.adm.utils.AdmContextUtil;
|
|
|
import com.persagy.proxy.adm.utils.AdmQueryCriteriaHelper;
|
|
|
import com.persagy.proxy.common.entity.DmpResult;
|
|
|
import com.persagy.proxy.common.entity.InstanceUrlParam;
|
|
|
+import com.persagy.proxy.migration.constant.MigrationConstant;
|
|
|
+import com.persagy.proxy.migration.constant.MigrationObjType;
|
|
|
import com.persagy.proxy.migration.handler.DataMigrationHandler;
|
|
|
import com.persagy.proxy.migration.model.DataMigrationExcel;
|
|
|
import com.persagy.proxy.migration.model.MigrationInfo;
|
|
@@ -27,8 +35,10 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
@@ -59,24 +69,21 @@ public class DataMigrationController {
|
|
|
public void migrateForExcel(HttpServletResponse response, @RequestBody MigrationInfo migrationInfo) {
|
|
|
//调用服务迁移数据,返回处理结果excel
|
|
|
try {
|
|
|
- InstanceUrlParam context = AdmContextUtil.toDmpContext();
|
|
|
- context.setGroupCode(migrationInfo.getTargetGroupCode());
|
|
|
-
|
|
|
response.setContentType("application/vnd.ms-excel");
|
|
|
String encode = StandardCharsets.UTF_8.name();
|
|
|
response.setCharacterEncoding(encode);
|
|
|
- if(migrationInfo == null || StrUtil.isBlank(migrationInfo.getTargetUrl())){
|
|
|
+ if (migrationInfo == null || StrUtil.isBlank(migrationInfo.getTargetUrl())) {
|
|
|
throw new BusinessException(ResponseCode.A0402.getCode(), ResponseCode.A0402.getDesc());
|
|
|
}
|
|
|
- if(StrUtil.isBlank(migrationInfo.getTargetGroupCode())){
|
|
|
- migrationInfo.setTargetGroupCode(context.getGroupCode());
|
|
|
- }
|
|
|
- if(StrUtil.isBlank(migrationInfo.getTargetProjectId())){
|
|
|
- migrationInfo.setTargetProjectId(context.getProjectId());
|
|
|
+ InstanceUrlParam context = AdmContextUtil.toDmpContext();
|
|
|
+ //确定集团编码 和 项目id
|
|
|
+ ensureParamValue(migrationInfo, context);
|
|
|
+ //验证目标项目地址是否正确
|
|
|
+ boolean valid = validTargetUrl(migrationInfo, context);
|
|
|
+ if (!valid) {
|
|
|
+ getErrorExcel(response);
|
|
|
}
|
|
|
|
|
|
- Map<String,List<DataMigrationExcel>> results = dataMigrationHandler.handleAndGetExcelList(context, migrationInfo);
|
|
|
-
|
|
|
AdmQueryCriteria projectRequest = new AdmQueryCriteria();
|
|
|
projectRequest.setName(AdmProject.OBJ_TYPE);
|
|
|
projectRequest.setFilters("id = '" + context.getProjectId() + "';");
|
|
@@ -92,8 +99,9 @@ public class DataMigrationController {
|
|
|
fileName = URLEncoder.encode(fileName, encode);
|
|
|
|
|
|
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
|
|
|
+ Map<String, List<DataMigrationExcel>> results = dataMigrationHandler.handleAndGetExcelList(context, migrationInfo);
|
|
|
//每个迁移的表写成一个sheet
|
|
|
- if(CollUtil.isNotEmpty(results)){
|
|
|
+ if (CollUtil.isNotEmpty(results)) {
|
|
|
int sheetNo = 0;
|
|
|
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), DataMigrationExcel.class).build();
|
|
|
try {
|
|
@@ -110,23 +118,23 @@ public class DataMigrationController {
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- log.error("数据迁移内容获取失败",e);
|
|
|
+ getErrorExcel(response);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@PostMapping("/start")
|
|
|
public AdmResponse migrateForLog(@RequestBody MigrationInfo migrationInfo) {
|
|
|
InstanceUrlParam context = AdmContextUtil.toDmpContext();
|
|
|
- context.setGroupCode(migrationInfo.getTargetGroupCode());
|
|
|
|
|
|
if(migrationInfo == null || StrUtil.isBlank(migrationInfo.getTargetUrl())){
|
|
|
throw new BusinessException(ResponseCode.A0402.getCode(), ResponseCode.A0402.getDesc());
|
|
|
}
|
|
|
- if(StrUtil.isBlank(migrationInfo.getTargetGroupCode())){
|
|
|
- migrationInfo.setTargetGroupCode(context.getGroupCode());
|
|
|
- }
|
|
|
- if(StrUtil.isBlank(migrationInfo.getTargetProjectId())){
|
|
|
- migrationInfo.setTargetProjectId(context.getProjectId());
|
|
|
+ //确定集团编码 和 项目id
|
|
|
+ ensureParamValue(migrationInfo, context);
|
|
|
+ //验证目标项目地址是否正确
|
|
|
+ boolean valid = validTargetUrl(migrationInfo, context);
|
|
|
+ if(!valid){
|
|
|
+ return AdmResponse.failure(MigrationConstant.ERROR);
|
|
|
}
|
|
|
dataMigrationHandler.handleAndSaveLog(context, migrationInfo);
|
|
|
return AdmResponse.success();
|
|
@@ -157,4 +165,76 @@ public class DataMigrationController {
|
|
|
return AdmResponse.failure(result.getMessage());
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ private void ensureParamValue(MigrationInfo migrationInfo, InstanceUrlParam context){
|
|
|
+ if(migrationInfo.getTargetSign() == 0){
|
|
|
+ migrationInfo.setTargetGroupCode(context.getGroupCode());
|
|
|
+ migrationInfo.setTargetProjectId(context.getProjectId());
|
|
|
+ }
|
|
|
+ if(migrationInfo.getTargetSign() == 1){
|
|
|
+ if(StrUtil.isBlank(migrationInfo.getTargetGroupCode())){
|
|
|
+ migrationInfo.setTargetGroupCode(context.getGroupCode());
|
|
|
+ }
|
|
|
+ if(StrUtil.isBlank(migrationInfo.getTargetProjectId())){
|
|
|
+ migrationInfo.setTargetProjectId(context.getProjectId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean validTargetUrl(MigrationInfo migrationInfo, InstanceUrlParam context){
|
|
|
+
|
|
|
+ StringBuffer url = new StringBuffer(migrationInfo.getTargetUrl() + MigrationConstant.DT_OBJECT);
|
|
|
+ url.append("groupCode=").append(migrationInfo.getTargetGroupCode());
|
|
|
+ url.append("&projectId=").append(migrationInfo.getTargetProjectId());
|
|
|
+ if(StrUtil.isNotEmpty(context.getUserId())){
|
|
|
+ url.append("&userId=").append(context.getUserId());
|
|
|
+ }
|
|
|
+ if(StrUtil.isNotEmpty(context.getAppId())) {
|
|
|
+ url.append("&appId=").append(context.getAppId());
|
|
|
+ }
|
|
|
+ QueryCriteria queryCriteria = new QueryCriteria();
|
|
|
+ queryCriteria.setOnlyCount(true);
|
|
|
+ ObjectNode criteria = JsonNodeFactory.instance.objectNode();
|
|
|
+ criteria.put("valid", 1);
|
|
|
+ criteria.put("objType", MigrationObjType.project.getCode());
|
|
|
+ queryCriteria.setCriteria(criteria);
|
|
|
+
|
|
|
+ try{
|
|
|
+ String requestBody = null;
|
|
|
+ try {
|
|
|
+ requestBody = JsonHelper.toJsonStr(queryCriteria);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("参数转化失败(QueryCriteria -> jsonString)"+ e.getMessage());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ String response = HttpUtil.post(url.toString(), requestBody, 5000);
|
|
|
+ CommonResult<List> commonResult = JSONUtil.toBean(response, CommonResult.class);
|
|
|
+ if(commonResult.getResult().equals(DmpResult.SUCCESS) && commonResult.getCount() == 1 ){
|
|
|
+ return true;
|
|
|
+ }else {
|
|
|
+ log.error(commonResult.getMessage());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ }catch (HttpException e){
|
|
|
+ log.error(ResponseCode.C0001.getDesc()+",url:"+ url);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void getErrorExcel(HttpServletResponse response){
|
|
|
+ List<DataMigrationExcel> dataMigrationExcels = new ArrayList<>(1);
|
|
|
+ DataMigrationExcel dataMigrationExcel = new DataMigrationExcel();
|
|
|
+ dataMigrationExcel.setId("error");
|
|
|
+ dataMigrationExcel.setMark(MigrationConstant.ERROR);
|
|
|
+ dataMigrationExcels.add(dataMigrationExcel);
|
|
|
+ response.setHeader("Content-disposition", "attachment;filename=error.xlsx");
|
|
|
+ try {
|
|
|
+ EasyExcel.write(response.getOutputStream(), DataMigrationExcel.class).sheet("sheet1").doWrite(dataMigrationExcels);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|