1449 MPM同步物料图片及钢构包管理的接口开发
This commit is contained in:
parent
f6191d655f
commit
247f7ff1e4
|
|
@ -32,11 +32,16 @@ import jakarta.validation.constraints.NotBlank;
|
||||||
import jakarta.validation.constraints.NotEmpty;
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
import org.apache.commons.io.FilenameUtils;
|
import org.apache.commons.io.FilenameUtils;
|
||||||
import org.apache.commons.lang3.math.NumberUtils;
|
import org.apache.commons.lang3.math.NumberUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.core.ParameterizedTypeReference;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.transaction.annotation.Propagation;
|
import org.springframework.transaction.annotation.Propagation;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import org.ttzero.excel.entity.ListSheet;
|
import org.ttzero.excel.entity.ListSheet;
|
||||||
import org.ttzero.excel.entity.Workbook;
|
import org.ttzero.excel.entity.Workbook;
|
||||||
|
|
@ -57,6 +62,7 @@ import java.time.LocalDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
@ -82,6 +88,12 @@ public class MaterialControllerService {
|
||||||
@Resource
|
@Resource
|
||||||
private IUserSupplierService userSupplierService;
|
private IUserSupplierService userSupplierService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
|
@Value("${mpm.download.url}")
|
||||||
|
private String mpmDownloadUrl;
|
||||||
|
|
||||||
public void add(@Valid MaterialAddQO request) {
|
public void add(@Valid MaterialAddQO request) {
|
||||||
WmsMaterial old = materialService.getCurrent(request.getNo());
|
WmsMaterial old = materialService.getCurrent(request.getNo());
|
||||||
VUtil.trueThrowBusinessError(Objects.nonNull(old) && !old.getComplete())
|
VUtil.trueThrowBusinessError(Objects.nonNull(old) && !old.getComplete())
|
||||||
|
|
@ -428,11 +440,131 @@ public class MaterialControllerService {
|
||||||
wmsMaterial.setCreateTime(LocalDateTime.now());
|
wmsMaterial.setCreateTime(LocalDateTime.now());
|
||||||
wmsMaterial.setFromMpm(true); // 标记为来自MPM
|
wmsMaterial.setFromMpm(true); // 标记为来自MPM
|
||||||
|
|
||||||
|
// 如果fileindex和filename都不为空,则从MPM下载文件并上传到本系统
|
||||||
|
if (StrUtil.isNotBlank(request.getFileindex()) && StrUtil.isNotBlank(request.getFilename())) {
|
||||||
|
try {
|
||||||
|
String fileUrl = downloadAndUploadFileFromMpm(request.getFileindex(), request.getFilename());
|
||||||
|
if (StrUtil.isNotBlank(fileUrl)) {
|
||||||
|
wmsMaterial.setImage(fileUrl);
|
||||||
|
log.info("成功从MPM下载并上传文件: {}, 文件URL: {}", request.getNo(), fileUrl);
|
||||||
|
} else {
|
||||||
|
log.warn("从MPM下载文件失败,但继续同步图纸数据: {}", request.getNo());
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("从MPM下载并上传文件失败: {}, 错误: {}", request.getNo(), e.getMessage(), e);
|
||||||
|
// 文件下载失败不影响主流程,继续同步其他数据
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
materialService.add(wmsMaterial);
|
materialService.add(wmsMaterial);
|
||||||
|
|
||||||
log.info("成功从MPM系统同步图纸数据: {}, 版本: {}", request.getNo(), wmsMaterial.getVersion());
|
log.info("成功从MPM系统同步图纸数据: {}, 版本: {}", request.getNo(), wmsMaterial.getVersion());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从MPM系统下载文件并上传到本系统
|
||||||
|
*
|
||||||
|
* @param fileindex 文件索引
|
||||||
|
* @param filename 文件名
|
||||||
|
* @return 上传后的文件URL
|
||||||
|
*/
|
||||||
|
private String downloadAndUploadFileFromMpm(String fileindex, String filename) {
|
||||||
|
try {
|
||||||
|
// 构建MPM下载请求URL
|
||||||
|
String mpmRequestUrl = mpmDownloadUrl + "/" + filename + "," + fileindex;
|
||||||
|
log.info("请求MPM文件下载地址: {}", mpmRequestUrl);
|
||||||
|
|
||||||
|
// 发送GET请求获取文件下载地址
|
||||||
|
ResponseEntity<Map<String, Object>> response = restTemplate.exchange(
|
||||||
|
mpmRequestUrl,
|
||||||
|
HttpMethod.GET,
|
||||||
|
null,
|
||||||
|
new ParameterizedTypeReference<Map<String, Object>>() {}
|
||||||
|
);
|
||||||
|
|
||||||
|
Map<String, Object> responseBody = response.getBody();
|
||||||
|
if (responseBody != null && Boolean.TRUE.equals(responseBody.get("success"))) {
|
||||||
|
Map<String, String> data = (Map<String, String>) responseBody.get("data");
|
||||||
|
if (data != null && data.containsKey(filename)) {
|
||||||
|
String downloadUrl = data.get(filename);
|
||||||
|
log.info("获取到MPM文件下载地址: {}", downloadUrl);
|
||||||
|
|
||||||
|
// 下载文件内容
|
||||||
|
ResponseEntity<byte[]> fileResponse = restTemplate.exchange(
|
||||||
|
downloadUrl,
|
||||||
|
HttpMethod.GET,
|
||||||
|
null,
|
||||||
|
byte[].class
|
||||||
|
);
|
||||||
|
|
||||||
|
if (fileResponse.getStatusCode().is2xxSuccessful() && fileResponse.getBody() != null) {
|
||||||
|
byte[] fileContent = fileResponse.getBody();
|
||||||
|
|
||||||
|
// 生成文件路径
|
||||||
|
String filePath = buildFilePathForMpm(filename);
|
||||||
|
|
||||||
|
// 上传到本系统
|
||||||
|
String uploadUrl = fileUploadService.upload(
|
||||||
|
filePath,
|
||||||
|
new java.io.ByteArrayInputStream(fileContent),
|
||||||
|
getContentType(filename)
|
||||||
|
);
|
||||||
|
|
||||||
|
log.info("文件上传成功,URL: {}", uploadUrl);
|
||||||
|
return uploadUrl;
|
||||||
|
} else {
|
||||||
|
log.error("从MPM下载文件失败,状态码: {}", fileResponse.getStatusCode());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.error("MPM响应中未找到文件下载地址: {}", filename);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.error("MPM请求失败: {}", responseBody != null ? responseBody.get("message") : "未知错误");
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("从MPM下载并上传文件异常: fileindex={}, filename={}, 错误: {}", fileindex, filename, e.getMessage(), e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据文件名生成文件路径
|
||||||
|
*
|
||||||
|
* @param fileName 文件名
|
||||||
|
* @return 文件路径
|
||||||
|
*/
|
||||||
|
private String buildFilePathForMpm(String fileName) {
|
||||||
|
String fileType = "." + FilenameUtils.getExtension(fileName);
|
||||||
|
return StrUtil.format("admin/{}/{}/{}/{}{}",
|
||||||
|
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")),
|
||||||
|
"MPM_SYNC",
|
||||||
|
RandomUtil.randomString(4),
|
||||||
|
IdUtil.fastUUID(),
|
||||||
|
fileType);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据文件扩展名获取Content-Type
|
||||||
|
*
|
||||||
|
* @param fileName 文件名
|
||||||
|
* @return Content-Type
|
||||||
|
*/
|
||||||
|
private String getContentType(String fileName) {
|
||||||
|
String extension = FilenameUtils.getExtension(fileName).toLowerCase();
|
||||||
|
return switch (extension) {
|
||||||
|
case "pdf" -> "application/pdf";
|
||||||
|
case "jpg", "jpeg" -> "image/jpeg";
|
||||||
|
case "png" -> "image/png";
|
||||||
|
case "gif" -> "image/gif";
|
||||||
|
case "doc" -> "application/msword";
|
||||||
|
case "docx" -> "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
||||||
|
case "xls" -> "application/vnd.ms-excel";
|
||||||
|
case "xlsx" -> "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
||||||
|
default -> "application/octet-stream";
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 递增版本号
|
* 递增版本号
|
||||||
* @param currentVersion 当前版本号
|
* @param currentVersion 当前版本号
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue