1.国网附件上送代码更改

This commit is contained in:
wr
2023-10-26 14:43:33 +08:00
parent eeaa98f3be
commit b7a1697e67
3 changed files with 94 additions and 141 deletions

View File

@@ -89,7 +89,7 @@ public class SupvPushGwController extends BaseController {
@OperateInfo(info = LogEnum.BUSINESS_COMMON) @OperateInfo(info = LogEnum.BUSINESS_COMMON)
@ApiOperation("推送附件接口") @ApiOperation("推送附件接口")
@ApiImplicitParam(name = "busIds",value = "请求体",required = true) @ApiImplicitParam(name = "busIds",value = "请求体",required = true)
public HttpResult<String> pushFile(@RequestBody List<String> busIds) { public HttpResult<String> pushFile(@RequestBody List<String> busIds) throws IOException {
String methodDescribe = getMethodDescribe("pushFile"); String methodDescribe = getMethodDescribe("pushFile");
String s = supvPushGwService.pushFile(busIds); String s = supvPushGwService.pushFile(busIds);
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, s, methodDescribe); return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, s, methodDescribe);

View File

@@ -31,7 +31,7 @@ public interface SupvPushGwService {
* @author cdf * @author cdf
* @date 2023/6/28 * @date 2023/6/28
*/ */
String pushFile(List<String> busIds) ; String pushFile(List<String> busIds) throws IOException;
/** /**

View File

@@ -1,6 +1,7 @@
package com.njcn.process.service.impl; package com.njcn.process.service.impl;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
@@ -9,19 +10,15 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.alibaba.csb.sdk.*; import com.alibaba.csb.sdk.*;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.njcn.common.pojo.exception.BusinessException; import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.oss.utils.FileStorageUtil; import com.njcn.oss.utils.FileStorageUtil;
import com.njcn.process.enums.ProcessResponseEnum;
import com.njcn.process.mapper.SupvFileMapper;
import com.njcn.process.mapper.SupvPlanMapper; import com.njcn.process.mapper.SupvPlanMapper;
import com.njcn.process.mapper.SupvProblemMapper; import com.njcn.process.mapper.SupvProblemMapper;
import com.njcn.process.mapper.SupvReportMMapper; import com.njcn.process.mapper.SupvReportMMapper;
import com.njcn.process.pojo.param.SendParam; import com.njcn.process.pojo.param.SendParam;
import com.njcn.process.pojo.param.SupvPlanParam;
import com.njcn.process.pojo.po.*; import com.njcn.process.pojo.po.*;
import com.njcn.process.pojo.vo.gw.*; import com.njcn.process.pojo.vo.gw.*;
import com.njcn.process.service.*; import com.njcn.process.service.*;
@@ -36,16 +33,11 @@ import com.njcn.user.pojo.vo.PvTerminalTreeVO;
import com.njcn.web.utils.RestTemplateUtil; import com.njcn.web.utils.RestTemplateUtil;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@@ -339,7 +331,7 @@ public class SupvPushGwServiceImpl implements SupvPushGwService {
} }
@Override @Override
public String pushFile(List<String> busIds) { public String pushFile(List<String> busIds) throws IOException {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
LambdaQueryWrapper<SupvFile> lambdaQueryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<SupvFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.in(SupvFile::getUuid, busIds); lambdaQueryWrapper.in(SupvFile::getUuid, busIds);
@@ -347,9 +339,6 @@ public class SupvPushGwServiceImpl implements SupvPushGwService {
if (supvFiles.size() > 100) { if (supvFiles.size() > 100) {
throw new BusinessException("一次最多上送100条数据"); throw new BusinessException("一次最多上送100条数据");
} }
JSONArray objects = new JSONArray(Collections.singletonList(supvFiles));
String s = objects.toString();
log.info(Thread.currentThread().getName() + "获取返回体 推送附件接口:" + s + "结束----");
List<DictData> fileList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.file_type.getCode().trim()).getData(); List<DictData> fileList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.file_type.getCode().trim()).getData();
Map<String, DictData> mapFile = fileList.stream().collect(Collectors.toMap(DictData::getId, Function.identity())); Map<String, DictData> mapFile = fileList.stream().collect(Collectors.toMap(DictData::getId, Function.identity()));
DictData dictData ; DictData dictData ;
@@ -362,31 +351,19 @@ public class SupvPushGwServiceImpl implements SupvPushGwService {
stringBuilder.append("" + (i+1) + "行文件上送失败: 请检查上送附件类型是否正确;</br>"); stringBuilder.append("" + (i+1) + "行文件上送失败: 请检查上送附件类型是否正确;</br>");
continue; continue;
} }
log.info(Thread.currentThread().getName() + "获取返回体 推送附件接口:" + supvFiles.get(i) + "结束----");
Map<String, String> sendFile = sendFile(getUrl(4), supvFiles.get(i)); Map<String, String> sendFile = sendFile(getUrl(4), supvFiles.get(i));
log.info(Thread.currentThread().getName() + "获取返回体 总部提供附件接收接口,省公司调用此接口,完成附件上报响应结果:" + sendFile + "结束----"); log.info(Thread.currentThread().getName() + "获取返回体 总部提供附件接收接口,省公司调用此接口,完成附件上报响应结果:" + sendFile + "结束----");
if (sendFile.containsKey("succeed")) { if (sendFile.containsKey("succeed")) {
String succeed = sendFile.get("succeed"); supvFileService.update(new LambdaUpdateWrapper<SupvFile>()
if (succeed.indexOf("\\\"") != -1) { .eq(SupvFile::getUuid,supvFiles.get(i).getUuid())
succeed = succeed.replace("\\\"", "\""); .set(SupvFile::getIsUploadHead,1)
} );
Map map = JSON.parseObject(succeed, Map.class); stringBuilder.append("" + (i+1) + "行文件上送成功:成功数据" + sendFile.get("succeed") + "条;</br>");
String status = map.get("status").toString();
if ("000000".equals(status)) {
supvFileService.update(new LambdaUpdateWrapper<SupvFile>()
.eq(SupvFile::getUuid,supvFiles.get(i).getUuid())
.set(SupvFile::getIsUploadHead,1)
);
String result = map.get("result").toString();
Map mapCount = JSON.parseObject(result, Map.class);
String count = mapCount.get("count").toString();
stringBuilder.append("" + (i+1) + "行文件上送失败:成功数据" + count + "条;</br>");
} else {
String errors = map.get("errors").toString();
stringBuilder.append("" + (i+1) + "次行文件上送失败:" + status + "_" + errors+";</br>");
}
} else { } else {
stringBuilder.append("" + (i+1)+ "行文件上送失败:当前时间段国网上送请求过多,请稍后再试;</br>"); stringBuilder.append("" + (i+1) + "行文件上送失败:" + sendFile.get("error")+";</br>");
} }
} }
return stringBuilder.toString(); return stringBuilder.toString();
} }
@@ -707,129 +684,105 @@ public class SupvPushGwServiceImpl implements SupvPushGwService {
return token; return token;
} }
public Map<String, String> sendFile(String url, SupvFile supvFile) { public Map<String,String> sendFile(String url,SupvFile supvFile) throws IOException {
//获取文件服务器地址
Map<String,String> maps=new HashMap<>();
String path = supvFile.getFileUrl(); String path = supvFile.getFileUrl();
if (StrUtil.isBlank(path)) { if (StrUtil.isBlank(path)) {
throw new BusinessException("获取文件上传路径为空!请检查原始路径是否存在"); maps.put("error", "获取文件上传路径为空!请检查原始路径是否存在");
} return maps;
String attachmentName = supvFile.getAttachmentName();
if (StrUtil.isNotBlank(attachmentName)) {
int i = attachmentName.lastIndexOf(".");
if (i != -1) {
attachmentName = attachmentName.substring(0, i);
}
} else {
throw new BusinessException("不存在文件,文件名称");
} }
InputStream fileStream = fileStorageUtil.getFileStream(path); InputStream in = fileStorageUtil.getFileStream(path);
if (ObjectUtil.isNull(fileStream)) { if (ObjectUtil.isNull(in)) {
throw new BusinessException("文件服务器,文件不存在"); maps.put("error", "文件服务器,文件不存在");
return maps;
} }
Map<String, String> map = new LinkedHashMap<>();
//ContentBody传递要求使用post方式进行调用
//如果需要传递请求参数 可以拼接到请求URL中或者设置paramsMap参数由SDK内部进行拼接
HttpParameters.Builder builder = HttpParameters.newBuilder();
builder.requestURL(url) // 设置请求的URL,可以拼接URL请求参数
.api("zongbuSync") // 设置服务名
.version("1.0.0") // 设置版本号
.method("post") // 设置调用方式, 必须为 post
.contentType("application/x-www-form-urlencoded;charset=utf-8") //设置请求content-type
.accessKey("7d4cb2c0afb5468ca56e0654b1a442ef").secretKey("lW2xr6zKjbaqVDOSgQpcGrM6Rg0="); // 设置accessKey 和 设置secretKey
String token = LoginToken();
log.info(Thread.currentThread().getName() + "3.错误信息:" + token);
builder.putHeaderParamsMap("x-token", token);
builder.putHeaderParamsMap("serviceName", "pqFileCreate");
Map<String,String> otherParams = new HashMap<>();
otherParams.put("provinceId",code );
otherParams.put("uuid", supvFile.getUuid());
otherParams.put("attachmentName",supvFile.getAttachmentName());
otherParams.put("attachmentType",supvFile.getAttachmentType());
otherParams.put("busiId", supvFile.getBusiId());
otherParams.put("uploaderName",supvFile.getUploaderName());
String uploadTime = supvFile.getUploadTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); String uploadTime = supvFile.getUploadTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
// 设置form请求参数 otherParams.put("uploadTime",uploadTime);
builder.putParamsMap("uuid", supvFile.getUuid()) otherParams.put("uploaderId",supvFile.getUploaderId());
.putParamsMap("provinceId", code)
.putParamsMap("attachmentName", supvFile.getAttachmentName())
.putParamsMap("attachmentType", supvFile.getAttachmentType())
.putParamsMap("busiId", supvFile.getBusiId())
.putParamsMap("uploaderName", supvFile.getUploaderName())
.putParamsMap("uploadTime", uploadTime)
.putParamsMap("uploaderId", supvFile.getUploaderId());
//设置上传文件
builder.addAttachFile("file", attachmentName, fileStream);
//进行调用,返回结果 log.info("文件上报接口发送:{}",JSONObject.toJSONString(otherParams));
try { byte[] bytes = IOUtils.toByteArray(in);
HttpReturn ret = HttpCaller.invokeReturn(builder.build()); String s = Base64.encodeStr(bytes, false, false);
String responseStr = ret.getResponseStr();//获取响应的文本串
map.put("succeed", responseStr); int m = 1;
} catch (HttpCallerException e) { String str="";
// error process for (int i = 0; i < s.length(); i += 1365000) {
log.info(Thread.currentThread().getName() + "错误信息:" + e); otherParams.put("fileByte", s.substring(i, Math.min(i + 1365000, s.length())));
map.put("error", e.toString()); otherParams.put("fileCount", (int) Math.ceil(s.length() /(1365000 * 1.0)) + "");
otherParams.put("fileCurrentNum",m + "");
str= requestCommon("pqFileFromHyCreate", url, otherParams);
m++;
} }
return map; if(StrUtil.isNotBlank(str)) {
String[] split = str.split("_");
if("succeed".equals(split[0])){
maps.put("succeed", "000000");
}else{
maps.put("error", split[1]);
}
}else {
maps.put("error", "国网文件上传失败");
}
in.close();
return maps;
} }
/** /**
* 文件上送提交 * 文件上传请求
* * @param serviceName
* @param url * @param url
* @param token * @param param
* @return * @return
*/ */
public Map postFileUrl(String url, String token, SupvFile supvFile) throws IOException { public String requestCommon(String serviceName, String url, Map<String, String> param) {
Map body = new HashMap(); String str;
Map<String, String> mapHeader = new HashMap<>(); HttpParameters.Builder builder = new HttpParameters.Builder();
mapHeader.put("Content-Type", "multipart/form-data"); String token = LoginToken();
String path = supvFile.getFileUrl();
if (StrUtil.isBlank(path)) { builder.putHeaderParamsMap("x-token", token);
throw new BusinessException("获取文件上传路径为空!请检查原始路径是否存在"); builder.requestURL(url) // 设置请求的URL,可以拼接URL请求参数
} .api("zongbuSync") // 设置服务名
String suffix = path.substring(path.lastIndexOf(".")); .version("1.0.0") // 设置版本号
String attachmentName = supvFile.getAttachmentName(); .method("post") // 设置调用方式, 必须为 post
if (StrUtil.isNotBlank(attachmentName)) { .accessKey("7d4cb2c0afb5468ca56e0654b1a442ef")
int i = attachmentName.lastIndexOf("."); .secretKey("lW2xr6zKjbaqVDOSgQpcGrM6Rg0=") // 设置accessKey 和 设置secretKey
if (i != -1) { .contentType("application/x-www-form-urlencoded;charset=utf-8") //设置请求content-type
attachmentName = attachmentName.substring(0, i); .putHeaderParamsMap("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
.putHeaderParamsMap("serviceName", serviceName)
.putParamsMapAll(param);
try {
HttpReturn ret = HttpCaller.invokeReturn(builder.build());
String responseStr = ret.getResponseStr();//获取响应的文本串
log.info(Thread.currentThread().getName() + "附件返回响应:" + responseStr);
if (responseStr.indexOf("\\\"") != -1) {
responseStr = responseStr.replace("\\\"", "\"");
} }
} else { Map map = JSON.parseObject(responseStr, Map.class);
throw new BusinessException("不存在文件,文件名称"); String status = map.get("status").toString();
String errors = map.get("errors").toString();
if ("000000".equals(status)) {
str="succeed_"+errors;
}else{
str="error_"+errors;
}
} catch (HttpCallerException e) {
// error process
log.info(Thread.currentThread().getName() + "附件错误信息:" + e);
str="error_"+"国网附件服务器内部错误";
} }
return str;
InputStream fileStream = fileStorageUtil.getFileStream(path);
if (ObjectUtil.isNull(fileStream)) {
throw new BusinessException("文件服务器,文件不存在");
}
byte[] fileBytes = IOUtils.toByteArray(fileStream);
File files = File.createTempFile(attachmentName, suffix);
FileOutputStream stream = new FileOutputStream(files);
stream.write(fileBytes);
stream.close();
FileSystemResource resource = new FileSystemResource(files);
MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
param.add("uuid", supvFile.getUuid());
param.add("file", resource);
param.add("attachmentName", supvFile.getAttachmentName());
param.add("attachmentType", supvFile.getAttachmentType());
param.add("busiId", supvFile.getBusiId());
param.add("uploaderName", supvFile.getUploaderName());
String uploadTime = supvFile.getUploadTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
param.add("uploadTime", uploadTime);
param.add("uploaderId", supvFile.getUploaderId());
ResponseEntity<Map> userEntity = RestTemplateUtil.post(url, mapHeader, param, Map.class, new ArrayList<>());
files.deleteOnExit();
if (userEntity.getStatusCodeValue() == 200) {
//获取返回体
body = userEntity.getBody();
log.info(Thread.currentThread().getName() + "获取返回体 getResourceData" + body + "结束----");
}
return body;
} }
/** /**
@@ -864,7 +817,7 @@ public class SupvPushGwServiceImpl implements SupvPushGwService {
/** /**
* 总部提供附件接收接口,省公司调用此接口,完成附件上报 * 总部提供附件接收接口,省公司调用此接口,完成附件上报
*/ */
url += "/WMCenter/powerQuality/file/create"; url += "/WMCenter/powerQuality/file/createFromHy";
break; break;
case 5: case 5:
/** /**