importBills.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 };
  41. } else { //清单示例表
  42. return { code: 0, name: 1, unit: 2, quantity: 3 };
  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 colMapping = {code: 0, name: 1, unit: 2, quantity: 4};
  75. let dataTable = sheetData.data.dataTable,
  76. rowCount = sheetData.rowCount;
  77. let rst = [];
  78. for (let row = 0; row < rowCount; row++) {
  79. //表格中顶层节点
  80. if (isGCLHead(dataTable[row], dataTable[row + 1])) {
  81. let rootRow = dataTable[row + 2];
  82. let name = rootRow[0].value ? _deNR(rootRow[0].value) : '';
  83. let existsRoot = findLast(rst, x => x.name === name && x.depth === 0);
  84. if (!existsRoot) {
  85. let root = {
  86. ID: uuid.v1(),
  87. NextSiblingID: -1,
  88. ParentID: -1,
  89. name: name,
  90. depth: 0,
  91. parent: null,
  92. unitPriceAnalysis: 1
  93. };
  94. let preData = findLast(rst, x => x.depth === root.depth);
  95. if (preData) {
  96. preData.NextSiblingID = root.ID;
  97. }
  98. rst.push(root);
  99. }
  100. row += 3;
  101. continue;
  102. }
  103. let code = dataTable[row][colMapping.code] ? dataTable[row][colMapping.code].value : null,
  104. name = dataTable[row][colMapping.name] ? _deNR(dataTable[row][colMapping.name].value) : null,
  105. unit = dataTable[row][colMapping.unit] ? dataTable[row][colMapping.unit].value : null,
  106. quantity = dataTable[row][colMapping.quantity] ? dataTable[row][colMapping.quantity].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. //获取data的父节点链,成为兄弟节点,只能在父链里找前兄弟(不能跨父链)
  131. let parents = getParents(lastData);
  132. let preData = findLast(parents, x => x.depth === depth);
  133. if (preData) {
  134. preData.NextSiblingID = data.ID;
  135. data.ParentID = preData.ParentID;
  136. data.parent = preData.parent;
  137. } else {
  138. data.ParentID = lastData.ID;
  139. data.parent = lastData;
  140. }
  141. rst.push(data);
  142. }
  143. console.log(rst);
  144. return rst;
  145. function getDepth(code) {
  146. if (!code) {
  147. return 1;
  148. }
  149. let match = code.match(/\s/g);
  150. return match ? match.length + 1 : 1;
  151. }
  152. }
  153. function getParents(data) {
  154. let rst = [];
  155. let parent = data.parent;
  156. while (parent) {
  157. rst.push(parent);
  158. parent = parent.parent;
  159. }
  160. rst.push(data);
  161. return rst;
  162. }
  163. //获取编号前缀: 101-1 => 101 101-1-1 => 101-1
  164. function getPrefix(v) {
  165. if (!v) {
  166. return null;
  167. }
  168. let reg = /(.*)-/;
  169. let match = reg.exec(v);
  170. return match ? match[1] : null;
  171. }
  172. // 示例列映射
  173. const slColMap = { code: 0, name: 1, unit: 2, quantity: 3 };
  174. function isValidGCLExRow(rowData) {
  175. if (rowData[0] && /编制[::]/.test(rowData[0].value)) {
  176. return false;
  177. }
  178. if (rowData[1] && /合计/.test(rowData[1].value)) {
  179. return false;
  180. }
  181. if ((!rowData[slColMap.code] || !rowData[slColMap.code].value) &&
  182. (!rowData[slColMap.name] || !rowData[slColMap.name].value) &&
  183. (!rowData[slColMap.unit] || !rowData[slColMap.unit].value) &&
  184. (!rowData[slColMap.quantity] || !rowData[slColMap.quantity].value)) {
  185. return false;
  186. }
  187. return true;
  188. }
  189. // 将“工程量清单预算表”去掉表头表尾,并转换成清单示例表。
  190. // 工程量清单预算表的格式可参考需求:BUG #3037
  191. function transformGCLExToSL(sheetData) {
  192. const rst = {
  193. data: { dataTable: [] },
  194. rowCount: 0,
  195. };
  196. const dataTable = sheetData.data.dataTable;
  197. const rowCount = sheetData.rowCount;
  198. let preRootName;
  199. for (let row = 0; row < rowCount; row++) {
  200. const rowData = dataTable[row];
  201. if (isGCLExtendHead(rowData, dataTable[row + 1])) {
  202. const rootRowdata = dataTable[row + 3];
  203. const name = rootRowdata[0].value;
  204. if (name) {
  205. const rootName = name.replace('工程量清单', '清单');
  206. if (rootName !== preRootName) {
  207. preRootName = rootName;
  208. rst.data.dataTable.push({
  209. [slColMap.name]: { value: rootName }
  210. });
  211. }
  212. }
  213. row += 4;
  214. continue;
  215. }
  216. if (isValidGCLExRow(rowData)) {
  217. const cellData = {
  218. [slColMap.code]: { value: rowData[slColMap.code] && rowData[slColMap.code].value || null },
  219. [slColMap.name]: { value: rowData[slColMap.name] && rowData[slColMap.name].value || null },
  220. [slColMap.unit]: { value: rowData[slColMap.unit] && rowData[slColMap.unit].value || null },
  221. [slColMap.quantity]: { value: rowData[slColMap.quantity] && rowData[slColMap.quantity].value || null },
  222. };
  223. rst.data.dataTable.push(cellData);
  224. }
  225. }
  226. rst.rowCount = rst.data.dataTable.length;
  227. return rst;
  228. }
  229. //提取清单示例数据
  230. function extractSLDatas(sheetData) {
  231. let dataTable = sheetData.data.dataTable,
  232. rowCount = sheetData.rowCount;
  233. let rst = [];
  234. let curRoot = null;
  235. for (let row = 0; row < rowCount; row++) {
  236. let code = dataTable[row][slColMap.code] && dataTable[row][slColMap.code].value ? String(dataTable[row][slColMap.code].value).trim() : null,
  237. name = dataTable[row][slColMap.name] ? _deNR(dataTable[row][slColMap.name].value) : null,
  238. unit = dataTable[row][slColMap.unit] ? dataTable[row][slColMap.unit].value : null,
  239. quantity = dataTable[row][slColMap.quantity] ? dataTable[row][slColMap.quantity].value : null;
  240. if (!code) { //没有编号的数据,名称必须为:清单 第xx章,认为新的表根节点
  241. const reg = /清单\s+第[^章]+章/;
  242. //if (name && /清单 第\d+章/.test(name)) {
  243. if (name && reg.test(name)) {
  244. curRoot = {
  245. code: null,
  246. name: name,
  247. ID: uuid.v1(),
  248. ParentID: -1,
  249. NextSiblingID: -1,
  250. parent: null,
  251. unitPriceAnalysis: 1
  252. };
  253. rst.push(curRoot);
  254. } else {
  255. curRoot = null;
  256. }
  257. } else if (!curRoot) { //根节点为无效根节点,其下子数据全部过滤掉
  258. continue;
  259. } else {
  260. //有code且有有效表根节点
  261. let prefix = getPrefix(code);
  262. let data = {
  263. code: code,
  264. name: name,
  265. unit: unit,
  266. quantity: quantity,
  267. ID: uuid.v1(),
  268. NextSiblingID: -1,
  269. unitPriceAnalysis: 1
  270. };
  271. let lastData = rst[rst.length - 1];
  272. let parents = getParents(lastData);
  273. //某数据编号为此数据的前缀,则某数据为此数据的父节点
  274. let parentData = findLast(parents, x => prefix === x.code);
  275. if (!parentData && prefix === '') { // -x的数据,在父链上找不到编号与prefix相同的数据时,父链上-x的数据,则这两数据为兄弟节点,没有则上一行数据为其父节点
  276. let samePrefixData = findLast(parents, x => getPrefix(x.code) === prefix);
  277. parentData = samePrefixData ? samePrefixData.parent : lastData;
  278. } else if (!parentData && prefix !== '') { //不是-x的数据,在父链上找不到编号与prefix相同的数据时,表根节点为其父节点
  279. parentData = curRoot;
  280. }
  281. data.ParentID = parentData.ID;
  282. data.parent = parentData;
  283. let preData = findLast(parents, x => x.ParentID === data.ParentID);
  284. if (preData) {
  285. preData.NextSiblingID = data.ID;
  286. }
  287. rst.push(data);
  288. }
  289. }
  290. console.log(rst);
  291. return rst;
  292. }
  293. function extactDatas(sheets) {
  294. let rst = [];
  295. let curSheetType = null;
  296. for (let sheetName in sheets) {
  297. let sheetData = sheets[sheetName];
  298. let sheetType = getFileType(sheetData);
  299. if (curSheetType !== null && sheetType !== curSheetType) {
  300. throw 'excel文件中存在不同格式的表格。';
  301. }
  302. curSheetType = sheetType;
  303. let colMapping = getColMapping(sheetType);
  304. let datas = [];
  305. if (sheetType === fileType.gcl) {
  306. datas = extractGCLDatas(sheetData, colMapping);
  307. } else if (sheetType === fileType.qdsl) {
  308. datas = extractSLDatas(sheetData, colMapping);
  309. } else {
  310. const slSheetData = transformGCLExToSL(sheetData);
  311. datas = extractSLDatas(slSheetData, colMapping);
  312. }
  313. rst = rst.concat(datas);
  314. }
  315. //编号去除空格 清除多余数据 设置数据
  316. for (let data of rst) {
  317. if (data.code && typeof data.code === 'string') {
  318. data.code = data.code.replace(/\s/g, '');
  319. }
  320. if (data.unit === '㎡') {
  321. data.unit = 'm2';
  322. } else if (data.unit === 'm³') {
  323. data.unit = 'm3';
  324. }
  325. data.projectID = projectObj.project.ID();
  326. data.type = billType.BILL;
  327. delete data.parent;
  328. delete data.depth;
  329. }
  330. //将表根节点的ParentID设置成第100章至700章清单的ID
  331. let fixedBill = projectObj.project.Bills.tree.roots.find(node => node.data &&
  332. node.data.flagsIndex && node.data.flagsIndex.fixed && node.data.flagsIndex.fixed.flag === fixedFlag.ONE_SEVEN_BILLS);
  333. let rootDatas = rst.filter(data => data.ParentID === -1);
  334. for (let root of rootDatas) {
  335. root.ParentID = fixedBill.data.ID;
  336. }
  337. //清单 第100章 总则清单需要加上固定ID
  338. let oneHundredBills = rootDatas.find(data => data.name && /第100章/.test(data.name));
  339. if (oneHundredBills) {
  340. oneHundredBills.flags = [{ flag: fixedFlag.ONE_HUNDRED_BILLS, fieldName: 'fixed' }];
  341. }
  342. return rst;
  343. }
  344. return { extactDatas }
  345. })();