sen
2026-01-29 fea2d693c33fdbcb4c8304a96a4e584829cb437b
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
<template>
  <basicContainer>
    <el-tabs v-model="activeAccountSet" @tab-click="handleTabClick">
      <el-tab-pane label="珠海汇畅" name="0"></el-tab-pane>
      <el-tab-pane label="广珠物流贸易" name="1"></el-tab-pane>
    </el-tabs>
    <avue-crud :option="option" :table-loading="pageF.loading" :data="tableData" :page="page"
      :permission="permissionList" :before-open="beforeOpen" v-model="form" ref="crudRef" @row-update="rowUpdate"
      @row-save="rowSave" @refresh-change="refreshChange" @row-del="rowDel" @search-change="searchChange"
      @search-reset="searchReset" @selection-change="selectionChange" @current-change="currentChange"
      @size-change="sizeChange" @on-load="onLoad">
      <template #menu-left>
        <el-button type="success" icon="Edit" :disabled="pageF.single" v-hasPermi="['cwgl:voucherSubjectSetting:edit']"
          @click="handleUpdate">修改
        </el-button>
        <el-button type="danger" icon="Delete" :disabled="pageF.multiple" @click="handleDelete"
          v-hasPermi="['cwgl:voucherSubjectSetting:remove']">删除
        </el-button>
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['cwgl:voucherSubjectSetting:export']">导出
        </el-button>
      </template>
      <template #menu="{ row, index, size }">
        <el-button type="primary" text icon="Plus" @click="handleRowAdd(row)">新增子项</el-button>
      </template>
    </avue-crud>
  </basicContainer>
</template>
 
<script setup name="voucherSubjectSetting" lang="ts">
import { VoucherSubjectSettingI, addVoucherSubjectSetting, delVoucherSubjectSetting, exportVoucherSubjectSetting, getVoucherSubjectSetting, listVoucherSubjectSetting, updateVoucherSubjectSetting } from "@/api/cwgl/voucherSubjectSetting";
import useCurrentInstance from "@/utils/useCurrentInstance";
import { computed, reactive, ref, toRefs } from "vue";
import { PagesInterface, PageQueryInterface } from "@/utils/globalInterface";
import { usePagePlus } from "@/hooks/usePagePlus";
import { hasPermission } from "@/utils/permissionUtils";
 
const { proxy } = useCurrentInstance();
const crudRef = ref();
 
const permissionList = computed(() => {
  return {
    addBtn: hasPermission(["cwgl:voucherSubjectSetting:add"]),
    delBtn: hasPermission(["cwgl:voucherSubjectSetting:remove"]),
    editBtn: hasPermission(["cwgl:voucherSubjectSetting:edit"]),
    viewBtn: hasPermission(["cwgl:voucherSubjectSetting:query"]),
  }
})
// 记录是否来自行内操作
const isFromRow = ref(false);
const activeAccountSet = ref("0");
const data = reactive({
  form: <VoucherSubjectSettingI>{},
  queryParams: <VoucherSubjectSettingI & PageQueryInterface>{
    type: "0",
  },
  page: <PagesInterface>{
    pageSize: 10,
    total: 0,
    currentPage: 1,
  },
  selectionList: [],
})
const { queryParams, form, page, selectionList } = toRefs(data);
const option = ref({
  pageKey: 'VoucherSubjectSetting',
  rowKey: 'id',
  searchSpan: 5,
  labelWidth: 150,
  searchLabelWidth: 120,
  column: {
    // id: {
    //   label: '科目ID11',
    // },
    // parentId: {
    //   label: '父科目ID',
    // },
    // ancestors: {
    //   label: '祖级列表',
    //   type: 'textarea', minRows: 3, maxRows: 5,
    // },
    //   type: {
    //   label: '帐套类型',
    //   search: true,
    //   minWidth: 120,
    //   type: 'select', dataType: 'string', dicUrl: '/system/dict/data/type/sys_accounting_type',
    //   rules: [
    //     {
    //       required: true,
    //       message: "帐套类型不能为空", trigger: "change"
    //     }
    //   ],
    // },
    accountSet: {
      label: '账套',
      search: true,
      minWidth: 120,
      addDisabled: true,
      editDisabled: true,
      type: 'select', dataType: 'string', dicUrl: '/system/dict/data/type/sys_accounting_type',
      rules: [
        {
          required: true,
          message: "帐套类型不能为空", trigger: "change"
        }
      ],
    },
    // parentSubjectName: {
 
 
    //   label: '上级科目名称',
    //   prop: 'parentSubjectCode',
    //   type: 'cascader',      // 级联选择器
    //   dataType: 'string',
    //   // dicUrl: '/api/cwgl/subject/tree', 
    //   placeholder: '请选择上级科目名称',
 
    //   // --- 控制显示逻辑 ---
    //   hide: true,            // 在表格列中隐藏
    //   search: false,         // 在搜索栏中隐藏
    //   display: true,         // 在新增/修改弹窗中显示(默认为true)
    //   // --------------------
 
    //   filterable: true,      // 开启搜索过滤
    //   checkStrictly: true,   // 允许选择任意一级(可选父级)
    //   emitPath: false,       // 提交时只保留最后一级的值
    //   props: {
    //     label: 'name',
    //     value: 'code',
    //     children: 'children'
    //   },
    //   minWidth: 150,
    //   // rules: [
    //   //   {
    //   //     required: true,
    //   //     message: "上级科目名称不能为空", trigger: "blur"
    //   //   }
    //   // ],
    //   /*  */
 
    // },
    parentSubjectName: {
      label: '上级科目名称',
      minWidth: 150,
 
      search: true,
      rules: [
        {
          required: true,
          message: "科目名称不能为空", trigger: "blur"
        }
      ],
    },
    parentSubjectCode: {
      label: '上级科目代码',
      minWidth: 150,
 
      search: true,
      rules: [
        {
          required: true,
          message: "科目名称不能为空", trigger: "blur"
        }
      ],
    },
    // parentSubjectCode: {
    //   label: '上级科目代码',
    //   minWidth: 150,
    //   display: true,
    //   hide: true,            // 在表格列中隐藏
    //   search: false,         // 在搜索栏中隐藏
    //   display: true,         // 在新增/修改弹窗中显示(默认为true)
    //   addDisabled: true,     // 新增时置灰,不可编辑
    //   editDisabled: true,    // 修改时置灰,不可编辑
    //   // readonly: true,      // 或者使用只读属性(取决于 UI 需求)
    //   // rules: [
    //   //   {
    //   //     required: true,
    //   //     message: "上级科目代码不能为空", trigger: "blur"
    //   //   }
    //   // ],
    // },
    subjectName: {
      label: '科目名称',
      minWidth: 150,
 
      search: true,
      rules: [
        {
          required: true,
          message: "科目名称不能为空", trigger: "blur"
        }
      ],
    },
    subjectCode: {
      label: '科目代码',
      minWidth: 150,
      search: true,
      rules: [
        {
          required: true,
          message: "科目代码不能为空", trigger: "blur"
        }
      ],
    },
    balanceDirection: {
      label: '余额方向',
      search: true,
      minWidth: 120,
      type: 'radio',
      dicUrl: '/system/dict/data/type/sys_balance_direction',
      rules: [
        {
          required: true,
          message: "余额方向不能为空", trigger: "blur"
        }
      ],
    },
    enabled: {
      label: '是否启用',
      minWidth: 120,
      type: 'radio', dicUrl: '/system/dict/data/type/sys_whether_type',
      rules: [
        {
          required: true,
          message: "是否启用不能为空", trigger: "change"
        }
      ],
      search: true,
    },
    accountingItemsDesc: {
      label: '核算项目',
      minWidth: 150,
      addDisplay: false,
      editDisplay: false,
    },
    accountingItemsContains: {
      label: '核算项目1',
      minWidth: 150,
      search: true,
      type: 'select', // 确保类型为 select
      multiple: true, // 开启多选
      addDisplay: false,    // 在新增时隐藏
      editDisplay: false,
      hide: true,
      dataType: 'string',
      dicUrl: '/system/dict/data/type/sys_accounting_item_name',
      placeholder: '请选择核算项目(可多选)',
      props: {
        label: 'dictLabel',
        value: 'dictValue'
      }
    },
    accountingItems: {
      label: '核算项目2',
      minWidth: 150,
      type: 'select',
      multiple: true,
      dataType: 'string',
      dicUrl: '/system/dict/data/type/sys_accounting_item_name',
      placeholder: '请选择核算项目(可多选)',
      props: {
        label: 'dictLabel',
        value: 'dictValue'
      },
      // --- 关键修改 ---
      hide: true,           // 在表格列中隐藏
      search: false,        // 在搜索栏中隐藏
      display: true,        // 确保在弹窗表单中依然显示(默认即为 true)
      // ----------------
      minWidth: 150
    },
    subjectType: {
      label: '科目类别',
      minWidth: 150,
      minWidth: 120,
      type: 'select', dicUrl: '/system/dict/data/type/sys_subject_category',
      rules: [
        {
          required: true,
          message: "科目类别不能为空", trigger: "change"
        }
      ],
      search: true,
    },
    quantityAmountAccounting: {
      label: '数量金额核算',
      minWidth: 120,
      type: 'radio', dicUrl: '/system/dict/data/type/sys_right_wrong',
      rules: [
        {
          required: true,
          message: "数量金额核算不能为空", trigger: "change"
        }
      ],
    },
    unitOfMeasurement: {
      label: '计量单位',
      minWidth: 150,
 
    },
    expenseName: {
      label: '费用名称',
      minWidth: 120,
      type: 'select', dicUrl: '/system/dict/data/type/sys_name_harge',
      rules: [
        {
          required: true,
          message: "费用名称不能为空", trigger: "change"
        }
      ],
      search: true,
 
    },
    mnemonicCode: {
      label: '助记码',
      minWidth: 150,
 
    },
    foreignCurrencyAccounting: {
      label: '外币核算',
      minWidth: 150,
      type: 'select', dicUrl: '/system/dict/data/type/sys_foreign_verification',
    },
    cashSubject: {
      label: '现金科目',
      minWidth: 150,
      type: 'radio', dicUrl: '/system/dict/data/type/sys_right_wrong',
 
 
    },
    bankSubject: {
      label: '银行科目',
      minWidth: 150,
      type: 'radio', dicUrl: '/system/dict/data/type/sys_right_wrong',
 
    },
    cashFlowSubject: {
      label: '现金流量科目',
      minWidth: 150,
      type: 'radio', dicUrl: '/system/dict/data/type/sys_right_wrong',
 
    },
    contactUnit: {
      label: '往来单位',
      minWidth: 150,
      type: 'radio', dicUrl: '/system/dict/data/type/sys_right_wrong',
 
    },
    department: {
      label: '部门',
      minWidth: 150,
      type: 'radio', dicUrl: '/system/dict/data/type/sys_right_wrong',
 
    },
    productName: {
      label: '品名',
      minWidth: 150,
      type: 'radio', dicUrl: '/system/dict/data/type/sys_right_wrong',
 
    },
 
 
    // orderNum: {
    //   label: '显示顺序',
    // },
    // status: {
    //   label: '状态',
    // },
    // delFlag: {
    //   label: '删除标志',
    // },
    // createBy: {
    //   label: '创建者',
    // },
    // createTime: {
    //   label: '创建时间',
    // },
    // updateBy: {
    //   label: '更新者',
    // },
    // updateTime: {
    //   label: '更新时间',
    // },
    // remark: {
    //   label: '备注',
    //   type: 'textarea', minRows: 3, maxRows: 5,
    // },
 
  }
})
// sys_accounting_type
 
const { tableData, pageF, rowSave, rowUpdate, rowDel, beforeOpen, searchChange,
  searchReset, selectionChange, onLoad, currentChange, sizeChange, handleDelete, handleExport, handleUpdate, refreshChange } = usePagePlus({
    form: form,
    option: option,
    queryParams: queryParams,
    idKey: 'id',
    page: page.value,
    getListApi: listVoucherSubjectSetting,
    getDetailApi: getVoucherSubjectSetting,
    exportApi: exportVoucherSubjectSetting,
    deleteApi: delVoucherSubjectSetting,
    addApi: addVoucherSubjectSetting,
    updateApi: updateVoucherSubjectSetting,
    handleUpdateFunc: () => {
      isFromRow.value = true; // 修改操作也视为禁用上级科目
      crudRef.value.rowEdit(selectionList.value[0]);
    },
    handleSelectionChangeFunc: (selection: any) => {
      selectionList.value = selection;
    },
    getBeginListFunc: (params = {}) => {
      let newParams = { ...params };
      newParams.type = activeAccountSet.value;
 
      // 处理 accountingItems 参数
      if (newParams.accountingItemsContains) {
        if (Array.isArray(newParams.accountingItemsContains)) {
          // 如果是数组,按原逻辑处理
          newParams.accountingItemsContains = newParams.accountingItemsContains.join(',');
        } else {
          // 如果是字符串,检查是否为逗号分隔的数字
          const itemsStr = String(newParams.accountingItemsContains).trim();
          if (itemsStr && itemsStr.includes(',')) {
            // 分割字符串,转换为数字并相加
            const sum = itemsStr
              .split(',')
              .map(item => parseInt(item.trim(), 10))
              .filter(num => !isNaN(num))
              .reduce((acc, curr) => acc + curr, 0);
 
            // 使用相加后的结果
            newParams.accountingItemsContains = sum > 0 ? sum.toString() : '';
          }
        }
      }
 
      queryParams.value = newParams;
      return newParams;
    },
   handleBeforeOpenFunc: (type: string) => {
  if (!option.value?.column) return;
 
  const nameCol = option.value.column.parentSubjectName;
  const codeCol = option.value.column.parentSubjectCode;
 
  // --- 1. 新增逻辑修改 ---
  if (type === 'add') {
    if (isFromRow.value) {
      // 行内“新增子项”点击
      nameCol.disabled = true;
      codeCol.disabled = true;
      option.value.saveBtnText = '新增';
      
      // 赋值当前页签的账套(虽然行内新增通常会带入父级账套,但这里显式同步一次)
      form.value.accountSet = activeAccountSet.value; 
    } else {
      // 顶部“新增”按钮点击
      nameCol.disabled = false;
      codeCol.disabled = false;
      option.value.saveBtnText = '保 存';
      
      // 【关键修复】先清空可能残留的旧数据(如 ID、科目代码等)
      Object.keys(form.value).forEach(key => delete form.value[key]);
      
      // 【核心要求】将页签的值赋值给表单账套字段
      form.value.accountSet = activeAccountSet.value; 
      form.value.type = activeAccountSet.value; 
 
      
      // 初始化其他必要字段
      form.value.accountingItems = []; 
      form.value.enabled = "1"; // 默认启用
    }
  } 
 
  // --- 2. 修改/查看逻辑(保持原样) ---
  else if (type === 'edit' || type === 'view') {
    nameCol.disabled = true;
    codeCol.disabled = true;
    option.value.updateBtnText = '修 改';
 
    if (typeof form.value.accountingItems === 'number') {
      form.value.accountingItems = decomposeAccountingItems(form.value.accountingItems);
    } else if (!form.value.accountingItems) {
      form.value.accountingItems = [];
    }
  }
 
  nextTick(() => {
    isFromRow.value = false;
  });
},
 
    // 新增保存前的逻辑
    rowSaveBegin: (row: any, done: any, loading: any) => {
      processAccountingItems(row);
      delete row.accountingItemsContains;
      // 如果 row 对象里带了 id,新增接口可能会报错或变成修改,如果是顶部新增,确保没有 id
      if (!isFromRow.value) {
        delete row.id;
      }
      done(row); // 必须调用 done 并传入处理后的 row 才会继续请求接口
    },
 
    // 修改保存前的逻辑
    rowUpdateBegin: (row: any, done: any, loading: any) => {
      processAccountingItems(row);
      delete row.accountingItemsContains;
      done(row); // 同理
    },
  })
 
const processAccountingItems = (row: any) => {
  if (!row.accountingItems) {
    row.accountingItems = 0;
    return;
  }
 
  // 1. 如果已经是数组(正常多选组件返回的结果)
  if (Array.isArray(row.accountingItems)) {
    row.accountingItems = row.accountingItems
      .map(item => parseInt(String(item), 10))
      .filter(num => !isNaN(num))
      .reduce((acc, curr) => acc + curr, 0);
  }
  // 2. 如果是逗号分隔的字符串
  else if (typeof row.accountingItems === 'string' && row.accountingItems.includes(',')) {
    row.accountingItems = row.accountingItems
      .split(',')
      .map(item => parseInt(item.trim(), 10))
      .filter(num => !isNaN(num))
      .reduce((acc, curr) => acc + curr, 0);
  }
  // 3. 如果已经是数字,保持不变
  console.log('转换后的提交值:', row.accountingItems);
};
const handleTabClick = (tab: any) => {
  const selectedTabName = tab.props.name;
  console.log("Tab点击的值:", selectedTabName);
  activeAccountSet.value = selectedTabName;
  queryParams.value.type = selectedTabName;
  const nextParams = {
    ...queryParams.value,
    type: selectedTabName
  };
 
  // 4. 重置分页并加载
  page.value.currentPage = 1;
  onLoad(page.value, nextParams);
}
 
/**
 * 拆分函数:将 3 转换为 ['1', '2']
 */
function decomposeAccountingItems(num: number): string[] {
  const result: string[] = [];
  let bit = 1;
  while (bit <= num) {
    if ((num & bit) === bit) {
      result.push(bit.toString());
    }
    bit <<= 1; // 位移运算,等同于 bit *= 2
  }
  return result;
}
 
/**
 * 处理行内“新增”点击
 */
const handleRowAdd = (row: any) => {
  isFromRow.value = true;
  getVoucherSubjectSetting(row.id).then((res: any) => {
    if (res.code == 200) {
      form.value = res.data;
      form.value.parentId = form.value.id;
      const num = form.value.accountingItems;
      form.value.accountingItems = decomposeAccountingItems(num);
      // 触发 Avue 的内置新增弹窗
      crudRef.value.rowAdd();
    }
  });
 
 
};
</script>