刘光辉
4 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
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
<script lang="ts" setup>
import type { ActionItem, BasicColumn } from '@jnpf/ui/vxeTable';
 
import type { FormulaDetail, FormulaListItem, FormulaPageQuery } from '#/api/x/eln/formulas';
 
import { ref } from 'vue';
 
import { useMessage } from '@jnpf/hooks';
import { useDrawer } from '@jnpf/ui/drawer';
import { BasicForm, useForm } from '@jnpf/ui/form';
import { BasicModal, useModal } from '@jnpf/ui/modal';
import { BasicVxeTable, TableAction, useVxeTable } from '@jnpf/ui/vxeTable';
 
import { disableFormula, enableFormula, getFormulaPage } from '#/api/x/eln/formulas';
 
import FormulaForm from './FormulaForm.vue';
import VersionDrawer from './VersionDrawer.vue';
 
defineOptions({ name: 'ElnFormulaCatalog' });
 
type FormulaChange = 'disable' | 'enable';
 
const { createMessage } = useMessage();
const [registerFormulaForm, { openModal: openFormulaForm }] = useModal();
const [registerReasonModal, { closeModal: closeReasonModal, openModal: openReasonModal }] = useModal();
const [registerVersionDrawer, { openDrawer: openVersionDrawer }] = useDrawer();
const reason = ref('');
const pendingChange = ref<{ formula: FormulaListItem; type: FormulaChange }>();
 
const columns: BasicColumn[] = [
  { dataIndex: 'gongshiBianma', minWidth: 210, title: '公式编码', slots: { default: 'gongshiBianma' } },
  { dataIndex: 'gongshiMingcheng', minWidth: 150, title: '名称' },
  { dataIndex: 'yewuYongtu', minWidth: 130, title: '用途' },
  { dataIndex: 'dangqianBanbenHao', title: '当前版本', width: 100, slots: { default: 'dangqianBanbenHao' } },
  { dataIndex: 'zuixinFabuBanbenHao', title: '最新发布版本', width: 120, slots: { default: 'zuixinFabuBanbenHao' } },
  { dataIndex: 'bizEnabled', title: '启用状态', width: 100, slots: { default: 'bizEnabled' } },
  { align: 'right', dataIndex: 'mubanYinyongShu', title: '模板引用数', width: 110 },
  { dataIndex: 'gengxinShijian', format: 'date|YYYY-MM-DD HH:mm:ss', title: '更新时间', width: 165 },
];
 
const [registerSearchForm, { getFieldsValue, resetFields }] = useForm({
  baseColProps: { lg: 8, md: 12, sm: 12, xl: 6, xs: 24 },
  compact: true,
  labelWidth: 76,
  schemas: [
    { component: 'Input', componentProps: { placeholder: '公式编码或名称', submitOnPressEnter: true }, field: 'keyword', label: '关键字' },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          { fullName: '启用', id: 'enabled' },
          { fullName: '停用', id: 'disabled' },
        ],
        placeholder: '全部',
      },
      field: 'bizEnabled',
      label: '状态',
    },
  ],
  showActionButtonGroup: true,
  showAdvancedButton: false,
});
 
const [registerTable, { reload }] = useVxeTable({
  actionColumn: { dataIndex: 'action', fixed: 'right', title: '操作', width: 180 },
  api: fetchFormulaPage,
  beforeFetch: normalizeQuery,
  columns,
  immediate: true,
  pagination: { pageSize: 20, pageSizeOptions: ['20', '50', '100'], showQuickJumper: true, showSizeChanger: true, showTotal: (total) => `共 ${total} 个公式` },
  rowKey: 'id',
  showIndexColumn: false,
});
 
function normalizeQuery(raw: Record<string, unknown>): FormulaPageQuery {
  const query: Record<string, unknown> = { ...raw };
  if (typeof query.keyword === 'string') query.keyword = query.keyword.trim();
  if (!query.keyword) delete query.keyword;
  if (!query.bizEnabled) delete query.bizEnabled;
  query.currentPage = Math.max(1, Number(query.currentPage) || 1);
  query.pageSize = Math.max(1, Number(query.pageSize) || 20);
  return query as unknown as FormulaPageQuery;
}
 
async function fetchFormulaPage(query: FormulaPageQuery) {
  const response = await getFormulaPage(query);
  return { data: response.data };
}
 
function handleSearch() {
  reload({ page: 1, searchInfo: getFieldsValue() });
}
 
async function handleReset() {
  await resetFields();
  reload({ page: 1, searchInfo: {} });
}
 
function handleFormulaCreated(formula: FormulaDetail) {
  reload();
  openVersionDrawer(true, { formulaId: formula.id, openDraft: true });
}
 
function getTableActions(formula: FormulaListItem): ActionItem[] {
  const actions: ActionItem[] = [{ label: '版本', onClick: () => openVersionDrawer(true, { formulaId: formula.id }) }];
  if (formula.bizEnabled === 'enabled') actions.push({ color: 'error', label: '停用', onClick: () => requestFormulaChange(formula, 'disable') });
  else actions.push({ color: 'success', label: '启用', onClick: () => requestFormulaChange(formula, 'enable') });
  return actions;
}
 
function requestFormulaChange(formula: FormulaListItem, type: FormulaChange) {
  reason.value = '';
  pendingChange.value = { formula, type };
  openReasonModal(true);
}
 
async function confirmFormulaChange() {
  const change = pendingChange.value;
  const actualReason = reason.value.trim();
  if (!change || !actualReason) {
    createMessage.warning('请输入操作原因');
    return;
  }
  await (change.type === 'enable' ? enableFormula(change.formula.id, actualReason) : disableFormula(change.formula.id, actualReason));
  createMessage.success(change.type === 'enable' ? '公式已启用' : '公式已停用');
  closeReasonModal();
  reload();
}
</script>
 
<template>
  <div class="jnpf-content-wrapper">
    <div class="jnpf-content-wrapper-center">
      <div class="jnpf-content-wrapper-search-box">
        <BasicForm @register="registerSearchForm" @reset="handleReset" @submit="handleSearch" />
      </div>
      <div class="jnpf-content-wrapper-content">
        <BasicVxeTable @register="registerTable">
          <template #tableTitle>
            <a-button type="primary" pre-icon="icon-ym icon-ym-btn-add" @click="openFormulaForm(true, {})">新增公式</a-button>
          </template>
          <template #gongshiBianma="{ record }">
            <span :title="record.gongshiBianma">{{ record.gongshiBianma }}</span>
          </template>
          <template #dangqianBanbenHao="{ record }">{{ record.dangqianBanbenHao ? `V${record.dangqianBanbenHao}` : '-' }}</template>
          <template #zuixinFabuBanbenHao="{ record }">{{ record.zuixinFabuBanbenHao ? `V${record.zuixinFabuBanbenHao}` : '-' }}</template>
          <template #bizEnabled="{ record }">
            <a-tag :color="record.bizEnabled === 'enabled' ? 'success' : 'default'">{{ record.bizEnabled === 'enabled' ? '启用' : '停用' }}</a-tag>
          </template>
          <template #action="{ record }"><TableAction :actions="getTableActions(record)" /></template>
        </BasicVxeTable>
      </div>
    </div>
    <FormulaForm @created="handleFormulaCreated" @register="registerFormulaForm" />
    <VersionDrawer @register="registerVersionDrawer" @reload="reload" />
    <BasicModal @register="registerReasonModal" :title="pendingChange?.type === 'enable' ? '启用公式' : '停用公式'" destroy-on-close @ok="confirmFormulaChange">
      <a-form-item label="操作原因" required>
        <a-textarea v-model:value="reason" :maxlength="500" :rows="4" placeholder="请输入操作原因" show-count />
      </a-form-item>
    </BasicModal>
  </div>
</template>
 
<style lang="scss" scoped>
:deep(.jnpf-vxe-table) {
  min-width: 0;
}
</style>