exportStdInterfaceBase.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2019/6/20
  7. * @version
  8. */
  9. const XML_EXPORT_BASE = (() => {
  10. // 属性类型
  11. const TYPE = {
  12. DATE: 1, //日期类型YYYY-MM-DD
  13. DATE_TIME: 2, //日期类型YYY-MM-DDTHH:mm:ss
  14. INT: 3, //整数类型
  15. DECIMAL: 4, //数值类型,不限制小数位数
  16. NUM2: 5, //数值类型2:最多两位小数
  17. BOOL: 6 //布尔型
  18. };
  19. // 空白字符处理
  20. const WHITE_SPACE = {
  21. COLLAPSE: 1 //移除所有空白字符(换行、回车、空格以及制表符会被替换为空格,开头和结尾的空格会被移除,而多个连续的空格会被缩减为一个单一的空格)
  22. };
  23. // 计税方法
  24. const TAX_TYPE = {
  25. '1': '一般计税法',
  26. '2': '简易计税法',
  27. };
  28. // 承包人材料调整类型
  29. const ADJUST_TYPE = {
  30. info: 'priceInfo', // 造价信息差额调整法
  31. coe: 'priceCoe' // 价格指数调整法
  32. };
  33. // 加载数据间隔,减少服务器压力
  34. const TIMEOUT_TIME = 500;
  35. // 导出粒度
  36. const GRANULARITY = {
  37. PROJECT: 1, //导出建设项目
  38. ENGINEERING: 2, //导出单项工程
  39. TENDER: 3 //导出单位工程
  40. };
  41. // 导出的文件类型选项
  42. const EXPORT_KIND = {
  43. Tender: 1, //投标
  44. Bid: 2, //招标
  45. Control: 3 //控制价
  46. };
  47. // 配置项
  48. const CONFIG = Object.freeze({
  49. TYPE,
  50. WHITE_SPACE,
  51. TAX_TYPE,
  52. ADJUST_TYPE,
  53. TIMEOUT_TIME,
  54. GRANULARITY,
  55. EXPORT_KIND
  56. });
  57. // 缓存项 不需要的时候需要清空
  58. let _cache = {
  59. // 失败列表
  60. failList: [],
  61. // 项目数据(不包含详细数据,项目管理数据)
  62. projectData: {},
  63. // 当前导出类型,默认投标
  64. exportKind: EXPORT_KIND.Tender,
  65. // 记录拉取的单位工程项目详细数据,导出的时候,可能会导出多个文件,只有导出第一个文件的时候需要请求数据
  66. tenderDetailMap: {}
  67. };
  68. // 返回缓存项
  69. function getItem(key) {
  70. return _cache[key] || null;
  71. }
  72. // 设置缓存项
  73. function setItem(key, value) {
  74. // 与原数据是同类型的数据才可设置成功
  75. if (_cache[key] &&
  76. Object.prototype.toString.call(_cache[key]) ===
  77. Object.prototype.toString.call(value)) {
  78. _cache[key] = value;
  79. }
  80. }
  81. // 清空缓存项
  82. function clear() {
  83. _cache.failList = [];
  84. _cache.projectData = {};
  85. _cache.exportKind = EXPORT_KIND.Tender;
  86. _cache.tenderDetailMap = {};
  87. }
  88. const CACHE = Object.freeze({
  89. getItem,
  90. setItem,
  91. clear
  92. });
  93. /*
  94. * 定义不设置一个Node方法统一进入的原因:模板化比较直观,不分开定义节点的话,调用传参也很麻烦而且不直观。
  95. * 一个节点对应一个构造方法,方便调整配置、方便其他版本开发、接手的人看起来更直观
  96. * @param {String}name 节点名
  97. * {Array}attrs 节点属性数据
  98. * {Array}failList 失败列表
  99. * @return {void}
  100. * */
  101. function Element(name, attrs) {
  102. this.name = name;
  103. let checkData = check(name, attrs);
  104. this.fail = checkData.failHints;
  105. this.attrs = checkData.filterAttrs;
  106. this.children = [];
  107. _cache.failList.push(...this.fail);
  108. }
  109. /*
  110. * 检查
  111. * 创建节点时检查节点的数据
  112. * 1.长度限制minLen,maxLen
  113. * 2.值的限制,固定范围:enumeration
  114. * @param {String}eleName 节点名称
  115. * {Array}datas 需要检查的属性数据
  116. * @return {Object} failHints没通过的属性提示 filterAttrs过滤后的属性数据(失败提示在属性是必须的时候才提示,如果该属性失败了,但是是非必要属性,那么该属性不显示)
  117. * */
  118. function check(eleName, datas) {
  119. let rst = {failHints: [], filterAttrs: []};
  120. 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])))/;
  121. for (let data of datas) {
  122. data.value = typeof data.value === 'undefined' || data.value === null ? '' : String(data.value);
  123. if (data.whiteSpace && data.whiteSpace === WHITE_SPACE.COLLAPSE) { //处理空格相关
  124. data.value = data.value.replace(/[\r,\n,\t]/g, ' ');
  125. data.value = data.value.trim();
  126. data.value = data.value.replace(/\s{1,}/g, ' ');
  127. }
  128. /*if (!data.value && !data.minLen && !data.enumeration) { //值为空,且没有限制最小字符数,且没有限制值,则不需判断
  129. rst.filterAttrs.push(data);
  130. continue;
  131. }*/
  132. let isFail = false,
  133. tempFail = '';
  134. if (data.minLen && data.value.length < data.minLen){
  135. isFail = true;
  136. tempFail = data.failHint
  137. ? `${data.failHint}字符数不可小于${data.minLen}个。`
  138. :`${eleName}-“${data.name}”字符数不可小于${data.minLen}个。`;
  139. } else if (data.maxLen && data.value.length > data.maxLen) {
  140. isFail = true;
  141. tempFail = data.failHint
  142. ? `${data.failHint}字符数不可大于${data.maxLen}个。`
  143. : `${eleName}-“${data.name}”字符数不可大于${data.maxLen}个。`;
  144. } else if (data.enumeration && !data.enumeration.includes(data.value)) {
  145. isFail = true;
  146. let enumerationHint = data.enumerationHint
  147. ? data.enumerationHint.join(';')
  148. : data.enumeration.join(';');
  149. tempFail = data.failHint
  150. ? `${data.failHint}只能从“${enumerationHint}”中选择。`
  151. : `${eleName}-“${data.name}”只能从“${enumerationHint}”中选择。`;
  152. } else if (data.type && data.type === TYPE.DATE && !dateReg.test(data.value)) {
  153. isFail = true;
  154. tempFail = data.failHint
  155. ? `${data.failHint}日期格式必须是YYYY-MM-DD。`
  156. : `${eleName}-“${data.name}”日期格式必须是YYYY-MM-DD。`;
  157. } else if (data.type && data.type === TYPE.INT && !Number.isInteger(parseFloat(data.value))) {
  158. isFail = true;
  159. tempFail = data.failHint
  160. ? `${data.failHint}必须为整数。`
  161. : `${eleName}-“${data.name}”必须为整数。`;
  162. } else if (data.type && data.type === TYPE.DECIMAL && isNaN(parseFloat(data.value))) {
  163. isFail = true;
  164. tempFail = data.failHint
  165. ? `${data.failHint}必须为数值。`
  166. : `${eleName}-“${data.name}”必须为数值。`;
  167. } else if (data.type && data.type === TYPE.NUM2) {
  168. let v = parseFloat(data.value);
  169. if (isNaN(v)) {
  170. isFail = true;
  171. tempFail = data.failHint
  172. ? `${data.failHint}必须为数值。`
  173. : `${eleName}-“${data.name}”必须为数值。`;
  174. } else if (!data.value.length || (data.value.split('.').length > 1 && data.value.split('.')[1].length > 2)){
  175. isFail = true;
  176. }
  177. } else if (data.type && data.type === TYPE.BOOL && !['true', 'false'].includes(String(data.value))) {
  178. isFail = true;
  179. tempFail = data.failHint
  180. ? `${data.failHint}必须为true或false。`
  181. : `${eleName}-“${data.name}”必须为true或false。`;
  182. }
  183. if (!isFail || data.required) {
  184. rst.filterAttrs.push(data);
  185. }
  186. if (isFail && data.required && tempFail) {
  187. rst.failHints.push(tempFail);
  188. }
  189. }
  190. return rst;
  191. }
  192. // 等待一段时间
  193. function setTimeoutSync(handle, time) {
  194. return new Promise((resolve, reject) => {
  195. setTimeout(() => {
  196. if (handle && typeof handle === 'function') {
  197. handle();
  198. }
  199. resolve();
  200. }, time);
  201. });
  202. }
  203. function isDef(v) {
  204. return typeof v !== 'undefined' && v !== null;
  205. }
  206. /*
  207. * 从fees数组中获取相关费用
  208. * @param {Array}fees 费用数组
  209. * {String}feeFields 费用字段
  210. * @return {Number}
  211. * @example getFee(source.fees, 'common.totalFee')
  212. * */
  213. function getFee(fees, feeFields) {
  214. if (!Array.isArray(fees)) {
  215. return 0;
  216. }
  217. let fields = feeFields.split('.');
  218. let fee = fees.find(data => data.fieldName === fields[0]);
  219. if (!fee) {
  220. return 0;
  221. }
  222. return fee[fields[1]] || 0;
  223. }
  224. /*
  225. * 根据key获取对应的基本信息、工程特征数据
  226. * @param {Array}data
  227. * {String}key
  228. * @return {String}
  229. * @example getValueByKey(source.basicInformation, 'projectScale')
  230. * */
  231. function getValueByKey(data, key) {
  232. for (let d of data) {
  233. if (d.key === key) {
  234. return d.value;
  235. }
  236. if (d.items && d.items.length > 0) {
  237. let findData = d.items.find(x => x.key === key);
  238. if (findData) {
  239. return findData.value;
  240. }
  241. }
  242. }
  243. return '';
  244. }
  245. // 数组打平成对象
  246. function arrayToObj(arr) {
  247. let rst = {};
  248. for (let data of arr) {
  249. rst[data.key] = data.value;
  250. }
  251. return rst;
  252. }
  253. //检测层数
  254. //@param {Number}maxDepth(最大深度) {Object}node(需要检测的清单树节点)
  255. //@return {Boolean}
  256. function validDepth(maxDepth, node) {
  257. let nodeDepth = node.depth();
  258. let allNodes = node.getPosterity();
  259. //检测相对深度
  260. for (let n of allNodes) {
  261. let relativeDepth = n.depth() - nodeDepth;
  262. if (relativeDepth > maxDepth) {
  263. return false;
  264. }
  265. }
  266. return true;
  267. }
  268. //检测唯一性
  269. //@param {Object}constraints(约束池) {All}data(检测的数据) {String}hint(提示已存在的内容) {String}subHint(额外提示,有额外提示时,不用data提示)
  270. //@return {void}
  271. function checkUnique(constraints, data, hint, subHint) {
  272. if (constraints.includes(data)) {
  273. let failHint = subHint
  274. ? `${hint}“${subHint}”已存在`
  275. : `${hint}“${data}”已存在`;
  276. _cache.failList.push(failHint);
  277. } else if (data) {
  278. constraints.push(data);
  279. }
  280. }
  281. //根据数据的NextSiblingID进行排序,返回排序后的数组
  282. function sortByNext(datas) {
  283. let target = [],
  284. temp = {};
  285. for (let data of datas) {
  286. temp[data.ID] = {me: data, next: null, prev: null};
  287. }
  288. for (let data of datas) {
  289. let next = temp[data.NextSiblingID] || null;
  290. temp[data.ID].next = next;
  291. if (next) {
  292. next.prev = temp[data.ID];
  293. }
  294. }
  295. let first = null;
  296. for (let data of datas) {
  297. let me = temp[data.ID];
  298. if (!me.prev) {
  299. first = me;
  300. }
  301. }
  302. if (!first) {
  303. return datas;
  304. }
  305. while (first) {
  306. target.push(first.me);
  307. first = first.next;
  308. }
  309. return target;
  310. }
  311. //转换基数表达式
  312. //1.有子项,则取固定清单对应基数
  313. //2.无子项,有基数,a.优先转换为行代号(不可自身) b.不能转换为行代号则找对应字典
  314. //3.基数中有无法转换的,设为金额
  315. function transformCalcBase(tenderDetail, node, {CalcBaseMap, FlagCalcBaseMap}) {
  316. let expr = node.data.calcBase || '';
  317. if (node.children.length) {
  318. let flag = node.getFlag();
  319. return FlagCalcBaseMap[flag] || '';
  320. }
  321. if (expr) {
  322. let illegal = false;
  323. let normalBase = _getNormalBase(expr),
  324. idBase = _getIDBase(expr);
  325. //普通基数转基数字典
  326. normalBase.forEach(base => {
  327. let replaceStr = CalcBaseMap[base];
  328. //转换成行代号的优先级比较高,进行清单匹配
  329. let flag = FlagCalcBaseMap[base];
  330. if (flag) {
  331. let flagNode = tenderDetail.mainTree.items.find(mNode => mNode.getFlag() === flag);
  332. //匹配到了 普通基数转换成行引用
  333. if (flagNode) {
  334. replaceStr = `F${flagNode.serialNo() + 1}`;
  335. }
  336. }
  337. //存在无法处理的基数
  338. if (!replaceStr) {
  339. illegal = true;
  340. return;
  341. }
  342. expr = expr.replace(new RegExp(base, 'g'), replaceStr);
  343. });
  344. //id引用转行代号引用
  345. idBase.forEach(base => {
  346. let id = base.match(/[^@]+/)[0];
  347. let theNode = tenderDetail.mainTree.getNodeByID(id),
  348. rowCode = theNode ? `F${theNode.serialNo() + 1}` : '';
  349. if (!rowCode) {
  350. illegal = true;
  351. return;
  352. }
  353. expr = expr.replace(new RegExp(base, 'g'), rowCode);
  354. });
  355. //不合法 返回金额
  356. if (illegal) {
  357. return getFee(node.data.fees, 'common.totalFee');
  358. }
  359. return expr;
  360. }
  361. //获取普通基数: {xxx}
  362. function _getNormalBase(str) {
  363. let reg = /{.+?}/g,
  364. matchs = str.match(reg);
  365. return matchs || [];
  366. }
  367. //获取id引用基数: @xxx-xxx-xx
  368. function _getIDBase(str) {
  369. let reg = /@.{36}/g,
  370. matchs = str.match(reg);
  371. return matchs || [];
  372. }
  373. }
  374. //转换基数说明,根据转换后的基数处理
  375. //1.行引用转换为对应行的名称
  376. //2.基数字典转换为中文
  377. function transformCalcBaseState(tenderDetail, expr, CalcStateMap) {
  378. if (!expr) {
  379. return '';
  380. }
  381. expr = String(expr);
  382. //提取基数
  383. let bases = expr.split(/[\+\-\*\/]/g);
  384. //提取操作符
  385. let oprs = expr.match(/[\+\-\*\/]/g);
  386. //转换后的基数
  387. let newBase = [];
  388. let illegal = false;
  389. for (let base of bases) {
  390. //行引用转换为名称
  391. if (/F\d+/.test(base)) {
  392. let rowCode = base.match(/\d+/)[0],
  393. node = tenderDetail.mainTree.items[rowCode - 1];
  394. if (!node || !node.data.name) {
  395. illegal = true;
  396. break;
  397. }
  398. newBase.push(node && node.data.name ? node.data.name : '');
  399. } else if (CalcStateMap[base]){ //字典转换为中文
  400. newBase.push(CalcStateMap[base]);
  401. } else if (/^\d+(\.\d+)?$/.test(base)) { //金额
  402. newBase.push(base);
  403. } else {
  404. illegal = true;
  405. break;
  406. }
  407. }
  408. if (illegal) {
  409. return '';
  410. }
  411. let newExpr = '';
  412. for (let i = 0; i < newBase.length; i++) {
  413. newExpr += newBase[i];
  414. if (oprs && oprs[i]) {
  415. newExpr += oprs[i];
  416. }
  417. }
  418. return newExpr;
  419. }
  420. // 获取工程编号表格相关数据(导出需要弹出工程编号让用户选择)
  421. function getCodeSheetData(PMData) {
  422. let curCode = '0';
  423. let sheetData = [];
  424. sheetData.push(getObj(PMData));
  425. PMData.children.forEach(eng => {
  426. sheetData.push(getObj(eng));
  427. eng.children.forEach(tender => {
  428. sheetData.push(getObj(tender));
  429. });
  430. });
  431. //建设项目父ID设置为-1
  432. if (sheetData.length) {
  433. sheetData[0].ParentID = -1;
  434. sheetData[0].code = '';
  435. }
  436. return sheetData;
  437. function getObj(data) {
  438. return {
  439. collapsed: false,
  440. ID: data.ID,
  441. ParentID: data.ParentID,
  442. NextSiblingID: data.NextSiblingID,
  443. name: data.name,
  444. code: data.code || String(curCode++)
  445. };
  446. }
  447. }
  448. // 从srcNode节点中获取为target实例的节点
  449. function _getNodeFromSrc(srcNode, target) {
  450. if (!srcNode || !srcNode.children || !srcNode.children.length) {
  451. return [];
  452. }
  453. return srcNode.children.filter(node => node instanceof target);
  454. }
  455. // 设置完工程编号后,更新原始数据的工程编号
  456. function setupCode(originalDatas, codeDatas, {Tender, Engineering}) {
  457. originalDatas.forEach(orgData => {
  458. let curIdx = 0;
  459. let engs = _getNodeFromSrc(orgData.data, Engineering);
  460. engs.forEach(eng => {
  461. eng.attrs.find(attr => attr.name === '编号').value = codeDatas[curIdx++];
  462. let tenders = _getNodeFromSrc(eng, Tender);
  463. tenders.forEach(tender => {
  464. tender.attrs.find(attr => attr.name === '编号').value = codeDatas[curIdx++];
  465. });
  466. });
  467. });
  468. }
  469. const UTIL = Object.freeze({
  470. isDef,
  471. setTimeoutSync,
  472. getFee,
  473. getValueByKey,
  474. arrayToObj,
  475. validDepth,
  476. checkUnique,
  477. sortByNext,
  478. transformCalcBase,
  479. transformCalcBaseState,
  480. getCodeSheetData,
  481. setupCode
  482. });
  483. // 开始标签
  484. function _startTag(ele) {
  485. let rst = `<${ele.name}`;
  486. for (let attr of ele.attrs) {
  487. rst += ` ${attr.name}="${attr.value}"`;
  488. }
  489. rst += ele.children.length > 0 ? '>' : '/>';
  490. return rst;
  491. }
  492. // 结束标签
  493. function _endTag(ele) {
  494. return `</${ele.name}>`;
  495. }
  496. // 拼接成xml字符串
  497. function _toXMLStr(eles) {
  498. let rst = '';
  499. for (let ele of eles) {
  500. rst += _startTag(ele);
  501. if (ele.children.length > 0) {
  502. rst += _toXMLStr(ele.children);
  503. rst += _endTag(ele);
  504. }
  505. }
  506. return rst;
  507. }
  508. //格式化xml字符串
  509. function _formatXml(text) {
  510. //去掉多余的空格
  511. text = '\n' + text.replace(/>\s*?</g, ">\n<");
  512. //调整格式
  513. let rgx = /\n(<(([^\?]).+?)(?:\s|\s*?>|\s*?(\/)>)(?:.*?(?:(?:(\/)>)|(?:<(\/)\2>)))?)/mg;
  514. let nodeStack = [];
  515. let output = text.replace(rgx, function($0, all, name, isBegin, isCloseFull1, isCloseFull2, isFull1, isFull2){
  516. let isClosed = (isCloseFull1 === '/') || (isCloseFull2 === '/' ) || (isFull1 === '/') || (isFull2 === '/');
  517. let prefix = '';
  518. if (isBegin === '!') {
  519. prefix = getPrefix(nodeStack.length);
  520. } else {
  521. if (isBegin !== '/') {
  522. prefix = getPrefix(nodeStack.length);
  523. if (!isClosed) {
  524. nodeStack.push(name);
  525. }
  526. } else {
  527. nodeStack.pop();
  528. prefix = getPrefix(nodeStack.length);
  529. }
  530. }
  531. let ret = '\n' + prefix + all;
  532. return ret;
  533. });
  534. let outputText = output.substring(1);
  535. return outputText;
  536. function getPrefix(prefixIndex) {
  537. let span = ' ';
  538. let output = [];
  539. for (let i = 0 ; i < prefixIndex; ++i) {
  540. output.push(span);
  541. }
  542. return output.join('');
  543. }
  544. }
  545. // 将数据转换为xml文件并导出
  546. async function exportFile(originalDatas) {
  547. let fileDatas = [];
  548. //源数据转换成blob
  549. for (let orgData of originalDatas) {
  550. //转换成xml字符串
  551. let xmlStr = _toXMLStr([orgData.data]);
  552. //加上xml声明
  553. xmlStr = `<?xml version="1.0" encoding="utf-8"?>${xmlStr}`;
  554. //格式化
  555. xmlStr = _formatXml(xmlStr);
  556. let blob = new Blob([xmlStr], {type: 'text/plain;charset=utf-8'});
  557. fileDatas.push({blob: blob, fileName: orgData.fileName});
  558. }
  559. //导出文件
  560. if (fileDatas.length === 1) {
  561. saveAs(fileDatas[0].blob, fileDatas[0].fileName);
  562. } else if (fileDatas.length > 1) { //导出压缩包
  563. let zip = new JSZip();
  564. for (let file of fileDatas) {
  565. zip.file(file.fileName, file.blob, {binary: true});
  566. }
  567. let zipFile = await zip.generateAsync({type: 'blob'});
  568. saveAs(zipFile, '重庆标准交换数据.zip');
  569. }
  570. }
  571. return {
  572. CONFIG,
  573. CACHE,
  574. UTIL,
  575. Element,
  576. exportFile
  577. };
  578. })();