zhangwencui
6 天以前 bbb54adc8d9fd6ae6b87ac036c8b21f3ad7daef4
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
<script lang="ts" setup>
import type { PendingTask } from '../../../types';
import type { FormType } from '../../../pro/feedback/data';
 
import { computed, ref, watch } from 'vue';
 
import {
  MesAutoCodeRuleCode,
  MesProFeedbackStatusEnum,
} from '@vben/constants';
import { useUserStore } from '@vben/stores';
 
import { Button, message, Popconfirm } from 'ant-design-vue';
 
import { useVbenForm } from '#/adapter/form';
import { generateAutoCode } from '#/api/mes/md/autocode/record';
import {
  approveFeedback,
  createFeedback,
  getFeedback,
  rejectFeedback,
  submitFeedback,
  updateFeedback,
} from '#/api/mes/pro/feedback';
import { getRouteProcessByRouteAndProcess } from '#/api/mes/pro/route/process';
import { $t } from '#/locales';
 
import { useFormSchema } from '../../../pro/feedback/data';
 
defineOptions({ name: 'WorkbenchFeedbackTab' });
 
const props = defineProps<{
  task?: PendingTask | null;
  /** 外部触发刷新(如报工成功后刷新列表) */
  refreshKey?: number;
}>();
 
const emit = defineEmits<{
  reported: [];
}>();
 
const userStore = useUserStore();
const formType = ref<FormType>('create');
const feedbackId = ref<number>();
const feedbackStatus = ref<number>();
const originalSnapshot = ref('');
 
const isEditable = computed(() =>
  ['create', 'submit', 'update'].includes(formType.value),
);
const canSubmit = computed(
  () =>
    isEditable.value &&
    feedbackStatus.value === MesProFeedbackStatusEnum.PREPARE,
);
const canApprove = computed(() => formType.value === 'approve');
 
const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: { class: 'w-full' },
    formItemClass: 'col-span-1',
    labelWidth: 110,
  },
  layout: 'horizontal',
  schema: [],
  showDefaultActions: false,
  wrapperClass: 'grid-cols-3',
});
 
/** 根据 checkFlag 对齐数量 */
function alignQuantity(data: any) {
  if (data.checkFlag) {
    data.uncheckQuantity = data.feedbackQuantity;
    data.qualifiedQuantity = 0;
    data.unqualifiedQuantity = 0;
    data.laborScrapQuantity = 0;
    data.materialScrapQuantity = 0;
    data.otherScrapQuantity = 0;
  } else {
    data.feedbackQuantity =
      (data.qualifiedQuantity || 0) + (data.unqualifiedQuantity || 0);
    data.uncheckQuantity = 0;
  }
}
 
async function getAlignedData() {
  const data = (await formApi.getValues()) as any;
  alignQuantity(data);
  return data;
}
 
/** 加载 checkFlag */
async function resolveCheckFlag(routeId?: number, processId?: number) {
  if (!routeId || !processId) return true;
  try {
    const rp = await getRouteProcessByRouteAndProcess(routeId, processId);
    return rp?.checkFlag ?? false;
  } catch {
    return true;
  }
}
 
/** 任务变更时重置表单 */
async function resetForTask(task: PendingTask) {
  formType.value = 'create';
  feedbackId.value = undefined;
  feedbackStatus.value = undefined;
  originalSnapshot.value = '';
 
  formApi.setState({ schema: useFormSchema(formType.value, formApi) });
 
  const code = await generateAutoCode(MesAutoCodeRuleCode.PRO_FEEDBACK_CODE);
  const checkFlag = await resolveCheckFlag(task.routeId, task.processId);
 
  await formApi.setValues({
    code,
    type: 1,
    feedbackTime: Date.now(),
    feedbackUserId: userStore.userInfo?.id,
    taskId: task.id,
    workOrderId: task.workOrderId,
    routeId: task.routeId,
    processId: task.processId,
    workstationId: task.workstationId,
    itemId: task.itemId,
    itemCode: task.itemCode,
    itemName: task.itemName,
    itemSpecification: task.itemSpecification,
    unitMeasureName: task.unitMeasureName,
    scheduledQuantity: task.quantity,
    checkFlag,
  });
 
  originalSnapshot.value = JSON.stringify(await getAlignedData());
}
 
/** 保存 */
async function handleSave() {
  const { valid } = await formApi.validate();
  if (!valid) return;
 
  const data = await getAlignedData();
 
  if (formType.value === 'create') {
    const id = await createFeedback(data);
    feedbackId.value = id;
    feedbackStatus.value = MesProFeedbackStatusEnum.PREPARE;
    formType.value = 'update';
    formApi.setState({ schema: useFormSchema(formType.value, formApi) });
    await formApi.setFieldValue('id', id);
    message.success($t('common.createSuccess'));
  } else {
    await updateFeedback(data);
    message.success($t('common.updateSuccess'));
  }
  originalSnapshot.value = JSON.stringify(data);
  emit('reported');
}
 
/** 提交 */
async function handleSubmit() {
  const { valid } = await formApi.validate();
  if (!valid) return;
 
  const data = await getAlignedData();
  if (JSON.stringify(data) !== originalSnapshot.value) {
    await updateFeedback(data);
    originalSnapshot.value = JSON.stringify(data);
  }
  await submitFeedback(feedbackId.value!);
  message.success('报订单已提交');
  // 重置表单为新建模式
  if (props.task) {
    await resetForTask(props.task);
  }
  emit('reported');
}
 
/** 审批通过 */
async function handleApprove() {
  if (!feedbackId.value) return;
  const finished = await approveFeedback(feedbackId.value);
  message.success(
    finished ? '报订单已审批完成' : '报工成功,请等待质量检验完成!',
  );
  if (props.task) {
    await resetForTask(props.task);
  }
  emit('reported');
}
 
/** 审批驳回 */
async function handleReject() {
  if (!feedbackId.value) return;
  await rejectFeedback(feedbackId.value);
  message.success('报订单已驳回');
  if (props.task) {
    await resetForTask(props.task);
  }
  emit('reported');
}
 
/** 加载已有报工单进行编辑 */
async function loadFeedback(id: number) {
  const data = await getFeedback(id);
  feedbackId.value = data.id;
  feedbackStatus.value = data.status;
  formType.value = 'update';
  formApi.setState({ schema: useFormSchema(formType.value, formApi) });
  const checkFlag = await resolveCheckFlag(data.routeId, data.processId);
  await formApi.setValues({ ...data, checkFlag });
  originalSnapshot.value = JSON.stringify(await getAlignedData());
}
 
// 监听任务变更(仅当任务 ID 真正变化时才重置表单)
watch(
  () => props.task?.id,
  () => {
    if (props.task) {
      resetForTask(props.task);
    }
  },
  { immediate: true },
);
 
// 暴露方法供外部调用
defineExpose({ loadFeedback, handleSave });
</script>
 
<template>
  <div class="feedback-tab">
    <div v-if="!task" class="feedback-tab__empty">
      请从左侧工单列表选择一条任务
    </div>
    <template v-else>
      <div class="feedback-tab__header">
        <h3 class="feedback-tab__title">
          {{ formType === 'create' ? '新建报工' : '编辑报工' }}
        </h3>
        <span v-if="task.workOrderCode" class="feedback-tab__sub">
          工单: {{ task.workOrderCode }} {{ task.workOrderName }}
        </span>
      </div>
 
      <Form class="feedback-tab__form" />
 
      <div class="feedback-tab__actions">
        <Button
          v-if="isEditable"
          type="primary"
          @click="handleSave"
        >
          保存
        </Button>
        <Popconfirm
          v-if="canSubmit"
          title="确认提交该报订单?提交后将不能修改。"
          @confirm="handleSubmit"
        >
          <Button type="primary">{{ $t('common.submit') }}</Button>
        </Popconfirm>
        <Popconfirm
          v-if="canApprove"
          title="确认审批通过该报订单?"
          @confirm="handleApprove"
        >
          <Button type="primary">通过</Button>
        </Popconfirm>
        <Popconfirm
          v-if="canApprove"
          title="确认驳回该报订单?"
          @confirm="handleReject"
        >
          <Button danger>不通过</Button>
        </Popconfirm>
      </div>
    </template>
  </div>
</template>
 
<style lang="scss" scoped>
.feedback-tab {
  padding: 20px 24px;
  min-height: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: auto;
}
 
.feedback-tab__empty {
  padding: 64px 0;
  text-align: center;
  color: #8c8c8c;
  font-size: 16px;
}
 
.feedback-tab__header {
  display: flex;
  align-items: baseline;
  gap: 16px;
  margin-bottom: 20px;
  padding-bottom: 16px;
  border-bottom: 1px solid #f0f0f0;
}
 
.feedback-tab__title {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #1a1a1a;
}
 
.feedback-tab__sub {
  font-size: 14px;
  color: #8c8c8c;
}
 
.feedback-tab__form {
  flex: 1;
}
 
.feedback-tab__actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  padding-top: 16px;
  border-top: 1px solid #f0f0f0;
  margin-top: 16px;
}
</style>