importBills.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2018/8/2
  7. * @version
  8. */
  9. /*
  10. * 清单导入模块,前端导入excel,进行数据提取,用lz-string进行压缩上传处理
  11. * */
  12. const importBills = (function () {
  13. //单元格数据是否存在
  14. function _isDef(data) {
  15. return typeof data !== 'undefined' && data !== null && data !== '';
  16. }
  17. //去除转义字符
  18. function _deESC(data) {
  19. return _isDef(data) ? data.toString().replace(/[\r\n\s\t]/g, '') : data;
  20. }
  21. function _deNR(data) {
  22. return _isDef(data) ? data.toString().replace(/[\r\n]/g, '') : data;
  23. }
  24. //find 返回最后匹配
  25. function findLast(datas, func) {
  26. let filter = datas.filter(func);
  27. if (filter.length > 0) {
  28. return filter[filter.length - 1];
  29. }
  30. return null;
  31. }
  32. const fileType = {
  33. gcl: 0, //工程量清单
  34. qdsl: 1, //清单示例
  35. gclex: 2, // 单机版导出的工程量清单预算表,需要转换成清单示例表来处理
  36. };
  37. //获取列字段对应
  38. function getColMapping(type) {
  39. if (type === 0) { //工程量清单
  40. return { code: 0, name: 1, unit: 2, quantity: 4, unitPrice: 5 };
  41. } else { //清单示例表
  42. return { code: 0, name: 1, unit: 2, quantity: 3, unitPrice: 4 };
  43. }
  44. }
  45. function isGCLHead(dataRow, nextDataRow) {
  46. const cell = dataRow[0];
  47. const nextCell = nextDataRow && nextDataRow[0];
  48. return cell && cell.value === '工程量清单' && (!nextCell || !/建设项目名称/.test(nextCell.value)); // 兼容招清单01-1表
  49. }
  50. function isGCLExtendHead(dataRow, nextDataRow) {
  51. const cell = dataRow[0];
  52. const nextCell = nextDataRow && nextDataRow[0];
  53. if ((cell && cell.value === '工程量清单预算表') || (nextCell && /建设项目名称/.test(nextCell.value))) { // 兼容招清单01-1表
  54. return true;
  55. }
  56. }
  57. //分析文件,1、工程量清单 2、清单示例表
  58. function getFileType(sheetData) {
  59. let dataTable = sheetData.data.dataTable,
  60. rowCount = sheetData.rowCount;
  61. for (let row = 0; row < rowCount; row++) {
  62. if (isGCLHead(dataTable[row], dataTable[row + 1])) {
  63. return fileType.gcl;
  64. }
  65. if (isGCLExtendHead(dataTable[row], dataTable[row + 1])) {
  66. return fileType.gclex;
  67. }
  68. }
  69. return fileType.qdsl;
  70. }
  71. //提取工程量清单数据
  72. //层级由depth确定,表格里最顶层depth为0(表头里一清单),表格内容里数据的depth为空格数+1
  73. function extractGCLDatas(sheetData, colMapping) {
  74. let dataTable = sheetData.data.dataTable,
  75. rowCount = sheetData.rowCount;
  76. let rst = [];
  77. for (let row = 0; row < rowCount; row++) {
  78. //表格中顶层节点
  79. if (isGCLHead(dataTable[row], dataTable[row + 1])) {
  80. let rootRow = dataTable[row + 2];
  81. let name = rootRow[0].value ? _deNR(rootRow[0].value) : '';
  82. let existsRoot = findLast(rst, x => x.name === name && x.depth === 0);
  83. if (!existsRoot) {
  84. let root = {
  85. ID: uuid.v1(),
  86. NextSiblingID: -1,
  87. ParentID: -1,
  88. name: name,
  89. depth: 0,
  90. parent: null,
  91. unitPriceAnalysis: 1
  92. };
  93. let preData = findLast(rst, x => x.depth === root.depth);
  94. if (preData) {
  95. preData.NextSiblingID = root.ID;
  96. }
  97. rst.push(root);
  98. }
  99. row += 3;
  100. continue;
  101. }
  102. let code = dataTable[row][colMapping.code] ? dataTable[row][colMapping.code].value : null,
  103. name = dataTable[row][colMapping.name] ? _deNR(dataTable[row][colMapping.name].value) : null,
  104. unit = dataTable[row][colMapping.unit] ? dataTable[row][colMapping.unit].value : null,
  105. quantity = dataTable[row][colMapping.quantity] ? dataTable[row][colMapping.quantity].value : null,
  106. unitPrice = dataTable[row][colMapping.unitPrice] ? dataTable[row][colMapping.unitPrice].value : null;
  107. if (!code && !name || /合计/.test(code)) { //过滤掉同时没有编号和名称的、过滤合计行
  108. continue;
  109. }
  110. // “子目号”、“单位”、“数量”都为空,“子目名称”不为空时,应将此行清单名称合并到上一行
  111. let lastData = rst[rst.length - 1];
  112. if (!code && !unit && !quantity && name) {
  113. lastData.name += name;
  114. continue;
  115. }
  116. //表格内的数据
  117. code = String(code);
  118. let depth = getDepth(code);
  119. let data = {
  120. ID: uuid.v1(),
  121. NextSiblingID: -1,
  122. ParentID: -1,
  123. code: code,
  124. name: name,
  125. unit: unit,
  126. quantity: quantity,
  127. depth: depth,
  128. unitPriceAnalysis: 1,
  129. };
  130. if (+unitPrice && +quantity) {
  131. unitPrice = scMathUtil.roundForObj(unitPrice, decimalObj.bills.unitPrice);
  132. const totalPrice = scMathUtil.roundForObj(unitPrice * +quantity, decimalObj.bills.totalPrice);
  133. data.fees = [
  134. {
  135. fieldName: "common",
  136. tenderTotalFee: totalPrice,
  137. tenderUnitFee: unitPrice,
  138. totalFee: totalPrice,
  139. unitFee: unitPrice,
  140. }
  141. ];
  142. data.calcFlag = treeNodeCalcFlag.customUnitPrice;
  143. }
  144. //获取data的父节点链,成为兄弟节点,只能在父链里找前兄弟(不能跨父链)
  145. let parents = getParents(lastData);
  146. let preData = findLast(parents, x => x.depth === depth);
  147. if (preData) {
  148. preData.NextSiblingID = data.ID;
  149. data.ParentID = preData.ParentID;
  150. data.parent = preData.parent;
  151. } else {
  152. data.ParentID = lastData.ID;
  153. data.parent = lastData;
  154. }
  155. rst.push(data);
  156. }
  157. console.log(rst);
  158. return rst;
  159. function getDepth(code) {
  160. if (!code) {
  161. return 1;
  162. }
  163. let match = code.match(/\s/g);
  164. return match ? match.length + 1 : 1;
  165. }
  166. }
  167. function getParents(data) {
  168. let rst = [];
  169. let parent = data.parent;
  170. while (parent) {
  171. rst.push(parent);
  172. parent = parent.parent;
  173. }
  174. rst.push(data);
  175. return rst;
  176. }
  177. //获取编号前缀: 101-1 => 101 101-1-1 => 101-1
  178. function getPrefix(v) {
  179. if (!v) {
  180. return null;
  181. }
  182. let reg = /(.*)-/;
  183. let match = reg.exec(v);
  184. return match ? match[1] : null;
  185. }
  186. // 示例列映射
  187. const slColMap = { code: 0, name: 1, unit: 2, quantity: 3, unitPrice: 4 };
  188. function isValidGCLExRow(rowData) {
  189. if (rowData[0] && /编制[::]/.test(rowData[0].value)) {
  190. return false;
  191. }
  192. if (rowData[1] && /合计/.test(rowData[1].value)) {
  193. return false;
  194. }
  195. if ((!rowData[slColMap.code] || !rowData[slColMap.code].value) &&
  196. (!rowData[slColMap.name] || !rowData[slColMap.name].value) &&
  197. (!rowData[slColMap.unit] || !rowData[slColMap.unit].value) &&
  198. (!rowData[slColMap.quantity] || !rowData[slColMap.quantity].value)) {
  199. return false;
  200. }
  201. return true;
  202. }
  203. // 将“工程量清单预算表”去掉表头表尾,并转换成清单示例表。
  204. // 工程量清单预算表的格式可参考需求:BUG #3037
  205. function transformGCLExToSL(sheetData) {
  206. const rst = {
  207. data: { dataTable: [] },
  208. rowCount: 0,
  209. };
  210. const dataTable = sheetData.data.dataTable;
  211. const rowCount = sheetData.rowCount;
  212. let preRootName;
  213. for (let row = 0; row < rowCount; row++) {
  214. const rowData = dataTable[row];
  215. if (isGCLExtendHead(rowData, dataTable[row + 1])) {
  216. const rootRowdata = dataTable[row + 3];
  217. const name = rootRowdata[0].value;
  218. if (name) {
  219. const rootName = name.replace('工程量清单', '清单');
  220. if (rootName !== preRootName) {
  221. preRootName = rootName;
  222. rst.data.dataTable.push({
  223. [slColMap.name]: { value: rootName }
  224. });
  225. }
  226. }
  227. row += 4;
  228. continue;
  229. }
  230. if (isValidGCLExRow(rowData)) {
  231. const cellData = {
  232. [slColMap.code]: { value: rowData[slColMap.code] && rowData[slColMap.code].value || null },
  233. [slColMap.name]: { value: rowData[slColMap.name] && rowData[slColMap.name].value || null },
  234. [slColMap.unit]: { value: rowData[slColMap.unit] && rowData[slColMap.unit].value || null },
  235. [slColMap.quantity]: { value: rowData[slColMap.quantity] && rowData[slColMap.quantity].value || null },
  236. [slColMap.unitPrice]: { value: rowData[slColMap.unitPrice] && rowData[slColMap.unitPrice].value || null },
  237. };
  238. rst.data.dataTable.push(cellData);
  239. }
  240. }
  241. rst.rowCount = rst.data.dataTable.length;
  242. return rst;
  243. }
  244. //提取清单示例数据
  245. function extractSLDatas(sheetData) {
  246. let dataTable = sheetData.data.dataTable,
  247. rowCount = sheetData.rowCount;
  248. let rst = [];
  249. let curRoot = null;
  250. for (let row = 0; row < rowCount; row++) {
  251. let code = dataTable[row][slColMap.code] && dataTable[row][slColMap.code].value ? String(dataTable[row][slColMap.code].value).trim() : null,
  252. name = dataTable[row][slColMap.name] ? _deNR(dataTable[row][slColMap.name].value) : null,
  253. unit = dataTable[row][slColMap.unit] ? dataTable[row][slColMap.unit].value : null,
  254. quantity = dataTable[row][slColMap.quantity] ? dataTable[row][slColMap.quantity].value : null,
  255. unitPrice = dataTable[row][slColMap.unitPrice] ? dataTable[row][slColMap.unitPrice].value : null;
  256. if (!code) { //没有编号的数据,名称必须为:清单 第xx章,认为新的表根节点
  257. const reg = /清单\s+第[^章]+章/;
  258. //if (name && /清单 第\d+章/.test(name)) {
  259. if (name && reg.test(name)) {
  260. curRoot = {
  261. code: null,
  262. name: name,
  263. ID: uuid.v1(),
  264. ParentID: -1,
  265. NextSiblingID: -1,
  266. parent: null,
  267. unitPriceAnalysis: 1
  268. };
  269. rst.push(curRoot);
  270. } else {
  271. curRoot = null;
  272. }
  273. } else if (!curRoot) { //根节点为无效根节点,其下子数据全部过滤掉
  274. continue;
  275. } else {
  276. //有code且有有效表根节点
  277. let prefix = getPrefix(code);
  278. let data = {
  279. code: code,
  280. name: name,
  281. unit: unit,
  282. quantity: quantity,
  283. ID: uuid.v1(),
  284. NextSiblingID: -1,
  285. unitPriceAnalysis: 1
  286. };
  287. if (+unitPrice && +quantity) {
  288. unitPrice = scMathUtil.roundForObj(unitPrice, decimalObj.bills.unitPrice);
  289. const totalPrice = scMathUtil.roundForObj(unitPrice * +quantity, decimalObj.bills.totalPrice);
  290. data.fees = [
  291. {
  292. fieldName: "common",
  293. tenderTotalFee: totalPrice,
  294. tenderUnitFee: unitPrice,
  295. totalFee: totalPrice,
  296. unitFee: unitPrice,
  297. }
  298. ];
  299. data.calcFlag = treeNodeCalcFlag.customUnitPrice;
  300. }
  301. let lastData = rst[rst.length - 1];
  302. let parents = getParents(lastData);
  303. //某数据编号为此数据的前缀,则某数据为此数据的父节点
  304. let parentData = findLast(parents, x => prefix === x.code);
  305. if (!parentData && prefix === '') { // -x的数据,在父链上找不到编号与prefix相同的数据时,父链上-x的数据,则这两数据为兄弟节点,没有则上一行数据为其父节点
  306. let samePrefixData = findLast(parents, x => getPrefix(x.code) === prefix);
  307. parentData = samePrefixData ? samePrefixData.parent : lastData;
  308. } else if (!parentData && prefix !== '') { //不是-x的数据,在父链上找不到编号与prefix相同的数据时,表根节点为其父节点
  309. parentData = curRoot;
  310. }
  311. data.ParentID = parentData.ID;
  312. data.parent = parentData;
  313. let preData = findLast(parents, x => x.ParentID === data.ParentID);
  314. if (preData) {
  315. preData.NextSiblingID = data.ID;
  316. }
  317. rst.push(data);
  318. }
  319. }
  320. console.log(rst);
  321. return rst;
  322. }
  323. function extactDatas(sheets) {
  324. let rst = [];
  325. let curSheetType = null;
  326. for (let sheetName in sheets) {
  327. let sheetData = sheets[sheetName];
  328. if (!sheetData.data.dataTable) {
  329. continue;
  330. }
  331. let sheetType = getFileType(sheetData);
  332. if (curSheetType !== null && sheetType !== curSheetType) {
  333. throw 'excel文件中存在不同格式的表格。';
  334. }
  335. curSheetType = sheetType;
  336. let colMapping = getColMapping(sheetType);
  337. let datas = [];
  338. if (sheetType === fileType.gcl) {
  339. datas = extractGCLDatas(sheetData, colMapping);
  340. } else if (sheetType === fileType.qdsl) {
  341. datas = extractSLDatas(sheetData, colMapping);
  342. } else {
  343. const slSheetData = transformGCLExToSL(sheetData);
  344. datas = extractSLDatas(slSheetData, colMapping);
  345. }
  346. rst = rst.concat(datas);
  347. }
  348. //编号去除空格 清除多余数据 设置数据
  349. for (let data of rst) {
  350. if (data.code && typeof data.code === 'string') {
  351. data.code = data.code.replace(/\s/g, '');
  352. }
  353. if (data.unit === '㎡') {
  354. data.unit = 'm2';
  355. } else if (data.unit === 'm³') {
  356. data.unit = 'm3';
  357. }
  358. data.projectID = projectObj.project.ID();
  359. data.type = billType.BILL;
  360. delete data.parent;
  361. delete data.depth;
  362. }
  363. //将表根节点的ParentID设置成第100章至700章清单的ID
  364. let fixedBill = projectObj.project.Bills.tree.roots.find(node => node.data &&
  365. node.data.flagsIndex && node.data.flagsIndex.fixed && node.data.flagsIndex.fixed.flag === fixedFlag.ONE_SEVEN_BILLS);
  366. let rootDatas = rst.filter(data => data.ParentID === -1);
  367. for (let root of rootDatas) {
  368. root.ParentID = fixedBill.data.ID;
  369. }
  370. //清单 第100章 总则清单需要加上固定ID
  371. let oneHundredBills = rootDatas.find(data => data.name && /第100章/.test(data.name));
  372. if (oneHundredBills) {
  373. oneHundredBills.flags = [{ flag: fixedFlag.ONE_HUNDRED_BILLS, fieldName: 'fixed' }];
  374. }
  375. return rst;
  376. }
  377. return { extactDatas }
  378. })();