廖井涛
10 小时以前 f7a2fcdda7f1120498c5c5f75c5a99955fc54b43
north-glass-erp/northglass-erp/src/views/pp/glassOptimize/GlassComputed.vue
@@ -5,11 +5,13 @@
import {onMounted, ref} from 'vue';
import {ElMessage, ElLoading} from "element-plus";
import request from "@/utils/request";
import requestTemp from "@/utils/requestTemp";
const props = defineProps({
  project : null
});
const state = ref()
const computed = ref(null);
const computedCard = ref(null);
const computedData = ref({
@@ -39,7 +41,7 @@
onMounted(() => {
  if (props.project) {
    console.log(props.project)
    console.log('ceshiyixia',props.project)
    handleFetchData(props.project.projectNumber);
    //handleTableData(data);
    //handleDataReceive(data);
@@ -56,14 +58,56 @@
// 用于存储从后端获取到的数据,初始化为空数组
const receivedData = ref([]);
const emit = defineEmits(['update:project']);
const fetchProjectInfo = async (projectNumber) => {
  try {
    const res = await request.post(`/glassOptimize/getProjectByProjectNo/${projectNumber}`);
    console.log('完整响应数据:', res); // 用于调试查看完整响应结构
    if (Number(res.code) === 200 && res.data && res.data.data && res.data.data.length > 0) {
      // 正确访问嵌套的数据结构
      const projectData = res.data.data[0]; // 注意这里是 res.data.data[0]
      const updatedProject = {
        ...props.project,
        glass_thickness: projectData.glass_thickness || '',
        glass_type: projectData.glass_type || ''
      };
      console.log('获取到的glass_thickness:', projectData.glass_thickness);
      console.log('获取到的glass_type:', projectData.glass_type);
      // // 通过 emit 更新 project
      emit('update:project', updatedProject);
      // 更新 computed.value
      computed.value = {
        ...computed.value,
        glass_thickness: projectData.glass_thickness,
        glass_type: projectData.glass_type
      };
      // 确保computed.value已初始化
      if (!computed.value) {
        computed.value = {};
      }
      computed.value.glass_thickness = projectData.glass_thickness;
      computed.value.glass_type = projectData.glass_type;
      console.log('设置后的computed.value:', computed.value);
    } else {
      console.warn('未找到项目信息或数据格式不正确');
    }
  } catch (error) {
    console.error('获取项目信息失败', error);
    ElMessage.error('获取项目信息失败');
  }
};
console.log(receivedData)
const handleFetchData = async (projectNumber) => {
  try {
    const res = await request.post(`/glassOptimize/selectProjectCompute/${projectNumber}`);
    if (Number(res.code) === 200 && res.data && res.data.data) {
      receivedData.value = res.data.data;
      // 获取项目信息
      await fetchProjectInfo(projectNumber);
    } else {
      console.error('请求出现问题,状态码:', res.code, ',错误信息:', res.msg);
      if (res.code === 404) {
@@ -85,271 +129,174 @@
  console.log('父组件接收到的数据1:', data);
}
const handleSimulation = async () => {
  const data=computeCardRef.value.selectFullData();
  if(props.project!=null){
    computed.value.glass_thickness=props.project.glass_thickness
    computed.value.glass_type=props.project.glass_type
  }
  data.forEach(item=>{
    const processCard = {
      process_no: item.processId,
      layers: item.technologyNumber,
      total_layers: item.total_layers,
      total_num: item.total_num,
      total_area: item.total_area,
      is_must: true,
      allow_rotate: item.allow_rotate,
      priority_level: 0,
      tempering: item.tempering,
      curtain_wall: item.curtain_wall,
      patch_state: item.patch_state,
      merge: item.merge,
      glass_details: []
    };
// 发送数据到钢化计算接口
const sendToComputeTempering = async () => {
  let loading=ElLoading.service({
    lock: true,
    text: '正在计算中,请稍候...',
    background: 'rgba(0, 0, 0, 0.7)'
  });
  try {
    console.log('发送到后端的数据', JSON.stringify(computedCard.value, null, 2));
    request.post(`/glassOptimize/selectComputeDetail/${item.processId}/${item.technologyNumber}/${item.patch_state}`).then((res) => {
      if(Number(res.code) === 200){
        res.data.data.forEach(items=>{
          const detail={
            process_id:null,
            technology_number:null,
            order_number:null,
            layers_number:null,
            max_width:null,
            max_height:null,
            child_width:null,
            child_height:null,
            quantity:null,
            patch_state:null
          }
          detail.process_id=items.process_id
          detail.technology_number=items.technology_number
          detail.order_number=items.order_number
          detail.layers_number=item.total_layers
          detail.max_width=items.maxwidth
          detail.max_height=items.maxheight
          detail.child_width=items.width
          detail.child_height=items.height
          detail.quantity=items.quantity
          detail.patch_state=item.patch_state
    // 发送数据到钢化计算接口
    const response = await requestTemp.post('/computeTempering/inputTemperingData', computedCard.value);
          processCard.glass_details.push(detail)
        })
    console.log('后端原始响应:', response);
    // 检查响应结构并正确处理
    if (response && (response.code === 200 || response.status === 200)) {
      ElMessage.success('模拟计算成功!');
      }else{
        ElMessage.warning(res.msg)
      }
    })
    computed.value.process_cards.push(processCard)
  })
  console.log(computed.value)
      // 处理返回的数据
      let resultData = response.data;
  /*request.post(`/glassOptimize/simulationCalculate`,computed.value).then((res) => {
    if(Number(res.code) === 200){
    }
  })*/
  /*try {
    const responses = await Promise.all(requests);
    // 整合数据
    const processData = responses.reduce((acc, res, index) => {
      // 检查res.data是否为对象,并且包含'data'字段
      if (typeof res.data === 'object' && res.data !== null && 'data' in res.data) {
        // 检查data是否为数组
        if (!Array.isArray(data)) {
          console.error(`响应数据中的'data'字段不是数组,process_id:`);
          return acc;
      // 如果data是字符串,需要解析JSON
      if (typeof resultData === 'string') {
        try {
          resultData = JSON.parse(resultData);
        } catch (parseError) {
          console.error('解析返回数据失败:', parseError);
          ElMessage.warning('返回数据格式异常,使用原始数据');
          // 即使解析失败也继续使用原始数据
        }
      }
        // 初始化process_card对象
      // 更新computedData以显示返回的结果
      computedData.value = {
        code: 200,
        data: resultData
      };
      console.log('接收到的处理后数据:', computedData.value);
    } else {
      // 处理错误情况
      const errorMessage = response?.msg || response?.message || '模拟计算失败';
      ElMessage.error(errorMessage);
    }
  } catch (error) {
    console.error('请求失败:', error);
    // 确保关闭加载提示
    if (loading) {
      loading.close();
    }
  }finally {
    loading.close();
  }
};
const handleSimulation = async () => {
  let arry = computeCardRef.value.selectFullData();
  let data = arry;
  data = data.reduce((acc, item) => {
    if (item.tempering) {
      acc.push(item);
    }
    return acc;
  }, []);
  if (data.length === arry.length) {
    state.value = 1
  } else {
    state.value = 2
  }
  // 计算钢化流程卡的数量总和
  let maxQty = 0;
  if (data.length > 0) {
    // 遍历所有勾选了钢化的流程卡,累加它们的数量
    arry.forEach(item => {
      if (item.tempering) {
        maxQty += item.total_num || 0;
      }
    });
    // 将计算出的钢化数量总和添加到 computed.value 中
    computed.value.max_qty = maxQty;
    if (data.length > 0) {
      // 创建所有异步请求的Promise数组
      const detailPromises = [];
      data.forEach(item => {
        const processCard = {
          process_no: data.processId,
          layers: data.technologyNumber,
          total_layers: data.total_layers,
          total_num: data.total_num,
          total_area: data.total_area,
          process_no: item.processId,
          layers: item.technologyNumber,
          total_layers: item.total_layers,
          total_num: item.total_num,
          total_area: item.total_area,
          is_must: true,
          allow_rotate: data.allow_rotate,
          allow_rotate: item.allow_rotate,
          priority_level: 0,
          tempering: data.tempering,
          curtain_wall: data.curtain_wall,
          patch_state: data.patch_state,
          merge: data.merge,
          tempering: item.tempering,
          curtain_wall: item.curtain_wall,
          patch_state: item.patch_state,
          merge: item.merge,
          glass_details: []
        };
        // 整合glass_details
        data.forEach(detail => {
          const matchedNewData = data.find(
              item => item.technology_number === detail.technology_number
          );
          processCard.glass_details.push({
            process_id: data.processId,
            technology_number: detail.technology_number,
            order_number: detail.order_number,
            layers_number: detail.layers_number,
            max_width: detail.width,
            max_height: detail.height,
            child_width: detail.child_width,
            child_height: detail.child_height,
            quantity: matchedNewData ? matchedNewData.quantity : 0,
            patch_state: 0
          });
        // 创建异步请求Promise并保存
        const detailPromise = request.post(`/glassOptimize/selectComputeDetail/${item.processId}/${item.technologyNumber}/${item.patch_state}`).then((res) => {
          if (Number(res.code) === 200) {
            res.data.data.forEach(items => {
              const detail = {
                process_id: items.process_id,
                technology_number: items.technology_number,
                order_number: items.order_number,
                layers_number: item.total_layers,
                max_width: items.maxwidth,
                max_height: items.maxheight,
                child_width: items.width,
                child_height: items.height,
                quantity: items.quantity,
                patch_state: item.patch_state
              }
              processCard.glass_details.push(detail)
            })
            return processCard;
          } else {
            ElMessage.warning(res.msg)
            return processCard;
          }
        }).catch(error => {
          console.error('获取详情失败:', error);
          return processCard;
        });
        // 将processCard添加到acc中
        if (!acc.process_cards) {
          acc.process_cards = [];
        }
        acc.process_cards.push(processCard);
        // 设置其他字段的值
        detailPromises.push(detailPromise);
      });
      } else {
        console.error(`响应数据格式不正确,process_id: ${processId}`);
      // 等待所有异步请求完成
      try {
        const completedProcessCards = await Promise.all(detailPromises);
        // 将完成的流程卡添加到computed.value中
        if (!computed.value.process_cards) {
          computed.value.process_cards = [];
        }
        computed.value.process_cards = completedProcessCards;
        console.log('完整的computed.value数据:', computed.value);
      } catch (error) {
        console.error('处理流程卡详情时出错:', error);
        ElMessage.error('获取流程卡详情失败');
      }
      return acc;
    }, {});
    if (data.length > 0) {
      // 假设所有条目的thickness和glassType相同
      processData.glass_thickness = data[0].thickness;
      processData.glass_type = data[0].glassType;
    } else {
      processData.glass_thickness = "";
      processData.glass_type = "";
      ElMessage.warning("没有需要钢化工艺的数据请直接保存")
      state.value = 3
      // 即使没有钢化数据,也要设置 max_qty 为 0
      computed.value.max_qty = 0;
      return
    }
    // 发送整合后的数据到父组件
    emit('sendData', processData);
  } catch (error) {
    console.error('请求失败:', error);
  }*/
  const loading = ElLoading.service({
      lock: true,
      text: '正在计算中,请稍候...',
      background: 'rgba(0, 0, 0, 0.7)'
    });
   console.log('发送到后端的数据',computedCard.value)
  const mockData = {
      code: 200,
      data: [
        {
          "glass_details": [
            {
              "angle": 0.0,
              "glass_type": 3,
              "height": 862,
              "layers_number": 2,
              "order_number": 33,
              "process_id": "NG24120028A005",
              "technology_number": 2,
              "tempering_feed_sequence": 14,
              "tempering_layout_id": 15,
              "width": 814,
              "x_coordinate": 1582.0,
              "y_coordinate": 3768.0
            },
            {
              "angle": 0.1,
              "glass_type": 4,
              "height": 862,
              "layers_number": 2,
              "order_number": 33,
              "process_id": "NG24120028A005",
              "technology_number": 2,
              "tempering_feed_sequence": 14,
              "tempering_layout_id": 15,
              "width": 814,
              "x_coordinate": 1582.0,
              "y_coordinate": 3768.0
            }
          ],
          "guidance": 15,
          "rackinfos": [
            "NG24120026A003-2",
            "NG24120027A003-2",
            "NG24120028A005-2"
          ],
          "ratioResult": [
            {
              "area": 10.53,
              "glass_total": 15,
              "ratio": 0.74,
              "tempering_layout_id": "13"
            },
            {
              "area": 10.53,
              "glass_total": 15,
              "ratio": 0.74,
              "tempering_layout_id": "14"
            },
            {
              "area": 9.82,
              "glass_total": 14,
              "ratio": 0.69,
              "tempering_layout_id": "15"
            }
          ],
          "resultSum": [15.0, 0.74]
        }
      ]
    // 合并数据
    computedCard.value = {
      ...computed.value,
      ...computedCard.value
    };
    if (mockData.code === 200) {
    loading.close();
    ElMessage.success('模拟计算成功!');
    }
    // 更新computedData
    computedData.value = mockData;
  // try {
  //   if (!computed.value ) {
  //     ElMessage.warning('请先加载数据再进行模拟计算');
  //     return;
  //   }
  //   // 显示加载提示
  //   const loading = ElLoading.service({
  //     lock: true,
  //     text: '正在计算中,请稍候...',
  //     background: 'rgba(0, 0, 0, 0.7)'
  //   });
    // console.log('最终发送到后端的数据:', computedCard.value);
  //   try {
  //     // 将数据提交到后端
  //     const response = await request.post('/glassOptimize/simulationCalculate', {
  //       data: computedCard.value
  //     });
  //     if (response.code === 200) {
  //       ElMessage.success('模拟计算成功!');
  //     } else {
  //       ElMessage.error(response.msg || '模拟计算失败');
  //     }
  //   } catch (error) {
  //     console.error('请求失败:', error);
  //     ElMessage.error('网络异常,请稍后再试');
  //   } finally {
  //     // 关闭加载提示
  //     loading.close();
  //   }
  // } catch (error) {
  //   console.error('请求失败:', error);
  //   ElMessage.error('网络异常,请稍后再试');
  // }
    await sendToComputeTempering();
  };
};
@@ -378,7 +325,11 @@
  <div style="width: 100%; height: 100%;">
   
    <div id="compute">
      <compute  :data="computedData"  @fetch-data="handleFetchData" :project="props.project"  @sendData="handleData"  @simulate-click="handleSimulation" />
      <compute  :data="computedData" :state="state"
                @fetch-data="handleFetchData"
                :project="props.project"
                @sendData="handleData"
                @simulate-click="handleSimulation" />
    </div>
    <div id="computeCard">