diff --git a/pqs-process/process-boot/src/main/java/com/njcn/process/controller/SupvPushGwController.java b/pqs-process/process-boot/src/main/java/com/njcn/process/controller/SupvPushGwController.java index 944bed02d..492a877bb 100644 --- a/pqs-process/process-boot/src/main/java/com/njcn/process/controller/SupvPushGwController.java +++ b/pqs-process/process-boot/src/main/java/com/njcn/process/controller/SupvPushGwController.java @@ -89,7 +89,7 @@ public class SupvPushGwController extends BaseController { @OperateInfo(info = LogEnum.BUSINESS_COMMON) @ApiOperation("推送附件接口") @ApiImplicitParam(name = "busIds",value = "请求体",required = true) - public HttpResult pushFile(@RequestBody List busIds) { + public HttpResult pushFile(@RequestBody List busIds) throws IOException { String methodDescribe = getMethodDescribe("pushFile"); String s = supvPushGwService.pushFile(busIds); return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, s, methodDescribe); diff --git a/pqs-process/process-boot/src/main/java/com/njcn/process/service/SupvPushGwService.java b/pqs-process/process-boot/src/main/java/com/njcn/process/service/SupvPushGwService.java index f64b6bf7a..c4c62b008 100644 --- a/pqs-process/process-boot/src/main/java/com/njcn/process/service/SupvPushGwService.java +++ b/pqs-process/process-boot/src/main/java/com/njcn/process/service/SupvPushGwService.java @@ -31,7 +31,7 @@ public interface SupvPushGwService { * @author cdf * @date 2023/6/28 */ - String pushFile(List busIds) ; + String pushFile(List busIds) throws IOException; /** diff --git a/pqs-process/process-boot/src/main/java/com/njcn/process/service/impl/SupvPushGwServiceImpl.java b/pqs-process/process-boot/src/main/java/com/njcn/process/service/impl/SupvPushGwServiceImpl.java index 11784ae76..79f8d210a 100644 --- a/pqs-process/process-boot/src/main/java/com/njcn/process/service/impl/SupvPushGwServiceImpl.java +++ b/pqs-process/process-boot/src/main/java/com/njcn/process/service/impl/SupvPushGwServiceImpl.java @@ -1,6 +1,7 @@ package com.njcn.process.service.impl; import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.codec.Base64; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; @@ -9,19 +10,15 @@ import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.csb.sdk.*; import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.njcn.common.pojo.exception.BusinessException; 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.SupvProblemMapper; import com.njcn.process.mapper.SupvReportMMapper; 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.vo.gw.*; import com.njcn.process.service.*; @@ -36,16 +33,11 @@ import com.njcn.user.pojo.vo.PvTerminalTreeVO; import com.njcn.web.utils.RestTemplateUtil; import lombok.RequiredArgsConstructor; 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.core.io.FileSystemResource; import org.springframework.http.ResponseEntity; 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.InputStream; import java.time.LocalDateTime; @@ -339,7 +331,7 @@ public class SupvPushGwServiceImpl implements SupvPushGwService { } @Override - public String pushFile(List busIds) { + public String pushFile(List busIds) throws IOException { StringBuilder stringBuilder = new StringBuilder(); LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.in(SupvFile::getUuid, busIds); @@ -347,9 +339,6 @@ public class SupvPushGwServiceImpl implements SupvPushGwService { if (supvFiles.size() > 100) { throw new BusinessException("一次最多上送100条数据"); } - JSONArray objects = new JSONArray(Collections.singletonList(supvFiles)); - String s = objects.toString(); - log.info(Thread.currentThread().getName() + "获取返回体 推送附件接口:" + s + "结束----!"); List fileList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.file_type.getCode().trim()).getData(); Map mapFile = fileList.stream().collect(Collectors.toMap(DictData::getId, Function.identity())); DictData dictData ; @@ -362,31 +351,19 @@ public class SupvPushGwServiceImpl implements SupvPushGwService { stringBuilder.append("第" + (i+1) + "行文件上送失败: 请检查上送附件类型是否正确;
"); continue; } + log.info(Thread.currentThread().getName() + "获取返回体 推送附件接口:" + supvFiles.get(i) + "结束----!"); Map sendFile = sendFile(getUrl(4), supvFiles.get(i)); log.info(Thread.currentThread().getName() + "获取返回体 总部提供附件接收接口,省公司调用此接口,完成附件上报响应结果:" + sendFile + "结束----!"); if (sendFile.containsKey("succeed")) { - String succeed = sendFile.get("succeed"); - if (succeed.indexOf("\\\"") != -1) { - succeed = succeed.replace("\\\"", "\""); - } - Map map = JSON.parseObject(succeed, Map.class); - String status = map.get("status").toString(); - if ("000000".equals(status)) { - supvFileService.update(new LambdaUpdateWrapper() - .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 + "条;
"); - } else { - String errors = map.get("errors").toString(); - stringBuilder.append("第" + (i+1) + "次行文件上送失败:" + status + "_" + errors+";
"); - } + supvFileService.update(new LambdaUpdateWrapper() + .eq(SupvFile::getUuid,supvFiles.get(i).getUuid()) + .set(SupvFile::getIsUploadHead,1) + ); + stringBuilder.append("第" + (i+1) + "行文件上送成功:成功数据" + sendFile.get("succeed") + "条;
"); } else { - stringBuilder.append("第" + (i+1)+ "行文件上送失败:当前时间段国网上送请求过多,请稍后再试;
"); + stringBuilder.append("第" + (i+1) + "次行文件上送失败:" + sendFile.get("error")+";
"); } + } return stringBuilder.toString(); } @@ -707,129 +684,105 @@ public class SupvPushGwServiceImpl implements SupvPushGwService { return token; } - public Map sendFile(String url, SupvFile supvFile) { + public Map sendFile(String url,SupvFile supvFile) throws IOException { + //获取文件服务器地址 + Map maps=new HashMap<>(); String path = supvFile.getFileUrl(); if (StrUtil.isBlank(path)) { - throw new BusinessException("获取文件上传路径为空!请检查原始路径是否存在"); - } - String attachmentName = supvFile.getAttachmentName(); - if (StrUtil.isNotBlank(attachmentName)) { - int i = attachmentName.lastIndexOf("."); - if (i != -1) { - attachmentName = attachmentName.substring(0, i); - } - } else { - throw new BusinessException("不存在文件,文件名称"); + maps.put("error", "获取文件上传路径为空!请检查原始路径是否存在"); + return maps; } - InputStream fileStream = fileStorageUtil.getFileStream(path); - if (ObjectUtil.isNull(fileStream)) { - throw new BusinessException("文件服务器,文件不存在"); + InputStream in = fileStorageUtil.getFileStream(path); + if (ObjectUtil.isNull(in)) { + maps.put("error", "文件服务器,文件不存在"); + return maps; } - Map 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 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")); - // 设置form请求参数 - builder.putParamsMap("uuid", supvFile.getUuid()) - .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); + otherParams.put("uploadTime",uploadTime); + otherParams.put("uploaderId",supvFile.getUploaderId()); - //进行调用,返回结果 - try { - HttpReturn ret = HttpCaller.invokeReturn(builder.build()); - String responseStr = ret.getResponseStr();//获取响应的文本串 - map.put("succeed", responseStr); - } catch (HttpCallerException e) { - // error process - log.info(Thread.currentThread().getName() + "错误信息:" + e); - map.put("error", e.toString()); + log.info("文件上报接口发送:{}",JSONObject.toJSONString(otherParams)); + byte[] bytes = IOUtils.toByteArray(in); + String s = Base64.encodeStr(bytes, false, false); + + int m = 1; + String str=""; + for (int i = 0; i < s.length(); i += 1365000) { + otherParams.put("fileByte", s.substring(i, Math.min(i + 1365000, s.length()))); + 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 token + * @param param * @return */ - public Map postFileUrl(String url, String token, SupvFile supvFile) throws IOException { - Map body = new HashMap(); - Map mapHeader = new HashMap<>(); - mapHeader.put("Content-Type", "multipart/form-data"); - String path = supvFile.getFileUrl(); - if (StrUtil.isBlank(path)) { - throw new BusinessException("获取文件上传路径为空!请检查原始路径是否存在"); - } - String suffix = path.substring(path.lastIndexOf(".")); - String attachmentName = supvFile.getAttachmentName(); - if (StrUtil.isNotBlank(attachmentName)) { - int i = attachmentName.lastIndexOf("."); - if (i != -1) { - attachmentName = attachmentName.substring(0, i); + public String requestCommon(String serviceName, String url, Map param) { + String str; + HttpParameters.Builder builder = new HttpParameters.Builder(); + String token = LoginToken(); + + builder.putHeaderParamsMap("x-token", token); + builder.requestURL(url) // 设置请求的URL,可以拼接URL请求参数 + .api("zongbuSync") // 设置服务名 + .version("1.0.0") // 设置版本号 + .method("post") // 设置调用方式, 必须为 post + .accessKey("7d4cb2c0afb5468ca56e0654b1a442ef") + .secretKey("lW2xr6zKjbaqVDOSgQpcGrM6Rg0=") // 设置accessKey 和 设置secretKey + .contentType("application/x-www-form-urlencoded;charset=utf-8") //设置请求content-type + .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 { - throw new BusinessException("不存在文件,文件名称"); + Map map = JSON.parseObject(responseStr, Map.class); + 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_"+"国网附件服务器内部错误"; } - - 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 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 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; + return str; } /** @@ -864,7 +817,7 @@ public class SupvPushGwServiceImpl implements SupvPushGwService { /** * 总部提供附件接收接口,省公司调用此接口,完成附件上报 */ - url += "/WMCenter/powerQuality/file/create"; + url += "/WMCenter/powerQuality/file/createFromHy"; break; case 5: /**