gaoluyang
2026-06-08 e35a067c05d61ff31f2bafacb3abb588179b0ecb
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
<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="检测结果:" 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-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-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"/>
              </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" 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" 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="stockInRatio">
                <el-input-number :step="0.01" :min="0" :max="100" style="width: 100%" v-model="form.stockInRatio" placeholder="请输入入库比例" clearable :precision="2" />
              </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 } 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(0); // 原材料检验类型
 
const data = reactive({
  form: {
    checkResult: '',
    testStandardId: '',
    quantity: undefined,
    qualifiedQuantity: undefined,
    unqualifiedQuantity: undefined,
    stockInRatio: 100.00,
    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" }],
    stockInRatio: [
      {
        validator: (rule, value, callback) => {
          if (value !== null && value !== undefined && value !== '') {
            if (value < 0 || value > 100) {
              callback(new Error('入库比例范围0~100'));
            } else {
              callback();
            }
          } else {
            callback();
          }
        },
        trigger: 'blur'
      }
    ],
  },
});
const { form, rules } = toRefs(data);
 
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;
  
  // 加载用户列表
  const userListsRes = await userListNoPage();
  userList.value = userListsRes.data;
  
  // 加载指标选项(根据第一个选中行的产品ID)
  if (rows && rows.length > 0 && rows[0].productId) {
    const params = {
      productId: rows[0].productId,
      inspectType: 0
    };
    const res = await qualityInspectDetailByProductId(params);
    testStandardOptions.value = res.data || [];
  } else {
    testStandardOptions.value = [];
  }
  
  // 重置表单
  form.value = {
    checkResult: '',
    testStandardId: '',
    quantity: undefined,
    qualifiedQuantity: undefined,
    unqualifiedQuantity: undefined,
    stockInRatio: 100.00,
    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) => {
  if (value === '合格') {
    // 合格时,合格数量等于数量,不合格数量为0
    form.value.qualifiedQuantity = form.value.quantity || 0;
    form.value.unqualifiedQuantity = 0;
  } else if (value === '不合格') {
    // 不合格时,合格数量为0,不合格数量等于数量
    form.value.qualifiedQuantity = 0;
    form.value.unqualifiedQuantity = form.value.quantity || 0;
  }
};
 
// 数量变化处理
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) {
      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>
</style>