import Vue from "vue"; import { get, postJ } from "@/utils/request"; // 岗位状态:0-停用 / 1-启用 / 2-待撤销(与后端一致) export const POSITION_STATUS = { DISABLED: 0, ENABLED: 1, PENDING_REVOKE: 2, }; export const POSITION_STATUS_LABEL = { 0: "停用", 1: "启用", 2: "待撤销", }; /** 岗位基本信息 → 页面选项形状 */ function positionOption(raw = {}) { return { id: String(raw.id != null ? raw.id : raw.positionId || ""), name: raw.positionName || raw.name || "", code: raw.positionCode || "", deptId: raw.deptId != null ? raw.deptId : "", deptName: raw.deptName || "", level: raw.positionLevel || "", levels: Array.isArray(raw.positionLevels) ? raw.positionLevels : [], sequence: raw.positionSequence || "", }; } /** 只保留正整数主键,避免把空值当条件发给后端 */ function positiveId(value) { const num = Number(value); return Number.isFinite(num) && num > 0 ? num : undefined; } /** * 岗位分页查询 * POST /hr/position/page */ export async function pagePosition(query = {}) { const body = { pageNum: Number(query.pageNum) || 1, size: Number(query.size) || 20, sortName: query.sortName || "createTime", orderBy: query.orderBy || "ascending", }; if (query.deptId !== undefined && query.deptId !== null && query.deptId !== "") { body.deptId = String(query.deptId || 0); } if ( query.organizationId !== undefined && query.organizationId !== null && query.organizationId !== "" ) { body.organizationId = String(query.organizationId) || 0; } if (query.keyword !== undefined && query.keyword !== null) { body.keyword = String(query.keyword); } if (query.positionSequence) body.positionSequence = String(query.positionSequence); if (query.positionLevel) body.positionLevel = String(query.positionLevel); if (query.status !== undefined && query.status !== null && query.status !== "") { body.status = Number(query.status); } const res = await postJ(Vue.prototype.apiUrl + "/hr/position/page", body); if (res.code == 0) { return ( res.data || { count: 0, list: [], pageNum: body.pageNum, size: body.size, } ); } throw new Error(res.msg || "岗位列表加载失败"); } /** * 按部门加载可选岗位(启用状态) * @returns {{ list: Array, restricted: boolean }} */ export async function loadSelectablePositions({ deptId, keyword = "" } = {}) { const deptKey = positiveId(deptId); if (!deptKey) return { list: [], restricted: false }; const res = await postJ(Vue.prototype.apiUrl + "/hr/position/page", { pageNum: 1, size: 200, sortName: "createTime", orderBy: "ascending", deptId: String(deptKey), status: POSITION_STATUS.ENABLED, }); const rows = (res.code == 0 && res.data && res.data.list) || []; const text = String(keyword || "").trim().toLowerCase(); const list = rows .filter((item) => { if (!text) return true; return `${item.positionCode || ""}${item.positionName || ""}` .toLowerCase() .indexOf(text) !== -1; }) .map(positionOption); return { list, restricted: false }; } /** 岗位状态翻译 */ export function translatePositionStatus(status) { if (status === undefined || status === null || status === "") return ""; return POSITION_STATUS_LABEL[status] || String(status); } // ============ 部门岗位关联表(/fm/tmaindepartmentposition)级联查询 ============ // 与 PC 端 kd-aiot-frontend-hr 的岗位调动表单共用同一套级联: // 部门 → 岗位序列 → 岗位 → 岗位性质(取首条) → 岗位层级 function compactQuery(object = {}) { return Object.keys(object).reduce((result, key) => { const value = object[key]; if (value !== "" && value !== null && value !== undefined) result[key] = value; return result; }, {}); } function keepId(value) { if (value === undefined || value === null || value === "") return undefined; const text = String(value).trim(); if (!/^-?\d+$/.test(text)) return value; if (!Number.isSafeInteger(Number(text))) return text; return Number(text); } async function cascadeGet(path, params, loding) { const res = await get(Vue.prototype.apiUrl + path, compactQuery(params), loding); if (res && Number(res.code) === 0) { const data = res.data; if (Array.isArray(data)) return data; if (data && Array.isArray(data.list)) return data.list; if (data && Array.isArray(data.records)) return data.records; return []; } throw new Error((res && res.message) || "查询失败"); } /** 部门 → 岗位序列 */ export async function getDeptPositionSequences(deptId, loding = true) { return cascadeGet( "/fm/tmaindepartmentposition/sequence/list", { deptId: keepId(deptId) }, loding ); } /** 部门 + 岗位序列 → 岗位 */ export async function getDeptPositions({ deptId, positionSequenceId } = {}, loding = true) { return cascadeGet( "/fm/tmaindepartmentposition/position/list", { deptId: keepId(deptId), positionSequenceId: keepId(positionSequenceId) }, loding ); } /** 部门 + 岗位 + 岗位序列 → 岗位性质(数组,调用方取首条) */ export async function getDeptPositionType( { deptId, positionId, positionSequenceId } = {}, loding = true ) { return cascadeGet( "/fm/tmaindepartmentposition/positionType/list", { deptId: keepId(deptId), positionId: keepId(positionId), positionSequenceId: keepId(positionSequenceId), }, loding ); } /** 部门 + 岗位 + 岗位序列 + 岗位性质 → 岗位层级 */ export async function getDeptPositionLevels( { deptId, positionId, positionSequenceId, positionType } = {}, loding = true ) { return cascadeGet( "/fm/tmaindepartmentposition/positionLevel/list", { deptId: keepId(deptId), positionId: keepId(positionId), positionSequenceId: keepId(positionSequenceId), positionType, }, loding ); }