| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- /**
- * 将 DMS 人员底数中偏「测试/模拟」的姓名,改为更自然的真实人名风格。
- * 同步更新 title / c_name / c_payee_name(收款人与原姓名相同时一并改)。
- * 并尽量同步发放明细、告警里的人员姓名展示字段。
- *
- * 用法:node scripts/rename-mock-personnel-names.js
- */
- const http = require("http");
- const qs = require("querystring");
- const HOST = "121.43.55.7";
- const PORT = 2101;
- const PERSONNEL = { columnId: 1847, modelId: 1970 };
- const PAYMENT = { columnId: 1849, modelId: 1974 };
- const ALERT = { columnId: 1841, modelId: 1964 };
- const SURNAMES =
- "陈林黄赵吴周徐孙马朱胡郭何高罗郑梁谢宋唐许韩冯邓曹彭曾肖田董袁潘于蒋蔡余杜叶程苏魏吕丁任沈姚卢姜崔钟谭陆汪范金石廖贾夏韦傅方白邹孟熊秦邱江尹薛闫段雷侯龙史陶黎贺顾毛郝龚邵万钱严覃武戴莫孔向汤";
- const GIVEN = [
- "建国", "志强", "秀英", "桂英", "玉兰", "海燕", "丽华", "文斌", "晓东", "雅琴",
- "国强", "美玲", "建华", "春梅", "卫东", "秀兰", "永强", "桂芳", "俊杰", "秀珍",
- "立新", "慧敏", "德明", "玉珍", "志明", "秀云", "建军", "丽娟", "国平", "凤英",
- "永明", "秀芳", "学军", "玉梅", "伟强", "红梅", "建平", "金凤", "志华", "春花",
- "明华", "秀梅", "永华", "丽君", "国华", "玉华", "建民", "秀华", "志刚", "美华",
- "海波", "晓燕", "立军", "桂花", "文杰", "玉英", "永军", "淑芬", "建伟", "秀芬",
- "明辉", "玉芳", "国庆", "春兰", "志勇", "丽萍", "永刚", "秀琴", "建强", "美芳",
- "文华", "玉萍", "国栋", "桂兰", "志伟", "秀红", "永辉", "丽娜", "建辉", "美珍",
- "海峰", "玉红", "明强", "春燕", "立华", "秀萍", "文强", "桂珍", "永新", "丽敏",
- "建新", "美云", "国伟", "玉琴", "志军", "秀娟", "永平", "红英", "建龙", "美琴",
- ];
- function isMockName(name) {
- const n = String(name || "").trim();
- if (!n) return true;
- if (/测|测试|门禁|闭环|回盘|驳回|生命周期|暂停测|失败员|知晓请/.test(n)) return true;
- if (/^(张三|李四|王五|赵六|钱七|孙八|周九|吴十)$/.test(n)) return true;
- return false;
- }
- function post(urlPath, fields, token) {
- return new Promise((resolve, reject) => {
- const body = qs.stringify(fields || {});
- const headers = {
- "Content-Type": "application/x-www-form-urlencoded",
- "Content-Length": Buffer.byteLength(body),
- };
- if (token) {
- headers.Token = token;
- headers.token = token;
- }
- const req = http.request(
- { hostname: HOST, port: PORT, path: urlPath, method: "POST", headers },
- (res) => {
- let data = "";
- res.on("data", (c) => (data += c));
- res.on("end", () => {
- try {
- resolve(JSON.parse(data));
- } catch (e) {
- resolve({ raw: data, status: res.statusCode });
- }
- });
- }
- );
- req.on("error", reject);
- req.write(body);
- req.end();
- });
- }
- async function listAll(token, col) {
- const all = [];
- for (let page = 0; page < 100; page++) {
- const list = await post(
- "/proxy_dms/content/selectContentList",
- {
- columnId: String(col.columnId),
- modelId: String(col.modelId),
- page: String(page),
- pageSize: "100",
- states: "0",
- },
- token
- );
- const rows = (list.content && list.content.data) || [];
- all.push(...rows);
- if (rows.length < 100) break;
- }
- return all;
- }
- async function updateRow(token, col, patch) {
- return post(
- "/proxy_dms/content/updateContent",
- {
- columnId: String(col.columnId),
- modelId: String(col.modelId),
- content: JSON.stringify(patch),
- },
- token
- );
- }
- function buildNamePool(count) {
- const used = new Set();
- const out = [];
- let i = 0;
- while (out.length < count && i < count * 20) {
- i++;
- const sn = SURNAMES[out.length % SURNAMES.length];
- const gn = GIVEN[(out.length * 7 + i) % GIVEN.length];
- const name = sn + gn;
- if (used.has(name)) continue;
- used.add(name);
- out.push(name);
- }
- // 兜底:加序号避免撞名
- while (out.length < count) {
- const name = "陈" + GIVEN[out.length % GIVEN.length] + (out.length + 1);
- if (!used.has(name)) {
- used.add(name);
- out.push(name);
- }
- }
- return out;
- }
- (async () => {
- const login = await post("/proxy_oauth/user/login", {
- userName: "user_liu",
- password: "WE176852439@lmx",
- clientId: "1",
- });
- if (login.code != 200) throw new Error("login fail " + JSON.stringify(login));
- const token = login.message;
- const people = await listAll(token, PERSONNEL);
- const targets = people.filter((p) => isMockName(p.c_name || p.title));
- // 保留已像真人的(如 王忆),其余全改;若用户希望「都」改成更自然,对非 mock 也跳过
- console.log("personnel total=" + people.length + " rename=" + targets.length);
- const pool = buildNamePool(targets.length);
- const idToNew = new Map();
- const oldToNew = new Map();
- let ok = 0;
- let fail = 0;
- for (let i = 0; i < targets.length; i++) {
- const row = targets[i];
- const oldName = String(row.c_name || row.title || "").trim();
- const newName = pool[i];
- idToNew.set(String(row.id), newName);
- if (!oldToNew.has(oldName)) oldToNew.set(oldName, []);
- oldToNew.get(oldName).push(newName);
- const payee = String(row.c_payee_name || "").trim();
- const patch = {
- id: row.id,
- title: newName,
- c_name: newName,
- };
- if (!payee || payee === oldName || isMockName(payee)) {
- patch.c_payee_name = newName;
- }
- const res = await updateRow(token, PERSONNEL, patch);
- if (res.code == 200) {
- ok++;
- console.log("P", oldName, "->", newName, "id=" + row.id);
- } else {
- fail++;
- console.log("FAIL person", row.id, res.message || res.code);
- }
- }
- // 发放明细:按 personnelId 优先,否则按旧姓名匹配(同名多条时按出现顺序轮转)
- const details = await listAll(token, PAYMENT);
- const oldQueues = new Map();
- for (const [old, arr] of oldToNew) oldQueues.set(old, arr.slice());
- let dOk = 0;
- for (const row of details) {
- const pid = String(row.c_personnel_id || row.c_person_id || "");
- const oldName = String(row.c_name || row.c_personnel_name || "").trim();
- let newName = pid && idToNew.get(pid);
- if (!newName && oldName && isMockName(oldName)) {
- const q = oldQueues.get(oldName);
- if (q && q.length) newName = q.length === 1 ? q[0] : q.shift();
- else if (oldToNew.has(oldName)) newName = oldToNew.get(oldName)[0];
- }
- if (!newName) continue;
- const patch = { id: row.id, c_name: newName };
- if (row.title && isMockName(row.title)) patch.title = String(row.title).replace(oldName, newName);
- if (row.c_personnel_name && isMockName(row.c_personnel_name)) patch.c_personnel_name = newName;
- const res = await updateRow(token, PAYMENT, patch);
- if (res.code == 200) dOk++;
- }
- console.log("payment-detail renamed=" + dOk + "/" + details.length);
- // 告警
- const alerts = await listAll(token, ALERT);
- let aOk = 0;
- for (const row of alerts) {
- const pid = String(row.c_personnel_id || "");
- const oldName = String(row.c_personnel_name || "").trim();
- let newName = pid && idToNew.get(pid);
- if (!newName && oldName && isMockName(oldName) && oldToNew.has(oldName)) {
- newName = oldToNew.get(oldName)[0];
- }
- if (!newName) continue;
- const patch = { id: row.id, c_personnel_name: newName };
- if (row.title && isMockName(row.title)) {
- patch.title = String(row.title).split(oldName).join(newName);
- }
- const res = await updateRow(token, ALERT, patch);
- if (res.code == 200) aOk++;
- }
- console.log("alert renamed=" + aOk + "/" + alerts.length);
- console.log("DONE personnel ok=" + ok + " fail=" + fail);
- })().catch((e) => {
- console.error(e);
- process.exit(1);
- });
|