evoToK3Cloud/ruoyi-system/src/main/java/com/ruoyi/system/controller/MesDrawingController.java
2026-10-28 17:14:56 +08:00

113 lines
5.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.ruoyi.system.controller;
import cn.dev33.satoken.annotation.SaIgnore;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.vo.mes.MesDrawingListVo;
import com.ruoyi.system.service.IMesDrawingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
/**
* MES 图纸开放接口:按生产令号查询路径与下载地址,并按图号下载 PDF / DWG。
* <p>
* 鉴权:请求头 {@code X-App-Key} / {@code X-App-Secret},或 query 参数 {@code appKey} / {@code appSecret}。
*/
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/open/mes/drawing")
public class MesDrawingController extends BaseController {
private final IMesDrawingService mesDrawingService;
/**
* 按生产令号列出图纸。可选 materialCode 只返回该图号。
*/
@SaIgnore
@GetMapping("/list")
public R<MesDrawingListVo> list(
@RequestHeader(value = "X-App-Key", required = false) String headerAppKey,
@RequestHeader(value = "X-App-Secret", required = false) String headerAppSecret,
@RequestParam(value = "appKey", required = false) String appKey,
@RequestParam(value = "appSecret", required = false) String appSecret,
@RequestParam("productionOrderNo") String productionOrderNo,
@RequestParam(value = "materialCode", required = false) String materialCode) {
mesDrawingService.assertAuth(firstNonBlank(headerAppKey, appKey), firstNonBlank(headerAppSecret, appSecret));
return R.ok(mesDrawingService.listDrawings(productionOrderNo, materialCode));
}
/**
* 按生产令号 + 物料编码下载图纸。fileType:pdf(默认)或 dwg。
*/
@SaIgnore
@GetMapping("/download")
public void download(
@RequestHeader(value = "X-App-Key", required = false) String headerAppKey,
@RequestHeader(value = "X-App-Secret", required = false) String headerAppSecret,
@RequestParam(value = "appKey", required = false) String appKey,
@RequestParam(value = "appSecret", required = false) String appSecret,
@RequestParam("productionOrderNo") String productionOrderNo,
@RequestParam("materialCode") String materialCode,
@RequestParam(value = "fileType", required = false) String fileType,
HttpServletResponse response) {
try {
mesDrawingService.assertAuth(firstNonBlank(headerAppKey, appKey), firstNonBlank(headerAppSecret, appSecret));
File file = mesDrawingService.resolveDownloadFile(productionOrderNo, materialCode, fileType);
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode(file.getName(), StandardCharsets.UTF_8.name()).replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName);
response.setContentLengthLong(file.length());
try (FileInputStream fis = new FileInputStream(file);
OutputStream os = response.getOutputStream()) {
byte[] buffer = new byte[8192];
int length;
while ((length = fis.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
os.flush();
}
} catch (ServiceException e) {
writeJsonFail(response, e.getMessage());
} catch (Exception e) {
log.error("MES下载图纸失败 productionOrderNo={} materialCode={}", productionOrderNo, materialCode, e);
writeJsonFail(response, "下载失败: " + e.getMessage());
}
}
private static String firstNonBlank(String a, String b) {
return StringUtils.isNotBlank(a) ? a.trim() : StringUtils.trimToEmpty(b);
}
private void writeJsonFail(HttpServletResponse response, String msg) {
try {
if (!response.isCommitted()) {
response.reset();
}
response.setContentType("application/json;charset=utf-8");
response.getWriter().write(JSONObject.toJSONString(R.fail(msg)));
} catch (IOException ex) {
log.error("写入MES图纸错误响应失败", ex);
}
}
}