廖井涛
2025-12-01 21eed4f6a8065bb59d0aaf7481dfe145dba74f2a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<script setup>
import ComputeCard from "@/views/pp/glassOptimize/page/ComputeCard.vue";
import ComputeDetail from "@/views/pp/glassOptimize/page/ComputeDetail.vue";
import Compute from "@/views/pp/glassOptimize/page/Compute.vue";
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({
  // 要传递给子组件的数据
});
 
// 监听子组件ComputeCard的数据
 
 
const  computeCardRef=ref(null)
 
 
const handleCardData = (data) => {
 
  computedCard.value = data;
  // 将 computed.value 合并到 computedCard.value 的最外层
  computedCard.value = {
    ...computed.value,
    ...computedCard.value
  };
};
 
// 监听子组件ComputeDetail的数据
const handleData = (data) => {
  computed.value = data;
};
 
onMounted(() => {
  if (props.project) {
    console.log('ceshiyixia',props.project)
    handleFetchData(props.project.projectNumber);
    //handleTableData(data);
    //handleDataReceive(data);
  }
});
 
// 在父组件中定义处理接收数据的函数
const handleDataReceive= async (data) => {
  console.log('接收到子组件的数据2:', data);
  // 处理数据,例如更新父组件的状态或调用后端API
 
}
 
 
// 用于存储从后端获取到的数据,初始化为空数组
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('获取项目信息失败');
  }
};
 
 
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) {
        ElMessage.error('未找到对应工程号的数据,请检查输入是否正确');
      } else if (res.code === 500) {
        ElMessage.error('服务器内部出现错误,请稍后再试');
      } else {
        ElMessage.warning(res.msg);
      }
    }
  } catch (error) {
    console.error('请求出错', error);
    ElMessage.error('请输入工程号!');
  }
};
 
const  handleTableData= async (data) => {
  // 处理接收到的表格数据
  console.log('父组件接收到的数据1:', data);
}
 
// 发送数据到钢化计算接口
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));
 
    // 发送数据到钢化计算接口
    const response = await requestTemp.post('/computeTempering/inputTemperingData', computedCard.value);
 
    console.log('后端原始响应:', response);
 
    // 检查响应结构并正确处理
    if (response && (response.code === 200 || response.status === 200)) {
      ElMessage.success('模拟计算成功!');
 
      // 处理返回的数据
      let resultData = response.data;
 
      // 如果data是字符串,需要解析JSON
      if (typeof resultData === 'string') {
        try {
          resultData = JSON.parse(resultData);
        } catch (parseError) {
          console.error('解析返回数据失败:', parseError);
          ElMessage.warning('返回数据格式异常,使用原始数据');
          // 即使解析失败也继续使用原始数据
        }
      }
 
      // 更新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: 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: []
        };
 
        // 创建异步请求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;
        });
 
        detailPromises.push(detailPromise);
      });
 
      // 等待所有异步请求完成
      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('获取流程卡详情失败');
      }
    } else {
      ElMessage.warning("没有需要钢化工艺的数据请直接保存")
      state.value = 3
      // 即使没有钢化数据,也要设置 max_qty 为 0
      computed.value.max_qty = 0;
      return
    }
 
    // 合并数据
    computedCard.value = {
      ...computed.value,
      ...computedCard.value
    };
 
    // console.log('最终发送到后端的数据:', computedCard.value);
 
    await sendToComputeTempering();
  };
};
 
 
 
//接受子组件ComputeCard的流程卡号
let projectRow = ref({
  processId:null,
  patchState:null,
  technologyNumber:null
})
const handleUpdateProcessId = (processId,patchState,technologyNumber) => {
  projectRow.value.processId = processId;
  projectRow.value.patchState = patchState;
};
 
const handleTechnologyNumberUpdate = (processId,patchState,technologyNumber) => {
  projectRow.value.technologyNumber = technologyNumber;
  projectRow.value.patchState = patchState;
};
 
 
 
</script>
 
<template>
  <div style="width: 100%; height: 100%;">
   
    <div id="compute">
      <compute  :data="computedData" :state="state"
                @fetch-data="handleFetchData"
                :project="props.project"
                @sendData="handleData"
                @simulate-click="handleSimulation" />
    </div>
 
    <div id="computeCard">
      <compute-card ref="computeCardRef" :table-data="receivedData"
                    :process-id="projectRow.processId===null?null:projectRow.processId"
                    :technology-number="projectRow.technologyNumber===null?null:projectRow.technologyNumber"
                    @upProcessId="handleUpdateProcessId"  @updateTechnologyNumber="handleTechnologyNumberUpdate" @sendData="handleCardData"
      />
    </div>
 
    <div id="computeDetail">
      <compute-detail :process-id="projectRow.processId===null?null:projectRow.processId"
                      :technology-number="projectRow.technologyNumber===null?null:projectRow.technologyNumber"
                      :patch-state="projectRow.patchState===null?null:projectRow.patchState"/>
    </div>
   
  </div>
</template>
 
<style scoped>
#compute{
  float: left;
  margin-top: -30px;
  margin-bottom: 2%;
  width: 100%;
  height: 37%;
}
#computeCard{
  width: 64%;
  height: 55%;
  float: left;
}
#computeDetail{
  margin-left: 1%;
  float: left;
  width: 35%;
  height: 55%;
}
</style>