yyb
11 小时以前 a1df9699594b0a0e46d26a0394eafb1eb030c68b
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
import {
  createEmptyForm,
  normalizeEnterpriseNewsStatus,
  publishStatusLabel,
  PUBLISH_STATUS_OPTIONS,
} 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) => {
        const hit = PUBLISH_STATUS_OPTIONS.find((x) => x.value === v);
        return hit?.tag || "info";
      },
    },
    { 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(),
    },
  ];
}