base.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * @Descripttion: 导入通用代码
  3. * @Author: vian
  4. * @Date: 2020-09-09 10:45:54
  5. */
  6. const INTERFACE_EXPORT_BASE = (() => {
  7. // xml字符实体
  8. const XMLEntity = {
  9. ' ': 'escape{space}',
  10. ' ': 'escape{simpleSpace}',
  11. '	': 'escape{tab}',
  12. '	': 'escape{simpleTab}',
  13. '
': 'escape{return}',
  14. '
': 'escape{simpleReturn}',
  15. '&#000A;': 'escape{newLine}',
  16. '
': 'escape{simpleNewLine}',
  17. '<': 'escape{less}',
  18. '>': 'escape{greater}',
  19. '&': 'escape{and}',
  20. '"': 'escape{quot}',
  21. ''': 'escape{apos}'
  22. };
  23. // 避免字符实体进行转义。原文本中含有xml字符实体,转换为其他字符。
  24. function escapeXMLEntity(str) {
  25. for (const [key, value] of Object.entries(XMLEntity)) {
  26. str = str.replace(new RegExp(key, 'g'), value);
  27. }
  28. return str;
  29. }
  30. // 将文本还原为字符实体
  31. function restoreXMLEntity(str) {
  32. for (const [key, value] of Object.entries(XMLEntity)) {
  33. str = str.replace(new RegExp(value, 'g'), key);
  34. }
  35. return str;
  36. }
  37. /*
  38. * 根据字段数组获得所要字段的值 eg: 要获取标段下的单项工程: ['标段', '单项工程'];
  39. * 属性需要加前缀:“_”
  40. * 节点的不需要加前缀
  41. * @param {Object}source 源数据
  42. * {Array}fields 字段数组
  43. * @return {String}
  44. * @example getValue(source, ['标段', '_文件类型'])
  45. * */
  46. function getValue(source, fields) {
  47. let cur = source;
  48. for (const field of fields) {
  49. if (!cur[field]) {
  50. return '';
  51. }
  52. cur = cur[field];
  53. }
  54. return cur || '';
  55. }
  56. // 获取布尔型的数据
  57. function getBool(source, fields) {
  58. return getValue(source, fields) === 'true' ? true : false;
  59. }
  60. // 获取数据类型
  61. function _plainType(v) {
  62. return Object.prototype.toString.call(v).slice(8, -1);
  63. }
  64. /*
  65. * 获取某字段的值,强制返回数组,防止一些错误。如果期待返回数组,可以用此方法。
  66. * @param {Object}source 数据源
  67. * {Array}fields 取的字段
  68. * @return {Array}
  69. * @example arrayValue(source, ['标段', '单项工程'])
  70. * */
  71. function arrayValue(source, fields) {
  72. let target = getValue(source, fields);
  73. if (_plainType(target) === 'Object') {
  74. target = [target];
  75. } else if (_plainType(target) !== 'Array') {
  76. target = []
  77. }
  78. return target;
  79. }
  80. // 获取费用
  81. function getFee(fees, fields) {
  82. if (!Array.isArray(fees) || !fees.length) {
  83. return '0';
  84. }
  85. const feeData = fees.find(fee => fee.fieldName === fields[0]);
  86. return feeData && feeData[fields[1]] || '0';
  87. }
  88. // 合并价格
  89. function mergeFees(feesA, feesB) {
  90. if (!feesA) {
  91. return feesB;
  92. }
  93. if (!feesB) {
  94. return [];
  95. }
  96. feesB.forEach(feeB => {
  97. const sameKindFee = feesA.find(feeA => feeA.fieldName === feeB.fieldName);
  98. if (sameKindFee) {
  99. Object.assign(sameKindFee, feeB);
  100. } else {
  101. feesA.push(feeB);
  102. }
  103. });
  104. return feesA;
  105. }
  106. // 将A对象的属性赋值到B对象上
  107. function assignAttr(target, source, attrs) {
  108. if (!source || !target) {
  109. return;
  110. }
  111. const sourceAttrs = attrs || Object.keys(source);
  112. for (const attr of sourceAttrs) {
  113. // 如果值是undefined,则不进行赋值覆盖处理
  114. if (attr === 'children' || source[attr] === undefined) {
  115. continue;
  116. }
  117. target[attr] = attr === 'fees'
  118. ? mergeFees(target[attr], source[attr]) // 如果是价格,不能简单地覆盖,要合并两个对象的价格
  119. : source[attr];
  120. }
  121. }
  122. // 获取固定ID
  123. function getFlag(data) {
  124. return data.flags && data.flags[0] && data.flags[0].flag || 0;
  125. }
  126. // 设置成树结构数据
  127. function setTreeData(data, parent, next) {
  128. const defalutID = -1;
  129. data.ID = uuid.v1();
  130. data.ParentID = parent && parent.ID || defalutID;
  131. data.NextSiblingID = next && next.ID || defalutID;
  132. }
  133. // 递归设置树结构数据,并返回设置好的数据,递归items数组
  134. function mergeDataRecur(parent, items) {
  135. const rst = [];
  136. for (let i = 0; i < items.length; i++) {
  137. const cur = items[i];
  138. const next = items[i + 1];
  139. setTreeData(cur, parent, next);
  140. rst.push(cur);
  141. if (cur.items && cur.items.length) {
  142. rst.push(...mergeDataRecur(cur, cur.items));
  143. }
  144. }
  145. return rst;
  146. }
  147. // 递归获取相关数据,(同层可以出现不同节点)
  148. // fields内字段的顺序即决定了提取数据类型的顺序,如fields = [['gruop'], ['item']],则提取的数据同层中group数据在item数据之前
  149. function extractItemsRecur(src, fields, extractFuc) {
  150. const rst = [];
  151. for (const field of fields) {
  152. const itemsSrc = arrayValue(src, field);
  153. if (itemsSrc.length) {
  154. const items = itemsSrc.map(itemSrc => {
  155. const obj = extractFuc(itemSrc, field[0]);
  156. obj.children = extractItemsRecur(itemSrc, fields, extractFuc);
  157. return obj;
  158. });
  159. rst.push(...items);
  160. }
  161. }
  162. return rst;
  163. }
  164. const UTIL = Object.freeze({
  165. escapeXMLEntity,
  166. restoreXMLEntity,
  167. getValue,
  168. getBool,
  169. arrayValue,
  170. getFee,
  171. mergeFees,
  172. assignAttr,
  173. setTreeData,
  174. mergeDataRecur,
  175. getFlag,
  176. extractItemsRecur,
  177. });
  178. /**
  179. * 合并基本信息或工程特征
  180. * @param {Array} source - 提取的数据
  181. * @param {Array} target - 模板数据
  182. * @return {Array}
  183. */
  184. function mergeInfo(source, target) {
  185. source.forEach(item => mergeChild(item, target));
  186. return target;
  187. function mergeChild(item, target) {
  188. for (const child of target) {
  189. if (child.key === item.key) {
  190. child.value = item.value;
  191. return true;
  192. }
  193. if (child.items && child.items.length) {
  194. const rst = mergeChild(item, child.items);
  195. if (rst) {
  196. return true;
  197. }
  198. }
  199. }
  200. return false;
  201. }
  202. }
  203. const { fixedFlag, BillType } = window.commonConstants;
  204. /**
  205. * 将提取出来的清单合并进清单模板
  206. * @param {Array} source - 从xml提取出来的清单
  207. * @param {Array} target - 清单模板数据
  208. * @param {Object} parent - 匹配到模板清单的父清单
  209. * @return {void}
  210. */
  211. function mergeBills(source, target, parent) {
  212. source.forEach(bills => {
  213. const simpleName = bills.name ? bills.name.replace(/\s/g, '') : '';
  214. let matched;
  215. if (!parent) {
  216. if (/100章至第700章|100章至700章/.test(simpleName)) {
  217. matched = target.find(bills => getFlag(bills) === fixedFlag.ONE_SEVEN_BILLS);
  218. } else if (/包含在清单合计中的材料、工程设备、专业工程暂估/.test(simpleName)) {
  219. matched = target.find(bills => getFlag(bills) === fixedFlag.PROVISIONAL_TOTAL);
  220. } else if (/清单合计减去材料、工程设备、专业工程暂估价/.test(simpleName)) {
  221. matched = target.find(bills => getFlag(bills) === fixedFlag.BILLS_TOTAL_WT_PROV);
  222. } else if (/计日工合计/.test(simpleName)) {
  223. matched = target.find(bills => getFlag(bills) === fixedFlag.DAYWORK_LABOR);
  224. } else if (/暂列金额[((]不含/.test(simpleName)) {
  225. matched = target.find(bills => getFlag(bills) === fixedFlag.PROVISIONAL);
  226. } else if (/报价/.test(simpleName)) {
  227. matched = target.find(bills => getFlag(bills) === fixedFlag.TOTAL_COST);
  228. }
  229. } else {
  230. const parentSimpleName = parent.name ? parent.name.replace(/\s/g, '') : '';
  231. if (/100章至第700章|100章至700章/.test(parentSimpleName) && /100章总则/.test(simpleName)) {
  232. matched = target.find(bills => getFlag(bills) === fixedFlag.ONE_HUNDRED_BILLS);
  233. } else if (/计日工合计/.test(parentSimpleName) && /劳务/.test(simpleName)) {
  234. matched = target.find(bills => getFlag(bills) === fixedFlag.LABOUR_SERVICE);
  235. } else if (/计日工合计/.test(parentSimpleName) && /材料/.test(simpleName)) {
  236. matched = target.find(bills => getFlag(bills) === fixedFlag.MATERIAL);
  237. } else if (/计日工合计/.test(parentSimpleName) && /机械/.test(simpleName)) {
  238. matched = target.find(bills => getFlag(bills) === fixedFlag.CONSTRUCTION_MACHINE);
  239. }
  240. }
  241. if (matched) {
  242. assignAttr(matched, bills);
  243. if (bills.children && bills.children.length) {
  244. mergeBills(bills.children, matched.children, matched);
  245. }
  246. } else {
  247. target.push(bills);
  248. }
  249. });
  250. }
  251. /**
  252. * 处理清单
  253. * @param {Array} tenderBills - 从xml提取出来的清单
  254. * @param {Array} billsTarget - 拷贝一份的模板清单,用于合并提取清单
  255. * @param {Number} tenderID - 单位工程ID
  256. * @return {Array}
  257. */
  258. function handleBills(tenderBills, billsTarget, tenderID) {
  259. const rst = [];
  260. // 将提取的清单数据合并进清单模板数据
  261. mergeBills(tenderBills, billsTarget, null);
  262. // 给清单设置数据
  263. const rowCodeData = []; // 行号数据,用于转换行引用
  264. const toBeTransformBills = []; // 待转换的清单
  265. function setBills(bills, parentID) {
  266. bills.forEach((child, index) => {
  267. rst.push(child);
  268. child.projectID = tenderID;
  269. // 如果本身清单就有ID,那不用处理,可以减少处理清单模板的一些ID引用问题
  270. if (!child.ID) {
  271. child.ID = uuid.v1();
  272. }
  273. child.ParentID = parentID;
  274. child.type = parentID === -1 ? BillType.DXFY : BillType.BILL;
  275. child.NextSiblingID = -1;
  276. const preChild = bills[index - 1];
  277. if (preChild) {
  278. preChild.NextSiblingID = child.ID;
  279. }
  280. if (child.rowCode) {
  281. rowCodeData.push({ reg: new RegExp(`\\b${child.rowCode}\\b`, 'g'), ID: child.ID });
  282. }
  283. if (child.calcBase) {
  284. toBeTransformBills.push(child);
  285. }
  286. if (child.children && child.children.length) {
  287. setBills(child.children, child.ID);
  288. }
  289. });
  290. }
  291. setBills(billsTarget, -1);
  292. // 转换计算基数,将行引用转换为ID引用
  293. toBeTransformBills.forEach(bills => {
  294. rowCodeData.forEach(({ reg, ID }) => {
  295. bills.calcBase = bills.calcBase.replace(reg, `@${ID}`);
  296. });
  297. });
  298. rst.forEach(bills => delete bills.children);
  299. return rst;
  300. }
  301. // 处理单位工程数据
  302. function handleTenderData(tenders, templateData) {
  303. tenders.forEach((tender, index) => {
  304. tender.compilation = compilationData._id;
  305. tender.userID = userID;
  306. tender.ID = templateData.projectBeginID + index + 1;
  307. tender.ParentID = templateData.projectBeginID;
  308. tender.NextSiblingID = index === tenders.length - 1 ? -1 : templateData.projectBeginID + index + 2;
  309. tender.projType = projectType.tender;
  310. const featureTarget = _.cloneDeep(templateData.feature); // 必须拷贝出一份新数据,否则会被下一个单位工程覆盖
  311. const rationValuationData = JSON.parse(rationValuation)[0];
  312. const engineeringList = rationValuationData.engineering_list;
  313. const engineeringLib = engineeringList.find(item => item.lib.visible);
  314. if (!engineeringLib) {
  315. throw '不存在可用工程专业。';
  316. }
  317. const taxData = engineeringLib.lib.tax_group[0];
  318. const featureSource = [
  319. ...tender.feature,
  320. { key: 'valuationType', value: '工程量清单' }, // 导入的时候以下项不一定有数据,但是需要自动生成
  321. { key: 'feeStandard', value: engineeringLib.lib.feeName },
  322. ];
  323. tender.property = {
  324. rootProjectID: tender.ParentID,
  325. region: '全省',
  326. engineering_id: engineeringLib.engineering_id,
  327. engineeringName: engineeringLib.lib.name,
  328. feeStandardName: engineeringLib.lib.feeName,
  329. engineering: engineeringLib.engineering,
  330. isInstall: engineeringLib.lib.isInstall,
  331. projectEngineering: engineeringLib.lib.projectEngineering,
  332. valuation: rationValuationData.id,
  333. valuationName: rationValuationData.name,
  334. valuationType: commonConstants.ValuationType.BOQ, // 必为工程量清单
  335. boqType: commonConstants.BOQType.BID_SUBMISSION, // 导入后必为投标
  336. taxType: taxData.taxType,
  337. projectFeature: mergeInfo(featureSource, featureTarget),
  338. featureLibID: engineeringLib.lib.feature_lib[0] && engineeringLib.lib.feature_lib[0].id || '',
  339. calcProgram: { name: taxData.program_lib.name, id: taxData.program_lib.id },
  340. colLibID: taxData.col_lib.id,
  341. templateLibID: taxData.template_lib.id,
  342. unitPriceFile: { name: tender.name, id: templateData.unitPriceFileBeginID + index }, // 新建单价文件
  343. feeFile: { name: tender.name, id: `newFeeRate@@${taxData.fee_lib.id}` } // 新建费率文件
  344. };
  345. delete tender.feature;
  346. tender.bills = handleBills(tender.bills, _.cloneDeep(templateData.bills), tender.ID,); // 必须要拷贝一份,否则多单位工程情况下,前单位工程的清单数据会被后单位工程的覆盖
  347. });
  348. }
  349. /**
  350. * 将接口中提取出来数据转换成可入库的有效数据
  351. * 因为无法保证这一套逻辑能不能兼容以后的所有接口,因此提取数据与标准数据模板的合并放在前端进行。
  352. * 当统一逻辑无法满足某一接口时,接口可以根据标准模板数据自行进行相关处理。
  353. * @param {Object} importData - 各接口从xml提取出来的数据
  354. * @return {Promise<Object>}
  355. */
  356. async function handleImportData(importData) {
  357. const valuationID = compilationData.ration_valuation[0].id;
  358. if (!Array.isArray(importData.tenders) && !importData.tenders.length) {
  359. throw '导入的文件中不存在有效的标段数据。';
  360. }
  361. const projectCount = 1 + importData.tenders.length;
  362. const templateData = await ajaxPost('/pm/api/getImportTemplateData', { user_id: userID, valuationID, projectCount });
  363. if (!templateData) {
  364. throw '无法获取有效模板数据。';
  365. }
  366. console.log(templateData);
  367. // 处理建设项目数据
  368. // 确定建设项目的名称(不允许重复)
  369. const sameDepthProjs = getProjs(projTreeObj.tree.selected);
  370. const matchedProject = sameDepthProjs.find(node => node.data.name === importData.name);
  371. if (matchedProject) {
  372. importData.name += `(${moment(Date.now()).format('YYYY-MM-DD HH:mm:ss')})`;
  373. }
  374. importData.compilation = compilationData._id;
  375. importData.userID = userID;
  376. importData.ID = templateData.projectBeginID;
  377. const { parentProjectID, preProjectID, nextProjectID } = projTreeObj.getRelProjectID(projTreeObj.tree.selected);
  378. importData.ParentID = parentProjectID;
  379. importData.preID = preProjectID;
  380. importData.NextSiblingID = nextProjectID;
  381. importData.projType = projectType.project;
  382. importData.property = {
  383. valuationType: commonConstants.ValuationType.BOQ, // 必为工程量清单
  384. boqType: commonConstants.BOQType.BID_SUBMISSION, // 导入后必为投标
  385. basicInformation: mergeInfo(importData.info, templateData.basicInfo) // 将提取的基本信息数据与标准基本信息数据进行合并(目前只赋值,没有匹配到的不追加)
  386. };
  387. delete importData.info;
  388. // 处理单位工程数据
  389. handleTenderData(importData.tenders, templateData);
  390. console.log(importData);
  391. }
  392. /*
  393. * 读取文件转换为utf-8编码的字符串
  394. * @param {Blob} file
  395. * @return {Promise}
  396. * */
  397. function readAsTextSync(file) {
  398. return new Promise((resolve, reject) => {
  399. const fr = new FileReader();
  400. fr.readAsText(file); // 默认utf-8,如果出现乱码,得看导入文件是什么编码
  401. fr.onload = function () {
  402. resolve(this.result);
  403. };
  404. fr.onerror = function () {
  405. reject('读取文件失败,请重试。');
  406. }
  407. });
  408. }
  409. /**
  410. *
  411. * @param {Function} entryFunc - 各导入接口提取导入数据方法
  412. * @param {File} file - 导入的文件
  413. * @param {String} areaKey - 地区标识,如:'安徽@马鞍山'
  414. * @param {Boolean} escape - 是否需要避免xml中的实体字符转换
  415. * @return {Promise<Object>}
  416. */
  417. async function extractImportData(entryFunc, file, areaKey, escape = false) {
  418. // 将二进制文件转换成字符串
  419. let xmlStr = await readAsTextSync(file);
  420. if (escape) {
  421. // x2js的str to json的实现方式基于DOMParser,DOMParser会自动将一些实体字符进行转换,比如 “&lt; to <”。如果不想进行自动转换,需要进行处理。
  422. xmlStr = escapeXMLEntity(xmlStr);
  423. }
  424. // 将xml格式良好的字符串转换成对象
  425. const x2js = new X2JS();
  426. let xmlObj = x2js.xml_str2json(xmlStr);
  427. xmlObj = JSON.parse(restoreXMLEntity(JSON.stringify(xmlObj)));
  428. console.log(xmlObj);
  429. if (!xmlObj) {
  430. throw '无有效数据。';
  431. }
  432. const importData = await entryFunc(areaKey, xmlObj);
  433. await handleImportData(importData);
  434. return importData;
  435. }
  436. return {
  437. UTIL,
  438. extractImportData,
  439. }
  440. })();