spring
5 天以前 e268cb55fa740e5775bdeccc7c60e71c9cfdf30b
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
470
471
472
473
474
<template>
  <view class="list_box">
    <z-paging
      ref="pagingRef"
      :fixed="false"
      :auto-show-back-to-top="true"
      :loading-more-enabled="false"
      @query="getList"
    >
      <template #top>
        <CardTitle title="单丝领用" :hideAction="false">
          <template #action>
            <wd-button type="icon" icon="scan" color="#0D867F" @click="openScan"></wd-button>
            <wd-button
              type="icon"
              icon="add-circle"
              color="#0D867F"
              @click="openManualInput"
            ></wd-button>
          </template>
        </CardTitle>
      </template>
      <!-- <MonofilCard v-for="(item, index) in cardList" :key="index" :data="item" /> -->
      <wd-tabs v-model="tab" slidable="always" class="tabs-container">
        <block v-for="item in nodeList" :key="item">
          <wd-tab :title="item.twistedLayer" :name="item.twistedLayer">
            <scroll-view class="content" scroll-y>
              <MonofilCard v-for="(m, i) in item.strandedWireDish" :key="i" :data="m" />
            </scroll-view>
          </wd-tab>
        </block>
      </wd-tabs>
      <template #bottom>
        <view class="flex justify-center items-center">
          <wd-button block @click="save">
            <text class="text-[#fff]">保存</text>
          </wd-button>
        </view>
      </template>
    </z-paging>
    <Scan ref="scanRef" emitName="scanMono" />
    <StatisticsModal
      v-model:visible="showStatisticsModal"
      :nodeList="nodeList"
      @confirm="handleConfirmSave"
    />
 
    <!-- 手动输入质量追溯号弹框 -->
    <wd-popup v-model="showManualInput" position="center" :close-on-click-modal="false">
      <view class="manual-input-modal">
        <view class="modal-header">
          <text class="title">手动领用单丝</text>
          <wd-icon name="close" @click="closeManualInput" />
        </view>
        <view class="modal-content">
          <view class="input-label">单丝编号</view>
          <wd-input
            v-model="manualOutPutId"
            placeholder="请输入单丝编号"
            clearable
            @clear="manualOutPutId = ''"
          />
        </view>
        <view class="modal-footer">
          <wd-button @click="closeManualInput" type="default">取消</wd-button>
          <wd-button type="primary" @click="handleManualConfirm" custom-class="confirm-btn">
            确定
          </wd-button>
        </view>
      </view>
    </wd-popup>
 
    <wd-toast />
  </view>
</template>
 
<script setup lang="ts">
import CardTitle from "@/components/card-title/index.vue";
import MonofilCard from "../components/MonofilCard.vue";
import StatisticsModal from "../components/StatisticsModal.vue";
import { useToast } from "wot-design-uni";
import { onLoad, onUnload } from "@dcloudio/uni-app";
import Scan from "@/components/scan/index.vue";
import ManageApi from "@/api/product/manage";
import TwistApi from "@/api/product/twist";
import zPaging from "@/components/z-paging/z-paging.vue";
// import { useZebraScan } from "@/hooks/useZebraScan";
// const { init, start, stop, triggerScan } = useZebraScan();
 
const paramsId = ref();
const pagingRef = ref();
const scanRef = ref();
const toast = useToast();
const tab = ref("");
const showStatisticsModal = ref(false);
const showManualInput = ref(false);
const manualOutPutId = ref("");
 
// 监听标签切换
watch(tab, () => {
  if (tab.value) {
    console.log("tab.value:===========1", tab.value);
    getList();
  }
});
 
const getScanCode = async (code: any) => {
  console.log("自定义扫描的结果回调函数:", code);
  // let parseData = code.trim();
  console.log("code:===========", JSON.parse(code.code));
  console.log("id:=============", JSON.parse(code.code).id);
  try {
    // 检查是否已选择标签
    if (!tab.value) {
      toast.error("请先选择一个层");
      return;
    }
 
    // 找到当前选中的层
    console.log("tab.value:===========2", tab.value);
    const currentLayer = nodeList.value.find((node) => node.twistedLayer === tab.value);
    if (!currentLayer) {
      toast.error("未找到当前选中的层");
      return;
    }
    console.log("tab.value:===========3", currentLayer);
    // 在发起请求前,先校验该单丝是否已在当前或其他层级被领用
    const scannedOutputId = JSON.parse(code.code).id;
    const alreadyUsed = nodeList.value.some((node) =>
      (node.strandedWireDish || []).some((item: any) => item.outputId === scannedOutputId)
    );
    if (alreadyUsed) {
      toast.error("该单丝已领用,请勿重复扫码");
      return;
    }
    const { data } = await TwistApi.getScarn({
      outPutId: scannedOutputId,
      twistId: currentLayer.twistId,
    });
 
    // 检查当前层是否已存在该单丝
    const exists = currentLayer.strandedWireDish?.some(
      (item: any) => item.monofilamentNumber === data.monofilamentNumber
    );
    if (!exists) {
      const { id, outPutId, wireId, oneLength, ...rest } = data;
      const newItem = {
        wireId: paramsId.value,
        outputId: id,
        amount: oneLength,
        ongLength: oneLength,
        ...rest,
      };
 
      // 添加到当前层的 strandedWireDish 中
      if (!currentLayer.strandedWireDish) {
        currentLayer.strandedWireDish = [];
      }
      currentLayer.strandedWireDish.push(newItem);
 
      // 刷新当前层的数据显示
      getList();
    } else {
      toast.error("该单丝已领用,请勿重复扫码");
    }
  } catch (error) {
    toast.error("二维码异常,请更换二维码!");
  }
};
 
const openScan = () => {
  scanRef.value.triggerScan();
};
 
// 打开手动输入弹框
const openManualInput = () => {
  // 检查是否已选择标签
  if (!tab.value) {
    toast.error("请先选择一个层");
    return;
  }
  showManualInput.value = true;
};
 
// 关闭手动输入弹框
const closeManualInput = () => {
  showManualInput.value = false;
  manualOutPutId.value = "";
};
 
// 手动输入确认
const handleManualConfirm = async () => {
  if (!manualOutPutId.value.trim()) {
    toast.error("请输入质量追溯号");
    return;
  }
 
  try {
    const { data } = await TwistApi.selectByMonofilamentNumber({
      monofilamentNumber: manualOutPutId.value.trim(),
    });
 
    // 找到当前选中的层
    const currentLayer = nodeList.value.find((node) => node.twistedLayer === tab.value);
    if (!currentLayer) {
      toast.error("未找到当前选中的层");
      return;
    }
 
    // 检查当前层是否已存在该单丝
    const exists = currentLayer.strandedWireDish?.some(
      (item: any) => item.monofilamentNumber === data.monofilamentNumber
    );
 
    if (!exists) {
      const { id, outPutId, wireId, oneLength, ...rest } = data;
      const newItem = {
        wireId: paramsId.value,
        outputId: id,
        amount: oneLength,
        ongLength: oneLength,
        ...rest,
      };
 
      // 添加到当前层的 strandedWireDish 中
      if (!currentLayer.strandedWireDish) {
        currentLayer.strandedWireDish = [];
      }
      currentLayer.strandedWireDish.push(newItem);
 
      // 刷新当前层的数据显示
      getList();
 
      // 关闭弹框并清空输入
      closeManualInput();
      toast.success("添加成功");
    } else {
      toast.error("该单丝已领用,请勿重复添加");
    }
  } catch (error: any) {
    toast.error(error.msg || "查询失败");
  }
};
 
const getList = async () => {
  // 从当前选中层的 strandedWireDish 中获取数据
  const currentLayer = nodeList.value.find((node) => node.twistedLayer === tab.value);
  if (currentLayer && currentLayer.strandedWireDish) {
    pagingRef.value.complete(currentLayer.strandedWireDish);
  } else {
    pagingRef.value.complete([]);
  }
};
 
let nodeList = ref<any[]>([]);
// 获取根号
const getRootNumber = async (wireId: number) => {
  const { code, data, msg } = await TwistApi.getRootNumber({ wireId });
  if (code === 200) {
    nodeList.value = data.nodeList;
    if (data.otherStrandedWireDish && data.otherStrandedWireDish.length > 0) {
      nodeList.value.push({
        strandedWireDish: data.otherStrandedWireDish,
        twistedLayer: "其他",
        twistId: null,
      });
    }
    // 设置默认第一层
    if (nodeList.value && nodeList.value.length > 0 && !tab.value) {
      tab.value = nodeList.value[0].twistedLayer;
      console.log("设置默认第一层:", tab.value);
      // 设置默认标签后,加载第一层的数据
      getList();
    }
  } else {
    toast.error(msg || "获取根号失败");
  }
};
 
const save = () => {
  // 检查 nodeList 中是否有新的单丝数据需要保存
  let hasNewData = false;
 
  nodeList.value.forEach((node) => {
    if (node.strandedWireDish && Array.isArray(node.strandedWireDish)) {
      const hasNewInLayer = node.strandedWireDish.some(
        (item: { id?: number }) => item.id === undefined || item.id === null
      );
      if (hasNewInLayer) {
        hasNewData = true;
      }
    }
  });
 
  if (!hasNewData) {
    toast.error("没有新的单丝数据需要保存");
    return;
  }
 
  // 显示统计弹框
  showStatisticsModal.value = true;
};
 
const handleConfirmSave = async () => {
  // 从 nodeList 中收集所有需要保存的新单丝数据
  const newData: any[] = [];
 
  nodeList.value.forEach((node) => {
    node.strandedWireDish.forEach((item: any) => {
      item.saleTwistId = node.twistId;
    });
    if (node.strandedWireDish && Array.isArray(node.strandedWireDish)) {
      const layerNewData = node.strandedWireDish.filter(
        (item: { id?: number }) => item.id === undefined || item.id === null
      );
      newData.push(...layerNewData);
    }
  });
 
  if (newData.length === 0) {
    toast.error("没有新的单丝数据需要保存");
    return;
  }
 
  const { code, msg } = await TwistApi.addStrandedWireDish(newData);
  if (code == 200) {
    toast.success(msg);
 
    // 清空所有层中已保存的数据(给已保存的数据添加 id)
    nodeList.value.forEach((node) => {
      if (node.strandedWireDish && Array.isArray(node.strandedWireDish)) {
        node.strandedWireDish.forEach((item: any) => {
          if (item.id === undefined || item.id === null) {
            item.id = Date.now(); // 临时标记为已保存
          }
        });
      }
    });
 
    pagingRef.value.refresh();
    getList();
  } else {
    toast.error("保存失败");
  }
};
 
onLoad(async (options: any) => {
  // 开启广播监听事件
  uni.$on("scanMono", getScanCode);
  paramsId.value = options.id;
 
  // 调用获取根号接口
  getRootNumber(options.id);
  // getRootNumber(118);
});
onUnload(() => {
  // 开启广播监听事件
  uni.$off("scanMono", getScanCode);
});
</script>
 
<style lang="scss" scoped>
.list_box {
  height: calc(100vh - 50px);
  background: #f3f9f8;
  display: flex;
  flex-direction: column;
}
 
// 手动输入弹框样式
.manual-input-modal {
  width: 320px;
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
 
  .modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid #f0f0f0;
 
    .title {
      font-size: 18px;
      font-weight: 600;
      color: #333;
    }
  }
 
  .modal-content {
    padding: 20px;
 
    .input-label {
      font-size: 14px;
      color: #333;
      margin-bottom: 8px;
      font-weight: 500;
    }
  }
 
  .modal-footer {
    display: flex;
    gap: 12px;
    padding: 16px 20px;
    border-top: 1px solid #f0f0f0;
  }
}
 
:deep(.manual-input-modal .confirm-btn) {
  background-color: #0d867f !important;
  border-color: #0d867f !important;
 
  .wd-button__content {
    color: #fff !important;
  }
}
 
// tab容器样式
.tabs-container {
  height: calc(100vh - 200px);
  display: flex;
  flex-direction: column;
}
 
.content {
  height: calc(100vh - 200px);
  width: 100%;
}
 
:deep(.wd-button__content) {
  color: #0d867f;
}
:deep(.zp-paging-container) {
  background: transparent !important;
  flex: 1;
  display: flex;
  flex-direction: column;
}
:deep(.zp-paging-container-content) {
  background: transparent !important;
  flex: 1;
  display: flex;
  flex-direction: column;
}
:deep(.wd-tabs) {
  background: transparent !important;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
:deep(.wd-tabs__nav) {
  margin-bottom: 10px;
  flex-shrink: 0;
  position: sticky;
  top: 0;
  z-index: 10;
  background: #f3f9f8;
}
:deep(.wd-tabs__content) {
  flex: 1;
  overflow: visible;
}
:deep(.wd-tab__pane) {
  height: 100%;
}
:deep(.zp-paging-container-top) {
  flex-shrink: 0;
}
:deep(.zp-paging-container-bottom) {
  flex-shrink: 0;
}
</style>