base.js 24 KB

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