| | |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.dynamic.datasource.annotation.DS; |
| | | import com.baomidou.mybatisplus.annotation.IdType; |
| | | import com.baomidou.mybatisplus.annotation.TableId; |
| | | import com.example.erp.common.RabbitMQUtil; |
| | | import com.example.erp.dto.pp.MesCalculateDTO; |
| | | import com.example.erp.entity.pp.*; |
| | | import com.example.erp.entity.sd.Delivery; |
| | | import com.example.erp.entity.sd.OrderDetail; |
| | | import com.example.erp.entity.userInfo.Log; |
| | | import com.example.erp.entity.userInfo.SysError; |
| | | import com.example.erp.mapper.pp.GlassOptimizeMapper; |
| | | import com.example.erp.service.userInfo.SysErrorService; |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.fasterxml.jackson.databind.DeserializationFeature; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.transaction.interceptor.TransactionAspectSupport; |
| | | |
| | | import java.io.BufferedReader; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.io.OutputStream; |
| | | import java.lang.reflect.Field; |
| | | import java.math.BigDecimal; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.sql.Date; |
| | | import java.text.DecimalFormat; |
| | | import java.time.LocalDate; |
| | | import java.util.*; |
| | | import java.util.concurrent.TimeoutException; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | |
| | | GlassOptimizeMapper glassOptimizeMapper; |
| | | @Autowired |
| | | SysErrorService sysErrorService; |
| | | |
| | | @Value("${mesIp:localhost}") |
| | | private String mesIp; |
| | | |
| | | RabbitMQUtil rabbitMQUtil; |
| | | //模拟计算 |
| | |
| | | } else { |
| | | dataList = glassOptimizeMapper.directOptimization(projectNo); |
| | | } |
| | | List<Map<String, Object>> tempDataList = null; |
| | | if (Integer.parseInt(stringObjectMap.get("optimize_state").toString())==1) { |
| | | tempDataList = glassOptimizeMapper.optimizeTemp(projectNo); |
| | | } |
| | | |
| | | // 获取磨量配置数据 |
| | | List<Map<String, Object>> grindingTrimmingList = glassOptimizeMapper.getGrindingOptimize(username); |
| | |
| | | } |
| | | } |
| | | } |
| | | if (tempDataList != null && !tempDataList.isEmpty()) { |
| | | dataList.addAll(tempDataList); |
| | | } |
| | | map.put("optimizeState", Integer.parseInt(stringObjectMap.get("optimize_state").toString())); |
| | | map.put("data", dataList); |
| | | map.put("project", glassOptimizeMapper.selectProjectCount(projectNo)); |
| | | map.put("grindingTrimming", glassOptimizeMapper.getGrindingTrimming(username)); |
| | | return map; |
| | | } |
| | | |
| | |
| | | int state2 = 0; |
| | | StringBuilder processId= new StringBuilder(); |
| | | List<FlowCard> flowCardList = JSONArray.parseArray(JSONObject.toJSONString(object.get("projectdetail")), FlowCard.class); |
| | | if (!flowCardList.isEmpty()) { |
| | | if(projectType.equals("2")){ |
| | | deleteProject(projectId,2); |
| | | if(projectType.equals("2")){ |
| | | deleteProject(projectId,2); |
| | | } |
| | | |
| | | // 创建一个映射来存储 (processId-technologyNumber) 组合到 rack 编号的映射 |
| | | Map<String, Integer> rackMap = new HashMap<>(); |
| | | int rackCounter = 1; |
| | | |
| | | // 如果为空流程卡,则将 type 设置为 1 |
| | | if (flowCardList == null || flowCardList.isEmpty()) { |
| | | type = 1; |
| | | } |
| | | for (FlowCard flowCard : flowCardList) { |
| | | // 为每个唯一的 processId-technologyNumber 组合分配 rack 编号 |
| | | String key = flowCard.getProcessId() + "-" + flowCard.getTechnologyNumber(); |
| | | if (!rackMap.containsKey(key)) { |
| | | rackMap.put(key, rackCounter++); |
| | | } |
| | | int rackValue = rackMap.get(key); |
| | | |
| | | if(flowCard.getPatchState().equals(0)){ |
| | | state1=1; |
| | | //给流程卡表添加对应的工程号 |
| | | Boolean a = glassOptimizeMapper.updateFlowCardProject(flowCard.getProcessId(),flowCard.getTechnologyNumber(),projectId); |
| | | // 更新流程卡的rack字段 |
| | | glassOptimizeMapper.updateFlowCardRack(flowCard.getProcessId(), flowCard.getTechnologyNumber(), rackValue); |
| | | area = glassOptimizeMapper.getSelectArea(flowCard.getProcessId(),flowCard.getTechnologyNumber()).doubleValue(); |
| | | sumArea = sumArea.add(BigDecimal.valueOf(area)); |
| | | sumQuantity +=flowCard.getQuantity(); |
| | | processId.append(flowCard.getProcessId()).append("-").append(flowCard.getTechnologyNumber()).append(";"); |
| | | }else{ |
| | | state2=1; |
| | | Boolean a=glassOptimizeMapper.updatePatchLogProject(flowCard.getProcessId(),flowCard.getTechnologyNumber(),projectId); |
| | | area = glassOptimizeMapper.getSelectAreaPatchLog(flowCard.getProcessId(),flowCard.getTechnologyNumber()).doubleValue(); |
| | | sumArea = sumArea.add(BigDecimal.valueOf(area)); |
| | | sumQuantity +=flowCard.getQuantity(); |
| | | processId.append(flowCard.getProcessId()).append("-").append(flowCard.getTechnologyNumber()).append(";"); |
| | | } |
| | | |
| | | // 创建一个映射来存储 (processId-technologyNumber) 组合到 rack 编号的映射 |
| | | Map<String, Integer> rackMap = new HashMap<>(); |
| | | int rackCounter = 1; |
| | | } |
| | | if(state1==1&&state2==0){ |
| | | type=1; |
| | | }else if(state1==1&&state2==1){ |
| | | type=2; |
| | | } |
| | | |
| | | for (FlowCard flowCard : flowCardList) { |
| | | // 为每个唯一的 processId-technologyNumber 组合分配 rack 编号 |
| | | String key = flowCard.getProcessId() + "-" + flowCard.getTechnologyNumber(); |
| | | if (!rackMap.containsKey(key)) { |
| | | rackMap.put(key, rackCounter++); |
| | | } |
| | | int rackValue = rackMap.get(key); |
| | | |
| | | if(flowCard.getPatchState().equals(0)){ |
| | | state1=1; |
| | | //给流程卡表添加对应的工程号 |
| | | Boolean a = glassOptimizeMapper.updateFlowCardProject(flowCard.getProcessId(),flowCard.getTechnologyNumber(),projectId); |
| | | // 更新流程卡的rack字段 |
| | | glassOptimizeMapper.updateFlowCardRack(flowCard.getProcessId(), flowCard.getTechnologyNumber(), rackValue); |
| | | area = glassOptimizeMapper.getSelectArea(flowCard.getProcessId(),flowCard.getTechnologyNumber()).doubleValue(); |
| | | sumArea = sumArea.add(BigDecimal.valueOf(area)); |
| | | sumQuantity +=flowCard.getQuantity(); |
| | | processId.append(flowCard.getProcessId()).append("-").append(flowCard.getTechnologyNumber()).append(";"); |
| | | }else{ |
| | | state2=1; |
| | | Boolean a=glassOptimizeMapper.updatePatchLogProject(flowCard.getProcessId(),flowCard.getTechnologyNumber(),projectId); |
| | | area = glassOptimizeMapper.getSelectAreaPatchLog(flowCard.getProcessId(),flowCard.getTechnologyNumber()).doubleValue(); |
| | | sumArea = sumArea.add(BigDecimal.valueOf(area)); |
| | | sumQuantity +=flowCard.getQuantity(); |
| | | processId.append(flowCard.getProcessId()).append("-").append(flowCard.getTechnologyNumber()).append(";"); |
| | | } |
| | | |
| | | } |
| | | if(state1==1&&state2==0){ |
| | | type=1; |
| | | }else if(state1==1&&state2==1){ |
| | | type=2; |
| | | } |
| | | int index = optionVal.indexOf("mm"); |
| | | // 获取 "mm" 前面的部分 |
| | | String glass_thickness = optionVal.substring(0, index); |
| | | // 获取 "mm" 后面的部分 |
| | | String glass_type = optionVal.substring(index + 2); |
| | | if(projectType.equals("2")){ |
| | | glassOptimizeMapper.updateProjectMp(projectId,projectNmae,glass_thickness,glass_type,sumQuantity,sumArea.doubleValue(),userName, |
| | | int index = optionVal.indexOf("mm"); |
| | | // 获取 "mm" 前面的部分 |
| | | String glass_thickness = optionVal.substring(0, index); |
| | | // 获取 "mm" 后面的部分 |
| | | String glass_type = optionVal.substring(index + 2); |
| | | if(projectType.equals("2")){ |
| | | glassOptimizeMapper.updateProjectMp(projectId,projectNmae,glass_thickness,glass_type,sumQuantity,sumArea.doubleValue(),userName, |
| | | flowCardList.size(), String.valueOf(processId),type); |
| | | }else{ |
| | | if(glassOptimizeMapper.selectProjectCount(projectId)==null){ |
| | | glassOptimizeMapper.addProjectMp(projectId,projectNmae,glass_thickness,glass_type,sumQuantity,sumArea.doubleValue(),userName, |
| | | flowCardList.size(), String.valueOf(processId),type); |
| | | }else{ |
| | | if(glassOptimizeMapper.selectProjectCount(projectId)==null){ |
| | | glassOptimizeMapper.addProjectMp(projectId,projectNmae,glass_thickness,glass_type,sumQuantity,sumArea.doubleValue(),userName, |
| | | flowCardList.size(), String.valueOf(processId),type); |
| | | }else{ |
| | | TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint); |
| | | return "false1"; |
| | | } |
| | | TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint); |
| | | return "false1"; |
| | | } |
| | | }else { |
| | | return "false2"; |
| | | |
| | | } |
| | | } catch (Exception e) { |
| | | TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint); |
| | |
| | | public Boolean issuingProjects(String projectNo) throws JsonProcessingException { |
| | | boolean saveState=false; |
| | | try { |
| | | System.out.println(mesIp); |
| | | // 1. 创建URL对象 |
| | | // URL url = new URL("http://10.153.19.31:88/api/loadGlass/engineering/importEngineer"); |
| | | URL url = new URL("http://localhost:88/api/loadGlass/engineering/importEngineer"); |
| | | |
| | | URL url = new URL("http://" + mesIp + ":88/api/loadGlass/engineering/importEngineer"); |
| | | |
| | | // 2. 打开连接 |
| | | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| | |
| | | public Map<String, Object> getProductListSv(String processId) { |
| | | Map<String, Object> productMap = new HashMap<>(); |
| | | List<Map<String, Object>> productInfo = glassOptimizeMapper.selectProjectList(processId); |
| | | List<Map<String, Object>> tempProductInfo = glassOptimizeMapper.selectTempProject(processId); |
| | | productInfo.addAll(tempProductInfo); |
| | | productMap.put("productInfo", productInfo); |
| | | Map<String, Object> map = new HashMap<>(); |
| | | |
| | |
| | | // 按照 processCard 分组 |
| | | Map<String, List<Map<String, Object>>> groupedByProcessCard = new HashMap<>(); |
| | | for (Map<String, Object> item : productInfo) { |
| | | String processCard = (String) item.get("processCard"); |
| | | String processCard = String.valueOf(item.get("processCard")); |
| | | if (processCard != null) { |
| | | groupedByProcessCard.computeIfAbsent(processCard, k -> new ArrayList<>()).add(item); |
| | | } |
| | |
| | | |
| | | try { |
| | | // 1. 创建URL对象 |
| | | // URL url = new URL("http://10.153.19.31:88/api/loadGlass/engineering/optimizeCancelTask"); |
| | | URL url = new URL("http://localhost:88/api/loadGlass/engineering/optimizeCancelTask"); |
| | | URL url = new URL("http://" + mesIp + ":88/api/loadGlass/engineering/optimizeCancelTask"); |
| | | // URL url = new URL("http://localhost:88/api/loadGlass/engineering/optimizeCancelTask"); |
| | | |
| | | |
| | | // 2. 打开连接 |
| | |
| | | } |
| | | |
| | | |
| | | public void processExternalOptimizeRequest(MesCalculateDTO mesCalculateDTO){ |
| | | // 使用反射将对象转换为 Map,使用 Java 字段名 |
| | | List<Map<String, Object>> materialDetailList = new ArrayList<>(); |
| | | if (mesCalculateDTO.getEngineeringRawQueueList() != null) { |
| | | for (MesCalculateDTO.EngineeringRawQueue materialDetail : mesCalculateDTO.getEngineeringRawQueueList()) { |
| | | Map<String, Object> materialMap = objectToMapUsingFields(materialDetail); |
| | | materialDetailList.add(materialMap); |
| | | } |
| | | } |
| | | |
| | | List<Map<String, Object>> glassDetailList = new ArrayList<>(); |
| | | if (mesCalculateDTO.getGlassInfoList() != null) { |
| | | for (MesCalculateDTO.GlassInfo glassDetail : mesCalculateDTO.getGlassInfoList()) { |
| | | Map<String, Object> glassMap = objectToMapUsingFields(glassDetail); |
| | | glassDetailList.add(glassMap); |
| | | } |
| | | } |
| | | |
| | | List<Map<String, Object>> glassDamageList = new ArrayList<>(); |
| | | if (mesCalculateDTO.getDamageList() != null) { |
| | | for (MesCalculateDTO.DamageList damageInfo : mesCalculateDTO.getDamageList()) { |
| | | Map<String, Object> damageMap = objectToMapUsingFields(damageInfo); |
| | | glassDamageList.add(damageMap); |
| | | } |
| | | } |
| | | |
| | | List<Map<String, Object>> glassDamageTempList = new ArrayList<>(); |
| | | if (mesCalculateDTO.getDamageList() != null) { |
| | | for (MesCalculateDTO.DamageList damageInfo : mesCalculateDTO.getDamageList()) { |
| | | Map<String, Object> damageMap = objectToMapUsingFields(damageInfo); |
| | | // 创建新的Map只保留glassId字段并添加reportState字段 |
| | | Map<String, Object> filteredDamageMap = new HashMap<>(); |
| | | if (damageMap.containsKey("glassId")) { |
| | | filteredDamageMap.put("glassId", damageMap.get("glassId")); |
| | | } |
| | | filteredDamageMap.put("reportState", 10); |
| | | glassDamageTempList.add(filteredDamageMap); |
| | | } |
| | | } |
| | | |
| | | String projectNo = mesCalculateDTO.getEngineerId(); |
| | | System.out.println("projectNo: " + projectNo); |
| | | |
| | | for (Map<String, Object> materialDetail : materialDetailList) { |
| | | Object stockId = materialDetail.get("rawSequence"); |
| | | Map<String, Object> trimInfo = glassOptimizeMapper.getTrimInfo(projectNo, stockId); |
| | | if (trimInfo != null) { |
| | | materialDetail.putAll(trimInfo); |
| | | } |
| | | } |
| | | |
| | | Map<String, Object> glassOptimaizeMap = new HashMap<>(); |
| | | Map<String, Object> optimizeProperty = glassOptimizeMapper.getGlassType(projectNo); |
| | | |
| | | glassOptimaizeMap.put("requestSource", "ERP"); |
| | | glassOptimaizeMap.put("usageRate", mesCalculateDTO.getUsageRate()); |
| | | glassOptimaizeMap.put("optimizeProperty", optimizeProperty); |
| | | glassOptimaizeMap.put("engineeringRawQueueList", materialDetailList); |
| | | glassOptimaizeMap.put("glassInfoList", glassDetailList); |
| | | |
| | | try { |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | |
| | | String glassDamageListJson = objectMapper.writeValueAsString(glassDamageList); |
| | | System.out.println("damageList JSON: " + glassDamageListJson); |
| | | |
| | | String glassDamageTempListJson = objectMapper.writeValueAsString(glassDamageTempList); |
| | | System.out.println("glassDamageTempListJson: " + glassDamageTempListJson); |
| | | |
| | | String optimizeJsonString = objectMapper.writeValueAsString(glassOptimaizeMap); |
| | | System.out.println("glassTypeMap JSON: " + optimizeJsonString); |
| | | |
| | | // 获取并输出动态优化返回结果 |
| | | String optimizeResult = callDynamicOptimizeApi(optimizeJsonString); |
| | | System.out.println("动态优化返回结果: " + optimizeResult); |
| | | |
| | | // 将结果转换为JSON格式 |
| | | Object optimaizeResultJson = null; |
| | | try { |
| | | // 尝试解析为JSON对象 |
| | | optimaizeResultJson = objectMapper.readValue(optimizeResult, Object.class); |
| | | } catch (Exception e) { |
| | | // 如果不是有效的JSON字符串,创建一个包含原始字符串的Map |
| | | Map<String, String> errorResult = new HashMap<>(); |
| | | errorResult.put("result", optimizeResult); |
| | | optimaizeResultJson = errorResult; |
| | | } |
| | | |
| | | // 输出JSON格式的结果 |
| | | String jsonOptimizeResult = objectMapper.writeValueAsString(optimaizeResultJson); |
| | | System.out.println("动态优化返回结果JSON格式: " + jsonOptimizeResult); |
| | | |
| | | // 调用issuingDamageDynamicProjects接口 |
| | | Boolean MESResult = issuingDamageDynamicProjects(projectNo, glassDamageTempList); |
| | | System.out.println("MES返回结果: " + MESResult); |
| | | |
| | | |
| | | // 检查issuingDamageDynamicProjects接口调用结果,如果失败则停止后续操作 |
| | | if (!MESResult) { |
| | | System.out.println("issuingDamageDynamicProjects接口调用失败,停止后续操作"); |
| | | return; |
| | | } |
| | | |
| | | // 调用cutFiles接口 |
| | | Boolean cutFileResult = callCutFilesApi(jsonOptimizeResult); |
| | | System.out.println("调用cutFiles接口返回结果: " + cutFileResult); |
| | | |
| | | // 检查cutFiles接口调用结果,如果失败则停止后续操作 |
| | | if (!cutFileResult) { |
| | | System.out.println("cutFiles接口调用失败,停止后续操作"); |
| | | return; |
| | | } |
| | | |
| | | // 调用issuingDynamicProjects接口 |
| | | Boolean MESTwiceResult = issuingDynamicProjects(projectNo); |
| | | System.out.println("MES二次返回结果: " + MESTwiceResult); |
| | | |
| | | // 检查issuingDynamicProjects接口调用结果,如果失败则停止后续操作 |
| | | if (!MESTwiceResult) { |
| | | System.out.println("issuingDynamicProjects接口调用失败,停止后续操作"); |
| | | } |
| | | |
| | | } catch (JsonProcessingException e) { |
| | | System.err.println("转换JSON失败: " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | // 使用反射将对象转换为 Map,使用 Java 字段名 |
| | | private Map<String, Object> objectToMapUsingFields(Object obj) { |
| | | Map<String, Object> map = new HashMap<>(); |
| | | if (obj == null) return map; |
| | | |
| | | Class<?> clazz = obj.getClass(); |
| | | Field[] fields = clazz.getDeclaredFields(); |
| | | |
| | | for (Field field : fields) { |
| | | try { |
| | | field.setAccessible(true); |
| | | Object value = field.get(obj); |
| | | // 使用 Java 字段名作为 key |
| | | map.put(field.getName(), value); |
| | | } catch (IllegalAccessException e) { |
| | | // 记录或处理异常 |
| | | System.err.println("访问字段失败: " + field.getName() + ", 错误: " + e.getMessage()); |
| | | } |
| | | } |
| | | return map; |
| | | } |
| | | |
| | | private String callDynamicOptimizeApi(String jsonData) { |
| | | try { |
| | | URL url = new URL("http://" + mesIp + ":8078/api/dynamicOptimize"); |
| | | |
| | | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| | | |
| | | // 设置请求方法 |
| | | conn.setRequestMethod("POST"); |
| | | |
| | | // 设置请求头 |
| | | conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); |
| | | conn.setRequestProperty("Accept", "application/json"); |
| | | |
| | | // 允许输出 |
| | | conn.setDoOutput(true); |
| | | |
| | | // 设置超时时间 |
| | | conn.setConnectTimeout(10000); // 10秒连接超时 |
| | | conn.setReadTimeout(30000); // 30秒读取超时 |
| | | |
| | | // 发送JSON数据 |
| | | try (OutputStream os = conn.getOutputStream()) { |
| | | byte[] input = jsonData.getBytes(StandardCharsets.UTF_8); |
| | | os.write(input, 0, input.length); |
| | | } |
| | | |
| | | // 读取响应 |
| | | StringBuilder response = new StringBuilder(); |
| | | try (BufferedReader br = new BufferedReader( |
| | | new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) { |
| | | String responseLine; |
| | | while ((responseLine = br.readLine()) != null) { |
| | | response.append(responseLine.trim()); |
| | | } |
| | | } |
| | | |
| | | String responseResult = response.toString(); |
| | | System.out.println("优化软件响应结果: " + responseResult); |
| | | // 断开连接 |
| | | conn.disconnect(); |
| | | |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | // 启用类型转换功能,允许将浮点数转换为整数 |
| | | objectMapper.configure(DeserializationFeature.ACCEPT_FLOAT_AS_INT, true); |
| | | Map<String, Object> responseMap = objectMapper.readValue(responseResult, Map.class); |
| | | |
| | | // 检查code是否为200 |
| | | Object codeObj = responseMap.get("code"); |
| | | int code = -1; |
| | | if (codeObj instanceof Integer) { |
| | | code = (Integer) codeObj; |
| | | } else if (codeObj instanceof String) { |
| | | try { |
| | | code = Integer.parseInt((String) codeObj); |
| | | } catch (NumberFormatException e) { |
| | | System.err.println("无法解析code字段: " + codeObj); |
| | | } |
| | | } |
| | | |
| | | if (code == 200) { |
| | | // 设置回滚点,用于事务处理 |
| | | Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint(); |
| | | try { |
| | | // 返回data字段的内容作为JSON字符串 |
| | | Object dataObj = responseMap.get("data"); |
| | | if (dataObj != null) { |
| | | // 确保 dataObj 是 Map 类型并验证 projectNo 字段 |
| | | if (dataObj instanceof Map) { |
| | | Map<String, Object> dataMap = (Map<String, Object>) dataObj; |
| | | |
| | | // 验证 projectNo 是否存在 |
| | | Object projectNoObj = dataMap.get("projectNo"); |
| | | if (projectNoObj == null) { |
| | | throw new RuntimeException("projectNo 不能为空"); |
| | | } |
| | | |
| | | String projectNo = projectNoObj.toString(); |
| | | dataMap.put("projectNo", projectNo); |
| | | |
| | | // 删除已存在的相关数据 |
| | | glassOptimizeMapper.deleteOptimizeDynamicProject(projectNo); |
| | | glassOptimizeMapper.deleteOptimizeDynamicLayout(projectNo); |
| | | glassOptimizeMapper.deleteOptimizeDynamicDetail(projectNo); |
| | | |
| | | glassOptimizeMapper.insertDynamicProject(dataMap); |
| | | |
| | | // 处理 layouts 数组并插入 |
| | | Object layoutsObj = dataMap.get("layouts"); |
| | | if (layoutsObj instanceof List) { |
| | | @SuppressWarnings("unchecked") |
| | | List<Map<String, Object>> layouts = (List<Map<String, Object>>) layoutsObj; |
| | | |
| | | for (Map<String, Object> layout : layouts) { |
| | | // 为每个 layout 添加 projectNo |
| | | layout.put("projectNo", projectNo); |
| | | |
| | | // 获取当前 layout 的 glassDetails |
| | | Object glassDetailsObj = layout.get("glassDetails"); |
| | | if (glassDetailsObj instanceof List) { |
| | | @SuppressWarnings("unchecked") |
| | | List<Map<String, Object>> glassDetails = (List<Map<String, Object>>) glassDetailsObj; |
| | | |
| | | // 为每个 glassDetail 添加 projectNo 和处理 glassPoint |
| | | for (Map<String, Object> detail : glassDetails) { |
| | | detail.put("projectNo", projectNo); |
| | | |
| | | |
| | | // // 处理 isRotate 为 1 时交换 width 和 height 的逻辑 |
| | | // if (detail.containsKey("isRotate")) { |
| | | // Object isRotateObj = detail.get("isRotate"); |
| | | // int isRotate = 0; |
| | | // if (isRotateObj instanceof Number) { |
| | | // isRotate = ((Number) isRotateObj).intValue(); |
| | | // } else if (isRotateObj instanceof String) { |
| | | // isRotate = Integer.parseInt(isRotateObj.toString()); |
| | | // } |
| | | // |
| | | // if (isRotate == 1) { |
| | | // // 交换 width 和 height |
| | | // Object widthObj = detail.get("width"); |
| | | // Object heightObj = detail.get("height"); |
| | | // detail.put("width", heightObj); |
| | | // detail.put("height", widthObj); |
| | | // |
| | | // |
| | | // } |
| | | // } |
| | | |
| | | // 处理 glassPoint 字段转换 |
| | | if (detail.containsKey("glassPoint")) { |
| | | Object glassPointObj = detail.get("glassPoint"); |
| | | if (glassPointObj != null) { |
| | | try { |
| | | // 如果 glassPointObj 已经是字符串,则不需要转换 |
| | | if (!(glassPointObj instanceof String)) { |
| | | // 将对象转换为 JSON 字符串 |
| | | String glassPointStr = objectMapper.writeValueAsString(glassPointObj); |
| | | detail.put("glassPoint", glassPointStr); |
| | | } |
| | | } catch (Exception e) { |
| | | // 如果转换失败,记录错误并移除该字段 |
| | | System.err.println("转换 glassPoint 失败: " + e.getMessage()); |
| | | detail.remove("glassPoint"); |
| | | } |
| | | } else { |
| | | // 如果 glassPointObj 为 null,移除该字段 |
| | | detail.remove("glassPoint"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | glassOptimizeMapper.insertDynamicDetail(glassDetails, projectNo); |
| | | } |
| | | |
| | | // 插入 layout 到 optimize_dynamic_layout 表 |
| | | glassOptimizeMapper.insertDynamicLayout(layout); |
| | | } |
| | | } |
| | | } |
| | | return responseResult; |
| | | } else { |
| | | return "{}"; // 如果data字段为空,返回空JSON对象 |
| | | } |
| | | } catch (Exception e) { |
| | | // 发生异常时回滚事务 |
| | | TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint); |
| | | throw e; // 重新抛出异常 |
| | | } |
| | | } else { |
| | | // 当code不是200时,返回错误信息 |
| | | Object messageObj = responseMap.get("message"); |
| | | String errorMessage = "接口返回错误码: " + code; |
| | | if (messageObj != null) { |
| | | errorMessage = messageObj.toString(); |
| | | } |
| | | return errorMessage; |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | System.err.println("调用动态优化接口失败: " + e.getMessage()); |
| | | e.printStackTrace(); |
| | | return "调用动态优化接口失败: " + e.getMessage(); |
| | | } |
| | | } |
| | | |
| | | |
| | | public Boolean issuingDamageDynamicProjects(String projectNo, List<Map<String, Object>> glassDamageTempList){ |
| | | boolean saveState=false; |
| | | try { |
| | | System.out.println(mesIp); |
| | | // 1. 创建URL对象 |
| | | URL url = new URL("http://" + mesIp + ":88/api/loadGlass/engineering/engineerOptimizeImport"); |
| | | |
| | | // 2. 打开连接 |
| | | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| | | conn.setRequestMethod("POST"); |
| | | conn.setRequestProperty("Content-Type", "application/json"); |
| | | conn.setRequestProperty("Accept", "application/json"); |
| | | conn.setDoOutput(true); |
| | | |
| | | // 3. 准备请求体 |
| | | |
| | | // 获取glassInfolList数据 |
| | | List<Map<String, Object>> glassInfolList = glassOptimizeMapper.selectOptimizeDynamicDetail(projectNo); |
| | | |
| | | // 处理damageList,对glassInfolList中存在的glassId设置reportState为10,不存在的设置为1 |
| | | List<Map<String, Object>> processedDamageList = new ArrayList<>(); |
| | | Set<String> glassIdSet = new HashSet<>(); |
| | | |
| | | // 收集glassInfolList中的所有glassId |
| | | for (Map<String, Object> glassInfo : glassInfolList) { |
| | | if (glassInfo.containsKey("glassId") && glassInfo.get("glassId") != null) { |
| | | glassIdSet.add(glassInfo.get("glassId").toString()); |
| | | } |
| | | } |
| | | |
| | | // 处理glassDamageTempList,根据glassId是否在glassInfolList中存在来设置reportState |
| | | for (Map<String, Object> damageItem : glassDamageTempList) { |
| | | if (damageItem.containsKey("glassId") && damageItem.get("glassId") != null) { |
| | | String glassId = damageItem.get("glassId").toString(); |
| | | Map<String, Object> processedItem = new HashMap<>(damageItem); |
| | | |
| | | // 如果glassId在glassInfolList中存在,reportState保持为10,否则改为6 |
| | | if (glassIdSet.contains(glassId)) { |
| | | // glassId存在,保持reportState为10(已在glassDamageTempList中设置) |
| | | processedItem.put("reportState", 10); |
| | | } else { |
| | | // glassId不存在,将reportState改为6 |
| | | processedItem.put("reportState", 6); |
| | | } |
| | | |
| | | processedDamageList.add(processedItem); |
| | | } |
| | | } |
| | | |
| | | Map<String, Object> optimizeProject=glassOptimizeMapper.selectOptimizeDynamicProject(projectNo); |
| | | optimizeProject.put("engineeringRawQueueList", glassOptimizeMapper.selectOptimizeDynamicLayout(projectNo)); |
| | | optimizeProject.put("glassInfolList", glassOptimizeMapper.selectOptimizeDynamicDetail(projectNo)); |
| | | optimizeProject.put("flowCardInfoList", glassOptimizeMapper.selectFlowCardDynamicInfoList(projectNo)); |
| | | optimizeProject.put("damageList", processedDamageList); |
| | | ObjectMapper mapper = new ObjectMapper(); |
| | | mapper.registerModule(new JavaTimeModule()); |
| | | String jsonInputString = mapper.writeValueAsString(optimizeProject); |
| | | System.out.println("传给MES的数据"+ jsonInputString); |
| | | |
| | | //发送请求 |
| | | try(OutputStream os = conn.getOutputStream()) { |
| | | byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8); |
| | | os.write(input, 0, input.length); |
| | | } |
| | | |
| | | // 获取响应 |
| | | try(BufferedReader br = new BufferedReader( |
| | | new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) { |
| | | StringBuilder response = new StringBuilder(); |
| | | String responseLine; |
| | | while ((responseLine = br.readLine()) != null) { |
| | | response.append(responseLine.trim()); |
| | | } |
| | | System.out.println("MES第一次返回信息: " + response.toString()); |
| | | JSONObject obj = JSONObject.parseObject(response.toString()); |
| | | if(obj.get("code").equals(200)&&obj.get("data").equals(true)){ |
| | | saveState=true; |
| | | } |
| | | |
| | | } |
| | | |
| | | //关闭连接 |
| | | conn.disconnect(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | saveState= false; |
| | | } |
| | | return saveState; |
| | | } |
| | | |
| | | |
| | | public Boolean issuingDynamicProjects(String projectNo){ |
| | | boolean saveState=false; |
| | | try { |
| | | System.out.println(mesIp); |
| | | // 1. 创建URL对象 |
| | | URL url = new URL("http://" + mesIp + ":88/api/loadGlass/engineering/importEngineer"); |
| | | |
| | | // 2. 打开连接 |
| | | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| | | conn.setRequestMethod("POST"); |
| | | conn.setRequestProperty("Content-Type", "application/json"); |
| | | conn.setRequestProperty("Accept", "application/json"); |
| | | conn.setDoOutput(true); |
| | | |
| | | // 3. 准备请求体 |
| | | Map<String, Object> optimizeProject=glassOptimizeMapper.selectOptimizeDynamicProject(projectNo); |
| | | optimizeProject.put("engineeringRawQueueList", glassOptimizeMapper.selectOptimizeDynamicLayout(projectNo)); |
| | | optimizeProject.put("glassInfolList", glassOptimizeMapper.selectOptimizeDynamicDetail(projectNo)); |
| | | optimizeProject.put("flowCardInfoList", glassOptimizeMapper.selectFlowCardDynamicInfoList(projectNo)); |
| | | ObjectMapper mapper = new ObjectMapper(); |
| | | mapper.registerModule(new JavaTimeModule()); |
| | | String jsonInputString = mapper.writeValueAsString(optimizeProject); |
| | | |
| | | //发送请求 |
| | | try(OutputStream os = conn.getOutputStream()) { |
| | | byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8); |
| | | os.write(input, 0, input.length); |
| | | } |
| | | |
| | | // 获取响应 |
| | | try(BufferedReader br = new BufferedReader( |
| | | new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) { |
| | | StringBuilder response = new StringBuilder(); |
| | | String responseLine; |
| | | while ((responseLine = br.readLine()) != null) { |
| | | response.append(responseLine.trim()); |
| | | } |
| | | System.out.println("MES第二次返回信息: " + response.toString()); |
| | | JSONObject obj = JSONObject.parseObject(response.toString()); |
| | | if(obj.get("code").equals(200)&&obj.get("data").equals(true)){ |
| | | saveState=true; |
| | | } |
| | | |
| | | } |
| | | |
| | | //关闭连接 |
| | | conn.disconnect(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | saveState= false; |
| | | } |
| | | return saveState; |
| | | } |
| | | |
| | | |
| | | private Boolean callCutFilesApi(String jsonOptimizeResult) { |
| | | boolean saveState = false; |
| | | try { |
| | | System.out.println(mesIp); |
| | | // 1. 创建URL对象 |
| | | URL url = new URL("http://" + mesIp + ":8078/api/resultToCutFiles"); |
| | | |
| | | // 2. 打开连接 |
| | | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| | | conn.setRequestMethod("POST"); |
| | | conn.setRequestProperty("Content-Type", "application/json"); |
| | | conn.setRequestProperty("Accept", "application/json"); |
| | | conn.setDoOutput(true); |
| | | |
| | | // 3. 发送请求体 |
| | | try(OutputStream os = conn.getOutputStream()) { |
| | | byte[] input = jsonOptimizeResult.getBytes(StandardCharsets.UTF_8); |
| | | os.write(input, 0, input.length); |
| | | } |
| | | |
| | | // 获取响应 |
| | | try(BufferedReader br = new BufferedReader( |
| | | new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) { |
| | | StringBuilder response = new StringBuilder(); |
| | | String responseLine; |
| | | while ((responseLine = br.readLine()) != null) { |
| | | response.append(responseLine.trim()); |
| | | } |
| | | System.out.println("保存切割文件端口返回数据: " + response.toString()); |
| | | JSONObject obj = JSONObject.parseObject(response.toString()); |
| | | if(obj.get("code").equals(200)){ |
| | | saveState = true; |
| | | } |
| | | |
| | | } |
| | | |
| | | //关闭连接 |
| | | conn.disconnect(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | saveState = false; |
| | | } |
| | | return saveState; |
| | | } |
| | | } |