产品类型导出逻辑
private JSONObject dealDataType4Numerical(String specs) {
// float、int 类型转换
log.info("===>dealDataType4Numerical, {}", specs);
JSONObject specsObject = JSON.parseObject(specs);
BigDecimal min = new BigDecimal(specsObject.getString(MIN));
BigDecimal max = new BigDecimal(specsObject.getString(MAX));
specsObject.remove(MIN);
specsObject.remove(MAX);
System.out.println("min " + min + " max: " + max);
specsObject.put(RANGE, new ArrayList<>(Arrays.asList(min, max)));
log.info("===>dealDataType4Numerical, {}", specsObject);
return specsObject;
}
@ApiOperation(value = "优化,以中间表的ref来填充默认服务的参数列表产品类型下载压缩包内(内容为JSON ***.json 文件)", httpMethod = "GET", notes = "@author be_insighted, \n type: 0: SDK导出 1: 产品证书 2: 物模型")
@GetMapping(value = "/product/export/perf/byProductId", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public void exportProductInfoByProductIdAndTypePerf(HttpServletResponse response, @RequestParam(value = "productId") Long productId, @RequestParam(value = "type") Integer type) throws Exception {
//待下载文件名
//System.out.println("请求参数: " + productId + " ,导出类型: " + type);
if (productId == null || productId < 1) {
throw new IotBusinessException("参数异常!");
}
ProductsEntity product = productsService.selectById(productId);
if (product == null) {
throw new IotBusinessException("数据不存在!");
}
String productName = product.getName();
String exportJsonString = "";
String fileNameSuffix = "";
// type: 0: SDK导出 1: 产品证书 2: 产品物模型
if (type.equals(0)) {
// SDK 导出
fileNameSuffix = "_SDK";
throw new RuntimeException("此功能暂未开发!");
}
if (type.equals(1)) {
// 产品证书导出
fileNameSuffix = "_产品证书";
ProductCertificateDTO certificateDTO = new ProductCertificateDTO();
certificateDTO.setProductId(String.valueOf(productId)).setProductSecret(productId + "");
//log.info("产品证书导出: " + JSON.toJSON(certificateDTO));
exportJsonString = JSON.toJSONString(certificateDTO, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat);
}
if (type.equals(2)) {
QueryWrapper<ProductInnerIdentifierEntity> identifierWrapper = new QueryWrapper<>();
identifierWrapper.eq("product_id", productId);
List<ProductInnerIdentifierEntity> list = identifierDao.selectList(identifierWrapper);
if (CollectionUtils.isEmpty(list)) {
throw new IotBusinessException("脏数据,产品类型identifier表(标识符中间表)中无标识符记录");
}
Map<String, Integer> productIdAndIdentifier2RefMap = new HashMap<>(list.size());
list.forEach(a -> productIdAndIdentifier2RefMap.put((a.getProductId() + a.getIdentifier()), a.getRef()));
// 产品物模型
fileNameSuffix = "_产品物模型";
ProductExportDTO exportDTO = new ProductExportDTO();
exportDTO.setSchema("1.0");
// A、填充profile信息
ProductProfileDTO profile = new ProductProfileDTO();
profile.setProductId(String.valueOf(productId)).setDescription(product.getDescription());
exportDTO.setProfile(profile);
// B、填充properties信息
List<ProductPropertiesExportDTO> propertiesExportDTOS = dealProductProperties(productId, productIdAndIdentifier2RefMap);
exportDTO.setProperties(propertiesExportDTOS);
// C、填充services信息
List<ProductServicesExportDTO> servicesExportDTOS = dealProductServices(productId, productIdAndIdentifier2RefMap);
exportDTO.setServices(servicesExportDTOS);
// D、填充events信息
List<ProductEventsExportDTO> eventsExportDTOS = dealProductEvents(productId, productIdAndIdentifier2RefMap);
exportDTO.setEvents(eventsExportDTOS);
//System.err.println("产品类型导出: " + JSON.toJSON(exportDTO));
exportJsonString = JSON.toJSONString(exportDTO, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat);
}
// windows服务器可能只有C
StringBuilder tempFilePath = new StringBuilder("C:\\export").append(File.separator)
.append(IotDateUtils.getDatetime(new Date(), IotDateUtils.DEFAULT_DATE_FORMAT)).append(File.separator);
String fileName = productName + "_" + productId + fileNameSuffix + ".json";
String fileNameZip = productName + "_" + productId + fileNameSuffix + ".zip";
String filePath = tempFilePath.toString();
IotCreateJsonFileUtil.createJsonFile(exportJsonString, filePath, fileName);
new ZipFile(filePath + File.separator + fileName.substring(0, fileName.lastIndexOf(".")) + ".zip").addFile(filePath + File.separator + fileName);
/* try {
fileNameZip = new String(fileNameZip.getBytes("UTF-8"), "ISO-8859-1"); // 直接请求,中文名称正常
// fileNameZip = new String(fileNameZip.getBytes(Charset.defaultCharset()));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}*/
/**
* response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + URLEncoder.encode(fileName,"UTF-8")),
*/
response.setHeader("content-type", "multipart/form-data");
response.setContentType("application/force-download");
response.setContentType("application/binary;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileNameZip, "UTF-8"));
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
byte[] buff = new byte[1024];
//创建缓冲输入流
BufferedInputStream bis = null;
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
//这个路径为待下载文件的路径
filePath = filePath + fileName.substring(0, fileName.lastIndexOf(".")) + ".zip";
log.info("压缩文件路径: filePath = {}", filePath);
bis = new BufferedInputStream(new FileInputStream(new File(filePath)));
int read = bis.read(buff);
//通过while循环写入到指定了的文件夹中
while (read != -1) {
outputStream.write(buff, 0, buff.length);
outputStream.flush();
read = bis.read(buff);
}
} catch (IOException e) {
e.printStackTrace();
//出现异常返回给页面失败的信息
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Files.deleteIfExists(Paths.get(filePath + File.separator + fileName));
}