zhou zhou
3 天以前 0a1d91e42e6c5af96e1108e9ebcc37e99eb3b22c
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
<template>
  <div class="wave-rule-page art-full-height">
    <ArtSearchBar
      v-model="searchForm"
      :items="searchItems"
      :showExpand="false"
      @search="handleSearch"
      @reset="handleReset"
    />
 
    <ElCard class="art-table-card">
      <ArtTableHeader v-model:columns="columnChecks" :loading="loading" @refresh="refreshData">
        <template #left>
          <ElSpace wrap>
            <ElButton v-auth="'add'" @click="showDialog('add')" v-ripple>{{ t('pages.manager.waveRule.actions.add') }}</ElButton>
            <ElButton
              v-auth="'delete'"
              type="danger"
              :disabled="selectedRows.length === 0"
              @click="handleBatchDelete"
              v-ripple
            >
              {{ t('common.actions.batchDelete') }}
            </ElButton>
          </ElSpace>
        </template>
      </ArtTableHeader>
 
      <ArtTable
        :loading="loading"
        :data="data"
        :columns="columns"
        :pagination="pagination"
        @selection-change="handleSelectionChange"
        @pagination:size-change="handleSizeChange"
        @pagination:current-change="handleCurrentChange"
      />
 
      <WaveRuleDialog
        v-model:visible="dialogVisible"
        :wave-rule-data="currentWaveRuleData"
        :type-options="typeOptions"
        @submit="handleDialogSubmit"
      />
 
      <WaveRuleDetailDrawer
        v-model:visible="detailDrawerVisible"
        :loading="detailLoading"
        :detail-data="detailData"
      />
    </ElCard>
  </div>
</template>
 
<script setup>
  import { ElMessage } from 'element-plus'
  import { useI18n } from 'vue-i18n'
  import { guardRequestWithMessage } from '@/utils/sys/requestGuard'
  import { useAuth } from '@/hooks/core/useAuth'
  import { useTable } from '@/hooks/core/useTable'
  import { useCrudPage } from '@/views/system/common/useCrudPage'
  import {
    fetchDeleteWaveRule,
    fetchDictDataPage,
    fetchGetWaveRuleDetail,
    fetchSaveWaveRule,
    fetchUpdateWaveRule,
    fetchWaveRulePage
  } from '@/api/system-manage'
  import WaveRuleDialog from './modules/wave-rule-dialog.vue'
  import WaveRuleDetailDrawer from './modules/wave-rule-detail-drawer.vue'
  import { createWaveRuleTableColumns } from './waveRuleTable.columns'
  import {
    buildWaveRuleDialogModel,
    buildWaveRulePageQueryParams,
    buildWaveRuleSavePayload,
    buildWaveRuleSearchParams,
    buildWaveRuleTypeOptions,
    createWaveRuleSearchState,
    getWaveRuleDictTypeCode,
    getWaveRulePaginationKey,
    normalizeWaveRuleListRow
  } from './waveRulePage.helpers'
 
  defineOptions({ name: 'WaveRule' })
  const { t } = useI18n()
 
  const { hasAuth } = useAuth()
  const searchForm = ref(createWaveRuleSearchState())
  const detailDrawerVisible = ref(false)
  const detailLoading = ref(false)
  const detailData = ref({})
  const typeOptions = ref([])
  let handleDeleteAction = null
 
  const searchItems = computed(() => [
    {
      label: t('table.keyword'),
      key: 'condition',
      type: 'input',
      props: {
        clearable: true,
        placeholder: t('pages.manager.waveRule.search.conditionPlaceholder')
      }
    },
    {
      label: t('table.code'),
      key: 'code',
      type: 'input',
      props: {
        clearable: true,
        placeholder: t('pages.manager.waveRule.search.codePlaceholder')
      }
    },
    {
      label: t('pages.manager.waveRule.table.type'),
      key: 'type',
      type: 'select',
      props: {
        clearable: true,
        options: typeOptions.value
      }
    },
    {
      label: t('table.name'),
      key: 'name',
      type: 'input',
      props: {
        clearable: true,
        placeholder: t('pages.manager.waveRule.search.namePlaceholder')
      }
    }
  ])
 
  async function loadTypeOptions() {
    const records = await guardRequestWithMessage(
      fetchDictDataPage({
        current: 1,
        pageSize: 200,
        dictTypeCode: getWaveRuleDictTypeCode(),
        status: 1
      }),
      [],
      {
        timeoutMessage: t('pages.manager.waveRule.messages.typeTimeout')
      }
    )
    typeOptions.value = buildWaveRuleTypeOptions(Array.isArray(records?.records) ? records.records : records?.list || records || [])
  }
 
  async function openDetail(row) {
    detailDrawerVisible.value = true
    detailLoading.value = true
    try {
      detailData.value = normalizeWaveRuleListRow(await fetchGetWaveRuleDetail(row.id))
    } catch (error) {
      detailDrawerVisible.value = false
      detailData.value = {}
      ElMessage.error(error?.message || t('pages.manager.waveRule.messages.detailFailed'))
    } finally {
      detailLoading.value = false
    }
  }
 
  async function openEditDialog(row) {
    try {
      currentWaveRuleData.value = buildWaveRuleDialogModel(await fetchGetWaveRuleDetail(row.id))
      dialogVisible.value = true
      dialogType.value = 'edit'
    } catch (error) {
      ElMessage.error(error?.message || t('pages.manager.waveRule.messages.detailFailed'))
    }
  }
 
  const {
    columns,
    columnChecks,
    data,
    loading,
    pagination,
    getData,
    replaceSearchParams,
    resetSearchParams,
    handleSizeChange,
    handleCurrentChange,
    refreshData,
    refreshCreate,
    refreshUpdate,
    refreshRemove
  } = useTable({
    core: {
      apiFn: fetchWaveRulePage,
      apiParams: buildWaveRulePageQueryParams(searchForm.value),
      paginationKey: getWaveRulePaginationKey(),
      columnsFactory: () =>
        createWaveRuleTableColumns({
          handleView: openDetail,
          handleEdit: openEditDialog,
          handleDelete: hasAuth('delete') ? (row) => handleDeleteAction?.(row) : null
        })
    },
    transform: {
      dataTransformer: (records) => {
        if (!Array.isArray(records)) {
          return []
        }
        return records.map((item) => normalizeWaveRuleListRow(item))
      }
    }
  })
 
  const {
    dialogVisible,
    dialogType,
    currentRecord: currentWaveRuleData,
    selectedRows,
    handleSelectionChange,
    showDialog,
    handleDialogSubmit,
    handleDelete,
    handleBatchDelete
  } = useCrudPage({
    createEmptyModel: () => buildWaveRuleDialogModel(),
    buildEditModel: (record) => buildWaveRuleDialogModel(record),
    buildSavePayload: (formData) => buildWaveRuleSavePayload(formData),
    saveRequest: fetchSaveWaveRule,
    updateRequest: fetchUpdateWaveRule,
    deleteRequest: fetchDeleteWaveRule,
    entityName: t('pages.manager.waveRule.entity'),
    resolveRecordLabel: (record) => record?.name || record?.code || record?.id,
    refreshCreate,
    refreshUpdate,
    refreshRemove
  })
  handleDeleteAction = handleDelete
 
  onMounted(() => {
    loadTypeOptions()
  })
 
  function handleSearch(params) {
    replaceSearchParams(buildWaveRuleSearchParams(params))
    getData()
  }
 
  function handleReset() {
    Object.assign(searchForm.value, createWaveRuleSearchState())
    resetSearchParams()
  }
</script>