huminmin
8 天以前 50dcd8345bc5d7baa6c1c8d914793175a86d0b50
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
<template>
  <el-dialog
    v-model="visible"
    title="选择工艺路线配置"
    width="1000px"
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <el-row :gutter="20">
      <el-col :span="24">
        <div class="dialog-topbar">
          <div>
            <div style="font-weight: 600; margin-bottom: 8px;">配置</div>
            <div style="font-size: 12px; margin-bottom: 8px;">
              <span v-if="boundRouteName" style="color: #67c23a;">已绑定:{{ boundRouteName }}</span>
              <span v-else style="color: #e6a23c;">未绑定</span>
            </div>
          </div>
        </div>
        <el-select
          v-model="selectedRouteId"
          filterable
          clearable
          placeholder="请选择工艺路线"
          style="width: 100%;"
          @change="handleRouteChange"
        >
          <el-option
            v-for="cfg in routeList"
            :key="cfg.routeId"
            :label="cfg.processRouteName"
            :value="cfg.routeId"
          />
        </el-select>
 
        <el-divider style="margin: 16px 0;" />
 
        <div style="font-weight: 600; margin-bottom: 8px;">步骤预览</div>
        <div style="font-size: 12px; color: #909399; margin-bottom: 10px;">
          根据所选配置展示流程图,勾选表示该工序已完成
        </div>
      </el-col>
 
      <el-col :span="24">
        <div class="process-diagram">
          <div v-if="steps.length === 0" class="process-diagram-empty">暂无步骤</div>
          <div
            v-for="(step, idx) in steps"
            :key="String(step.processId) + '_' + idx"
            class="process-diagram-segment"
          >
            <div class="process-diagram-node">
              <el-checkbox
                v-model="step.isCompleted"
                class="process-diagram-checkbox"
                @change="() => handleStepCompletedChange(step)"
              />
              <div class="process-diagram-index">{{ idx + 1 }}</div>
              <div class="process-diagram-name">{{ step.processName }}</div>
              <div class="process-diagram-status" :class="{ 'is-done': Number(step.isCompleted) === 1 }">
                {{ Number(step.isCompleted) === 1 ? "已完成" : "未完成" }}
              </div>
            </div>
            <div v-if="idx < steps.length - 1" class="process-diagram-arrow">→</div>
          </div>
        </div>
        <div v-if="selectedRouteId === null" style="margin-top: 10px; font-size: 12px; color: #909399;">
          请先选择一条已维护好的工艺路线
        </div>
      </el-col>
    </el-row>
 
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" :loading="saving" @click="confirmSelect">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
 
<script setup>
import { computed, getCurrentInstance, ref, watch } from "vue";
import { salesProcessFlowConfigList, salesProcessFlowConfigItemList } from "@/api/salesManagement/salesProcessFlowConfig.js";
 
const emit = defineEmits(["update:visible", "confirm"]);
 
const props = defineProps({
  visible: { type: Boolean, default: false },
  // 打开弹窗时的回显:若业务已绑定工艺路线则传入该 routeId;否则默认展示列表第一条
  defaultRouteId: { type: [Number, String, null], default: null },
  // 打开弹窗时的工序完成记录回显
  defaultRecordList: { type: Array, default: () => [] },
  // 页面提示:订单已绑定的工艺路线名称
  boundRouteName: { type: String, default: "" },
});
 
const { proxy } = getCurrentInstance();
 
const visible = computed({
  get() {
    return props.visible;
  },
  set(v) {
    emit("update:visible", v);
  },
});
 
const routeList = ref([]);
const selectedRouteId = ref(null);
const steps = ref([]);
const saving = ref(false);
 
const normalizeStepsFromApi = (list) => {
  if (!Array.isArray(list)) return [];
  return list.map((s, idx) => ({
    stepId: s.stepId ?? s.id ?? null,
    processRouteItemId: s.processRouteItemId ?? s.process_route_item_id ?? s.id ?? null,
    processId: s.processId ?? s.process_id ?? s.id ?? null,
    processName: s.processName ?? s.process_name ?? s.name ?? "",
    sortNo: s.sortNo ?? idx + 1,
    isCompleted: Boolean(Number(s.isCompleted ?? s.completed ?? 0)),
  }));
};
 
const normalizeRouteList = (list) => {
  if (!Array.isArray(list)) return [];
  return list.map((r) => ({
    routeId: r.routeId ?? r.id ?? null,
    processRouteName: r.processRouteName ?? r.routeName ?? r.name ?? "",
    isDefault: Boolean(r.isDefault),
  }));
};
 
const applyRecordListToSteps = (stepList, recordList) => {
  if (!Array.isArray(stepList) || stepList.length === 0) return stepList;
  if (!Array.isArray(recordList) || recordList.length === 0) return stepList;
 
  const recordMap = new Map(
    recordList
      .filter((item) => item && item.processRouteItemId !== null && item.processRouteItemId !== undefined)
      .map((item) => [String(item.processRouteItemId), item])
  );
 
  return stepList.map((step) => {
    const matched = recordMap.get(String(step.processRouteItemId));
    if (!matched) return step;
    return {
      ...step,
      isCompleted: Boolean(Number(matched.isCompleted ?? 0)),
      completedTime: matched.completedTime ?? matched.completed_time ?? null,
    };
  });
};
 
const fetchRouteList = async () => {
  // 选择弹窗:尽量一次性拉全,避免分页影响选择体验
  const res = await salesProcessFlowConfigList({ current: 1, size: 1000 });
  const records = res?.records ?? res?.data?.records ?? res?.data ?? res ?? [];
  routeList.value = normalizeRouteList(records).filter((r) => r.routeId !== null && r.routeId !== undefined && r.routeId !== "");
};
 
const fetchRouteSteps = async (routeId) => {
  if (!routeId) {
    steps.value = [];
    return;
  }
  const res = await salesProcessFlowConfigItemList(routeId);
  const raw = res?.data ?? res ?? [];
  const normalizedSteps = normalizeStepsFromApi(raw);
  if (String(routeId) === String(props.defaultRouteId)) {
    steps.value = applyRecordListToSteps(normalizedSteps, props.defaultRecordList);
    return;
  }
  steps.value = normalizedSteps;
};
 
watch(
  () => props.visible,
  async (v) => {
    if (v) {
      try {
        await fetchRouteList();
 
        // 回显绑定:
        // 1. 若传入 defaultRouteId,则优先使用它
        // 2. 否则优先选中标记为默认(isDefault=true)的工艺路线
        // 3. 若都没有,则回退为第一条
        const first = routeList.value?.[0] ?? null;
        const defaultRoute =
          routeList.value.find((r) => r.isDefault) ?? first;
        const desired = props.defaultRouteId ?? (defaultRoute ? defaultRoute.routeId : null);
        selectedRouteId.value = desired ?? null;
        await fetchRouteSteps(selectedRouteId.value);
      } catch {
        proxy?.$modal?.msgError?.("获取工艺路线配置失败");
      }
    }
  }
);
 
const handleRouteChange = async () => {
  await fetchRouteSteps(selectedRouteId.value);
};
 
const handleStepCompletedChange = (step) => {
  step.isCompleted = Boolean(step.isCompleted);
};
 
const handleClose = () => {
  emit("update:visible", false);
  saving.value = false;
};
 
const confirmSelect = async () => {
  if (saving.value) return;
  if (selectedRouteId.value === null || selectedRouteId.value === undefined || selectedRouteId.value === "") {
    proxy?.$modal?.msgWarning?.("请选择工艺路线");
    return;
  }
  saving.value = true;
  try {
    emit("confirm", {
      routeId: selectedRouteId.value,
      recordList: steps.value.map((step) => ({
        processRouteItemId: step.processRouteItemId,
        isCompleted: Number(step.isCompleted ?? 0),
      })),
    });
  } catch (e) {
    proxy?.$modal?.msgError?.("确认失败,请稍后重试");
  } finally {
    saving.value = false;
  }
};
</script>
 
<style scoped>
.process-diagram {
  display: flex;
  align-items: center;
  gap: 0;
  flex-wrap: nowrap;
  overflow-x: auto;
  padding: 10px 0;
}
 
.process-diagram-segment {
  display: flex;
  align-items: center;
}
 
.process-diagram-node {
  width: 160px;
  min-width: 160px;
  height: 78px;
  border: 1px solid #ebeef5;
  border-radius: 10px;
  background: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 10px 12px;
  margin-right: 10px;
  box-sizing: border-box;
  position: relative;
}
 
.process-diagram-checkbox {
  position: absolute;
  top: 8px;
  right: 8px;
}
 
.process-diagram-index {
  font-size: 12px;
  color: #909399;
  margin-bottom: 4px;
}
 
.process-diagram-name {
  font-size: 14px;
  font-weight: 600;
  color: #303133;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
 
.process-diagram-status {
  margin-top: 4px;
  font-size: 12px;
  color: #909399;
}
 
.process-diagram-status.is-done {
  color: #67c23a;
  font-weight: 600;
}
 
.process-diagram-arrow {
  font-size: 18px;
  color: #909399;
  margin-right: 14px;
  margin-left: -6px;
}
 
.process-diagram-empty {
  width: 100%;
  text-align: center;
  padding: 40px 0;
  color: #909399;
  border: 1px dashed #ebeef5;
  border-radius: 8px;
}
 
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
 
.dialog-topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
}
</style>