migrate-jebz-funeral-amount.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * 金额标准 sjnmtybt_jebz (modelId=1966) 增加 c_funeral_amount(丧葬金额),
  3. * 支撑「年度标准」一行双金额(土地 c_amount + 丧葬 c_funeral_amount)。
  4. */
  5. const http = require("http");
  6. const qs = require("querystring");
  7. const fs = require("fs");
  8. const path = require("path");
  9. const HOST = "121.43.55.7";
  10. const PORT = 2101;
  11. const MODEL_ID = 1966;
  12. function post(urlPath, fields, token, method) {
  13. return new Promise((resolve, reject) => {
  14. const body = qs.stringify(fields || {});
  15. const headers = {
  16. "Content-Type": "application/x-www-form-urlencoded",
  17. "Content-Length": Buffer.byteLength(body),
  18. };
  19. if (token) {
  20. headers.Token = token;
  21. headers.token = token;
  22. }
  23. const req = http.request(
  24. { hostname: HOST, port: PORT, path: urlPath, method: method || "POST", headers },
  25. (res) => {
  26. let data = "";
  27. res.on("data", (c) => (data += c));
  28. res.on("end", () => {
  29. try {
  30. resolve(JSON.parse(data));
  31. } catch (e) {
  32. resolve({ raw: data, status: res.statusCode });
  33. }
  34. });
  35. }
  36. );
  37. req.on("error", reject);
  38. req.write(body);
  39. req.end();
  40. });
  41. }
  42. function num(shortName, alias, must, seq) {
  43. return {
  44. alias,
  45. customType: "",
  46. describe: "number",
  47. frontType: "float_num",
  48. id: 6,
  49. must: !!must,
  50. name: "c_" + shortName,
  51. searchType: "1,3",
  52. sequence: seq || 5,
  53. showParam: "name,alias,desc,type,front_type,must,default_value",
  54. sortType: "1,2",
  55. type: "double",
  56. };
  57. }
  58. function normalizeFieldList(raw) {
  59. let fl = raw;
  60. if (typeof fl === "string") {
  61. try {
  62. fl = JSON.parse(fl);
  63. } catch (e) {
  64. return {};
  65. }
  66. }
  67. const out = {};
  68. Object.keys(fl || {}).forEach((k) => {
  69. let v = fl[k];
  70. if (typeof v === "string") {
  71. try {
  72. v = JSON.parse(v);
  73. } catch (e) {
  74. return;
  75. }
  76. }
  77. out[k] = v;
  78. });
  79. return out;
  80. }
  81. async function login() {
  82. const res = await post("/proxy_oauth/user/login", {
  83. userName: "user_liu",
  84. password: "WE176852439@lmx",
  85. clientId: "1",
  86. });
  87. if (res.code != 200) throw new Error("login failed: " + JSON.stringify(res));
  88. return res.message;
  89. }
  90. async function getModel(token, modelId) {
  91. const res = await post("/proxy_dms/model/getModelById", { modelId: String(modelId) }, token);
  92. if (res.code != 200) throw new Error("getModelById fail: " + JSON.stringify(res));
  93. return res.content;
  94. }
  95. async function main() {
  96. const token = await login();
  97. const model = await getModel(token, MODEL_ID);
  98. const fieldList = normalizeFieldList(model.fieldList);
  99. if (Object.keys(fieldList).length < 5) {
  100. throw new Error(
  101. "fieldList 异常(仅 " +
  102. Object.keys(fieldList).length +
  103. " 个字段),请先运行 restore-jebz-model-fields.js,避免再次覆盖模型。"
  104. );
  105. }
  106. const addfieldList = {};
  107. const shortName = "funeral_amount";
  108. const full = "c_" + shortName;
  109. if (!fieldList[full]) {
  110. const p = num(shortName, "丧葬金额", false, 14);
  111. fieldList[full] = p;
  112. addfieldList[shortName] = {
  113. alias: p.alias,
  114. type: p.type,
  115. frontType: p.frontType,
  116. must: p.must,
  117. name: shortName,
  118. describe: p.describe,
  119. searchType: p.searchType,
  120. sortType: p.sortType,
  121. sequence: p.sequence,
  122. showParam: p.showParam,
  123. customType: "",
  124. id: p.id,
  125. };
  126. }
  127. const payload = {
  128. modelId: String(MODEL_ID),
  129. fieldList: JSON.stringify(fieldList),
  130. addfieldList: JSON.stringify(addfieldList),
  131. delfieldList: JSON.stringify({}),
  132. updatefieldList: JSON.stringify({}),
  133. };
  134. const res = await post("/proxy_dms/model/updateFieldList", payload, token);
  135. const verify = await getModel(token, MODEL_ID);
  136. const fields = Object.keys(normalizeFieldList(verify.fieldList)).sort();
  137. const out = {
  138. update: { code: res.code, message: res.message || res.content, added: Object.keys(addfieldList) },
  139. fields,
  140. hasFuneralAmount: fields.includes("c_funeral_amount"),
  141. };
  142. const outPath = path.join(__dirname, "migrate-jebz-funeral-amount-result.json");
  143. fs.writeFileSync(outPath, JSON.stringify(out, null, 2), "utf8");
  144. console.log(JSON.stringify(out, null, 2));
  145. if (!out.hasFuneralAmount) process.exit(1);
  146. }
  147. main().catch((e) => {
  148. console.error(e);
  149. process.exit(1);
  150. });