exportStdInterfaceBase.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2019/6/20
  7. * @version
  8. */
  9. const XML_EXPORT_BASE = (() => {
  10. // 自检提示的开始记号,区分提示属于哪一部分的类型(eg: 招标、控制价),便于后续做提示的合并去重
  11. const HINT_START = '--start--';
  12. // 属性类型
  13. const TYPE = {
  14. DATE: 1, //日期类型YYYY-MM-DD
  15. DATE_TIME: 2, //日期类型YYY-MM-DDTHH:mm:ss
  16. INT: 3, //整数类型
  17. DECIMAL: 4, //数值类型,不限制小数位数
  18. NUM2: 5, //数值类型2:最多两位小数
  19. BOOL: 6 //布尔型
  20. };
  21. // 需要特殊处理的属性类型默认空值(当一个值为undefined、null的时候,默认给赋什么值)
  22. const DEFAULT_VALUE = {
  23. [TYPE.INT]: '0',
  24. [TYPE.DECIMAL]: '0',
  25. [TYPE.NUM2]: '0',
  26. [TYPE.BOOL]: 'false'
  27. };
  28. // 空白字符处理
  29. const WHITE_SPACE = {
  30. COLLAPSE: 1 //移除所有空白字符(换行、回车、空格以及制表符会被替换为空格,开头和结尾的空格会被移除,而多个连续的空格会被缩减为一个单一的空格)
  31. };
  32. // 计税方法
  33. const TAX_TYPE = {
  34. '1': '一般计税法',
  35. '2': '简易计税法',
  36. };
  37. // 承包人材料调整类型
  38. const ADJUST_TYPE = {
  39. info: 'priceInfo', // 造价信息差额调整法
  40. coe: 'priceCoe' // 价格指数调整法
  41. };
  42. // 加载数据间隔,减少服务器压力
  43. const TIMEOUT_TIME = 200;
  44. // 导出粒度
  45. const GRANULARITY = {
  46. PROJECT: 1, //导出建设项目
  47. ENGINEERING: 2, //导出单项工程
  48. TENDER: 3 //导出单位工程
  49. };
  50. // 导出的文件类型选项
  51. const EXPORT_KIND = {
  52. Tender: 1, //投标
  53. Bid: 2, //招标
  54. Control: 3 //控制价
  55. };
  56. // 配置项
  57. const CONFIG = Object.freeze({
  58. HINT_START,
  59. TYPE,
  60. WHITE_SPACE,
  61. TAX_TYPE,
  62. ADJUST_TYPE,
  63. TIMEOUT_TIME,
  64. GRANULARITY,
  65. EXPORT_KIND
  66. });
  67. // 缓存项 不需要的时候需要清空
  68. let _cache = {
  69. // 失败列表
  70. failList: [],
  71. // 项目数据(不包含详细数据,项目管理数据)
  72. projectData: {},
  73. // 当前导出类型,默认投标
  74. exportKind: EXPORT_KIND.Tender,
  75. // 记录拉取的单位工程项目详细数据,导出的时候,可能会导出多个文件,只有导出第一个文件的时候需要请求数据
  76. tenderDetailMap: {}
  77. };
  78. // 返回缓存项
  79. function getItem(key) {
  80. return _cache[key] || null;
  81. }
  82. // 设置缓存项
  83. function setItem(key, value) {
  84. // 与原数据是同类型的数据才可设置成功
  85. if (_cache[key] &&
  86. Object.prototype.toString.call(_cache[key]) ===
  87. Object.prototype.toString.call(value)) {
  88. _cache[key] = value;
  89. }
  90. }
  91. // 清空缓存项
  92. function clear() {
  93. _cache.failList = [];
  94. _cache.projectData = {};
  95. _cache.exportKind = EXPORT_KIND.Tender;
  96. _cache.tenderDetailMap = {};
  97. }
  98. const CACHE = Object.freeze({
  99. getItem,
  100. setItem,
  101. clear
  102. });
  103. /*
  104. * 定义不设置一个Node方法统一进入的原因:模板化比较直观,不分开定义节点的话,调用传参也很麻烦而且不直观。
  105. * 一个节点对应一个构造方法,方便调整配置、方便其他版本开发、接手的人看起来更直观
  106. * @param {String}name 节点名
  107. * {Array}attrs 节点属性数据
  108. * {Array}failList 失败列表
  109. * @return {void}
  110. * */
  111. function Element(name, attrs) {
  112. this.name = name;
  113. let checkData = check(name, attrs);
  114. this.fail = checkData.failHints;
  115. this.attrs = checkData.filterAttrs;
  116. handleXMLEntity(this.attrs);
  117. this.children = [];
  118. _cache.failList.push(...this.fail);
  119. }
  120. /*
  121. * xml字符实体的处理,这些特殊字符不处理会导致xml文件格式出错:""、<>、&
  122. * 要先处理&amp
  123. * */
  124. let _xmlEntity = {
  125. '&': '&amp;',
  126. '\n': '&#xA;',
  127. '"': '&quot;',
  128. '\'': '&apos;',
  129. '<': '&lt;',
  130. '>': '&gt;'
  131. };
  132. // 对每个元素的所有属性值进行特殊字符处理
  133. function handleXMLEntity(attrs) {
  134. for (let attr of attrs) {
  135. if (!attr.value) {
  136. continue;
  137. }
  138. for (let [key, value] of Object.entries(_xmlEntity)) {
  139. attr.value = attr.value.replace(new RegExp(key, 'g'), value);
  140. }
  141. }
  142. }
  143. // 获取处理实体字符后的数据
  144. function getParsedData(arr) {
  145. return arr.map(data => {
  146. for (let [key, value] of Object.entries(_xmlEntity)) {
  147. data = data.replace(new RegExp(key, 'g'), value);
  148. }
  149. return data;
  150. });
  151. }
  152. /*
  153. * 检查
  154. * 创建节点时检查节点的数据
  155. * 1.长度限制minLen,maxLen
  156. * 2.值的限制,固定范围:enumeration
  157. * @param {String}eleName 节点名称
  158. * {Array}datas 需要检查的属性数据
  159. * @return {Object} failHints没通过的属性提示 filterAttrs过滤后的属性数据(失败提示在属性是必须的时候才提示,如果该属性失败了,但是是非必要属性,那么该属性不显示)
  160. * */
  161. function check(eleName, datas) {
  162. let rst = {failHints: [], filterAttrs: []};
  163. let dateReg = /([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))/;
  164. for (let data of datas) {
  165. // 值统一转换成String
  166. data.value = !hasValue(data.value)
  167. ? DEFAULT_VALUE[data.type]
  168. ? DEFAULT_VALUE[data.type]
  169. : ''
  170. : String(data.value);
  171. if (data.whiteSpace && data.whiteSpace === WHITE_SPACE.COLLAPSE) { //处理空格相关
  172. data.value = data.value.replace(/[\r,\n,\t]/g, ' ');
  173. data.value = data.value.trim();
  174. data.value = data.value.replace(/\s{1,}/g, ' ');
  175. }
  176. /*if (!data.value && !data.minLen && !data.enumeration) { //值为空,且没有限制最小字符数,且没有限制值,则不需判断
  177. rst.filterAttrs.push(data);
  178. continue;
  179. }*/
  180. let isFail = false,
  181. tempFail = '';
  182. if (data.minLen && data.value.length < data.minLen){
  183. isFail = true;
  184. tempFail = data.failHint
  185. ? `${data.failHint}字符数不可小于${data.minLen}个。`
  186. :`${eleName}-“${data.name}”字符数不可小于${data.minLen}个。`;
  187. } else if (data.maxLen && data.value.length > data.maxLen) {
  188. isFail = true;
  189. tempFail = data.failHint
  190. ? `${data.failHint}字符数不可大于${data.maxLen}个。`
  191. : `${eleName}-“${data.name}”字符数不可大于${data.maxLen}个。`;
  192. } else if (data.enumeration && !data.enumeration.includes(data.value)) {
  193. isFail = true;
  194. let enumerationHint = data.enumerationHint
  195. ? data.enumerationHint.join(';')
  196. : data.enumeration.join(';');
  197. tempFail = data.failHint
  198. ? `${data.failHint}只能从“${enumerationHint}”中选择。`
  199. : `${eleName}-“${data.name}”只能从“${enumerationHint}”中选择。`;
  200. } else if (data.type && data.type === TYPE.DATE && !dateReg.test(data.value)) {
  201. isFail = true;
  202. tempFail = data.failHint
  203. ? `${data.failHint}日期格式必须是YYYY-MM-DD。`
  204. : `${eleName}-“${data.name}”日期格式必须是YYYY-MM-DD。`;
  205. } else if (data.type && data.type === TYPE.INT && !Number.isInteger(parseFloat(data.value))) {
  206. isFail = true;
  207. tempFail = data.failHint
  208. ? `${data.failHint}必须为整数。`
  209. : `${eleName}-“${data.name}”必须为整数。`;
  210. } else if (data.type && data.type === TYPE.DECIMAL && isNaN(parseFloat(data.value))) {
  211. isFail = true;
  212. tempFail = data.failHint
  213. ? `${data.failHint}必须为数值。`
  214. : `${eleName}-“${data.name}”必须为数值。`;
  215. } else if (data.type && data.type === TYPE.NUM2) {
  216. let v = parseFloat(data.value);
  217. if (isNaN(v)) {
  218. isFail = true;
  219. tempFail = data.failHint
  220. ? `${data.failHint}必须为数值。`
  221. : `${eleName}-“${data.name}”必须为数值。`;
  222. } else if (!data.value.length || (data.value.split('.').length > 1 && data.value.split('.')[1].length > 2)){
  223. isFail = true;
  224. }
  225. } else if (data.type && data.type === TYPE.BOOL && !['true', 'false'].includes(String(data.value))) {
  226. isFail = true;
  227. tempFail = data.failHint
  228. ? `${data.failHint}必须为true或false。`
  229. : `${eleName}-“${data.name}”必须为true或false。`;
  230. }
  231. if (!isFail || data.required) {
  232. rst.filterAttrs.push(data);
  233. }
  234. if (isFail && data.required && tempFail) {
  235. rst.failHints.push(tempFail);
  236. }
  237. }
  238. return rst;
  239. }
  240. // 提取各导出类型的自检数据
  241. function _extractHintParts(failList) {
  242. let rst = [],
  243. curPart;
  244. for (let hint of failList) {
  245. if (hint === HINT_START) {
  246. curPart = [];
  247. rst.push(curPart);
  248. continue;
  249. }
  250. curPart.push(hint);
  251. }
  252. return rst;
  253. }
  254. // 自检提示去重
  255. function deWeightHints(failList) {
  256. let rst = [];
  257. let hintParts = _extractHintParts(failList);
  258. // 建设项目提示文本
  259. let rootHints = [],
  260. // 单位工程提示文本映射
  261. tenderMap = {},
  262. reg = /^<span style="font-weight: bold">单位工程/;
  263. for (let hintPart of hintParts) {
  264. // 单位工程xxx提示
  265. let curTenderHint;
  266. // 提取建设项目提示、各自单位工程提示
  267. for (let hint of hintPart) {
  268. if (reg.test(hint)) {
  269. curTenderHint = hint;
  270. if (!tenderMap[curTenderHint]) {
  271. tenderMap[curTenderHint] = [];
  272. }
  273. continue;
  274. }
  275. if (curTenderHint) {
  276. tenderMap[curTenderHint].push(hint);
  277. } else {
  278. rootHints.push(hint);
  279. }
  280. }
  281. }
  282. // 建设项目提示去重,放入结果中
  283. rootHints = [...new Set(rootHints)];
  284. rst.push(...rootHints);
  285. // 单位工程提示放入结果中
  286. for (let tenderHint in tenderMap) {
  287. rst.push(tenderHint);
  288. // 单位工程提示去重
  289. let tenderHints = [...new Set(tenderMap[tenderHint])];
  290. rst.push(...tenderHints);
  291. }
  292. return rst;
  293. }
  294. // 等待一段时间
  295. function setTimeoutSync(handle, time) {
  296. return new Promise((resolve, reject) => {
  297. setTimeout(() => {
  298. if (handle && typeof handle === 'function') {
  299. handle();
  300. }
  301. resolve();
  302. }, time);
  303. });
  304. }
  305. // v是否定义了,不为undefined和null
  306. function isDef(v) {
  307. return typeof v !== 'undefined' && v !== null;
  308. }
  309. // v是否有值,不为undefined、null、''
  310. function hasValue(v) {
  311. return typeof v !== 'undefined' && v !== null && v !== '';
  312. }
  313. /*
  314. * 从fees数组中获取相关费用
  315. * @param {Array}fees 费用数组
  316. * {String}feeFields 费用字段
  317. * @return {Number}
  318. * @example getFee(source.fees, 'common.totalFee')
  319. * */
  320. function getFee(fees, feeFields) {
  321. if (!Array.isArray(fees)) {
  322. return 0;
  323. }
  324. let fields = feeFields.split('.');
  325. let fee = fees.find(data => data.fieldName === fields[0]);
  326. if (!fee) {
  327. return 0;
  328. }
  329. return fee[fields[1]] || 0;
  330. }
  331. /*
  332. * 根据key获取对应的基本信息、工程特征数据
  333. * @param {Array}data
  334. * {String}key
  335. * @return {String}
  336. * @example getValueByKey(source.basicInformation, 'projectScale')
  337. * */
  338. function getValueByKey(data, key) {
  339. for (let d of data) {
  340. if (d.key === key) {
  341. return d.value;
  342. }
  343. if (d.items && d.items.length > 0) {
  344. let findData = d.items.find(x => x.key === key);
  345. if (findData) {
  346. return findData.value;
  347. }
  348. }
  349. }
  350. return '';
  351. }
  352. // 数组打平成对象
  353. function arrayToObj(arr) {
  354. let rst = {};
  355. for (let data of arr) {
  356. rst[data.key] = data.value;
  357. }
  358. return rst;
  359. }
  360. /*
  361. * 检测层数
  362. * @param {Number}maxDepth(最大深度)
  363. * {Object}node(需要检测的清单树节点)
  364. * @return {Boolean}
  365. * */
  366. function validDepth(maxDepth, node) {
  367. let nodeDepth = node.depth();
  368. let allNodes = node.getPosterity();
  369. //检测相对深度
  370. for (let n of allNodes) {
  371. let relativeDepth = n.depth() - nodeDepth;
  372. if (relativeDepth > maxDepth) {
  373. return false;
  374. }
  375. }
  376. return true;
  377. }
  378. /*
  379. * 检测唯一性,有些属性在规定的数据范围内唯一
  380. * @param {Object}constraints(约束池)
  381. * {All}data(检测的数据)
  382. * {String}hint(提示已存在的内容)
  383. * {String}subHint(额外提示,有额外提示时,不用data提示)
  384. * @return {void}
  385. * */
  386. function checkUnique(constraints, data, hint, subHint) {
  387. if (constraints.includes(data)) {
  388. let failHint = subHint
  389. ? `${hint}“${subHint}”已存在`
  390. : `${hint}“${data}”已存在`;
  391. _cache.failList.push(failHint);
  392. } else if (data) {
  393. constraints.push(data);
  394. }
  395. }
  396. //根据数据的NextSiblingID进行排序,返回排序后的数组
  397. function sortByNext(datas) {
  398. let target = [],
  399. temp = {};
  400. for (let data of datas) {
  401. temp[data.ID] = {me: data, next: null, prev: null};
  402. }
  403. for (let data of datas) {
  404. let next = temp[data.NextSiblingID] || null;
  405. temp[data.ID].next = next;
  406. if (next) {
  407. next.prev = temp[data.ID];
  408. }
  409. }
  410. let first = null;
  411. for (let data of datas) {
  412. let me = temp[data.ID];
  413. if (!me.prev) {
  414. first = me;
  415. }
  416. }
  417. if (!first) {
  418. return datas;
  419. }
  420. while (first) {
  421. target.push(first.me);
  422. first = first.next;
  423. }
  424. return target;
  425. }
  426. /*
  427. * 根据粒度获取项目(不包含详细数据)数据
  428. * @param {Number}granularity 导出粒度
  429. * {Number}tenderID 单位工程ID
  430. * {String}userID 用户ID
  431. * @return {Object} 返回的数据结构:{children: [{children: []}]} 最外层为建设项目,中间为单项工程,最底层为单位工程
  432. * */
  433. async function getProjectByGranularity(granularity, tenderID, userID) {
  434. let projectData = _cache.projectData;
  435. // 没有数据,需要拉取
  436. if (!Object.keys(projectData).length) {
  437. projectData = await ajaxPost('/pm/api/getProjectByGranularity', {user_id: userID, tenderID: tenderID, granularity: granularity});
  438. _cache.projectData = projectData;
  439. }
  440. return projectData;
  441. }
  442. /*
  443. * 通过getData接口获取单位工程详细数据
  444. * @param {Number}tenderID 单位工程ID
  445. * {String}userID 用户ID
  446. * @return {Object} 跟projectObj.project的数据结构一致
  447. * */
  448. async function getTenderDetail(tenderID, userID) {
  449. // 获取单位工程详细数据
  450. let tenderDetail = _cache.tenderDetailMap[tenderID];
  451. if (!tenderDetail) {
  452. tenderDetail = PROJECT.createNew(tenderID, userID);
  453. await tenderDetail.loadDataSync();
  454. _cache.tenderDetailMap[tenderID] = tenderDetail;
  455. }
  456. return tenderDetail;
  457. }
  458. /*
  459. * 提取要导出的数据
  460. * @param {Function}entryFunc 提取数据的入口方法
  461. * {Number}granularity 导出粒度: 1-建设项目、2-单项工程、3-单位工程
  462. * {Number}exportKind 导出的文件类型:1-投标、2-招标、3-控制价
  463. * {Number}tenderID 单位工程ID
  464. * {String}userID 用户ID
  465. * @return {Array} 数据结构为:[{data: Object, exportKind: Number, fileName: String}]
  466. * */
  467. async function extractExportData(entryFunc, granularity, exportKind, tenderID, userID) {
  468. // 默认导出建设项目
  469. if (!granularity || ![1, 2, 3].includes(granularity)) {
  470. granularity = GRANULARITY.PROJECT;
  471. }
  472. // 默认导出投标文件
  473. if (!exportKind || ![1, 2, 3].includes(exportKind)) {
  474. exportKind = EXPORT_KIND.Tender;
  475. }
  476. // 拉取标段数据:建设项目、单项工程、单位工程数据
  477. let projectData = await getProjectByGranularity(granularity, tenderID, userID);
  478. if (!projectData) {
  479. throw '获取项目数据错误';
  480. }
  481. // 单项工程、单位工程按照树结构数据进行排序
  482. projectData.children = sortByNext(projectData.children);
  483. for (let engData of projectData.children) {
  484. engData.children = sortByNext(engData.children);
  485. }
  486. // 提取相关项目的详细导出数据
  487. return await entryFunc(userID, exportKind, projectData);
  488. }
  489. //转换基数表达式
  490. //1.有子项,则取固定清单对应基数
  491. //2.无子项,有基数,a.优先转换为行代号(不可自身) b.不能转换为行代号则找对应字典
  492. //3.基数中有无法转换的,设为金额
  493. function transformCalcBase(tenderDetail, node, {CalcBaseMap, FlagCalcBaseMap}) {
  494. let expr = node.data.calcBase || '';
  495. if (node.children.length) {
  496. let flag = node.getFlag();
  497. return FlagCalcBaseMap[flag] || '';
  498. }
  499. if (expr) {
  500. let illegal = false;
  501. let normalBase = _getNormalBase(expr),
  502. idBase = _getIDBase(expr);
  503. //普通基数转基数字典
  504. normalBase.forEach(base => {
  505. let replaceStr = CalcBaseMap[base];
  506. //转换成行代号的优先级比较高,进行清单匹配
  507. let flag = FlagCalcBaseMap[base];
  508. if (flag) {
  509. let flagNode = tenderDetail.mainTree.items.find(mNode => mNode.getFlag() === flag);
  510. //匹配到了 普通基数转换成行引用
  511. if (flagNode) {
  512. replaceStr = `F${flagNode.serialNo() + 1}`;
  513. }
  514. }
  515. //存在无法处理的基数
  516. if (!replaceStr) {
  517. illegal = true;
  518. return;
  519. }
  520. expr = expr.replace(new RegExp(base, 'g'), replaceStr);
  521. });
  522. //id引用转行代号引用
  523. idBase.forEach(base => {
  524. let id = base.match(/[^@]+/)[0];
  525. let theNode = tenderDetail.mainTree.getNodeByID(id),
  526. rowCode = theNode ? `F${theNode.serialNo() + 1}` : '';
  527. if (!rowCode) {
  528. illegal = true;
  529. return;
  530. }
  531. expr = expr.replace(new RegExp(base, 'g'), rowCode);
  532. });
  533. //不合法 返回金额
  534. if (illegal) {
  535. return getFee(node.data.fees, 'common.totalFee');
  536. }
  537. return expr;
  538. }
  539. //获取普通基数: {xxx}
  540. function _getNormalBase(str) {
  541. let reg = /{.+?}/g,
  542. matchs = str.match(reg);
  543. return matchs || [];
  544. }
  545. //获取id引用基数: @xxx-xxx-xx
  546. function _getIDBase(str) {
  547. let reg = /@.{36}/g,
  548. matchs = str.match(reg);
  549. return matchs || [];
  550. }
  551. }
  552. // 转换基数说明,根据转换后的基数处理
  553. //1.行引用转换为对应行的名称
  554. //2.基数字典转换为中文
  555. function transformCalcBaseState(tenderDetail, expr, CalcStateMap) {
  556. if (!expr) {
  557. return '';
  558. }
  559. expr = String(expr);
  560. //提取基数
  561. let bases = expr.split(/[\+\-\*\/]/g);
  562. //提取操作符
  563. let oprs = expr.match(/[\+\-\*\/]/g);
  564. //转换后的基数
  565. let newBase = [];
  566. let illegal = false;
  567. for (let base of bases) {
  568. //行引用转换为名称.
  569. if (/F\d+/.test(base)) {
  570. let rowCode = base.match(/\d+/)[0],
  571. node = tenderDetail.mainTree.items[rowCode - 1];
  572. if (!node || !node.data.name) {
  573. illegal = true;
  574. break;
  575. }
  576. newBase.push(node && node.data.name ? node.data.name : '');
  577. } else if (CalcStateMap[base]){ //字典转换为中文
  578. newBase.push(CalcStateMap[base]);
  579. } else if (/^\d+(\.\d+)?$/.test(base)) { //金额
  580. newBase.push(base);
  581. } else {
  582. illegal = true;
  583. break;
  584. }
  585. }
  586. if (illegal) {
  587. return '';
  588. }
  589. let newExpr = '';
  590. for (let i = 0; i < newBase.length; i++) {
  591. newExpr += newBase[i];
  592. if (oprs && oprs[i]) {
  593. newExpr += oprs[i];
  594. }
  595. }
  596. return newExpr;
  597. }
  598. // 获取工程编号表格相关数据(导出需要弹出工程编号让用户选择)
  599. function getCodeSheetData(projectData) {
  600. let curCode = '0';
  601. let sheetData = [];
  602. sheetData.push(getObj(projectData));
  603. projectData.children.forEach(eng => {
  604. sheetData.push(getObj(eng));
  605. eng.children.forEach(tender => {
  606. sheetData.push(getObj(tender));
  607. });
  608. });
  609. //建设项目父ID设置为-1
  610. if (sheetData.length) {
  611. sheetData[0].ParentID = -1;
  612. sheetData[0].code = '';
  613. }
  614. return sheetData;
  615. function getObj(data) {
  616. return {
  617. collapsed: false,
  618. ID: data.ID,
  619. ParentID: data.ParentID,
  620. NextSiblingID: data.NextSiblingID,
  621. name: data.name,
  622. code: data.code || String(curCode++)
  623. };
  624. }
  625. }
  626. // 从srcEle节点中获取元素名为eleName的元素
  627. function getElementFromSrc(srcEle, eleName) {
  628. if (!srcEle || !srcEle.children || !srcEle.children.length) {
  629. return [];
  630. }
  631. return srcEle.children.filter(ele => ele.name === eleName);
  632. }
  633. /*
  634. * 设置完工程编号后,更新原始数据的工程编号
  635. * 更新原始数据前需要将编号里的特殊字符进行转换
  636. * @param {Array}exportData 提取出来的需要导出的数据
  637. * {Array}codes 工程编号表中填写的工程编号
  638. * {String}EngineeringName 单项工程元素的名称
  639. * {String}tenderName 单位工程元素的名称
  640. * {String}codeName 编号属性的名称
  641. * @return {void}
  642. * */
  643. function setupCode(exportData, codes, EngineeringName, tenderName, codeName) {
  644. // 转换xml实体字符
  645. let parsedCodes = getParsedData(codes);
  646. // 给导出数据里的单项工程、单位工程填上用户设置的工程编号
  647. exportData.forEach(orgData => {
  648. let curIdx = 0;
  649. let engs = getElementFromSrc(orgData.data, EngineeringName);
  650. engs.forEach(eng => {
  651. eng.attrs.find(attr => attr.name === codeName).value = parsedCodes[curIdx++];
  652. let tenders = getElementFromSrc(eng, tenderName);
  653. tenders.forEach(tender => {
  654. tender.attrs.find(attr => attr.name === codeName).value = parsedCodes[curIdx++];
  655. });
  656. });
  657. });
  658. }
  659. const UTIL = Object.freeze({
  660. deWeightHints,
  661. isDef,
  662. hasValue,
  663. setTimeoutSync,
  664. getFee,
  665. getValueByKey,
  666. arrayToObj,
  667. validDepth,
  668. checkUnique,
  669. sortByNext,
  670. getTenderDetail,
  671. getProjectByGranularity,
  672. transformCalcBase,
  673. transformCalcBaseState,
  674. getCodeSheetData,
  675. getElementFromSrc,
  676. getParsedData,
  677. setupCode
  678. });
  679. // 开始标签
  680. function _startTag(ele) {
  681. let rst = `<${ele.name}`;
  682. for (let attr of ele.attrs) {
  683. rst += ` ${attr.name}="${attr.value}"`;
  684. }
  685. rst += ele.children.length > 0 ? '>' : '/>';
  686. return rst;
  687. }
  688. // 结束标签
  689. function _endTag(ele) {
  690. return `</${ele.name}>`;
  691. }
  692. // 拼接成xml字符串
  693. function _toXMLStr(eles) {
  694. let rst = '';
  695. for (let ele of eles) {
  696. rst += _startTag(ele);
  697. if (ele.children.length > 0) {
  698. rst += _toXMLStr(ele.children);
  699. rst += _endTag(ele);
  700. }
  701. }
  702. return rst;
  703. }
  704. //格式化xml字符串
  705. function _formatXml(text) {
  706. //去掉多余的空格
  707. text = '\n' + text.replace(/>\s*?</g, ">\n<");
  708. //调整格式
  709. let rgx = /\n(<(([^\?]).+?)(?:\s|\s*?>|\s*?(\/)>)(?:.*?(?:(?:(\/)>)|(?:<(\/)\2>)))?)/mg;
  710. let nodeStack = [];
  711. let output = text.replace(rgx, function($0, all, name, isBegin, isCloseFull1, isCloseFull2, isFull1, isFull2){
  712. let isClosed = (isCloseFull1 === '/') || (isCloseFull2 === '/' ) || (isFull1 === '/') || (isFull2 === '/');
  713. let prefix = '';
  714. if (isBegin === '!') {
  715. prefix = getPrefix(nodeStack.length);
  716. } else {
  717. if (isBegin !== '/') {
  718. prefix = getPrefix(nodeStack.length);
  719. if (!isClosed) {
  720. nodeStack.push(name);
  721. }
  722. } else {
  723. nodeStack.pop();
  724. prefix = getPrefix(nodeStack.length);
  725. }
  726. }
  727. let ret = '\n' + prefix + all;
  728. return ret;
  729. });
  730. let outputText = output.substring(1);
  731. return outputText;
  732. function getPrefix(prefixIndex) {
  733. let span = ' ';
  734. let output = [];
  735. for (let i = 0 ; i < prefixIndex; ++i) {
  736. output.push(span);
  737. }
  738. return output.join('');
  739. }
  740. }
  741. /*
  742. * 根据各自费用定额的文件结构,导出文件
  743. * 每个费用定额可能导出的结果文件都不同
  744. * 比如广东18需要将一个建设项目文件,多个单位工程文件打包成一个zip文件。重庆18就没这种要求
  745. * @param {Array}codes 工程编号数据
  746. * {Array}extractData 提取的数据
  747. * {Function}setCodeFunc 各自费用定额的导出前重设用户输入的工程编号方法
  748. * {Function}saveAsFunc 各自费用定额的导出方法
  749. * @return {void}
  750. * */
  751. async function exportFile(codes, extractData, setCodeFunc, saveAsFunc) {
  752. // 编号重置后将会被导出,需要将编号进行xml字符实体转换
  753. codes = getParsedData(codes);
  754. setCodeFunc(codes, extractData);
  755. // 获取文件数据
  756. let fileData = extractData.map(extractObj => {
  757. // 转换成xml字符串
  758. let xmlStr = _toXMLStr([extractObj.data]);
  759. // 加上xml声明
  760. xmlStr = `<?xml version="1.0" encoding="utf-8"?>${xmlStr}`;
  761. // 格式化
  762. xmlStr = _formatXml(xmlStr);
  763. let blob = new Blob([xmlStr], {type: 'text/plain;charset=utf-8'});
  764. return {
  765. blob: blob,
  766. exportKind: extractObj.exportKind,
  767. fileName: extractObj.fileName
  768. };
  769. });
  770. // 导出
  771. await saveAsFunc(fileData);
  772. }
  773. return {
  774. CONFIG,
  775. CACHE,
  776. UTIL,
  777. Element,
  778. extractExportData,
  779. exportFile
  780. };
  781. })();