base.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. const items = [];
  160. itemsSrc.forEach(itemSrc => {
  161. const obj = extractFuc(itemSrc, field[0]);
  162. if (obj) {
  163. obj.children = extractItemsRecur(itemSrc, fields, extractFuc);
  164. items.push(obj);
  165. }
  166. })
  167. rst.push(...items);
  168. }
  169. }
  170. return rst;
  171. }
  172. const UTIL = Object.freeze({
  173. escapeXMLEntity,
  174. restoreXMLEntity,
  175. getValue,
  176. getBool,
  177. arrayValue,
  178. getFee,
  179. mergeFees,
  180. assignAttr,
  181. setTreeData,
  182. mergeDataRecur,
  183. getFlag,
  184. extractItemsRecur,
  185. });
  186. /**
  187. * 合并基本信息或工程特征
  188. * @param {Array} source - 提取的数据
  189. * @param {Array} target - 模板数据
  190. * @return {Array}
  191. */
  192. function mergeInfo(source, target) {
  193. source.forEach(item => mergeChild(item, target));
  194. return target;
  195. function mergeChild(item, target) {
  196. for (const child of target) {
  197. if (child.key === item.key) {
  198. child.value = item.value;
  199. return true;
  200. }
  201. if (child.items && child.items.length) {
  202. const rst = mergeChild(item, child.items);
  203. if (rst) {
  204. return true;
  205. }
  206. }
  207. }
  208. return false;
  209. }
  210. }
  211. const { fixedFlag, BillType } = window.commonConstants;
  212. // 标题类别 - flag 映射
  213. const titleTypeToFlag = {
  214. 1: fixedFlag.ONE_SEVEN_BILLS,
  215. 2: fixedFlag.PROVISIONAL_TOTAL,
  216. 3: fixedFlag.BILLS_TOTAL_WT_PROV,
  217. 4: fixedFlag.DAYWORK_LABOR,
  218. 5: fixedFlag.PROVISIONAL,
  219. 6: fixedFlag.TOTAL_COST,
  220. }
  221. /**
  222. * 将提取出来的清单合并进清单模板
  223. * @param {Array} source - 从xml提取出来的清单
  224. * @param {Array} target - 清单模板数据
  225. * @param {Object} parent - 匹配到模板清单的父清单
  226. * @param {Boolean} onlyImportMatchBills - 是否只导入文件中的清单,未匹配的模板清单不生成
  227. * @return {void}
  228. */
  229. function mergeBills(source, target, parent, matchedFlags) {
  230. source.forEach((bills, index) => {
  231. // 为了大项费用排序
  232. if (!parent) {
  233. bills.seq = index;
  234. }
  235. const simpleName = bills.name ? bills.name.replace(/\s/g, '') : '';
  236. const titleType = bills.titleType || '0';
  237. let matched;
  238. if (!parent) {
  239. if (titleTypeToFlag[titleType] === fixedFlag.ONE_SEVEN_BILLS || /100章.*700章|100章.*900章/.test(simpleName)) {
  240. matched = target.find(bills => getFlag(bills) === fixedFlag.ONE_SEVEN_BILLS);
  241. } else if (titleTypeToFlag[titleType] === fixedFlag.PROVISIONAL_TOTAL || /包含在清单合计中的材料、工程设备、专业工程暂估/.test(simpleName)) {
  242. matched = target.find(bills => getFlag(bills) === fixedFlag.PROVISIONAL_TOTAL);
  243. } else if (titleTypeToFlag[titleType] === fixedFlag.BILLS_TOTAL_WT_PROV || /清单合计减去材料、工程设备、专业工程暂估价/.test(simpleName)) {
  244. matched = target.find(bills => getFlag(bills) === fixedFlag.BILLS_TOTAL_WT_PROV);
  245. } else if (titleTypeToFlag[titleType] === fixedFlag.DAYWORK_LABOR || /计日工合计/.test(simpleName)) {
  246. matched = target.find(bills => getFlag(bills) === fixedFlag.DAYWORK_LABOR);
  247. } else if (titleTypeToFlag[titleType] === fixedFlag.PROVISIONAL || /暂列金额[((]不含/.test(simpleName)) {
  248. matched = target.find(bills => getFlag(bills) === fixedFlag.PROVISIONAL);
  249. } else if (titleTypeToFlag[titleType] === fixedFlag.TOTAL_COST || /报价/.test(simpleName)) {
  250. matched = target.find(bills => getFlag(bills) === fixedFlag.TOTAL_COST);
  251. }
  252. } else {
  253. const parentSimpleName = parent.name ? parent.name.replace(/\s/g, '') : '';
  254. if (/100章.*700章|100章.*900章/.test(parentSimpleName) && /100章总则/.test(simpleName)) {
  255. matched = target.find(bills => getFlag(bills) === fixedFlag.ONE_HUNDRED_BILLS);
  256. } else if (/计日工合计/.test(parentSimpleName) && /劳务/.test(simpleName)) {
  257. matched = target.find(bills => getFlag(bills) === fixedFlag.LABOUR_SERVICE);
  258. } else if (/计日工合计/.test(parentSimpleName) && /材料/.test(simpleName)) {
  259. matched = target.find(bills => getFlag(bills) === fixedFlag.MATERIAL);
  260. } else if (/计日工合计/.test(parentSimpleName) && /机械/.test(simpleName)) {
  261. matched = target.find(bills => getFlag(bills) === fixedFlag.CONSTRUCTION_MACHINE);
  262. }
  263. }
  264. if (matched) {
  265. matchedFlags.push(getFlag(matched));
  266. assignAttr(matched, bills);
  267. if (bills.children && bills.children.length) {
  268. mergeBills(bills.children, matched.children, matched, matchedFlags);
  269. }
  270. } else {
  271. target.push(bills);
  272. }
  273. });
  274. }
  275. /**
  276. * 处理清单
  277. * @param {Array} tenderBills - 从xml提取出来的清单
  278. * @param {Array} billsTarget - 拷贝一份的模板清单,用于合并提取清单
  279. * @param {Number} tenderID - 单位工程ID
  280. * @param {Boolean} onlyImportMatchBills - 是否只导入文件中的清单,未匹配的模板清单不生成
  281. * @return {Array}
  282. */
  283. function handleBills(tenderBills, billsTarget, tenderID, onlyImportMatchBills) {
  284. const rst = [];
  285. // 将提取的清单数据合并进清单模板数据
  286. const matchedFlags = [];
  287. mergeBills(tenderBills, billsTarget, null, matchedFlags);
  288. // 如果只导入文件中有的清单,除100-700/900章清单、报价清单外,其他未匹配到的固定清单模板,均不导入
  289. function removeNotMatchFlagBills(bills) {
  290. const rst = [];
  291. bills.forEach(item => {
  292. const flag = getFlag(item);
  293. if (!flag || [fixedFlag.ONE_SEVEN_BILLS, fixedFlag.TOTAL_COST].includes(flag) || matchedFlags.includes(flag)) {
  294. rst.push(item);
  295. }
  296. if (flag && ![fixedFlag.ONE_SEVEN_BILLS, fixedFlag.TOTAL_COST].includes(flag) && item.children && item.children.length) {
  297. item.children = removeNotMatchFlagBills(item.children);
  298. }
  299. });
  300. return rst;
  301. }
  302. if (onlyImportMatchBills) {
  303. billsTarget = removeNotMatchFlagBills(billsTarget)
  304. }
  305. // 大项费用按照清单标题排序
  306. billsTarget.sort((a, b) => a.seq - b.seq);
  307. // 给清单设置数据
  308. const rowCodeData = []; // 行号数据,用于转换行引用
  309. const toBeTransformBills = []; // 待转换的清单
  310. function setBills(bills, parentID) {
  311. bills.forEach((child, index) => {
  312. rst.push(child);
  313. child.projectID = tenderID;
  314. // 如果本身清单就有ID,那不用处理,可以减少处理清单模板的一些ID引用问题
  315. if (!child.ID) {
  316. child.ID = uuid.v1();
  317. }
  318. if (child.quantity) {
  319. child.quantityEXP = child.quantity;
  320. }
  321. child.ParentID = parentID;
  322. child.type = parentID === -1 ? BillType.DXFY : BillType.BILL;
  323. child.NextSiblingID = -1;
  324. const preChild = bills[index - 1];
  325. if (preChild) {
  326. preChild.NextSiblingID = child.ID;
  327. }
  328. if (child.rowCode) {
  329. const regStr = /{[^{}]+}/.test(child.rowCode) ? child.rowCode : `\\b${child.rowCode}\\b`;
  330. rowCodeData.push({ reg: new RegExp(regStr, 'g'), ID: child.ID, rowCode: child.rowCode });
  331. }
  332. if (child.tempCalcBase) {
  333. toBeTransformBills.push(child);
  334. }
  335. if (child.children && child.children.length) {
  336. setBills(child.children, child.ID);
  337. }
  338. });
  339. }
  340. setBills(billsTarget, -1);
  341. // 转换计算基数
  342. toBeTransformBills.forEach(bills => {
  343. rowCodeData.forEach(({ reg, ID, rowCode }) => {
  344. const rowCodeReg = new RegExp(`{${rowCode}}`);
  345. // 替换行号
  346. if (rowCodeReg.test(bills.tempCalcBase)) {
  347. bills.tempCalcBase = bills.tempCalcBase.replace(rowCodeReg, `@${ID}`);
  348. } else {
  349. bills.tempCalcBase = bills.tempCalcBase.replace(reg, `@${ID}`);
  350. }
  351. // 替换基数,防止其他公司的软件导出没有{}
  352. bills.tempCalcBase = bills.tempCalcBase.replace(/.?专项暂定合计.?/g, '{专项暂定合计}');
  353. bills.tempCalcBase = bills.tempCalcBase.replace(/.?各章清单合计.?/g, '{各章清单合计}');
  354. });
  355. /* 检查基数有效性,无效则使用模板的基数 */
  356. // 消除ID引用对基数分割的影响
  357. const IDs = bills.tempCalcBase.match(/@[\da-zA-Z-]{36}/g) || [];
  358. const bases = [...IDs];
  359. const str = bills.tempCalcBase.replace(/@[\da-zA-Z-]{36}/g, '');
  360. const otherBases = str.split(/[+-/*]/).filter(item => !!item);
  361. bases.push(...otherBases);
  362. // 判定基数有效性
  363. const isValid = bases.every(base => {
  364. if (base === '{专项暂定合计}' || base === '{各章清单合计}') {
  365. return true;
  366. }
  367. if (/^(\d+(\.\d+)?)%?$/.test(base)) { // 数值+%(可有可无)
  368. return true;
  369. }
  370. if (/[\da-zA-Z-]{36}/.test(base)) { // ID引用
  371. return true;
  372. }
  373. return false;
  374. });
  375. if (isValid) {
  376. bills.calcBase = bills.tempCalcBase;
  377. }
  378. });
  379. rst.forEach(bills => delete bills.children);
  380. return rst;
  381. }
  382. function getTemplateBillsTarget(templateBills) {
  383. const templateTarget = _.cloneDeep(templateBills);
  384. const ungroupedData = [];
  385. function getBillsFromChildren(billsData) {
  386. for (const bills of billsData) {
  387. ungroupedData.push(bills);
  388. if (bills.children && bills.children.length) {
  389. getBillsFromChildren(bills.children);
  390. }
  391. }
  392. }
  393. getBillsFromChildren(templateTarget);
  394. BILLS_UTIL.resetTreeData(ungroupedData, uuid.v1, true);
  395. return templateTarget;
  396. }
  397. // 处理单位工程数据
  398. function handleTenderData(tenders, templateData, rationValuationData, engineeringLib, areaKey, onlyImportMatchBills) {
  399. tenders.forEach((tender, index) => {
  400. tender.compilation = compilationData._id;
  401. tender.userID = userID;
  402. tender.ID = templateData.projectBeginID + index + 1;
  403. tender.ParentID = templateData.projectBeginID;
  404. tender.NextSiblingID = index === tenders.length - 1 ? -1 : templateData.projectBeginID + index + 2;
  405. tender.projType = projectType.tender;
  406. const featureTarget = _.cloneDeep(templateData.feature); // 必须拷贝出一份新数据,否则会被下一个单位工程覆盖
  407. if (!engineeringLib) {
  408. throw '不存在可用工程专业。';
  409. }
  410. const taxData = engineeringLib.lib.tax_group[0];
  411. const featureSource = [
  412. ...(tender.feature || []),
  413. { key: 'valuationType', value: '工程量清单' }, // 导入的时候以下项不一定有数据,但是需要自动生成
  414. { key: 'feeStandard', value: engineeringLib.lib.feeName },
  415. ];
  416. const needEngineering = featureTarget && featureTarget.find(item => item.key === 'engineering');
  417. if (needEngineering) {
  418. featureSource.push({ key: 'engineering', value: engineeringLib.lib.name });
  419. }
  420. tender.property = {
  421. areaKey,
  422. rootProjectID: tender.ParentID,
  423. region: '全省',
  424. engineering_id: engineeringLib.engineering_id,
  425. engineeringName: engineeringLib.lib.name,
  426. feeStandardName: engineeringLib.lib.feeName,
  427. engineering: engineeringLib.engineering,
  428. isInstall: engineeringLib.lib.isInstall,
  429. projectEngineering: engineeringLib.lib.projectEngineering,
  430. valuation: rationValuationData.id,
  431. valuationName: rationValuationData.name,
  432. valuationType: commonConstants.ValuationType.BOQ, // 必为工程量清单
  433. boqType: commonConstants.BOQType.BID_SUBMISSION, // 导入后必为投标
  434. taxType: taxData.taxType,
  435. projectFeature: mergeInfo(featureSource, featureTarget),
  436. featureLibID: engineeringLib.lib.feature_lib[0] && engineeringLib.lib.feature_lib[0].id || '',
  437. calcProgram: { name: taxData.program_lib.name, id: taxData.program_lib.id },
  438. colLibID: taxData.col_lib.id,
  439. templateLibID: taxData.template_lib.id,
  440. unitPriceFile: { name: tender.name, id: templateData.unitPriceFileBeginID + index }, // 新建单价文件
  441. feeFile: { name: tender.name, id: `newFeeRate@@${taxData.fee_lib.id}` } // 新建费率文件
  442. };
  443. delete tender.feature;
  444. const tenderDataBills = getTemplateBillsTarget(templateData.bills);
  445. tender.bills = handleBills(tender.bills, tenderDataBills, tender.ID, onlyImportMatchBills); // 必须要拷贝一份,否则多单位工程情况下,前单位工程的清单数据会被后单位工程的覆盖
  446. // 给暂估材料和评标材料设置项目数据
  447. const setGLJRefFunc = glj => {
  448. glj.ID = uuid.v1();
  449. glj.projectID = tender.ID;
  450. }
  451. if (tender.evaluationList && tender.evaluationList.length) {
  452. tender.evaluationList.forEach(setGLJRefFunc);
  453. }
  454. if (tender.bidEvaluationList && tender.bidEvaluationList.length) {
  455. tender.bidEvaluationList.forEach(setGLJRefFunc);
  456. }
  457. });
  458. }
  459. /**
  460. * 将接口中提取出来数据转换成可入库的有效数据
  461. * 因为无法保证这一套逻辑能不能兼容以后的所有接口,因此提取数据与标准数据模板的合并放在前端进行。
  462. * 当统一逻辑无法满足某一接口时,接口可以根据标准模板数据自行进行相关处理。
  463. * @param {Object} importData - 各接口从xml提取出来的数据
  464. * @param {String} areaKey - 接口地区
  465. * @param {Boolean} onlyImportMatchBills - 是否只导入文件中的清单,未匹配的模板清单不生成
  466. * @return {Promise<Object>}
  467. */
  468. async function handleImportData(importData, areaKey, onlyImportMatchBills) {
  469. const valuationID = compilationData.ration_valuation[0].id;
  470. if (!Array.isArray(importData.tenders) && !importData.tenders.length) {
  471. throw '导入的文件中不存在有效的标段数据。';
  472. }
  473. const projectCount = 1 + importData.tenders.length;
  474. // const feeName = compilationData.name === '安徽养护(2018)' ? '安徽养护' : '公路工程';
  475. // 一些接口需要根据导入文件,匹配工程专业库
  476. const rationValuationData = rationValuation && JSON.parse(rationValuation)[0]; // 只有工程量清单才能导入接口
  477. if (!rationValuationData) {
  478. throw '无法获取工程量清单计价数据';
  479. }
  480. const engineeringList = (rationValuationData.engineering_list || []).filter(item => item.lib.visible);
  481. let engineeringLib = engineeringList[0];
  482. if (importData.engineeringName && importData.feeName) {
  483. const matchLibs = engineeringList.filter(item => item.lib && item.lib.name === importData.engineeringName);
  484. engineeringLib = matchLibs.find(item => item.lib.feeName === importData.feeName) || matchLibs[0] || engineeringList[0];
  485. }
  486. const engineeringID = engineeringLib.engineering_id || null;
  487. const templateData = await ajaxPost('/pm/api/getImportTemplateData', { user_id: userID, valuationID, engineeringID, projectCount });
  488. if (!templateData) {
  489. throw '无法获取有效模板数据。';
  490. }
  491. console.log(templateData);
  492. // 处理建设项目数据
  493. // 确定建设项目的名称(不允许重复)
  494. const sameDepthProjs = getProjs(projTreeObj.tree.selected);
  495. const matchedProject = sameDepthProjs.find(node => node.data.name === importData.name);
  496. if (matchedProject) {
  497. importData.name += `(${moment(Date.now()).format('YYYY-MM-DD HH:mm:ss')})`;
  498. }
  499. importData.compilation = compilationData._id;
  500. importData.userID = userID;
  501. importData.ID = templateData.projectBeginID;
  502. const { parentProjectID, preProjectID, nextProjectID } = projTreeObj.getRelProjectID(projTreeObj.tree.selected);
  503. importData.ParentID = parentProjectID;
  504. importData.preID = preProjectID;
  505. importData.NextSiblingID = nextProjectID;
  506. importData.projType = projectType.project;
  507. importData.property = {
  508. valuationType: commonConstants.ValuationType.BOQ, // 必为工程量清单
  509. boqType: commonConstants.BOQType.BID_SUBMISSION, // 导入后必为投标
  510. basicInformation: mergeInfo(importData.info, templateData.basicInfo) // 将提取的基本信息数据与标准基本信息数据进行合并(目前只赋值,没有匹配到的不追加)
  511. };
  512. delete importData.info;
  513. // 处理单位工程数据
  514. handleTenderData(importData.tenders, templateData, rationValuationData, engineeringLib, areaKey, onlyImportMatchBills);
  515. console.log(importData);
  516. }
  517. /*
  518. * 读取文件转换为utf-8编码的字符串
  519. * @param {Blob} file
  520. * @return {Promise}
  521. * */
  522. function readAsTextSync(file) {
  523. return new Promise((resolve, reject) => {
  524. const fr = new FileReader();
  525. fr.readAsText(file); // 默认utf-8,如果出现乱码,得看导入文件是什么编码
  526. fr.onload = function () {
  527. resolve(this.result);
  528. };
  529. fr.onerror = function () {
  530. reject('读取文件失败,请重试。');
  531. }
  532. });
  533. }
  534. /**
  535. *
  536. * @param {Function} entryFunc - 各导入接口提取导入数据方法
  537. * @param {File} file - 导入的文件
  538. * @param {String} areaKey - 地区标识,如:'安徽@马鞍山'
  539. * @param {Boolean} escape - 是否需要避免xml中的实体字符转换
  540. * @param {Boolean} onlyImportMatchBills - 是否只导入文件中的清单,未匹配的模板清单不生成
  541. * @return {Promise<Object>}
  542. */
  543. async function extractImportData(entryFunc, file, areaKey, escape = false, onlyImportMatchBills = false) {
  544. // 将二进制文件转换成字符串
  545. let xmlStr = await readAsTextSync(file);
  546. if (escape) {
  547. // x2js的str to json的实现方式基于DOMParser,DOMParser会自动将一些实体字符进行转换,比如 “&lt; to <”。如果不想进行自动转换,需要进行处理。
  548. xmlStr = escapeXMLEntity(xmlStr);
  549. }
  550. // 将xml格式良好的字符串转换成对象
  551. const x2js = new X2JS();
  552. let xmlObj = x2js.xml_str2json(xmlStr);
  553. xmlObj = JSON.parse(restoreXMLEntity(JSON.stringify(xmlObj)));
  554. console.log(xmlObj);
  555. if (!xmlObj) {
  556. throw '无有效数据。';
  557. }
  558. const importData = await entryFunc(areaKey, xmlObj);
  559. console.log(areaKey);
  560. await handleImportData(importData, areaKey, onlyImportMatchBills);
  561. return importData;
  562. }
  563. return {
  564. UTIL,
  565. extractImportData,
  566. }
  567. })();