spring
9 天以前 d991871847391f150dd9c5aa9c871a96b75b7880
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<template>
  <div>
    <el-dialog
        v-model="dialogFormVisible"
        :title="operationType === 'add' ? '新增不合格管理' : '编辑不合格管理'"
        width="70%"
        @close="closeDia"
    >
      <el-form :model="form" label-width="140px" label-position="top" :rules="rules" ref="formRef">
        <el-row :gutter="30">
          <el-col :span="12">
            <el-form-item label="产品名称:" prop="productId">
              <el-tree-select
                  v-if="operationType !== 'edit'"
                  v-model="form.productId"
                  placeholder="请选择"
                  clearable
                  check-strictly
                  @change="getModels"
                  :data="productOptions"
                  :render-after-expand="false"
                  style="width: 100%"
              />
              <!-- 编辑态:不依赖下拉选项回显,直接展示文本 -->
              <el-input
                  v-else
                  v-model="form.productName"
                  disabled
                  style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规格型号:" prop="productModelId">
              <el-select
                  v-if="operationType !== 'edit'"
                  v-model="form.productModelId"
                  placeholder="请选择"
                  clearable
                  filterable
                  readonly
                  @change="handleChangeModel"
              >
                <el-option
                    v-for="item in modelOptions"
                    :key="item.id"
                    :label="item.model"
                    :value="item.id"
                />
              </el-select>
              <!-- 编辑态:不展示规格型号列表,直接展示文本 -->
              <el-input
                  v-else
                  v-model="form.model"
                  disabled
                  style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="12">
            <el-form-item label="单位:" prop="unit">
              <el-input v-model="form.unit" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="批号:" prop="batchNo">
              <el-input style="width: 100%" v-model="form.batchNo" placeholder="请输入" clearable/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="12">
            <el-form-item label="检验类型:" prop="checkType">
              <el-select v-model="form.checkType">
                <el-option label="入厂检" :value="0"/>
                <el-option label="车间检" :value="1"/>
                <el-option label="出厂检" :value="2"/>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="检测结果:" prop="checkResult">
              <el-select v-model="form.checkResult">
                <el-option label="合格" :value="1"/>
                <el-option label="不合格" :value="0"/>
              </el-select>
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-row :gutter="30">
          <el-col :span="12">
            <el-form-item label="检验员:" prop="checkName">
              <el-select v-model="form.checkName" placeholder="请选择" clearable filterable style="width: 100%">
                <el-option v-for="item in userList" :key="item.nickName" :label="item.nickName"
                           :value="item.nickName"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检测日期:" prop="checkTime">
              <el-date-picker
                  v-model="form.checkTime"
                  type="date"
                  placeholder="请选择日期"
                  value-format="YYYY-MM-DD"
                  format="YYYY-MM-DD"
                  clearable
                  style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="12">
            <el-form-item label="不合格现象:" prop="defectivePhenomena">
              <el-input v-model="form.defectivePhenomena" placeholder="请输入" clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处理结果:" prop="dealResult">
              <el-select v-model="form.dealResult" placeholder="报废" disabled>
                <el-option
                    :label="item.label"
                    :value="item.value"
                    v-for="item in rejection_handling"
                    :key="item.value"
                    :disabled="String(item.label) !== '报废'"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="12">
            <el-form-item label="处理人:" prop="dealName">
              <el-select v-model="form.dealName" placeholder="请选择" clearable filterable style="width: 100%">
                <el-option v-for="item in userList" :key="item.nickName" :label="item.nickName"
                           :value="item.nickName"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处理日期:" prop="dealTime">
              <el-date-picker
                  v-model="form.dealTime"
                  type="date"
                  placeholder="请选择日期"
                  value-format="YYYY-MM-DD"
                  format="YYYY-MM-DD"
                  clearable
                  style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确认</el-button>
          <el-button @click="closeDia">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
 
<script setup>
import {ref, reactive, toRefs, getCurrentInstance} from "vue";
import {modelList, productTreeList} from "@/api/basicData/product.js";
import {
  getQualityUnqualifiedInfo,
  qualityUnqualifiedAdd,
  qualityUnqualifiedUpdate
} from "@/api/qualityManagement/nonconformingManagement.js";
import {userListNoPage} from "@/api/system/user.js";
const { proxy } = getCurrentInstance()
const emit = defineEmits(['close'])
 
const dialogFormVisible = ref(false);
const operationType = ref('')
const { rejection_handling } = proxy.useDict("rejection_handling")
 
const getScrapDealResultValue = () => {
  const list = rejection_handling?.value || rejection_handling || [];
  const scrap = (list || []).find((it) => String(it?.label ?? "") === "报废");
  return scrap?.value ?? "";
};
const data = reactive({
  form: {
    checkTime: "",
    checkName: "",
    productName: "",
    productId: "",
    productModelId: "",
    model: "",
    unit: "",
    batchNo: "",
    checkType: undefined,
    checkResult: "",
    defectivePhenomena: '',
    dealResult: '',
    dealName: "",
    dealTime: "",
  },
  rules: {
    checkTime: [{ required: true, message: "请选择检测日期", trigger: "change" }],
    checkName: [{ required: true, message: "请选择检验员", trigger: "change" }],
    productId: [{ required: true, message: "请选择产品名称", trigger: "change" }],
    productModelId: [{ required: true, message: "请选择规格型号", trigger: "change" }],
    batchNo: [{ required: true, message: "请输入批号", trigger: "blur" }],
    checkType: [{ required: true, message: "请选择检验类型", trigger: "change" }],
    checkResult: [{ required: true, message: "请选择检测结果", trigger: "change" }],
    dealResult: [{ required: true, message: "处理结果默认为报废", trigger: "change" }],
    dealTime: [{ required: true, message: "请选择处理日期", trigger: "change" }],
  },
});
const { form, rules } = toRefs(data);
const userList = ref([]);
const productOptions = ref([]);
const modelOptions = ref([])
 
// 打开弹框
const openDialog = async (type, row) => {
  operationType.value = type;
  dialogFormVisible.value = true;
  form.value = {}
 
  // 编辑态不校验规格型号(prop 仍绑定 productModelId,但编辑态改为文本展示)
  data.rules.productModelId = [
    {
      required: type !== "edit",
      message: "请选择规格型号",
      trigger: "change",
    },
  ];
 
  // 先加载下拉选项,确保编辑数据可以正确匹配回显
  const userRes = await userListNoPage();
  userList.value = userRes.data || [];
 
  await getProductOptions();
 
  // 处理结果默认“报废”,且不可选择其它项
  form.value.dealResult = getScrapDealResultValue();
  if (operationType.value === 'edit') {
    const fallback = row || {};
    const res = await getQualityUnqualifiedInfo(fallback.id);
    const { inspectState, ...rest } = res.data || {};
 
    // 先用列表行数据把“必回显字段”直接填上,避免详情接口字段名不一致导致全空。
    const productName = rest?.productName ?? fallback?.productName;
    const modelName = rest?.model ?? fallback?.model;
 
    const checkTypeValue = rest?.checkType ?? fallback?.checkType;
    const checkNameValue =
      rest?.checkName ??
      rest?.checkUserName ??
      fallback?.checkName ??
      fallback?.checkUserName;
 
    const productId =
      rest?.productId ??
      findProductIdByLabel(productOptions.value, productName);
 
    // 先回填字段(productModelId 需要依赖 modelOptions,稍后再补)
    const normalizedProductId = normalizeProductIdByOptions(productId);
 
    // 编辑态产品名称展示只展示 label,避免树组件回显依赖 value 匹配
    const productNameLabel =
      rest?.productName ??
      fallback?.productName ??
      findNodeById(productOptions.value, normalizedProductId) ??
      productName;
    form.value = {
      ...rest,
      productName: productNameLabel,
      productId: normalizedProductId,
      productModelId: rest?.productModelId ?? undefined,
      model: rest?.model ?? fallback?.model,
      unit: rest?.unit ?? fallback?.unit,
      batchNo: rest?.batchNo ?? fallback?.batchNo ?? "",
      checkType:
        checkTypeValue === undefined || checkTypeValue === null
          ? undefined
          : Number(checkTypeValue),
      checkName: checkNameValue ?? "",
      checkTime: rest?.checkTime ?? fallback?.checkTime ?? "",
      defectivePhenomena:
        rest?.defectivePhenomena ?? fallback?.defectivePhenomena ?? "",
      dealName: rest?.dealName ?? fallback?.dealName ?? "",
      dealTime: rest?.dealTime ?? fallback?.dealTime ?? "",
      dealResult: getScrapDealResultValue(),
    };
 
    // 规格型号下拉需要依赖 productId
    await loadModelsForProductId(form.value.productId);
 
    // 规格型号回显(如详情没给 productModelId,就用 model 名称反查)
    if (!form.value.productModelId) {
      form.value.productModelId = findModelIdByModel(
        modelOptions.value,
        modelName
      );
    }
 
    // 根据 productModelId 回填 model/unit
    if (form.value.productModelId) {
      form.value.productModelId = normalizeModelIdByOptions(form.value.productModelId);
      handleChangeModel(form.value.productModelId);
    } else if (modelName) {
      // productModelId 仍然拿不到时,至少保证 model/unit 文本回显
      const matched =
        (modelOptions.value || []).find((m) => {
          const model = String(m?.model ?? "");
          const id = String(m?.id ?? "");
          const target = String(modelName ?? "");
          return model === target || id === target;
        }) ?? null;
      if (matched) {
        form.value.model = matched.model ?? form.value.model;
        form.value.unit = matched.unit ?? form.value.unit;
      } else {
        form.value.model = modelName;
      }
    }
  }
}
const getProductOptions = async () => {
  const res = await productTreeList();
  productOptions.value = convertIdToValue(res);
};
const getModels = (value) => {
  form.value.productName = findNodeById(productOptions.value, value);
  form.value.productModelId = undefined;
  form.value.model = "";
  form.value.unit = "";
  modelOptions.value = [];
  modelList({ id: value }).then((res) => {
    modelOptions.value = res;
  })
};
 
// 编辑模式/或任意需要时:只拉取规格型号列表,不清空已回填的字段
const loadModelsForProductId = async (productId) => {
  if (!productId) return;
  const res = await modelList({ id: productId });
  modelOptions.value = res || [];
 
  // 让单位/型号等字段保持与当前 productModelId 一致
  if (form.value.productModelId) {
    form.value.productModelId = normalizeModelIdByOptions(form.value.productModelId);
    handleChangeModel(form.value.productModelId);
  }
};
 
const handleChangeModel = (value) => {
  form.value.model = modelOptions.value.find(item => item.id == value)?.model || '';
  form.value.unit = modelOptions.value.find(item => item.id == value)?.unit || '';
};
 
// 解决回显时类型不一致导致 el-tree-select / el-select 只显示 value(id)
const normalizeProductIdByOptions = (productId) => {
  if (productId === undefined || productId === null) return productId;
  const target = String(productId);
  const stack = Array.isArray(productOptions.value) ? [...productOptions.value] : [];
  while (stack.length) {
    const node = stack.shift();
    if (node && String(node?.value ?? "") === target) return node?.value;
    if (node?.children?.length) stack.push(...node.children);
  }
  return productId;
};
 
const normalizeModelIdByOptions = (modelId) => {
  if (modelId === undefined || modelId === null) return modelId;
  const target = String(modelId);
  return (modelOptions.value || []).find((m) => String(m?.id ?? "") === target)?.id ?? modelId;
};
const findNodeById = (nodes, productId) => {
  for (let i = 0; i < nodes.length; i++) {
    if (nodes[i].value === productId) {
      return nodes[i].label; // 找到节点,返回该节点
    }
    if (nodes[i].children && nodes[i].children.length > 0) {
      const foundNode = findNodeById(nodes[i].children, productId);
      if (foundNode) {
        return foundNode; // 在子节点中找到,返回该节点
      }
    }
  }
  return null; // 没有找到节点,返回null
};
 
// 根据树节点 label 回填 value(编辑回显兜底用)
const findProductIdByLabel = (nodes, label) => {
  const target = String(label ?? "");
  if (!target) return undefined;
 
  const stack = Array.isArray(nodes) ? [...nodes] : [];
  while (stack.length) {
    const node = stack.shift();
    if (node && String(node?.label ?? "") === target) return node?.value;
    if (node?.children?.length) stack.push(...node.children);
  }
  return undefined;
};
 
// 根据规格型号名称反查 id(编辑回显兜底用)
const findModelIdByModel = (models, model) => {
  const target = String(model ?? "");
  if (!target) return undefined;
  return (
    (models || []).find((m) => {
      const mModel = String(m?.model ?? "");
      const mId = String(m?.id ?? "");
      return mModel === target || mId === target;
    })?.id
  );
};
function convertIdToValue(data) {
  return data.map((item) => {
    const { id, children, ...rest } = item;
    const newItem = {
      ...rest,
      value: id, // 将 id 改为 value
    };
    if (children && children.length > 0) {
      newItem.children = convertIdToValue(children);
    }
    
    return newItem;
  });
}
// 提交产品表单
const submitForm = () => {
  proxy.$refs.formRef.validate(valid => {
    if (valid) {
      // 状态字段不在表单填写,也不传给后端
      const { inspectState, ...payload } = (form.value || {})
      if (operationType.value === "add") {
        qualityUnqualifiedAdd(payload).then(res => {
          proxy.$modal.msgSuccess("提交成功");
          closeDia();
        })
      } else {
        qualityUnqualifiedUpdate(payload).then(res => {
          proxy.$modal.msgSuccess("提交成功");
          closeDia();
        })
      }
    }
  })
}
// 关闭弹框
const closeDia = () => {
  proxy.resetForm("formRef");
  dialogFormVisible.value = false;
  emit('close')
};
defineExpose({
  openDialog,
});
</script>
 
<style scoped>
 
</style>