zouyu
2023-11-10 789bd1e36e88e09a45fa24fa1569330ada4c45b7
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
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
<template>
  <div style="height: 100%">
    <div class="page-header">
      <div class="header-left">
        <a @click="$router.go(-1)"><i class="icon-btn-back"></i></a>
        <h2 v-if="editable">编辑-完整产品结构</h2>
        <h2 v-if="!editable">查看-完整产品结构</h2>
      </div>
      <div class="btn-group header-right" v-if="editable">
        <el-button :disabled="isSubmit" v-thinclick="`dataFormSubmit`"
          >保存</el-button
        >
      </div>
    </div>
    <div class="page-main">
      <div class="completeProductStructure-basic">
        <el-form
          :inline="true"
          :model="treeForm"
          style="width: 100%"
          class="l-mes"
          :disabled="!editable"
          :rules="dataRule"
        >
          <el-row>
            <el-col :span="24">
              <el-form-item label="BOM编号">
                <el-input v-model="treeForm.number" placeholder="BOM编号">
                </el-input>
              </el-form-item>
 
              <el-form-item label="零件号" prop="partNo">
                <el-input v-model="treeForm.partNo" placeholder="零件" readonly>
                  <el-button
                    slot="append"
                    icon="el-icon-search"
                    @click="showProductStructure = true"
                  ></el-button>
                </el-input>
              </el-form-item>
 
              <el-form-item label="零件名" prop="partName">
                <el-input
                  v-model="treeForm.partName"
                  placeholder="零件名"
                  style="width: 300px"
                  disabled
                >
                </el-input>
              </el-form-item>
 
              <el-form-item label="类型" prop="bomTypeDb">
                <el-select
                  v-model="treeForm.bomTypeDb"
                  placeholder="请选择类型"
                  style="width:100%"
                  @visible-change="bomTypeDbChange"
                >
                  <el-option
                    v-for="(item, index) in bomTypeDbOptions"
                    :label="item.label"
                    :value="item.value"
                    :key="index"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
 
              <el-form-item label="版本号" prop="version">
                <el-input
                  v-model="treeForm.version"
                  placeholder="版本号"
                  disabled
                >
                </el-input>
              </el-form-item>
<!-- 
              <el-form-item label="替代号" prop="alternativeNo">
                <el-input
                  v-model="treeForm.alternativeNo"
                  placeholder="替代号"
                  disabled
                >
                </el-input>
              </el-form-item>
 
              <el-form-item label="替代描述" prop="alternativeDesc">
                <el-input
                  v-model="treeForm.alternativeDesc"
                  placeholder="替代描述"
                  disabled
                >
                </el-input>
              </el-form-item>
 
              <el-form-item label="绝缘颜色">
                <el-input
                  v-model="treeForm.insulationColor"
                  placeholder="绝缘颜色"
                >
                </el-input>
              </el-form-item>
              <el-form-item label="护套颜色">
                <el-input v-model="treeForm.sheathColor" placeholder="护套颜色">
                </el-input>
              </el-form-item>
              <el-form-item label="特性1">
                <el-input
                  v-model="treeForm.characteristicOne"
                  placeholder="特性1"
                >
                </el-input>
              </el-form-item> -->
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="completeProductStructure-detail">
        <div class="completeProductStructure-design">
          <div class="completeProductStructure-design-title">构建BOM</div>
          <el-table
            class="tree-select-table"
            ref="tableRef"
            :data="tableData"
            style="width: 100%;"
            row-key="id"
            border
            height="740"
            default-expand-all
            @expand-change="expandChange"
            @selection-change="selectionChange"
            @select="rowSelect"
            :row-class-name="tableRowClassName"
          >
            <el-table-column type="selection" width="55"> </el-table-column>
            <el-table-column prop="partName" label="零件名" width="430">
            </el-table-column>
            <el-table-column prop="partNo" label="零件号" width="120">
            </el-table-column>
            <el-table-column
              prop="originalQpa"
              label="单位父节点零件所需数量"
              width="100"
            >
            </el-table-column>
            <el-table-column
              prop="qpa"
              label="单位根节点零件所需数量"
              width="100"
            >
            </el-table-column>
            <el-table-column prop="unit" label="单位" width="100">
            </el-table-column>
            <el-table-column prop="operationName" label="消耗工序">
            </el-table-column>
          <el-table-column label="磨具编号" prop="sharpenerNo" align="center">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.sharpenerNo" placeholder="请填写磨具编号"
                    v-show="scope.row.isUpdate"></el-input>
                  <span v-show="!scope.row.isUpdate">{{
                    scope.row.sharpenerNo
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column label="每模穴数" prop="caveNum" align="center">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.caveNum" placeholder="请填写每模穴数"
                    v-show="scope.row.isUpdate"></el-input>
                  <span v-show="!scope.row.isUpdate">{{
                    scope.row.caveNum
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column label="克重(kg)" prop="gramWeight" align="center">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.gramWeight" placeholder="请填写克重"
                    v-show="scope.row.isUpdate"></el-input>
                  <span v-show="!scope.row.isUpdate">{{
                    scope.row.gramWeight
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column label="浇口克重(kg)" prop="gateGramWeight" align="center">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.gateGramWeight" placeholder="请填写浇口克重"
                    v-show="scope.row.isUpdate"></el-input>
                  <span v-show="!scope.row.isUpdate">{{
                    scope.row.gateGramWeight
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column label="成型周期(h)" prop="moldingCycle" align="center">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.moldingCycle" placeholder="请填写成型周期"
                    v-show="scope.row.isUpdate"></el-input>
                  <span v-show="!scope.row.isUpdate">{{
                    scope.row.moldingCycle
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column label="机台(t)" prop="machine" align="center">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.machine" placeholder="请填写机台"
                    v-show="scope.row.isUpdate"></el-input>
                  <span v-show="!scope.row.isUpdate">{{
                    scope.row.machine
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column label="回料比例(%)" prop="scale" align="center">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.scale" placeholder="请填写回料比例"
                    v-show="scope.row.isUpdate"></el-input>
                  <span v-show="!scope.row.isUpdate">{{
                    scope.row.scale
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column label="烘料时间(h)" prop="dryingTime" align="center">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.dryingTime" placeholder="请填写烘料时间"
                    v-show="scope.row.isUpdate"></el-input>
                  <span v-show="!scope.row.isUpdate">{{
                    scope.row.dryingTime
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column label="烘料温度(℃)" prop="dryingTemperature" align="center">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.dryingTemperature" placeholder="请填写烘料温度"
                    v-show="scope.row.isUpdate"></el-input>
                  <span v-show="!scope.row.isUpdate">{{
                    scope.row.dryingTemperature
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column label="包装信息" prop="packingInfo" align="center">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.packingInfo" placeholder="请填写包装信息"
                    v-show="scope.row.isUpdate"></el-input>
                  <span v-show="!scope.row.isUpdate">{{
                    scope.row.packingInfo
                  }}</span>
                </template>
              </el-table-column>
          </el-table>
        </div>
        <div class="completeProductStructure-queryPreview"></div>
        <div class="completeProductStructure-preview">
          <div class="completeProductStructure-preview-title">结果预览</div>
          <el-table
            :data="preViewData"
            style="width: 100%;"
            row-key="id"
            border
            height="740"
            default-expand-all
            class="preView-structure-table"
          >
            <el-table-column prop="partName" label="零件名" width="430">
            </el-table-column>
            <el-table-column
              prop="partNo"
              label="零件号"
              width="100"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              prop="originalQpa"
              label="单位父节点零件所需数量"
              width="100"
            >
            </el-table-column>
            <el-table-column
              prop="qpa"
              label="单位根节点零件所需数量"
              width="100"
            >
            </el-table-column>
            <el-table-column prop="unit" label="单位" width="100">
            </el-table-column>
            <!-- <el-table-column prop="discNum" label="盘数(盘)" width="100">
              <template slot-scope="scope">
                {{ scope.row.discNum || 1 }}
              </template>
            </el-table-column> -->
            <el-table-column prop="operationName" label="消耗工序">
            </el-table-column>
            <el-table-column label="磨具编号" prop="sharpenerNo" align="center">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.sharpenerNo" placeholder="请填写磨具编号"
                    v-show="scope.row.isUpdate"></el-input>
                  <span v-show="!scope.row.isUpdate">{{
                    scope.row.sharpenerNo
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column label="每模穴数" prop="caveNum" align="center">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.caveNum" placeholder="请填写每模穴数"
                    v-show="scope.row.isUpdate"></el-input>
                  <span v-show="!scope.row.isUpdate">{{
                    scope.row.caveNum
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column label="克重(kg)" prop="gramWeight" align="center">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.gramWeight" placeholder="请填写克重"
                    v-show="scope.row.isUpdate"></el-input>
                  <span v-show="!scope.row.isUpdate">{{
                    scope.row.gramWeight
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column label="浇口克重(kg)" prop="gateGramWeight" align="center">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.gateGramWeight" placeholder="请填写浇口克重"
                    v-show="scope.row.isUpdate"></el-input>
                  <span v-show="!scope.row.isUpdate">{{
                    scope.row.gateGramWeight
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column label="成型周期(h)" prop="moldingCycle" align="center">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.moldingCycle" placeholder="请填写成型周期"
                    v-show="scope.row.isUpdate"></el-input>
                  <span v-show="!scope.row.isUpdate">{{
                    scope.row.moldingCycle
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column label="机台(t)" prop="machine" align="center">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.machine" placeholder="请填写机台"
                    v-show="scope.row.isUpdate"></el-input>
                  <span v-show="!scope.row.isUpdate">{{
                    scope.row.machine
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column label="回料比例(%)" prop="scale" align="center">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.scale" placeholder="请填写回料比例"
                    v-show="scope.row.isUpdate"></el-input>
                  <span v-show="!scope.row.isUpdate">{{
                    scope.row.scale
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column label="烘料时间(h)" prop="dryingTime" align="center">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.dryingTime" placeholder="请填写烘料时间"
                    v-show="scope.row.isUpdate"></el-input>
                  <span v-show="!scope.row.isUpdate">{{
                    scope.row.dryingTime
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column label="烘料温度(℃)" prop="dryingTemperature" align="center">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.dryingTemperature" placeholder="请填写烘料温度"
                    v-show="scope.row.isUpdate"></el-input>
                  <span v-show="!scope.row.isUpdate">{{
                    scope.row.dryingTemperature
                  }}</span>
                </template>
              </el-table-column>
              <el-table-column label="包装信息" prop="packingInfo" align="center">
                <template slot-scope="scope">
                  <el-input size="small" v-model="scope.row.packingInfo" placeholder="请填写包装信息"
                    v-show="scope.row.isUpdate"></el-input>
                  <span v-show="!scope.row.isUpdate">{{
                    scope.row.packingInfo
                  }}</span>
                </template>
              </el-table-column>
          </el-table>
        </div>
      </div>
    </div>
    <productStructureDialog
      :paramArr="[
        {
          prop: 'bomTypeDb',
          propVal: treeForm.bomTypeDb
        }
      ]"
      :currshowlist.sync="showProductStructure"
      @listenToProductstructureEvent="selectProductStructure"
    />
  </div>
</template>
 
<script>
import {
  getPartAllStructureExt,
  addObj,
  getObj,
  putObj
} from '@/api/technology/completeproductstructure'
import { remote } from '@/api/admin/dict'
import productStructureDialog from '@/views/common/productstructure.vue'
export default {
  components: { productStructureDialog },
  data() {
    return {
      dataRule: {
        partName: [
          { required: true, message: '最终零件不能为空', trigger: 'blur' }
        ],
        version: [
          { required: true, message: '版本号不能为空', trigger: 'blur' }
        ],
        bomTypeDb: [
          { required: true, message: '结构类型不能为空', trigger: 'blur' }
        ]
      },
 
      dataForm: { id: null, state: null },
      treeForm: {
        partId: null,
        partNo: null,
        partName: null,
        number: null,
        insulationColor: null,
        sheathColor: null,
        characteristicOne: null,
        version: null,
        alternativeNo: null,
        alternativeDesc: null,
        bomTypeDb: 'M'
      },
      before: null,
      showProductStructure: false,
      selectVals: [],
      currFloorBrotherAndSameGroupData: [], // 同层同组的兄弟元素
      currFloorBrotherAndDiffGroupData: [], // 同层不同组的兄弟元素
      currFloorData: [],
      preViewData: [],
      tableData: [],
      isSubmit: false,
      bomTypeDbOptions: []
    }
  },
  mounted() {
    window.addEventListener(
      'hashchange',
      () => {
        const currentPath = window.location.hash.slice(1)
        if (this.$route.path !== currentPath) {
          this.$router.push(currentPath)
        }
      },
      false
    )
  },
  computed: {
    editable: function() {
      console.log()
      if (!this.dataForm.id) {
        return true
      }
      if (
        this.dataForm.id &&
        (this.dataForm.state == '01draft' ||
          this.dataForm.state == '03cancelled')
      ) {
        return true
      }
      return false
    }
  },
  methods: {
    getStructue() {
      if (!this.treeForm.partName) {
        this.$message.error('请选择零件!')
        return
      }
      var paramObj = {}
      paramObj.partId = this.treeForm.partId
      paramObj.version = this.treeForm.version
      paramObj.alternativeNo = this.treeForm.alternativeNo
      paramObj.alternativeDesc = this.treeForm.alternativeDesc
      paramObj.bomTypeDb = this.treeForm.bomTypeDb
 
      this.tableData = []
 
      getPartAllStructureExt(paramObj).then((response) => {
        const result = response.data
        if (result.code === 0) {
          this.tableData.push(result.data)
        }
      })
    },
 
    init() {
      if (this.dataForm.id) {
        getObj(this.dataForm.id).then((response) => {
          var resultData = response.data.data
          this.treeForm.partId = resultData.partId
          this.treeForm.partNo = resultData.partNo
          this.treeForm.partName = resultData.partName
          this.treeForm.number = resultData.number
          this.treeForm.insulationColor = resultData.insulationColor
          this.treeForm.sheathColor = resultData.sheathColor
          this.treeForm.characteristicOne = resultData.characteristicOne
          this.treeForm.version = resultData.version
          this.treeForm.alternativeNo = resultData.alternativeNo
          this.treeForm.alternativeDesc = resultData.alternativeDesc
          this.treeForm.bomTypeDb = resultData.bomTypeDb
          this.dataForm.state = resultData.state
          this.preViewData = []
          this.preViewData.push(resultData.tree)
          this.tableData = []
          getPartAllStructureExt(this.treeForm).then((res) => {
            const result = res.data
            if (result.code === 0) {
              this.tableData.push(result.data)
            }
          })
        })
      } else {
        this.initDefaultForm()
      }
    },
    initDefaultForm() {
      this.treeForm.partId = null
      this.treeForm.partName = null
      this.treeForm.insulationColor = null
      this.treeForm.sheathColor = null
      this.treeForm.characteristicOne = null
      this.treeForm.version = null
      this.treeForm.bomTypeDb = 'M'
      this.selectVals = []
      this.currFloorBrotherAndSameGroupData = []
      this.currFloorBrotherAndDiffGroupData = []
      this.currFloorData = []
      this.preViewData = []
      this.tableData = []
    },
    setDefaultDiscNum(treeData) {
      if (!treeData) {
        return
      }
      if (treeData instanceof Array) {
        for (var i in treeData) {
          // 遍历数组
          if (!treeData[i].discNum) {
            treeData[i].discNum = 1
          }
          if (
            treeData.children &&
            Array.isArray(treeData.children) &&
            treeData.children.length > 0
          ) {
            this.setDefaultDiscNum(treeData.children)
          }
        }
      } else {
        if (!treeData.discNum) {
          treeData.discNum = 1
        }
        if (
          treeData.children &&
          Array.isArray(treeData.children) &&
          treeData.children.length > 0
        ) {
          this.setDefaultDiscNum(treeData.children)
        }
      }
    },
    dataFormSubmit() {
      const mapTree = (org) => {
        const haveChildren =
          Array.isArray(org.children) && org.children.length > 0
        return {
          checked: org.checked,
          compId: org.compId,
          discNum: org.discNum || 1,
          expand: org.expand,
          id: org.id,
          operationId: org.operationId,
          operationName: org.operationName,
          operationNo: org.operationNo,
          originalQpa: org.originalQpa,
          parentId: org.parentId,
          partId: org.partId,
          partName: org.partName,
          partNo: org.partNo,
          version: org.version,
          alternativeNo: org.alternativeNo,
          alternativeDesc: org.alternativeDesc,
          qpa: org.qpa,
          structureId: org.structureId,
          unit: org.unit,
          color: org.color,
          planningMethod: org.planningMethod,
          sharpenerNo: org.sharpenerNo,
          caveNum:org.caveNum,
          gramWeight:org.gramWeight,
          gateGramWeight:org.gateGramWeight,
          moldingCycle:org.moldingCycle,
          machine:org.machine,
          scale:org.scale,
          dryingTime:org.dryingTime,
          dryingTemperature:org.dryingTemperature,
          packingInfo: org.packingInfo,
          children: haveChildren ? org.children.map((i) => mapTree(i)) : []
        }
      }
 
      this.isSubmit = true
      this.generateStructure()
      if (this.preViewData != null && this.preViewData.length > 0) {
        if (this.preViewData.length === 1) {
          if (this.dataForm.id) {
            var updateObj = {}
            updateObj.id = this.dataForm.id
            updateObj.partId = this.treeForm.partId
            updateObj.number = this.treeForm.number
            updateObj.insulationColor = this.treeForm.insulationColor
            updateObj.sheathColor = this.treeForm.sheathColor
            updateObj.characteristicOne = this.treeForm.characteristicOne
            updateObj.version = this.treeForm.version
            updateObj.alternativeNo = this.treeForm.alternativeNo
            updateObj.alternativeDesc = this.treeForm.alternativeDesc
            updateObj.bomTypeDb = this.treeForm.bomTypeDb
 
            // const tree = this.preViewData[0];
            const gxqTree = this.preViewData.map((org) => mapTree(org))
            // this.setDefaultDiscNum(gxqTree);
            updateObj.tree = gxqTree[0]
            putObj(updateObj)
              .then((response) => {
                var data = response.data
                if (data.code === 0) {
                  this.$message.success('修改成功')
                } else {
                  this.$message.error('修改失败')
                }
                this.isSubmit = false
              })
              .catch((error) => {
                this.isSubmit = false
                console.log(error)
              })
          } else {
            var newObj = {}
            newObj.id = null
            newObj.partId = this.treeForm.partId
            newObj.number = this.treeForm.number
            newObj.insulationColor = this.treeForm.insulationColor
            newObj.sheathColor = this.treeForm.sheathColor
            newObj.characteristicOne = this.treeForm.characteristicOne
            newObj.version = this.treeForm.version
            newObj.alternativeNo = this.treeForm.alternativeNo
            newObj.alternativeDesc = this.treeForm.alternativeDesc
            newObj.bomTypeDb = this.treeForm.bomTypeDb
            const gxqTree = this.preViewData.map((org) => mapTree(org))
            newObj.tree = gxqTree[0]
            addObj(newObj)
              .then((response) => {
                var data = response.data
                if (data.code === 0) {
                  this.dataForm.id = data.data.id
                  this.treeForm.number = data.data.number
                  this.$message.success('保存成功')
                } else {
                  this.$message.error('保存失败')
                }
                this.isSubmit = false
              })
              .catch((error) => {
                this.isSubmit = false
                console.log(error)
              })
          }
        } else {
          this.isSubmit = false
          this.$message.error('存在多个根节点,无法进行保存')
        }
      } else {
        this.isSubmit = false
        this.$message.error('请先构建BOM,再进行保存')
      }
    },
    // 生成最终的结构
    generateStructure() {
      if (this.selectVals != null && this.selectVals.length > 0) {
        // 先将选中的节点行进行分组,分为头部(无父母,有子)、中部(有父母,有子)、尾部(有父母,无子)、特殊节点(无父母、无子)
        var groupResult = this.structureDivideIntoGroups(this.selectVals)
        // 将分组后的节点,进行连接,连接方式为父母找孩子,头部去中部和尾部找孩子,中部去中部和尾部找孩子
 
        var noParentAndHasChildList = groupResult.noParentAndHasChild // 头部
        var noParentAndNoChildList = groupResult.noParentAndNoChild // 特殊节点
        var hasParentAndHasChildList = groupResult.hasParentAndHasChild // 中部
        var hasParentAndNoChildList = groupResult.hasParentAndNoChild // 尾部
        this.findChilds(noParentAndHasChildList, hasParentAndHasChildList)
        this.findChilds(noParentAndHasChildList, hasParentAndNoChildList)
        this.findChilds(hasParentAndHasChildList, hasParentAndHasChildList)
        this.findChilds(hasParentAndHasChildList, hasParentAndNoChildList)
        this.preViewData = []
        if (noParentAndHasChildList.length > 0) {
          for (let i = 0; i < noParentAndHasChildList.length; i++) {
            this.preViewData.push(noParentAndHasChildList[i])
          }
        }
        if (noParentAndNoChildList.length > 0) {
          for (let i = 0; i < noParentAndNoChildList.length; i++) {
            this.preViewData.push(noParentAndNoChildList[i])
          }
        }
        // 按id排个序,保持一致
        if (this.preViewData && this.preViewData.length > 0) {
          this.preViewData.sort((a, b) => {
            return a.id - b.id
          })
        }
      } else {
        this.preViewData = []
      }
    },
    // 父母找孩子
    findChilds(parents, childs) {
      let parent
      let child
      for (let i = 0; i < parents.length; i++) {
        parent = parents[i]
        for (let k = 0; k < childs.length; k++) {
          child = childs[k]
          if (parent.id === child.parentId) {
            parent.children.push(child)
          }
        }
        // 按id排个序,保持一致
        if (parent.children && parent.children.length > 0) {
          parent.children.sort((a, b) => {
            return a.id - b.id
          })
        }
      }
    },
    // 节点分组,分为头部(无父母,有子)、中部(有父母,有子)、尾部(有父母,无子)、特殊节点(无父母、无子)
    structureDivideIntoGroups(selects) {
      var noParentList = [] // 无父母
      var hasParentList = [] // 有父母
      var noParentAndHasChildList = [] // 无父母有子
      var hasParentAndHasChildList = [] // 有父母有子
      var hasParentAndNoChildList = [] // 有父母无子
      var noParentAndNoChildList = [] // 无父母无子
      let outSelect
      let inSelect
      // 筛选出有无父母noParentList、hasParentList
      for (let i = 0; i < selects.length; i++) {
        outSelect = selects[i]
        let flag = false
        for (let k = 0; k < selects.length; k++) {
          inSelect = selects[k]
          if (outSelect.parentId === inSelect.id) {
            flag = true
            break
          }
        }
        if (flag) {
          // 存在父母
          hasParentList.push(outSelect)
        } else {
          // 不存在
          noParentList.push(outSelect)
        }
      }
      // hasParentList筛选出有无子
      if (hasParentList.length > 0) {
        let copyHasParent
        let hasParent
        let se
        for (let i = 0; i < hasParentList.length; i++) {
          hasParent = hasParentList[i]
          let flag = false
          for (let k = 0; k < selects.length; k++) {
            se = selects[k]
            if (se.parentId === hasParent.id) {
              flag = true
              break
            }
          }
          copyHasParent = {}
          copyHasParent.id = hasParent.id
          copyHasParent.parentId = hasParent.parentId
          copyHasParent.compId = hasParent.compId
          copyHasParent.structureId = hasParent.structureId
          copyHasParent.partId = hasParent.partId
          copyHasParent.partName = hasParent.partName
          copyHasParent.operationId = hasParent.operationId
          copyHasParent.operationName = hasParent.operationName
 
          copyHasParent.discNum = hasParent.discNum
          copyHasParent.qpa = hasParent.qpa
          copyHasParent.originalQpa = hasParent.originalQpa
          copyHasParent.color = hasParent.color
          copyHasParent.planningMethod = hasParent.planningMethod
          copyHasParent.unit = hasParent.unit
          copyHasParent.expand = hasParent.expand
          copyHasParent.children = []
          if (flag) {
            // 存在子
            hasParentAndHasChildList.push(copyHasParent)
          } else {
            // 不存在子
            hasParentAndNoChildList.push(copyHasParent)
          }
        }
      }
 
      // noParentList筛选出有无子
      if (noParentList.length > 0) {
        let copyNoParent
        let noParent
        let se
        for (let i = 0; i < noParentList.length; i++) {
          noParent = noParentList[i]
          let flag = false
          for (let k = 0; k < selects.length; k++) {
            se = selects[k]
            if (se.parentId === noParent.id) {
              flag = true
              break
            }
          }
          copyNoParent = {}
          copyNoParent.id = noParent.id
          copyNoParent.parentId = noParent.parentId
          copyNoParent.compId = noParent.compId
          copyNoParent.structureId = noParent.structureId
          copyNoParent.partId = noParent.partId
          copyNoParent.partName = noParent.partName
          copyNoParent.operationId = noParent.operationId
          copyNoParent.operationName = noParent.operationName
 
          copyNoParent.sharpenerNo= noParent.sharpenerNo
          copyNoParent.caveNum=noParent.caveNum
          copyNoParent.gramWeight=noParent.gramWeight
          copyNoParent.gateGramWeight=noParent.gateGramWeight
          copyNoParent.moldingCycle=noParent.moldingCycle
          copyNoParent.machine=noParent.machine
          copyNoParent.scale=noParent.scale
          copyNoParent.dryingTime=noParent.dryingTime
          copyNoParent.dryingTemperature=noParent.dryingTemperature
          copyNoParent.packingInfo= noParent.packingInfo
 
          copyNoParent.discNum = noParent.discNum
          copyNoParent.qpa = noParent.qpa
          copyNoParent.originalQpa = noParent.originalQpa
          copyNoParent.unit = noParent.unit
          copyNoParent.color = noParent.color
          copyNoParent.planningMethod = noParent.planningMethod
          copyNoParent.expand = noParent.expand
          copyNoParent.children = []
          if (flag) {
            // 存在子
            noParentAndHasChildList.push(copyNoParent)
          } else {
            // 不存在子
            noParentAndNoChildList.push(copyNoParent)
          }
        }
      }
 
      var groupResult = {
        noParentAndHasChild: noParentAndHasChildList,
        noParentAndNoChild: noParentAndNoChildList,
        hasParentAndHasChild: hasParentAndHasChildList,
        hasParentAndNoChild: hasParentAndNoChildList
      }
      return groupResult
    },
    rowSelect(selection, row) {
      const inSelection = selection.length && selection.indexOf(row) !== -1
      let selectFlag = false
      if (inSelection) {
        selectFlag = true
      }
      // 若selection为空,则为取消,将其同组的兄弟元素及其所有子元素(包括兄弟元素的子元素)取消,并将这些元素置灰。若selection不为空,则为选中,将其同组的兄弟元素及其所有子元素(包括兄弟元素的子元素)选中(子元素之间是同组且只取某一组),并且高亮,并将同级别不同组的兄弟元素及其所有子元素(包括兄弟元素的子元素)取消,并将这些元素置灰
 
      this.currFloorBrotherAndSameGroupData = []
      this.currFloorBrotherAndDiffGroupData = []
      // 找出同组的兄弟元素
      this.iterationDataFindCurrFloorBrother(
        this.tableData,
        row.id,
        row.structureId
      )
 
      this.$nextTick(() => {
        // 对同层同组兄弟元素的取消或选中, 及对其子元素的取消或选中,置灰或高亮
        for (let i = 0; i < this.currFloorBrotherAndSameGroupData.length; i++) {
          this.$refs.tableRef.toggleRowSelection(
            this.currFloorBrotherAndSameGroupData[i],
            selectFlag
          )
          this.currFloorBrotherAndSameGroupData[i].expand = selectFlag
          this.cancelOrSelectRowsBySomeRule(
            this.currFloorBrotherAndSameGroupData[i].children,
            selectFlag
          )
        }
 
        // 对同层不同组的兄弟元素的取消、置灰
        for (let i = 0; i < this.currFloorBrotherAndDiffGroupData.length; i++) {
          this.$refs.tableRef.toggleRowSelection(
            this.currFloorBrotherAndDiffGroupData[i],
            false
          )
          this.currFloorBrotherAndDiffGroupData[i].expand = false
          this.cancelRows(this.currFloorBrotherAndDiffGroupData[i].children)
        }
      })
    },
    selectionChange(val) {
      this.selectVals = val
      this.generateStructure()
    },
    expandChange(row, expanded) {
      /*
      this.currFloorData = []
      this.iterationDataFindCurrFloor(this.tableData, row.id)
      if (expanded) {
        // 展开
 
        this.iterationData(this.currFloorData, row.id)
      } else {
        this.setBrotherExpand(this.currFloorData)
      } */
    },
    closeRowExpansion(row) {
      this.$refs.tableRef.toggleRowExpansion(row, false)
    },
    // 迭代,若为取消则,将子元素全部取消并置灰或若选中某组元素(根据某种规则选一组元素,这里是选第一个的组)
    cancelOrSelectRowsBySomeRule(datas, selected) {
      if (datas != null && datas && datas.length > 0) {
        if (selected) {
          // 选中时
          const defaultStructureId = datas[0].structureId
 
          for (let k = 0; k < datas.length; k++) {
            if (datas[k].structureId === defaultStructureId) {
              // 选中并继续找孩子迭代,并高亮
              datas[k].expand = true
              this.$refs.tableRef.toggleRowSelection(datas[k], true)
              this.cancelOrSelectRowsBySomeRule(datas[k].children, true)
            } else {
              datas[k].expand = false
              this.$refs.tableRef.toggleRowSelection(datas[k], false)
              this.cancelRows(datas[k].children, false)
            }
          }
        } else {
          // 取消选中时
          this.cancelRows(datas)
        }
      }
    },
    // 取消所有选中并置灰
    cancelRows(datas) {
      if (datas != null && datas && datas.length > 0) {
        for (let k = 0; k < datas.length; k++) {
          // 取消并置灰
          datas[k].expand = false
          this.$refs.tableRef.toggleRowSelection(datas[k], false)
          if (
            datas[k].children != null &&
            datas[k].children &&
            datas[k].children.length > 0
          ) {
            this.cancelRows(datas[k].children)
          }
        }
      }
    },
    // 迭代找出当前id所在层的所有的同组层对象
    iterationDataFindCurrFloorBrother(datas, id, structureId) {
      let flag = false
      for (let i = 0; i < datas.length; i++) {
        if (datas[i].id === id) {
          flag = true
        }
      }
      if (flag) {
        // id在这一层
        for (let i = 0; i < datas.length; i++) {
          if (datas[i].structureId === structureId) {
            // 同层同组的
            this.currFloorBrotherAndSameGroupData.push(datas[i])
          } else {
            // 同层不同组的
            this.currFloorBrotherAndDiffGroupData.push(datas[i])
          }
        }
      } else {
        let da
        for (let i = 0; i < datas.length; i++) {
          da = datas[i]
          if (da.children != null && da.children && da.children.length > 0) {
            this.iterationDataFindCurrFloorBrother(da.children, id, structureId)
          }
        }
      }
    },
    // 迭代找出当前id所在层的所有的层对象
    iterationDataFindCurrFloor(datas, id) {
      let flag = false
      for (let i = 0; i < datas.length; i++) {
        if (datas[i].id === id) {
          flag = true
        }
      }
      if (flag) {
        // id在这一层
        for (let i = 0; i < datas.length; i++) {
          this.currFloorData.push(datas[i])
        }
      } else {
        let da
        for (let i = 0; i < datas.length; i++) {
          da = datas[i]
          if (da.children != null && da.children && da.children.length > 0) {
            this.iterationDataFindCurrFloor(da.children, id)
          }
        }
      }
    },
    // 将兄弟元素的expand设置为true
    setBrotherExpand(datas) {
      let da
      for (let i = 0; i < datas.length; i++) {
        da = datas[i]
        da.expand = true
      }
    },
 
    // 迭代,取消兄弟元素及其子元素的选中并缩回
    iterationData(datas, id) {
      var flag = true
      var data = datas.find((item) => item.id === id)
      // 判断id是否在这一层
      if (data) {
        flag = false
      }
      if (flag) {
        // id不在这一层,继续找
        let da
        for (let i = 0; i < datas.length; i++) {
          da = datas[i]
          // 存在子节点,继续迭代,不存在,则不管
          if (da.children != null && da.children && da.children.length > 0) {
            this.iterationData(da.children, id)
          }
        }
      } else {
        // id在这一层,找出不同structureId分组的成员,并把属性设置为expand=false(包括孩子),找出相同structureId分组的成员,并把属性设置为expand=true(包括孩子)
        let da
        for (let i = 0; i < datas.length; i++) {
          da = datas[i]
          if (da.structureId === data.structureId) {
            da.expand = true
          } else {
            da.expand = false
          }
 
          if (!da.expand) {
            // 取消选中跟当前节点不为一组的兄弟节点
            this.$refs.tableRef.toggleRowSelection(da, da.expand)
            // 收缩跟当前节点不为一组的兄弟节点
            this.$refs.tableRef.toggleRowExpansion(da, da.expand)
          }
          if (da.children != null && da.children && da.children.length > 0) {
            this.setexpand(da.children, da.expand)
          }
        }
      }
    },
    setexpand(childs, expand) {
      let child
      for (let i = 0; i < childs.length; i++) {
        child = childs[i]
        // child.expand = expand
        if (!expand) {
          this.$refs.tableRef.toggleRowSelection(child, expand)
          this.$refs.tableRef.toggleRowExpansion(child, expand)
        }
 
        if (
          child.children != null &&
          child.children &&
          child.children.length > 0
        ) {
          this.setexpand(child.children, expand)
        }
      }
    },
    // 响应零件选择的回调
    selectProductStructure(row) {
      if (row != null) {
        this.treeForm.partId = row.partId
        this.treeForm.partNo = row.partNo
        this.treeForm.partName = row.partName
        this.treeForm.version = row.version
        this.treeForm.alternativeNo = row.alternativeNo
        this.treeForm.alternativeDesc = row.alternativeDesc
        this.treeForm.bomTypeDb = row.bomTypeDb
        this.tableData = []
        this.getStructue()
        // getPartAllStructure(row.id).then((response) => {
        //   const result = response.data
        //   if (result.code === 0) {
        //     this.tableData.push(result.data)
        //   }
        // })
      }
    },
    bomTypeDbChange(e) {
      if (e) {
        this.before = this.treeForm.bomTypeDb
      } else {
        if (this.before !== this.treeForm.bomTypeDb) {
          this.treeForm.partId = null
          this.treeForm.partNo = null
          this.treeForm.partName = null
          this.treeForm.version = null
          this.treeForm.alternativeNo = null
          this.treeForm.alternativeDesc = null
          this.tableData = []
        }
      }
    },
    // table设置行样式
    tableRowClassName({ row, rowIndex }) {
      if (row.expand) {
        return ''
      } else {
        return 'forbid-row'
      }
    },
    // 查询结构类型字典
    getBomTypeDbOptions() {
      remote('bom_type_db').then((response) => {
        if (response.data.code === 0) {
          this.bomTypeDbOptions = response.data.data
        } else {
          this.bomTypeDbOptions = []
        }
      })
    }
  },
  created() {
    this.dataForm.id = this.$route.params.id
    this.init()
    this.getBomTypeDbOptions()
  }
}
</script>
<style>
.tree-select-table .el-table__header-wrapper .el-checkbox {
  display: none;
}
.completeProductStructure-basic {
  background-color: #fff;
  height: 100px;
  display: flex;
  align-items: flex-end;
  padding: 10px 30px;
  border: 1px solid #ddd;
  box-sizing: border-box;
}
 
.completeProductStructure-detail {
  width: 100%;
  height: 800px;
  padding-top: 10px;
  display: flex;
  justify-content: flex-start;
  box-sizing: border-box;
}
 
.completeProductStructure-design {
  float: left;
  width: 48%;
  height: 800px;
  padding: 10px 20px;
  border: 1px solid #ddd;
  background-color: #fff;
  margin-top: 0px;
  box-sizing: border-box;
}
.completeProductStructure-design-title {
  height: 40px;
  color: #006eff;
}
 
.completeProductStructure-queryPreview {
  float: left;
  width: 4%;
  height: 800px;
  padding: 0px 0px;
  border: 1px solid #ddd;
  background-color: #fff;
  margin-top: 0px;
}
.completeProductStructure-preview {
  float: right;
  width: 48%;
  height: 800px;
  padding: 10px 20px;
  border: 1px solid #ddd;
  background-color: #fff;
  margin-top: 0px;
  box-sizing: border-box;
}
.completeProductStructure-preview-title {
  height: 40px;
  color: #006eff;
}
.forbid-row .cell {
  color: #c0c4cc;
}
 
.tree-select-table th.gutter {
  display: table-cell !important;
  width: 10px !important;
}
 
.tree-select-table colgroup.gutter {
  display: table-cell !important;
  width: 10px !important;
}
 
.preView-structure-table th.gutter {
  display: table-cell !important;
  width: 10px !important;
}
 
.preView-structure-table colgroup.gutter {
  display: table-cell !important;
  width: 10px !important;
}
</style>