@Override
@Transactional(rollbackFor = Exception.class)
public String reportBdjbxxInfo(JbxxRequest jbxxRequest) {
LambdaQueryWrapper<SfBdjbxx> queryWrapper=new LambdaQueryWrapper<>();
if (jbxxRequest.getXmmc() != null && !jbxxRequest.getXmmc().isEmpty()) {
queryWrapper.eq(SfBdjbxx::getXmmc, jbxxRequest.getXmmc());
}
if (jbxxRequest.getInstId() != null && !jbxxRequest.getInstId().isEmpty()) {
queryWrapper.eq(SfBdjbxx::getInstId, jbxxRequest.getInstId());
}
List<SfBdjbxx> sfBdjbxxes = mapper.selectList(queryWrapper);
if (!sfBdjbxxes.isEmpty()){
String token = "";
try {
String performedLogin = this.performLogin();
JSONObject jsonObject = JSONUtil.parseObj(performedLogin);
JSONObject data = jsonObject.getJSONObject("data");
if (data != null) {
token = data.getStr("token");
}else {
throw new BusinessException("获取登录接口token失败"+jsonObject.getJSONObject("msg"));
}
System.out.println(performedLogin);
}catch (Exception e){
throw new BusinessException("获取登录接口token失败"+e.getMessage());
}
SfBdjbxx sfBdjbxx = sfBdjbxxes.get(0);
try {
String sendPostRequest = this.sendPostRequest(reportPartUrl, sfBdjbxx, token);
if (sendPostRequest!=null){
JSONObject jsonObject = JSONUtil.parseObj(sendPostRequest);
JSONObject data = jsonObject.getJSONObject("data");
if (data != null) {
sfBdjbxx.setProjid(data.getStr("projId"));
sfBdjbxx.setProjnum(data.getStr("projNum"));
sfBdjbxx.setChangeid(data.getStr("changeId"));
sfBdjbxx.setSfsb("已上报");
mapper.updateById(sfBdjbxx);
System.out.println("=====上报数据成功["+data+"]");
}else {
throw new BusinessException("数据上报部接口失败"+sendPostRequest);
}
}
} catch (IOException e) {
throw new BusinessException("数据上报部接口失败"+e.getMessage());
}
}
return "上报数据部接口成功";
}
public String performLogin() throws IOException {
String encrypt = AESVueUtil.aesEncrypt(password);
HttpResponse response = HttpRequest.post(loginUrl)
.form("username", username)
.form("password", encrypt)
.form("tenantCode", tenantCode)
.form("isUserphoneCode", "0")
.form("captcha", "0")
.form("loginType", "0")
.execute();
if (response.getStatus() != 200) {
throw new IOException("获取部登录接口token失败" + response.getStatus());
}
String responseBody = response.body();
if (responseBody != null && !responseBody.isEmpty()) {
return responseBody;
}
return "获取接口登录token失败";
}
public String sendPostRequest(String url, SfBdjbxx sfBdjbxx, String token) throws IOException {
Map<String, Object> params = new HashMap<>();
String ssnylx = sfBdjbxx.getSsnylx();
ssnylx = ssnylx.replaceAll("[\\[\\]\"]", "");
List<String> ids = Arrays.asList(ssnylx.split(","));
String codes = ids.stream()
.map(ProjectCategoryEnum::getCodeById)
.filter(Objects::nonNull)
.collect(Collectors.joining(","));
params.put("projName", sfBdjbxx.getXmmc());
params.put("projOrgP", String.format("%-6s", sfBdjbxx.getShengjxzq()).replace(' ', '0'));
params.put("projOrgC", String.format("%-6s", sfBdjbxx.getSjxzq()).replace(' ', '0'));
params.put("projOrgCo", sfBdjbxx.getXjxzq());
params.put("changeStartDate", sfBdjbxx.getKssj());
params.put("changeEndDate", sfBdjbxx.getJssj());
params.put("changeCuruse",codes);
params.put("curuseName", sfBdjbxx.getXmflmc());
params.put("projOrgX", sfBdjbxx.getXzxzqmc());
params.put("projOrgXc",sfBdjbxx.getCunzhuang());
String jsonData = mapToJson(params);
System.out.println("=====获取基本消息表上报数据"+jsonData);
try {
HttpResponse response = HttpRequest.post(url)
.header("Content-Type", "application/json; charset=utf-8")
.header("token", token)
.header("User-Agent", "PostmanRuntime/7.30.0")
.header("Accept", "*/*")
.header("Connection", "keep-alive")
.body(jsonData)
.execute();
String responseString = response.body();
if (responseString != null && !responseString.isEmpty()) {
return responseString;
}
}catch (Exception e){
throw new BusinessException("数据上报请求部接口失败"+e.getMessage());
}
return null;
}
@Override
public String reportVector(String instId) throws IOException {
File tempFile = null;
try {
SfBdjbxx sfBdjbxx = mapper.selectById(instId);
if (sfBdjbxx == null) {
throw new RuntimeException(String.format("数据[%s]不存在", instId));
}
String changeid = sfBdjbxx.getChangeid();
String projid = sfBdjbxx.getProjid();
if (!"已上报".equals(sfBdjbxx.getSfsb()) || changeid == null || projid == null) {
throw new RuntimeException(String.format("数据[%s]请先上报,再上传矢量数据", instId));
}
InstTxtFileEntity instTxtFileEntity = instTxtFileMapper.selectOne(
new LambdaQueryWrapper<InstTxtFileEntity>()
.eq(InstTxtFileEntity::getInstId, instId)
.eq(InstTxtFileEntity::getTabType, 0)
.like(InstTxtFileEntity::getFileName, "-矢量数据.zip")
);
if (instTxtFileEntity == null || instTxtFileEntity.getFileUrl() == null) {
throw new RuntimeException("未找到对应的矢量压缩包信息");
}
String fileUrl = instTxtFileEntity.getFileUrl();
InputStream inputStream = fileService.getObject(fileUrl);
if (inputStream == null) {
throw new RuntimeException("无法从 MinIO 获取文件:" + fileUrl);
}
String performedLogin = this.performLogin();
JSONObject jsonObject = JSONUtil.parseObj(performedLogin);
JSONObject data = jsonObject.getJSONObject("data");
String token = (data != null) ? data.getStr("token") : null;
if (token == null || token.isEmpty()) {
throw new BusinessException("获取登录接口 token 失败:" + jsonObject.getStr("msg"));
}
tempFile = File.createTempFile("tempZipFile", ".zip");
try (FileOutputStream outputStream = new FileOutputStream(tempFile)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
} finally {
inputStream.close();
}
HttpResponse response = HttpRequest.post(vectorUrl)
.header("Content-Type", "multipart/form-data")
.header("token", token)
.header("User-Agent", "PostmanRuntime/7.30.0")
.form("projId", projid)
.form("changeId",changeid)
.form("file", tempFile)
.execute();
if (response.getStatus() == 200) {
return response.body();
} else {
throw new IOException("上传失败,状态码:" + response.getStatus() + ",响应内容:" + response.body());
}
} catch (Exception e) {
throw new IOException("获取图斑矢量上报数据失败:" + e.getMessage(), e);
} finally {
if (tempFile != null && tempFile.exists()) {
boolean deleted = tempFile.delete();
if (!deleted) {
System.err.println("临时文件删除失败:" + tempFile.getAbsolutePath());
}
}
}
}