Sfoglia il codice sorgente

feat(inspectionWork): 添加默认执行部门和执行人功能,基于当前登录用户所属公司

xieyong 4 giorni fa
parent
commit
7ad201d1d7

+ 15 - 54
src/components/CommomSelect/dept-select.vue

@@ -20,7 +20,7 @@
 
 <script>
   import { listOrganizations } from '@/api/system/organization';
-  import { getCurrentUser } from '@/utils/token-util';
+  import { getCurrentGroupId, createCompanyFinder } from '@/utils/org-util';
   export default {
     props: {
       // 选中的数据(v-model)
@@ -83,67 +83,28 @@
           parentIdField: 'parentId'
         });
       },
-      /* 当前登录用户所属机构id(优先取顶栏切换后的当前机构) */
-      getCurrentGroupId() {
-        let currentGroupId = null;
-        try {
-          currentGroupId = getCurrentUser()?.currentGroupId;
-        } catch (e) {
-          currentGroupId = null;
-        }
-        const userInfo = this.$store?.state?.user?.info || {};
-        return currentGroupId || userInfo.groupId || null;
-      },
       /* 只保留当前登录用户所属公司(顶级机构)下的机构 */
       filterByCurrentCompany(list) {
-        const groupId = this.getCurrentGroupId();
+        const userInfo = this.$store?.state?.user?.info;
+        const groupId = getCurrentGroupId(userInfo);
         if (groupId == null || groupId === '') {
           return list;
         }
-        const nodeMap = new Map();
-        list.forEach((item) => nodeMap.set(String(item.id), item));
-        if (!nodeMap.has(String(groupId))) {
-          // 找不到当前用户所属机构时不做过滤, 避免下拉框被清空
+        const findCompany = createCompanyFinder(list);
+        const companyNode = findCompany(groupId);
+        if (!companyNode) {
+          // 找不到当前用户所属公司时不做过滤, 避免下拉框被清空
           return list;
         }
-        const companyCache = new Map();
-        /* 沿 parentId 上溯, 取所属顶级机构(公司) */
-        const getCompanyId = (id) => {
-          const key = String(id);
-          if (companyCache.has(key)) {
-            return companyCache.get(key);
-          }
-          let node = nodeMap.get(key);
-          let companyId = key;
-          const visited = new Set();
-          while (node) {
-            companyId = String(node.id);
-            const parentKey =
-              node.parentId == null || node.parentId === ''
-                ? ''
-                : String(node.parentId);
-            if (
-              !parentKey ||
-              !nodeMap.has(parentKey) ||
-              visited.has(companyId)
-            ) {
-              break;
-            }
-            visited.add(companyId);
-            node = nodeMap.get(parentKey);
-          }
-          companyCache.set(key, companyId);
-          return companyId;
-        };
-        const companyId = getCompanyId(groupId);
-        const selectedKey =
-          this.value == null || this.value === '' ? '' : String(this.value);
-        const selectedCompanyId =
-          selectedKey && nodeMap.has(selectedKey)
-            ? getCompanyId(selectedKey)
-            : '';
+        const companyId = String(companyNode.id);
+        const selectedNode =
+          this.value == null || this.value === ''
+            ? null
+            : findCompany(this.value);
+        const selectedCompanyId = selectedNode ? String(selectedNode.id) : '';
         return list.filter((item) => {
-          const itemCompanyId = getCompanyId(item.id);
+          const itemCompany = findCompany(item.id);
+          const itemCompanyId = itemCompany ? String(itemCompany.id) : '';
           if (itemCompanyId === companyId) {
             return true;
           }

+ 80 - 0
src/utils/org-util.js

@@ -0,0 +1,80 @@
+import { getCurrentUser } from '@/utils/token-util';
+import { listOrganizations } from '@/api/system/organization';
+
+/**
+ * 当前登录用户所属机构id
+ * 优先取顶栏切换后的当前机构, 兜底取用户自身所属机构
+ * @param {Object} userInfo 当前登录用户信息
+ * @returns {String|Number|null}
+ */
+export function getCurrentGroupId(userInfo) {
+  let currentGroupId = null;
+  try {
+    currentGroupId = getCurrentUser()?.currentGroupId;
+  } catch (e) {
+    currentGroupId = null;
+  }
+  return currentGroupId || userInfo?.groupId || null;
+}
+
+/**
+ * 生成"查所属公司"的函数
+ * 机构树顶级节点即为公司, 沿 parentId 向上找到顶级节点
+ * @param {Array} list 机构列表(扁平)
+ * @returns {Function} (groupId) => 公司节点 | null
+ */
+export function createCompanyFinder(list) {
+  const nodeMap = new Map();
+  (list || []).forEach((item) => nodeMap.set(String(item.id), item));
+  return (groupId) => {
+    if (groupId == null || groupId === '') {
+      return null;
+    }
+    let node = nodeMap.get(String(groupId));
+    if (!node) {
+      return null;
+    }
+    // 沿 parentId 上溯, 同时用 visited 防止脏数据造成的死循环
+    const visited = new Set();
+    while (node) {
+      const parentKey =
+        node.parentId == null || node.parentId === ''
+          ? ''
+          : String(node.parentId);
+      if (
+        !parentKey ||
+        !nodeMap.has(parentKey) ||
+        visited.has(String(node.id))
+      ) {
+        break;
+      }
+      visited.add(String(node.id));
+      node = nodeMap.get(parentKey);
+    }
+    return node;
+  };
+}
+
+/**
+ * 查找机构所属的公司(顶级机构)
+ * @param {Array} list 机构列表(扁平)
+ * @param {String|Number} groupId 机构id
+ * @returns {Object|null} 公司节点
+ */
+export function findCompanyNode(list, groupId) {
+  return createCompanyFinder(list)(groupId);
+}
+
+/**
+ * 当前登录用户所属的公司
+ * @param {Object} userInfo 当前登录用户信息
+ * @returns {Promise<Object|null>} 公司节点
+ */
+export async function getCurrentUserCompany(userInfo) {
+  const groupId = getCurrentGroupId(userInfo);
+  if (!groupId) {
+    return null;
+  }
+  const list = await listOrganizations();
+  return findCompanyNode(list, groupId);
+}

+ 56 - 5
src/views/inspectionWork/components/baseInfo.vue

@@ -160,7 +160,7 @@
                 v-for="item in form.qualityType == 2
                   ? form.jobList.find((job) => job.id == form.executeJobId)
                       ?.userVOList
-                  : executorList"
+                  : executorOptions"
                 :key="item.id"
                 :value="item.id"
                 :label="item.name"
@@ -457,6 +457,7 @@
   import { getProduceTaskList } from '@/api/aps';
   import { getByCode } from '@/api/system/dictionary-data';
   import { parameterGetByCode } from '@/api/main/index';
+  import { getCurrentUserCompany } from '@/utils/org-util';
   export default {
     components: {
       EquipmentDialog,
@@ -519,6 +520,20 @@
       //   deep: true
       // }
     },
+    computed: {
+      /* 执行人候选项: 保证当前登录用户始终在列表中, 否则多选框回显不出姓名 */
+      executorOptions() {
+        const list = Array.isArray(this.executorList) ? this.executorList : [];
+        const userInfo = this.$store?.state?.user?.info;
+        if (!userInfo || !userInfo.userId) {
+          return list;
+        }
+        if (list.find((item) => item.id == userInfo.userId)) {
+          return list;
+        }
+        return [...list, { id: userInfo.userId, name: userInfo.name }];
+      }
+    },
     data() {
       return {
         executorList: [],
@@ -767,9 +782,45 @@
             data = Object.assign(data, params);
           }
           const res = await getUserPage(data);
-          this.executorList = res.list;
+          this.executorList = (res && res.list) || [];
         } catch (error) {}
       },
+      /* 默认执行部门=当前登录用户所属公司, 默认执行人=当前登录用户 */
+      async setDefaultExecuteInfo() {
+        const userInfo = this.$store?.state?.user?.info;
+        if (!userInfo || !userInfo.userId) {
+          return;
+        }
+        // 班组模式下执行人由班组决定, 检验方案模式下没有执行部门, 均不处理
+        if (this.form.assignType == 1 || this.form.qualityType == 2) {
+          return;
+        }
+        // 执行人: 未指定时默认当前登录用户
+        // 注意: 不能依赖执行部门是否取到公司, 否则公司查不到时执行人也不会默认值
+        if (
+          !this.form.executeUserIdList ||
+          !this.form.executeUserIdList.length
+        ) {
+          this.$set(this.form, 'executeUserIdList', [userInfo.userId]);
+          this.$set(this.form, 'executeUserName', userInfo.name);
+          this.$set(this.form, 'executeUserId', String(userInfo.userId));
+        }
+        // 执行部门: 未指定时默认当前登录用户所属公司
+        if (!this.form.executeDeptId) {
+          let company = null;
+          try {
+            company = await getCurrentUserCompany(userInfo);
+          } catch (e) {
+            company = null;
+          }
+          if (company) {
+            this.$set(this.form, 'executeDeptId', company.id);
+            this.$set(this.form, 'executeDeptName', company.name);
+            // 执行部门变了, 人员范围跟着变
+            await this.getUserList({ groupId: company.id });
+          }
+        }
+      },
       // 人员选择
       changeExecutor(val) {
         this.$set(this.form, 'qualityIdList', val);
@@ -778,7 +829,7 @@
           this.form.qualityName = val
             .map(
               (item) =>
-                this.executorList.find((item2) => item2.id == item)?.name
+                this.executorOptions.find((item2) => item2.id == item)?.name
             )
             .join(',');
         } else {
@@ -795,9 +846,9 @@
               ? this.form.jobList.find(
                   (job) => job.id == this.form.executeJobId
                 )?.userVOList
-              : this.executorList;
+              : this.executorOptions;
           this.form.executeUserName = val
-            .map((item) => arr.find((item2) => item2.id == item)?.name)
+            .map((item) => arr?.find((item2) => item2.id == item)?.name)
             .join(',');
         } else {
           this.form.executeUserName = '';

+ 5 - 5
src/views/inspectionWork/components/inspectionWorkList.vue

@@ -159,7 +159,7 @@
           @click="openNumber(row)"
           >合格证</el-link
         >
-        <jimureportBrowse
+        <!-- <jimureportBrowse
           style="display: inline-block; margin-left: 5px"
           v-if="
             row.status == 1 &&
@@ -169,12 +169,12 @@
           text="检测报告"
           :businessId="row.id"
           businessCode="qmsqualityinspectionprint"
-        ></jimureportBrowse>
+        ></jimureportBrowse> -->
         <el-dropdown
           trigger="click"
           v-if="
             row.status == 1 &&
-            isReportApproval == 1 &&
+            // isReportApproval == 1 &&
             (!row.reportApprovalStatus || row.reportApprovalStatus == 0) &&
             isEmptyObject(row.reportTemplateJson) &&
             $hasPermission('qms:quality_work_order:generateReport')
@@ -201,7 +201,7 @@
         <el-link
           v-if="
             row.status == 1 &&
-            isReportApproval == 1 &&
+            // isReportApproval == 1 &&
             row.reportApprovalStatus &&
             !isEmptyObject(row.reportTemplateJson) &&
             $hasPermission('qms:quality_work_order:generateReport')
@@ -215,7 +215,7 @@
         <el-link
           v-if="
             row.status == 1 &&
-            isReportApproval == 1 &&
+            // isReportApproval == 1 &&
             (!row.reportApprovalStatus || row.reportApprovalStatus == 3) &&
             !isEmptyObject(row.reportTemplateJson) &&
             $hasPermission('qms:quality_work_order:qualityReportApproval')

+ 13 - 0
src/views/inspectionWork/edit.vue

@@ -1200,6 +1200,8 @@
         if (this.type == 'add') {
           const code = await getCode('quality_work_order_code');
           this.form.code = code;
+          // 新增时默认执行部门/执行人
+          this.setDefaultExecuteInfo();
         } else {
           if (
             this.$route.query.qualityTimeStart != '' ||
@@ -1210,6 +1212,15 @@
           }
         }
       },
+      /* 执行部门/执行人默认值: 执行部门=当前登录用户所属公司, 执行人=当前登录用户 */
+      setDefaultExecuteInfo() {
+        this.$nextTick(() => {
+          const baseInfo = this.$refs.baseInfoRefs;
+          if (baseInfo && baseInfo.setDefaultExecuteInfo) {
+            baseInfo.setDefaultExecuteInfo();
+          }
+        });
+      },
       getNowTime() {
         const nowDate = this.getNowDate();
         return (
@@ -1286,6 +1297,8 @@
               this.$refs.baseInfoRefs?.getTaskList();
             });
           }
+          // 执行部门/执行人默认值(仅在未指定时生效)
+          this.setDefaultExecuteInfo();
         });
 
         // await this.queryQualityInventory()