importBills.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. //列名对应中文字符
  22. const colText = {
  23. serialNo: ['序号'],
  24. code: ['编码', '项目编码'],
  25. name: ['名称', '项目名称'],
  26. itemCharacterText: ['特征', '项目特征'],
  27. unit: ['单位', '计量单位'],
  28. quantity: ['工程量', '项目工程量'],
  29. money: ['金额'],
  30. quantityDetail: ['工程量明细'],
  31. feeDetail: ['费用明细'],
  32. summation: ['合计', '本页小计'],
  33. };
  34. //导入位置对应清单固定标记
  35. const positionFlag = {
  36. fbfx: fixedFlag.SUB_ENGINERRING,
  37. jscsxm: fixedFlag.CONSTRUCTION_TECH,
  38. zzcsxm: fixedFlag.CONSTRUCTION_ORGANIZATION,
  39. };
  40. //上传类型
  41. const uploadType = {
  42. lj: 'lj',
  43. gld: 'gld',
  44. };
  45. //设置导入表内容(选择导入位置)
  46. //@param {Object}workBook
  47. function setImportSheetsInfo(sheets){
  48. let sheetNames = [];
  49. let indexMapping = {};
  50. for(let sheetName in sheets){
  51. indexMapping[sheets[sheetName]['index']] = sheetName;
  52. }
  53. let sheetsCount = Object.keys(sheets).length;
  54. for(let i = 0; i < sheetsCount; i++){
  55. sheetNames.push(indexMapping[i]);
  56. }
  57. let sheetArea = $('#uploadSheets');
  58. $('#uploadSheets').height('');
  59. sheetArea.empty();
  60. for(let sheetName of sheetNames){
  61. let sheetDiv = $(`<div style="margin-left: 5px;margin-top: 5px;" class="input-group form-check"><label class="form-check-label" style="width:270px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis">
  62. <input class="form-check-input" type="checkbox">${sheetName}</label></div>`);
  63. sheetDiv.find('input[type="checkbox"]').click(function () {
  64. if($('#uploadAlert').is(':visible')){
  65. $('#uploadAlert').hide();
  66. }
  67. });
  68. let sel = $(`<select style="margin-left: 5px; border-radius: .20rem;"><option value="fbfx">分部分项工程</option><option value="zzcsxm">施工组织措施项目</option><option value="jscsxm">施工技术措施项目</option></select>`);
  69. if(sheetName.includes('分部分项工程项目清单计价表')){
  70. sheetDiv.find('input[type="checkbox"]').prop('checked', true);
  71. sel.find('option:eq(0)').prop('selected', true);
  72. }
  73. else if(sheetName.includes('施工组织措施项目清单计价表')){
  74. sheetDiv.find('input[type="checkbox"]').prop('checked', true);
  75. sel.find('option:eq(1)').prop('selected', true);
  76. }
  77. else if(sheetName.includes('施工技术措施项目清单计价表')){
  78. sheetDiv.find('input[type="checkbox"]').prop('checked', true);
  79. sel.find('option:eq(2)').prop('selected', true);
  80. }
  81. sheetDiv.append(sel);
  82. sheetArea.append(sheetDiv);
  83. }
  84. if(sheetNames.length > 0){
  85. sheetArea.show();
  86. }
  87. if($('#uploadSheets').height() > 250){
  88. sheetArea.css('overflow', 'auto');
  89. sheetArea.height(250);
  90. }
  91. else {
  92. sheetArea.css('overflow', 'hidden');
  93. }
  94. }
  95. //获得选择导入的表信息(表索引及导入位置)
  96. //@return {Object}
  97. function getImportSheetsInfo(){
  98. let rst = [];
  99. let sheetArea = $('#uploadSheets');
  100. let checkedInputs = sheetArea.find('input:checked');
  101. for(let checked of checkedInputs){
  102. rst.push({index: $(checked).parent().parent().index(), position: $(checked).parent().next().select().val()})
  103. }
  104. return rst;
  105. }
  106. function getSheetByIndex(sheets, index){
  107. for(let sheetName in sheets){
  108. let sheet = sheets[sheetName];
  109. if(sheet.index === index){
  110. return sheet;
  111. }
  112. }
  113. return null;
  114. }
  115. //提取excel表头列名与列下标映射
  116. //@
  117. function getColMapping(dataTable){
  118. //获取表头
  119. function getHeadRow(dataTable){
  120. for(let rowIdx in dataTable ){
  121. for(let colIdx in dataTable[rowIdx]){
  122. let cellData = dataTable[rowIdx][colIdx]['value'];
  123. if(cellData && _deESC(cellData) === colText.serialNo[0]){
  124. return dataTable[rowIdx];
  125. }
  126. }
  127. }
  128. return {};
  129. }
  130. //获取需要的表头列与列号对应关系
  131. let colMapping = {};
  132. let headRow = getHeadRow(dataTable);
  133. for(let colIdx in headRow){
  134. let cellData = headRow[colIdx]['value'];
  135. if(!_isDef(cellData)){
  136. continue;
  137. }
  138. //序号
  139. if(colMapping.serialNo === undefined && cellData === colText.serialNo[0]){
  140. colMapping.serialNo = colIdx;
  141. }
  142. //编码
  143. else if(colMapping.code === undefined && (cellData === colText.code[0] || cellData === colText.code[1])){
  144. colMapping.code = colIdx;
  145. }
  146. //名称
  147. else if(colMapping.name === undefined && (cellData === colText.name[0] || cellData === colText.name[1])){
  148. colMapping.name = colIdx;
  149. }
  150. //项目特征
  151. else if(colMapping.itemCharacterText === undefined && (cellData === colText.itemCharacterText[0] || cellData === colText.itemCharacterText[1])){
  152. colMapping.itemCharacterText = colIdx;
  153. }
  154. //单位
  155. else if(colMapping.unit === undefined && (cellData === colText.unit[0] || cellData === colText.unit[1])){
  156. colMapping.unit = colIdx;
  157. }
  158. //工程量
  159. else if(colMapping.quantity === undefined && (cellData === colText.quantity[0] || cellData === colText.quantity[1])){
  160. colMapping.quantity = colIdx;
  161. }
  162. //金额
  163. else if(colMapping.money === undefined && cellData.includes(colText.money[0])){
  164. colMapping.money = colIdx;
  165. }
  166. //工程量明细
  167. else if(colMapping.quantityDetail === undefined && cellData === colText.quantityDetail[0]){
  168. colMapping.quantityDetail = colIdx;
  169. }
  170. //费用明细
  171. else if(colMapping.feeDetail === undefined && cellData === colText.feeDetail[0]){
  172. colMapping.feeDetail = colIdx;
  173. }
  174. }
  175. return colMapping;
  176. }
  177. //是否是有效的表头列格式,只要含有各表需要的列就行,不严格控制多少列
  178. function isValidSheet(colMapping, fileType){
  179. function hasField(field, all){
  180. for(let i of all){
  181. if(field === i){
  182. return true;
  183. }
  184. }
  185. return false;
  186. }
  187. let needFields;
  188. if(fileType === uploadType.lj){
  189. //09表:序号、项目编码、项目名称、项目特征、计量单位、工程量、金额
  190. needFields = ['serialNo', 'code', 'name', 'money'];
  191. }
  192. else {
  193. //广联达表:序号、项目编码、项目名称、项目特征、计量单位、工程量、工程量明细、费用明细
  194. needFields = ['serialNo', 'code', 'name', 'itemCharacterText', 'unit', 'quantity', 'quantityDetail', 'feeDetail'];
  195. }
  196. let hasFieldCount = 0;
  197. for(let attr in colMapping){
  198. if(hasField(attr, needFields)){
  199. hasFieldCount++;
  200. }
  201. }
  202. return hasFieldCount === needFields.length;
  203. }
  204. //获取要无效和有效导入表
  205. //@param {Array}importSheetInfo 勾选要导入的表
  206. function getImportSheets(sheets, sheetsInfo, fileType){
  207. let rst = {invalidSheets: [], validSheets: {fbfx: [], jscsxm: [], zzcsxm: []}};
  208. for(let sheetInfo of sheetsInfo){
  209. let sheet = getSheetByIndex(sheets, sheetInfo.index);
  210. if(!sheet){
  211. continue;
  212. }
  213. //没有数据
  214. if(!sheet.data.dataTable){
  215. rst.invalidSheets.push(sheet.name);
  216. continue;
  217. }
  218. //获取表的列设置确定导入的格式是否合法(09、广联达)
  219. let colMapping = getColMapping(sheet.data.dataTable);
  220. if(!isValidSheet(colMapping, fileType)){
  221. rst.invalidSheets.push(sheet.name);
  222. continue;
  223. }
  224. //合法的表
  225. sheet.colMapping = colMapping;
  226. //将合法的表按导入位置分类当做一个表来处理
  227. if(rst.validSheets[sheetInfo.position] !== undefined){
  228. rst.validSheets[sheetInfo.position].push(sheet)
  229. }
  230. }
  231. return rst;
  232. }
  233. //行存在数据
  234. function rowExistData(rowData){
  235. for(let colIdx in rowData){
  236. let colData = rowData[colIdx]['value'];
  237. if(_isDef(colData)){
  238. return true;
  239. }
  240. }
  241. return false;
  242. }
  243. //提取excel表数据中的有效数据(去表头表尾,提取其中的excel数据)(根据fixedBill获取栏头占行数)
  244. function getValidImportData(colMapping, sheetData){
  245. let withingD = false;
  246. let validData = [];
  247. function isHead(rData){
  248. return rData[colMapping.serialNo] && _deESC(rData[colMapping.serialNo]['value']) === colText.serialNo[0];
  249. }
  250. function isTail(rowData){
  251. for(let colIdx in rowData){
  252. let colData = rowData[colIdx]['value'];
  253. if(colData){
  254. let trimColData= _deESC(colData);
  255. if(trimColData === colText.summation[0] || trimColData === colText.summation[1]){
  256. return true;
  257. }
  258. }
  259. }
  260. return false;
  261. }
  262. for(let rowIdx in sheetData){
  263. let rowData = sheetData[rowIdx];
  264. if(isHead(rowData)){
  265. withingD = true;
  266. continue;
  267. }
  268. else if(isTail(rowData)){
  269. withingD = false;
  270. }
  271. if(withingD && rowExistData(rowData)){
  272. validData.push(rowData);
  273. }
  274. }
  275. return validData;
  276. }
  277. //excel数据转换成清单数据
  278. function parseToBillData(validData, colMapping, flag, projectID){
  279. let rst = [];
  280. let billIdx = {};
  281. let preRootID = -1,
  282. preLeafID = -1,
  283. preID = -1;
  284. //父节点:1.无序号 2有编码
  285. function isRoot(rData){
  286. //序号和编码去除转义字符(有的表格单元格看起来是没数据,实际含有\r,\n等数据)
  287. let serialNo = rData[colMapping.serialNo] ? _deESC(rData[colMapping.serialNo]['value']) : '';
  288. let code = rData[colMapping.code] ? _deESC(rData[colMapping.code]['value']) : '';
  289. return !_isDef(serialNo) && _isDef(code);
  290. }
  291. //子节点:有序号
  292. function isLeaf(rData){
  293. let serialNo = rData[colMapping.serialNo] ? _deESC(rData[colMapping.serialNo]['value']) : '';
  294. return _isDef(serialNo);
  295. }
  296. //续数据:1. 前数据有效 2.无序号 3.无编码 4.有名称或特征
  297. function isExtend(preData, rData){
  298. let serialNo = rData[colMapping.serialNo] ? _deESC(rData[colMapping.serialNo]['value']) : '';
  299. let code = rData[colMapping.code] ? _deESC(rData[colMapping.code]['value']) : '';
  300. let name = rData[colMapping.name] ? _deESC(rData[colMapping.name]['value']) : '';
  301. let itemCharacterText = rData[colMapping.itemCharacterText] ? _deESC(rData[colMapping.itemCharacterText]['value']) : '';
  302. return _isDef(preData) && (isRoot(preData) || isLeaf(preData)) && !_isDef(serialNo) && !_isDef(code) && (_isDef(name) || _isDef(itemCharacterText));
  303. }
  304. function getBillType(rData, flag){
  305. if(flag === fixedFlag.CONSTRUCTION_TECH || flag === fixedFlag.CONSTRUCTION_ORGANIZATION){
  306. return billType.BILL;
  307. }
  308. else if(flag === fixedFlag.SUB_ENGINERRING){
  309. return isLeaf(rData) ? billType.FX : billType.FB;
  310. }
  311. return null;
  312. }
  313. //excel数据与标准库数据匹配,根据清单前九位编码匹配,匹配成功则获取标准清单对应的工程专业、特征及内容
  314. /*function matchStdBill(excelBill, stdData){
  315. let isMatch = false;
  316. let regExp = /^\d{12}$/g;
  317. if(regExp.test(excelBill.code)){
  318. let nineCode = excelBill.code.substr(0, 9);
  319. for(let stdBill of stdData.stdBills){
  320. //set programID
  321. if(nineCode == stdBill.code){
  322. isMatch = true;
  323. excelBill.programID = stdBill.engineering ? stdBill.engineering : null;
  324. excelBill.billsLibId = stdBill.billsLibId ? stdBill.billsLibId : null;
  325. //set jobContent and itemCharacter
  326. let tempJob = [], tempCharacter = [];
  327. for(let billJob of stdBill.jobs){
  328. for(let stdJob of stdData.stdJobs) {
  329. if (billJob.id == stdJob.id) {
  330. tempJob.push({isChecked: false, serialNo: billJob.serialNo, content: stdJob.content});
  331. }
  332. }
  333. }
  334. for(let billCharacter of stdBill.items){
  335. for(let stdCharacter of stdData.stdCharacters){
  336. if(billCharacter.id == stdCharacter.id){
  337. let eigenvalue = [];
  338. for(let eValue of stdCharacter.itemValue){
  339. eigenvalue.push({isSelected: false, value: eValue.value});
  340. }
  341. tempCharacter.push({isChecked: false, serialNo: billCharacter.serialNo, character: stdCharacter.content, eigenvalue: eigenvalue});
  342. }
  343. }
  344. }
  345. excelBill.jobContent = tempJob;
  346. excelBill.itemCharacter = tempCharacter;
  347. }
  348. }
  349. }
  350. if(!isMatch && excelBill.type === billType.FX){//分项不为空,同时在标准清单中不匹配,则识别为补项
  351. excelBill.type = billType.BX;
  352. }
  353. }*/
  354. for(let r = 0; r < validData.length; r++){
  355. let preData = validData[r-1],
  356. rData = validData[r];
  357. if(flag == fixedFlag.CONSTRUCTION_TECH && rData[colMapping.name] && rData[colMapping.name]['value'] === '施工技术措施项目'
  358. || flag == fixedFlag.CONSTRUCTION_ORGANIZATION && rData[colMapping.name] && rData[colMapping.name]['value'] === '施工组织措施项目'){
  359. continue;
  360. }
  361. //过滤无效数据
  362. if(!isRoot(rData) && !isLeaf(rData) && !isExtend(preData, rData)){
  363. continue;
  364. }
  365. if(isExtend(preData, rData)){
  366. let preBill = billIdx[preID];
  367. if(preBill){
  368. //合并续数据
  369. preBill.code += rData[colMapping.code] && rData[colMapping.code]['value'] ? rData[colMapping.code]['value'] : '';
  370. preBill.name += rData[colMapping.name] && rData[colMapping.name]['value'] ? rData[colMapping.name]['value'] : '';
  371. preBill.itemCharacterText += rData[colMapping.itemCharacterText] && rData[colMapping.itemCharacterText]['value'] ? rData[colMapping.itemCharacterText]['value'] : '';
  372. preBill.unit += rData[colMapping.unit] && rData[colMapping.unit]['value'] ? rData[colMapping.unit]['value'] : '';
  373. preBill.quantity += rData[colMapping.quantity] && rData[colMapping.quantity]['value'] ? rData[colMapping.quantity]['value'] : '';
  374. }
  375. }
  376. else {
  377. let newID = uuid.v1();
  378. let pID = -1;
  379. let preBill = null;
  380. let nodeType = 'root';//后端以此标记来设置ParentID
  381. if(isRoot(rData)){
  382. //pID = 'fixedBillID';
  383. preBill = billIdx[preRootID];
  384. }
  385. else if(isLeaf(rData)){
  386. nodeType = 'leaf';
  387. //pID = preRootID !== -1 ? preRootID : fixedBill.ID;
  388. preBill = billIdx[preLeafID];
  389. }
  390. //set bill data
  391. billIdx[newID] = {
  392. nodeType: nodeType,
  393. ID: newID, ParentID: pID, NextSiblingID: -1,
  394. code: rData[colMapping.code] && rData[colMapping.code]['value'] ? _deESC(rData[colMapping.code]['value']) : '',
  395. name: rData[colMapping.name] && rData[colMapping.name]['value'] ? _deESC(rData[colMapping.name]['value']) : '',
  396. itemCharacterText: rData[colMapping.itemCharacterText] && rData[colMapping.itemCharacterText]['value'] ? rData[colMapping.itemCharacterText]['value'] : '',
  397. itemCharacter: [],
  398. jobContentText: '',
  399. jobContent: [],
  400. programID: null,
  401. unit: rData[colMapping.unit] && rData[colMapping.unit]['value'] ? rData[colMapping.unit]['value'] : '',
  402. quantity: rData[colMapping.quantity] && rData[colMapping.quantity]['value'] ? rData[colMapping.quantity]['value'] : '',
  403. //安全文明
  404. flags: flag === fixedFlag.CONSTRUCTION_ORGANIZATION && (rData[colMapping.name] && (rData[colMapping.name]['value'] === '安全文明施工专项费用' || rData[colMapping.name]['value'] === '安全文明施工费')) ?
  405. [{fieldName: 'fixed', flag: fixedFlag.SAFETY_CONSTRUCTION}] : [],
  406. fees: [],
  407. projectID: projectID,
  408. type: getBillType(rData, flag)};
  409. //match stdBill and reset programID、jobContent、itemCharacter
  410. //matchStdBill(billIdx[newID], stdData);
  411. //update preBill NextSibling
  412. if(preBill){
  413. preBill.NextSiblingID = newID;
  414. }
  415. //set new preID
  416. preID = newID;
  417. preRootID = isRoot(rData) ? newID : preRootID;
  418. preLeafID = isLeaf(rData) ? newID : preLeafID;
  419. }
  420. }
  421. for(let i in billIdx){
  422. rst.push(billIdx[i]);
  423. }
  424. return rst;
  425. }
  426. function getImportData(validSheets, projectID){
  427. let rst = {fbfx: [], jscsxm: [], zzcsxm: []};
  428. let validSheetsDatas = [];
  429. for(let uploadPosition in validSheets){
  430. let validExcelData = [];
  431. for(let uSheet of validSheets[uploadPosition]){
  432. validExcelData = validExcelData.concat(getValidImportData(uSheet.colMapping, uSheet.data.dataTable));
  433. }
  434. if(validSheets[uploadPosition].length > 0){
  435. validSheetsDatas.push({position: uploadPosition, colMapping: validSheets[uploadPosition][0].colMapping, validExcelData: validExcelData});
  436. }
  437. }
  438. console.log(validSheetsDatas);
  439. for(let validSheetData of validSheetsDatas){
  440. if(validSheetData.validExcelData.length > 0){
  441. rst[validSheetData.position] = parseToBillData(validSheetData.validExcelData, validSheetData.colMapping, positionFlag[validSheetData.position], projectID);
  442. }
  443. }
  444. console.log(rst);
  445. return rst;
  446. }
  447. function excelHasValidBills(importBillsData){
  448. for(let i in importBillsData){
  449. if(importBillsData[i].length > 0){
  450. return true;
  451. }
  452. }
  453. return false;
  454. }
  455. return {setImportSheetsInfo, getImportSheetsInfo, getImportSheets, getImportData, excelHasValidBills}
  456. })();