empDialog.vue 10.9 KB
<template>
  <d2-dialog
    title="选择员工"
    :is-visible.sync="isVisible"
    width="1080px"
    btn-ok-label="确定"
    @confirmDialog="handleConfirm"
    @closeDialog="handleClose"
    class="relateEmp"
  >
    <div class="header">
      <d2-search
        ref="showSearch"
        class="show-search"
        :search-form="searchForm"
        :label-width="'100px'"
        :form-data="showFormData"
      >
      </d2-search>
      <div class="up-btns">
        <el-button size="small" type="primary" @click="handleSearch">
          查询
        </el-button>
        <el-button size="small" @click="handleReset"> 重置 </el-button>
      </div>
    </div>
    <div class="select-text">已选择:{{ selectedRows.length }}</div>
    <d2-table
      ref="seletingTable"
      :tableData="selectingData"
      :tableTotal="total"
      :table-params="tableParams"
      :reserve-selection="true"
      @refreshTable="refreshTable"
      @selectionTable="onSelectionChange"
    >
      <!-- 人员状态 -->
      <div slot="zzStatusHtml" slot-scope="scope">
        <div class="online-status">
          <span
            class="dot"
            :style="{
              background: getStatusColor(
                scope && scope.scope && scope.scope.zzStatus,
                'zzStatusMap'
              )
            }"
          ></span>
          {{ getStatusItem(baseData, "zzStatusOpt", scope.scope, "zzStatus") }}
        </div>
      </div>
    </d2-table>
  </d2-dialog>
</template>
<script>
import {
  getBeiSenEmployeeList,
  getOrganizationList,
  getBeisenJobList
} from "@/api/serviceCentre"
import { zzStatusOpt, zzStatusMap } from "@/common/constants.js"
import { Util, deepClone } from "@/common/util"
export default {
  data() {
    return {
      selectedIds: new Set(),
      selectedData: [],
      selectedRows: [], // 改为数组,仅存整条行数据
      selectingData: [],
      // 分页
      total: 0,
      page: {
        currentPage: 1,
        pageSize: 10
      },
      searchForm: {
        name: "",
        mobilePhone: "",
        oIdJobPostId: "",
        oldDepartmentId: "",
        zzStatus: ""
      },
      showFormData: [
        {
          label: "员工名称",
          name: "name",
          type: "input",
          isClear: true
        },
        {
          label: "员工手机号",
          name: "mobilePhone",
          type: "input",
          isClear: true
        },
        {
          label: "所属部门",
          name: "oldDepartmentId",
          type: "select",
          isClear: true,
          optionList: [],
          optionId: "oid",
          optionLabel: "name",
          optionsName: "organizationOpt"
        },
        {
          label: "职务",
          name: "oIdJobPostId",
          type: "select",
          isClear: true,
          optionList: [],
          optionId: "oid",
          optionLabel: "name",
          optionsName: "postTypeOpt"
        },
        {
          label: "人员状态",
          name: "zzStatus",
          type: "select",
          isClear: true,
          optionList: [],
          optionId: "confId",
          optionLabel: "confValue",
          optionsName: "zzStatusOpt"
        }
      ],
      tableParams: {
        listName: "list",
        mutiSelectUniqueKey: "userID",
        mutiSelectName: "mutiSelect",
        tableColumns: [
          {
            label: "员工姓名",
            prop: "name",
            width: "200px"
          },
          {
            label: "员工手机号",
            prop: "mobilePhone"
          },
          {
            label: "所属部门",
            prop: "oldDepartmentName"
          },
          {
            label: "职务",
            prop: "oidJobPostName"
          },
          {
            label: "人员状态",
            prop: "zzStatus",
            addHtml: "zzStatusHtml"
          }
        ]
      },
      baseData: {
        zzStatusMap: deepClone(zzStatusMap),
        organizationOpt: [], //部门
        postTypeOpt: [], //职务
        zzStatusOpt: deepClone(zzStatusOpt)
      },
      isFetchingPage: false, // 正在分页/拉取数据时的标记
      isRestoringSelection: false // 正在程序化恢复勾选时的标记
    }
  },
  props: {
    isVisible: {
      type: Boolean,
      default: false
    },
    selectingList: {
      type: Array,
      default: () => []
    }
  },
  watch: {
    isVisible: {
      handler(val) {
        if (val) {
          this.$nextTick(() => {
            this.searchForm = this.$options.data.call(this).searchForm
            this.page = this.$options.data.call(this).page
            this.$refs.seletingTable.refreshTable_noApi()
            this.getSelectingList()
          })

          this.fetchOnboardingPlans()
        }
      },
      deep: true
    }
  },
  methods: {
    refreshTable(page) {
      this.page = page
      this.getSelectingList()
    },
    handleReset() {
      this.searchForm = this.$options.data.call(this).searchForm
      this.handleSearch()
    },
    handleSearch() {
      this.$refs.seletingTable.refreshTable_noApi()
      this.page = this.$options.data.call(this).page
      this.getSelectingList()
    },
    getStatusColor(status, optKey) {
      if (status === null || status === undefined) return "#C0C4CC"
      const map = (this.baseData && this.baseData[optKey]) || {}
      return map[status] || "#C0C4CC"
    },
    getStatusItem(baseData, optKey, row, rowKey) {
      if (!baseData || !baseData[optKey] || !row) return null
      let item = baseData[optKey].find((item) => item.confId == row[rowKey])
      return (item && item.confValue) || "-"
    },
    openEmployeeDialog(initialSelection = []) {
      this.selectedRows = []
      ;(initialSelection || []).forEach((item) => {
        if (item && typeof item === "object") {
          this.selectedRows.push(item)
        } else {
          this.selectedRows.push({ userID: item })
        }
      })
      this.getSelectingList()
    },
    getParams() {
      let params = deepClone({ ...this.searchForm, ...this.page })
      return params
    },
    getSelectingList() {
      this.isFetchingPage = true
      let params = this.getParams()
      getBeiSenEmployeeList(params)
        .then((res) => {
          this.selectingData = res.list
          this.total = res.total
          this.$nextTick(() => {
            this.isRestoringSelection = true
            this.restoreSelection()
            this.isRestoringSelection = false
            this.isFetchingPage = false
          })
        })
        .catch(() => {
          this.isFetchingPage = false
        })
    },
    // 恢复勾选:根据 selectedRows 中的 id 判断
    restoreSelection() {
      const innerTableRefName = this.tableParams.mutiSelectName + "MutiTable"
      const innerElTable =
        this.$refs.seletingTable &&
        this.$refs.seletingTable.$refs &&
        this.$refs.seletingTable.$refs[innerTableRefName]
      if (!innerElTable) return

      const selectedIdSet = new Set(
        this.selectedRows.map((r) => String(r.userID))
      )
      this.selectingData.forEach((row) => {
        const checked = selectedIdSet.has(String(row.userID))
        innerElTable.toggleRowSelection(row, checked)
      })
    },
    // 分页选择变更:移除当前页未选,追加当前页已选(按 id 去重)
    onSelectionChange(rows) {
      if (this.isFetchingPage || this.isRestoringSelection) return

      const pageIds = new Set(this.selectingData.map((r) => String(r.userID)))
      const selectedInPageMap = new Map(rows.map((r) => [String(r.userID), r]))

      // 先移除当前页中未选的旧项
      this.selectedRows = this.selectedRows.filter((r) => {
        const userID = String(r.userID)
        return !pageIds.has(userID) || selectedInPageMap.has(userID)
      })

      // 再追加当前页新选中的行(去重)
      const existingIds = new Set(
        this.selectedRows.map((r) => String(r.userID))
      )
      selectedInPageMap.forEach((row, userID) => {
        if (!existingIds.has(userID)) this.selectedRows.push(row)
      })
    },
    // 确认仅返回整条数据数组
    handleConfirm() {
      console.log("this.selectedRows", this.selectedRows)
      this.$emit("handleConfirm", this.selectedRows)
    },
    handleClose() {
      this.$emit("handleClose")
    },
    getOptionsList(that, formName, hasAll = true, refreshSigle) {
      if (refreshSigle) {
        var item = that[formName].find((k) => k.name == refreshSigle)
        var index = that[formName].findIndex((k) => k.name == refreshSigle)
        that.$set(
          that[formName][index],
          "optionList",
          deepClone(that.baseData[item.optionsName])
        )
        if (
          hasAll &&
          that.baseData[item.optionsName].length &&
          that.baseData[item.optionsName][0][
            that[formName][index].optionLabel
          ] !== "全部"
        ) {
          const temp = {}
          temp[that[formName][index].optionId] = ""
          temp[that[formName][index].optionLabel] = "全部"
          that[formName][index].optionList.unshift(temp)
        }
      } else {
        that[formName].forEach((item, index) => {
          if (
            item.type === "select" &&
            that.baseData[item.optionsName]?.length
          ) {
            that.$set(
              that[formName][index],
              "optionList",
              deepClone(that.baseData[item.optionsName])
            )
            if (
              hasAll &&
              that.baseData[item.optionsName].length &&
              that.baseData[item.optionsName][0][
                that[formName][index].optionLabel
              ] !== "全部"
            ) {
              const temp = {}
              temp[that[formName][index].optionId] = ""
              temp[that[formName][index].optionLabel] = "全部"
              that[formName][index].optionList.unshift(temp)
            }
          }
        })
      }
    },
    async fetchOnboardingPlans() {
      try {
        const [organizationOpt, postTypeOpt] = await Promise.all([
          getOrganizationList(),
          getBeisenJobList()
        ])
        // 处理返回的数据
        this.baseData.organizationOpt = organizationOpt.list
        this.baseData.postTypeOpt = postTypeOpt.list
        this.getOptionsList(this, "showFormData", true)
      } catch (error) {
        console.error(error)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.relateEmp {
  ::v-deep .el-dialog__header {
    border-bottom: 1px solid #e5e5e5;
  }
  ::v-deep .d2-search .d2-search-block {
    padding: 0px !important;
  }
  ::v-deep .el-dialog__header {
    font-size: 20px;
    line-height: 24px;
  }
  .select-text {
    color: #333333;
    font-size: 14px;
    position: relative;
    right: 65%;
    margin-bottom: 8px;
  }
  .online-status {
    display: flex;
    justify-content: center;
    align-items: center;

    .dot {
      display: inline-block;
      width: 8px;
      height: 8px;
      border-radius: 50%;
      margin-right: 8px;
    }
  }
}
</style>