You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
6.5 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.tml.materialPeptide;
import com.dassault_systemes.platform.restServices.RestService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import matrix.db.JPO;
import matrix.util.MatrixException;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.List;
import java.util.logging.Logger;
@Path("/materialPeptide")
public class DSMaintenancePeptideAttrController extends RestService {
private static final Logger LOGGER = Logger.getLogger(DSMaintenancePeptideAttrController.class.getName());
@GET
public Response getInfo(@Context HttpServletRequest request,
@QueryParam("partCode") String partCode) {
return this.getPartPeptideAttr(request, partCode);
}
@PUT
@Path("/updatePartInfo")
@Consumes(MediaType.APPLICATION_JSON)
public Response updatePartInfo(@Context HttpServletRequest request,
PartAttrDTO partAttrDTO) {
return this.updatePartAttr(request,partAttrDTO);
}
@PUT
@Path("/updateInfo")
@Consumes(MediaType.APPLICATION_JSON)
public Response updateInfo(@Context HttpServletRequest request,
PeptideUpdateRequest updateRequest) {
return this.updatePeptideAttrs(request, updateRequest);
}
private Response updatePartAttr(HttpServletRequest request, PartAttrDTO partAttrDTO) {
if(partAttrDTO == null || partAttrDTO.getPartId() == null) {
return Response.status(Response.Status.BAD_REQUEST).entity("Missing required properties").build();
}
matrix.db.Context context = this.getAuthenticatedContext(request, false);
String partId = partAttrDTO.getPartId();
StringBuilder updateInfoBuilder = new StringBuilder();
updateInfoBuilder.append("modify bus ").append(partId);
// 如果有规格型号属性,添加到更新字符串
if (partAttrDTO.getSpecification() != null) {
updateInfoBuilder.append(" ").append("specification").append(" ").append(partAttrDTO.getSpecification());
}
// 如果有品牌属性,添加到更新字符串
if (partAttrDTO.getBrand() != null) {
updateInfoBuilder.append(" ").append("brand").append(" ").append(partAttrDTO.getBrand());
}
String updateInfo = updateInfoBuilder.toString();
String[] updateInfoArray = {updateInfo};
LOGGER.info("调用JPO更新物料属性: " + updateInfo);
try {
String resultJson = JPO.invoke(context, "TMLMaintainMaterialPeptideAttr", null, "updatePartAttr", updateInfoArray, String.class);
return Response.status(Response.Status.OK).entity(resultJson).build();
} catch (MatrixException e) {
throw new RuntimeException(e);
}
}
private Response updatePeptideAttrs(HttpServletRequest request, PeptideUpdateRequest updateRequest) {
if(updateRequest == null || updateRequest.getProperties() == null) {
return Response.status(Response.Status.BAD_REQUEST).entity("Missing required properties").build();
}
matrix.db.Context context = this.getAuthenticatedContext(request, false);
List<PropertyDTO> properties = updateRequest.getProperties();
String itemId = updateRequest.getPartId() + " ";
String itemInfo = itemId;
for(PropertyDTO property : properties) {
itemInfo += property.getName() + " ";
itemInfo += property.getValue() + " ";
}
String updateInfo = "modify bus " + itemInfo;
String[] updateInfoArray = {updateInfo};
try {
String resultJson = JPO.invoke(context, "TMLMaintainMaterialPeptideAttr", null, "updatePeptideAttr", updateInfoArray, String.class);
return Response.status(Response.Status.OK).entity(resultJson).build();
} catch (MatrixException e) {
throw new RuntimeException(e);
}
}
private Response getPartPeptideAttr(HttpServletRequest request, String partCode) {
if (partCode == null || partCode.trim().isEmpty()) {
return createErrorResponse(400, "缺少 partCode 参数");
}
try {
matrix.db.Context context = this.getAuthenticatedContext(request, false);
String[] partInfo;
if (partCode.contains("|")) {
partInfo = partCode.split("\\|");
} else {
partInfo = new String[]{partCode};
}
return executeQuery(context, partInfo);
} catch (Exception e) {
return createErrorResponse(500, "认证失败: " + e.getMessage());
}
}
private Response executeQuery(matrix.db.Context context, String[] partInfo) {
try {
// 调用 JPO 方法,返回包装的JSON
String resultJson = JPO.invoke(context, "TMLMaintainMaterialPeptideAttr", null, "getPeptideAttrMap", partInfo, String.class);
// 直接返回JSON字符串
return Response.ok()
.entity(resultJson)
.type(MediaType.APPLICATION_JSON)
.build();
} catch (MatrixException e) {
return createErrorResponse(500, "JPO调用失败: " + e.getMessage());
} catch (Exception e) {
return createErrorResponse(500, "服务器错误: " + e.getMessage());
}
}
private Response createErrorResponse(int statusCode, String message) {
try {
JSONObject errorResponse = new JSONObject();
errorResponse.put("success", false);
errorResponse.put("code", statusCode);
errorResponse.put("message", message);
errorResponse.put("data", new JSONArray());
errorResponse.put("timestamp", System.currentTimeMillis());
return Response.status(statusCode)
.entity(errorResponse.toString())
.type(MediaType.APPLICATION_JSON)
.build();
} catch (Exception e) {
// 如果JSON创建失败,返回简单文本
return Response.status(500)
.entity("{\"error\":\"Internal server error\"}")
.type(MediaType.APPLICATION_JSON)
.build();
}
}
}