courseDialog.vue 11.9 KB
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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
<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="titleHtml" slot-scope="scope">
        <span v-if="scope.scope.title.length < 28">{{
          scope.scope.title.slice(0, 28)
        }}</span>
        <div v-else>
          <el-tooltip
            class="item"
            effect="dark"
            :content="scope.scope.title"
            placement="top-start"
          >
            <span> {{ scope.scope.title.slice(0, 28) }}...</span>
          </el-tooltip>
        </div>
      </div>
      <!-- 课程状态 -->
      <div slot="statusHtml" slot-scope="scope">
        <div class="online-status">
          <span
            class="dot"
            :style="{
              background: getStatusColor(
                scope && scope.scope && scope.scope.courseStatus,
                'courseStatusOptMap'
              )
            }"
          ></span>
          {{
            getStatusItem(
              baseData,
              "courseStatusOpt",
              scope.scope,
              "courseStatus"
            )
          }}
        </div>
      </div>
    </d2-table>
  </d2-dialog>
</template>
<script>
import { queryCoursePage, getOrganizationList } from "@api/serviceCentre"
import {
  courseFileTypeOpt,
  courseStatusOpt,
  courseStatusOptMap
} from "@/common/constants.js"
import { Util, deepClone } from "@/common/util"
export default {
  data() {
    return {
      selectedRows: [], // 改为数组,仅存整条行数据
      selectingData: [],
      // 分页
      total: 0,
      page: {
        currentPage: 1,
        pageSize: 10
      },
      searchForm: {
        title: "",
        courseFileType: "",
        organizationV2Id: ""
      },
      showFormData: [
        {
          label: "课程标题",
          name: "title",
          type: "input",
          isClear: true
        },
        {
          label: "类型",
          name: "courseFileType",
          type: "select",
          isClear: true,
          optionList: [],
          optionId: "confId",
          optionLabel: "confValue",
          optionsName: "courseFileTypeOpt"
        },
        {
          label: "所属部门",
          name: "organizationV2Id",
          type: "select",
          isClear: true,
          optionList: [],
          optionId: "oid",
          optionLabel: "name",
          optionsName: "organizationOpt"
        }
      ],
      tableParams: {
        listName: "list",
        mutiSelectUniqueKey: "courseId",
        mutiSelectName: "mutiSelect",
        tableColumns: [
          {
            label: "课程标题",
            prop: "title",
            addHtml: "titleHtml",
            width: "200px"
          },
          {
            label: "类型",
            prop: "courseFileType",
            width: "100px",
            formatter: (cellValue) => {
              return this.getStatusItem(
                this.baseData,
                "courseFileTypeOpt",
                cellValue,
                "courseFileType"
              )
            }
          },
          {
            label: "课程状态",
            prop: "courseStatus",
            minWidth: 120,
            addHtml: "statusHtml",
            isPopConfig: {
              text: "课程状态来自北森系统的课程管理,若课程状态为“已下架”,则不会展示在学习中心。"
            }
          },
          {
            label: "课程分类",
            prop: "categoryName",
            width: 160
          },
          {
            label: "所属部门",
            prop: "organizationName",
            width: 120
          },
          {
            label: "评分",
            prop: "totalStar",
            width: 120
          }
        ]
      },
      baseData: {
        courseFileTypeOpt: deepClone(courseFileTypeOpt),
        courseStatusOptMap: deepClone(courseStatusOptMap),
        organizationOpt: [],
        courseStatusOpt: deepClone(courseStatusOpt)
      },
      isFetchingPage: false, // 正在分页/拉取数据时的标记
      isRestoringSelection: false // 正在程序化恢复勾选时的标记
    }
  },
  props: {
    isVisible: {
      type: Boolean,
      default: false
    }
  },
  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: {
    async fetchOnboardingPlans() {
      try {
        const [organizationOpt] = await Promise.all([getOrganizationList()])
        // 处理返回的数据
        this.baseData.organizationOpt = organizationOpt.list
        this.getOptionsList(this, "showFormData", true)
      } catch (error) {
        console.error(error)
      }
    },
    openEmployeeDialog(initialIds = []) {
      this.selectedIds = new Set(
        (initialIds || []).map((courseId) => String(courseId))
      )
      this.getSelectingList()
    },
    handleSearch() {
      this.$refs.seletingTable.refreshTable_noApi()
      this.page = this.$options.data.call(this).page
      this.getSelectingList()
    },
    refreshTable(page) {
      console.log(page)
      this.page = page
      this.getSelectingList()
    },
    handleReset() {
      this.searchForm = this.$options.data.call(this).searchForm
      this.handleSearch()
    },
    getParams() {
      let params = deepClone({ ...this.searchForm, ...this.page })
      params.courseStatus = 40
      return params
    },
    getSelectingList() {
      this.isFetchingPage = true
      let params = this.getParams()
      // 排除未分类课程
      params.excludeNullCategoryId = 1
      queryCoursePage(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
        })
    },
    // 支持初始化为 ID 数组或 Row 数组;都会写入 selectedRows
    openEmployeeDialog(initialSelection = []) {
      this.selectedRows = []
      ;(initialSelection || []).forEach((item) => {
        if (item && typeof item === "object") {
          this.selectedRows.push(item)
        } else {
          this.selectedRows.push({ courseId: item })
        }
      })
      this.getSelectingList()
    },
    // 恢复勾选:根据 selectedRows 中的 courseId 判断
    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.courseId))
      )
      this.selectingData.forEach((row) => {
        const checked = selectedIdSet.has(String(row.courseId))
        innerElTable.toggleRowSelection(row, checked)
      })
    },
    // 分页选择变更:移除当前页未选,追加当前页已选(按 courseId 去重)
    onSelectionChange(rows) {
      if (this.isFetchingPage || this.isRestoringSelection) return

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

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

      // 再追加当前页新选中的行(去重)
      const existingIds = new Set(
        this.selectedRows.map((r) => String(r.courseId))
      )
      selectedInPageMap.forEach((row, id) => {
        if (!existingIds.has(id)) this.selectedRows.push(row)
      })
    },
    // 确认仅返回整条数据数组
    handleConfirm() {
      this.$emit("handleConfirm", this.selectedRows)
    },
    handleClose() {
      this.$emit("handleClose")
    },
    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) || "-"
    },
    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)
            }
          }
        })
      }
    }
  }
}
</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>