base.js 30 KB

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