importBills.js 14 KB

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