spring
6 天以前 4d9eac00f0c1613d6279fbc75affd9ce31df5c6d
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
import request from "@/utils/request";
import type { BaseResult, PageResult } from "@/models/base";
 
export type TimelinessMaterialType = "单丝" | "绞线";
 
export interface TimeSensitiveTaskQuery {
  current: number;
  size?: number;
  initiatorName?: string;
  machineName?: string;
  materialType?: TimelinessMaterialType | string;
  taskStatus?: number | string;
  startTime?: string;
  endTime?: string;
}
 
export interface TimeSensitiveTaskForm {
  materialType: TimelinessMaterialType;
  initiatorName: string;
  initiateTime: string;
  setTemperature: number | string;
  agingDuration: number | string;
  machineName: string;
  inspectorName: string;
}
 
export interface TimeSensitiveTaskStartForm {
  id: number | string;
  initiatorName: string;
  initiateTime: string;
  setTemperature: number | string;
  agingDuration: number | string;
  machineName: string;
  furnaceInTime: string;
  inspectorName: string;
}
 
export interface TimeSensitiveCandidateQuery {
  current: number;
  size?: number;
  taskId: number | string;
  materialType: TimelinessMaterialType;
  categoryName?: string;
  model?: string;
  materialCode?: string;
}
 
export interface TimeSensitiveTaskItemInput {
  itemId: number | string;
  /** 单丝传“铁架号-铁牌序号”,绞线传数字序号。 */
  ironPlateNo: number | string;
}
 
export interface TimelinessInspectionDetailForm {
  id?: number | string;
  routingInspectionUser?: string;
  zoneOne?: number | string;
  zoneTwo?: number | string;
  zoneThree?: number | string;
  zoneFour?: number | string;
  zoneFive?: number | string;
  zoneSix?: number | string;
  timelinessInspectionId?: number | string;
}
 
/** 本地演示开关;正常运行时始终请求与 PC 端一致的真实接口。 */
export const USE_TIME_SENSITIVE_MOCK = false;
 
const mockTasks: any[] = [
  {
    id: 9001,
    taskCode: "SX-20260904-001",
    materialType: "单丝",
    initiatorName: "张工",
    initiateTime: "2026-09-04 09:10:00",
    setTemperature: 180,
    agingDuration: "2+4+1",
    machineName: "时效炉-01",
    itemCount: 2,
    furnaceInTime: "",
    furnaceOutTime: "",
    inspectorName: "李师傅",
    taskStatus: 0,
  },
  {
    id: 9002,
    taskCode: "JX-20260904-001",
    materialType: "绞线",
    initiatorName: "王工",
    initiateTime: "2026-09-04 08:30:00",
    setTemperature: 175,
    agingDuration: "2+3+1",
    machineName: "时效炉-02",
    itemCount: 2,
    furnaceInTime: "2026-09-04 10:00:00",
    furnaceOutTime: "",
    inspectorName: "赵师傅",
    taskStatus: 1,
  },
  {
    id: 9003,
    taskCode: "SX-20260903-001",
    materialType: "单丝",
    initiatorName: "刘工",
    initiateTime: "2026-09-03 08:00:00",
    setTemperature: 180,
    agingDuration: "2+4+1",
    machineName: "时效炉-01",
    itemCount: 1,
    furnaceInTime: "2026-09-03 08:30:00",
    furnaceOutTime: "2026-09-03 15:30:00",
    inspectorName: "孙师傅",
    taskStatus: 2,
  },
];
 
const mockMaterialPool: Record<TimelinessMaterialType, any[]> = {
  单丝: [
    {
      id: 9101,
      materialType: "单丝",
      model: "Φ5.6",
      poleModel: "1A60 H12 Φ9.5 A2",
      rodNo: "720526083102-014",
      batchCode: "750826090101-005",
      monofilamentNumber: "750826090101-005",
      weight: 2875,
      supplier: "山东元旺电工科技有限公司",
    },
    {
      id: 9102,
      materialType: "单丝",
      model: "L-3.45",
      poleModel: "1A60-H14 Φ9.5 A6",
      rodNo: "710726081602-003",
      batchCode: "750426083002-037",
      monofilamentNumber: "750426083002-037",
      weight: 2650,
      supplier: "山东元旺电工科技有限公司",
    },
    {
      id: 9301,
      outputId: 9301,
      materialType: "单丝",
      model: "8030-95L",
      poleModel: "8030",
      rodNo: "720626082404-001",
      batchCode: "750626083101-001",
      monofilamentNumber: "750626083101-001",
      weight: 2400,
      supplier: "山东元旺电工科技有限公司",
    },
    {
      id: 9302,
      outputId: 9302,
      materialType: "单丝",
      model: "Φ3.20",
      poleModel: "1A60 H14 Φ9.5 A6",
      rodNo: "710726090401-002",
      batchCode: "750726090401-002",
      monofilamentNumber: "750726090401-002",
      weight: 2520,
      supplier: "山东元旺电工科技有限公司",
    },
    {
      id: 9501,
      materialType: "单丝",
      model: "Φ4.50",
      poleModel: "1A60 H12 Φ9.5 A2",
      rodNo: "720526083102-018",
      batchCode: "750826090201-001",
      monofilamentNumber: "750826090201-001",
      weight: 2780,
      supplier: "山东元旺电工科技有限公司",
    },
  ],
  绞线: [
    {
      id: 9201,
      outputId: 9201,
      materialType: "绞线",
      model: "JLHA1/G1A-16/3-6/1",
      batchCode: "JX260904001-01",
      weight: 1180,
    },
    {
      id: 9202,
      outputId: 9202,
      materialType: "绞线",
      model: "JLHA1/G1A-25/4-6/1",
      batchCode: "JX260904001-02",
      weight: 1260,
    },
    {
      id: 9401,
      outputId: 9401,
      materialType: "绞线",
      model: "JLHA1/G1A-35/6-6/1",
      batchCode: "JX260904002-01",
      weight: 1380,
    },
    {
      id: 9402,
      outputId: 9402,
      materialType: "绞线",
      model: "JLHA1/G1A-50/8-6/1",
      batchCode: "JX260904002-02",
      weight: 1460,
    },
  ],
};
 
const mockMaterialDetails: Record<string, Record<string, any>> = {
  "9101": {
    qualitySetTemperature: 180,
    qualitySetDuration: "2+4+1",
    confirmTime: "2026-09-04 08:35:00",
    confirmUserName: "张工",
    status: 0,
    ledgerNo: "SO2609010188",
    schedulingDetailNo: "LS-PC-260904-001",
    deviceCode: "LS006",
    clientName: "山东元旺电工科技有限公司",
    productionDate: "2026-09-04",
    groupTeam: "白班",
    groupTeamLeader: "王班长",
    productionNumber: 1,
    unit: "盘",
  },
  "9102": {
    qualitySetTemperature: 175,
    qualitySetDuration: "2+3+1",
    confirmTime: "2026-09-04 08:42:00",
    confirmUserName: "张工",
    status: 0,
    ledgerNo: "SO2609010192",
    schedulingDetailNo: "LS-PC-260904-002",
    deviceCode: "LS008",
    clientName: "华东电缆有限公司",
    productionDate: "2026-09-04",
    groupTeam: "白班",
    groupTeamLeader: "王班长",
    productionNumber: 1,
    unit: "盘",
  },
  "9301": {
    qualitySetTemperature: 180,
    qualitySetDuration: "2+4+1",
    confirmTime: "2026-09-05 09:15:00",
    confirmUserName: "李工",
    status: 0,
    ledgerNo: "SO2609050106",
    schedulingDetailNo: "LS-PC-260905-006",
    deviceCode: "LS003",
    clientName: "山东元旺电工科技有限公司",
    productionDate: "2026-09-05",
    groupTeam: "白班",
    groupTeamLeader: "赵班长",
    productionNumber: 1,
    unit: "盘",
  },
  "9302": {
    qualitySetTemperature: 180,
    qualitySetDuration: "2+4+1",
    confirmTime: "2026-09-05 09:32:00",
    confirmUserName: "李工",
    status: 0,
    ledgerNo: "SO2609050112",
    schedulingDetailNo: "LS-PC-260905-007",
    deviceCode: "LS005",
    clientName: "华北线缆有限公司",
    productionDate: "2026-09-05",
    groupTeam: "白班",
    groupTeamLeader: "赵班长",
    productionNumber: 1,
    unit: "盘",
  },
  "9501": {
    qualitySetTemperature: 180,
    qualitySetDuration: "2+4+1",
    confirmTime: "2026-09-03 10:20:00",
    confirmUserName: "刘工",
    status: 2,
    ledgerNo: "SO2609030088",
    schedulingDetailNo: "LS-PC-260903-003",
    deviceCode: "LS002",
    clientName: "鲁中电缆有限公司",
    productionDate: "2026-09-03",
    groupTeam: "中班",
    groupTeamLeader: "孙班长",
    productionNumber: 1,
    unit: "盘",
  },
  "9201": {
    qualitySetTemperature: 175,
    qualitySetDuration: "2+3+1",
    confirmTime: "2026-09-04 09:05:00",
    confirmUserName: "王工",
    status: 1,
    ledgerNo: "SO2609020116",
    schedulingDetailNo: "JX-PC-260904-001",
    deviceCode: "JX003",
    clientName: "华北电缆有限公司",
    productionDate: "2026-09-04",
    groupTeam: "中班",
    groupTeamLeader: "赵班长",
    productionNumber: 1,
    productionWeight: 1180,
    unit: "盘",
  },
  "9202": {
    qualitySetTemperature: 175,
    qualitySetDuration: "2+3+1",
    confirmTime: "2026-09-04 09:12:00",
    confirmUserName: "王工",
    status: 1,
    ledgerNo: "SO2609020118",
    schedulingDetailNo: "JX-PC-260904-002",
    deviceCode: "JX004",
    clientName: "华北电缆有限公司",
    productionDate: "2026-09-04",
    groupTeam: "中班",
    groupTeamLeader: "赵班长",
    productionNumber: 1,
    productionWeight: 1260,
    unit: "盘",
  },
  "9401": {
    qualitySetTemperature: 175,
    qualitySetDuration: "2+3+1",
    confirmTime: "2026-09-05 10:10:00",
    confirmUserName: "李工",
    status: 0,
    ledgerNo: "SO2609050216",
    schedulingDetailNo: "JX-PC-260905-002",
    deviceCode: "JX006",
    clientName: "华北电缆有限公司",
    productionDate: "2026-09-05",
    groupTeam: "中班",
    groupTeamLeader: "赵班长",
    productionNumber: 1,
    productionWeight: 1380,
    unit: "盘",
  },
  "9402": {
    qualitySetTemperature: 175,
    qualitySetDuration: "2+3+1",
    confirmTime: "2026-09-05 10:22:00",
    confirmUserName: "李工",
    status: 0,
    ledgerNo: "SO2609050220",
    schedulingDetailNo: "JX-PC-260905-003",
    deviceCode: "JX007",
    clientName: "华南电缆有限公司",
    productionDate: "2026-09-05",
    groupTeam: "中班",
    groupTeamLeader: "赵班长",
    productionNumber: 1,
    productionWeight: 1460,
    unit: "盘",
  },
};
 
[...mockMaterialPool.单丝, ...mockMaterialPool.绞线].forEach((item) => {
  Object.assign(item, mockMaterialDetails[String(item.id)] || {});
});
 
const findMockMaterial = (id: number | string) =>
  [...mockMaterialPool.单丝, ...mockMaterialPool.绞线].find(
    (item) => String(item.id) === String(id)
  );
 
const cloneMockMaterial = (id: number | string) => {
  const item = findMockMaterial(id);
  return item ? { ...item } : undefined;
};
 
const cloneMockMaterialWithIronPlate = (id: number | string, ironPlateNo: number | string) => {
  const item = cloneMockMaterial(id);
  return item ? { ...item, ironPlateNo } : undefined;
};
 
const mockItems: Record<string, any[]> = {
  "9001": [cloneMockMaterialWithIronPlate(9101, 1), cloneMockMaterialWithIronPlate(9102, 2)].filter(
    Boolean
  ),
  "9002": [cloneMockMaterialWithIronPlate(9201, 1), cloneMockMaterialWithIronPlate(9202, 2)].filter(
    Boolean
  ),
  "9003": [cloneMockMaterialWithIronPlate(9501, 1)].filter(Boolean),
};
 
const mockInspections: Record<string, any[]> = {
  "9002": [
    {
      id: 9601,
      routingInspectionUser: "赵师傅",
      zoneOne: 175,
      zoneTwo: 175.2,
      zoneThree: 174.8,
      zoneFour: 175.1,
      zoneFive: 175,
      zoneSix: 174.9,
      createTime: "2026-09-04 11:00:00",
      timelinessInspectionId: 9002,
    },
  ],
  "9003": [
    {
      id: 9602,
      routingInspectionUser: "孙师傅",
      zoneOne: 180,
      zoneTwo: 180,
      zoneThree: 179.8,
      zoneFour: 180.1,
      zoneFive: 180,
      zoneSix: 179.9,
      createTime: "2026-09-03 10:30:00",
      timelinessInspectionId: 9003,
    },
  ],
};
 
const mockOperations: Record<string, any[]> = {
  "9001": [
    {
      id: 9701,
      operationType: 1,
      operationTypeDesc: "新增",
      beforeStatus: "",
      afterStatus: "待开始",
      operationDetail: "张工创建时效任务",
      operatorName: "张工",
      operateTime: "2026-09-04 09:10:00",
    },
  ],
  "9002": [
    {
      id: 9702,
      operationType: 2,
      operationTypeDesc: "开始时效",
      beforeStatus: "待开始",
      afterStatus: "时效中",
      operationDetail: "赵师傅确认任务信息并开始时效",
      operatorName: "赵师傅",
      operateTime: "2026-09-04 10:00:00",
    },
  ],
  "9003": [
    {
      id: 9703,
      operationType: 3,
      operationTypeDesc: "结束时效",
      beforeStatus: "时效中",
      afterStatus: "时效结束",
      operationDetail: "孙师傅结束时效任务",
      operatorName: "孙师傅",
      operateTime: "2026-09-03 15:30:00",
    },
  ],
};
 
const mockUsers = [
  { userId: 1, nickName: "张工" },
  { userId: 2, nickName: "王工" },
  { userId: 3, nickName: "刘工" },
  { userId: 4, nickName: "李师傅" },
  { userId: 5, nickName: "赵师傅" },
  { userId: 6, nickName: "孙师傅" },
];
 
const mockMachines = [
  { deviceId: 1, deviceModel: "时效炉-01" },
  { deviceId: 2, deviceModel: "时效炉-02" },
  { deviceId: 3, deviceModel: "时效炉-03" },
];
 
const pad = (value: number) => String(value).padStart(2, "0");
const mockDateTime = () => {
  const date = new Date();
  return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(
    date.getHours()
  )}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
};
 
const mockResult = <T>(data: T, msg = "success") =>
  new Promise<BaseResult<T>>((resolve) => {
    setTimeout(() => resolve({ code: 200, msg, data }), 120);
  });
 
const mockMaterialCode = (item: any) =>
  String(item?.flowCode || item?.batchCode || item?.monofilamentNumber || "").trim();
 
const updateMockItemCount = (taskId: number | string) => {
  const task = mockTasks.find((item) => String(item.id) === String(taskId));
  if (task) task.itemCount = (mockItems[String(taskId)] || []).length;
};
 
const addMockOperation = (
  taskId: number | string,
  operationType: number,
  operationTypeDesc: string,
  beforeStatus: string,
  afterStatus: string,
  operationDetail: string,
  operatorName: string
) => {
  const key = String(taskId);
  mockOperations[key] ||= [];
  mockOperations[key].unshift({
    id: Date.now(),
    operationType,
    operationTypeDesc,
    beforeStatus,
    afterStatus,
    operationDetail,
    operatorName,
    operateTime: mockDateTime(),
  });
};
 
const TimeSensitiveTaskApi = {
  queryPage(params: TimeSensitiveTaskQuery) {
    if (USE_TIME_SENSITIVE_MOCK) {
      let rows = [...mockTasks];
      if (params.initiatorName) {
        rows = rows.filter((item) => item.initiatorName.includes(params.initiatorName!));
      }
      if (params.machineName) {
        rows = rows.filter((item) => item.machineName.includes(params.machineName!));
      }
      if (params.materialType) {
        rows = rows.filter((item) => item.materialType === params.materialType);
      }
      if (params.taskStatus !== undefined && params.taskStatus !== "") {
        rows = rows.filter((item) => Number(item.taskStatus) === Number(params.taskStatus));
      }
      if (params.startTime) {
        rows = rows.filter((item) => item.initiateTime >= params.startTime!);
      }
      if (params.endTime) {
        rows = rows.filter((item) => item.initiateTime <= params.endTime!);
      }
      const current = Number(params.current || 1);
      const size = Number(params.size || 10);
      return mockResult<PageResult>({
        records: rows.slice((current - 1) * size, current * size),
        total: rows.length,
        current,
        size,
        pages: current * size < rows.length,
      });
    }
    return request<BaseResult<PageResult>>({
      url: "/timeSensitiveTask/queryPage",
      method: "POST",
      data: params,
    });
  },
 
  getDetail(id: number | string) {
    if (USE_TIME_SENSITIVE_MOCK) {
      return mockResult(mockTasks.find((item) => String(item.id) === String(id)) || null);
    }
    return request<BaseResult<any>>({
      url: "/timeSensitiveTask/getDetailById",
      method: "POST",
      data: id as any,
    });
  },
 
  getItems(taskId: number | string) {
    if (USE_TIME_SENSITIVE_MOCK) {
      return mockResult<any[]>((mockItems[String(taskId)] || []).map((item) => ({ ...item })));
    }
    return request<BaseResult<any[]>>({
      url: "/timeSensitiveTask/getItems",
      method: "POST",
      data: taskId as any,
    });
  },
 
  create(data: TimeSensitiveTaskForm) {
    if (USE_TIME_SENSITIVE_MOCK) {
      const id = Date.now();
      const taskCode = `${data.materialType === "单丝" ? "SX" : "JX"}-${String(id).slice(-10)}`;
      mockTasks.unshift({
        id,
        taskCode,
        ...data,
        itemCount: 0,
        furnaceInTime: "",
        furnaceOutTime: "",
        taskStatus: 0,
      });
      mockItems[String(id)] = [];
      addMockOperation(
        id,
        1,
        "新增",
        "",
        "待开始",
        `${data.initiatorName}创建时效任务`,
        data.initiatorName
      );
      return mockResult({ id, taskCode }, "时效任务新增成功");
    }
    return request<BaseResult<any>>({
      url: "/timeSensitiveTask/add",
      method: "POST",
      data,
    });
  },
 
  update(data: TimeSensitiveTaskForm & { id: number | string }) {
    if (USE_TIME_SENSITIVE_MOCK) {
      const task = mockTasks.find((item) => String(item.id) === String(data.id));
      if (task) Object.assign(task, data);
      addMockOperation(
        data.id,
        2,
        "修改",
        "待开始",
        "待开始",
        "修改时效任务基础信息",
        data.initiatorName
      );
      return mockResult(task, "时效任务修改成功");
    }
    return request<BaseResult<any>>({
      url: "/timeSensitiveTask/update",
      method: "POST",
      data,
    });
  },
 
  delete(id: number | string) {
    if (USE_TIME_SENSITIVE_MOCK) {
      const index = mockTasks.findIndex((item) => String(item.id) === String(id));
      if (index >= 0) mockTasks.splice(index, 1);
      delete mockItems[String(id)];
      delete mockOperations[String(id)];
      delete mockInspections[String(id)];
      return mockResult(true, "时效任务删除成功");
    }
    return request<BaseResult<any>>({
      url: "/timeSensitiveTask/delete",
      method: "POST",
      data: id as any,
    });
  },
 
  start(data: TimeSensitiveTaskStartForm) {
    if (USE_TIME_SENSITIVE_MOCK) {
      const task = mockTasks.find((item) => String(item.id) === String(data.id));
      if (task) Object.assign(task, data, { taskStatus: 1 });
      addMockOperation(
        data.id,
        2,
        "开始时效",
        "待开始",
        "时效中",
        `${data.inspectorName}确认任务信息并开始时效`,
        data.inspectorName
      );
      return mockResult(task, "时效已开始");
    }
    return request<BaseResult<any>>({
      url: "/timeSensitiveTask/start",
      method: "POST",
      data,
    });
  },
 
  checkEnd(id: number | string) {
    if (USE_TIME_SENSITIVE_MOCK) {
      return mockResult({
        canEnd: false,
        reachedDuration: false,
        elapsedMinutes: 120,
        requiredMinutes: 360,
        message: "Mock:尚未达到设定时效时长。",
      });
    }
    return request<BaseResult<any>>({
      url: "/timeSensitiveTask/checkEnd",
      method: "POST",
      data: id as any,
    });
  },
 
  end(data: { id: number | string; forceEnd: boolean }) {
    if (USE_TIME_SENSITIVE_MOCK) {
      const task = mockTasks.find((item) => String(item.id) === String(data.id));
      if (task) Object.assign(task, { taskStatus: 2, furnaceOutTime: mockDateTime() });
      addMockOperation(
        data.id,
        3,
        "结束时效",
        "时效中",
        "时效结束",
        data.forceEnd ? "未达到时长,确认强制结束时效" : "结束时效任务",
        task?.inspectorName || "操作人"
      );
      return mockResult(task, "时效已结束");
    }
    return request<BaseResult<any>>({
      url: "/timeSensitiveTask/end",
      method: "POST",
      data,
    });
  },
 
  queryCandidates(params: TimeSensitiveCandidateQuery) {
    if (USE_TIME_SENSITIVE_MOCK) {
      const existingIds = new Set(
        Object.values(mockItems)
          .flat()
          .map((item) => String(item.id))
      );
      let rows = mockMaterialPool[params.materialType]
        .filter((item) => !existingIds.has(String(item.id)))
        .map((item) => ({ ...item }));
      if (params.model) {
        const model = params.model.trim().toUpperCase();
        rows = rows.filter((item) =>
          String(item.model || "")
            .toUpperCase()
            .includes(model)
        );
      }
      if (params.materialCode) {
        const code = params.materialCode.trim().toUpperCase();
        rows = rows.filter(
          (item) =>
            mockMaterialCode(item).toUpperCase().includes(code) ||
            String(item.id).toUpperCase() === code ||
            String(item.outputId || "").toUpperCase() === code
        );
      }
      const current = Number(params.current || 1);
      const size = Number(params.size || 10);
      return mockResult<PageResult>({
        records: rows.slice((current - 1) * size, current * size),
        total: rows.length,
        current,
        size,
        pages: current * size < rows.length,
      });
    }
    return request<BaseResult<PageResult>>({
      url: "/timeSensitiveTask/queryCandidates",
      method: "POST",
      data: params,
    });
  },
 
  addItems(data: {
    taskId: number | string;
    itemIds: Array<number | string>;
    items: TimeSensitiveTaskItemInput[];
  }) {
    if (USE_TIME_SENSITIVE_MOCK) {
      const key = String(data.taskId);
      mockItems[key] ||= [];
      const existingIds = new Set(mockItems[key].map((item) => String(item.id)));
      data.items.forEach(({ itemId, ironPlateNo }) => {
        const material = cloneMockMaterial(itemId);
        if (material && !existingIds.has(String(itemId))) {
          mockItems[key].push({ ...material, ironPlateNo });
        }
      });
      updateMockItemCount(data.taskId);
      const task = mockTasks.find((item) => String(item.id) === key);
      addMockOperation(
        data.taskId,
        1,
        "新增明细",
        "待开始",
        "待开始",
        `领用 ${data.itemIds.length} 条物料`,
        task?.initiatorName || "操作人"
      );
      return mockResult(true, "物料领用成功");
    }
    return request<BaseResult<any>>({
      url: "/timeSensitiveTask/addItems",
      method: "POST",
      data,
    });
  },
 
  deleteItems(data: { taskId: number | string; itemIds: Array<number | string> }) {
    if (USE_TIME_SENSITIVE_MOCK) {
      const key = String(data.taskId);
      const idSet = new Set(data.itemIds.map(String));
      mockItems[key] = (mockItems[key] || []).filter((item) => !idSet.has(String(item.id)));
      updateMockItemCount(data.taskId);
      const task = mockTasks.find((item) => String(item.id) === key);
      addMockOperation(
        data.taskId,
        4,
        "删除明细",
        "待开始",
        "待开始",
        `删除 ${data.itemIds.length} 条领用物料`,
        task?.initiatorName || "操作人"
      );
      return mockResult(true, "领用物料删除成功");
    }
    return request<BaseResult<any>>({
      url: "/timeSensitiveTask/deleteItems",
      method: "POST",
      data,
    });
  },
 
  getMachines() {
    if (USE_TIME_SENSITIVE_MOCK) return mockResult(mockMachines.map((item) => ({ ...item })));
    return request<BaseResult<any[]>>({
      url: "/device/querydeviceSx",
      method: "GET",
    });
  },
 
  getUsers() {
    if (USE_TIME_SENSITIVE_MOCK) return mockResult(mockUsers.map((item) => ({ ...item })));
    return request<BaseResult<any[]>>({
      url: "/team/getUser",
      method: "GET",
    });
  },
 
  /** 与 PC 端 aging_rack_number 字典一致,用作单丝领用铁架号。 */
  getAgingRackNumbers() {
    return request<BaseResult<any[]>>({
      url: "/system/dict/data/type/aging_rack_number",
      method: "GET",
    });
  },
 
  getOperationRecords(recordId: number | string) {
    if (USE_TIME_SENSITIVE_MOCK) {
      return mockResult<any[]>(
        (mockOperations[String(recordId)] || []).map((item) => ({ ...item }))
      );
    }
    return request<BaseResult<any[]>>({
      url: "/operationRecord/list",
      method: "POST",
      data: { tableName: "time_sensitive_task", recordId },
    });
  },
 
  getInspectionDetails(id: number | string) {
    if (USE_TIME_SENSITIVE_MOCK) {
      return mockResult<any[]>((mockInspections[String(id)] || []).map((item) => ({ ...item })));
    }
    return request<BaseResult<any>>({
      url: "/timelinessInspection/getDetailById",
      method: "GET",
      data: { id },
    });
  },
 
  saveInspectionDetails(data: TimelinessInspectionDetailForm[]) {
    if (USE_TIME_SENSITIVE_MOCK) {
      const inspectionId = data[0]?.timelinessInspectionId;
      if (inspectionId !== undefined) {
        mockInspections[String(inspectionId)] = data.map((item, index) => ({
          ...item,
          id: item.id || Date.now() + index,
          createTime: (item as any).createTime || mockDateTime(),
        }));
      }
      return mockResult(true, "巡检记录保存成功");
    }
    return request<BaseResult<any>>({
      url: "/timelinessInspectionDetail/add",
      method: "POST",
      data,
    });
  },
 
  deleteInspectionDetail(id: number | string) {
    if (USE_TIME_SENSITIVE_MOCK) {
      Object.keys(mockInspections).forEach((key) => {
        mockInspections[key] = mockInspections[key].filter(
          (item) => String(item.id) !== String(id)
        );
      });
      return mockResult(true, "巡检记录删除成功");
    }
    return request<BaseResult<any>>({
      url: "/timelinessInspectionDetail/delete",
      method: "DELETE",
      data: { id },
    });
  },
};
 
export default TimeSensitiveTaskApi;