From e181f04c642204e79749af93fa921875ff6c21ba Mon Sep 17 00:00:00 2001
From: baoshiwei <baoshiwei@shlanbao.cn>
Date: 星期二, 20 五月 2025 10:46:35 +0800
Subject: [PATCH] refactor(qms): 重构趋势图展示逻辑

---
 src/components/UserSelect/index.vue |  133 +++++++++++++++++++++++++++++--------------
 1 files changed, 89 insertions(+), 44 deletions(-)

diff --git a/src/components/UserSelect/index.vue b/src/components/UserSelect/index.vue
index f2ef014..937a395 100644
--- a/src/components/UserSelect/index.vue
+++ b/src/components/UserSelect/index.vue
@@ -24,22 +24,16 @@
           <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
             <div v-show="showSearch" class="mb-[10px]">
               <el-card shadow="hover">
-                <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="68px">
+                <el-form ref="queryFormRef" :model="queryParams" :inline="true">
                   <el-form-item label="鐢ㄦ埛鍚嶇О" prop="userName">
-                    <el-input v-model="queryParams.userName" placeholder="璇疯緭鍏ョ敤鎴峰悕绉�" clearable style="width: 200px" @keyup.enter="handleQuery" />
+                    <el-input v-model="queryParams.userName" placeholder="璇疯緭鍏ョ敤鎴峰悕绉�" clearable @keyup.enter="handleQuery" />
                   </el-form-item>
                   <el-form-item label="鎵嬫満鍙风爜" prop="phonenumber">
-                    <el-input
-                      v-model="queryParams.phonenumber"
-                      placeholder="璇疯緭鍏ユ墜鏈哄彿鐮�"
-                      clearable
-                      style="width: 200px"
-                      @keyup.enter="handleQuery"
-                    />
+                    <el-input v-model="queryParams.phonenumber" placeholder="璇疯緭鍏ユ墜鏈哄彿鐮�" clearable @keyup.enter="handleQuery" />
                   </el-form-item>
                   <el-form-item>
                     <el-button type="primary" icon="Search" @click="handleQuery">鎼滅储</el-button>
-                    <el-button icon="Refresh" @click="resetQuery">閲嶇疆</el-button>
+                    <el-button icon="Refresh" @click="() => resetQuery()">閲嶇疆</el-button>
                   </el-form-item>
                 </el-form>
               </el-card>
@@ -47,9 +41,9 @@
           </transition>
 
           <el-card shadow="hover">
-            <template #header>
+            <template v-if="prop.multiple" #header>
               <el-tag v-for="user in selectUserList" :key="user.userId" closable style="margin: 2px" @close="handleCloseTag(user)">
-                {{ user.userName }}
+                {{ user.nickName }}
               </el-tag>
             </template>
 
@@ -60,9 +54,8 @@
               show-overflow
               :data="userList"
               :loading="loading"
-              :row-config="{ keyField: 'userId' }"
-              :checkbox-config="{ reserve: true, checkRowKeys: userIds }"
-              highlight-current-row
+              :row-config="{ keyField: 'userId', isHover: true }"
+              :checkbox-config="{ reserve: true, trigger: 'row', highlight: true, showHeader: prop.multiple }"
               @checkbox-all="handleCheckboxAll"
               @checkbox-change="handleCheckboxChange"
             >
@@ -90,14 +83,14 @@
               v-model:page="queryParams.pageNum"
               v-model:limit="queryParams.pageSize"
               :total="total"
-              @pagination="getList"
+              @pagination="pageList"
             />
           </el-card>
         </el-col>
       </el-row>
 
       <template #footer>
-        <el-button @click="userDialog.closeDialog">鍙栨秷</el-button>
+        <el-button @click="close">鍙栨秷</el-button>
         <el-button type="primary" @click="confirm">纭畾</el-button>
       </template>
     </el-dialog>
@@ -107,22 +100,24 @@
 <script setup lang="ts">
 import api from '@/api/system/user';
 import { UserQuery, UserVO } from '@/api/system/user/types';
-import { DeptVO } from '@/api/system/dept/types';
+import { DeptTreeVO, DeptVO } from '@/api/system/dept/types';
 import { VxeTableInstance } from 'vxe-table';
 import useDialog from '@/hooks/useDialog';
 
 interface PropType {
-  modelValue?: UserVO[];
+  modelValue?: UserVO[] | UserVO | undefined;
+  multiple?: boolean;
+  data?: string | number | (string | number)[] | undefined;
 }
 const prop = withDefaults(defineProps<PropType>(), {
-  modelValue: () => []
+  multiple: true,
+  modelValue: undefined,
+  data: undefined
 });
-const emit = defineEmits(['update:modelValue']);
+const emit = defineEmits(['update:modelValue', 'confirmCallBack']);
 
 const { proxy } = getCurrentInstance() as ComponentInternalInstance;
 const { sys_normal_disable } = toRefs<any>(proxy?.useDict('sys_normal_disable'));
-
-const userIds = computed(() => prop.modelValue.map((item) => item.userId as string));
 
 const userList = ref<UserVO[]>();
 const loading = ref(true);
@@ -130,7 +125,7 @@
 const total = ref(0);
 const dateRange = ref<[DateModelType, DateModelType]>(['', '']);
 const deptName = ref('');
-const deptOptions = ref<DeptVO[]>([]);
+const deptOptions = ref<DeptTreeVO[]>([]);
 const selectUserList = ref<UserVO[]>([]);
 
 const deptTreeRef = ref<ElTreeInstance>();
@@ -151,11 +146,8 @@
   roleId: ''
 });
 
-/** 閫氳繃鏉′欢杩囨护鑺傜偣  */
-const filterNode = (value: string, data: any) => {
-  if (!value) return true;
-  return data.label.indexOf(value) !== -1;
-};
+const defaultSelectUserIds = computed(() => computedIds(prop.data));
+
 /** 鏍规嵁鍚嶇О绛涢�夐儴闂ㄦ爲 */
 watchEffect(
   () => {
@@ -167,8 +159,28 @@
 );
 
 const confirm = () => {
-  emit('update:modelValue', [...selectUserList.value]);
+  emit('update:modelValue', selectUserList.value);
+  emit('confirmCallBack', selectUserList.value);
   userDialog.closeDialog();
+};
+
+const computedIds = (data) => {
+  if (data instanceof Array) {
+    return data.map(item => String(item));
+  } else if (typeof data === 'string') {
+    return data.split(',');
+  } else if (typeof data === 'number') {
+    return [data];
+  } else {
+    console.warn('<UserSelect> The data type of data should be array or string or number, but I received other');
+    return [];
+  }
+};
+
+/** 閫氳繃鏉′欢杩囨护鑺傜偣  */
+const filterNode = (value: string, data: any) => {
+  if (!value) return true;
+  return data.label.indexOf(value) !== -1;
 };
 
 /** 鏌ヨ閮ㄩ棬涓嬫媺鏍戠粨鏋� */
@@ -186,6 +198,14 @@
   total.value = res.total;
 };
 
+const pageList = async () => {
+  await getList();
+  const users = userList.value.filter((item) => {
+    return selectUserList.value.some((user) => user.userId === item.userId);
+  });
+  await tableRef.value.setCheckboxRow(users, true);
+};
+
 /** 鑺傜偣鍗曞嚮浜嬩欢 */
 const handleNodeClick = (data: DeptVO) => {
   queryParams.value.deptId = data.id;
@@ -198,16 +218,20 @@
   getList();
 };
 /** 閲嶇疆鎸夐挳鎿嶄綔 */
-const resetQuery = () => {
+const resetQuery = (refresh = true) => {
   dateRange.value = ['', ''];
   queryFormRef.value?.resetFields();
   queryParams.value.pageNum = 1;
   queryParams.value.deptId = undefined;
   deptTreeRef.value?.setCurrentKey(undefined);
-  handleQuery();
+  refresh && handleQuery();
 };
 
 const handleCheckboxChange = (checked) => {
+  if (!prop.multiple && checked.checked) {
+    tableRef.value.setCheckboxRow(selectUserList.value, false);
+    selectUserList.value = [];
+  }
   const row = checked.row;
   if (checked.checked) {
     selectUserList.value.push(row);
@@ -234,23 +258,44 @@
 
 const handleCloseTag = (user: UserVO) => {
   const userId = user.userId;
+  // 浣跨敤split鍒犻櫎鐢ㄦ埛
   const index = selectUserList.value.findIndex((item) => item.userId === userId);
   const rows = selectUserList.value[index];
   tableRef.value?.setCheckboxRow(rows, false);
   selectUserList.value.splice(index, 1);
 };
-watch(
-  () => prop.modelValue,
-  (newVal, oldValue) => {
-    Object.assign(selectUserList.value, newVal);
-  },
-  { deep: true }
-);
 
-onMounted(() => {
-  getTreeSelect();
-  getList();
-});
+const initSelectUser = async () => {
+  if (defaultSelectUserIds.value.length > 0) {
+    const { data } = await api.optionSelect(defaultSelectUserIds.value);
+    selectUserList.value = data;
+    const users = userList.value.filter((item) => {
+      return defaultSelectUserIds.value.includes(String(item.userId));
+    });
+    await nextTick(() => {
+      tableRef.value.setCheckboxRow(users, true);
+    });
+  }
+};
+const close = () => {
+  userDialog.closeDialog();
+};
+
+watch(
+  () => userDialog.visible.value,
+  async (newValue: boolean) => {
+    if (newValue) {
+      await getTreeSelect(); // 鍒濆鍖栭儴闂ㄦ暟鎹�
+      await getList(); // 鍒濆鍖栧垪琛ㄦ暟鎹�
+      await initSelectUser();
+    } else {
+      tableRef.value.clearCheckboxReserve();
+      tableRef.value.clearCheckboxRow();
+      resetQuery(false);
+      selectUserList.value = [];
+    }
+  }
+);
 
 defineExpose({
   open: userDialog.openDialog,
@@ -258,4 +303,4 @@
 });
 </script>
 
-<style lang="scss" scoped></style>
\ No newline at end of file
+<style lang="scss" scoped></style>

--
Gitblit v1.9.3