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
<template>
  <div>
    <el-dialog
      v-model="dialogVisible"
      title="快速检验"
      width="70%"
      @close="closeDialog"
    >
      <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="检验模式:">
              <el-tag :type="inspectModeTag" size="large">{{ inspectModeLabel }}</el-tag>
              <span v-if="showSampleFields && currentInspect" class="mode-tip">
                (抽检比例: {{ currentInspect.sampleRatio || 0 }}%)
              </span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检测结果:" prop="checkResult">
              <el-select v-model="form.checkResult" placeholder="请选择检测结果" style="width: 100%" @change="handleCheckResultChange">
                <el-option label="合格" value="合格" />
                <el-option label="不合格" value="不合格" />
                <el-option label="部分合格" value="部分合格" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="12">
            <el-form-item label="指标选择:" prop="testStandardId">
              <el-select v-model="form.testStandardId" placeholder="请选择指标" style="width: 100%" @change="handleTestStandardChange">
                <el-option
                  v-for="item in testStandardOptions"
                  :key="item.id"
                  :label="item.standardName || item.standardNo"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="showSampleFields">
            <el-form-item label="抽检数量:" prop="sampleQuantity">
              <el-input-number
                :step="1"
                :min="1"
                :max="maxSampleQuantity"
                style="width: 100%"
                v-model="form.sampleQuantity"
                placeholder="请输入抽检数量"
                :precision="0"
              />
              <span class="tip-text">最大可抽检: {{ maxSampleQuantity }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <template v-if="form.checkResult">
          <el-row :gutter="30">
            <el-col :span="12">
              <el-form-item label="检验数量:" prop="quantity">
                <el-input-number :step="0.01" :min="0" style="width: 100%" v-model="form.quantity" placeholder="请输入数量" clearable :precision="2" @change="handleQuantityChange" :disabled="showSampleFields"/>
                <span v-if="showSampleFields" class="tip-text">抽检模式下检验数量等于抽检数量</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="检测单位:" prop="checkCompany">
                <el-input v-model="form.checkCompany" placeholder="请输入检测单位" clearable/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="30">
            <el-col :span="12">
              <el-form-item label="合格数量:" prop="qualifiedQuantity">
                <el-input-number :step="0.01" :min="0" :max="form.quantity" style="width: 100%" v-model="form.qualifiedQuantity" placeholder="请输入合格数量" clearable :precision="2" @change="handleQualifiedQuantityChange"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="不合格数量:" prop="unqualifiedQuantity">
                <el-input-number :step="0.01" :min="0" :max="form.quantity" style="width: 100%" v-model="form.unqualifiedQuantity" placeholder="请输入不合格数量" clearable :precision="2" @change="handleUnqualifiedQuantityChange"/>
              </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 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>
        </template>
      </el-form>
      <PIMTable
        rowKey="id"
        :column="tableColumn"
        :tableData="tableData"
        :tableLoading="tableLoading"
        height="400"
      >
        <template #slot="{ row }">
          <el-input v-model="row.testValue" clearable/>
        </template>
      </PIMTable>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确认</el-button>
          <el-button @click="closeDialog">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
 
<script setup>
import { ref, reactive, toRefs, getCurrentInstance, nextTick, computed, watch } from "vue";
import { userListNoPage } from "@/api/system/user.js";
import { batchQuickInspect } from "@/api/qualityManagement/rawMaterialInspection.js";
import { qualityInspectDetailByProductId, getQualityTestStandardParamByTestStandardId } from "@/api/qualityManagement/metricMaintenance.js";
 
const { proxy } = getCurrentInstance();
const emit = defineEmits(['close', 'success']);
 
const dialogVisible = ref(false);
const userList = ref([]);
const selectedIds = ref([]);
const selectedRows = ref([]);
const testStandardOptions = ref([]);
const inspectType = ref(1); // 过程检验类型
const currentInspect = ref(null);
 
const data = reactive({
  form: {
    checkResult: '',
    testStandardId: '',
    quantity: undefined,
    sampleQuantity: undefined,
    qualifiedQuantity: undefined,
    unqualifiedQuantity: undefined,
    checkCompany: '',
    checkName: '',
    checkTime: '',
  },
  rules: {
    checkResult: [{ required: true, message: "请选择检测结果", trigger: "change" }],
    testStandardId: [{ required: true, message: "请选择指标", trigger: "change" }],
    quantity: [{ required: true, message: "请输入数量", trigger: "blur" }],
    qualifiedQuantity: [{ required: true, message: "请输入合格数量", trigger: "blur" }],
    unqualifiedQuantity: [{ required: true, message: "请输入不合格数量", trigger: "blur" }],
    checkCompany: [{ required: true, message: "请输入检测单位", trigger: "blur" }],
    checkName: [{ required: true, message: "请选择检验员", trigger: "change" }],
    checkTime: [{ required: true, message: "请选择检测日期", trigger: "change" }],
  },
});
const { form, rules } = toRefs(data);
 
// 检验模式标签
const inspectModeLabel = computed(() => {
  if (!currentInspect.value) return '全检';
  return currentInspect.value.inspectRule === 1 ? '抽检' : '全检';
});
 
const inspectModeTag = computed(() => {
  if (!currentInspect.value) return 'success';
  return currentInspect.value.inspectRule === 1 ? 'warning' : 'success';
});
 
// 是否显示抽检字段
const showSampleFields = computed(() => {
  return currentInspect.value && currentInspect.value.inspectRule === 1;
});
 
// 总数量
const totalQuantity = computed(() => {
  return currentInspect.value?.quantity || 0;
});
 
// 最大抽检数量
const maxSampleQuantity = computed(() => {
  return totalQuantity.value;
});
 
// 默认抽检数量(从检验单获取或按比例计算)
const defaultSampleQuantity = computed(() => {
  if (!currentInspect.value) return 0;
  if (currentInspect.value.sampleQuantity) {
    return currentInspect.value.sampleQuantity;
  }
  if (currentInspect.value.sampleRatio) {
    return Math.ceil(totalQuantity.value * currentInspect.value.sampleRatio / 100);
  }
  return totalQuantity.value;
});
 
// 监听抽检数量变化
watch(() => form.value.sampleQuantity, (val) => {
  if (showSampleFields.value && val) {
    form.value.quantity = val;
    // 更新合格/不合格数量
    if (form.value.checkResult === '合格') {
      form.value.qualifiedQuantity = val;
      form.value.unqualifiedQuantity = 0;
    } else if (form.value.checkResult === '不合格') {
      form.value.qualifiedQuantity = 0;
      form.value.unqualifiedQuantity = val;
    }
  }
});
 
const tableColumn = ref([
  {
    label: "指标",
    prop: "parameterItem",
  },
  {
    label: "单位",
    prop: "unit",
  },
  {
    label: "厂家标准值",
    prop: "standardValue",
  },
  {
    label: "内控值",
    prop: "controlValue",
  },
  {
    label: "检验值",
    prop: "testValue",
    dataType: 'slot',
    slot: 'slot',
  },
]);
const tableData = ref([]);
const tableLoading = ref(false);
 
// 打开弹框
const openDialog = async (ids, rows) => {
  selectedIds.value = ids;
  selectedRows.value = rows;
  dialogVisible.value = true;
 
  // 取第一条检验单的信息作为参考
  if (rows && rows.length > 0) {
    currentInspect.value = rows[0];
  } else {
    currentInspect.value = null;
  }
 
  // 加载用户列表
  const userListsRes = await userListNoPage();
  userList.value = userListsRes.data;
 
  // 加载指标选项(根据第一个选中行的产品ID)
  if (rows && rows.length > 0 && rows[0].productId) {
    const params = {
      productId: rows[0].productId,
      inspectType: 1
    };
    const res = await qualityInspectDetailByProductId(params);
    testStandardOptions.value = res.data || [];
  } else {
    testStandardOptions.value = [];
  }
 
  // 重置表单
  const totalQty = currentInspect.value?.quantity || undefined;
  const sampleQty = showSampleFields.value ? defaultSampleQuantity.value : undefined;
 
  form.value = {
    checkResult: '',
    testStandardId: '',
    quantity: showSampleFields.value ? sampleQty : totalQty,
    sampleQuantity: sampleQty,
    qualifiedQuantity: undefined,
    unqualifiedQuantity: undefined,
    checkCompany: '',
    checkName: '',
    checkTime: '',
  };
  tableData.value = [];
 
  await nextTick();
  proxy.$refs.formRef?.clearValidate();
};
 
// 指标选择变化处理
const handleTestStandardChange = async (testStandardId) => {
  if (!testStandardId) {
    tableData.value = [];
    return;
  }
  tableLoading.value = true;
  try {
    const res = await getQualityTestStandardParamByTestStandardId(testStandardId);
    tableData.value = (res.data || []).map(item => ({
      ...item,
      id: null,
      testValue: ''
    }));
  } catch (error) {
    console.error('获取标准参数失败:', error);
    tableData.value = [];
  } finally {
    tableLoading.value = false;
  }
};
 
// 检测结果变化处理
const handleCheckResultChange = (value) => {
  const qty = form.value.quantity || 0;
  if (value === '合格') {
    form.value.qualifiedQuantity = qty;
    form.value.unqualifiedQuantity = 0;
  } else if (value === '不合格') {
    form.value.qualifiedQuantity = 0;
    form.value.unqualifiedQuantity = qty;
  } else if (value === '部分合格') {
    form.value.qualifiedQuantity = undefined;
    form.value.unqualifiedQuantity = undefined;
  }
};
 
// 数量变化处理
const handleQuantityChange = (value) => {
  if (form.value.checkResult === '合格') {
    form.value.qualifiedQuantity = value || 0;
    form.value.unqualifiedQuantity = 0;
  } else if (form.value.checkResult === '不合格') {
    form.value.qualifiedQuantity = 0;
    form.value.unqualifiedQuantity = value || 0;
  }
};
 
// 合格数量变化处理
const handleQualifiedQuantityChange = (value) => {
  const quantity = form.value.quantity || 0;
  if (value > quantity) {
    proxy.$modal.msgWarning("合格数量不能大于检验数量");
    form.value.qualifiedQuantity = quantity;
    form.value.unqualifiedQuantity = 0;
  } else {
    form.value.unqualifiedQuantity = Number((quantity - value).toFixed(2));
  }
  updateCheckResult();
};
 
// 不合格数量变化处理
const handleUnqualifiedQuantityChange = (value) => {
  const quantity = form.value.quantity || 0;
  if (value > quantity) {
    proxy.$modal.msgWarning("不合格数量不能大于检验数量");
    form.value.unqualifiedQuantity = quantity;
    form.value.qualifiedQuantity = 0;
  } else {
    form.value.qualifiedQuantity = Number((quantity - value).toFixed(2));
  }
  updateCheckResult();
};
 
// 根据合格/不合格数量更新检测结果
const updateCheckResult = () => {
  const qualified = form.value.qualifiedQuantity || 0;
  const unqualified = form.value.unqualifiedQuantity || 0;
  const quantity = form.value.quantity || 0;
 
  if (qualified === quantity && unqualified === 0) {
    form.value.checkResult = '合格';
  } else if (unqualified === quantity && qualified === 0) {
    form.value.checkResult = '不合格';
  } else if (qualified > 0 && unqualified > 0) {
    form.value.checkResult = '部分合格';
  }
};
 
// 提交表单
const submitForm = () => {
  proxy.$refs.formRef.validate((valid) => {
    if (valid) {
      // 部分合格时校验数量
      if (form.value.checkResult === '部分合格') {
        const sum = (form.value.qualifiedQuantity || 0) + (form.value.unqualifiedQuantity || 0);
        if (sum > (form.value.quantity || 0)) {
          proxy.$modal.msgWarning('合格数量+不合格数量不能超过检验数量');
          return;
        }
      }
 
      const data = {
        ids: selectedIds.value,
        inspectType: inspectType.value,
        ...form.value,
        paramList: tableData.value
      };
      batchQuickInspect(data).then(res => {
        proxy.$modal.msgSuccess(res.msg || "快速检验完成");
        emit('success');
        closeDialog();
      });
    }
  });
};
 
// 关闭弹框
const closeDialog = () => {
  dialogVisible.value = false;
  emit('close');
};
 
defineExpose({
  openDialog,
});
</script>
 
<style scoped>
.mode-tip {
  margin-left: 10px;
  font-size: 13px;
  color: #909399;
}
 
.tip-text {
  display: block;
  font-size: 12px;
  color: #909399;
  margin-top: 4px;
}
</style>