import { createEmptyForm, normalizeEnterpriseNewsStatus, publishStatusLabel, publishStatusTag, } from "./enterpriseNewsUtils.js"; /** formPayload 中存放完整企业新闻业务数据的键(审批实例保存用) */ export const ENTERPRISE_NEWS_PAYLOAD_KEY = "enterpriseNews"; const READ_SCOPE_FROM_API = { all: "all", dept: "department", department: "department", custom: "custom", management: "management", }; const READ_SCOPE_TO_API = { all: "all", department: "dept", dept: "dept", custom: "custom", management: "all", }; export function mapReadScopeFromApi(scope) { const key = String(scope ?? "").trim().toLowerCase(); return READ_SCOPE_FROM_API[key] || key || "all"; } export function mapReadScopeToApi(scope) { return READ_SCOPE_TO_API[scope] || scope || "all"; } export function unwrapEnterpriseNewsPage(res) { const data = res?.data ?? res; if (!data || typeof data !== "object") { return { records: [], total: 0 }; } if (Array.isArray(data.records)) { return { records: data.records, total: Number(data.total ?? 0) }; } const nested = data.data; if (nested && typeof nested === "object" && Array.isArray(nested.records)) { return { records: nested.records, total: Number(nested.total ?? 0) }; } return { records: [], total: 0 }; } /** 组装 listPage 查询参数 */ export function buildEnterpriseNewsListParams({ page, searchForm }) { const params = { current: page.current, size: page.size, }; const kw = (searchForm?.keyword || "").trim(); if (kw) params.title = kw; if (searchForm?.newsType) params.category = searchForm.newsType; if (searchForm?.status) params.status = searchForm.status; const range = searchForm?.createTimeRange; if (Array.isArray(range) && range[0]) { params.createTimeStart = range[0]; } if (Array.isArray(range) && range[1]) { params.createTimeEnd = range[1]; } return params; } /** 接口 EnterpriseNewsVo → 列表行 */ export function mapEnterpriseNewsFromApi(row) { if (!row) return {}; const newsStatus = normalizeEnterpriseNewsStatus(row.status); return { ...row, newsNo: row.id != null ? String(row.id) : "—", newsType: row.category || "", contentHtml: row.content || "", publisherName: row.createUserName || "—", publishTime: row.createTime || "", updateTime: row.updateTime || "", newsStatus, requireReadConfirm: row.isRequired === "1" || row.isRequired === 1, readScope: mapReadScopeFromApi(row.readScope), readCount: row.readCount ?? 0, requiredReadCount: row.requiredReadCount ?? 0, }; } /** 是否允许修改(草稿、驳回可改) */ export function canEditEnterpriseNewsRow(row) { const status = normalizeEnterpriseNewsStatus(row?.newsStatus ?? row?.status); return status === "DRAFT" || status === "REJECTED"; } /** 接口行 / 详情 → 表单 */ export function mapApiRowToNewsForm(row) { if (!row) return createEmptyForm(); return { ...createEmptyForm(), id: row.id != null ? String(row.id) : "", newsNo: row.id != null ? String(row.id) : "", title: row.title || "", summary: row.summary || "", contentHtml: row.content || row.contentHtml || "", newsType: row.newsType || row.category || "announcement", readScope: mapReadScopeFromApi(row.readScope), requireReadConfirm: Boolean(row.requireReadConfirm ?? row.isRequired === "1"), publisherName: row.createUserName || row.publisherName || "", publishStatus: normalizeEnterpriseNewsStatus(row.newsStatus ?? row.status), templateId: row.templateId, templateName: row.templateName || "", targetDeptIds: [...(row.deptIds || row.targetDeptIds || [])], targetUserIds: [...(row.userIds || row.targetUserIds || [])], }; } /** 审批实例行 formPayload → 表单(兼容旧数据) */ export function extractEnterpriseNewsFromRow(row) { if (!row?.formPayload && !row?.formFieldDefs && !row?.instanceNo) { return mapApiRowToNewsForm(row); } const payload = row?.formPayload || {}; const raw = payload[ENTERPRISE_NEWS_PAYLOAD_KEY]; if (raw && typeof raw === "object") { return { ...createEmptyForm(), ...raw }; } return { ...createEmptyForm(), title: payload.title || row?.title || "", summary: payload.summary || "", newsType: payload.newsType || row?.category || "announcement", contentHtml: payload.contentHtml || row?.content || "", }; } export function syncNewsFormToSubmitPayload(newsForm, submitForm) { const snapshot = JSON.parse(JSON.stringify(newsForm)); submitForm.formPayload = { ...(submitForm.formPayload || {}), [ENTERPRISE_NEWS_PAYLOAD_KEY]: snapshot, title: snapshot.title, summary: snapshot.summary, }; } function toIdList(ids) { if (!Array.isArray(ids) || !ids.length) return undefined; const list = ids .map((id) => (typeof id === "number" ? id : Number(id))) .filter((n) => !Number.isNaN(n)); return list.length ? list : undefined; } /** 表单 → POST /enterpriseNews/save 请求体 */ export function buildEnterpriseNewsSaveDto(newsForm, { status } = {}) { const dto = { title: (newsForm.title || "").trim(), summary: newsForm.summary || "", content: newsForm.contentHtml || "", category: newsForm.newsType || "", readScope: mapReadScopeToApi(newsForm.readScope), isRequired: newsForm.requireReadConfirm ? "1" : "0", status: normalizeEnterpriseNewsStatus(status ?? newsForm.publishStatus), }; const rawId = newsForm.id; if (rawId != null && rawId !== "") { const id = Number(rawId); if (!Number.isNaN(id)) dto.id = id; } const deptIds = toIdList(newsForm.targetDeptIds); if (deptIds) dto.deptIds = deptIds; const userIds = toIdList(newsForm.targetUserIds); if (userIds) dto.userIds = userIds; const templateId = newsForm.templateId; if (templateId != null && templateId !== "") { const tid = Number(templateId); if (!Number.isNaN(tid)) dto.templateId = tid; } if (newsForm.templateName) dto.templateName = newsForm.templateName; return dto; } export function buildEnterpriseNewsTableColumns(buildTableActions) { return [ { label: "编号", prop: "newsNo", width: 120 }, { label: "标题", prop: "title", minWidth: 180, showOverflowTooltip: true }, { label: "分类", prop: "newsType", width: 100, dataType: "slot", slot: "newsType", }, { label: "状态", prop: "newsStatus", width: 100, dataType: "tag", formatData: (v) => publishStatusLabel(v), formatType: (v) => publishStatusTag(v), }, { label: "创建人", prop: "publisherName", width: 110 }, { label: "创建时间", prop: "createTime", width: 170 }, { label: "更新时间", prop: "updateTime", width: 170 }, { dataType: "action", label: "操作", align: "center", fixed: "right", width: 220, operation: buildTableActions(), }, ]; }