wujingjing
2024-05-17 1c8b789894e34f697e23a1a82bdd4d17bb9323a6
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
<template>
    <el-row :gutter="8" class="h100">
        <el-col :span="4" :xs="24" class="h100">
            <el-card shadow="hover" class="h100 left-tree-card" v-loading="treeLoading">
                <LeftTreeByMgr
                    title-name="类型列表"
                    :treedata="listTreeData"
                    :current-node-key="currentListID"
                    :default-props="{
                        children: 'Children',
                        label: 'LogicalName',
                        id: 'LogicalID',
                    }"
                    :folder-icon="(node, data) => data.LogicalType === MODULE_CODE"
                    :default-expand-all="true"
                    @click="handleClickNode"
                >
                </LeftTreeByMgr>
            </el-card>
        </el-col>
        <el-col :span="20" :xs="24" class="flex-column h100">
            <el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
                <el-form :inline="true" :model="queryParams">
                    <el-form-item label="标签名称" prop="Name">
                        <el-input v-model="queryParams.Name" style="width: 226.4px" placeholder="名称" clearable></el-input>
                    </el-form-item>
 
                    <el-form-item>
                        <el-button type="primary" icon="ele-Search" @click="handleQueryTable"> 查询 </el-button>
                        <el-button icon="ele-Refresh" @click="resetQuery">重置 </el-button>
                        <el-button icon="ele-Plus" @click="openOperateDialog()"> 增加 </el-button>
                    </el-form-item>
                    <el-form-item label="排序">
                        <el-switch
                            v-model="isDragStatus"
                            @change="handleDragStatus"
                            inline-prompt
                            active-icon="ele-Check"
                            inactive-icon="ele-Close"
                        >
                        </el-switch>
                    </el-form-item>
                </el-form>
            </el-card>
 
            <el-card class="flex-auto scroll-table-card" shadow="hover" style="margin-top: 8px">
                <el-table
                    v-loading="tableLoading"
                    ref="draggableTableRef"
                    border
                    row-key="ID"
                    :row-class-name="isDragStatus ? 'cursor-move' : 'cursor-pointer'"
                    :cell-style="{ textAlign: 'center' }"
                    :header-cell-style="{ textAlign: 'center' }"
                    :data="displayTableData"
                    style="width: 100%"
                    highlight-current-row
                >
                    <el-table-column prop="Name" label="名称" fixed="left" show-overflow-tooltip />
                    <el-table-column prop="Description" width="550" label="说明" show-overflow-tooltip />
                    <el-table-column label="操作" fixed="right" show-overflow-tooltip>
                        <template #default="scope">
                            <el-button icon="ele-Edit" size="small" text type="primary" @click="openOperateDialog(scope.row)"> 编辑 </el-button>
                            <el-button icon="ele-Delete" size="small" text type="danger" @click="deleteCurrentRow(scope.row)"> 删除 </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-card>
            <el-dialog :destroy-on-close="true" v-model="dialogIsShow" width="400" :close-on-click-modal="false" @closed="closeDialog">
                <template #header>
                    <div style="color: #fff">
                        <SvgIcon :name="dialogHeaderIcon" :size="16" style="margin-right: 3px; display: inline; vertical-align: middle" />
                        <span> {{ dialogTitle }} </span>
                    </div>
                </template>
 
                <el-form :model="dialogFormValue" ref="dialogFormRef" :rules="dialogFormRules" label-width="55">
                    <el-form-item label="名称" prop="Name">
                        <el-input placeholder="请输入名称" v-model="dialogFormValue.Name"></el-input>
                    </el-form-item>
 
                    <el-form-item label="说明" prop="Description">
                        <el-input placeholder="请输入说明" v-model="dialogFormValue.Description" type="textarea" :rows="3" />
                    </el-form-item>
                </el-form>
                <template #footer>
                    <div>
                        <el-button @click="closeDialog">取 消</el-button>
                        <el-button type="primary" @click="submitFormValue">确 定</el-button>
                    </div>
                </template>
            </el-dialog>
        </el-col>
    </el-row>
</template>
 
<script setup lang="ts">
import type { FormInstance, FormRules } from 'element-plus';
import { ElMessage, ElMessageBox } from 'element-plus';
import LeftTreeByMgr from '/@/components/tree/leftTreeByMgr.vue';
import type { PropType } from 'vue';
import { ref, onMounted, computed } from 'vue';
import { deepClone } from '/@/utils/other';
import { useQueryTable } from '/@/hooks/useQueryTable';
import { useTableSort } from '/@/hooks/useTableSort';
import {
    DeleteAFlag,
    GetTypeListTreeData,
    GetAllFlagByID,
    GetIsExistFlagName,
    InsertAFlag,
    UpdateAFlag,
    UpdateFlagSorter,
} from '/@/api/basic/flagInfo';
import { useValidateUniqueness } from '/@/hooks/useValidateUniqueness';
import type { AxiosRequestConfig, AxiosResponse } from 'axios';
import { MODULE_CODE } from '/@/constants';
 
const props = defineProps({
    request: {
        type: Function as PropType<(config: AxiosRequestConfig<any>) => Promise<AxiosResponse<any, any>>>,
    },
});
//#region ====================== 左侧树数据,tree with select init ======================
const treeLoading = ref(false);
 
const currentTreeNode = ref(null);
 
const listTreeData = ref([]);
const currentListID = ref('');
const handleClickNode = (data) => {
    currentTreeNode.value = data;
    currentListID.value = data.LogicalID;
 
    if (data.LogicalType === MODULE_CODE) {
        tableData.value = [];
        return;
    }
 
    getTableData();
};
const getListTreeData = async () => {
    treeLoading.value = true;
    const res = await GetTypeListTreeData(props.request).finally(() => {
        treeLoading.value = false;
    });
    if (res?.Code === 0) {
        listTreeData.value = res.Data || [];
        const firstListTreeNode = listTreeData.value[0]?.Children[0];
        if (firstListTreeNode) {
            handleClickNode(firstListTreeNode);
        } else {
            tableData.value = [];
            currentTreeNode.value = null;
            currentListID.value = null;
        }
    } else {
        ElMessage.error('获取类型列表失败' + (res?.Message ? `,${JSON.stringify(res.Message)}` : ''));
    }
};
//#endregion
 
//#region ====================== 表格数据,table init ======================
const tableLoading = ref(false);
const tableData = ref([]);
const isDragStatus = ref(false);
const getTableData = async () => {
    tableLoading.value = true;
    const res = await GetAllFlagByID({ TypeID: currentListID.value }, props.request).finally(() => {
        tableLoading.value = false;
    });
    if (res?.Code === 0) {
        tableData.value = res.Data || [];
    } else {
        ElMessage.error('获取标签失败' + (res?.Message ? `,${JSON.stringify(res.Message)}` : ''));
    }
};
const deleteCurrentRow = (row: any) => {
    ElMessageBox.confirm(`确定删除标签:【${row.Name}】?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(async () => {
        const res = await DeleteAFlag(
            {
                ID: row.ID,
            },
            props.request
        );
        if (res?.Code === 0) {
            if (res.Data) {
                ElMessage.success('删除标签成功');
                getTableData();
            } else {
                ElMessage.error('删除标签失败');
            }
        } else {
            ElMessage.error('删除标签失败' + (res?.Message ? `,${JSON.stringify(res.Message)}` : ''));
        }
    });
};
//#endregion
 
//#region ====================== 表格查询、排序,search form init ======================
const queryParams = ref({
    Name: '',
});
const { handleDragStatus, draggableTableRef } = useTableSort(
    tableData,
    UpdateFlagSorter,
    getTableData,
    undefined,
    false,
    props.request
);
 
const { resetQuery, handleQueryTable, displayTableData } = useQueryTable(tableData, queryParams, getTableData);
 
//#endregion
 
//#region ====================== 增加、修改记录操作, dialog init======================
const isEditDialog = ref(false);
const dialogTitle = computed(() => {
    return isEditDialog.value ? '修改标签' : '添加标签';
});
const dialogHeaderIcon = computed(() => {
    return isEditDialog.value ? 'ele-Edit' : 'ele-Plus';
});
const dialogFormValue = ref({
    Name: '',
    Description: '',
}) as any;
const dialogIsShow = ref(false);
const dialogFormRef = ref<FormInstance>(null);
 
const initialValue = ref('');
 
const { uniquenessValidator: nameValidator } = useValidateUniqueness(
    GetIsExistFlagName,
    initialValue,
    '标签名称',
    'Name',
    {
        TypeID: currentListID,
    },
    false,
    props.request
);
 
//#endregion
 
const dialogFormRules = ref<FormRules>({
    Name: [{ required: true, validator: nameValidator as any, trigger: 'blur' }],
});
const currentRow = ref(null);
const openOperateDialog = (row?) => {
    if (!currentListID.value) {
        return ElMessage.warning('请先选择类型!');
    }
    if (row) {
        isEditDialog.value = true;
        initialValue.value = row.Name;
        currentRow.value = row;
        const { ID, Name, Description } = row;
        dialogFormValue.value = deepClone({ ID, Name, Description });
    } else {
        if (currentTreeNode.value.LogicalType === MODULE_CODE) return ElMessage.warning('请先选择系统类型再添加!');
        initialValue.value = '';
        isEditDialog.value = false;
        dialogFormValue.value = { Name: '', Description: '' };
    }
    dialogIsShow.value = true;
};
 
const closeDialog = () => {
    dialogIsShow.value = false;
    dialogFormRef.value.clearValidate();
};
 
const submitFormValue = async () => {
    const valid = await dialogFormRef.value.validate().catch(() => {});
    if (!valid) return;
 
    if (isEditDialog.value) {
        const res = await UpdateAFlag(dialogFormValue.value, props.request);
        if (res?.Code === 0) {
            if (res.Data) {
                getTableData();
                closeDialog();
                ElMessage.success('修改标签成功');
            } else {
                ElMessage.error('修改标签失败');
            }
        } else {
            ElMessage.error('修改标签失败' + (res?.Message ? `,${JSON.stringify(res.Message)}` : ''));
        }
    } else {
        const res = await InsertAFlag({ ...dialogFormValue.value, TypeID: currentListID.value }, props.request);
        if (res?.Code === 0) {
            if (res.Data) {
                getTableData();
                closeDialog();
                ElMessage.success('添加标签成功');
            } else {
                ElMessage.error('添加标签失败');
            }
        } else {
            ElMessage.error('添加标签失败' + (res?.Message ? `,${JSON.stringify(res.Message)}` : ''));
        }
    }
};
 
//#endregion
 
onMounted(() => {
    getListTreeData();
});
</script>
<style scoped lang="scss"></style>